1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25 /*
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 */
30 /*
31 * Copyright 2011 cyril.galibern@opensvc.com
32 */
33
34 /*
35 * SCSI disk target driver.
36 */
37 #include <sys/scsi/scsi.h>
38 #include <sys/dkbad.h>
39 #include <sys/dklabel.h>
40 #include <sys/dkio.h>
41 #include <sys/fdio.h>
42 #include <sys/cdio.h>
43 #include <sys/mhd.h>
44 #include <sys/vtoc.h>
45 #include <sys/dktp/fdisk.h>
46 #include <sys/kstat.h>
47 #include <sys/vtrace.h>
48 #include <sys/note.h>
49 #include <sys/thread.h>
50 #include <sys/proc.h>
51 #include <sys/efi_partition.h>
52 #include <sys/var.h>
53 #include <sys/aio_req.h>
54
55 #ifdef __lock_lint
56 #define _LP64
57 #define __amd64
58 #endif
59
60 #if (defined(__fibre))
61 /* Note: is there a leadville version of the following? */
62 #include <sys/fc4/fcal_linkapp.h>
63 #endif
64 #include <sys/taskq.h>
65 #include <sys/uuid.h>
66 #include <sys/byteorder.h>
67 #include <sys/sdt.h>
68
69 #include "sd_xbuf.h"
70
71 #include <sys/scsi/targets/sddef.h>
72 #include <sys/cmlb.h>
73 #include <sys/sysevent/eventdefs.h>
74 #include <sys/sysevent/dev.h>
75
76 #include <sys/fm/protocol.h>
77
78 /*
79 * Loadable module info.
80 */
81 #if (defined(__fibre))
82 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver"
83 #else /* !__fibre */
84 #define SD_MODULE_NAME "SCSI Disk Driver"
85 #endif /* !__fibre */
86
87 /*
88 * Define the interconnect type, to allow the driver to distinguish
89 * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
90 *
91 * This is really for backward compatibility. In the future, the driver
92 * should actually check the "interconnect-type" property as reported by
93 * the HBA; however at present this property is not defined by all HBAs,
94 * so we will use this #define (1) to permit the driver to run in
95 * backward-compatibility mode; and (2) to print a notification message
96 * if an FC HBA does not support the "interconnect-type" property. The
97 * behavior of the driver will be to assume parallel SCSI behaviors unless
98 * the "interconnect-type" property is defined by the HBA **AND** has a
99 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
100 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
101 * Channel behaviors (as per the old ssd). (Note that the
102 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
103 * will result in the driver assuming parallel SCSI behaviors.)
104 *
105 * (see common/sys/scsi/impl/services.h)
106 *
107 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
108 * since some FC HBAs may already support that, and there is some code in
109 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the
110 * default would confuse that code, and besides things should work fine
111 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
112 * "interconnect_type" property.
113 *
114 */
115 #if (defined(__fibre))
116 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE
117 #else
118 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL
119 #endif
120
121 /*
122 * The name of the driver, established from the module name in _init.
123 */
124 static char *sd_label = NULL;
125
126 /*
127 * Driver name is unfortunately prefixed on some driver.conf properties.
128 */
129 #if (defined(__fibre))
130 #define sd_max_xfer_size ssd_max_xfer_size
131 #define sd_config_list ssd_config_list
132 static char *sd_max_xfer_size = "ssd_max_xfer_size";
133 static char *sd_config_list = "ssd-config-list";
134 #else
135 static char *sd_max_xfer_size = "sd_max_xfer_size";
136 static char *sd_config_list = "sd-config-list";
137 #endif
138
139 /*
140 * Driver global variables
141 */
142
143 #if (defined(__fibre))
144 /*
145 * These #defines are to avoid namespace collisions that occur because this
146 * code is currently used to compile two separate driver modules: sd and ssd.
147 * All global variables need to be treated this way (even if declared static)
148 * in order to allow the debugger to resolve the names properly.
149 * It is anticipated that in the near future the ssd module will be obsoleted,
150 * at which time this namespace issue should go away.
151 */
152 #define sd_state ssd_state
153 #define sd_io_time ssd_io_time
154 #define sd_failfast_enable ssd_failfast_enable
155 #define sd_ua_retry_count ssd_ua_retry_count
156 #define sd_report_pfa ssd_report_pfa
157 #define sd_max_throttle ssd_max_throttle
158 #define sd_min_throttle ssd_min_throttle
159 #define sd_rot_delay ssd_rot_delay
160
161 #define sd_retry_on_reservation_conflict \
162 ssd_retry_on_reservation_conflict
163 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay
164 #define sd_resv_conflict_name ssd_resv_conflict_name
165
166 #define sd_component_mask ssd_component_mask
167 #define sd_level_mask ssd_level_mask
168 #define sd_debug_un ssd_debug_un
169 #define sd_error_level ssd_error_level
170
171 #define sd_xbuf_active_limit ssd_xbuf_active_limit
172 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit
173
174 #define sd_tr ssd_tr
175 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout
176 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout
177 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable
178 #define sd_check_media_time ssd_check_media_time
179 #define sd_wait_cmds_complete ssd_wait_cmds_complete
180 #define sd_label_mutex ssd_label_mutex
181 #define sd_detach_mutex ssd_detach_mutex
182 #define sd_log_buf ssd_log_buf
183 #define sd_log_mutex ssd_log_mutex
184
185 #define sd_disk_table ssd_disk_table
186 #define sd_disk_table_size ssd_disk_table_size
187 #define sd_sense_mutex ssd_sense_mutex
188 #define sd_cdbtab ssd_cdbtab
189
190 #define sd_cb_ops ssd_cb_ops
191 #define sd_ops ssd_ops
192 #define sd_additional_codes ssd_additional_codes
193 #define sd_tgops ssd_tgops
194
195 #define sd_minor_data ssd_minor_data
196 #define sd_minor_data_efi ssd_minor_data_efi
197
198 #define sd_tq ssd_tq
199 #define sd_wmr_tq ssd_wmr_tq
200 #define sd_taskq_name ssd_taskq_name
201 #define sd_wmr_taskq_name ssd_wmr_taskq_name
202 #define sd_taskq_minalloc ssd_taskq_minalloc
203 #define sd_taskq_maxalloc ssd_taskq_maxalloc
204
205 #define sd_dump_format_string ssd_dump_format_string
206
207 #define sd_iostart_chain ssd_iostart_chain
208 #define sd_iodone_chain ssd_iodone_chain
209
210 #define sd_pm_idletime ssd_pm_idletime
211
212 #define sd_force_pm_supported ssd_force_pm_supported
213
214 #define sd_dtype_optical_bind ssd_dtype_optical_bind
215
216 #define sd_ssc_init ssd_ssc_init
217 #define sd_ssc_send ssd_ssc_send
218 #define sd_ssc_fini ssd_ssc_fini
219 #define sd_ssc_assessment ssd_ssc_assessment
220 #define sd_ssc_post ssd_ssc_post
221 #define sd_ssc_print ssd_ssc_print
222 #define sd_ssc_ereport_post ssd_ssc_ereport_post
223 #define sd_ssc_set_info ssd_ssc_set_info
224 #define sd_ssc_extract_info ssd_ssc_extract_info
225
226 #endif
227
228 #ifdef SDDEBUG
229 int sd_force_pm_supported = 0;
230 #endif /* SDDEBUG */
231
232 void *sd_state = NULL;
233 int sd_io_time = SD_IO_TIME;
234 int sd_failfast_enable = 1;
235 int sd_ua_retry_count = SD_UA_RETRY_COUNT;
236 int sd_report_pfa = 1;
237 int sd_max_throttle = SD_MAX_THROTTLE;
238 int sd_min_throttle = SD_MIN_THROTTLE;
239 int sd_rot_delay = 4; /* Default 4ms Rotation delay */
240 int sd_qfull_throttle_enable = TRUE;
241
242 int sd_retry_on_reservation_conflict = 1;
243 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
244 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
245
246 static int sd_dtype_optical_bind = -1;
247
248 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
249 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict";
250
251 /*
252 * Global data for debug logging. To enable debug printing, sd_component_mask
253 * and sd_level_mask should be set to the desired bit patterns as outlined in
254 * sddef.h.
255 */
256 uint_t sd_component_mask = 0x0;
257 uint_t sd_level_mask = 0x0;
258 struct sd_lun *sd_debug_un = NULL;
259 uint_t sd_error_level = SCSI_ERR_RETRYABLE;
260
261 /* Note: these may go away in the future... */
262 static uint32_t sd_xbuf_active_limit = 512;
263 static uint32_t sd_xbuf_reserve_limit = 16;
264
265 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
266
267 /*
268 * Timer value used to reset the throttle after it has been reduced
269 * (typically in response to TRAN_BUSY or STATUS_QFULL)
270 */
271 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT;
272 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT;
273
274 /*
275 * Interval value associated with the media change scsi watch.
276 */
277 static int sd_check_media_time = 3000000;
278
279 /*
280 * Wait value used for in progress operations during a DDI_SUSPEND
281 */
282 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
283
284 /*
285 * sd_label_mutex protects a static buffer used in the disk label
286 * component of the driver
287 */
288 static kmutex_t sd_label_mutex;
289
290 /*
291 * sd_detach_mutex protects un_layer_count, un_detach_count, and
292 * un_opens_in_progress in the sd_lun structure.
293 */
294 static kmutex_t sd_detach_mutex;
295
296 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
297 sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
298
299 /*
300 * Global buffer and mutex for debug logging
301 */
302 static char sd_log_buf[1024];
303 static kmutex_t sd_log_mutex;
304
305 /*
306 * Structs and globals for recording attached lun information.
307 * This maintains a chain. Each node in the chain represents a SCSI controller.
308 * The structure records the number of luns attached to each target connected
309 * with the controller.
310 * For parallel scsi device only.
311 */
312 struct sd_scsi_hba_tgt_lun {
313 struct sd_scsi_hba_tgt_lun *next;
314 dev_info_t *pdip;
315 int nlun[NTARGETS_WIDE];
316 };
317
318 /*
319 * Flag to indicate the lun is attached or detached
320 */
321 #define SD_SCSI_LUN_ATTACH 0
322 #define SD_SCSI_LUN_DETACH 1
323
324 static kmutex_t sd_scsi_target_lun_mutex;
325 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
326
327 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
328 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
329
330 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
331 sd_scsi_target_lun_head))
332
333 /*
334 * "Smart" Probe Caching structs, globals, #defines, etc.
335 * For parallel scsi and non-self-identify device only.
336 */
337
338 /*
339 * The following resources and routines are implemented to support
340 * "smart" probing, which caches the scsi_probe() results in an array,
341 * in order to help avoid long probe times.
342 */
343 struct sd_scsi_probe_cache {
344 struct sd_scsi_probe_cache *next;
345 dev_info_t *pdip;
346 int cache[NTARGETS_WIDE];
347 };
348
349 static kmutex_t sd_scsi_probe_cache_mutex;
350 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
351
352 /*
353 * Really we only need protection on the head of the linked list, but
354 * better safe than sorry.
355 */
356 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
357 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
358
359 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
360 sd_scsi_probe_cache_head))
361
362 /*
363 * Power attribute table
364 */
365 static sd_power_attr_ss sd_pwr_ss = {
366 { "NAME=spindle-motor", "0=off", "1=on", NULL },
367 {0, 100},
368 {30, 0},
369 {20000, 0}
370 };
371
372 static sd_power_attr_pc sd_pwr_pc = {
373 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
374 "3=active", NULL },
375 {0, 0, 0, 100},
376 {90, 90, 20, 0},
377 {15000, 15000, 1000, 0}
378 };
379
380 /*
381 * Power level to power condition
382 */
383 static int sd_pl2pc[] = {
384 SD_TARGET_START_VALID,
385 SD_TARGET_STANDBY,
386 SD_TARGET_IDLE,
387 SD_TARGET_ACTIVE
388 };
389
390 /*
391 * Vendor specific data name property declarations
392 */
393
394 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
395
396 static sd_tunables seagate_properties = {
397 SEAGATE_THROTTLE_VALUE,
398 0,
399 0,
400 0,
401 0,
402 0,
403 0,
404 0,
405 0
406 };
407
408
409 static sd_tunables fujitsu_properties = {
410 FUJITSU_THROTTLE_VALUE,
411 0,
412 0,
413 0,
414 0,
415 0,
416 0,
417 0,
418 0
419 };
420
421 static sd_tunables ibm_properties = {
422 IBM_THROTTLE_VALUE,
423 0,
424 0,
425 0,
426 0,
427 0,
428 0,
429 0,
430 0
431 };
432
433 static sd_tunables purple_properties = {
434 PURPLE_THROTTLE_VALUE,
435 0,
436 0,
437 PURPLE_BUSY_RETRIES,
438 PURPLE_RESET_RETRY_COUNT,
439 PURPLE_RESERVE_RELEASE_TIME,
440 0,
441 0,
442 0
443 };
444
445 static sd_tunables sve_properties = {
446 SVE_THROTTLE_VALUE,
447 0,
448 0,
449 SVE_BUSY_RETRIES,
450 SVE_RESET_RETRY_COUNT,
451 SVE_RESERVE_RELEASE_TIME,
452 SVE_MIN_THROTTLE_VALUE,
453 SVE_DISKSORT_DISABLED_FLAG,
454 0
455 };
456
457 static sd_tunables maserati_properties = {
458 0,
459 0,
460 0,
461 0,
462 0,
463 0,
464 0,
465 MASERATI_DISKSORT_DISABLED_FLAG,
466 MASERATI_LUN_RESET_ENABLED_FLAG
467 };
468
469 static sd_tunables pirus_properties = {
470 PIRUS_THROTTLE_VALUE,
471 0,
472 PIRUS_NRR_COUNT,
473 PIRUS_BUSY_RETRIES,
474 PIRUS_RESET_RETRY_COUNT,
475 0,
476 PIRUS_MIN_THROTTLE_VALUE,
477 PIRUS_DISKSORT_DISABLED_FLAG,
478 PIRUS_LUN_RESET_ENABLED_FLAG
479 };
480
481 #endif
482
483 #if (defined(__sparc) && !defined(__fibre)) || \
484 (defined(__i386) || defined(__amd64))
485
486
487 static sd_tunables elite_properties = {
488 ELITE_THROTTLE_VALUE,
489 0,
490 0,
491 0,
492 0,
493 0,
494 0,
495 0,
496 0
497 };
498
499 static sd_tunables st31200n_properties = {
500 ST31200N_THROTTLE_VALUE,
501 0,
502 0,
503 0,
504 0,
505 0,
506 0,
507 0,
508 0
509 };
510
511 #endif /* Fibre or not */
512
513 static sd_tunables lsi_properties_scsi = {
514 LSI_THROTTLE_VALUE,
515 0,
516 LSI_NOTREADY_RETRIES,
517 0,
518 0,
519 0,
520 0,
521 0,
522 0
523 };
524
525 static sd_tunables symbios_properties = {
526 SYMBIOS_THROTTLE_VALUE,
527 0,
528 SYMBIOS_NOTREADY_RETRIES,
529 0,
530 0,
531 0,
532 0,
533 0,
534 0
535 };
536
537 static sd_tunables lsi_properties = {
538 0,
539 0,
540 LSI_NOTREADY_RETRIES,
541 0,
542 0,
543 0,
544 0,
545 0,
546 0
547 };
548
549 static sd_tunables lsi_oem_properties = {
550 0,
551 0,
552 LSI_OEM_NOTREADY_RETRIES,
553 0,
554 0,
555 0,
556 0,
557 0,
558 0,
559 1
560 };
561
562
563
564 #if (defined(SD_PROP_TST))
565
566 #define SD_TST_CTYPE_VAL CTYPE_CDROM
567 #define SD_TST_THROTTLE_VAL 16
568 #define SD_TST_NOTREADY_VAL 12
569 #define SD_TST_BUSY_VAL 60
570 #define SD_TST_RST_RETRY_VAL 36
571 #define SD_TST_RSV_REL_TIME 60
572
573 static sd_tunables tst_properties = {
574 SD_TST_THROTTLE_VAL,
575 SD_TST_CTYPE_VAL,
576 SD_TST_NOTREADY_VAL,
577 SD_TST_BUSY_VAL,
578 SD_TST_RST_RETRY_VAL,
579 SD_TST_RSV_REL_TIME,
580 0,
581 0,
582 0
583 };
584 #endif
585
586 /* This is similar to the ANSI toupper implementation */
587 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
588
589 /*
590 * Static Driver Configuration Table
591 *
592 * This is the table of disks which need throttle adjustment (or, perhaps
593 * something else as defined by the flags at a future time.) device_id
594 * is a string consisting of concatenated vid (vendor), pid (product/model)
595 * and revision strings as defined in the scsi_inquiry structure. Offsets of
596 * the parts of the string are as defined by the sizes in the scsi_inquiry
597 * structure. Device type is searched as far as the device_id string is
598 * defined. Flags defines which values are to be set in the driver from the
599 * properties list.
600 *
601 * Entries below which begin and end with a "*" are a special case.
602 * These do not have a specific vendor, and the string which follows
603 * can appear anywhere in the 16 byte PID portion of the inquiry data.
604 *
605 * Entries below which begin and end with a " " (blank) are a special
606 * case. The comparison function will treat multiple consecutive blanks
607 * as equivalent to a single blank. For example, this causes a
608 * sd_disk_table entry of " NEC CDROM " to match a device's id string
609 * of "NEC CDROM".
610 *
611 * Note: The MD21 controller type has been obsoleted.
612 * ST318202F is a Legacy device
613 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
614 * made with an FC connection. The entries here are a legacy.
615 */
616 static sd_disk_config_t sd_disk_table[] = {
617 #if defined(__fibre) || defined(__i386) || defined(__amd64)
618 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
633 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
641 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties },
642 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties },
643 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties },
644 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties },
645 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT |
668 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
669 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT |
670 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
671 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
672 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
673 { "SUN T3", SD_CONF_BSET_THROTTLE |
674 SD_CONF_BSET_BSY_RETRY_COUNT|
675 SD_CONF_BSET_RST_RETRIES|
676 SD_CONF_BSET_RSV_REL_TIME,
677 &purple_properties },
678 { "SUN SESS01", SD_CONF_BSET_THROTTLE |
679 SD_CONF_BSET_BSY_RETRY_COUNT|
680 SD_CONF_BSET_RST_RETRIES|
681 SD_CONF_BSET_RSV_REL_TIME|
682 SD_CONF_BSET_MIN_THROTTLE|
683 SD_CONF_BSET_DISKSORT_DISABLED,
684 &sve_properties },
685 { "SUN T4", SD_CONF_BSET_THROTTLE |
686 SD_CONF_BSET_BSY_RETRY_COUNT|
687 SD_CONF_BSET_RST_RETRIES|
688 SD_CONF_BSET_RSV_REL_TIME,
689 &purple_properties },
690 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
691 SD_CONF_BSET_LUN_RESET_ENABLED,
692 &maserati_properties },
693 { "SUN SE6920", SD_CONF_BSET_THROTTLE |
694 SD_CONF_BSET_NRR_COUNT|
695 SD_CONF_BSET_BSY_RETRY_COUNT|
696 SD_CONF_BSET_RST_RETRIES|
697 SD_CONF_BSET_MIN_THROTTLE|
698 SD_CONF_BSET_DISKSORT_DISABLED|
699 SD_CONF_BSET_LUN_RESET_ENABLED,
700 &pirus_properties },
701 { "SUN SE6940", SD_CONF_BSET_THROTTLE |
702 SD_CONF_BSET_NRR_COUNT|
703 SD_CONF_BSET_BSY_RETRY_COUNT|
704 SD_CONF_BSET_RST_RETRIES|
705 SD_CONF_BSET_MIN_THROTTLE|
706 SD_CONF_BSET_DISKSORT_DISABLED|
707 SD_CONF_BSET_LUN_RESET_ENABLED,
708 &pirus_properties },
709 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE |
710 SD_CONF_BSET_NRR_COUNT|
711 SD_CONF_BSET_BSY_RETRY_COUNT|
712 SD_CONF_BSET_RST_RETRIES|
713 SD_CONF_BSET_MIN_THROTTLE|
714 SD_CONF_BSET_DISKSORT_DISABLED|
715 SD_CONF_BSET_LUN_RESET_ENABLED,
716 &pirus_properties },
717 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE |
718 SD_CONF_BSET_NRR_COUNT|
719 SD_CONF_BSET_BSY_RETRY_COUNT|
720 SD_CONF_BSET_RST_RETRIES|
721 SD_CONF_BSET_MIN_THROTTLE|
722 SD_CONF_BSET_DISKSORT_DISABLED|
723 SD_CONF_BSET_LUN_RESET_ENABLED,
724 &pirus_properties },
725 { "SUN PSX1000", SD_CONF_BSET_THROTTLE |
726 SD_CONF_BSET_NRR_COUNT|
727 SD_CONF_BSET_BSY_RETRY_COUNT|
728 SD_CONF_BSET_RST_RETRIES|
729 SD_CONF_BSET_MIN_THROTTLE|
730 SD_CONF_BSET_DISKSORT_DISABLED|
731 SD_CONF_BSET_LUN_RESET_ENABLED,
732 &pirus_properties },
733 { "SUN SE6330", SD_CONF_BSET_THROTTLE |
734 SD_CONF_BSET_NRR_COUNT|
735 SD_CONF_BSET_BSY_RETRY_COUNT|
736 SD_CONF_BSET_RST_RETRIES|
737 SD_CONF_BSET_MIN_THROTTLE|
738 SD_CONF_BSET_DISKSORT_DISABLED|
739 SD_CONF_BSET_LUN_RESET_ENABLED,
740 &pirus_properties },
741 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
742 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
743 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
748 #endif /* fibre or NON-sparc platforms */
749 #if ((defined(__sparc) && !defined(__fibre)) ||\
750 (defined(__i386) || defined(__amd64)))
751 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
752 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
753 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
754 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
755 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
756 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
757 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
758 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
759 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
760 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
761 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
762 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL },
763 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
764 &symbios_properties },
765 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
766 &lsi_properties_scsi },
767 #if defined(__i386) || defined(__amd64)
768 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
769 | SD_CONF_BSET_READSUB_BCD
770 | SD_CONF_BSET_READ_TOC_ADDR_BCD
771 | SD_CONF_BSET_NO_READ_HEADER
772 | SD_CONF_BSET_READ_CD_XD4), NULL },
773
774 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
775 | SD_CONF_BSET_READSUB_BCD
776 | SD_CONF_BSET_READ_TOC_ADDR_BCD
777 | SD_CONF_BSET_NO_READ_HEADER
778 | SD_CONF_BSET_READ_CD_XD4), NULL },
779 #endif /* __i386 || __amd64 */
780 #endif /* sparc NON-fibre or NON-sparc platforms */
781
782 #if (defined(SD_PROP_TST))
783 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE
784 | SD_CONF_BSET_CTYPE
785 | SD_CONF_BSET_NRR_COUNT
786 | SD_CONF_BSET_FAB_DEVID
787 | SD_CONF_BSET_NOCACHE
788 | SD_CONF_BSET_BSY_RETRY_COUNT
789 | SD_CONF_BSET_PLAYMSF_BCD
790 | SD_CONF_BSET_READSUB_BCD
791 | SD_CONF_BSET_READ_TOC_TRK_BCD
792 | SD_CONF_BSET_READ_TOC_ADDR_BCD
793 | SD_CONF_BSET_NO_READ_HEADER
794 | SD_CONF_BSET_READ_CD_XD4
795 | SD_CONF_BSET_RST_RETRIES
796 | SD_CONF_BSET_RSV_REL_TIME
797 | SD_CONF_BSET_TUR_CHECK), &tst_properties},
798 #endif
799 };
800
801 static const int sd_disk_table_size =
802 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
803
804 /*
805 * Emulation mode disk drive VID/PID table
806 */
807 static char sd_flash_dev_table[][25] = {
808 "ATA MARVELL SD88SA02",
809 "MARVELL SD88SA02",
810 "TOSHIBA THNSNV05",
811 };
812
813 static const int sd_flash_dev_table_size =
814 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
815
816 #define SD_INTERCONNECT_PARALLEL 0
817 #define SD_INTERCONNECT_FABRIC 1
818 #define SD_INTERCONNECT_FIBRE 2
819 #define SD_INTERCONNECT_SSA 3
820 #define SD_INTERCONNECT_SATA 4
821 #define SD_INTERCONNECT_SAS 5
822
823 #define SD_IS_PARALLEL_SCSI(un) \
824 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
825 #define SD_IS_SERIAL(un) \
826 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
827 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
828
829 /*
830 * Definitions used by device id registration routines
831 */
832 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */
833 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */
834 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */
835
836 static kmutex_t sd_sense_mutex = {0};
837
838 /*
839 * Macros for updates of the driver state
840 */
841 #define New_state(un, s) \
842 (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
843 #define Restore_state(un) \
844 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
845
846 static struct sd_cdbinfo sd_cdbtab[] = {
847 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
848 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
849 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
850 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
851 };
852
853 /*
854 * Specifies the number of seconds that must have elapsed since the last
855 * cmd. has completed for a device to be declared idle to the PM framework.
856 */
857 static int sd_pm_idletime = 1;
858
859 /*
860 * Internal function prototypes
861 */
862
863 #if (defined(__fibre))
864 /*
865 * These #defines are to avoid namespace collisions that occur because this
866 * code is currently used to compile two separate driver modules: sd and ssd.
867 * All function names need to be treated this way (even if declared static)
868 * in order to allow the debugger to resolve the names properly.
869 * It is anticipated that in the near future the ssd module will be obsoleted,
870 * at which time this ugliness should go away.
871 */
872 #define sd_log_trace ssd_log_trace
873 #define sd_log_info ssd_log_info
874 #define sd_log_err ssd_log_err
875 #define sdprobe ssdprobe
876 #define sdinfo ssdinfo
877 #define sd_prop_op ssd_prop_op
878 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init
879 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini
880 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache
881 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache
882 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init
883 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini
884 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count
885 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target
886 #define sd_spin_up_unit ssd_spin_up_unit
887 #define sd_enable_descr_sense ssd_enable_descr_sense
888 #define sd_reenable_dsense_task ssd_reenable_dsense_task
889 #define sd_set_mmc_caps ssd_set_mmc_caps
890 #define sd_read_unit_properties ssd_read_unit_properties
891 #define sd_process_sdconf_file ssd_process_sdconf_file
892 #define sd_process_sdconf_table ssd_process_sdconf_table
893 #define sd_sdconf_id_match ssd_sdconf_id_match
894 #define sd_blank_cmp ssd_blank_cmp
895 #define sd_chk_vers1_data ssd_chk_vers1_data
896 #define sd_set_vers1_properties ssd_set_vers1_properties
897 #define sd_check_solid_state ssd_check_solid_state
898 #define sd_check_emulation_mode ssd_check_emulation_mode
899
900 #define sd_get_physical_geometry ssd_get_physical_geometry
901 #define sd_get_virtual_geometry ssd_get_virtual_geometry
902 #define sd_update_block_info ssd_update_block_info
903 #define sd_register_devid ssd_register_devid
904 #define sd_get_devid ssd_get_devid
905 #define sd_create_devid ssd_create_devid
906 #define sd_write_deviceid ssd_write_deviceid
907 #define sd_check_vpd_page_support ssd_check_vpd_page_support
908 #define sd_setup_pm ssd_setup_pm
909 #define sd_create_pm_components ssd_create_pm_components
910 #define sd_ddi_suspend ssd_ddi_suspend
911 #define sd_ddi_resume ssd_ddi_resume
912 #define sd_pm_state_change ssd_pm_state_change
913 #define sdpower ssdpower
914 #define sdattach ssdattach
915 #define sddetach ssddetach
916 #define sd_unit_attach ssd_unit_attach
917 #define sd_unit_detach ssd_unit_detach
918 #define sd_set_unit_attributes ssd_set_unit_attributes
919 #define sd_create_errstats ssd_create_errstats
920 #define sd_set_errstats ssd_set_errstats
921 #define sd_set_pstats ssd_set_pstats
922 #define sddump ssddump
923 #define sd_scsi_poll ssd_scsi_poll
924 #define sd_send_polled_RQS ssd_send_polled_RQS
925 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll
926 #define sd_init_event_callbacks ssd_init_event_callbacks
927 #define sd_event_callback ssd_event_callback
928 #define sd_cache_control ssd_cache_control
929 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled
930 #define sd_get_nv_sup ssd_get_nv_sup
931 #define sd_make_device ssd_make_device
932 #define sdopen ssdopen
933 #define sdclose ssdclose
934 #define sd_ready_and_valid ssd_ready_and_valid
935 #define sdmin ssdmin
936 #define sdread ssdread
937 #define sdwrite ssdwrite
938 #define sdaread ssdaread
939 #define sdawrite ssdawrite
940 #define sdstrategy ssdstrategy
941 #define sdioctl ssdioctl
942 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart
943 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart
944 #define sd_checksum_iostart ssd_checksum_iostart
945 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart
946 #define sd_pm_iostart ssd_pm_iostart
947 #define sd_core_iostart ssd_core_iostart
948 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone
949 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone
950 #define sd_checksum_iodone ssd_checksum_iodone
951 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone
952 #define sd_pm_iodone ssd_pm_iodone
953 #define sd_initpkt_for_buf ssd_initpkt_for_buf
954 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf
955 #define sd_setup_rw_pkt ssd_setup_rw_pkt
956 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt
957 #define sd_buf_iodone ssd_buf_iodone
958 #define sd_uscsi_strategy ssd_uscsi_strategy
959 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi
960 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi
961 #define sd_uscsi_iodone ssd_uscsi_iodone
962 #define sd_xbuf_strategy ssd_xbuf_strategy
963 #define sd_xbuf_init ssd_xbuf_init
964 #define sd_pm_entry ssd_pm_entry
965 #define sd_pm_exit ssd_pm_exit
966
967 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler
968 #define sd_pm_timeout_handler ssd_pm_timeout_handler
969
970 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq
971 #define sdintr ssdintr
972 #define sd_start_cmds ssd_start_cmds
973 #define sd_send_scsi_cmd ssd_send_scsi_cmd
974 #define sd_bioclone_alloc ssd_bioclone_alloc
975 #define sd_bioclone_free ssd_bioclone_free
976 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc
977 #define sd_shadow_buf_free ssd_shadow_buf_free
978 #define sd_print_transport_rejected_message \
979 ssd_print_transport_rejected_message
980 #define sd_retry_command ssd_retry_command
981 #define sd_set_retry_bp ssd_set_retry_bp
982 #define sd_send_request_sense_command ssd_send_request_sense_command
983 #define sd_start_retry_command ssd_start_retry_command
984 #define sd_start_direct_priority_command \
985 ssd_start_direct_priority_command
986 #define sd_return_failed_command ssd_return_failed_command
987 #define sd_return_failed_command_no_restart \
988 ssd_return_failed_command_no_restart
989 #define sd_return_command ssd_return_command
990 #define sd_sync_with_callback ssd_sync_with_callback
991 #define sdrunout ssdrunout
992 #define sd_mark_rqs_busy ssd_mark_rqs_busy
993 #define sd_mark_rqs_idle ssd_mark_rqs_idle
994 #define sd_reduce_throttle ssd_reduce_throttle
995 #define sd_restore_throttle ssd_restore_throttle
996 #define sd_print_incomplete_msg ssd_print_incomplete_msg
997 #define sd_init_cdb_limits ssd_init_cdb_limits
998 #define sd_pkt_status_good ssd_pkt_status_good
999 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition
1000 #define sd_pkt_status_busy ssd_pkt_status_busy
1001 #define sd_pkt_status_reservation_conflict \
1002 ssd_pkt_status_reservation_conflict
1003 #define sd_pkt_status_qfull ssd_pkt_status_qfull
1004 #define sd_handle_request_sense ssd_handle_request_sense
1005 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense
1006 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg
1007 #define sd_validate_sense_data ssd_validate_sense_data
1008 #define sd_decode_sense ssd_decode_sense
1009 #define sd_print_sense_msg ssd_print_sense_msg
1010 #define sd_sense_key_no_sense ssd_sense_key_no_sense
1011 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error
1012 #define sd_sense_key_not_ready ssd_sense_key_not_ready
1013 #define sd_sense_key_medium_or_hardware_error \
1014 ssd_sense_key_medium_or_hardware_error
1015 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request
1016 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention
1017 #define sd_sense_key_fail_command ssd_sense_key_fail_command
1018 #define sd_sense_key_blank_check ssd_sense_key_blank_check
1019 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command
1020 #define sd_sense_key_default ssd_sense_key_default
1021 #define sd_print_retry_msg ssd_print_retry_msg
1022 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg
1023 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete
1024 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err
1025 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset
1026 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted
1027 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout
1028 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free
1029 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject
1030 #define sd_pkt_reason_default ssd_pkt_reason_default
1031 #define sd_reset_target ssd_reset_target
1032 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback
1033 #define sd_start_stop_unit_task ssd_start_stop_unit_task
1034 #define sd_taskq_create ssd_taskq_create
1035 #define sd_taskq_delete ssd_taskq_delete
1036 #define sd_target_change_task ssd_target_change_task
1037 #define sd_log_dev_status_event ssd_log_dev_status_event
1038 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event
1039 #define sd_log_eject_request_event ssd_log_eject_request_event
1040 #define sd_media_change_task ssd_media_change_task
1041 #define sd_handle_mchange ssd_handle_mchange
1042 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK
1043 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY
1044 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16
1045 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION
1046 #define sd_send_scsi_feature_GET_CONFIGURATION \
1047 sd_send_scsi_feature_GET_CONFIGURATION
1048 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT
1049 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY
1050 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY
1051 #define sd_send_scsi_PERSISTENT_RESERVE_IN \
1052 ssd_send_scsi_PERSISTENT_RESERVE_IN
1053 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \
1054 ssd_send_scsi_PERSISTENT_RESERVE_OUT
1055 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE
1056 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \
1057 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1058 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE
1059 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT
1060 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR
1061 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE
1062 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \
1063 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1064 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid
1065 #define sd_alloc_rqs ssd_alloc_rqs
1066 #define sd_free_rqs ssd_free_rqs
1067 #define sd_dump_memory ssd_dump_memory
1068 #define sd_get_media_info_com ssd_get_media_info_com
1069 #define sd_get_media_info ssd_get_media_info
1070 #define sd_get_media_info_ext ssd_get_media_info_ext
1071 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info
1072 #define sd_nvpair_str_decode ssd_nvpair_str_decode
1073 #define sd_strtok_r ssd_strtok_r
1074 #define sd_set_properties ssd_set_properties
1075 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf
1076 #define sd_setup_next_xfer ssd_setup_next_xfer
1077 #define sd_dkio_get_temp ssd_dkio_get_temp
1078 #define sd_check_mhd ssd_check_mhd
1079 #define sd_mhd_watch_cb ssd_mhd_watch_cb
1080 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete
1081 #define sd_sname ssd_sname
1082 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover
1083 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread
1084 #define sd_take_ownership ssd_take_ownership
1085 #define sd_reserve_release ssd_reserve_release
1086 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req
1087 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb
1088 #define sd_persistent_reservation_in_read_keys \
1089 ssd_persistent_reservation_in_read_keys
1090 #define sd_persistent_reservation_in_read_resv \
1091 ssd_persistent_reservation_in_read_resv
1092 #define sd_mhdioc_takeown ssd_mhdioc_takeown
1093 #define sd_mhdioc_failfast ssd_mhdioc_failfast
1094 #define sd_mhdioc_release ssd_mhdioc_release
1095 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid
1096 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys
1097 #define sd_mhdioc_inresv ssd_mhdioc_inresv
1098 #define sr_change_blkmode ssr_change_blkmode
1099 #define sr_change_speed ssr_change_speed
1100 #define sr_atapi_change_speed ssr_atapi_change_speed
1101 #define sr_pause_resume ssr_pause_resume
1102 #define sr_play_msf ssr_play_msf
1103 #define sr_play_trkind ssr_play_trkind
1104 #define sr_read_all_subcodes ssr_read_all_subcodes
1105 #define sr_read_subchannel ssr_read_subchannel
1106 #define sr_read_tocentry ssr_read_tocentry
1107 #define sr_read_tochdr ssr_read_tochdr
1108 #define sr_read_cdda ssr_read_cdda
1109 #define sr_read_cdxa ssr_read_cdxa
1110 #define sr_read_mode1 ssr_read_mode1
1111 #define sr_read_mode2 ssr_read_mode2
1112 #define sr_read_cd_mode2 ssr_read_cd_mode2
1113 #define sr_sector_mode ssr_sector_mode
1114 #define sr_eject ssr_eject
1115 #define sr_ejected ssr_ejected
1116 #define sr_check_wp ssr_check_wp
1117 #define sd_watch_request_submit ssd_watch_request_submit
1118 #define sd_check_media ssd_check_media
1119 #define sd_media_watch_cb ssd_media_watch_cb
1120 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast
1121 #define sr_volume_ctrl ssr_volume_ctrl
1122 #define sr_read_sony_session_offset ssr_read_sony_session_offset
1123 #define sd_log_page_supported ssd_log_page_supported
1124 #define sd_check_for_writable_cd ssd_check_for_writable_cd
1125 #define sd_wm_cache_constructor ssd_wm_cache_constructor
1126 #define sd_wm_cache_destructor ssd_wm_cache_destructor
1127 #define sd_range_lock ssd_range_lock
1128 #define sd_get_range ssd_get_range
1129 #define sd_free_inlist_wmap ssd_free_inlist_wmap
1130 #define sd_range_unlock ssd_range_unlock
1131 #define sd_read_modify_write_task ssd_read_modify_write_task
1132 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw
1133
1134 #define sd_iostart_chain ssd_iostart_chain
1135 #define sd_iodone_chain ssd_iodone_chain
1136 #define sd_initpkt_map ssd_initpkt_map
1137 #define sd_destroypkt_map ssd_destroypkt_map
1138 #define sd_chain_type_map ssd_chain_type_map
1139 #define sd_chain_index_map ssd_chain_index_map
1140
1141 #define sd_failfast_flushctl ssd_failfast_flushctl
1142 #define sd_failfast_flushq ssd_failfast_flushq
1143 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback
1144
1145 #define sd_is_lsi ssd_is_lsi
1146 #define sd_tg_rdwr ssd_tg_rdwr
1147 #define sd_tg_getinfo ssd_tg_getinfo
1148 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler
1149
1150 #endif /* #if (defined(__fibre)) */
1151
1152
1153 int _init(void);
1154 int _fini(void);
1155 int _info(struct modinfo *modinfop);
1156
1157 /*PRINTFLIKE3*/
1158 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1159 /*PRINTFLIKE3*/
1160 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1161 /*PRINTFLIKE3*/
1162 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1163
1164 static int sdprobe(dev_info_t *devi);
1165 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1166 void **result);
1167 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1168 int mod_flags, char *name, caddr_t valuep, int *lengthp);
1169
1170 /*
1171 * Smart probe for parallel scsi
1172 */
1173 static void sd_scsi_probe_cache_init(void);
1174 static void sd_scsi_probe_cache_fini(void);
1175 static void sd_scsi_clear_probe_cache(void);
1176 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1177
1178 /*
1179 * Attached luns on target for parallel scsi
1180 */
1181 static void sd_scsi_target_lun_init(void);
1182 static void sd_scsi_target_lun_fini(void);
1183 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1184 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1185
1186 static int sd_spin_up_unit(sd_ssc_t *ssc);
1187
1188 /*
1189 * Using sd_ssc_init to establish sd_ssc_t struct
1190 * Using sd_ssc_send to send uscsi internal command
1191 * Using sd_ssc_fini to free sd_ssc_t struct
1192 */
1193 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1194 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1195 int flag, enum uio_seg dataspace, int path_flag);
1196 static void sd_ssc_fini(sd_ssc_t *ssc);
1197
1198 /*
1199 * Using sd_ssc_assessment to set correct type-of-assessment
1200 * Using sd_ssc_post to post ereport & system log
1201 * sd_ssc_post will call sd_ssc_print to print system log
1202 * sd_ssc_post will call sd_ssd_ereport_post to post ereport
1203 */
1204 static void sd_ssc_assessment(sd_ssc_t *ssc,
1205 enum sd_type_assessment tp_assess);
1206
1207 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1208 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1209 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1210 enum sd_driver_assessment drv_assess);
1211
1212 /*
1213 * Using sd_ssc_set_info to mark an un-decodable-data error.
1214 * Using sd_ssc_extract_info to transfer information from internal
1215 * data structures to sd_ssc_t.
1216 */
1217 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1218 const char *fmt, ...);
1219 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1220 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1221
1222 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1223 enum uio_seg dataspace, int path_flag);
1224
1225 #ifdef _LP64
1226 static void sd_enable_descr_sense(sd_ssc_t *ssc);
1227 static void sd_reenable_dsense_task(void *arg);
1228 #endif /* _LP64 */
1229
1230 static void sd_set_mmc_caps(sd_ssc_t *ssc);
1231
1232 static void sd_read_unit_properties(struct sd_lun *un);
1233 static int sd_process_sdconf_file(struct sd_lun *un);
1234 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1235 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1236 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1237 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1238 int *data_list, sd_tunables *values);
1239 static void sd_process_sdconf_table(struct sd_lun *un);
1240 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1241 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1242 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1243 int list_len, char *dataname_ptr);
1244 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1245 sd_tunables *prop_list);
1246
1247 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1248 int reservation_flag);
1249 static int sd_get_devid(sd_ssc_t *ssc);
1250 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1251 static int sd_write_deviceid(sd_ssc_t *ssc);
1252 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1253 static int sd_check_vpd_page_support(sd_ssc_t *ssc);
1254
1255 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1256 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1257
1258 static int sd_ddi_suspend(dev_info_t *devi);
1259 static int sd_ddi_resume(dev_info_t *devi);
1260 static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
1261 static int sdpower(dev_info_t *devi, int component, int level);
1262
1263 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1264 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1265 static int sd_unit_attach(dev_info_t *devi);
1266 static int sd_unit_detach(dev_info_t *devi);
1267
1268 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1269 static void sd_create_errstats(struct sd_lun *un, int instance);
1270 static void sd_set_errstats(struct sd_lun *un);
1271 static void sd_set_pstats(struct sd_lun *un);
1272
1273 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1274 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1275 static int sd_send_polled_RQS(struct sd_lun *un);
1276 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1277
1278 #if (defined(__fibre))
1279 /*
1280 * Event callbacks (photon)
1281 */
1282 static void sd_init_event_callbacks(struct sd_lun *un);
1283 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1284 #endif
1285
1286 /*
1287 * Defines for sd_cache_control
1288 */
1289
1290 #define SD_CACHE_ENABLE 1
1291 #define SD_CACHE_DISABLE 0
1292 #define SD_CACHE_NOCHANGE -1
1293
1294 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1295 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1296 static void sd_get_nv_sup(sd_ssc_t *ssc);
1297 static dev_t sd_make_device(dev_info_t *devi);
1298 static void sd_check_solid_state(sd_ssc_t *ssc);
1299 static void sd_check_emulation_mode(sd_ssc_t *ssc);
1300 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1301 uint64_t capacity);
1302
1303 /*
1304 * Driver entry point functions.
1305 */
1306 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1307 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1308 static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
1309
1310 static void sdmin(struct buf *bp);
1311 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1312 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1313 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1314 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1315
1316 static int sdstrategy(struct buf *bp);
1317 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1318
1319 /*
1320 * Function prototypes for layering functions in the iostart chain.
1321 */
1322 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1323 struct buf *bp);
1324 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1325 struct buf *bp);
1326 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1327 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1328 struct buf *bp);
1329 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1330 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1331
1332 /*
1333 * Function prototypes for layering functions in the iodone chain.
1334 */
1335 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1336 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1337 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1338 struct buf *bp);
1339 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1340 struct buf *bp);
1341 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1342 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1343 struct buf *bp);
1344 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1345
1346 /*
1347 * Prototypes for functions to support buf(9S) based IO.
1348 */
1349 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1350 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1351 static void sd_destroypkt_for_buf(struct buf *);
1352 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1353 struct buf *bp, int flags,
1354 int (*callback)(caddr_t), caddr_t callback_arg,
1355 diskaddr_t lba, uint32_t blockcount);
1356 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1357 struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1358
1359 /*
1360 * Prototypes for functions to support USCSI IO.
1361 */
1362 static int sd_uscsi_strategy(struct buf *bp);
1363 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1364 static void sd_destroypkt_for_uscsi(struct buf *);
1365
1366 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1367 uchar_t chain_type, void *pktinfop);
1368
1369 static int sd_pm_entry(struct sd_lun *un);
1370 static void sd_pm_exit(struct sd_lun *un);
1371
1372 static void sd_pm_idletimeout_handler(void *arg);
1373
1374 /*
1375 * sd_core internal functions (used at the sd_core_io layer).
1376 */
1377 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1378 static void sdintr(struct scsi_pkt *pktp);
1379 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1380
1381 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1382 enum uio_seg dataspace, int path_flag);
1383
1384 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1385 daddr_t blkno, int (*func)(struct buf *));
1386 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1387 uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1388 static void sd_bioclone_free(struct buf *bp);
1389 static void sd_shadow_buf_free(struct buf *bp);
1390
1391 static void sd_print_transport_rejected_message(struct sd_lun *un,
1392 struct sd_xbuf *xp, int code);
1393 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1394 void *arg, int code);
1395 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1396 void *arg, int code);
1397 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1398 void *arg, int code);
1399
1400 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1401 int retry_check_flag,
1402 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1403 int c),
1404 void *user_arg, int failure_code, clock_t retry_delay,
1405 void (*statp)(kstat_io_t *));
1406
1407 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1408 clock_t retry_delay, void (*statp)(kstat_io_t *));
1409
1410 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1411 struct scsi_pkt *pktp);
1412 static void sd_start_retry_command(void *arg);
1413 static void sd_start_direct_priority_command(void *arg);
1414 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1415 int errcode);
1416 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1417 struct buf *bp, int errcode);
1418 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1419 static void sd_sync_with_callback(struct sd_lun *un);
1420 static int sdrunout(caddr_t arg);
1421
1422 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1423 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1424
1425 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1426 static void sd_restore_throttle(void *arg);
1427
1428 static void sd_init_cdb_limits(struct sd_lun *un);
1429
1430 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1431 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1432
1433 /*
1434 * Error handling functions
1435 */
1436 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1437 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1438 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1439 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1440 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1441 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1442 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1443 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1444
1445 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1446 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1447 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1448 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1449 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1450 struct sd_xbuf *xp, size_t actual_len);
1451 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1452 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1453
1454 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1455 void *arg, int code);
1456
1457 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1458 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1459 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1460 uint8_t *sense_datap,
1461 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1462 static void sd_sense_key_not_ready(struct sd_lun *un,
1463 uint8_t *sense_datap,
1464 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1466 uint8_t *sense_datap,
1467 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1468 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1469 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1470 static void sd_sense_key_unit_attention(struct sd_lun *un,
1471 uint8_t *sense_datap,
1472 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1474 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1476 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1478 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1479 static void sd_sense_key_default(struct sd_lun *un,
1480 uint8_t *sense_datap,
1481 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1482
1483 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1484 void *arg, int flag);
1485
1486 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1487 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1488 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1489 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1490 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1491 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1492 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1493 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1494 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1495 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1496 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1497 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1498 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1499 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1500 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1501 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1502
1503 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1504
1505 static void sd_start_stop_unit_callback(void *arg);
1506 static void sd_start_stop_unit_task(void *arg);
1507
1508 static void sd_taskq_create(void);
1509 static void sd_taskq_delete(void);
1510 static void sd_target_change_task(void *arg);
1511 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1512 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1513 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1514 static void sd_media_change_task(void *arg);
1515
1516 static int sd_handle_mchange(struct sd_lun *un);
1517 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1518 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1519 uint32_t *lbap, int path_flag);
1520 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1521 uint32_t *lbap, uint32_t *psp, int path_flag);
1522 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1523 int flag, int path_flag);
1524 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1525 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1526 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1527 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1528 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1529 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1530 uchar_t usr_cmd, uchar_t *usr_bufp);
1531 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1532 struct dk_callback *dkc);
1533 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1534 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1535 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1536 uchar_t *bufaddr, uint_t buflen, int path_flag);
1537 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1538 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1539 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1540 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1541 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1542 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1543 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1544 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1545 size_t buflen, daddr_t start_block, int path_flag);
1546 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1547 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1548 path_flag)
1549 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1550 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1551 path_flag)
1552
1553 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1554 uint16_t buflen, uchar_t page_code, uchar_t page_control,
1555 uint16_t param_ptr, int path_flag);
1556 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1557 uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1558 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1559
1560 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1561 static void sd_free_rqs(struct sd_lun *un);
1562
1563 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1564 uchar_t *data, int len, int fmt);
1565 static void sd_panic_for_res_conflict(struct sd_lun *un);
1566
1567 /*
1568 * Disk Ioctl Function Prototypes
1569 */
1570 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1571 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1572 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1573 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1574
1575 /*
1576 * Multi-host Ioctl Prototypes
1577 */
1578 static int sd_check_mhd(dev_t dev, int interval);
1579 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1580 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1581 static char *sd_sname(uchar_t status);
1582 static void sd_mhd_resvd_recover(void *arg);
1583 static void sd_resv_reclaim_thread();
1584 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1585 static int sd_reserve_release(dev_t dev, int cmd);
1586 static void sd_rmv_resv_reclaim_req(dev_t dev);
1587 static void sd_mhd_reset_notify_cb(caddr_t arg);
1588 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1589 mhioc_inkeys_t *usrp, int flag);
1590 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1591 mhioc_inresvs_t *usrp, int flag);
1592 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1593 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1594 static int sd_mhdioc_release(dev_t dev);
1595 static int sd_mhdioc_register_devid(dev_t dev);
1596 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1597 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1598
1599 /*
1600 * SCSI removable prototypes
1601 */
1602 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1603 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1604 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1605 static int sr_pause_resume(dev_t dev, int mode);
1606 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1607 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1608 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1617 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1618 static int sr_eject(dev_t dev);
1619 static void sr_ejected(register struct sd_lun *un);
1620 static int sr_check_wp(dev_t dev);
1621 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1622 static int sd_check_media(dev_t dev, enum dkio_state state);
1623 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1624 static void sd_delayed_cv_broadcast(void *arg);
1625 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1626 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1627
1628 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1629
1630 /*
1631 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1632 */
1633 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1634 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1635 static void sd_wm_cache_destructor(void *wm, void *un);
1636 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1637 daddr_t endb, ushort_t typ);
1638 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1639 daddr_t endb);
1640 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1641 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1642 static void sd_read_modify_write_task(void * arg);
1643 static int
1644 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1645 struct buf **bpp);
1646
1647
1648 /*
1649 * Function prototypes for failfast support.
1650 */
1651 static void sd_failfast_flushq(struct sd_lun *un);
1652 static int sd_failfast_flushq_callback(struct buf *bp);
1653
1654 /*
1655 * Function prototypes to check for lsi devices
1656 */
1657 static void sd_is_lsi(struct sd_lun *un);
1658
1659 /*
1660 * Function prototypes for partial DMA support
1661 */
1662 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1663 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1664
1665
1666 /* Function prototypes for cmlb */
1667 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1668 diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1669
1670 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1671
1672 /*
1673 * For printing RMW warning message timely
1674 */
1675 static void sd_rmw_msg_print_handler(void *arg);
1676
1677 /*
1678 * Constants for failfast support:
1679 *
1680 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1681 * failfast processing being performed.
1682 *
1683 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1684 * failfast processing on all bufs with B_FAILFAST set.
1685 */
1686
1687 #define SD_FAILFAST_INACTIVE 0
1688 #define SD_FAILFAST_ACTIVE 1
1689
1690 /*
1691 * Bitmask to control behavior of buf(9S) flushes when a transition to
1692 * the failfast state occurs. Optional bits include:
1693 *
1694 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1695 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1696 * be flushed.
1697 *
1698 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1699 * driver, in addition to the regular wait queue. This includes the xbuf
1700 * queues. When clear, only the driver's wait queue will be flushed.
1701 */
1702 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
1703 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
1704
1705 /*
1706 * The default behavior is to only flush bufs that have B_FAILFAST set, but
1707 * to flush all queues within the driver.
1708 */
1709 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1710
1711
1712 /*
1713 * SD Testing Fault Injection
1714 */
1715 #ifdef SD_FAULT_INJECTION
1716 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1717 static void sd_faultinjection(struct scsi_pkt *pktp);
1718 static void sd_injection_log(char *buf, struct sd_lun *un);
1719 #endif
1720
1721 /*
1722 * Device driver ops vector
1723 */
1724 static struct cb_ops sd_cb_ops = {
1725 sdopen, /* open */
1726 sdclose, /* close */
1727 sdstrategy, /* strategy */
1728 nodev, /* print */
1729 sddump, /* dump */
1730 sdread, /* read */
1731 sdwrite, /* write */
1732 sdioctl, /* ioctl */
1733 nodev, /* devmap */
1734 nodev, /* mmap */
1735 nodev, /* segmap */
1736 nochpoll, /* poll */
1737 sd_prop_op, /* cb_prop_op */
1738 0, /* streamtab */
1739 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1740 CB_REV, /* cb_rev */
1741 sdaread, /* async I/O read entry point */
1742 sdawrite /* async I/O write entry point */
1743 };
1744
1745 struct dev_ops sd_ops = {
1746 DEVO_REV, /* devo_rev, */
1747 0, /* refcnt */
1748 sdinfo, /* info */
1749 nulldev, /* identify */
1750 sdprobe, /* probe */
1751 sdattach, /* attach */
1752 sddetach, /* detach */
1753 nodev, /* reset */
1754 &sd_cb_ops, /* driver operations */
1755 NULL, /* bus operations */
1756 sdpower, /* power */
1757 ddi_quiesce_not_needed, /* quiesce */
1758 };
1759
1760 /*
1761 * This is the loadable module wrapper.
1762 */
1763 #include <sys/modctl.h>
1764
1765 #ifndef XPV_HVM_DRIVER
1766 static struct modldrv modldrv = {
1767 &mod_driverops, /* Type of module. This one is a driver */
1768 SD_MODULE_NAME, /* Module name. */
1769 &sd_ops /* driver ops */
1770 };
1771
1772 static struct modlinkage modlinkage = {
1773 MODREV_1, &modldrv, NULL
1774 };
1775
1776 #else /* XPV_HVM_DRIVER */
1777 static struct modlmisc modlmisc = {
1778 &mod_miscops, /* Type of module. This one is a misc */
1779 "HVM " SD_MODULE_NAME, /* Module name. */
1780 };
1781
1782 static struct modlinkage modlinkage = {
1783 MODREV_1, &modlmisc, NULL
1784 };
1785
1786 #endif /* XPV_HVM_DRIVER */
1787
1788 static cmlb_tg_ops_t sd_tgops = {
1789 TG_DK_OPS_VERSION_1,
1790 sd_tg_rdwr,
1791 sd_tg_getinfo
1792 };
1793
1794 static struct scsi_asq_key_strings sd_additional_codes[] = {
1795 0x81, 0, "Logical Unit is Reserved",
1796 0x85, 0, "Audio Address Not Valid",
1797 0xb6, 0, "Media Load Mechanism Failed",
1798 0xB9, 0, "Audio Play Operation Aborted",
1799 0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1800 0x53, 2, "Medium removal prevented",
1801 0x6f, 0, "Authentication failed during key exchange",
1802 0x6f, 1, "Key not present",
1803 0x6f, 2, "Key not established",
1804 0x6f, 3, "Read without proper authentication",
1805 0x6f, 4, "Mismatched region to this logical unit",
1806 0x6f, 5, "Region reset count error",
1807 0xffff, 0x0, NULL
1808 };
1809
1810
1811 /*
1812 * Struct for passing printing information for sense data messages
1813 */
1814 struct sd_sense_info {
1815 int ssi_severity;
1816 int ssi_pfa_flag;
1817 };
1818
1819 /*
1820 * Table of function pointers for iostart-side routines. Separate "chains"
1821 * of layered function calls are formed by placing the function pointers
1822 * sequentially in the desired order. Functions are called according to an
1823 * incrementing table index ordering. The last function in each chain must
1824 * be sd_core_iostart(). The corresponding iodone-side routines are expected
1825 * in the sd_iodone_chain[] array.
1826 *
1827 * Note: It may seem more natural to organize both the iostart and iodone
1828 * functions together, into an array of structures (or some similar
1829 * organization) with a common index, rather than two separate arrays which
1830 * must be maintained in synchronization. The purpose of this division is
1831 * to achieve improved performance: individual arrays allows for more
1832 * effective cache line utilization on certain platforms.
1833 */
1834
1835 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1836
1837
1838 static sd_chain_t sd_iostart_chain[] = {
1839
1840 /* Chain for buf IO for disk drive targets (PM enabled) */
1841 sd_mapblockaddr_iostart, /* Index: 0 */
1842 sd_pm_iostart, /* Index: 1 */
1843 sd_core_iostart, /* Index: 2 */
1844
1845 /* Chain for buf IO for disk drive targets (PM disabled) */
1846 sd_mapblockaddr_iostart, /* Index: 3 */
1847 sd_core_iostart, /* Index: 4 */
1848
1849 /*
1850 * Chain for buf IO for removable-media or large sector size
1851 * disk drive targets with RMW needed (PM enabled)
1852 */
1853 sd_mapblockaddr_iostart, /* Index: 5 */
1854 sd_mapblocksize_iostart, /* Index: 6 */
1855 sd_pm_iostart, /* Index: 7 */
1856 sd_core_iostart, /* Index: 8 */
1857
1858 /*
1859 * Chain for buf IO for removable-media or large sector size
1860 * disk drive targets with RMW needed (PM disabled)
1861 */
1862 sd_mapblockaddr_iostart, /* Index: 9 */
1863 sd_mapblocksize_iostart, /* Index: 10 */
1864 sd_core_iostart, /* Index: 11 */
1865
1866 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1867 sd_mapblockaddr_iostart, /* Index: 12 */
1868 sd_checksum_iostart, /* Index: 13 */
1869 sd_pm_iostart, /* Index: 14 */
1870 sd_core_iostart, /* Index: 15 */
1871
1872 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1873 sd_mapblockaddr_iostart, /* Index: 16 */
1874 sd_checksum_iostart, /* Index: 17 */
1875 sd_core_iostart, /* Index: 18 */
1876
1877 /* Chain for USCSI commands (all targets) */
1878 sd_pm_iostart, /* Index: 19 */
1879 sd_core_iostart, /* Index: 20 */
1880
1881 /* Chain for checksumming USCSI commands (all targets) */
1882 sd_checksum_uscsi_iostart, /* Index: 21 */
1883 sd_pm_iostart, /* Index: 22 */
1884 sd_core_iostart, /* Index: 23 */
1885
1886 /* Chain for "direct" USCSI commands (all targets) */
1887 sd_core_iostart, /* Index: 24 */
1888
1889 /* Chain for "direct priority" USCSI commands (all targets) */
1890 sd_core_iostart, /* Index: 25 */
1891
1892 /*
1893 * Chain for buf IO for large sector size disk drive targets
1894 * with RMW needed with checksumming (PM enabled)
1895 */
1896 sd_mapblockaddr_iostart, /* Index: 26 */
1897 sd_mapblocksize_iostart, /* Index: 27 */
1898 sd_checksum_iostart, /* Index: 28 */
1899 sd_pm_iostart, /* Index: 29 */
1900 sd_core_iostart, /* Index: 30 */
1901
1902 /*
1903 * Chain for buf IO for large sector size disk drive targets
1904 * with RMW needed with checksumming (PM disabled)
1905 */
1906 sd_mapblockaddr_iostart, /* Index: 31 */
1907 sd_mapblocksize_iostart, /* Index: 32 */
1908 sd_checksum_iostart, /* Index: 33 */
1909 sd_core_iostart, /* Index: 34 */
1910
1911 };
1912
1913 /*
1914 * Macros to locate the first function of each iostart chain in the
1915 * sd_iostart_chain[] array. These are located by the index in the array.
1916 */
1917 #define SD_CHAIN_DISK_IOSTART 0
1918 #define SD_CHAIN_DISK_IOSTART_NO_PM 3
1919 #define SD_CHAIN_MSS_DISK_IOSTART 5
1920 #define SD_CHAIN_RMMEDIA_IOSTART 5
1921 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9
1922 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9
1923 #define SD_CHAIN_CHKSUM_IOSTART 12
1924 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16
1925 #define SD_CHAIN_USCSI_CMD_IOSTART 19
1926 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21
1927 #define SD_CHAIN_DIRECT_CMD_IOSTART 24
1928 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25
1929 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26
1930 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31
1931
1932
1933 /*
1934 * Table of function pointers for the iodone-side routines for the driver-
1935 * internal layering mechanism. The calling sequence for iodone routines
1936 * uses a decrementing table index, so the last routine called in a chain
1937 * must be at the lowest array index location for that chain. The last
1938 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1939 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering
1940 * of the functions in an iodone side chain must correspond to the ordering
1941 * of the iostart routines for that chain. Note that there is no iodone
1942 * side routine that corresponds to sd_core_iostart(), so there is no
1943 * entry in the table for this.
1944 */
1945
1946 static sd_chain_t sd_iodone_chain[] = {
1947
1948 /* Chain for buf IO for disk drive targets (PM enabled) */
1949 sd_buf_iodone, /* Index: 0 */
1950 sd_mapblockaddr_iodone, /* Index: 1 */
1951 sd_pm_iodone, /* Index: 2 */
1952
1953 /* Chain for buf IO for disk drive targets (PM disabled) */
1954 sd_buf_iodone, /* Index: 3 */
1955 sd_mapblockaddr_iodone, /* Index: 4 */
1956
1957 /*
1958 * Chain for buf IO for removable-media or large sector size
1959 * disk drive targets with RMW needed (PM enabled)
1960 */
1961 sd_buf_iodone, /* Index: 5 */
1962 sd_mapblockaddr_iodone, /* Index: 6 */
1963 sd_mapblocksize_iodone, /* Index: 7 */
1964 sd_pm_iodone, /* Index: 8 */
1965
1966 /*
1967 * Chain for buf IO for removable-media or large sector size
1968 * disk drive targets with RMW needed (PM disabled)
1969 */
1970 sd_buf_iodone, /* Index: 9 */
1971 sd_mapblockaddr_iodone, /* Index: 10 */
1972 sd_mapblocksize_iodone, /* Index: 11 */
1973
1974 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1975 sd_buf_iodone, /* Index: 12 */
1976 sd_mapblockaddr_iodone, /* Index: 13 */
1977 sd_checksum_iodone, /* Index: 14 */
1978 sd_pm_iodone, /* Index: 15 */
1979
1980 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1981 sd_buf_iodone, /* Index: 16 */
1982 sd_mapblockaddr_iodone, /* Index: 17 */
1983 sd_checksum_iodone, /* Index: 18 */
1984
1985 /* Chain for USCSI commands (non-checksum targets) */
1986 sd_uscsi_iodone, /* Index: 19 */
1987 sd_pm_iodone, /* Index: 20 */
1988
1989 /* Chain for USCSI commands (checksum targets) */
1990 sd_uscsi_iodone, /* Index: 21 */
1991 sd_checksum_uscsi_iodone, /* Index: 22 */
1992 sd_pm_iodone, /* Index: 22 */
1993
1994 /* Chain for "direct" USCSI commands (all targets) */
1995 sd_uscsi_iodone, /* Index: 24 */
1996
1997 /* Chain for "direct priority" USCSI commands (all targets) */
1998 sd_uscsi_iodone, /* Index: 25 */
1999
2000 /*
2001 * Chain for buf IO for large sector size disk drive targets
2002 * with checksumming (PM enabled)
2003 */
2004 sd_buf_iodone, /* Index: 26 */
2005 sd_mapblockaddr_iodone, /* Index: 27 */
2006 sd_mapblocksize_iodone, /* Index: 28 */
2007 sd_checksum_iodone, /* Index: 29 */
2008 sd_pm_iodone, /* Index: 30 */
2009
2010 /*
2011 * Chain for buf IO for large sector size disk drive targets
2012 * with checksumming (PM disabled)
2013 */
2014 sd_buf_iodone, /* Index: 31 */
2015 sd_mapblockaddr_iodone, /* Index: 32 */
2016 sd_mapblocksize_iodone, /* Index: 33 */
2017 sd_checksum_iodone, /* Index: 34 */
2018 };
2019
2020
2021 /*
2022 * Macros to locate the "first" function in the sd_iodone_chain[] array for
2023 * each iodone-side chain. These are located by the array index, but as the
2024 * iodone side functions are called in a decrementing-index order, the
2025 * highest index number in each chain must be specified (as these correspond
2026 * to the first function in the iodone chain that will be called by the core
2027 * at IO completion time).
2028 */
2029
2030 #define SD_CHAIN_DISK_IODONE 2
2031 #define SD_CHAIN_DISK_IODONE_NO_PM 4
2032 #define SD_CHAIN_RMMEDIA_IODONE 8
2033 #define SD_CHAIN_MSS_DISK_IODONE 8
2034 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11
2035 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11
2036 #define SD_CHAIN_CHKSUM_IODONE 15
2037 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18
2038 #define SD_CHAIN_USCSI_CMD_IODONE 20
2039 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22
2040 #define SD_CHAIN_DIRECT_CMD_IODONE 24
2041 #define SD_CHAIN_PRIORITY_CMD_IODONE 25
2042 #define SD_CHAIN_MSS_CHKSUM_IODONE 30
2043 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34
2044
2045
2046
2047 /*
2048 * Array to map a layering chain index to the appropriate initpkt routine.
2049 * The redundant entries are present so that the index used for accessing
2050 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2051 * with this table as well.
2052 */
2053 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2054
2055 static sd_initpkt_t sd_initpkt_map[] = {
2056
2057 /* Chain for buf IO for disk drive targets (PM enabled) */
2058 sd_initpkt_for_buf, /* Index: 0 */
2059 sd_initpkt_for_buf, /* Index: 1 */
2060 sd_initpkt_for_buf, /* Index: 2 */
2061
2062 /* Chain for buf IO for disk drive targets (PM disabled) */
2063 sd_initpkt_for_buf, /* Index: 3 */
2064 sd_initpkt_for_buf, /* Index: 4 */
2065
2066 /*
2067 * Chain for buf IO for removable-media or large sector size
2068 * disk drive targets (PM enabled)
2069 */
2070 sd_initpkt_for_buf, /* Index: 5 */
2071 sd_initpkt_for_buf, /* Index: 6 */
2072 sd_initpkt_for_buf, /* Index: 7 */
2073 sd_initpkt_for_buf, /* Index: 8 */
2074
2075 /*
2076 * Chain for buf IO for removable-media or large sector size
2077 * disk drive targets (PM disabled)
2078 */
2079 sd_initpkt_for_buf, /* Index: 9 */
2080 sd_initpkt_for_buf, /* Index: 10 */
2081 sd_initpkt_for_buf, /* Index: 11 */
2082
2083 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2084 sd_initpkt_for_buf, /* Index: 12 */
2085 sd_initpkt_for_buf, /* Index: 13 */
2086 sd_initpkt_for_buf, /* Index: 14 */
2087 sd_initpkt_for_buf, /* Index: 15 */
2088
2089 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2090 sd_initpkt_for_buf, /* Index: 16 */
2091 sd_initpkt_for_buf, /* Index: 17 */
2092 sd_initpkt_for_buf, /* Index: 18 */
2093
2094 /* Chain for USCSI commands (non-checksum targets) */
2095 sd_initpkt_for_uscsi, /* Index: 19 */
2096 sd_initpkt_for_uscsi, /* Index: 20 */
2097
2098 /* Chain for USCSI commands (checksum targets) */
2099 sd_initpkt_for_uscsi, /* Index: 21 */
2100 sd_initpkt_for_uscsi, /* Index: 22 */
2101 sd_initpkt_for_uscsi, /* Index: 22 */
2102
2103 /* Chain for "direct" USCSI commands (all targets) */
2104 sd_initpkt_for_uscsi, /* Index: 24 */
2105
2106 /* Chain for "direct priority" USCSI commands (all targets) */
2107 sd_initpkt_for_uscsi, /* Index: 25 */
2108
2109 /*
2110 * Chain for buf IO for large sector size disk drive targets
2111 * with checksumming (PM enabled)
2112 */
2113 sd_initpkt_for_buf, /* Index: 26 */
2114 sd_initpkt_for_buf, /* Index: 27 */
2115 sd_initpkt_for_buf, /* Index: 28 */
2116 sd_initpkt_for_buf, /* Index: 29 */
2117 sd_initpkt_for_buf, /* Index: 30 */
2118
2119 /*
2120 * Chain for buf IO for large sector size disk drive targets
2121 * with checksumming (PM disabled)
2122 */
2123 sd_initpkt_for_buf, /* Index: 31 */
2124 sd_initpkt_for_buf, /* Index: 32 */
2125 sd_initpkt_for_buf, /* Index: 33 */
2126 sd_initpkt_for_buf, /* Index: 34 */
2127 };
2128
2129
2130 /*
2131 * Array to map a layering chain index to the appropriate destroypktpkt routine.
2132 * The redundant entries are present so that the index used for accessing
2133 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2134 * with this table as well.
2135 */
2136 typedef void (*sd_destroypkt_t)(struct buf *);
2137
2138 static sd_destroypkt_t sd_destroypkt_map[] = {
2139
2140 /* Chain for buf IO for disk drive targets (PM enabled) */
2141 sd_destroypkt_for_buf, /* Index: 0 */
2142 sd_destroypkt_for_buf, /* Index: 1 */
2143 sd_destroypkt_for_buf, /* Index: 2 */
2144
2145 /* Chain for buf IO for disk drive targets (PM disabled) */
2146 sd_destroypkt_for_buf, /* Index: 3 */
2147 sd_destroypkt_for_buf, /* Index: 4 */
2148
2149 /*
2150 * Chain for buf IO for removable-media or large sector size
2151 * disk drive targets (PM enabled)
2152 */
2153 sd_destroypkt_for_buf, /* Index: 5 */
2154 sd_destroypkt_for_buf, /* Index: 6 */
2155 sd_destroypkt_for_buf, /* Index: 7 */
2156 sd_destroypkt_for_buf, /* Index: 8 */
2157
2158 /*
2159 * Chain for buf IO for removable-media or large sector size
2160 * disk drive targets (PM disabled)
2161 */
2162 sd_destroypkt_for_buf, /* Index: 9 */
2163 sd_destroypkt_for_buf, /* Index: 10 */
2164 sd_destroypkt_for_buf, /* Index: 11 */
2165
2166 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2167 sd_destroypkt_for_buf, /* Index: 12 */
2168 sd_destroypkt_for_buf, /* Index: 13 */
2169 sd_destroypkt_for_buf, /* Index: 14 */
2170 sd_destroypkt_for_buf, /* Index: 15 */
2171
2172 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2173 sd_destroypkt_for_buf, /* Index: 16 */
2174 sd_destroypkt_for_buf, /* Index: 17 */
2175 sd_destroypkt_for_buf, /* Index: 18 */
2176
2177 /* Chain for USCSI commands (non-checksum targets) */
2178 sd_destroypkt_for_uscsi, /* Index: 19 */
2179 sd_destroypkt_for_uscsi, /* Index: 20 */
2180
2181 /* Chain for USCSI commands (checksum targets) */
2182 sd_destroypkt_for_uscsi, /* Index: 21 */
2183 sd_destroypkt_for_uscsi, /* Index: 22 */
2184 sd_destroypkt_for_uscsi, /* Index: 22 */
2185
2186 /* Chain for "direct" USCSI commands (all targets) */
2187 sd_destroypkt_for_uscsi, /* Index: 24 */
2188
2189 /* Chain for "direct priority" USCSI commands (all targets) */
2190 sd_destroypkt_for_uscsi, /* Index: 25 */
2191
2192 /*
2193 * Chain for buf IO for large sector size disk drive targets
2194 * with checksumming (PM disabled)
2195 */
2196 sd_destroypkt_for_buf, /* Index: 26 */
2197 sd_destroypkt_for_buf, /* Index: 27 */
2198 sd_destroypkt_for_buf, /* Index: 28 */
2199 sd_destroypkt_for_buf, /* Index: 29 */
2200 sd_destroypkt_for_buf, /* Index: 30 */
2201
2202 /*
2203 * Chain for buf IO for large sector size disk drive targets
2204 * with checksumming (PM enabled)
2205 */
2206 sd_destroypkt_for_buf, /* Index: 31 */
2207 sd_destroypkt_for_buf, /* Index: 32 */
2208 sd_destroypkt_for_buf, /* Index: 33 */
2209 sd_destroypkt_for_buf, /* Index: 34 */
2210 };
2211
2212
2213
2214 /*
2215 * Array to map a layering chain index to the appropriate chain "type".
2216 * The chain type indicates a specific property/usage of the chain.
2217 * The redundant entries are present so that the index used for accessing
2218 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2219 * with this table as well.
2220 */
2221
2222 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */
2223 #define SD_CHAIN_BUFIO 1 /* regular buf IO */
2224 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */
2225 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */
2226 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */
2227 /* (for error recovery) */
2228
2229 static int sd_chain_type_map[] = {
2230
2231 /* Chain for buf IO for disk drive targets (PM enabled) */
2232 SD_CHAIN_BUFIO, /* Index: 0 */
2233 SD_CHAIN_BUFIO, /* Index: 1 */
2234 SD_CHAIN_BUFIO, /* Index: 2 */
2235
2236 /* Chain for buf IO for disk drive targets (PM disabled) */
2237 SD_CHAIN_BUFIO, /* Index: 3 */
2238 SD_CHAIN_BUFIO, /* Index: 4 */
2239
2240 /*
2241 * Chain for buf IO for removable-media or large sector size
2242 * disk drive targets (PM enabled)
2243 */
2244 SD_CHAIN_BUFIO, /* Index: 5 */
2245 SD_CHAIN_BUFIO, /* Index: 6 */
2246 SD_CHAIN_BUFIO, /* Index: 7 */
2247 SD_CHAIN_BUFIO, /* Index: 8 */
2248
2249 /*
2250 * Chain for buf IO for removable-media or large sector size
2251 * disk drive targets (PM disabled)
2252 */
2253 SD_CHAIN_BUFIO, /* Index: 9 */
2254 SD_CHAIN_BUFIO, /* Index: 10 */
2255 SD_CHAIN_BUFIO, /* Index: 11 */
2256
2257 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2258 SD_CHAIN_BUFIO, /* Index: 12 */
2259 SD_CHAIN_BUFIO, /* Index: 13 */
2260 SD_CHAIN_BUFIO, /* Index: 14 */
2261 SD_CHAIN_BUFIO, /* Index: 15 */
2262
2263 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2264 SD_CHAIN_BUFIO, /* Index: 16 */
2265 SD_CHAIN_BUFIO, /* Index: 17 */
2266 SD_CHAIN_BUFIO, /* Index: 18 */
2267
2268 /* Chain for USCSI commands (non-checksum targets) */
2269 SD_CHAIN_USCSI, /* Index: 19 */
2270 SD_CHAIN_USCSI, /* Index: 20 */
2271
2272 /* Chain for USCSI commands (checksum targets) */
2273 SD_CHAIN_USCSI, /* Index: 21 */
2274 SD_CHAIN_USCSI, /* Index: 22 */
2275 SD_CHAIN_USCSI, /* Index: 23 */
2276
2277 /* Chain for "direct" USCSI commands (all targets) */
2278 SD_CHAIN_DIRECT, /* Index: 24 */
2279
2280 /* Chain for "direct priority" USCSI commands (all targets) */
2281 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */
2282
2283 /*
2284 * Chain for buf IO for large sector size disk drive targets
2285 * with checksumming (PM enabled)
2286 */
2287 SD_CHAIN_BUFIO, /* Index: 26 */
2288 SD_CHAIN_BUFIO, /* Index: 27 */
2289 SD_CHAIN_BUFIO, /* Index: 28 */
2290 SD_CHAIN_BUFIO, /* Index: 29 */
2291 SD_CHAIN_BUFIO, /* Index: 30 */
2292
2293 /*
2294 * Chain for buf IO for large sector size disk drive targets
2295 * with checksumming (PM disabled)
2296 */
2297 SD_CHAIN_BUFIO, /* Index: 31 */
2298 SD_CHAIN_BUFIO, /* Index: 32 */
2299 SD_CHAIN_BUFIO, /* Index: 33 */
2300 SD_CHAIN_BUFIO, /* Index: 34 */
2301 };
2302
2303
2304 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2305 #define SD_IS_BUFIO(xp) \
2306 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2307
2308 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2309 #define SD_IS_DIRECT_PRIORITY(xp) \
2310 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2311
2312
2313
2314 /*
2315 * Struct, array, and macros to map a specific chain to the appropriate
2316 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2317 *
2318 * The sd_chain_index_map[] array is used at attach time to set the various
2319 * un_xxx_chain type members of the sd_lun softstate to the specific layering
2320 * chain to be used with the instance. This allows different instances to use
2321 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2322 * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2323 * values at sd_xbuf init time, this allows (1) layering chains may be changed
2324 * dynamically & without the use of locking; and (2) a layer may update the
2325 * xb_chain_io[start|done] member in a given xbuf with its current index value,
2326 * to allow for deferred processing of an IO within the same chain from a
2327 * different execution context.
2328 */
2329
2330 struct sd_chain_index {
2331 int sci_iostart_index;
2332 int sci_iodone_index;
2333 };
2334
2335 static struct sd_chain_index sd_chain_index_map[] = {
2336 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE },
2337 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM },
2338 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE },
2339 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2340 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE },
2341 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM },
2342 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE },
2343 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE },
2344 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE },
2345 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE },
2346 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE },
2347 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2348
2349 };
2350
2351
2352 /*
2353 * The following are indexes into the sd_chain_index_map[] array.
2354 */
2355
2356 /* un->un_buf_chain_type must be set to one of these */
2357 #define SD_CHAIN_INFO_DISK 0
2358 #define SD_CHAIN_INFO_DISK_NO_PM 1
2359 #define SD_CHAIN_INFO_RMMEDIA 2
2360 #define SD_CHAIN_INFO_MSS_DISK 2
2361 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3
2362 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3
2363 #define SD_CHAIN_INFO_CHKSUM 4
2364 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5
2365 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10
2366 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
2367
2368 /* un->un_uscsi_chain_type must be set to one of these */
2369 #define SD_CHAIN_INFO_USCSI_CMD 6
2370 /* USCSI with PM disabled is the same as DIRECT */
2371 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
2372 #define SD_CHAIN_INFO_USCSI_CHKSUM 7
2373
2374 /* un->un_direct_chain_type must be set to one of these */
2375 #define SD_CHAIN_INFO_DIRECT_CMD 8
2376
2377 /* un->un_priority_chain_type must be set to one of these */
2378 #define SD_CHAIN_INFO_PRIORITY_CMD 9
2379
2380 /* size for devid inquiries */
2381 #define MAX_INQUIRY_SIZE 0xF0
2382
2383 /*
2384 * Macros used by functions to pass a given buf(9S) struct along to the
2385 * next function in the layering chain for further processing.
2386 *
2387 * In the following macros, passing more than three arguments to the called
2388 * routines causes the optimizer for the SPARC compiler to stop doing tail
2389 * call elimination which results in significant performance degradation.
2390 */
2391 #define SD_BEGIN_IOSTART(index, un, bp) \
2392 ((*(sd_iostart_chain[index]))(index, un, bp))
2393
2394 #define SD_BEGIN_IODONE(index, un, bp) \
2395 ((*(sd_iodone_chain[index]))(index, un, bp))
2396
2397 #define SD_NEXT_IOSTART(index, un, bp) \
2398 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2399
2400 #define SD_NEXT_IODONE(index, un, bp) \
2401 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2402
2403 /*
2404 * Function: _init
2405 *
2406 * Description: This is the driver _init(9E) entry point.
2407 *
2408 * Return Code: Returns the value from mod_install(9F) or
2409 * ddi_soft_state_init(9F) as appropriate.
2410 *
2411 * Context: Called when driver module loaded.
2412 */
2413
2414 int
2415 _init(void)
2416 {
2417 int err;
2418
2419 /* establish driver name from module name */
2420 sd_label = (char *)mod_modname(&modlinkage);
2421
2422 #ifndef XPV_HVM_DRIVER
2423 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2424 SD_MAXUNIT);
2425 if (err != 0) {
2426 return (err);
2427 }
2428
2429 #else /* XPV_HVM_DRIVER */
2430 /* Remove the leading "hvm_" from the module name */
2431 ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2432 sd_label += strlen("hvm_");
2433
2434 #endif /* XPV_HVM_DRIVER */
2435
2436 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2437 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL);
2438 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL);
2439
2440 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2441 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2442 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2443
2444 /*
2445 * it's ok to init here even for fibre device
2446 */
2447 sd_scsi_probe_cache_init();
2448
2449 sd_scsi_target_lun_init();
2450
2451 /*
2452 * Creating taskq before mod_install ensures that all callers (threads)
2453 * that enter the module after a successful mod_install encounter
2454 * a valid taskq.
2455 */
2456 sd_taskq_create();
2457
2458 err = mod_install(&modlinkage);
2459 if (err != 0) {
2460 /* delete taskq if install fails */
2461 sd_taskq_delete();
2462
2463 mutex_destroy(&sd_detach_mutex);
2464 mutex_destroy(&sd_log_mutex);
2465 mutex_destroy(&sd_label_mutex);
2466
2467 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2468 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2469 cv_destroy(&sd_tr.srq_inprocess_cv);
2470
2471 sd_scsi_probe_cache_fini();
2472
2473 sd_scsi_target_lun_fini();
2474
2475 #ifndef XPV_HVM_DRIVER
2476 ddi_soft_state_fini(&sd_state);
2477 #endif /* !XPV_HVM_DRIVER */
2478 return (err);
2479 }
2480
2481 return (err);
2482 }
2483
2484
2485 /*
2486 * Function: _fini
2487 *
2488 * Description: This is the driver _fini(9E) entry point.
2489 *
2490 * Return Code: Returns the value from mod_remove(9F)
2491 *
2492 * Context: Called when driver module is unloaded.
2493 */
2494
2495 int
2496 _fini(void)
2497 {
2498 int err;
2499
2500 if ((err = mod_remove(&modlinkage)) != 0) {
2501 return (err);
2502 }
2503
2504 sd_taskq_delete();
2505
2506 mutex_destroy(&sd_detach_mutex);
2507 mutex_destroy(&sd_log_mutex);
2508 mutex_destroy(&sd_label_mutex);
2509 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2510
2511 sd_scsi_probe_cache_fini();
2512
2513 sd_scsi_target_lun_fini();
2514
2515 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2516 cv_destroy(&sd_tr.srq_inprocess_cv);
2517
2518 #ifndef XPV_HVM_DRIVER
2519 ddi_soft_state_fini(&sd_state);
2520 #endif /* !XPV_HVM_DRIVER */
2521
2522 return (err);
2523 }
2524
2525
2526 /*
2527 * Function: _info
2528 *
2529 * Description: This is the driver _info(9E) entry point.
2530 *
2531 * Arguments: modinfop - pointer to the driver modinfo structure
2532 *
2533 * Return Code: Returns the value from mod_info(9F).
2534 *
2535 * Context: Kernel thread context
2536 */
2537
2538 int
2539 _info(struct modinfo *modinfop)
2540 {
2541 return (mod_info(&modlinkage, modinfop));
2542 }
2543
2544
2545 /*
2546 * The following routines implement the driver message logging facility.
2547 * They provide component- and level- based debug output filtering.
2548 * Output may also be restricted to messages for a single instance by
2549 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2550 * to NULL, then messages for all instances are printed.
2551 *
2552 * These routines have been cloned from each other due to the language
2553 * constraints of macros and variable argument list processing.
2554 */
2555
2556
2557 /*
2558 * Function: sd_log_err
2559 *
2560 * Description: This routine is called by the SD_ERROR macro for debug
2561 * logging of error conditions.
2562 *
2563 * Arguments: comp - driver component being logged
2564 * dev - pointer to driver info structure
2565 * fmt - error string and format to be logged
2566 */
2567
2568 static void
2569 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2570 {
2571 va_list ap;
2572 dev_info_t *dev;
2573
2574 ASSERT(un != NULL);
2575 dev = SD_DEVINFO(un);
2576 ASSERT(dev != NULL);
2577
2578 /*
2579 * Filter messages based on the global component and level masks.
2580 * Also print if un matches the value of sd_debug_un, or if
2581 * sd_debug_un is set to NULL.
2582 */
2583 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2584 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2585 mutex_enter(&sd_log_mutex);
2586 va_start(ap, fmt);
2587 (void) vsprintf(sd_log_buf, fmt, ap);
2588 va_end(ap);
2589 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2590 mutex_exit(&sd_log_mutex);
2591 }
2592 #ifdef SD_FAULT_INJECTION
2593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2594 if (un->sd_injection_mask & comp) {
2595 mutex_enter(&sd_log_mutex);
2596 va_start(ap, fmt);
2597 (void) vsprintf(sd_log_buf, fmt, ap);
2598 va_end(ap);
2599 sd_injection_log(sd_log_buf, un);
2600 mutex_exit(&sd_log_mutex);
2601 }
2602 #endif
2603 }
2604
2605
2606 /*
2607 * Function: sd_log_info
2608 *
2609 * Description: This routine is called by the SD_INFO macro for debug
2610 * logging of general purpose informational conditions.
2611 *
2612 * Arguments: comp - driver component being logged
2613 * dev - pointer to driver info structure
2614 * fmt - info string and format to be logged
2615 */
2616
2617 static void
2618 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2619 {
2620 va_list ap;
2621 dev_info_t *dev;
2622
2623 ASSERT(un != NULL);
2624 dev = SD_DEVINFO(un);
2625 ASSERT(dev != NULL);
2626
2627 /*
2628 * Filter messages based on the global component and level masks.
2629 * Also print if un matches the value of sd_debug_un, or if
2630 * sd_debug_un is set to NULL.
2631 */
2632 if ((sd_component_mask & component) &&
2633 (sd_level_mask & SD_LOGMASK_INFO) &&
2634 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2635 mutex_enter(&sd_log_mutex);
2636 va_start(ap, fmt);
2637 (void) vsprintf(sd_log_buf, fmt, ap);
2638 va_end(ap);
2639 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2640 mutex_exit(&sd_log_mutex);
2641 }
2642 #ifdef SD_FAULT_INJECTION
2643 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2644 if (un->sd_injection_mask & component) {
2645 mutex_enter(&sd_log_mutex);
2646 va_start(ap, fmt);
2647 (void) vsprintf(sd_log_buf, fmt, ap);
2648 va_end(ap);
2649 sd_injection_log(sd_log_buf, un);
2650 mutex_exit(&sd_log_mutex);
2651 }
2652 #endif
2653 }
2654
2655
2656 /*
2657 * Function: sd_log_trace
2658 *
2659 * Description: This routine is called by the SD_TRACE macro for debug
2660 * logging of trace conditions (i.e. function entry/exit).
2661 *
2662 * Arguments: comp - driver component being logged
2663 * dev - pointer to driver info structure
2664 * fmt - trace string and format to be logged
2665 */
2666
2667 static void
2668 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2669 {
2670 va_list ap;
2671 dev_info_t *dev;
2672
2673 ASSERT(un != NULL);
2674 dev = SD_DEVINFO(un);
2675 ASSERT(dev != NULL);
2676
2677 /*
2678 * Filter messages based on the global component and level masks.
2679 * Also print if un matches the value of sd_debug_un, or if
2680 * sd_debug_un is set to NULL.
2681 */
2682 if ((sd_component_mask & component) &&
2683 (sd_level_mask & SD_LOGMASK_TRACE) &&
2684 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2685 mutex_enter(&sd_log_mutex);
2686 va_start(ap, fmt);
2687 (void) vsprintf(sd_log_buf, fmt, ap);
2688 va_end(ap);
2689 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2690 mutex_exit(&sd_log_mutex);
2691 }
2692 #ifdef SD_FAULT_INJECTION
2693 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2694 if (un->sd_injection_mask & component) {
2695 mutex_enter(&sd_log_mutex);
2696 va_start(ap, fmt);
2697 (void) vsprintf(sd_log_buf, fmt, ap);
2698 va_end(ap);
2699 sd_injection_log(sd_log_buf, un);
2700 mutex_exit(&sd_log_mutex);
2701 }
2702 #endif
2703 }
2704
2705
2706 /*
2707 * Function: sdprobe
2708 *
2709 * Description: This is the driver probe(9e) entry point function.
2710 *
2711 * Arguments: devi - opaque device info handle
2712 *
2713 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2714 * DDI_PROBE_FAILURE: If the probe failed.
2715 * DDI_PROBE_PARTIAL: If the instance is not present now,
2716 * but may be present in the future.
2717 */
2718
2719 static int
2720 sdprobe(dev_info_t *devi)
2721 {
2722 struct scsi_device *devp;
2723 int rval;
2724 #ifndef XPV_HVM_DRIVER
2725 int instance = ddi_get_instance(devi);
2726 #endif /* !XPV_HVM_DRIVER */
2727
2728 /*
2729 * if it wasn't for pln, sdprobe could actually be nulldev
2730 * in the "__fibre" case.
2731 */
2732 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2733 return (DDI_PROBE_DONTCARE);
2734 }
2735
2736 devp = ddi_get_driver_private(devi);
2737
2738 if (devp == NULL) {
2739 /* Ooops... nexus driver is mis-configured... */
2740 return (DDI_PROBE_FAILURE);
2741 }
2742
2743 #ifndef XPV_HVM_DRIVER
2744 if (ddi_get_soft_state(sd_state, instance) != NULL) {
2745 return (DDI_PROBE_PARTIAL);
2746 }
2747 #endif /* !XPV_HVM_DRIVER */
2748
2749 /*
2750 * Call the SCSA utility probe routine to see if we actually
2751 * have a target at this SCSI nexus.
2752 */
2753 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2754 case SCSIPROBE_EXISTS:
2755 switch (devp->sd_inq->inq_dtype) {
2756 case DTYPE_DIRECT:
2757 rval = DDI_PROBE_SUCCESS;
2758 break;
2759 case DTYPE_RODIRECT:
2760 /* CDs etc. Can be removable media */
2761 rval = DDI_PROBE_SUCCESS;
2762 break;
2763 case DTYPE_OPTICAL:
2764 /*
2765 * Rewritable optical driver HP115AA
2766 * Can also be removable media
2767 */
2768
2769 /*
2770 * Do not attempt to bind to DTYPE_OPTICAL if
2771 * pre solaris 9 sparc sd behavior is required
2772 *
2773 * If first time through and sd_dtype_optical_bind
2774 * has not been set in /etc/system check properties
2775 */
2776
2777 if (sd_dtype_optical_bind < 0) {
2778 sd_dtype_optical_bind = ddi_prop_get_int
2779 (DDI_DEV_T_ANY, devi, 0,
2780 "optical-device-bind", 1);
2781 }
2782
2783 if (sd_dtype_optical_bind == 0) {
2784 rval = DDI_PROBE_FAILURE;
2785 } else {
2786 rval = DDI_PROBE_SUCCESS;
2787 }
2788 break;
2789
2790 case DTYPE_NOTPRESENT:
2791 default:
2792 rval = DDI_PROBE_FAILURE;
2793 break;
2794 }
2795 break;
2796 default:
2797 rval = DDI_PROBE_PARTIAL;
2798 break;
2799 }
2800
2801 /*
2802 * This routine checks for resource allocation prior to freeing,
2803 * so it will take care of the "smart probing" case where a
2804 * scsi_probe() may or may not have been issued and will *not*
2805 * free previously-freed resources.
2806 */
2807 scsi_unprobe(devp);
2808 return (rval);
2809 }
2810
2811
2812 /*
2813 * Function: sdinfo
2814 *
2815 * Description: This is the driver getinfo(9e) entry point function.
2816 * Given the device number, return the devinfo pointer from
2817 * the scsi_device structure or the instance number
2818 * associated with the dev_t.
2819 *
2820 * Arguments: dip - pointer to device info structure
2821 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2822 * DDI_INFO_DEVT2INSTANCE)
2823 * arg - driver dev_t
2824 * resultp - user buffer for request response
2825 *
2826 * Return Code: DDI_SUCCESS
2827 * DDI_FAILURE
2828 */
2829 /* ARGSUSED */
2830 static int
2831 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2832 {
2833 struct sd_lun *un;
2834 dev_t dev;
2835 int instance;
2836 int error;
2837
2838 switch (infocmd) {
2839 case DDI_INFO_DEVT2DEVINFO:
2840 dev = (dev_t)arg;
2841 instance = SDUNIT(dev);
2842 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2843 return (DDI_FAILURE);
2844 }
2845 *result = (void *) SD_DEVINFO(un);
2846 error = DDI_SUCCESS;
2847 break;
2848 case DDI_INFO_DEVT2INSTANCE:
2849 dev = (dev_t)arg;
2850 instance = SDUNIT(dev);
2851 *result = (void *)(uintptr_t)instance;
2852 error = DDI_SUCCESS;
2853 break;
2854 default:
2855 error = DDI_FAILURE;
2856 }
2857 return (error);
2858 }
2859
2860 /*
2861 * Function: sd_prop_op
2862 *
2863 * Description: This is the driver prop_op(9e) entry point function.
2864 * Return the number of blocks for the partition in question
2865 * or forward the request to the property facilities.
2866 *
2867 * Arguments: dev - device number
2868 * dip - pointer to device info structure
2869 * prop_op - property operator
2870 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2871 * name - pointer to property name
2872 * valuep - pointer or address of the user buffer
2873 * lengthp - property length
2874 *
2875 * Return Code: DDI_PROP_SUCCESS
2876 * DDI_PROP_NOT_FOUND
2877 * DDI_PROP_UNDEFINED
2878 * DDI_PROP_NO_MEMORY
2879 * DDI_PROP_BUF_TOO_SMALL
2880 */
2881
2882 static int
2883 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2884 char *name, caddr_t valuep, int *lengthp)
2885 {
2886 struct sd_lun *un;
2887
2888 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2889 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2890 name, valuep, lengthp));
2891
2892 return (cmlb_prop_op(un->un_cmlbhandle,
2893 dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2894 SDPART(dev), (void *)SD_PATH_DIRECT));
2895 }
2896
2897 /*
2898 * The following functions are for smart probing:
2899 * sd_scsi_probe_cache_init()
2900 * sd_scsi_probe_cache_fini()
2901 * sd_scsi_clear_probe_cache()
2902 * sd_scsi_probe_with_cache()
2903 */
2904
2905 /*
2906 * Function: sd_scsi_probe_cache_init
2907 *
2908 * Description: Initializes the probe response cache mutex and head pointer.
2909 *
2910 * Context: Kernel thread context
2911 */
2912
2913 static void
2914 sd_scsi_probe_cache_init(void)
2915 {
2916 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2917 sd_scsi_probe_cache_head = NULL;
2918 }
2919
2920
2921 /*
2922 * Function: sd_scsi_probe_cache_fini
2923 *
2924 * Description: Frees all resources associated with the probe response cache.
2925 *
2926 * Context: Kernel thread context
2927 */
2928
2929 static void
2930 sd_scsi_probe_cache_fini(void)
2931 {
2932 struct sd_scsi_probe_cache *cp;
2933 struct sd_scsi_probe_cache *ncp;
2934
2935 /* Clean up our smart probing linked list */
2936 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2937 ncp = cp->next;
2938 kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2939 }
2940 sd_scsi_probe_cache_head = NULL;
2941 mutex_destroy(&sd_scsi_probe_cache_mutex);
2942 }
2943
2944
2945 /*
2946 * Function: sd_scsi_clear_probe_cache
2947 *
2948 * Description: This routine clears the probe response cache. This is
2949 * done when open() returns ENXIO so that when deferred
2950 * attach is attempted (possibly after a device has been
2951 * turned on) we will retry the probe. Since we don't know
2952 * which target we failed to open, we just clear the
2953 * entire cache.
2954 *
2955 * Context: Kernel thread context
2956 */
2957
2958 static void
2959 sd_scsi_clear_probe_cache(void)
2960 {
2961 struct sd_scsi_probe_cache *cp;
2962 int i;
2963
2964 mutex_enter(&sd_scsi_probe_cache_mutex);
2965 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2966 /*
2967 * Reset all entries to SCSIPROBE_EXISTS. This will
2968 * force probing to be performed the next time
2969 * sd_scsi_probe_with_cache is called.
2970 */
2971 for (i = 0; i < NTARGETS_WIDE; i++) {
2972 cp->cache[i] = SCSIPROBE_EXISTS;
2973 }
2974 }
2975 mutex_exit(&sd_scsi_probe_cache_mutex);
2976 }
2977
2978
2979 /*
2980 * Function: sd_scsi_probe_with_cache
2981 *
2982 * Description: This routine implements support for a scsi device probe
2983 * with cache. The driver maintains a cache of the target
2984 * responses to scsi probes. If we get no response from a
2985 * target during a probe inquiry, we remember that, and we
2986 * avoid additional calls to scsi_probe on non-zero LUNs
2987 * on the same target until the cache is cleared. By doing
2988 * so we avoid the 1/4 sec selection timeout for nonzero
2989 * LUNs. lun0 of a target is always probed.
2990 *
2991 * Arguments: devp - Pointer to a scsi_device(9S) structure
2992 * waitfunc - indicates what the allocator routines should
2993 * do when resources are not available. This value
2994 * is passed on to scsi_probe() when that routine
2995 * is called.
2996 *
2997 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2998 * otherwise the value returned by scsi_probe(9F).
2999 *
3000 * Context: Kernel thread context
3001 */
3002
3003 static int
3004 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3005 {
3006 struct sd_scsi_probe_cache *cp;
3007 dev_info_t *pdip = ddi_get_parent(devp->sd_dev);
3008 int lun, tgt;
3009
3010 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3011 SCSI_ADDR_PROP_LUN, 0);
3012 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3013 SCSI_ADDR_PROP_TARGET, -1);
3014
3015 /* Make sure caching enabled and target in range */
3016 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3017 /* do it the old way (no cache) */
3018 return (scsi_probe(devp, waitfn));
3019 }
3020
3021 mutex_enter(&sd_scsi_probe_cache_mutex);
3022
3023 /* Find the cache for this scsi bus instance */
3024 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3025 if (cp->pdip == pdip) {
3026 break;
3027 }
3028 }
3029
3030 /* If we can't find a cache for this pdip, create one */
3031 if (cp == NULL) {
3032 int i;
3033
3034 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3035 KM_SLEEP);
3036 cp->pdip = pdip;
3037 cp->next = sd_scsi_probe_cache_head;
3038 sd_scsi_probe_cache_head = cp;
3039 for (i = 0; i < NTARGETS_WIDE; i++) {
3040 cp->cache[i] = SCSIPROBE_EXISTS;
3041 }
3042 }
3043
3044 mutex_exit(&sd_scsi_probe_cache_mutex);
3045
3046 /* Recompute the cache for this target if LUN zero */
3047 if (lun == 0) {
3048 cp->cache[tgt] = SCSIPROBE_EXISTS;
3049 }
3050
3051 /* Don't probe if cache remembers a NORESP from a previous LUN. */
3052 if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3053 return (SCSIPROBE_NORESP);
3054 }
3055
3056 /* Do the actual probe; save & return the result */
3057 return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3058 }
3059
3060
3061 /*
3062 * Function: sd_scsi_target_lun_init
3063 *
3064 * Description: Initializes the attached lun chain mutex and head pointer.
3065 *
3066 * Context: Kernel thread context
3067 */
3068
3069 static void
3070 sd_scsi_target_lun_init(void)
3071 {
3072 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3073 sd_scsi_target_lun_head = NULL;
3074 }
3075
3076
3077 /*
3078 * Function: sd_scsi_target_lun_fini
3079 *
3080 * Description: Frees all resources associated with the attached lun
3081 * chain
3082 *
3083 * Context: Kernel thread context
3084 */
3085
3086 static void
3087 sd_scsi_target_lun_fini(void)
3088 {
3089 struct sd_scsi_hba_tgt_lun *cp;
3090 struct sd_scsi_hba_tgt_lun *ncp;
3091
3092 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3093 ncp = cp->next;
3094 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3095 }
3096 sd_scsi_target_lun_head = NULL;
3097 mutex_destroy(&sd_scsi_target_lun_mutex);
3098 }
3099
3100
3101 /*
3102 * Function: sd_scsi_get_target_lun_count
3103 *
3104 * Description: This routine will check in the attached lun chain to see
3105 * how many luns are attached on the required SCSI controller
3106 * and target. Currently, some capabilities like tagged queue
3107 * are supported per target based by HBA. So all luns in a
3108 * target have the same capabilities. Based on this assumption,
3109 * sd should only set these capabilities once per target. This
3110 * function is called when sd needs to decide how many luns
3111 * already attached on a target.
3112 *
3113 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
3114 * controller device.
3115 * target - The target ID on the controller's SCSI bus.
3116 *
3117 * Return Code: The number of luns attached on the required target and
3118 * controller.
3119 * -1 if target ID is not in parallel SCSI scope or the given
3120 * dip is not in the chain.
3121 *
3122 * Context: Kernel thread context
3123 */
3124
3125 static int
3126 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3127 {
3128 struct sd_scsi_hba_tgt_lun *cp;
3129
3130 if ((target < 0) || (target >= NTARGETS_WIDE)) {
3131 return (-1);
3132 }
3133
3134 mutex_enter(&sd_scsi_target_lun_mutex);
3135
3136 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3137 if (cp->pdip == dip) {
3138 break;
3139 }
3140 }
3141
3142 mutex_exit(&sd_scsi_target_lun_mutex);
3143
3144 if (cp == NULL) {
3145 return (-1);
3146 }
3147
3148 return (cp->nlun[target]);
3149 }
3150
3151
3152 /*
3153 * Function: sd_scsi_update_lun_on_target
3154 *
3155 * Description: This routine is used to update the attached lun chain when a
3156 * lun is attached or detached on a target.
3157 *
3158 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
3159 * controller device.
3160 * target - The target ID on the controller's SCSI bus.
3161 * flag - Indicate the lun is attached or detached.
3162 *
3163 * Context: Kernel thread context
3164 */
3165
3166 static void
3167 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3168 {
3169 struct sd_scsi_hba_tgt_lun *cp;
3170
3171 mutex_enter(&sd_scsi_target_lun_mutex);
3172
3173 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3174 if (cp->pdip == dip) {
3175 break;
3176 }
3177 }
3178
3179 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3180 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3181 KM_SLEEP);
3182 cp->pdip = dip;
3183 cp->next = sd_scsi_target_lun_head;
3184 sd_scsi_target_lun_head = cp;
3185 }
3186
3187 mutex_exit(&sd_scsi_target_lun_mutex);
3188
3189 if (cp != NULL) {
3190 if (flag == SD_SCSI_LUN_ATTACH) {
3191 cp->nlun[target] ++;
3192 } else {
3193 cp->nlun[target] --;
3194 }
3195 }
3196 }
3197
3198
3199 /*
3200 * Function: sd_spin_up_unit
3201 *
3202 * Description: Issues the following commands to spin-up the device:
3203 * START STOP UNIT, and INQUIRY.
3204 *
3205 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3206 * structure for this target.
3207 *
3208 * Return Code: 0 - success
3209 * EIO - failure
3210 * EACCES - reservation conflict
3211 *
3212 * Context: Kernel thread context
3213 */
3214
3215 static int
3216 sd_spin_up_unit(sd_ssc_t *ssc)
3217 {
3218 size_t resid = 0;
3219 int has_conflict = FALSE;
3220 uchar_t *bufaddr;
3221 int status;
3222 struct sd_lun *un;
3223
3224 ASSERT(ssc != NULL);
3225 un = ssc->ssc_un;
3226 ASSERT(un != NULL);
3227
3228 /*
3229 * Send a throwaway START UNIT command.
3230 *
3231 * If we fail on this, we don't care presently what precisely
3232 * is wrong. EMC's arrays will also fail this with a check
3233 * condition (0x2/0x4/0x3) if the device is "inactive," but
3234 * we don't want to fail the attach because it may become
3235 * "active" later.
3236 * We don't know if power condition is supported or not at
3237 * this stage, use START STOP bit.
3238 */
3239 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3240 SD_TARGET_START, SD_PATH_DIRECT);
3241
3242 if (status != 0) {
3243 if (status == EACCES)
3244 has_conflict = TRUE;
3245 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3246 }
3247
3248 /*
3249 * Send another INQUIRY command to the target. This is necessary for
3250 * non-removable media direct access devices because their INQUIRY data
3251 * may not be fully qualified until they are spun up (perhaps via the
3252 * START command above). Note: This seems to be needed for some
3253 * legacy devices only.) The INQUIRY command should succeed even if a
3254 * Reservation Conflict is present.
3255 */
3256 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3257
3258 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3259 != 0) {
3260 kmem_free(bufaddr, SUN_INQSIZE);
3261 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3262 return (EIO);
3263 }
3264
3265 /*
3266 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3267 * Note that this routine does not return a failure here even if the
3268 * INQUIRY command did not return any data. This is a legacy behavior.
3269 */
3270 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3271 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3272 }
3273
3274 kmem_free(bufaddr, SUN_INQSIZE);
3275
3276 /* If we hit a reservation conflict above, tell the caller. */
3277 if (has_conflict == TRUE) {
3278 return (EACCES);
3279 }
3280
3281 return (0);
3282 }
3283
3284 #ifdef _LP64
3285 /*
3286 * Function: sd_enable_descr_sense
3287 *
3288 * Description: This routine attempts to select descriptor sense format
3289 * using the Control mode page. Devices that support 64 bit
3290 * LBAs (for >2TB luns) should also implement descriptor
3291 * sense data so we will call this function whenever we see
3292 * a lun larger than 2TB. If for some reason the device
3293 * supports 64 bit LBAs but doesn't support descriptor sense
3294 * presumably the mode select will fail. Everything will
3295 * continue to work normally except that we will not get
3296 * complete sense data for commands that fail with an LBA
3297 * larger than 32 bits.
3298 *
3299 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3300 * structure for this target.
3301 *
3302 * Context: Kernel thread context only
3303 */
3304
3305 static void
3306 sd_enable_descr_sense(sd_ssc_t *ssc)
3307 {
3308 uchar_t *header;
3309 struct mode_control_scsi3 *ctrl_bufp;
3310 size_t buflen;
3311 size_t bd_len;
3312 int status;
3313 struct sd_lun *un;
3314
3315 ASSERT(ssc != NULL);
3316 un = ssc->ssc_un;
3317 ASSERT(un != NULL);
3318
3319 /*
3320 * Read MODE SENSE page 0xA, Control Mode Page
3321 */
3322 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3323 sizeof (struct mode_control_scsi3);
3324 header = kmem_zalloc(buflen, KM_SLEEP);
3325
3326 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3327 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3328
3329 if (status != 0) {
3330 SD_ERROR(SD_LOG_COMMON, un,
3331 "sd_enable_descr_sense: mode sense ctrl page failed\n");
3332 goto eds_exit;
3333 }
3334
3335 /*
3336 * Determine size of Block Descriptors in order to locate
3337 * the mode page data. ATAPI devices return 0, SCSI devices
3338 * should return MODE_BLK_DESC_LENGTH.
3339 */
3340 bd_len = ((struct mode_header *)header)->bdesc_length;
3341
3342 /* Clear the mode data length field for MODE SELECT */
3343 ((struct mode_header *)header)->length = 0;
3344
3345 ctrl_bufp = (struct mode_control_scsi3 *)
3346 (header + MODE_HEADER_LENGTH + bd_len);
3347
3348 /*
3349 * If the page length is smaller than the expected value,
3350 * the target device doesn't support D_SENSE. Bail out here.
3351 */
3352 if (ctrl_bufp->mode_page.length <
3353 sizeof (struct mode_control_scsi3) - 2) {
3354 SD_ERROR(SD_LOG_COMMON, un,
3355 "sd_enable_descr_sense: enable D_SENSE failed\n");
3356 goto eds_exit;
3357 }
3358
3359 /*
3360 * Clear PS bit for MODE SELECT
3361 */
3362 ctrl_bufp->mode_page.ps = 0;
3363
3364 /*
3365 * Set D_SENSE to enable descriptor sense format.
3366 */
3367 ctrl_bufp->d_sense = 1;
3368
3369 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3370
3371 /*
3372 * Use MODE SELECT to commit the change to the D_SENSE bit
3373 */
3374 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3375 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3376
3377 if (status != 0) {
3378 SD_INFO(SD_LOG_COMMON, un,
3379 "sd_enable_descr_sense: mode select ctrl page failed\n");
3380 } else {
3381 kmem_free(header, buflen);
3382 return;
3383 }
3384
3385 eds_exit:
3386 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3387 kmem_free(header, buflen);
3388 }
3389
3390 /*
3391 * Function: sd_reenable_dsense_task
3392 *
3393 * Description: Re-enable descriptor sense after device or bus reset
3394 *
3395 * Context: Executes in a taskq() thread context
3396 */
3397 static void
3398 sd_reenable_dsense_task(void *arg)
3399 {
3400 struct sd_lun *un = arg;
3401 sd_ssc_t *ssc;
3402
3403 ASSERT(un != NULL);
3404
3405 ssc = sd_ssc_init(un);
3406 sd_enable_descr_sense(ssc);
3407 sd_ssc_fini(ssc);
3408 }
3409 #endif /* _LP64 */
3410
3411 /*
3412 * Function: sd_set_mmc_caps
3413 *
3414 * Description: This routine determines if the device is MMC compliant and if
3415 * the device supports CDDA via a mode sense of the CDVD
3416 * capabilities mode page. Also checks if the device is a
3417 * dvdram writable device.
3418 *
3419 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3420 * structure for this target.
3421 *
3422 * Context: Kernel thread context only
3423 */
3424
3425 static void
3426 sd_set_mmc_caps(sd_ssc_t *ssc)
3427 {
3428 struct mode_header_grp2 *sense_mhp;
3429 uchar_t *sense_page;
3430 caddr_t buf;
3431 int bd_len;
3432 int status;
3433 struct uscsi_cmd com;
3434 int rtn;
3435 uchar_t *out_data_rw, *out_data_hd;
3436 uchar_t *rqbuf_rw, *rqbuf_hd;
3437 uchar_t *out_data_gesn;
3438 int gesn_len;
3439 struct sd_lun *un;
3440
3441 ASSERT(ssc != NULL);
3442 un = ssc->ssc_un;
3443 ASSERT(un != NULL);
3444
3445 /*
3446 * The flags which will be set in this function are - mmc compliant,
3447 * dvdram writable device, cdda support. Initialize them to FALSE
3448 * and if a capability is detected - it will be set to TRUE.
3449 */
3450 un->un_f_mmc_cap = FALSE;
3451 un->un_f_dvdram_writable_device = FALSE;
3452 un->un_f_cfg_cdda = FALSE;
3453
3454 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3455 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3456 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3457
3458 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3459
3460 if (status != 0) {
3461 /* command failed; just return */
3462 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3463 return;
3464 }
3465 /*
3466 * If the mode sense request for the CDROM CAPABILITIES
3467 * page (0x2A) succeeds the device is assumed to be MMC.
3468 */
3469 un->un_f_mmc_cap = TRUE;
3470
3471 /* See if GET STATUS EVENT NOTIFICATION is supported */
3472 if (un->un_f_mmc_gesn_polling) {
3473 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3474 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3475
3476 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3477 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3478
3479 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3480
3481 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3482 un->un_f_mmc_gesn_polling = FALSE;
3483 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3484 "sd_set_mmc_caps: gesn not supported "
3485 "%d %x %x %x %x\n", rtn,
3486 out_data_gesn[0], out_data_gesn[1],
3487 out_data_gesn[2], out_data_gesn[3]);
3488 }
3489
3490 kmem_free(out_data_gesn, gesn_len);
3491 }
3492
3493 /* Get to the page data */
3494 sense_mhp = (struct mode_header_grp2 *)buf;
3495 bd_len = (sense_mhp->bdesc_length_hi << 8) |
3496 sense_mhp->bdesc_length_lo;
3497 if (bd_len > MODE_BLK_DESC_LENGTH) {
3498 /*
3499 * We did not get back the expected block descriptor
3500 * length so we cannot determine if the device supports
3501 * CDDA. However, we still indicate the device is MMC
3502 * according to the successful response to the page
3503 * 0x2A mode sense request.
3504 */
3505 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3506 "sd_set_mmc_caps: Mode Sense returned "
3507 "invalid block descriptor length\n");
3508 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3509 return;
3510 }
3511
3512 /* See if read CDDA is supported */
3513 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3514 bd_len);
3515 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3516
3517 /* See if writing DVD RAM is supported. */
3518 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3519 if (un->un_f_dvdram_writable_device == TRUE) {
3520 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3521 return;
3522 }
3523
3524 /*
3525 * If the device presents DVD or CD capabilities in the mode
3526 * page, we can return here since a RRD will not have
3527 * these capabilities.
3528 */
3529 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3530 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3531 return;
3532 }
3533 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3534
3535 /*
3536 * If un->un_f_dvdram_writable_device is still FALSE,
3537 * check for a Removable Rigid Disk (RRD). A RRD
3538 * device is identified by the features RANDOM_WRITABLE and
3539 * HARDWARE_DEFECT_MANAGEMENT.
3540 */
3541 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3542 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3543
3544 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3545 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3546 RANDOM_WRITABLE, SD_PATH_STANDARD);
3547
3548 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3549
3550 if (rtn != 0) {
3551 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3552 kmem_free(rqbuf_rw, SENSE_LENGTH);
3553 return;
3554 }
3555
3556 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3557 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3558
3559 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3560 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3561 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3562
3563 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3564
3565 if (rtn == 0) {
3566 /*
3567 * We have good information, check for random writable
3568 * and hardware defect features.
3569 */
3570 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3571 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3572 un->un_f_dvdram_writable_device = TRUE;
3573 }
3574 }
3575
3576 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3577 kmem_free(rqbuf_rw, SENSE_LENGTH);
3578 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3579 kmem_free(rqbuf_hd, SENSE_LENGTH);
3580 }
3581
3582 /*
3583 * Function: sd_check_for_writable_cd
3584 *
3585 * Description: This routine determines if the media in the device is
3586 * writable or not. It uses the get configuration command (0x46)
3587 * to determine if the media is writable
3588 *
3589 * Arguments: un - driver soft state (unit) structure
3590 * path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3591 * chain and the normal command waitq, or
3592 * SD_PATH_DIRECT_PRIORITY to use the USCSI
3593 * "direct" chain and bypass the normal command
3594 * waitq.
3595 *
3596 * Context: Never called at interrupt context.
3597 */
3598
3599 static void
3600 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3601 {
3602 struct uscsi_cmd com;
3603 uchar_t *out_data;
3604 uchar_t *rqbuf;
3605 int rtn;
3606 uchar_t *out_data_rw, *out_data_hd;
3607 uchar_t *rqbuf_rw, *rqbuf_hd;
3608 struct mode_header_grp2 *sense_mhp;
3609 uchar_t *sense_page;
3610 caddr_t buf;
3611 int bd_len;
3612 int status;
3613 struct sd_lun *un;
3614
3615 ASSERT(ssc != NULL);
3616 un = ssc->ssc_un;
3617 ASSERT(un != NULL);
3618 ASSERT(mutex_owned(SD_MUTEX(un)));
3619
3620 /*
3621 * Initialize the writable media to false, if configuration info.
3622 * tells us otherwise then only we will set it.
3623 */
3624 un->un_f_mmc_writable_media = FALSE;
3625 mutex_exit(SD_MUTEX(un));
3626
3627 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3628 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3629
3630 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3631 out_data, SD_PROFILE_HEADER_LEN, path_flag);
3632
3633 if (rtn != 0)
3634 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3635
3636 mutex_enter(SD_MUTEX(un));
3637 if (rtn == 0) {
3638 /*
3639 * We have good information, check for writable DVD.
3640 */
3641 if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3642 un->un_f_mmc_writable_media = TRUE;
3643 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3644 kmem_free(rqbuf, SENSE_LENGTH);
3645 return;
3646 }
3647 }
3648
3649 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3650 kmem_free(rqbuf, SENSE_LENGTH);
3651
3652 /*
3653 * Determine if this is a RRD type device.
3654 */
3655 mutex_exit(SD_MUTEX(un));
3656 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3657 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3658 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3659
3660 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3661
3662 mutex_enter(SD_MUTEX(un));
3663 if (status != 0) {
3664 /* command failed; just return */
3665 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3666 return;
3667 }
3668
3669 /* Get to the page data */
3670 sense_mhp = (struct mode_header_grp2 *)buf;
3671 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3672 if (bd_len > MODE_BLK_DESC_LENGTH) {
3673 /*
3674 * We did not get back the expected block descriptor length so
3675 * we cannot check the mode page.
3676 */
3677 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3678 "sd_check_for_writable_cd: Mode Sense returned "
3679 "invalid block descriptor length\n");
3680 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3681 return;
3682 }
3683
3684 /*
3685 * If the device presents DVD or CD capabilities in the mode
3686 * page, we can return here since a RRD device will not have
3687 * these capabilities.
3688 */
3689 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3690 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3691 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3692 return;
3693 }
3694 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3695
3696 /*
3697 * If un->un_f_mmc_writable_media is still FALSE,
3698 * check for RRD type media. A RRD device is identified
3699 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3700 */
3701 mutex_exit(SD_MUTEX(un));
3702 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3703 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3704
3705 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3706 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3707 RANDOM_WRITABLE, path_flag);
3708
3709 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3710 if (rtn != 0) {
3711 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3712 kmem_free(rqbuf_rw, SENSE_LENGTH);
3713 mutex_enter(SD_MUTEX(un));
3714 return;
3715 }
3716
3717 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3718 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3719
3720 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3721 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3722 HARDWARE_DEFECT_MANAGEMENT, path_flag);
3723
3724 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3725 mutex_enter(SD_MUTEX(un));
3726 if (rtn == 0) {
3727 /*
3728 * We have good information, check for random writable
3729 * and hardware defect features as current.
3730 */
3731 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3732 (out_data_rw[10] & 0x1) &&
3733 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3734 (out_data_hd[10] & 0x1)) {
3735 un->un_f_mmc_writable_media = TRUE;
3736 }
3737 }
3738
3739 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3740 kmem_free(rqbuf_rw, SENSE_LENGTH);
3741 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3742 kmem_free(rqbuf_hd, SENSE_LENGTH);
3743 }
3744
3745 /*
3746 * Function: sd_read_unit_properties
3747 *
3748 * Description: The following implements a property lookup mechanism.
3749 * Properties for particular disks (keyed on vendor, model
3750 * and rev numbers) are sought in the sd.conf file via
3751 * sd_process_sdconf_file(), and if not found there, are
3752 * looked for in a list hardcoded in this driver via
3753 * sd_process_sdconf_table() Once located the properties
3754 * are used to update the driver unit structure.
3755 *
3756 * Arguments: un - driver soft state (unit) structure
3757 */
3758
3759 static void
3760 sd_read_unit_properties(struct sd_lun *un)
3761 {
3762 /*
3763 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3764 * the "sd-config-list" property (from the sd.conf file) or if
3765 * there was not a match for the inquiry vid/pid. If this event
3766 * occurs the static driver configuration table is searched for
3767 * a match.
3768 */
3769 ASSERT(un != NULL);
3770 if (sd_process_sdconf_file(un) == SD_FAILURE) {
3771 sd_process_sdconf_table(un);
3772 }
3773
3774 /* check for LSI device */
3775 sd_is_lsi(un);
3776
3777
3778 }
3779
3780
3781 /*
3782 * Function: sd_process_sdconf_file
3783 *
3784 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3785 * driver's config file (ie, sd.conf) and update the driver
3786 * soft state structure accordingly.
3787 *
3788 * Arguments: un - driver soft state (unit) structure
3789 *
3790 * Return Code: SD_SUCCESS - The properties were successfully set according
3791 * to the driver configuration file.
3792 * SD_FAILURE - The driver config list was not obtained or
3793 * there was no vid/pid match. This indicates that
3794 * the static config table should be used.
3795 *
3796 * The config file has a property, "sd-config-list". Currently we support
3797 * two kinds of formats. For both formats, the value of this property
3798 * is a list of duplets:
3799 *
3800 * sd-config-list=
3801 * <duplet>,
3802 * [,<duplet>]*;
3803 *
3804 * For the improved format, where
3805 *
3806 * <duplet>:= "<vid+pid>","<tunable-list>"
3807 *
3808 * and
3809 *
3810 * <tunable-list>:= <tunable> [, <tunable> ]*;
3811 * <tunable> = <name> : <value>
3812 *
3813 * The <vid+pid> is the string that is returned by the target device on a
3814 * SCSI inquiry command, the <tunable-list> contains one or more tunables
3815 * to apply to all target devices with the specified <vid+pid>.
3816 *
3817 * Each <tunable> is a "<name> : <value>" pair.
3818 *
3819 * For the old format, the structure of each duplet is as follows:
3820 *
3821 * <duplet>:= "<vid+pid>","<data-property-name_list>"
3822 *
3823 * The first entry of the duplet is the device ID string (the concatenated
3824 * vid & pid; not to be confused with a device_id). This is defined in
3825 * the same way as in the sd_disk_table.
3826 *
3827 * The second part of the duplet is a string that identifies a
3828 * data-property-name-list. The data-property-name-list is defined as
3829 * follows:
3830 *
3831 * <data-property-name-list>:=<data-property-name> [<data-property-name>]
3832 *
3833 * The syntax of <data-property-name> depends on the <version> field.
3834 *
3835 * If version = SD_CONF_VERSION_1 we have the following syntax:
3836 *
3837 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3838 *
3839 * where the prop0 value will be used to set prop0 if bit0 set in the
3840 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3841 *
3842 */
3843
3844 static int
3845 sd_process_sdconf_file(struct sd_lun *un)
3846 {
3847 char **config_list = NULL;
3848 uint_t nelements;
3849 char *vidptr;
3850 int vidlen;
3851 char *dnlist_ptr;
3852 char *dataname_ptr;
3853 char *dataname_lasts;
3854 int *data_list = NULL;
3855 uint_t data_list_len;
3856 int rval = SD_FAILURE;
3857 int i;
3858
3859 ASSERT(un != NULL);
3860
3861 /* Obtain the configuration list associated with the .conf file */
3862 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3863 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3864 &config_list, &nelements) != DDI_PROP_SUCCESS) {
3865 return (SD_FAILURE);
3866 }
3867
3868 /*
3869 * Compare vids in each duplet to the inquiry vid - if a match is
3870 * made, get the data value and update the soft state structure
3871 * accordingly.
3872 *
3873 * Each duplet should show as a pair of strings, return SD_FAILURE
3874 * otherwise.
3875 */
3876 if (nelements & 1) {
3877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3878 "sd-config-list should show as pairs of strings.\n");
3879 if (config_list)
3880 ddi_prop_free(config_list);
3881 return (SD_FAILURE);
3882 }
3883
3884 for (i = 0; i < nelements; i += 2) {
3885 /*
3886 * Note: The assumption here is that each vid entry is on
3887 * a unique line from its associated duplet.
3888 */
3889 vidptr = config_list[i];
3890 vidlen = (int)strlen(vidptr);
3891 if ((vidlen == 0) ||
3892 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3893 continue;
3894 }
3895
3896 /*
3897 * dnlist contains 1 or more blank separated
3898 * data-property-name entries
3899 */
3900 dnlist_ptr = config_list[i + 1];
3901
3902 if (strchr(dnlist_ptr, ':') != NULL) {
3903 /*
3904 * Decode the improved format sd-config-list.
3905 */
3906 sd_nvpair_str_decode(un, dnlist_ptr);
3907 } else {
3908 /*
3909 * The old format sd-config-list, loop through all
3910 * data-property-name entries in the
3911 * data-property-name-list
3912 * setting the properties for each.
3913 */
3914 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3915 &dataname_lasts); dataname_ptr != NULL;
3916 dataname_ptr = sd_strtok_r(NULL, " \t",
3917 &dataname_lasts)) {
3918 int version;
3919
3920 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3921 "sd_process_sdconf_file: disk:%s, "
3922 "data:%s\n", vidptr, dataname_ptr);
3923
3924 /* Get the data list */
3925 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3926 SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3927 &data_list_len) != DDI_PROP_SUCCESS) {
3928 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3929 "sd_process_sdconf_file: data "
3930 "property (%s) has no value\n",
3931 dataname_ptr);
3932 continue;
3933 }
3934
3935 version = data_list[0];
3936
3937 if (version == SD_CONF_VERSION_1) {
3938 sd_tunables values;
3939
3940 /* Set the properties */
3941 if (sd_chk_vers1_data(un, data_list[1],
3942 &data_list[2], data_list_len,
3943 dataname_ptr) == SD_SUCCESS) {
3944 sd_get_tunables_from_conf(un,
3945 data_list[1], &data_list[2],
3946 &values);
3947 sd_set_vers1_properties(un,
3948 data_list[1], &values);
3949 rval = SD_SUCCESS;
3950 } else {
3951 rval = SD_FAILURE;
3952 }
3953 } else {
3954 scsi_log(SD_DEVINFO(un), sd_label,
3955 CE_WARN, "data property %s version "
3956 "0x%x is invalid.",
3957 dataname_ptr, version);
3958 rval = SD_FAILURE;
3959 }
3960 if (data_list)
3961 ddi_prop_free(data_list);
3962 }
3963 }
3964 }
3965
3966 /* free up the memory allocated by ddi_prop_lookup_string_array(). */
3967 if (config_list) {
3968 ddi_prop_free(config_list);
3969 }
3970
3971 return (rval);
3972 }
3973
3974 /*
3975 * Function: sd_nvpair_str_decode()
3976 *
3977 * Description: Parse the improved format sd-config-list to get
3978 * each entry of tunable, which includes a name-value pair.
3979 * Then call sd_set_properties() to set the property.
3980 *
3981 * Arguments: un - driver soft state (unit) structure
3982 * nvpair_str - the tunable list
3983 */
3984 static void
3985 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3986 {
3987 char *nv, *name, *value, *token;
3988 char *nv_lasts, *v_lasts, *x_lasts;
3989
3990 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3991 nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3992 token = sd_strtok_r(nv, ":", &v_lasts);
3993 name = sd_strtok_r(token, " \t", &x_lasts);
3994 token = sd_strtok_r(NULL, ":", &v_lasts);
3995 value = sd_strtok_r(token, " \t", &x_lasts);
3996 if (name == NULL || value == NULL) {
3997 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3998 "sd_nvpair_str_decode: "
3999 "name or value is not valid!\n");
4000 } else {
4001 sd_set_properties(un, name, value);
4002 }
4003 }
4004 }
4005
4006 /*
4007 * Function: sd_strtok_r()
4008 *
4009 * Description: This function uses strpbrk and strspn to break
4010 * string into tokens on sequentially subsequent calls. Return
4011 * NULL when no non-separator characters remain. The first
4012 * argument is NULL for subsequent calls.
4013 */
4014 static char *
4015 sd_strtok_r(char *string, const char *sepset, char **lasts)
4016 {
4017 char *q, *r;
4018
4019 /* First or subsequent call */
4020 if (string == NULL)
4021 string = *lasts;
4022
4023 if (string == NULL)
4024 return (NULL);
4025
4026 /* Skip leading separators */
4027 q = string + strspn(string, sepset);
4028
4029 if (*q == '\0')
4030 return (NULL);
4031
4032 if ((r = strpbrk(q, sepset)) == NULL)
4033 *lasts = NULL;
4034 else {
4035 *r = '\0';
4036 *lasts = r + 1;
4037 }
4038 return (q);
4039 }
4040
4041 /*
4042 * Function: sd_set_properties()
4043 *
4044 * Description: Set device properties based on the improved
4045 * format sd-config-list.
4046 *
4047 * Arguments: un - driver soft state (unit) structure
4048 * name - supported tunable name
4049 * value - tunable value
4050 */
4051 static void
4052 sd_set_properties(struct sd_lun *un, char *name, char *value)
4053 {
4054 char *endptr = NULL;
4055 long val = 0;
4056
4057 if (strcasecmp(name, "cache-nonvolatile") == 0) {
4058 if (strcasecmp(value, "true") == 0) {
4059 un->un_f_suppress_cache_flush = TRUE;
4060 } else if (strcasecmp(value, "false") == 0) {
4061 un->un_f_suppress_cache_flush = FALSE;
4062 } else {
4063 goto value_invalid;
4064 }
4065 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4066 "suppress_cache_flush flag set to %d\n",
4067 un->un_f_suppress_cache_flush);
4068 return;
4069 }
4070
4071 if (strcasecmp(name, "controller-type") == 0) {
4072 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4073 un->un_ctype = val;
4074 } else {
4075 goto value_invalid;
4076 }
4077 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4078 "ctype set to %d\n", un->un_ctype);
4079 return;
4080 }
4081
4082 if (strcasecmp(name, "delay-busy") == 0) {
4083 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4084 un->un_busy_timeout = drv_usectohz(val / 1000);
4085 } else {
4086 goto value_invalid;
4087 }
4088 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4089 "busy_timeout set to %d\n", un->un_busy_timeout);
4090 return;
4091 }
4092
4093 if (strcasecmp(name, "disksort") == 0) {
4094 if (strcasecmp(value, "true") == 0) {
4095 un->un_f_disksort_disabled = FALSE;
4096 } else if (strcasecmp(value, "false") == 0) {
4097 un->un_f_disksort_disabled = TRUE;
4098 } else {
4099 goto value_invalid;
4100 }
4101 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4102 "disksort disabled flag set to %d\n",
4103 un->un_f_disksort_disabled);
4104 return;
4105 }
4106
4107 if (strcasecmp(name, "power-condition") == 0) {
4108 if (strcasecmp(value, "true") == 0) {
4109 un->un_f_power_condition_disabled = FALSE;
4110 } else if (strcasecmp(value, "false") == 0) {
4111 un->un_f_power_condition_disabled = TRUE;
4112 } else {
4113 goto value_invalid;
4114 }
4115 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4116 "power condition disabled flag set to %d\n",
4117 un->un_f_power_condition_disabled);
4118 return;
4119 }
4120
4121 if (strcasecmp(name, "timeout-releasereservation") == 0) {
4122 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4123 un->un_reserve_release_time = val;
4124 } else {
4125 goto value_invalid;
4126 }
4127 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4128 "reservation release timeout set to %d\n",
4129 un->un_reserve_release_time);
4130 return;
4131 }
4132
4133 if (strcasecmp(name, "reset-lun") == 0) {
4134 if (strcasecmp(value, "true") == 0) {
4135 un->un_f_lun_reset_enabled = TRUE;
4136 } else if (strcasecmp(value, "false") == 0) {
4137 un->un_f_lun_reset_enabled = FALSE;
4138 } else {
4139 goto value_invalid;
4140 }
4141 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4142 "lun reset enabled flag set to %d\n",
4143 un->un_f_lun_reset_enabled);
4144 return;
4145 }
4146
4147 if (strcasecmp(name, "retries-busy") == 0) {
4148 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4149 un->un_busy_retry_count = val;
4150 } else {
4151 goto value_invalid;
4152 }
4153 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4154 "busy retry count set to %d\n", un->un_busy_retry_count);
4155 return;
4156 }
4157
4158 if (strcasecmp(name, "retries-timeout") == 0) {
4159 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4160 un->un_retry_count = val;
4161 } else {
4162 goto value_invalid;
4163 }
4164 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4165 "timeout retry count set to %d\n", un->un_retry_count);
4166 return;
4167 }
4168
4169 if (strcasecmp(name, "retries-notready") == 0) {
4170 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4171 un->un_notready_retry_count = val;
4172 } else {
4173 goto value_invalid;
4174 }
4175 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4176 "notready retry count set to %d\n",
4177 un->un_notready_retry_count);
4178 return;
4179 }
4180
4181 if (strcasecmp(name, "retries-reset") == 0) {
4182 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4183 un->un_reset_retry_count = val;
4184 } else {
4185 goto value_invalid;
4186 }
4187 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4188 "reset retry count set to %d\n",
4189 un->un_reset_retry_count);
4190 return;
4191 }
4192
4193 if (strcasecmp(name, "throttle-max") == 0) {
4194 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4195 un->un_saved_throttle = un->un_throttle = val;
4196 } else {
4197 goto value_invalid;
4198 }
4199 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4200 "throttle set to %d\n", un->un_throttle);
4201 }
4202
4203 if (strcasecmp(name, "throttle-min") == 0) {
4204 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4205 un->un_min_throttle = val;
4206 } else {
4207 goto value_invalid;
4208 }
4209 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4210 "min throttle set to %d\n", un->un_min_throttle);
4211 }
4212
4213 if (strcasecmp(name, "rmw-type") == 0) {
4214 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4215 un->un_f_rmw_type = val;
4216 } else {
4217 goto value_invalid;
4218 }
4219 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4220 "RMW type set to %d\n", un->un_f_rmw_type);
4221 }
4222
4223 if (strcasecmp(name, "physical-block-size") == 0) {
4224 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4225 ISP2(val) && val >= un->un_tgt_blocksize &&
4226 val >= un->un_sys_blocksize) {
4227 un->un_phy_blocksize = val;
4228 } else {
4229 goto value_invalid;
4230 }
4231 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4232 "physical block size set to %d\n", un->un_phy_blocksize);
4233 }
4234
4235 /*
4236 * Validate the throttle values.
4237 * If any of the numbers are invalid, set everything to defaults.
4238 */
4239 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4240 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4241 (un->un_min_throttle > un->un_throttle)) {
4242 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4243 un->un_min_throttle = sd_min_throttle;
4244 }
4245
4246 if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4247 if (strcasecmp(value, "true") == 0) {
4248 un->un_f_mmc_gesn_polling = TRUE;
4249 } else if (strcasecmp(value, "false") == 0) {
4250 un->un_f_mmc_gesn_polling = FALSE;
4251 } else {
4252 goto value_invalid;
4253 }
4254 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4255 "mmc-gesn-polling set to %d\n",
4256 un->un_f_mmc_gesn_polling);
4257 }
4258
4259 return;
4260
4261 value_invalid:
4262 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4263 "value of prop %s is invalid\n", name);
4264 }
4265
4266 /*
4267 * Function: sd_get_tunables_from_conf()
4268 *
4269 *
4270 * This function reads the data list from the sd.conf file and pulls
4271 * the values that can have numeric values as arguments and places
4272 * the values in the appropriate sd_tunables member.
4273 * Since the order of the data list members varies across platforms
4274 * This function reads them from the data list in a platform specific
4275 * order and places them into the correct sd_tunable member that is
4276 * consistent across all platforms.
4277 */
4278 static void
4279 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4280 sd_tunables *values)
4281 {
4282 int i;
4283 int mask;
4284
4285 bzero(values, sizeof (sd_tunables));
4286
4287 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4288
4289 mask = 1 << i;
4290 if (mask > flags) {
4291 break;
4292 }
4293
4294 switch (mask & flags) {
4295 case 0: /* This mask bit not set in flags */
4296 continue;
4297 case SD_CONF_BSET_THROTTLE:
4298 values->sdt_throttle = data_list[i];
4299 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4300 "sd_get_tunables_from_conf: throttle = %d\n",
4301 values->sdt_throttle);
4302 break;
4303 case SD_CONF_BSET_CTYPE:
4304 values->sdt_ctype = data_list[i];
4305 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4306 "sd_get_tunables_from_conf: ctype = %d\n",
4307 values->sdt_ctype);
4308 break;
4309 case SD_CONF_BSET_NRR_COUNT:
4310 values->sdt_not_rdy_retries = data_list[i];
4311 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4312 "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4313 values->sdt_not_rdy_retries);
4314 break;
4315 case SD_CONF_BSET_BSY_RETRY_COUNT:
4316 values->sdt_busy_retries = data_list[i];
4317 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4318 "sd_get_tunables_from_conf: busy_retries = %d\n",
4319 values->sdt_busy_retries);
4320 break;
4321 case SD_CONF_BSET_RST_RETRIES:
4322 values->sdt_reset_retries = data_list[i];
4323 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4324 "sd_get_tunables_from_conf: reset_retries = %d\n",
4325 values->sdt_reset_retries);
4326 break;
4327 case SD_CONF_BSET_RSV_REL_TIME:
4328 values->sdt_reserv_rel_time = data_list[i];
4329 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4330 "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4331 values->sdt_reserv_rel_time);
4332 break;
4333 case SD_CONF_BSET_MIN_THROTTLE:
4334 values->sdt_min_throttle = data_list[i];
4335 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4336 "sd_get_tunables_from_conf: min_throttle = %d\n",
4337 values->sdt_min_throttle);
4338 break;
4339 case SD_CONF_BSET_DISKSORT_DISABLED:
4340 values->sdt_disk_sort_dis = data_list[i];
4341 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4342 "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4343 values->sdt_disk_sort_dis);
4344 break;
4345 case SD_CONF_BSET_LUN_RESET_ENABLED:
4346 values->sdt_lun_reset_enable = data_list[i];
4347 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348 "sd_get_tunables_from_conf: lun_reset_enable = %d"
4349 "\n", values->sdt_lun_reset_enable);
4350 break;
4351 case SD_CONF_BSET_CACHE_IS_NV:
4352 values->sdt_suppress_cache_flush = data_list[i];
4353 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4354 "sd_get_tunables_from_conf: \
4355 suppress_cache_flush = %d"
4356 "\n", values->sdt_suppress_cache_flush);
4357 break;
4358 case SD_CONF_BSET_PC_DISABLED:
4359 values->sdt_disk_sort_dis = data_list[i];
4360 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4361 "sd_get_tunables_from_conf: power_condition_dis = "
4362 "%d\n", values->sdt_power_condition_dis);
4363 break;
4364 }
4365 }
4366 }
4367
4368 /*
4369 * Function: sd_process_sdconf_table
4370 *
4371 * Description: Search the static configuration table for a match on the
4372 * inquiry vid/pid and update the driver soft state structure
4373 * according to the table property values for the device.
4374 *
4375 * The form of a configuration table entry is:
4376 * <vid+pid>,<flags>,<property-data>
4377 * "SEAGATE ST42400N",1,0x40000,
4378 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4379 *
4380 * Arguments: un - driver soft state (unit) structure
4381 */
4382
4383 static void
4384 sd_process_sdconf_table(struct sd_lun *un)
4385 {
4386 char *id = NULL;
4387 int table_index;
4388 int idlen;
4389
4390 ASSERT(un != NULL);
4391 for (table_index = 0; table_index < sd_disk_table_size;
4392 table_index++) {
4393 id = sd_disk_table[table_index].device_id;
4394 idlen = strlen(id);
4395 if (idlen == 0) {
4396 continue;
4397 }
4398
4399 /*
4400 * The static configuration table currently does not
4401 * implement version 10 properties. Additionally,
4402 * multiple data-property-name entries are not
4403 * implemented in the static configuration table.
4404 */
4405 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4406 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4407 "sd_process_sdconf_table: disk %s\n", id);
4408 sd_set_vers1_properties(un,
4409 sd_disk_table[table_index].flags,
4410 sd_disk_table[table_index].properties);
4411 break;
4412 }
4413 }
4414 }
4415
4416
4417 /*
4418 * Function: sd_sdconf_id_match
4419 *
4420 * Description: This local function implements a case sensitive vid/pid
4421 * comparison as well as the boundary cases of wild card and
4422 * multiple blanks.
4423 *
4424 * Note: An implicit assumption made here is that the scsi
4425 * inquiry structure will always keep the vid, pid and
4426 * revision strings in consecutive sequence, so they can be
4427 * read as a single string. If this assumption is not the
4428 * case, a separate string, to be used for the check, needs
4429 * to be built with these strings concatenated.
4430 *
4431 * Arguments: un - driver soft state (unit) structure
4432 * id - table or config file vid/pid
4433 * idlen - length of the vid/pid (bytes)
4434 *
4435 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4436 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4437 */
4438
4439 static int
4440 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4441 {
4442 struct scsi_inquiry *sd_inq;
4443 int rval = SD_SUCCESS;
4444
4445 ASSERT(un != NULL);
4446 sd_inq = un->un_sd->sd_inq;
4447 ASSERT(id != NULL);
4448
4449 /*
4450 * We use the inq_vid as a pointer to a buffer containing the
4451 * vid and pid and use the entire vid/pid length of the table
4452 * entry for the comparison. This works because the inq_pid
4453 * data member follows inq_vid in the scsi_inquiry structure.
4454 */
4455 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4456 /*
4457 * The user id string is compared to the inquiry vid/pid
4458 * using a case insensitive comparison and ignoring
4459 * multiple spaces.
4460 */
4461 rval = sd_blank_cmp(un, id, idlen);
4462 if (rval != SD_SUCCESS) {
4463 /*
4464 * User id strings that start and end with a "*"
4465 * are a special case. These do not have a
4466 * specific vendor, and the product string can
4467 * appear anywhere in the 16 byte PID portion of
4468 * the inquiry data. This is a simple strstr()
4469 * type search for the user id in the inquiry data.
4470 */
4471 if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4472 char *pidptr = &id[1];
4473 int i;
4474 int j;
4475 int pidstrlen = idlen - 2;
4476 j = sizeof (SD_INQUIRY(un)->inq_pid) -
4477 pidstrlen;
4478
4479 if (j < 0) {
4480 return (SD_FAILURE);
4481 }
4482 for (i = 0; i < j; i++) {
4483 if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4484 pidptr, pidstrlen) == 0) {
4485 rval = SD_SUCCESS;
4486 break;
4487 }
4488 }
4489 }
4490 }
4491 }
4492 return (rval);
4493 }
4494
4495
4496 /*
4497 * Function: sd_blank_cmp
4498 *
4499 * Description: If the id string starts and ends with a space, treat
4500 * multiple consecutive spaces as equivalent to a single
4501 * space. For example, this causes a sd_disk_table entry
4502 * of " NEC CDROM " to match a device's id string of
4503 * "NEC CDROM".
4504 *
4505 * Note: The success exit condition for this routine is if
4506 * the pointer to the table entry is '\0' and the cnt of
4507 * the inquiry length is zero. This will happen if the inquiry
4508 * string returned by the device is padded with spaces to be
4509 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4510 * SCSI spec states that the inquiry string is to be padded with
4511 * spaces.
4512 *
4513 * Arguments: un - driver soft state (unit) structure
4514 * id - table or config file vid/pid
4515 * idlen - length of the vid/pid (bytes)
4516 *
4517 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4518 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4519 */
4520
4521 static int
4522 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4523 {
4524 char *p1;
4525 char *p2;
4526 int cnt;
4527 cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4528 sizeof (SD_INQUIRY(un)->inq_pid);
4529
4530 ASSERT(un != NULL);
4531 p2 = un->un_sd->sd_inq->inq_vid;
4532 ASSERT(id != NULL);
4533 p1 = id;
4534
4535 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4536 /*
4537 * Note: string p1 is terminated by a NUL but string p2
4538 * isn't. The end of p2 is determined by cnt.
4539 */
4540 for (;;) {
4541 /* skip over any extra blanks in both strings */
4542 while ((*p1 != '\0') && (*p1 == ' ')) {
4543 p1++;
4544 }
4545 while ((cnt != 0) && (*p2 == ' ')) {
4546 p2++;
4547 cnt--;
4548 }
4549
4550 /* compare the two strings */
4551 if ((cnt == 0) ||
4552 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4553 break;
4554 }
4555 while ((cnt > 0) &&
4556 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4557 p1++;
4558 p2++;
4559 cnt--;
4560 }
4561 }
4562 }
4563
4564 /* return SD_SUCCESS if both strings match */
4565 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4566 }
4567
4568
4569 /*
4570 * Function: sd_chk_vers1_data
4571 *
4572 * Description: Verify the version 1 device properties provided by the
4573 * user via the configuration file
4574 *
4575 * Arguments: un - driver soft state (unit) structure
4576 * flags - integer mask indicating properties to be set
4577 * prop_list - integer list of property values
4578 * list_len - number of the elements
4579 *
4580 * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4581 * SD_FAILURE - Indicates the user provided data is invalid
4582 */
4583
4584 static int
4585 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4586 int list_len, char *dataname_ptr)
4587 {
4588 int i;
4589 int mask = 1;
4590 int index = 0;
4591
4592 ASSERT(un != NULL);
4593
4594 /* Check for a NULL property name and list */
4595 if (dataname_ptr == NULL) {
4596 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4597 "sd_chk_vers1_data: NULL data property name.");
4598 return (SD_FAILURE);
4599 }
4600 if (prop_list == NULL) {
4601 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4602 "sd_chk_vers1_data: %s NULL data property list.",
4603 dataname_ptr);
4604 return (SD_FAILURE);
4605 }
4606
4607 /* Display a warning if undefined bits are set in the flags */
4608 if (flags & ~SD_CONF_BIT_MASK) {
4609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4610 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4611 "Properties not set.",
4612 (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4613 return (SD_FAILURE);
4614 }
4615
4616 /*
4617 * Verify the length of the list by identifying the highest bit set
4618 * in the flags and validating that the property list has a length
4619 * up to the index of this bit.
4620 */
4621 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4622 if (flags & mask) {
4623 index++;
4624 }
4625 mask = 1 << i;
4626 }
4627 if (list_len < (index + 2)) {
4628 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4629 "sd_chk_vers1_data: "
4630 "Data property list %s size is incorrect. "
4631 "Properties not set.", dataname_ptr);
4632 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4633 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4634 return (SD_FAILURE);
4635 }
4636 return (SD_SUCCESS);
4637 }
4638
4639
4640 /*
4641 * Function: sd_set_vers1_properties
4642 *
4643 * Description: Set version 1 device properties based on a property list
4644 * retrieved from the driver configuration file or static
4645 * configuration table. Version 1 properties have the format:
4646 *
4647 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4648 *
4649 * where the prop0 value will be used to set prop0 if bit0
4650 * is set in the flags
4651 *
4652 * Arguments: un - driver soft state (unit) structure
4653 * flags - integer mask indicating properties to be set
4654 * prop_list - integer list of property values
4655 */
4656
4657 static void
4658 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4659 {
4660 ASSERT(un != NULL);
4661
4662 /*
4663 * Set the flag to indicate cache is to be disabled. An attempt
4664 * to disable the cache via sd_cache_control() will be made
4665 * later during attach once the basic initialization is complete.
4666 */
4667 if (flags & SD_CONF_BSET_NOCACHE) {
4668 un->un_f_opt_disable_cache = TRUE;
4669 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4670 "sd_set_vers1_properties: caching disabled flag set\n");
4671 }
4672
4673 /* CD-specific configuration parameters */
4674 if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4675 un->un_f_cfg_playmsf_bcd = TRUE;
4676 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4677 "sd_set_vers1_properties: playmsf_bcd set\n");
4678 }
4679 if (flags & SD_CONF_BSET_READSUB_BCD) {
4680 un->un_f_cfg_readsub_bcd = TRUE;
4681 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4682 "sd_set_vers1_properties: readsub_bcd set\n");
4683 }
4684 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4685 un->un_f_cfg_read_toc_trk_bcd = TRUE;
4686 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4687 "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4688 }
4689 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4690 un->un_f_cfg_read_toc_addr_bcd = TRUE;
4691 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4692 "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4693 }
4694 if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4695 un->un_f_cfg_no_read_header = TRUE;
4696 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4697 "sd_set_vers1_properties: no_read_header set\n");
4698 }
4699 if (flags & SD_CONF_BSET_READ_CD_XD4) {
4700 un->un_f_cfg_read_cd_xd4 = TRUE;
4701 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4702 "sd_set_vers1_properties: read_cd_xd4 set\n");
4703 }
4704
4705 /* Support for devices which do not have valid/unique serial numbers */
4706 if (flags & SD_CONF_BSET_FAB_DEVID) {
4707 un->un_f_opt_fab_devid = TRUE;
4708 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4709 "sd_set_vers1_properties: fab_devid bit set\n");
4710 }
4711
4712 /* Support for user throttle configuration */
4713 if (flags & SD_CONF_BSET_THROTTLE) {
4714 ASSERT(prop_list != NULL);
4715 un->un_saved_throttle = un->un_throttle =
4716 prop_list->sdt_throttle;
4717 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4718 "sd_set_vers1_properties: throttle set to %d\n",
4719 prop_list->sdt_throttle);
4720 }
4721
4722 /* Set the per disk retry count according to the conf file or table. */
4723 if (flags & SD_CONF_BSET_NRR_COUNT) {
4724 ASSERT(prop_list != NULL);
4725 if (prop_list->sdt_not_rdy_retries) {
4726 un->un_notready_retry_count =
4727 prop_list->sdt_not_rdy_retries;
4728 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4729 "sd_set_vers1_properties: not ready retry count"
4730 " set to %d\n", un->un_notready_retry_count);
4731 }
4732 }
4733
4734 /* The controller type is reported for generic disk driver ioctls */
4735 if (flags & SD_CONF_BSET_CTYPE) {
4736 ASSERT(prop_list != NULL);
4737 switch (prop_list->sdt_ctype) {
4738 case CTYPE_CDROM:
4739 un->un_ctype = prop_list->sdt_ctype;
4740 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4741 "sd_set_vers1_properties: ctype set to "
4742 "CTYPE_CDROM\n");
4743 break;
4744 case CTYPE_CCS:
4745 un->un_ctype = prop_list->sdt_ctype;
4746 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4747 "sd_set_vers1_properties: ctype set to "
4748 "CTYPE_CCS\n");
4749 break;
4750 case CTYPE_ROD: /* RW optical */
4751 un->un_ctype = prop_list->sdt_ctype;
4752 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4753 "sd_set_vers1_properties: ctype set to "
4754 "CTYPE_ROD\n");
4755 break;
4756 default:
4757 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4758 "sd_set_vers1_properties: Could not set "
4759 "invalid ctype value (%d)",
4760 prop_list->sdt_ctype);
4761 }
4762 }
4763
4764 /* Purple failover timeout */
4765 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4766 ASSERT(prop_list != NULL);
4767 un->un_busy_retry_count =
4768 prop_list->sdt_busy_retries;
4769 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4770 "sd_set_vers1_properties: "
4771 "busy retry count set to %d\n",
4772 un->un_busy_retry_count);
4773 }
4774
4775 /* Purple reset retry count */
4776 if (flags & SD_CONF_BSET_RST_RETRIES) {
4777 ASSERT(prop_list != NULL);
4778 un->un_reset_retry_count =
4779 prop_list->sdt_reset_retries;
4780 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4781 "sd_set_vers1_properties: "
4782 "reset retry count set to %d\n",
4783 un->un_reset_retry_count);
4784 }
4785
4786 /* Purple reservation release timeout */
4787 if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4788 ASSERT(prop_list != NULL);
4789 un->un_reserve_release_time =
4790 prop_list->sdt_reserv_rel_time;
4791 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4792 "sd_set_vers1_properties: "
4793 "reservation release timeout set to %d\n",
4794 un->un_reserve_release_time);
4795 }
4796
4797 /*
4798 * Driver flag telling the driver to verify that no commands are pending
4799 * for a device before issuing a Test Unit Ready. This is a workaround
4800 * for a firmware bug in some Seagate eliteI drives.
4801 */
4802 if (flags & SD_CONF_BSET_TUR_CHECK) {
4803 un->un_f_cfg_tur_check = TRUE;
4804 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4805 "sd_set_vers1_properties: tur queue check set\n");
4806 }
4807
4808 if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4809 un->un_min_throttle = prop_list->sdt_min_throttle;
4810 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4811 "sd_set_vers1_properties: min throttle set to %d\n",
4812 un->un_min_throttle);
4813 }
4814
4815 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4816 un->un_f_disksort_disabled =
4817 (prop_list->sdt_disk_sort_dis != 0) ?
4818 TRUE : FALSE;
4819 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4820 "sd_set_vers1_properties: disksort disabled "
4821 "flag set to %d\n",
4822 prop_list->sdt_disk_sort_dis);
4823 }
4824
4825 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4826 un->un_f_lun_reset_enabled =
4827 (prop_list->sdt_lun_reset_enable != 0) ?
4828 TRUE : FALSE;
4829 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4830 "sd_set_vers1_properties: lun reset enabled "
4831 "flag set to %d\n",
4832 prop_list->sdt_lun_reset_enable);
4833 }
4834
4835 if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4836 un->un_f_suppress_cache_flush =
4837 (prop_list->sdt_suppress_cache_flush != 0) ?
4838 TRUE : FALSE;
4839 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4840 "sd_set_vers1_properties: suppress_cache_flush "
4841 "flag set to %d\n",
4842 prop_list->sdt_suppress_cache_flush);
4843 }
4844
4845 if (flags & SD_CONF_BSET_PC_DISABLED) {
4846 un->un_f_power_condition_disabled =
4847 (prop_list->sdt_power_condition_dis != 0) ?
4848 TRUE : FALSE;
4849 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4850 "sd_set_vers1_properties: power_condition_disabled "
4851 "flag set to %d\n",
4852 prop_list->sdt_power_condition_dis);
4853 }
4854
4855 /*
4856 * Validate the throttle values.
4857 * If any of the numbers are invalid, set everything to defaults.
4858 */
4859 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4860 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4861 (un->un_min_throttle > un->un_throttle)) {
4862 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4863 un->un_min_throttle = sd_min_throttle;
4864 }
4865 }
4866
4867 /*
4868 * Function: sd_is_lsi()
4869 *
4870 * Description: Check for lsi devices, step through the static device
4871 * table to match vid/pid.
4872 *
4873 * Args: un - ptr to sd_lun
4874 *
4875 * Notes: When creating new LSI property, need to add the new LSI property
4876 * to this function.
4877 */
4878 static void
4879 sd_is_lsi(struct sd_lun *un)
4880 {
4881 char *id = NULL;
4882 int table_index;
4883 int idlen;
4884 void *prop;
4885
4886 ASSERT(un != NULL);
4887 for (table_index = 0; table_index < sd_disk_table_size;
4888 table_index++) {
4889 id = sd_disk_table[table_index].device_id;
4890 idlen = strlen(id);
4891 if (idlen == 0) {
4892 continue;
4893 }
4894
4895 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4896 prop = sd_disk_table[table_index].properties;
4897 if (prop == &lsi_properties ||
4898 prop == &lsi_oem_properties ||
4899 prop == &lsi_properties_scsi ||
4900 prop == &symbios_properties) {
4901 un->un_f_cfg_is_lsi = TRUE;
4902 }
4903 break;
4904 }
4905 }
4906 }
4907
4908 /*
4909 * Function: sd_get_physical_geometry
4910 *
4911 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4912 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4913 * target, and use this information to initialize the physical
4914 * geometry cache specified by pgeom_p.
4915 *
4916 * MODE SENSE is an optional command, so failure in this case
4917 * does not necessarily denote an error. We want to use the
4918 * MODE SENSE commands to derive the physical geometry of the
4919 * device, but if either command fails, the logical geometry is
4920 * used as the fallback for disk label geometry in cmlb.
4921 *
4922 * This requires that un->un_blockcount and un->un_tgt_blocksize
4923 * have already been initialized for the current target and
4924 * that the current values be passed as args so that we don't
4925 * end up ever trying to use -1 as a valid value. This could
4926 * happen if either value is reset while we're not holding
4927 * the mutex.
4928 *
4929 * Arguments: un - driver soft state (unit) structure
4930 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4931 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4932 * to use the USCSI "direct" chain and bypass the normal
4933 * command waitq.
4934 *
4935 * Context: Kernel thread only (can sleep).
4936 */
4937
4938 static int
4939 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4940 diskaddr_t capacity, int lbasize, int path_flag)
4941 {
4942 struct mode_format *page3p;
4943 struct mode_geometry *page4p;
4944 struct mode_header *headerp;
4945 int sector_size;
4946 int nsect;
4947 int nhead;
4948 int ncyl;
4949 int intrlv;
4950 int spc;
4951 diskaddr_t modesense_capacity;
4952 int rpm;
4953 int bd_len;
4954 int mode_header_length;
4955 uchar_t *p3bufp;
4956 uchar_t *p4bufp;
4957 int cdbsize;
4958 int ret = EIO;
4959 sd_ssc_t *ssc;
4960 int status;
4961
4962 ASSERT(un != NULL);
4963
4964 if (lbasize == 0) {
4965 if (ISCD(un)) {
4966 lbasize = 2048;
4967 } else {
4968 lbasize = un->un_sys_blocksize;
4969 }
4970 }
4971 pgeom_p->g_secsize = (unsigned short)lbasize;
4972
4973 /*
4974 * If the unit is a cd/dvd drive MODE SENSE page three
4975 * and MODE SENSE page four are reserved (see SBC spec
4976 * and MMC spec). To prevent soft errors just return
4977 * using the default LBA size.
4978 */
4979 if (ISCD(un))
4980 return (ret);
4981
4982 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4983
4984 /*
4985 * Retrieve MODE SENSE page 3 - Format Device Page
4986 */
4987 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4988 ssc = sd_ssc_init(un);
4989 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4990 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4991 if (status != 0) {
4992 SD_ERROR(SD_LOG_COMMON, un,
4993 "sd_get_physical_geometry: mode sense page 3 failed\n");
4994 goto page3_exit;
4995 }
4996
4997 /*
4998 * Determine size of Block Descriptors in order to locate the mode
4999 * page data. ATAPI devices return 0, SCSI devices should return
5000 * MODE_BLK_DESC_LENGTH.
5001 */
5002 headerp = (struct mode_header *)p3bufp;
5003 if (un->un_f_cfg_is_atapi == TRUE) {
5004 struct mode_header_grp2 *mhp =
5005 (struct mode_header_grp2 *)headerp;
5006 mode_header_length = MODE_HEADER_LENGTH_GRP2;
5007 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5008 } else {
5009 mode_header_length = MODE_HEADER_LENGTH;
5010 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5011 }
5012
5013 if (bd_len > MODE_BLK_DESC_LENGTH) {
5014 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5015 "sd_get_physical_geometry: received unexpected bd_len "
5016 "of %d, page3\n", bd_len);
5017 status = EIO;
5018 goto page3_exit;
5019 }
5020
5021 page3p = (struct mode_format *)
5022 ((caddr_t)headerp + mode_header_length + bd_len);
5023
5024 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5025 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5026 "sd_get_physical_geometry: mode sense pg3 code mismatch "
5027 "%d\n", page3p->mode_page.code);
5028 status = EIO;
5029 goto page3_exit;
5030 }
5031
5032 /*
5033 * Use this physical geometry data only if BOTH MODE SENSE commands
5034 * complete successfully; otherwise, revert to the logical geometry.
5035 * So, we need to save everything in temporary variables.
5036 */
5037 sector_size = BE_16(page3p->data_bytes_sect);
5038
5039 /*
5040 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5041 */
5042 if (sector_size == 0) {
5043 sector_size = un->un_sys_blocksize;
5044 } else {
5045 sector_size &= ~(un->un_sys_blocksize - 1);
5046 }
5047
5048 nsect = BE_16(page3p->sect_track);
5049 intrlv = BE_16(page3p->interleave);
5050
5051 SD_INFO(SD_LOG_COMMON, un,
5052 "sd_get_physical_geometry: Format Parameters (page 3)\n");
5053 SD_INFO(SD_LOG_COMMON, un,
5054 " mode page: %d; nsect: %d; sector size: %d;\n",
5055 page3p->mode_page.code, nsect, sector_size);
5056 SD_INFO(SD_LOG_COMMON, un,
5057 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5058 BE_16(page3p->track_skew),
5059 BE_16(page3p->cylinder_skew));
5060
5061 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5062
5063 /*
5064 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5065 */
5066 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5067 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5068 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5069 if (status != 0) {
5070 SD_ERROR(SD_LOG_COMMON, un,
5071 "sd_get_physical_geometry: mode sense page 4 failed\n");
5072 goto page4_exit;
5073 }
5074
5075 /*
5076 * Determine size of Block Descriptors in order to locate the mode
5077 * page data. ATAPI devices return 0, SCSI devices should return
5078 * MODE_BLK_DESC_LENGTH.
5079 */
5080 headerp = (struct mode_header *)p4bufp;
5081 if (un->un_f_cfg_is_atapi == TRUE) {
5082 struct mode_header_grp2 *mhp =
5083 (struct mode_header_grp2 *)headerp;
5084 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5085 } else {
5086 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5087 }
5088
5089 if (bd_len > MODE_BLK_DESC_LENGTH) {
5090 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5091 "sd_get_physical_geometry: received unexpected bd_len of "
5092 "%d, page4\n", bd_len);
5093 status = EIO;
5094 goto page4_exit;
5095 }
5096
5097 page4p = (struct mode_geometry *)
5098 ((caddr_t)headerp + mode_header_length + bd_len);
5099
5100 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5101 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5102 "sd_get_physical_geometry: mode sense pg4 code mismatch "
5103 "%d\n", page4p->mode_page.code);
5104 status = EIO;
5105 goto page4_exit;
5106 }
5107
5108 /*
5109 * Stash the data now, after we know that both commands completed.
5110 */
5111
5112
5113 nhead = (int)page4p->heads; /* uchar, so no conversion needed */
5114 spc = nhead * nsect;
5115 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5116 rpm = BE_16(page4p->rpm);
5117
5118 modesense_capacity = spc * ncyl;
5119
5120 SD_INFO(SD_LOG_COMMON, un,
5121 "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5122 SD_INFO(SD_LOG_COMMON, un,
5123 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5124 SD_INFO(SD_LOG_COMMON, un,
5125 " computed capacity(h*s*c): %d;\n", modesense_capacity);
5126 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n",
5127 (void *)pgeom_p, capacity);
5128
5129 /*
5130 * Compensate if the drive's geometry is not rectangular, i.e.,
5131 * the product of C * H * S returned by MODE SENSE >= that returned
5132 * by read capacity. This is an idiosyncrasy of the original x86
5133 * disk subsystem.
5134 */
5135 if (modesense_capacity >= capacity) {
5136 SD_INFO(SD_LOG_COMMON, un,
5137 "sd_get_physical_geometry: adjusting acyl; "
5138 "old: %d; new: %d\n", pgeom_p->g_acyl,
5139 (modesense_capacity - capacity + spc - 1) / spc);
5140 if (sector_size != 0) {
5141 /* 1243403: NEC D38x7 drives don't support sec size */
5142 pgeom_p->g_secsize = (unsigned short)sector_size;
5143 }
5144 pgeom_p->g_nsect = (unsigned short)nsect;
5145 pgeom_p->g_nhead = (unsigned short)nhead;
5146 pgeom_p->g_capacity = capacity;
5147 pgeom_p->g_acyl =
5148 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5149 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl;
5150 }
5151
5152 pgeom_p->g_rpm = (unsigned short)rpm;
5153 pgeom_p->g_intrlv = (unsigned short)intrlv;
5154 ret = 0;
5155
5156 SD_INFO(SD_LOG_COMMON, un,
5157 "sd_get_physical_geometry: mode sense geometry:\n");
5158 SD_INFO(SD_LOG_COMMON, un,
5159 " nsect: %d; sector size: %d; interlv: %d\n",
5160 nsect, sector_size, intrlv);
5161 SD_INFO(SD_LOG_COMMON, un,
5162 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5163 nhead, ncyl, rpm, modesense_capacity);
5164 SD_INFO(SD_LOG_COMMON, un,
5165 "sd_get_physical_geometry: (cached)\n");
5166 SD_INFO(SD_LOG_COMMON, un,
5167 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5168 pgeom_p->g_ncyl, pgeom_p->g_acyl,
5169 pgeom_p->g_nhead, pgeom_p->g_nsect);
5170 SD_INFO(SD_LOG_COMMON, un,
5171 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5172 pgeom_p->g_secsize, pgeom_p->g_capacity,
5173 pgeom_p->g_intrlv, pgeom_p->g_rpm);
5174 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5175
5176 page4_exit:
5177 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5178
5179 page3_exit:
5180 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5181
5182 if (status != 0) {
5183 if (status == EIO) {
5184 /*
5185 * Some disks do not support mode sense(6), we
5186 * should ignore this kind of error(sense key is
5187 * 0x5 - illegal request).
5188 */
5189 uint8_t *sensep;
5190 int senlen;
5191
5192 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5193 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5194 ssc->ssc_uscsi_cmd->uscsi_rqresid);
5195
5196 if (senlen > 0 &&
5197 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5198 sd_ssc_assessment(ssc,
5199 SD_FMT_IGNORE_COMPROMISE);
5200 } else {
5201 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5202 }
5203 } else {
5204 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5205 }
5206 }
5207 sd_ssc_fini(ssc);
5208 return (ret);
5209 }
5210
5211 /*
5212 * Function: sd_get_virtual_geometry
5213 *
5214 * Description: Ask the controller to tell us about the target device.
5215 *
5216 * Arguments: un - pointer to softstate
5217 * capacity - disk capacity in #blocks
5218 * lbasize - disk block size in bytes
5219 *
5220 * Context: Kernel thread only
5221 */
5222
5223 static int
5224 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5225 diskaddr_t capacity, int lbasize)
5226 {
5227 uint_t geombuf;
5228 int spc;
5229
5230 ASSERT(un != NULL);
5231
5232 /* Set sector size, and total number of sectors */
5233 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1);
5234 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5235
5236 /* Let the HBA tell us its geometry */
5237 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5238
5239 /* A value of -1 indicates an undefined "geometry" property */
5240 if (geombuf == (-1)) {
5241 return (EINVAL);
5242 }
5243
5244 /* Initialize the logical geometry cache. */
5245 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff;
5246 lgeom_p->g_nsect = geombuf & 0xffff;
5247 lgeom_p->g_secsize = un->un_sys_blocksize;
5248
5249 spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5250
5251 /*
5252 * Note: The driver originally converted the capacity value from
5253 * target blocks to system blocks. However, the capacity value passed
5254 * to this routine is already in terms of system blocks (this scaling
5255 * is done when the READ CAPACITY command is issued and processed).
5256 * This 'error' may have gone undetected because the usage of g_ncyl
5257 * (which is based upon g_capacity) is very limited within the driver
5258 */
5259 lgeom_p->g_capacity = capacity;
5260
5261 /*
5262 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5263 * hba may return zero values if the device has been removed.
5264 */
5265 if (spc == 0) {
5266 lgeom_p->g_ncyl = 0;
5267 } else {
5268 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5269 }
5270 lgeom_p->g_acyl = 0;
5271
5272 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5273 return (0);
5274
5275 }
5276 /*
5277 * Function: sd_update_block_info
5278 *
5279 * Description: Calculate a byte count to sector count bitshift value
5280 * from sector size.
5281 *
5282 * Arguments: un: unit struct.
5283 * lbasize: new target sector size
5284 * capacity: new target capacity, ie. block count
5285 *
5286 * Context: Kernel thread context
5287 */
5288
5289 static void
5290 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5291 {
5292 if (lbasize != 0) {
5293 un->un_tgt_blocksize = lbasize;
5294 un->un_f_tgt_blocksize_is_valid = TRUE;
5295 if (!un->un_f_has_removable_media) {
5296 un->un_sys_blocksize = lbasize;
5297 }
5298 }
5299
5300 if (capacity != 0) {
5301 un->un_blockcount = capacity;
5302 un->un_f_blockcount_is_valid = TRUE;
5303
5304 /*
5305 * The capacity has changed so update the errstats.
5306 */
5307 if (un->un_errstats != NULL) {
5308 struct sd_errstats *stp;
5309
5310 capacity *= un->un_sys_blocksize;
5311 stp = (struct sd_errstats *)un->un_errstats->ks_data;
5312 if (stp->sd_capacity.value.ui64 < capacity)
5313 stp->sd_capacity.value.ui64 = capacity;
5314 }
5315 }
5316 }
5317
5318
5319 /*
5320 * Function: sd_register_devid
5321 *
5322 * Description: This routine will obtain the device id information from the
5323 * target, obtain the serial number, and register the device
5324 * id with the ddi framework.
5325 *
5326 * Arguments: devi - the system's dev_info_t for the device.
5327 * un - driver soft state (unit) structure
5328 * reservation_flag - indicates if a reservation conflict
5329 * occurred during attach
5330 *
5331 * Context: Kernel Thread
5332 */
5333 static void
5334 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5335 {
5336 int rval = 0;
5337 uchar_t *inq80 = NULL;
5338 size_t inq80_len = MAX_INQUIRY_SIZE;
5339 size_t inq80_resid = 0;
5340 uchar_t *inq83 = NULL;
5341 size_t inq83_len = MAX_INQUIRY_SIZE;
5342 size_t inq83_resid = 0;
5343 int dlen, len;
5344 char *sn;
5345 struct sd_lun *un;
5346
5347 ASSERT(ssc != NULL);
5348 un = ssc->ssc_un;
5349 ASSERT(un != NULL);
5350 ASSERT(mutex_owned(SD_MUTEX(un)));
5351 ASSERT((SD_DEVINFO(un)) == devi);
5352
5353
5354 /*
5355 * We check the availability of the World Wide Name (0x83) and Unit
5356 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5357 * un_vpd_page_mask from them, we decide which way to get the WWN. If
5358 * 0x83 is available, that is the best choice. Our next choice is
5359 * 0x80. If neither are available, we munge the devid from the device
5360 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5361 * to fabricate a devid for non-Sun qualified disks.
5362 */
5363 if (sd_check_vpd_page_support(ssc) == 0) {
5364 /* collect page 80 data if available */
5365 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5366
5367 mutex_exit(SD_MUTEX(un));
5368 inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5369
5370 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5371 0x01, 0x80, &inq80_resid);
5372
5373 if (rval != 0) {
5374 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5375 kmem_free(inq80, inq80_len);
5376 inq80 = NULL;
5377 inq80_len = 0;
5378 } else if (ddi_prop_exists(
5379 DDI_DEV_T_NONE, SD_DEVINFO(un),
5380 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5381 INQUIRY_SERIAL_NO) == 0) {
5382 /*
5383 * If we don't already have a serial number
5384 * property, do quick verify of data returned
5385 * and define property.
5386 */
5387 dlen = inq80_len - inq80_resid;
5388 len = (size_t)inq80[3];
5389 if ((dlen >= 4) && ((len + 4) <= dlen)) {
5390 /*
5391 * Ensure sn termination, skip leading
5392 * blanks, and create property
5393 * 'inquiry-serial-no'.
5394 */
5395 sn = (char *)&inq80[4];
5396 sn[len] = 0;
5397 while (*sn && (*sn == ' '))
5398 sn++;
5399 if (*sn) {
5400 (void) ddi_prop_update_string(
5401 DDI_DEV_T_NONE,
5402 SD_DEVINFO(un),
5403 INQUIRY_SERIAL_NO, sn);
5404 }
5405 }
5406 }
5407 mutex_enter(SD_MUTEX(un));
5408 }
5409
5410 /* collect page 83 data if available */
5411 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5412 mutex_exit(SD_MUTEX(un));
5413 inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5414
5415 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5416 0x01, 0x83, &inq83_resid);
5417
5418 if (rval != 0) {
5419 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5420 kmem_free(inq83, inq83_len);
5421 inq83 = NULL;
5422 inq83_len = 0;
5423 }
5424 mutex_enter(SD_MUTEX(un));
5425 }
5426 }
5427
5428 /*
5429 * If transport has already registered a devid for this target
5430 * then that takes precedence over the driver's determination
5431 * of the devid.
5432 *
5433 * NOTE: The reason this check is done here instead of at the beginning
5434 * of the function is to allow the code above to create the
5435 * 'inquiry-serial-no' property.
5436 */
5437 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5438 ASSERT(un->un_devid);
5439 un->un_f_devid_transport_defined = TRUE;
5440 goto cleanup; /* use devid registered by the transport */
5441 }
5442
5443 /*
5444 * This is the case of antiquated Sun disk drives that have the
5445 * FAB_DEVID property set in the disk_table. These drives
5446 * manage the devid's by storing them in last 2 available sectors
5447 * on the drive and have them fabricated by the ddi layer by calling
5448 * ddi_devid_init and passing the DEVID_FAB flag.
5449 */
5450 if (un->un_f_opt_fab_devid == TRUE) {
5451 /*
5452 * Depending on EINVAL isn't reliable, since a reserved disk
5453 * may result in invalid geometry, so check to make sure a
5454 * reservation conflict did not occur during attach.
5455 */
5456 if ((sd_get_devid(ssc) == EINVAL) &&
5457 (reservation_flag != SD_TARGET_IS_RESERVED)) {
5458 /*
5459 * The devid is invalid AND there is no reservation
5460 * conflict. Fabricate a new devid.
5461 */
5462 (void) sd_create_devid(ssc);
5463 }
5464
5465 /* Register the devid if it exists */
5466 if (un->un_devid != NULL) {
5467 (void) ddi_devid_register(SD_DEVINFO(un),
5468 un->un_devid);
5469 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5470 "sd_register_devid: Devid Fabricated\n");
5471 }
5472 goto cleanup;
5473 }
5474
5475 /* encode best devid possible based on data available */
5476 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5477 (char *)ddi_driver_name(SD_DEVINFO(un)),
5478 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5479 inq80, inq80_len - inq80_resid, inq83, inq83_len -
5480 inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5481
5482 /* devid successfully encoded, register devid */
5483 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5484
5485 } else {
5486 /*
5487 * Unable to encode a devid based on data available.
5488 * This is not a Sun qualified disk. Older Sun disk
5489 * drives that have the SD_FAB_DEVID property
5490 * set in the disk_table and non Sun qualified
5491 * disks are treated in the same manner. These
5492 * drives manage the devid's by storing them in
5493 * last 2 available sectors on the drive and
5494 * have them fabricated by the ddi layer by
5495 * calling ddi_devid_init and passing the
5496 * DEVID_FAB flag.
5497 * Create a fabricate devid only if there's no
5498 * fabricate devid existed.
5499 */
5500 if (sd_get_devid(ssc) == EINVAL) {
5501 (void) sd_create_devid(ssc);
5502 }
5503 un->un_f_opt_fab_devid = TRUE;
5504
5505 /* Register the devid if it exists */
5506 if (un->un_devid != NULL) {
5507 (void) ddi_devid_register(SD_DEVINFO(un),
5508 un->un_devid);
5509 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5510 "sd_register_devid: devid fabricated using "
5511 "ddi framework\n");
5512 }
5513 }
5514
5515 cleanup:
5516 /* clean up resources */
5517 if (inq80 != NULL) {
5518 kmem_free(inq80, inq80_len);
5519 }
5520 if (inq83 != NULL) {
5521 kmem_free(inq83, inq83_len);
5522 }
5523 }
5524
5525
5526
5527 /*
5528 * Function: sd_get_devid
5529 *
5530 * Description: This routine will return 0 if a valid device id has been
5531 * obtained from the target and stored in the soft state. If a
5532 * valid device id has not been previously read and stored, a
5533 * read attempt will be made.
5534 *
5535 * Arguments: un - driver soft state (unit) structure
5536 *
5537 * Return Code: 0 if we successfully get the device id
5538 *
5539 * Context: Kernel Thread
5540 */
5541
5542 static int
5543 sd_get_devid(sd_ssc_t *ssc)
5544 {
5545 struct dk_devid *dkdevid;
5546 ddi_devid_t tmpid;
5547 uint_t *ip;
5548 size_t sz;
5549 diskaddr_t blk;
5550 int status;
5551 int chksum;
5552 int i;
5553 size_t buffer_size;
5554 struct sd_lun *un;
5555
5556 ASSERT(ssc != NULL);
5557 un = ssc->ssc_un;
5558 ASSERT(un != NULL);
5559 ASSERT(mutex_owned(SD_MUTEX(un)));
5560
5561 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5562 un);
5563
5564 if (un->un_devid != NULL) {
5565 return (0);
5566 }
5567
5568 mutex_exit(SD_MUTEX(un));
5569 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5570 (void *)SD_PATH_DIRECT) != 0) {
5571 mutex_enter(SD_MUTEX(un));
5572 return (EINVAL);
5573 }
5574
5575 /*
5576 * Read and verify device id, stored in the reserved cylinders at the
5577 * end of the disk. Backup label is on the odd sectors of the last
5578 * track of the last cylinder. Device id will be on track of the next
5579 * to last cylinder.
5580 */
5581 mutex_enter(SD_MUTEX(un));
5582 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5583 mutex_exit(SD_MUTEX(un));
5584 dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5585 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5586 SD_PATH_DIRECT);
5587
5588 if (status != 0) {
5589 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5590 goto error;
5591 }
5592
5593 /* Validate the revision */
5594 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5595 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5596 status = EINVAL;
5597 goto error;
5598 }
5599
5600 /* Calculate the checksum */
5601 chksum = 0;
5602 ip = (uint_t *)dkdevid;
5603 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5604 i++) {
5605 chksum ^= ip[i];
5606 }
5607
5608 /* Compare the checksums */
5609 if (DKD_GETCHKSUM(dkdevid) != chksum) {
5610 status = EINVAL;
5611 goto error;
5612 }
5613
5614 /* Validate the device id */
5615 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5616 status = EINVAL;
5617 goto error;
5618 }
5619
5620 /*
5621 * Store the device id in the driver soft state
5622 */
5623 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5624 tmpid = kmem_alloc(sz, KM_SLEEP);
5625
5626 mutex_enter(SD_MUTEX(un));
5627
5628 un->un_devid = tmpid;
5629 bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5630
5631 kmem_free(dkdevid, buffer_size);
5632
5633 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5634
5635 return (status);
5636 error:
5637 mutex_enter(SD_MUTEX(un));
5638 kmem_free(dkdevid, buffer_size);
5639 return (status);
5640 }
5641
5642
5643 /*
5644 * Function: sd_create_devid
5645 *
5646 * Description: This routine will fabricate the device id and write it
5647 * to the disk.
5648 *
5649 * Arguments: un - driver soft state (unit) structure
5650 *
5651 * Return Code: value of the fabricated device id
5652 *
5653 * Context: Kernel Thread
5654 */
5655
5656 static ddi_devid_t
5657 sd_create_devid(sd_ssc_t *ssc)
5658 {
5659 struct sd_lun *un;
5660
5661 ASSERT(ssc != NULL);
5662 un = ssc->ssc_un;
5663 ASSERT(un != NULL);
5664
5665 /* Fabricate the devid */
5666 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5667 == DDI_FAILURE) {
5668 return (NULL);
5669 }
5670
5671 /* Write the devid to disk */
5672 if (sd_write_deviceid(ssc) != 0) {
5673 ddi_devid_free(un->un_devid);
5674 un->un_devid = NULL;
5675 }
5676
5677 return (un->un_devid);
5678 }
5679
5680
5681 /*
5682 * Function: sd_write_deviceid
5683 *
5684 * Description: This routine will write the device id to the disk
5685 * reserved sector.
5686 *
5687 * Arguments: un - driver soft state (unit) structure
5688 *
5689 * Return Code: EINVAL
5690 * value returned by sd_send_scsi_cmd
5691 *
5692 * Context: Kernel Thread
5693 */
5694
5695 static int
5696 sd_write_deviceid(sd_ssc_t *ssc)
5697 {
5698 struct dk_devid *dkdevid;
5699 uchar_t *buf;
5700 diskaddr_t blk;
5701 uint_t *ip, chksum;
5702 int status;
5703 int i;
5704 struct sd_lun *un;
5705
5706 ASSERT(ssc != NULL);
5707 un = ssc->ssc_un;
5708 ASSERT(un != NULL);
5709 ASSERT(mutex_owned(SD_MUTEX(un)));
5710
5711 mutex_exit(SD_MUTEX(un));
5712 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5713 (void *)SD_PATH_DIRECT) != 0) {
5714 mutex_enter(SD_MUTEX(un));
5715 return (-1);
5716 }
5717
5718
5719 /* Allocate the buffer */
5720 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5721 dkdevid = (struct dk_devid *)buf;
5722
5723 /* Fill in the revision */
5724 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5725 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5726
5727 /* Copy in the device id */
5728 mutex_enter(SD_MUTEX(un));
5729 bcopy(un->un_devid, &dkdevid->dkd_devid,
5730 ddi_devid_sizeof(un->un_devid));
5731 mutex_exit(SD_MUTEX(un));
5732
5733 /* Calculate the checksum */
5734 chksum = 0;
5735 ip = (uint_t *)dkdevid;
5736 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5737 i++) {
5738 chksum ^= ip[i];
5739 }
5740
5741 /* Fill-in checksum */
5742 DKD_FORMCHKSUM(chksum, dkdevid);
5743
5744 /* Write the reserved sector */
5745 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5746 SD_PATH_DIRECT);
5747 if (status != 0)
5748 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5749
5750 kmem_free(buf, un->un_sys_blocksize);
5751
5752 mutex_enter(SD_MUTEX(un));
5753 return (status);
5754 }
5755
5756
5757 /*
5758 * Function: sd_check_vpd_page_support
5759 *
5760 * Description: This routine sends an inquiry command with the EVPD bit set and
5761 * a page code of 0x00 to the device. It is used to determine which
5762 * vital product pages are available to find the devid. We are
5763 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1,
5764 * the device does not support that command.
5765 *
5766 * Arguments: un - driver soft state (unit) structure
5767 *
5768 * Return Code: 0 - success
5769 * 1 - check condition
5770 *
5771 * Context: This routine can sleep.
5772 */
5773
5774 static int
5775 sd_check_vpd_page_support(sd_ssc_t *ssc)
5776 {
5777 uchar_t *page_list = NULL;
5778 uchar_t page_length = 0xff; /* Use max possible length */
5779 uchar_t evpd = 0x01; /* Set the EVPD bit */
5780 uchar_t page_code = 0x00; /* Supported VPD Pages */
5781 int rval = 0;
5782 int counter;
5783 struct sd_lun *un;
5784
5785 ASSERT(ssc != NULL);
5786 un = ssc->ssc_un;
5787 ASSERT(un != NULL);
5788 ASSERT(mutex_owned(SD_MUTEX(un)));
5789
5790 mutex_exit(SD_MUTEX(un));
5791
5792 /*
5793 * We'll set the page length to the maximum to save figuring it out
5794 * with an additional call.
5795 */
5796 page_list = kmem_zalloc(page_length, KM_SLEEP);
5797
5798 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5799 page_code, NULL);
5800
5801 if (rval != 0)
5802 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5803
5804 mutex_enter(SD_MUTEX(un));
5805
5806 /*
5807 * Now we must validate that the device accepted the command, as some
5808 * drives do not support it. If the drive does support it, we will
5809 * return 0, and the supported pages will be in un_vpd_page_mask. If
5810 * not, we return -1.
5811 */
5812 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5813 /* Loop to find one of the 2 pages we need */
5814 counter = 4; /* Supported pages start at byte 4, with 0x00 */
5815
5816 /*
5817 * Pages are returned in ascending order, and 0x83 is what we
5818 * are hoping for.
5819 */
5820 while ((page_list[counter] <= 0xB1) &&
5821 (counter <= (page_list[VPD_PAGE_LENGTH] +
5822 VPD_HEAD_OFFSET))) {
5823 /*
5824 * Add 3 because page_list[3] is the number of
5825 * pages minus 3
5826 */
5827
5828 switch (page_list[counter]) {
5829 case 0x00:
5830 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5831 break;
5832 case 0x80:
5833 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5834 break;
5835 case 0x81:
5836 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5837 break;
5838 case 0x82:
5839 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5840 break;
5841 case 0x83:
5842 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5843 break;
5844 case 0x86:
5845 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5846 break;
5847 case 0xB1:
5848 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5849 break;
5850 }
5851 counter++;
5852 }
5853
5854 } else {
5855 rval = -1;
5856
5857 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5858 "sd_check_vpd_page_support: This drive does not implement "
5859 "VPD pages.\n");
5860 }
5861
5862 kmem_free(page_list, page_length);
5863
5864 return (rval);
5865 }
5866
5867
5868 /*
5869 * Function: sd_setup_pm
5870 *
5871 * Description: Initialize Power Management on the device
5872 *
5873 * Context: Kernel Thread
5874 */
5875
5876 static void
5877 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5878 {
5879 uint_t log_page_size;
5880 uchar_t *log_page_data;
5881 int rval = 0;
5882 struct sd_lun *un;
5883
5884 ASSERT(ssc != NULL);
5885 un = ssc->ssc_un;
5886 ASSERT(un != NULL);
5887
5888 /*
5889 * Since we are called from attach, holding a mutex for
5890 * un is unnecessary. Because some of the routines called
5891 * from here require SD_MUTEX to not be held, assert this
5892 * right up front.
5893 */
5894 ASSERT(!mutex_owned(SD_MUTEX(un)));
5895 /*
5896 * Since the sd device does not have the 'reg' property,
5897 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5898 * The following code is to tell cpr that this device
5899 * DOES need to be suspended and resumed.
5900 */
5901 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5902 "pm-hardware-state", "needs-suspend-resume");
5903
5904 /*
5905 * This complies with the new power management framework
5906 * for certain desktop machines. Create the pm_components
5907 * property as a string array property.
5908 * If un_f_pm_supported is TRUE, that means the disk
5909 * attached HBA has set the "pm-capable" property and
5910 * the value of this property is bigger than 0.
5911 */
5912 if (un->un_f_pm_supported) {
5913 /*
5914 * not all devices have a motor, try it first.
5915 * some devices may return ILLEGAL REQUEST, some
5916 * will hang
5917 * The following START_STOP_UNIT is used to check if target
5918 * device has a motor.
5919 */
5920 un->un_f_start_stop_supported = TRUE;
5921
5922 if (un->un_f_power_condition_supported) {
5923 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5924 SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5925 SD_PATH_DIRECT);
5926 if (rval != 0) {
5927 un->un_f_power_condition_supported = FALSE;
5928 }
5929 }
5930 if (!un->un_f_power_condition_supported) {
5931 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5932 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5933 }
5934 if (rval != 0) {
5935 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5936 un->un_f_start_stop_supported = FALSE;
5937 }
5938
5939 /*
5940 * create pm properties anyways otherwise the parent can't
5941 * go to sleep
5942 */
5943 un->un_f_pm_is_enabled = TRUE;
5944 (void) sd_create_pm_components(devi, un);
5945
5946 /*
5947 * If it claims that log sense is supported, check it out.
5948 */
5949 if (un->un_f_log_sense_supported) {
5950 rval = sd_log_page_supported(ssc,
5951 START_STOP_CYCLE_PAGE);
5952 if (rval == 1) {
5953 /* Page found, use it. */
5954 un->un_start_stop_cycle_page =
5955 START_STOP_CYCLE_PAGE;
5956 } else {
5957 /*
5958 * Page not found or log sense is not
5959 * supported.
5960 * Notice we do not check the old style
5961 * START_STOP_CYCLE_VU_PAGE because this
5962 * code path does not apply to old disks.
5963 */
5964 un->un_f_log_sense_supported = FALSE;
5965 un->un_f_pm_log_sense_smart = FALSE;
5966 }
5967 }
5968
5969 return;
5970 }
5971
5972 /*
5973 * For the disk whose attached HBA has not set the "pm-capable"
5974 * property, check if it supports the power management.
5975 */
5976 if (!un->un_f_log_sense_supported) {
5977 un->un_power_level = SD_SPINDLE_ON;
5978 un->un_f_pm_is_enabled = FALSE;
5979 return;
5980 }
5981
5982 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5983
5984 #ifdef SDDEBUG
5985 if (sd_force_pm_supported) {
5986 /* Force a successful result */
5987 rval = 1;
5988 }
5989 #endif
5990
5991 /*
5992 * If the start-stop cycle counter log page is not supported
5993 * or if the pm-capable property is set to be false (0),
5994 * then we should not create the pm_components property.
5995 */
5996 if (rval == -1) {
5997 /*
5998 * Error.
5999 * Reading log sense failed, most likely this is
6000 * an older drive that does not support log sense.
6001 * If this fails auto-pm is not supported.
6002 */
6003 un->un_power_level = SD_SPINDLE_ON;
6004 un->un_f_pm_is_enabled = FALSE;
6005
6006 } else if (rval == 0) {
6007 /*
6008 * Page not found.
6009 * The start stop cycle counter is implemented as page
6010 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6011 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6012 */
6013 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6014 /*
6015 * Page found, use this one.
6016 */
6017 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6018 un->un_f_pm_is_enabled = TRUE;
6019 } else {
6020 /*
6021 * Error or page not found.
6022 * auto-pm is not supported for this device.
6023 */
6024 un->un_power_level = SD_SPINDLE_ON;
6025 un->un_f_pm_is_enabled = FALSE;
6026 }
6027 } else {
6028 /*
6029 * Page found, use it.
6030 */
6031 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6032 un->un_f_pm_is_enabled = TRUE;
6033 }
6034
6035
6036 if (un->un_f_pm_is_enabled == TRUE) {
6037 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6038 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6039
6040 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6041 log_page_size, un->un_start_stop_cycle_page,
6042 0x01, 0, SD_PATH_DIRECT);
6043
6044 if (rval != 0) {
6045 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6046 }
6047
6048 #ifdef SDDEBUG
6049 if (sd_force_pm_supported) {
6050 /* Force a successful result */
6051 rval = 0;
6052 }
6053 #endif
6054
6055 /*
6056 * If the Log sense for Page( Start/stop cycle counter page)
6057 * succeeds, then power management is supported and we can
6058 * enable auto-pm.
6059 */
6060 if (rval == 0) {
6061 (void) sd_create_pm_components(devi, un);
6062 } else {
6063 un->un_power_level = SD_SPINDLE_ON;
6064 un->un_f_pm_is_enabled = FALSE;
6065 }
6066
6067 kmem_free(log_page_data, log_page_size);
6068 }
6069 }
6070
6071
6072 /*
6073 * Function: sd_create_pm_components
6074 *
6075 * Description: Initialize PM property.
6076 *
6077 * Context: Kernel thread context
6078 */
6079
6080 static void
6081 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6082 {
6083 ASSERT(!mutex_owned(SD_MUTEX(un)));
6084
6085 if (un->un_f_power_condition_supported) {
6086 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6087 "pm-components", sd_pwr_pc.pm_comp, 5)
6088 != DDI_PROP_SUCCESS) {
6089 un->un_power_level = SD_SPINDLE_ACTIVE;
6090 un->un_f_pm_is_enabled = FALSE;
6091 return;
6092 }
6093 } else {
6094 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6095 "pm-components", sd_pwr_ss.pm_comp, 3)
6096 != DDI_PROP_SUCCESS) {
6097 un->un_power_level = SD_SPINDLE_ON;
6098 un->un_f_pm_is_enabled = FALSE;
6099 return;
6100 }
6101 }
6102 /*
6103 * When components are initially created they are idle,
6104 * power up any non-removables.
6105 * Note: the return value of pm_raise_power can't be used
6106 * for determining if PM should be enabled for this device.
6107 * Even if you check the return values and remove this
6108 * property created above, the PM framework will not honor the
6109 * change after the first call to pm_raise_power. Hence,
6110 * removal of that property does not help if pm_raise_power
6111 * fails. In the case of removable media, the start/stop
6112 * will fail if the media is not present.
6113 */
6114 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6115 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6116 mutex_enter(SD_MUTEX(un));
6117 un->un_power_level = SD_PM_STATE_ACTIVE(un);
6118 mutex_enter(&un->un_pm_mutex);
6119 /* Set to on and not busy. */
6120 un->un_pm_count = 0;
6121 } else {
6122 mutex_enter(SD_MUTEX(un));
6123 un->un_power_level = SD_PM_STATE_STOPPED(un);
6124 mutex_enter(&un->un_pm_mutex);
6125 /* Set to off. */
6126 un->un_pm_count = -1;
6127 }
6128 mutex_exit(&un->un_pm_mutex);
6129 mutex_exit(SD_MUTEX(un));
6130 }
6131
6132
6133 /*
6134 * Function: sd_ddi_suspend
6135 *
6136 * Description: Performs system power-down operations. This includes
6137 * setting the drive state to indicate its suspended so
6138 * that no new commands will be accepted. Also, wait for
6139 * all commands that are in transport or queued to a timer
6140 * for retry to complete. All timeout threads are cancelled.
6141 *
6142 * Return Code: DDI_FAILURE or DDI_SUCCESS
6143 *
6144 * Context: Kernel thread context
6145 */
6146
6147 static int
6148 sd_ddi_suspend(dev_info_t *devi)
6149 {
6150 struct sd_lun *un;
6151 clock_t wait_cmds_complete;
6152
6153 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6154 if (un == NULL) {
6155 return (DDI_FAILURE);
6156 }
6157
6158 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6159
6160 mutex_enter(SD_MUTEX(un));
6161
6162 /* Return success if the device is already suspended. */
6163 if (un->un_state == SD_STATE_SUSPENDED) {
6164 mutex_exit(SD_MUTEX(un));
6165 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6166 "device already suspended, exiting\n");
6167 return (DDI_SUCCESS);
6168 }
6169
6170 /* Return failure if the device is being used by HA */
6171 if (un->un_resvd_status &
6172 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6173 mutex_exit(SD_MUTEX(un));
6174 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6175 "device in use by HA, exiting\n");
6176 return (DDI_FAILURE);
6177 }
6178
6179 /*
6180 * Return failure if the device is in a resource wait
6181 * or power changing state.
6182 */
6183 if ((un->un_state == SD_STATE_RWAIT) ||
6184 (un->un_state == SD_STATE_PM_CHANGING)) {
6185 mutex_exit(SD_MUTEX(un));
6186 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6187 "device in resource wait state, exiting\n");
6188 return (DDI_FAILURE);
6189 }
6190
6191
6192 un->un_save_state = un->un_last_state;
6193 New_state(un, SD_STATE_SUSPENDED);
6194
6195 /*
6196 * Wait for all commands that are in transport or queued to a timer
6197 * for retry to complete.
6198 *
6199 * While waiting, no new commands will be accepted or sent because of
6200 * the new state we set above.
6201 *
6202 * Wait till current operation has completed. If we are in the resource
6203 * wait state (with an intr outstanding) then we need to wait till the
6204 * intr completes and starts the next cmd. We want to wait for
6205 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6206 */
6207 wait_cmds_complete = ddi_get_lbolt() +
6208 (sd_wait_cmds_complete * drv_usectohz(1000000));
6209
6210 while (un->un_ncmds_in_transport != 0) {
6211 /*
6212 * Fail if commands do not finish in the specified time.
6213 */
6214 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6215 wait_cmds_complete) == -1) {
6216 /*
6217 * Undo the state changes made above. Everything
6218 * must go back to it's original value.
6219 */
6220 Restore_state(un);
6221 un->un_last_state = un->un_save_state;
6222 /* Wake up any threads that might be waiting. */
6223 cv_broadcast(&un->un_suspend_cv);
6224 mutex_exit(SD_MUTEX(un));
6225 SD_ERROR(SD_LOG_IO_PM, un,
6226 "sd_ddi_suspend: failed due to outstanding cmds\n");
6227 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6228 return (DDI_FAILURE);
6229 }
6230 }
6231
6232 /*
6233 * Cancel SCSI watch thread and timeouts, if any are active
6234 */
6235
6236 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6237 opaque_t temp_token = un->un_swr_token;
6238 mutex_exit(SD_MUTEX(un));
6239 scsi_watch_suspend(temp_token);
6240 mutex_enter(SD_MUTEX(un));
6241 }
6242
6243 if (un->un_reset_throttle_timeid != NULL) {
6244 timeout_id_t temp_id = un->un_reset_throttle_timeid;
6245 un->un_reset_throttle_timeid = NULL;
6246 mutex_exit(SD_MUTEX(un));
6247 (void) untimeout(temp_id);
6248 mutex_enter(SD_MUTEX(un));
6249 }
6250
6251 if (un->un_dcvb_timeid != NULL) {
6252 timeout_id_t temp_id = un->un_dcvb_timeid;
6253 un->un_dcvb_timeid = NULL;
6254 mutex_exit(SD_MUTEX(un));
6255 (void) untimeout(temp_id);
6256 mutex_enter(SD_MUTEX(un));
6257 }
6258
6259 mutex_enter(&un->un_pm_mutex);
6260 if (un->un_pm_timeid != NULL) {
6261 timeout_id_t temp_id = un->un_pm_timeid;
6262 un->un_pm_timeid = NULL;
6263 mutex_exit(&un->un_pm_mutex);
6264 mutex_exit(SD_MUTEX(un));
6265 (void) untimeout(temp_id);
6266 mutex_enter(SD_MUTEX(un));
6267 } else {
6268 mutex_exit(&un->un_pm_mutex);
6269 }
6270
6271 if (un->un_rmw_msg_timeid != NULL) {
6272 timeout_id_t temp_id = un->un_rmw_msg_timeid;
6273 un->un_rmw_msg_timeid = NULL;
6274 mutex_exit(SD_MUTEX(un));
6275 (void) untimeout(temp_id);
6276 mutex_enter(SD_MUTEX(un));
6277 }
6278
6279 if (un->un_retry_timeid != NULL) {
6280 timeout_id_t temp_id = un->un_retry_timeid;
6281 un->un_retry_timeid = NULL;
6282 mutex_exit(SD_MUTEX(un));
6283 (void) untimeout(temp_id);
6284 mutex_enter(SD_MUTEX(un));
6285
6286 if (un->un_retry_bp != NULL) {
6287 un->un_retry_bp->av_forw = un->un_waitq_headp;
6288 un->un_waitq_headp = un->un_retry_bp;
6289 if (un->un_waitq_tailp == NULL) {
6290 un->un_waitq_tailp = un->un_retry_bp;
6291 }
6292 un->un_retry_bp = NULL;
6293 un->un_retry_statp = NULL;
6294 }
6295 }
6296
6297 if (un->un_direct_priority_timeid != NULL) {
6298 timeout_id_t temp_id = un->un_direct_priority_timeid;
6299 un->un_direct_priority_timeid = NULL;
6300 mutex_exit(SD_MUTEX(un));
6301 (void) untimeout(temp_id);
6302 mutex_enter(SD_MUTEX(un));
6303 }
6304
6305 if (un->un_f_is_fibre == TRUE) {
6306 /*
6307 * Remove callbacks for insert and remove events
6308 */
6309 if (un->un_insert_event != NULL) {
6310 mutex_exit(SD_MUTEX(un));
6311 (void) ddi_remove_event_handler(un->un_insert_cb_id);
6312 mutex_enter(SD_MUTEX(un));
6313 un->un_insert_event = NULL;
6314 }
6315
6316 if (un->un_remove_event != NULL) {
6317 mutex_exit(SD_MUTEX(un));
6318 (void) ddi_remove_event_handler(un->un_remove_cb_id);
6319 mutex_enter(SD_MUTEX(un));
6320 un->un_remove_event = NULL;
6321 }
6322 }
6323
6324 mutex_exit(SD_MUTEX(un));
6325
6326 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6327
6328 return (DDI_SUCCESS);
6329 }
6330
6331
6332 /*
6333 * Function: sd_ddi_resume
6334 *
6335 * Description: Performs system power-up operations..
6336 *
6337 * Return Code: DDI_SUCCESS
6338 * DDI_FAILURE
6339 *
6340 * Context: Kernel thread context
6341 */
6342
6343 static int
6344 sd_ddi_resume(dev_info_t *devi)
6345 {
6346 struct sd_lun *un;
6347
6348 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6349 if (un == NULL) {
6350 return (DDI_FAILURE);
6351 }
6352
6353 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6354
6355 mutex_enter(SD_MUTEX(un));
6356 Restore_state(un);
6357
6358 /*
6359 * Restore the state which was saved to give the
6360 * the right state in un_last_state
6361 */
6362 un->un_last_state = un->un_save_state;
6363 /*
6364 * Note: throttle comes back at full.
6365 * Also note: this MUST be done before calling pm_raise_power
6366 * otherwise the system can get hung in biowait. The scenario where
6367 * this'll happen is under cpr suspend. Writing of the system
6368 * state goes through sddump, which writes 0 to un_throttle. If
6369 * writing the system state then fails, example if the partition is
6370 * too small, then cpr attempts a resume. If throttle isn't restored
6371 * from the saved value until after calling pm_raise_power then
6372 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6373 * in biowait.
6374 */
6375 un->un_throttle = un->un_saved_throttle;
6376
6377 /*
6378 * The chance of failure is very rare as the only command done in power
6379 * entry point is START command when you transition from 0->1 or
6380 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6381 * which suspend was done. Ignore the return value as the resume should
6382 * not be failed. In the case of removable media the media need not be
6383 * inserted and hence there is a chance that raise power will fail with
6384 * media not present.
6385 */
6386 if (un->un_f_attach_spinup) {
6387 mutex_exit(SD_MUTEX(un));
6388 (void) pm_raise_power(SD_DEVINFO(un), 0,
6389 SD_PM_STATE_ACTIVE(un));
6390 mutex_enter(SD_MUTEX(un));
6391 }
6392
6393 /*
6394 * Don't broadcast to the suspend cv and therefore possibly
6395 * start I/O until after power has been restored.
6396 */
6397 cv_broadcast(&un->un_suspend_cv);
6398 cv_broadcast(&un->un_state_cv);
6399
6400 /* restart thread */
6401 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6402 scsi_watch_resume(un->un_swr_token);
6403 }
6404
6405 #if (defined(__fibre))
6406 if (un->un_f_is_fibre == TRUE) {
6407 /*
6408 * Add callbacks for insert and remove events
6409 */
6410 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6411 sd_init_event_callbacks(un);
6412 }
6413 }
6414 #endif
6415
6416 /*
6417 * Transport any pending commands to the target.
6418 *
6419 * If this is a low-activity device commands in queue will have to wait
6420 * until new commands come in, which may take awhile. Also, we
6421 * specifically don't check un_ncmds_in_transport because we know that
6422 * there really are no commands in progress after the unit was
6423 * suspended and we could have reached the throttle level, been
6424 * suspended, and have no new commands coming in for awhile. Highly
6425 * unlikely, but so is the low-activity disk scenario.
6426 */
6427 ddi_xbuf_dispatch(un->un_xbuf_attr);
6428
6429 sd_start_cmds(un, NULL);
6430 mutex_exit(SD_MUTEX(un));
6431
6432 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6433
6434 return (DDI_SUCCESS);
6435 }
6436
6437
6438 /*
6439 * Function: sd_pm_state_change
6440 *
6441 * Description: Change the driver power state.
6442 * Someone else is required to actually change the driver
6443 * power level.
6444 *
6445 * Arguments: un - driver soft state (unit) structure
6446 * level - the power level that is changed to
6447 * flag - to decide how to change the power state
6448 *
6449 * Return Code: DDI_SUCCESS
6450 *
6451 * Context: Kernel thread context
6452 */
6453 static int
6454 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6455 {
6456 ASSERT(un != NULL);
6457 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6458
6459 ASSERT(!mutex_owned(SD_MUTEX(un)));
6460 mutex_enter(SD_MUTEX(un));
6461
6462 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6463 un->un_power_level = level;
6464 ASSERT(!mutex_owned(&un->un_pm_mutex));
6465 mutex_enter(&un->un_pm_mutex);
6466 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6467 un->un_pm_count++;
6468 ASSERT(un->un_pm_count == 0);
6469 }
6470 mutex_exit(&un->un_pm_mutex);
6471 } else {
6472 /*
6473 * Exit if power management is not enabled for this device,
6474 * or if the device is being used by HA.
6475 */
6476 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6477 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6478 mutex_exit(SD_MUTEX(un));
6479 SD_TRACE(SD_LOG_POWER, un,
6480 "sd_pm_state_change: exiting\n");
6481 return (DDI_FAILURE);
6482 }
6483
6484 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6485 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6486
6487 /*
6488 * See if the device is not busy, ie.:
6489 * - we have no commands in the driver for this device
6490 * - not waiting for resources
6491 */
6492 if ((un->un_ncmds_in_driver == 0) &&
6493 (un->un_state != SD_STATE_RWAIT)) {
6494 /*
6495 * The device is not busy, so it is OK to go to low
6496 * power state. Indicate low power, but rely on someone
6497 * else to actually change it.
6498 */
6499 mutex_enter(&un->un_pm_mutex);
6500 un->un_pm_count = -1;
6501 mutex_exit(&un->un_pm_mutex);
6502 un->un_power_level = level;
6503 }
6504 }
6505
6506 mutex_exit(SD_MUTEX(un));
6507
6508 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6509
6510 return (DDI_SUCCESS);
6511 }
6512
6513
6514 /*
6515 * Function: sd_pm_idletimeout_handler
6516 *
6517 * Description: A timer routine that's active only while a device is busy.
6518 * The purpose is to extend slightly the pm framework's busy
6519 * view of the device to prevent busy/idle thrashing for
6520 * back-to-back commands. Do this by comparing the current time
6521 * to the time at which the last command completed and when the
6522 * difference is greater than sd_pm_idletime, call
6523 * pm_idle_component. In addition to indicating idle to the pm
6524 * framework, update the chain type to again use the internal pm
6525 * layers of the driver.
6526 *
6527 * Arguments: arg - driver soft state (unit) structure
6528 *
6529 * Context: Executes in a timeout(9F) thread context
6530 */
6531
6532 static void
6533 sd_pm_idletimeout_handler(void *arg)
6534 {
6535 struct sd_lun *un = arg;
6536
6537 time_t now;
6538
6539 mutex_enter(&sd_detach_mutex);
6540 if (un->un_detach_count != 0) {
6541 /* Abort if the instance is detaching */
6542 mutex_exit(&sd_detach_mutex);
6543 return;
6544 }
6545 mutex_exit(&sd_detach_mutex);
6546
6547 now = ddi_get_time();
6548 /*
6549 * Grab both mutexes, in the proper order, since we're accessing
6550 * both PM and softstate variables.
6551 */
6552 mutex_enter(SD_MUTEX(un));
6553 mutex_enter(&un->un_pm_mutex);
6554 if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
6555 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6556 /*
6557 * Update the chain types.
6558 * This takes affect on the next new command received.
6559 */
6560 if (un->un_f_non_devbsize_supported) {
6561 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6562 } else {
6563 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6564 }
6565 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6566
6567 SD_TRACE(SD_LOG_IO_PM, un,
6568 "sd_pm_idletimeout_handler: idling device\n");
6569 (void) pm_idle_component(SD_DEVINFO(un), 0);
6570 un->un_pm_idle_timeid = NULL;
6571 } else {
6572 un->un_pm_idle_timeid =
6573 timeout(sd_pm_idletimeout_handler, un,
6574 (drv_usectohz((clock_t)300000))); /* 300 ms. */
6575 }
6576 mutex_exit(&un->un_pm_mutex);
6577 mutex_exit(SD_MUTEX(un));
6578 }
6579
6580
6581 /*
6582 * Function: sd_pm_timeout_handler
6583 *
6584 * Description: Callback to tell framework we are idle.
6585 *
6586 * Context: timeout(9f) thread context.
6587 */
6588
6589 static void
6590 sd_pm_timeout_handler(void *arg)
6591 {
6592 struct sd_lun *un = arg;
6593
6594 (void) pm_idle_component(SD_DEVINFO(un), 0);
6595 mutex_enter(&un->un_pm_mutex);
6596 un->un_pm_timeid = NULL;
6597 mutex_exit(&un->un_pm_mutex);
6598 }
6599
6600
6601 /*
6602 * Function: sdpower
6603 *
6604 * Description: PM entry point.
6605 *
6606 * Return Code: DDI_SUCCESS
6607 * DDI_FAILURE
6608 *
6609 * Context: Kernel thread context
6610 */
6611
6612 static int
6613 sdpower(dev_info_t *devi, int component, int level)
6614 {
6615 struct sd_lun *un;
6616 int instance;
6617 int rval = DDI_SUCCESS;
6618 uint_t i, log_page_size, maxcycles, ncycles;
6619 uchar_t *log_page_data;
6620 int log_sense_page;
6621 int medium_present;
6622 time_t intvlp;
6623 struct pm_trans_data sd_pm_tran_data;
6624 uchar_t save_state;
6625 int sval;
6626 uchar_t state_before_pm;
6627 int got_semaphore_here;
6628 sd_ssc_t *ssc;
6629 int last_power_level;
6630
6631 instance = ddi_get_instance(devi);
6632
6633 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6634 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6635 return (DDI_FAILURE);
6636 }
6637
6638 ssc = sd_ssc_init(un);
6639
6640 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6641
6642 /*
6643 * Must synchronize power down with close.
6644 * Attempt to decrement/acquire the open/close semaphore,
6645 * but do NOT wait on it. If it's not greater than zero,
6646 * ie. it can't be decremented without waiting, then
6647 * someone else, either open or close, already has it
6648 * and the try returns 0. Use that knowledge here to determine
6649 * if it's OK to change the device power level.
6650 * Also, only increment it on exit if it was decremented, ie. gotten,
6651 * here.
6652 */
6653 got_semaphore_here = sema_tryp(&un->un_semoclose);
6654
6655 mutex_enter(SD_MUTEX(un));
6656
6657 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6658 un->un_ncmds_in_driver);
6659
6660 /*
6661 * If un_ncmds_in_driver is non-zero it indicates commands are
6662 * already being processed in the driver, or if the semaphore was
6663 * not gotten here it indicates an open or close is being processed.
6664 * At the same time somebody is requesting to go to a lower power
6665 * that can't perform I/O, which can't happen, therefore we need to
6666 * return failure.
6667 */
6668 if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6669 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6670 mutex_exit(SD_MUTEX(un));
6671
6672 if (got_semaphore_here != 0) {
6673 sema_v(&un->un_semoclose);
6674 }
6675 SD_TRACE(SD_LOG_IO_PM, un,
6676 "sdpower: exit, device has queued cmds.\n");
6677
6678 goto sdpower_failed;
6679 }
6680
6681 /*
6682 * if it is OFFLINE that means the disk is completely dead
6683 * in our case we have to put the disk in on or off by sending commands
6684 * Of course that will fail anyway so return back here.
6685 *
6686 * Power changes to a device that's OFFLINE or SUSPENDED
6687 * are not allowed.
6688 */
6689 if ((un->un_state == SD_STATE_OFFLINE) ||
6690 (un->un_state == SD_STATE_SUSPENDED)) {
6691 mutex_exit(SD_MUTEX(un));
6692
6693 if (got_semaphore_here != 0) {
6694 sema_v(&un->un_semoclose);
6695 }
6696 SD_TRACE(SD_LOG_IO_PM, un,
6697 "sdpower: exit, device is off-line.\n");
6698
6699 goto sdpower_failed;
6700 }
6701
6702 /*
6703 * Change the device's state to indicate it's power level
6704 * is being changed. Do this to prevent a power off in the
6705 * middle of commands, which is especially bad on devices
6706 * that are really powered off instead of just spun down.
6707 */
6708 state_before_pm = un->un_state;
6709 un->un_state = SD_STATE_PM_CHANGING;
6710
6711 mutex_exit(SD_MUTEX(un));
6712
6713 /*
6714 * If log sense command is not supported, bypass the
6715 * following checking, otherwise, check the log sense
6716 * information for this device.
6717 */
6718 if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6719 un->un_f_log_sense_supported) {
6720 /*
6721 * Get the log sense information to understand whether the
6722 * the powercycle counts have gone beyond the threshhold.
6723 */
6724 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6725 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6726
6727 mutex_enter(SD_MUTEX(un));
6728 log_sense_page = un->un_start_stop_cycle_page;
6729 mutex_exit(SD_MUTEX(un));
6730
6731 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6732 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6733
6734 if (rval != 0) {
6735 if (rval == EIO)
6736 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6737 else
6738 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6739 }
6740
6741 #ifdef SDDEBUG
6742 if (sd_force_pm_supported) {
6743 /* Force a successful result */
6744 rval = 0;
6745 }
6746 #endif
6747 if (rval != 0) {
6748 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6749 "Log Sense Failed\n");
6750
6751 kmem_free(log_page_data, log_page_size);
6752 /* Cannot support power management on those drives */
6753
6754 if (got_semaphore_here != 0) {
6755 sema_v(&un->un_semoclose);
6756 }
6757 /*
6758 * On exit put the state back to it's original value
6759 * and broadcast to anyone waiting for the power
6760 * change completion.
6761 */
6762 mutex_enter(SD_MUTEX(un));
6763 un->un_state = state_before_pm;
6764 cv_broadcast(&un->un_suspend_cv);
6765 mutex_exit(SD_MUTEX(un));
6766 SD_TRACE(SD_LOG_IO_PM, un,
6767 "sdpower: exit, Log Sense Failed.\n");
6768
6769 goto sdpower_failed;
6770 }
6771
6772 /*
6773 * From the page data - Convert the essential information to
6774 * pm_trans_data
6775 */
6776 maxcycles =
6777 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6778 (log_page_data[0x1E] << 8) | log_page_data[0x1F];
6779
6780 ncycles =
6781 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6782 (log_page_data[0x26] << 8) | log_page_data[0x27];
6783
6784 if (un->un_f_pm_log_sense_smart) {
6785 sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6786 sd_pm_tran_data.un.smart_count.consumed = ncycles;
6787 sd_pm_tran_data.un.smart_count.flag = 0;
6788 sd_pm_tran_data.format = DC_SMART_FORMAT;
6789 } else {
6790 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6791 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6792 for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6793 sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6794 log_page_data[8+i];
6795 }
6796 sd_pm_tran_data.un.scsi_cycles.flag = 0;
6797 sd_pm_tran_data.format = DC_SCSI_FORMAT;
6798 }
6799
6800 kmem_free(log_page_data, log_page_size);
6801
6802 /*
6803 * Call pm_trans_check routine to get the Ok from
6804 * the global policy
6805 */
6806 rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6807 #ifdef SDDEBUG
6808 if (sd_force_pm_supported) {
6809 /* Force a successful result */
6810 rval = 1;
6811 }
6812 #endif
6813 switch (rval) {
6814 case 0:
6815 /*
6816 * Not Ok to Power cycle or error in parameters passed
6817 * Would have given the advised time to consider power
6818 * cycle. Based on the new intvlp parameter we are
6819 * supposed to pretend we are busy so that pm framework
6820 * will never call our power entry point. Because of
6821 * that install a timeout handler and wait for the
6822 * recommended time to elapse so that power management
6823 * can be effective again.
6824 *
6825 * To effect this behavior, call pm_busy_component to
6826 * indicate to the framework this device is busy.
6827 * By not adjusting un_pm_count the rest of PM in
6828 * the driver will function normally, and independent
6829 * of this but because the framework is told the device
6830 * is busy it won't attempt powering down until it gets
6831 * a matching idle. The timeout handler sends this.
6832 * Note: sd_pm_entry can't be called here to do this
6833 * because sdpower may have been called as a result
6834 * of a call to pm_raise_power from within sd_pm_entry.
6835 *
6836 * If a timeout handler is already active then
6837 * don't install another.
6838 */
6839 mutex_enter(&un->un_pm_mutex);
6840 if (un->un_pm_timeid == NULL) {
6841 un->un_pm_timeid =
6842 timeout(sd_pm_timeout_handler,
6843 un, intvlp * drv_usectohz(1000000));
6844 mutex_exit(&un->un_pm_mutex);
6845 (void) pm_busy_component(SD_DEVINFO(un), 0);
6846 } else {
6847 mutex_exit(&un->un_pm_mutex);
6848 }
6849 if (got_semaphore_here != 0) {
6850 sema_v(&un->un_semoclose);
6851 }
6852 /*
6853 * On exit put the state back to it's original value
6854 * and broadcast to anyone waiting for the power
6855 * change completion.
6856 */
6857 mutex_enter(SD_MUTEX(un));
6858 un->un_state = state_before_pm;
6859 cv_broadcast(&un->un_suspend_cv);
6860 mutex_exit(SD_MUTEX(un));
6861
6862 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6863 "trans check Failed, not ok to power cycle.\n");
6864
6865 goto sdpower_failed;
6866 case -1:
6867 if (got_semaphore_here != 0) {
6868 sema_v(&un->un_semoclose);
6869 }
6870 /*
6871 * On exit put the state back to it's original value
6872 * and broadcast to anyone waiting for the power
6873 * change completion.
6874 */
6875 mutex_enter(SD_MUTEX(un));
6876 un->un_state = state_before_pm;
6877 cv_broadcast(&un->un_suspend_cv);
6878 mutex_exit(SD_MUTEX(un));
6879 SD_TRACE(SD_LOG_IO_PM, un,
6880 "sdpower: exit, trans check command Failed.\n");
6881
6882 goto sdpower_failed;
6883 }
6884 }
6885
6886 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6887 /*
6888 * Save the last state... if the STOP FAILS we need it
6889 * for restoring
6890 */
6891 mutex_enter(SD_MUTEX(un));
6892 save_state = un->un_last_state;
6893 last_power_level = un->un_power_level;
6894 /*
6895 * There must not be any cmds. getting processed
6896 * in the driver when we get here. Power to the
6897 * device is potentially going off.
6898 */
6899 ASSERT(un->un_ncmds_in_driver == 0);
6900 mutex_exit(SD_MUTEX(un));
6901
6902 /*
6903 * For now PM suspend the device completely before spindle is
6904 * turned off
6905 */
6906 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6907 == DDI_FAILURE) {
6908 if (got_semaphore_here != 0) {
6909 sema_v(&un->un_semoclose);
6910 }
6911 /*
6912 * On exit put the state back to it's original value
6913 * and broadcast to anyone waiting for the power
6914 * change completion.
6915 */
6916 mutex_enter(SD_MUTEX(un));
6917 un->un_state = state_before_pm;
6918 un->un_power_level = last_power_level;
6919 cv_broadcast(&un->un_suspend_cv);
6920 mutex_exit(SD_MUTEX(un));
6921 SD_TRACE(SD_LOG_IO_PM, un,
6922 "sdpower: exit, PM suspend Failed.\n");
6923
6924 goto sdpower_failed;
6925 }
6926 }
6927
6928 /*
6929 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6930 * close, or strategy. Dump no long uses this routine, it uses it's
6931 * own code so it can be done in polled mode.
6932 */
6933
6934 medium_present = TRUE;
6935
6936 /*
6937 * When powering up, issue a TUR in case the device is at unit
6938 * attention. Don't do retries. Bypass the PM layer, otherwise
6939 * a deadlock on un_pm_busy_cv will occur.
6940 */
6941 if (SD_PM_IS_IO_CAPABLE(un, level)) {
6942 sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6943 SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6944 if (sval != 0)
6945 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6946 }
6947
6948 if (un->un_f_power_condition_supported) {
6949 char *pm_condition_name[] = {"STOPPED", "STANDBY",
6950 "IDLE", "ACTIVE"};
6951 SD_TRACE(SD_LOG_IO_PM, un,
6952 "sdpower: sending \'%s\' power condition",
6953 pm_condition_name[level]);
6954 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6955 sd_pl2pc[level], SD_PATH_DIRECT);
6956 } else {
6957 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6958 ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6959 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6960 ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6961 SD_TARGET_STOP), SD_PATH_DIRECT);
6962 }
6963 if (sval != 0) {
6964 if (sval == EIO)
6965 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6966 else
6967 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6968 }
6969
6970 /* Command failed, check for media present. */
6971 if ((sval == ENXIO) && un->un_f_has_removable_media) {
6972 medium_present = FALSE;
6973 }
6974
6975 /*
6976 * The conditions of interest here are:
6977 * if a spindle off with media present fails,
6978 * then restore the state and return an error.
6979 * else if a spindle on fails,
6980 * then return an error (there's no state to restore).
6981 * In all other cases we setup for the new state
6982 * and return success.
6983 */
6984 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6985 if ((medium_present == TRUE) && (sval != 0)) {
6986 /* The stop command from above failed */
6987 rval = DDI_FAILURE;
6988 /*
6989 * The stop command failed, and we have media
6990 * present. Put the level back by calling the
6991 * sd_pm_resume() and set the state back to
6992 * it's previous value.
6993 */
6994 (void) sd_pm_state_change(un, last_power_level,
6995 SD_PM_STATE_ROLLBACK);
6996 mutex_enter(SD_MUTEX(un));
6997 un->un_last_state = save_state;
6998 mutex_exit(SD_MUTEX(un));
6999 } else if (un->un_f_monitor_media_state) {
7000 /*
7001 * The stop command from above succeeded.
7002 * Terminate watch thread in case of removable media
7003 * devices going into low power state. This is as per
7004 * the requirements of pm framework, otherwise commands
7005 * will be generated for the device (through watch
7006 * thread), even when the device is in low power state.
7007 */
7008 mutex_enter(SD_MUTEX(un));
7009 un->un_f_watcht_stopped = FALSE;
7010 if (un->un_swr_token != NULL) {
7011 opaque_t temp_token = un->un_swr_token;
7012 un->un_f_watcht_stopped = TRUE;
7013 un->un_swr_token = NULL;
7014 mutex_exit(SD_MUTEX(un));
7015 (void) scsi_watch_request_terminate(temp_token,
7016 SCSI_WATCH_TERMINATE_ALL_WAIT);
7017 } else {
7018 mutex_exit(SD_MUTEX(un));
7019 }
7020 }
7021 } else {
7022 /*
7023 * The level requested is I/O capable.
7024 * Legacy behavior: return success on a failed spinup
7025 * if there is no media in the drive.
7026 * Do this by looking at medium_present here.
7027 */
7028 if ((sval != 0) && medium_present) {
7029 /* The start command from above failed */
7030 rval = DDI_FAILURE;
7031 } else {
7032 /*
7033 * The start command from above succeeded
7034 * PM resume the devices now that we have
7035 * started the disks
7036 */
7037 (void) sd_pm_state_change(un, level,
7038 SD_PM_STATE_CHANGE);
7039
7040 /*
7041 * Resume the watch thread since it was suspended
7042 * when the device went into low power mode.
7043 */
7044 if (un->un_f_monitor_media_state) {
7045 mutex_enter(SD_MUTEX(un));
7046 if (un->un_f_watcht_stopped == TRUE) {
7047 opaque_t temp_token;
7048
7049 un->un_f_watcht_stopped = FALSE;
7050 mutex_exit(SD_MUTEX(un));
7051 temp_token =
7052 sd_watch_request_submit(un);
7053 mutex_enter(SD_MUTEX(un));
7054 un->un_swr_token = temp_token;
7055 }
7056 mutex_exit(SD_MUTEX(un));
7057 }
7058 }
7059 }
7060
7061 if (got_semaphore_here != 0) {
7062 sema_v(&un->un_semoclose);
7063 }
7064 /*
7065 * On exit put the state back to it's original value
7066 * and broadcast to anyone waiting for the power
7067 * change completion.
7068 */
7069 mutex_enter(SD_MUTEX(un));
7070 un->un_state = state_before_pm;
7071 cv_broadcast(&un->un_suspend_cv);
7072 mutex_exit(SD_MUTEX(un));
7073
7074 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7075
7076 sd_ssc_fini(ssc);
7077 return (rval);
7078
7079 sdpower_failed:
7080
7081 sd_ssc_fini(ssc);
7082 return (DDI_FAILURE);
7083 }
7084
7085
7086
7087 /*
7088 * Function: sdattach
7089 *
7090 * Description: Driver's attach(9e) entry point function.
7091 *
7092 * Arguments: devi - opaque device info handle
7093 * cmd - attach type
7094 *
7095 * Return Code: DDI_SUCCESS
7096 * DDI_FAILURE
7097 *
7098 * Context: Kernel thread context
7099 */
7100
7101 static int
7102 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7103 {
7104 switch (cmd) {
7105 case DDI_ATTACH:
7106 return (sd_unit_attach(devi));
7107 case DDI_RESUME:
7108 return (sd_ddi_resume(devi));
7109 default:
7110 break;
7111 }
7112 return (DDI_FAILURE);
7113 }
7114
7115
7116 /*
7117 * Function: sddetach
7118 *
7119 * Description: Driver's detach(9E) entry point function.
7120 *
7121 * Arguments: devi - opaque device info handle
7122 * cmd - detach type
7123 *
7124 * Return Code: DDI_SUCCESS
7125 * DDI_FAILURE
7126 *
7127 * Context: Kernel thread context
7128 */
7129
7130 static int
7131 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7132 {
7133 switch (cmd) {
7134 case DDI_DETACH:
7135 return (sd_unit_detach(devi));
7136 case DDI_SUSPEND:
7137 return (sd_ddi_suspend(devi));
7138 default:
7139 break;
7140 }
7141 return (DDI_FAILURE);
7142 }
7143
7144
7145 /*
7146 * Function: sd_sync_with_callback
7147 *
7148 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7149 * state while the callback routine is active.
7150 *
7151 * Arguments: un: softstate structure for the instance
7152 *
7153 * Context: Kernel thread context
7154 */
7155
7156 static void
7157 sd_sync_with_callback(struct sd_lun *un)
7158 {
7159 ASSERT(un != NULL);
7160
7161 mutex_enter(SD_MUTEX(un));
7162
7163 ASSERT(un->un_in_callback >= 0);
7164
7165 while (un->un_in_callback > 0) {
7166 mutex_exit(SD_MUTEX(un));
7167 delay(2);
7168 mutex_enter(SD_MUTEX(un));
7169 }
7170
7171 mutex_exit(SD_MUTEX(un));
7172 }
7173
7174 /*
7175 * Function: sd_unit_attach
7176 *
7177 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7178 * the soft state structure for the device and performs
7179 * all necessary structure and device initializations.
7180 *
7181 * Arguments: devi: the system's dev_info_t for the device.
7182 *
7183 * Return Code: DDI_SUCCESS if attach is successful.
7184 * DDI_FAILURE if any part of the attach fails.
7185 *
7186 * Context: Called at attach(9e) time for the DDI_ATTACH flag.
7187 * Kernel thread context only. Can sleep.
7188 */
7189
7190 static int
7191 sd_unit_attach(dev_info_t *devi)
7192 {
7193 struct scsi_device *devp;
7194 struct sd_lun *un;
7195 char *variantp;
7196 char name_str[48];
7197 int reservation_flag = SD_TARGET_IS_UNRESERVED;
7198 int instance;
7199 int rval;
7200 int wc_enabled;
7201 int tgt;
7202 uint64_t capacity;
7203 uint_t lbasize = 0;
7204 dev_info_t *pdip = ddi_get_parent(devi);
7205 int offbyone = 0;
7206 int geom_label_valid = 0;
7207 sd_ssc_t *ssc;
7208 int status;
7209 struct sd_fm_internal *sfip = NULL;
7210 int max_xfer_size;
7211
7212 /*
7213 * Retrieve the target driver's private data area. This was set
7214 * up by the HBA.
7215 */
7216 devp = ddi_get_driver_private(devi);
7217
7218 /*
7219 * Retrieve the target ID of the device.
7220 */
7221 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7222 SCSI_ADDR_PROP_TARGET, -1);
7223
7224 /*
7225 * Since we have no idea what state things were left in by the last
7226 * user of the device, set up some 'default' settings, ie. turn 'em
7227 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7228 * Do this before the scsi_probe, which sends an inquiry.
7229 * This is a fix for bug (4430280).
7230 * Of special importance is wide-xfer. The drive could have been left
7231 * in wide transfer mode by the last driver to communicate with it,
7232 * this includes us. If that's the case, and if the following is not
7233 * setup properly or we don't re-negotiate with the drive prior to
7234 * transferring data to/from the drive, it causes bus parity errors,
7235 * data overruns, and unexpected interrupts. This first occurred when
7236 * the fix for bug (4378686) was made.
7237 */
7238 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7239 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7240 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7241
7242 /*
7243 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7244 * on a target. Setting it per lun instance actually sets the
7245 * capability of this target, which affects those luns already
7246 * attached on the same target. So during attach, we can only disable
7247 * this capability only when no other lun has been attached on this
7248 * target. By doing this, we assume a target has the same tagged-qing
7249 * capability for every lun. The condition can be removed when HBA
7250 * is changed to support per lun based tagged-qing capability.
7251 */
7252 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7253 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7254 }
7255
7256 /*
7257 * Use scsi_probe() to issue an INQUIRY command to the device.
7258 * This call will allocate and fill in the scsi_inquiry structure
7259 * and point the sd_inq member of the scsi_device structure to it.
7260 * If the attach succeeds, then this memory will not be de-allocated
7261 * (via scsi_unprobe()) until the instance is detached.
7262 */
7263 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7264 goto probe_failed;
7265 }
7266
7267 /*
7268 * Check the device type as specified in the inquiry data and
7269 * claim it if it is of a type that we support.
7270 */
7271 switch (devp->sd_inq->inq_dtype) {
7272 case DTYPE_DIRECT:
7273 break;
7274 case DTYPE_RODIRECT:
7275 break;
7276 case DTYPE_OPTICAL:
7277 break;
7278 case DTYPE_NOTPRESENT:
7279 default:
7280 /* Unsupported device type; fail the attach. */
7281 goto probe_failed;
7282 }
7283
7284 /*
7285 * Allocate the soft state structure for this unit.
7286 *
7287 * We rely upon this memory being set to all zeroes by
7288 * ddi_soft_state_zalloc(). We assume that any member of the
7289 * soft state structure that is not explicitly initialized by
7290 * this routine will have a value of zero.
7291 */
7292 instance = ddi_get_instance(devp->sd_dev);
7293 #ifndef XPV_HVM_DRIVER
7294 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7295 goto probe_failed;
7296 }
7297 #endif /* !XPV_HVM_DRIVER */
7298
7299 /*
7300 * Retrieve a pointer to the newly-allocated soft state.
7301 *
7302 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7303 * was successful, unless something has gone horribly wrong and the
7304 * ddi's soft state internals are corrupt (in which case it is
7305 * probably better to halt here than just fail the attach....)
7306 */
7307 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7308 panic("sd_unit_attach: NULL soft state on instance:0x%x",
7309 instance);
7310 /*NOTREACHED*/
7311 }
7312
7313 /*
7314 * Link the back ptr of the driver soft state to the scsi_device
7315 * struct for this lun.
7316 * Save a pointer to the softstate in the driver-private area of
7317 * the scsi_device struct.
7318 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7319 * we first set un->un_sd below.
7320 */
7321 un->un_sd = devp;
7322 devp->sd_private = (opaque_t)un;
7323
7324 /*
7325 * The following must be after devp is stored in the soft state struct.
7326 */
7327 #ifdef SDDEBUG
7328 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7329 "%s_unit_attach: un:0x%p instance:%d\n",
7330 ddi_driver_name(devi), un, instance);
7331 #endif
7332
7333 /*
7334 * Set up the device type and node type (for the minor nodes).
7335 * By default we assume that the device can at least support the
7336 * Common Command Set. Call it a CD-ROM if it reports itself
7337 * as a RODIRECT device.
7338 */
7339 switch (devp->sd_inq->inq_dtype) {
7340 case DTYPE_RODIRECT:
7341 un->un_node_type = DDI_NT_CD_CHAN;
7342 un->un_ctype = CTYPE_CDROM;
7343 break;
7344 case DTYPE_OPTICAL:
7345 un->un_node_type = DDI_NT_BLOCK_CHAN;
7346 un->un_ctype = CTYPE_ROD;
7347 break;
7348 default:
7349 un->un_node_type = DDI_NT_BLOCK_CHAN;
7350 un->un_ctype = CTYPE_CCS;
7351 break;
7352 }
7353
7354 /*
7355 * Try to read the interconnect type from the HBA.
7356 *
7357 * Note: This driver is currently compiled as two binaries, a parallel
7358 * scsi version (sd) and a fibre channel version (ssd). All functional
7359 * differences are determined at compile time. In the future a single
7360 * binary will be provided and the interconnect type will be used to
7361 * differentiate between fibre and parallel scsi behaviors. At that time
7362 * it will be necessary for all fibre channel HBAs to support this
7363 * property.
7364 *
7365 * set un_f_is_fiber to TRUE ( default fiber )
7366 */
7367 un->un_f_is_fibre = TRUE;
7368 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7369 case INTERCONNECT_SSA:
7370 un->un_interconnect_type = SD_INTERCONNECT_SSA;
7371 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7372 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7373 break;
7374 case INTERCONNECT_PARALLEL:
7375 un->un_f_is_fibre = FALSE;
7376 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7377 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7378 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7379 break;
7380 case INTERCONNECT_SAS:
7381 un->un_f_is_fibre = FALSE;
7382 un->un_interconnect_type = SD_INTERCONNECT_SAS;
7383 un->un_node_type = DDI_NT_BLOCK_SAS;
7384 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7385 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7386 break;
7387 case INTERCONNECT_SATA:
7388 un->un_f_is_fibre = FALSE;
7389 un->un_interconnect_type = SD_INTERCONNECT_SATA;
7390 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7391 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7392 break;
7393 case INTERCONNECT_FIBRE:
7394 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7395 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7396 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7397 break;
7398 case INTERCONNECT_FABRIC:
7399 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7400 un->un_node_type = DDI_NT_BLOCK_FABRIC;
7401 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7402 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7403 break;
7404 default:
7405 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7406 /*
7407 * The HBA does not support the "interconnect-type" property
7408 * (or did not provide a recognized type).
7409 *
7410 * Note: This will be obsoleted when a single fibre channel
7411 * and parallel scsi driver is delivered. In the meantime the
7412 * interconnect type will be set to the platform default.If that
7413 * type is not parallel SCSI, it means that we should be
7414 * assuming "ssd" semantics. However, here this also means that
7415 * the FC HBA is not supporting the "interconnect-type" property
7416 * like we expect it to, so log this occurrence.
7417 */
7418 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7419 if (!SD_IS_PARALLEL_SCSI(un)) {
7420 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7421 "sd_unit_attach: un:0x%p Assuming "
7422 "INTERCONNECT_FIBRE\n", un);
7423 } else {
7424 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7425 "sd_unit_attach: un:0x%p Assuming "
7426 "INTERCONNECT_PARALLEL\n", un);
7427 un->un_f_is_fibre = FALSE;
7428 }
7429 #else
7430 /*
7431 * Note: This source will be implemented when a single fibre
7432 * channel and parallel scsi driver is delivered. The default
7433 * will be to assume that if a device does not support the
7434 * "interconnect-type" property it is a parallel SCSI HBA and
7435 * we will set the interconnect type for parallel scsi.
7436 */
7437 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7438 un->un_f_is_fibre = FALSE;
7439 #endif
7440 break;
7441 }
7442
7443 if (un->un_f_is_fibre == TRUE) {
7444 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7445 SCSI_VERSION_3) {
7446 switch (un->un_interconnect_type) {
7447 case SD_INTERCONNECT_FIBRE:
7448 case SD_INTERCONNECT_SSA:
7449 un->un_node_type = DDI_NT_BLOCK_WWN;
7450 break;
7451 default:
7452 break;
7453 }
7454 }
7455 }
7456
7457 /*
7458 * Initialize the Request Sense command for the target
7459 */
7460 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7461 goto alloc_rqs_failed;
7462 }
7463
7464 /*
7465 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7466 * with separate binary for sd and ssd.
7467 *
7468 * x86 has 1 binary, un_retry_count is set base on connection type.
7469 * The hardcoded values will go away when Sparc uses 1 binary
7470 * for sd and ssd. This hardcoded values need to match
7471 * SD_RETRY_COUNT in sddef.h
7472 * The value used is base on interconnect type.
7473 * fibre = 3, parallel = 5
7474 */
7475 #if defined(__i386) || defined(__amd64)
7476 un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7477 #else
7478 un->un_retry_count = SD_RETRY_COUNT;
7479 #endif
7480
7481 /*
7482 * Set the per disk retry count to the default number of retries
7483 * for disks and CDROMs. This value can be overridden by the
7484 * disk property list or an entry in sd.conf.
7485 */
7486 un->un_notready_retry_count =
7487 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7488 : DISK_NOT_READY_RETRY_COUNT(un);
7489
7490 /*
7491 * Set the busy retry count to the default value of un_retry_count.
7492 * This can be overridden by entries in sd.conf or the device
7493 * config table.
7494 */
7495 un->un_busy_retry_count = un->un_retry_count;
7496
7497 /*
7498 * Init the reset threshold for retries. This number determines
7499 * how many retries must be performed before a reset can be issued
7500 * (for certain error conditions). This can be overridden by entries
7501 * in sd.conf or the device config table.
7502 */
7503 un->un_reset_retry_count = (un->un_retry_count / 2);
7504
7505 /*
7506 * Set the victim_retry_count to the default un_retry_count
7507 */
7508 un->un_victim_retry_count = (2 * un->un_retry_count);
7509
7510 /*
7511 * Set the reservation release timeout to the default value of
7512 * 5 seconds. This can be overridden by entries in ssd.conf or the
7513 * device config table.
7514 */
7515 un->un_reserve_release_time = 5;
7516
7517 /*
7518 * Set up the default maximum transfer size. Note that this may
7519 * get updated later in the attach, when setting up default wide
7520 * operations for disks.
7521 */
7522 #if defined(__i386) || defined(__amd64)
7523 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7524 un->un_partial_dma_supported = 1;
7525 #else
7526 un->un_max_xfer_size = (uint_t)maxphys;
7527 #endif
7528
7529 /*
7530 * Get "allow bus device reset" property (defaults to "enabled" if
7531 * the property was not defined). This is to disable bus resets for
7532 * certain kinds of error recovery. Note: In the future when a run-time
7533 * fibre check is available the soft state flag should default to
7534 * enabled.
7535 */
7536 if (un->un_f_is_fibre == TRUE) {
7537 un->un_f_allow_bus_device_reset = TRUE;
7538 } else {
7539 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7540 "allow-bus-device-reset", 1) != 0) {
7541 un->un_f_allow_bus_device_reset = TRUE;
7542 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7543 "sd_unit_attach: un:0x%p Bus device reset "
7544 "enabled\n", un);
7545 } else {
7546 un->un_f_allow_bus_device_reset = FALSE;
7547 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7548 "sd_unit_attach: un:0x%p Bus device reset "
7549 "disabled\n", un);
7550 }
7551 }
7552
7553 /*
7554 * Check if this is an ATAPI device. ATAPI devices use Group 1
7555 * Read/Write commands and Group 2 Mode Sense/Select commands.
7556 *
7557 * Note: The "obsolete" way of doing this is to check for the "atapi"
7558 * property. The new "variant" property with a value of "atapi" has been
7559 * introduced so that future 'variants' of standard SCSI behavior (like
7560 * atapi) could be specified by the underlying HBA drivers by supplying
7561 * a new value for the "variant" property, instead of having to define a
7562 * new property.
7563 */
7564 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7565 un->un_f_cfg_is_atapi = TRUE;
7566 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7567 "sd_unit_attach: un:0x%p Atapi device\n", un);
7568 }
7569 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7570 &variantp) == DDI_PROP_SUCCESS) {
7571 if (strcmp(variantp, "atapi") == 0) {
7572 un->un_f_cfg_is_atapi = TRUE;
7573 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7574 "sd_unit_attach: un:0x%p Atapi device\n", un);
7575 }
7576 ddi_prop_free(variantp);
7577 }
7578
7579 un->un_cmd_timeout = SD_IO_TIME;
7580
7581 un->un_busy_timeout = SD_BSY_TIMEOUT;
7582
7583 /* Info on current states, statuses, etc. (Updated frequently) */
7584 un->un_state = SD_STATE_NORMAL;
7585 un->un_last_state = SD_STATE_NORMAL;
7586
7587 /* Control & status info for command throttling */
7588 un->un_throttle = sd_max_throttle;
7589 un->un_saved_throttle = sd_max_throttle;
7590 un->un_min_throttle = sd_min_throttle;
7591
7592 if (un->un_f_is_fibre == TRUE) {
7593 un->un_f_use_adaptive_throttle = TRUE;
7594 } else {
7595 un->un_f_use_adaptive_throttle = FALSE;
7596 }
7597
7598 /* Removable media support. */
7599 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7600 un->un_mediastate = DKIO_NONE;
7601 un->un_specified_mediastate = DKIO_NONE;
7602
7603 /* CVs for suspend/resume (PM or DR) */
7604 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
7605 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7606
7607 /* Power management support. */
7608 un->un_power_level = SD_SPINDLE_UNINIT;
7609
7610 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
7611 un->un_f_wcc_inprog = 0;
7612
7613 /*
7614 * The open/close semaphore is used to serialize threads executing
7615 * in the driver's open & close entry point routines for a given
7616 * instance.
7617 */
7618 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7619
7620 /*
7621 * The conf file entry and softstate variable is a forceful override,
7622 * meaning a non-zero value must be entered to change the default.
7623 */
7624 un->un_f_disksort_disabled = FALSE;
7625 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7626 un->un_f_enable_rmw = FALSE;
7627
7628 /*
7629 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7630 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7631 */
7632 un->un_f_mmc_gesn_polling = TRUE;
7633
7634 /*
7635 * physical sector size defaults to DEV_BSIZE currently. We can
7636 * override this value via the driver configuration file so we must
7637 * set it before calling sd_read_unit_properties().
7638 */
7639 un->un_phy_blocksize = DEV_BSIZE;
7640
7641 /*
7642 * Retrieve the properties from the static driver table or the driver
7643 * configuration file (.conf) for this unit and update the soft state
7644 * for the device as needed for the indicated properties.
7645 * Note: the property configuration needs to occur here as some of the
7646 * following routines may have dependencies on soft state flags set
7647 * as part of the driver property configuration.
7648 */
7649 sd_read_unit_properties(un);
7650 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7651 "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7652
7653 /*
7654 * Only if a device has "hotpluggable" property, it is
7655 * treated as hotpluggable device. Otherwise, it is
7656 * regarded as non-hotpluggable one.
7657 */
7658 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7659 -1) != -1) {
7660 un->un_f_is_hotpluggable = TRUE;
7661 }
7662
7663 /*
7664 * set unit's attributes(flags) according to "hotpluggable" and
7665 * RMB bit in INQUIRY data.
7666 */
7667 sd_set_unit_attributes(un, devi);
7668
7669 /*
7670 * By default, we mark the capacity, lbasize, and geometry
7671 * as invalid. Only if we successfully read a valid capacity
7672 * will we update the un_blockcount and un_tgt_blocksize with the
7673 * valid values (the geometry will be validated later).
7674 */
7675 un->un_f_blockcount_is_valid = FALSE;
7676 un->un_f_tgt_blocksize_is_valid = FALSE;
7677
7678 /*
7679 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7680 * otherwise.
7681 */
7682 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE;
7683 un->un_blockcount = 0;
7684
7685 /*
7686 * Set up the per-instance info needed to determine the correct
7687 * CDBs and other info for issuing commands to the target.
7688 */
7689 sd_init_cdb_limits(un);
7690
7691 /*
7692 * Set up the IO chains to use, based upon the target type.
7693 */
7694 if (un->un_f_non_devbsize_supported) {
7695 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7696 } else {
7697 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7698 }
7699 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
7700 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7701 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7702
7703 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7704 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit,
7705 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7706 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7707
7708
7709 if (ISCD(un)) {
7710 un->un_additional_codes = sd_additional_codes;
7711 } else {
7712 un->un_additional_codes = NULL;
7713 }
7714
7715 /*
7716 * Create the kstats here so they can be available for attach-time
7717 * routines that send commands to the unit (either polled or via
7718 * sd_send_scsi_cmd).
7719 *
7720 * Note: This is a critical sequence that needs to be maintained:
7721 * 1) Instantiate the kstats here, before any routines using the
7722 * iopath (i.e. sd_send_scsi_cmd).
7723 * 2) Instantiate and initialize the partition stats
7724 * (sd_set_pstats).
7725 * 3) Initialize the error stats (sd_set_errstats), following
7726 * sd_validate_geometry(),sd_register_devid(),
7727 * and sd_cache_control().
7728 */
7729
7730 un->un_stats = kstat_create(sd_label, instance,
7731 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7732 if (un->un_stats != NULL) {
7733 un->un_stats->ks_lock = SD_MUTEX(un);
7734 kstat_install(un->un_stats);
7735 }
7736 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7737 "sd_unit_attach: un:0x%p un_stats created\n", un);
7738
7739 sd_create_errstats(un, instance);
7740 if (un->un_errstats == NULL) {
7741 goto create_errstats_failed;
7742 }
7743 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7744 "sd_unit_attach: un:0x%p errstats created\n", un);
7745
7746 /*
7747 * The following if/else code was relocated here from below as part
7748 * of the fix for bug (4430280). However with the default setup added
7749 * on entry to this routine, it's no longer absolutely necessary for
7750 * this to be before the call to sd_spin_up_unit.
7751 */
7752 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7753 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7754 (devp->sd_inq->inq_ansi == 5)) &&
7755 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7756
7757 /*
7758 * If tagged queueing is supported by the target
7759 * and by the host adapter then we will enable it
7760 */
7761 un->un_tagflags = 0;
7762 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7763 (un->un_f_arq_enabled == TRUE)) {
7764 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7765 1, 1) == 1) {
7766 un->un_tagflags = FLAG_STAG;
7767 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7768 "sd_unit_attach: un:0x%p tag queueing "
7769 "enabled\n", un);
7770 } else if (scsi_ifgetcap(SD_ADDRESS(un),
7771 "untagged-qing", 0) == 1) {
7772 un->un_f_opt_queueing = TRUE;
7773 un->un_saved_throttle = un->un_throttle =
7774 min(un->un_throttle, 3);
7775 } else {
7776 un->un_f_opt_queueing = FALSE;
7777 un->un_saved_throttle = un->un_throttle = 1;
7778 }
7779 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7780 == 1) && (un->un_f_arq_enabled == TRUE)) {
7781 /* The Host Adapter supports internal queueing. */
7782 un->un_f_opt_queueing = TRUE;
7783 un->un_saved_throttle = un->un_throttle =
7784 min(un->un_throttle, 3);
7785 } else {
7786 un->un_f_opt_queueing = FALSE;
7787 un->un_saved_throttle = un->un_throttle = 1;
7788 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7789 "sd_unit_attach: un:0x%p no tag queueing\n", un);
7790 }
7791
7792 /*
7793 * Enable large transfers for SATA/SAS drives
7794 */
7795 if (SD_IS_SERIAL(un)) {
7796 un->un_max_xfer_size =
7797 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7798 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7799 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7800 "sd_unit_attach: un:0x%p max transfer "
7801 "size=0x%x\n", un, un->un_max_xfer_size);
7802
7803 }
7804
7805 /* Setup or tear down default wide operations for disks */
7806
7807 /*
7808 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7809 * and "ssd_max_xfer_size" to exist simultaneously on the same
7810 * system and be set to different values. In the future this
7811 * code may need to be updated when the ssd module is
7812 * obsoleted and removed from the system. (4299588)
7813 */
7814 if (SD_IS_PARALLEL_SCSI(un) &&
7815 (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7816 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7817 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7818 1, 1) == 1) {
7819 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7820 "sd_unit_attach: un:0x%p Wide Transfer "
7821 "enabled\n", un);
7822 }
7823
7824 /*
7825 * If tagged queuing has also been enabled, then
7826 * enable large xfers
7827 */
7828 if (un->un_saved_throttle == sd_max_throttle) {
7829 un->un_max_xfer_size =
7830 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7831 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7832 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7833 "sd_unit_attach: un:0x%p max transfer "
7834 "size=0x%x\n", un, un->un_max_xfer_size);
7835 }
7836 } else {
7837 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7838 0, 1) == 1) {
7839 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7840 "sd_unit_attach: un:0x%p "
7841 "Wide Transfer disabled\n", un);
7842 }
7843 }
7844 } else {
7845 un->un_tagflags = FLAG_STAG;
7846 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7847 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7848 }
7849
7850 /*
7851 * If this target supports LUN reset, try to enable it.
7852 */
7853 if (un->un_f_lun_reset_enabled) {
7854 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7855 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7856 "un:0x%p lun_reset capability set\n", un);
7857 } else {
7858 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7859 "un:0x%p lun-reset capability not set\n", un);
7860 }
7861 }
7862
7863 /*
7864 * Adjust the maximum transfer size. This is to fix
7865 * the problem of partial DMA support on SPARC. Some
7866 * HBA driver, like aac, has very small dma_attr_maxxfer
7867 * size, which requires partial DMA support on SPARC.
7868 * In the future the SPARC pci nexus driver may solve
7869 * the problem instead of this fix.
7870 */
7871 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7872 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7873 /* We need DMA partial even on sparc to ensure sddump() works */
7874 un->un_max_xfer_size = max_xfer_size;
7875 if (un->un_partial_dma_supported == 0)
7876 un->un_partial_dma_supported = 1;
7877 }
7878 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7879 DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7880 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7881 un->un_max_xfer_size) == 1) {
7882 un->un_buf_breakup_supported = 1;
7883 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7884 "un:0x%p Buf breakup enabled\n", un);
7885 }
7886 }
7887
7888 /*
7889 * Set PKT_DMA_PARTIAL flag.
7890 */
7891 if (un->un_partial_dma_supported == 1) {
7892 un->un_pkt_flags = PKT_DMA_PARTIAL;
7893 } else {
7894 un->un_pkt_flags = 0;
7895 }
7896
7897 /* Initialize sd_ssc_t for internal uscsi commands */
7898 ssc = sd_ssc_init(un);
7899 scsi_fm_init(devp);
7900
7901 /*
7902 * Allocate memory for SCSI FMA stuffs.
7903 */
7904 un->un_fm_private =
7905 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7906 sfip = (struct sd_fm_internal *)un->un_fm_private;
7907 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7908 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7909 sfip->fm_ssc.ssc_un = un;
7910
7911 if (ISCD(un) ||
7912 un->un_f_has_removable_media ||
7913 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7914 /*
7915 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7916 * Their log are unchanged.
7917 */
7918 sfip->fm_log_level = SD_FM_LOG_NSUP;
7919 } else {
7920 /*
7921 * If enter here, it should be non-CDROM and FM-capable
7922 * device, and it will not keep the old scsi_log as before
7923 * in /var/adm/messages. However, the property
7924 * "fm-scsi-log" will control whether the FM telemetry will
7925 * be logged in /var/adm/messages.
7926 */
7927 int fm_scsi_log;
7928 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7929 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7930
7931 if (fm_scsi_log)
7932 sfip->fm_log_level = SD_FM_LOG_EREPORT;
7933 else
7934 sfip->fm_log_level = SD_FM_LOG_SILENT;
7935 }
7936
7937 /*
7938 * At this point in the attach, we have enough info in the
7939 * soft state to be able to issue commands to the target.
7940 *
7941 * All command paths used below MUST issue their commands as
7942 * SD_PATH_DIRECT. This is important as intermediate layers
7943 * are not all initialized yet (such as PM).
7944 */
7945
7946 /*
7947 * Send a TEST UNIT READY command to the device. This should clear
7948 * any outstanding UNIT ATTENTION that may be present.
7949 *
7950 * Note: Don't check for success, just track if there is a reservation,
7951 * this is a throw away command to clear any unit attentions.
7952 *
7953 * Note: This MUST be the first command issued to the target during
7954 * attach to ensure power on UNIT ATTENTIONS are cleared.
7955 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7956 * with attempts at spinning up a device with no media.
7957 */
7958 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7959 if (status != 0) {
7960 if (status == EACCES)
7961 reservation_flag = SD_TARGET_IS_RESERVED;
7962 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7963 }
7964
7965 /*
7966 * If the device is NOT a removable media device, attempt to spin
7967 * it up (using the START_STOP_UNIT command) and read its capacity
7968 * (using the READ CAPACITY command). Note, however, that either
7969 * of these could fail and in some cases we would continue with
7970 * the attach despite the failure (see below).
7971 */
7972 if (un->un_f_descr_format_supported) {
7973
7974 switch (sd_spin_up_unit(ssc)) {
7975 case 0:
7976 /*
7977 * Spin-up was successful; now try to read the
7978 * capacity. If successful then save the results
7979 * and mark the capacity & lbasize as valid.
7980 */
7981 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7982 "sd_unit_attach: un:0x%p spin-up successful\n", un);
7983
7984 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7985 &lbasize, SD_PATH_DIRECT);
7986
7987 switch (status) {
7988 case 0: {
7989 if (capacity > DK_MAX_BLOCKS) {
7990 #ifdef _LP64
7991 if ((capacity + 1) >
7992 SD_GROUP1_MAX_ADDRESS) {
7993 /*
7994 * Enable descriptor format
7995 * sense data so that we can
7996 * get 64 bit sense data
7997 * fields.
7998 */
7999 sd_enable_descr_sense(ssc);
8000 }
8001 #else
8002 /* 32-bit kernels can't handle this */
8003 scsi_log(SD_DEVINFO(un),
8004 sd_label, CE_WARN,
8005 "disk has %llu blocks, which "
8006 "is too large for a 32-bit "
8007 "kernel", capacity);
8008
8009 #if defined(__i386) || defined(__amd64)
8010 /*
8011 * 1TB disk was treated as (1T - 512)B
8012 * in the past, so that it might have
8013 * valid VTOC and solaris partitions,
8014 * we have to allow it to continue to
8015 * work.
8016 */
8017 if (capacity -1 > DK_MAX_BLOCKS)
8018 #endif
8019 goto spinup_failed;
8020 #endif
8021 }
8022
8023 /*
8024 * Here it's not necessary to check the case:
8025 * the capacity of the device is bigger than
8026 * what the max hba cdb can support. Because
8027 * sd_send_scsi_READ_CAPACITY will retrieve
8028 * the capacity by sending USCSI command, which
8029 * is constrained by the max hba cdb. Actually,
8030 * sd_send_scsi_READ_CAPACITY will return
8031 * EINVAL when using bigger cdb than required
8032 * cdb length. Will handle this case in
8033 * "case EINVAL".
8034 */
8035
8036 /*
8037 * The following relies on
8038 * sd_send_scsi_READ_CAPACITY never
8039 * returning 0 for capacity and/or lbasize.
8040 */
8041 sd_update_block_info(un, lbasize, capacity);
8042
8043 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8044 "sd_unit_attach: un:0x%p capacity = %ld "
8045 "blocks; lbasize= %ld.\n", un,
8046 un->un_blockcount, un->un_tgt_blocksize);
8047
8048 break;
8049 }
8050 case EINVAL:
8051 /*
8052 * In the case where the max-cdb-length property
8053 * is smaller than the required CDB length for
8054 * a SCSI device, a target driver can fail to
8055 * attach to that device.
8056 */
8057 scsi_log(SD_DEVINFO(un),
8058 sd_label, CE_WARN,
8059 "disk capacity is too large "
8060 "for current cdb length");
8061 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8062
8063 goto spinup_failed;
8064 case EACCES:
8065 /*
8066 * Should never get here if the spin-up
8067 * succeeded, but code it in anyway.
8068 * From here, just continue with the attach...
8069 */
8070 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8071 "sd_unit_attach: un:0x%p "
8072 "sd_send_scsi_READ_CAPACITY "
8073 "returned reservation conflict\n", un);
8074 reservation_flag = SD_TARGET_IS_RESERVED;
8075 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8076 break;
8077 default:
8078 /*
8079 * Likewise, should never get here if the
8080 * spin-up succeeded. Just continue with
8081 * the attach...
8082 */
8083 if (status == EIO)
8084 sd_ssc_assessment(ssc,
8085 SD_FMT_STATUS_CHECK);
8086 else
8087 sd_ssc_assessment(ssc,
8088 SD_FMT_IGNORE);
8089 break;
8090 }
8091 break;
8092 case EACCES:
8093 /*
8094 * Device is reserved by another host. In this case
8095 * we could not spin it up or read the capacity, but
8096 * we continue with the attach anyway.
8097 */
8098 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8099 "sd_unit_attach: un:0x%p spin-up reservation "
8100 "conflict.\n", un);
8101 reservation_flag = SD_TARGET_IS_RESERVED;
8102 break;
8103 default:
8104 /* Fail the attach if the spin-up failed. */
8105 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8106 "sd_unit_attach: un:0x%p spin-up failed.", un);
8107 goto spinup_failed;
8108 }
8109
8110 }
8111
8112 /*
8113 * Check to see if this is a MMC drive
8114 */
8115 if (ISCD(un)) {
8116 sd_set_mmc_caps(ssc);
8117 }
8118
8119 /*
8120 * Add a zero-length attribute to tell the world we support
8121 * kernel ioctls (for layered drivers)
8122 */
8123 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8124 DDI_KERNEL_IOCTL, NULL, 0);
8125
8126 /*
8127 * Add a boolean property to tell the world we support
8128 * the B_FAILFAST flag (for layered drivers)
8129 */
8130 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8131 "ddi-failfast-supported", NULL, 0);
8132
8133 /*
8134 * Initialize power management
8135 */
8136 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8137 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8138 sd_setup_pm(ssc, devi);
8139 if (un->un_f_pm_is_enabled == FALSE) {
8140 /*
8141 * For performance, point to a jump table that does
8142 * not include pm.
8143 * The direct and priority chains don't change with PM.
8144 *
8145 * Note: this is currently done based on individual device
8146 * capabilities. When an interface for determining system
8147 * power enabled state becomes available, or when additional
8148 * layers are added to the command chain, these values will
8149 * have to be re-evaluated for correctness.
8150 */
8151 if (un->un_f_non_devbsize_supported) {
8152 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8153 } else {
8154 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8155 }
8156 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8157 }
8158
8159 /*
8160 * This property is set to 0 by HA software to avoid retries
8161 * on a reserved disk. (The preferred property name is
8162 * "retry-on-reservation-conflict") (1189689)
8163 *
8164 * Note: The use of a global here can have unintended consequences. A
8165 * per instance variable is preferable to match the capabilities of
8166 * different underlying hba's (4402600)
8167 */
8168 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8169 DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8170 sd_retry_on_reservation_conflict);
8171 if (sd_retry_on_reservation_conflict != 0) {
8172 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8173 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8174 sd_retry_on_reservation_conflict);
8175 }
8176
8177 /* Set up options for QFULL handling. */
8178 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8179 "qfull-retries", -1)) != -1) {
8180 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8181 rval, 1);
8182 }
8183 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8184 "qfull-retry-interval", -1)) != -1) {
8185 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8186 rval, 1);
8187 }
8188
8189 /*
8190 * This just prints a message that announces the existence of the
8191 * device. The message is always printed in the system logfile, but
8192 * only appears on the console if the system is booted with the
8193 * -v (verbose) argument.
8194 */
8195 ddi_report_dev(devi);
8196
8197 un->un_mediastate = DKIO_NONE;
8198
8199 /*
8200 * Check if this is a SSD(Solid State Drive).
8201 */
8202 sd_check_solid_state(ssc);
8203
8204 /*
8205 * Check whether the drive is in emulation mode.
8206 */
8207 sd_check_emulation_mode(ssc);
8208
8209 cmlb_alloc_handle(&un->un_cmlbhandle);
8210
8211 #if defined(__i386) || defined(__amd64)
8212 /*
8213 * On x86, compensate for off-by-1 legacy error
8214 */
8215 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8216 (lbasize == un->un_sys_blocksize))
8217 offbyone = CMLB_OFF_BY_ONE;
8218 #endif
8219
8220 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8221 VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8222 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8223 un->un_node_type, offbyone, un->un_cmlbhandle,
8224 (void *)SD_PATH_DIRECT) != 0) {
8225 goto cmlb_attach_failed;
8226 }
8227
8228
8229 /*
8230 * Read and validate the device's geometry (ie, disk label)
8231 * A new unformatted drive will not have a valid geometry, but
8232 * the driver needs to successfully attach to this device so
8233 * the drive can be formatted via ioctls.
8234 */
8235 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8236 (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8237
8238 mutex_enter(SD_MUTEX(un));
8239
8240 /*
8241 * Read and initialize the devid for the unit.
8242 */
8243 if (un->un_f_devid_supported) {
8244 sd_register_devid(ssc, devi, reservation_flag);
8245 }
8246 mutex_exit(SD_MUTEX(un));
8247
8248 #if (defined(__fibre))
8249 /*
8250 * Register callbacks for fibre only. You can't do this solely
8251 * on the basis of the devid_type because this is hba specific.
8252 * We need to query our hba capabilities to find out whether to
8253 * register or not.
8254 */
8255 if (un->un_f_is_fibre) {
8256 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8257 sd_init_event_callbacks(un);
8258 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8259 "sd_unit_attach: un:0x%p event callbacks inserted",
8260 un);
8261 }
8262 }
8263 #endif
8264
8265 if (un->un_f_opt_disable_cache == TRUE) {
8266 /*
8267 * Disable both read cache and write cache. This is
8268 * the historic behavior of the keywords in the config file.
8269 */
8270 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8271 0) {
8272 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8273 "sd_unit_attach: un:0x%p Could not disable "
8274 "caching", un);
8275 goto devid_failed;
8276 }
8277 }
8278
8279 /*
8280 * Check the value of the WCE bit now and
8281 * set un_f_write_cache_enabled accordingly.
8282 */
8283 (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8284 mutex_enter(SD_MUTEX(un));
8285 un->un_f_write_cache_enabled = (wc_enabled != 0);
8286 mutex_exit(SD_MUTEX(un));
8287
8288 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8289 un->un_tgt_blocksize != DEV_BSIZE) ||
8290 un->un_f_enable_rmw) {
8291 if (!(un->un_wm_cache)) {
8292 (void) snprintf(name_str, sizeof (name_str),
8293 "%s%d_cache",
8294 ddi_driver_name(SD_DEVINFO(un)),
8295 ddi_get_instance(SD_DEVINFO(un)));
8296 un->un_wm_cache = kmem_cache_create(
8297 name_str, sizeof (struct sd_w_map),
8298 8, sd_wm_cache_constructor,
8299 sd_wm_cache_destructor, NULL,
8300 (void *)un, NULL, 0);
8301 if (!(un->un_wm_cache)) {
8302 goto wm_cache_failed;
8303 }
8304 }
8305 }
8306
8307 /*
8308 * Check the value of the NV_SUP bit and set
8309 * un_f_suppress_cache_flush accordingly.
8310 */
8311 sd_get_nv_sup(ssc);
8312
8313 /*
8314 * Find out what type of reservation this disk supports.
8315 */
8316 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8317
8318 switch (status) {
8319 case 0:
8320 /*
8321 * SCSI-3 reservations are supported.
8322 */
8323 un->un_reservation_type = SD_SCSI3_RESERVATION;
8324 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8325 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8326 break;
8327 case ENOTSUP:
8328 /*
8329 * The PERSISTENT RESERVE IN command would not be recognized by
8330 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8331 */
8332 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8333 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8334 un->un_reservation_type = SD_SCSI2_RESERVATION;
8335
8336 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8337 break;
8338 default:
8339 /*
8340 * default to SCSI-3 reservations
8341 */
8342 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8343 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8344 un->un_reservation_type = SD_SCSI3_RESERVATION;
8345
8346 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8347 break;
8348 }
8349
8350 /*
8351 * Set the pstat and error stat values here, so data obtained during the
8352 * previous attach-time routines is available.
8353 *
8354 * Note: This is a critical sequence that needs to be maintained:
8355 * 1) Instantiate the kstats before any routines using the iopath
8356 * (i.e. sd_send_scsi_cmd).
8357 * 2) Initialize the error stats (sd_set_errstats) and partition
8358 * stats (sd_set_pstats)here, following
8359 * cmlb_validate_geometry(), sd_register_devid(), and
8360 * sd_cache_control().
8361 */
8362
8363 if (un->un_f_pkstats_enabled && geom_label_valid) {
8364 sd_set_pstats(un);
8365 SD_TRACE(SD_LOG_IO_PARTITION, un,
8366 "sd_unit_attach: un:0x%p pstats created and set\n", un);
8367 }
8368
8369 sd_set_errstats(un);
8370 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8371 "sd_unit_attach: un:0x%p errstats set\n", un);
8372
8373
8374 /*
8375 * After successfully attaching an instance, we record the information
8376 * of how many luns have been attached on the relative target and
8377 * controller for parallel SCSI. This information is used when sd tries
8378 * to set the tagged queuing capability in HBA.
8379 */
8380 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8381 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8382 }
8383
8384 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8385 "sd_unit_attach: un:0x%p exit success\n", un);
8386
8387 /* Uninitialize sd_ssc_t pointer */
8388 sd_ssc_fini(ssc);
8389
8390 return (DDI_SUCCESS);
8391
8392 /*
8393 * An error occurred during the attach; clean up & return failure.
8394 */
8395 wm_cache_failed:
8396 devid_failed:
8397
8398 setup_pm_failed:
8399 ddi_remove_minor_node(devi, NULL);
8400
8401 cmlb_attach_failed:
8402 /*
8403 * Cleanup from the scsi_ifsetcap() calls (437868)
8404 */
8405 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8406 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8407
8408 /*
8409 * Refer to the comments of setting tagged-qing in the beginning of
8410 * sd_unit_attach. We can only disable tagged queuing when there is
8411 * no lun attached on the target.
8412 */
8413 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8414 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8415 }
8416
8417 if (un->un_f_is_fibre == FALSE) {
8418 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8419 }
8420
8421 spinup_failed:
8422
8423 /* Uninitialize sd_ssc_t pointer */
8424 sd_ssc_fini(ssc);
8425
8426 mutex_enter(SD_MUTEX(un));
8427
8428 /* Deallocate SCSI FMA memory spaces */
8429 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8430
8431 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8432 if (un->un_direct_priority_timeid != NULL) {
8433 timeout_id_t temp_id = un->un_direct_priority_timeid;
8434 un->un_direct_priority_timeid = NULL;
8435 mutex_exit(SD_MUTEX(un));
8436 (void) untimeout(temp_id);
8437 mutex_enter(SD_MUTEX(un));
8438 }
8439
8440 /* Cancel any pending start/stop timeouts */
8441 if (un->un_startstop_timeid != NULL) {
8442 timeout_id_t temp_id = un->un_startstop_timeid;
8443 un->un_startstop_timeid = NULL;
8444 mutex_exit(SD_MUTEX(un));
8445 (void) untimeout(temp_id);
8446 mutex_enter(SD_MUTEX(un));
8447 }
8448
8449 /* Cancel any pending reset-throttle timeouts */
8450 if (un->un_reset_throttle_timeid != NULL) {
8451 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8452 un->un_reset_throttle_timeid = NULL;
8453 mutex_exit(SD_MUTEX(un));
8454 (void) untimeout(temp_id);
8455 mutex_enter(SD_MUTEX(un));
8456 }
8457
8458 /* Cancel rmw warning message timeouts */
8459 if (un->un_rmw_msg_timeid != NULL) {
8460 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8461 un->un_rmw_msg_timeid = NULL;
8462 mutex_exit(SD_MUTEX(un));
8463 (void) untimeout(temp_id);
8464 mutex_enter(SD_MUTEX(un));
8465 }
8466
8467 /* Cancel any pending retry timeouts */
8468 if (un->un_retry_timeid != NULL) {
8469 timeout_id_t temp_id = un->un_retry_timeid;
8470 un->un_retry_timeid = NULL;
8471 mutex_exit(SD_MUTEX(un));
8472 (void) untimeout(temp_id);
8473 mutex_enter(SD_MUTEX(un));
8474 }
8475
8476 /* Cancel any pending delayed cv broadcast timeouts */
8477 if (un->un_dcvb_timeid != NULL) {
8478 timeout_id_t temp_id = un->un_dcvb_timeid;
8479 un->un_dcvb_timeid = NULL;
8480 mutex_exit(SD_MUTEX(un));
8481 (void) untimeout(temp_id);
8482 mutex_enter(SD_MUTEX(un));
8483 }
8484
8485 mutex_exit(SD_MUTEX(un));
8486
8487 /* There should not be any in-progress I/O so ASSERT this check */
8488 ASSERT(un->un_ncmds_in_transport == 0);
8489 ASSERT(un->un_ncmds_in_driver == 0);
8490
8491 /* Do not free the softstate if the callback routine is active */
8492 sd_sync_with_callback(un);
8493
8494 /*
8495 * Partition stats apparently are not used with removables. These would
8496 * not have been created during attach, so no need to clean them up...
8497 */
8498 if (un->un_errstats != NULL) {
8499 kstat_delete(un->un_errstats);
8500 un->un_errstats = NULL;
8501 }
8502
8503 create_errstats_failed:
8504
8505 if (un->un_stats != NULL) {
8506 kstat_delete(un->un_stats);
8507 un->un_stats = NULL;
8508 }
8509
8510 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8511 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8512
8513 ddi_prop_remove_all(devi);
8514 sema_destroy(&un->un_semoclose);
8515 cv_destroy(&un->un_state_cv);
8516
8517 getrbuf_failed:
8518
8519 sd_free_rqs(un);
8520
8521 alloc_rqs_failed:
8522
8523 devp->sd_private = NULL;
8524 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
8525
8526 get_softstate_failed:
8527 /*
8528 * Note: the man pages are unclear as to whether or not doing a
8529 * ddi_soft_state_free(sd_state, instance) is the right way to
8530 * clean up after the ddi_soft_state_zalloc() if the subsequent
8531 * ddi_get_soft_state() fails. The implication seems to be
8532 * that the get_soft_state cannot fail if the zalloc succeeds.
8533 */
8534 #ifndef XPV_HVM_DRIVER
8535 ddi_soft_state_free(sd_state, instance);
8536 #endif /* !XPV_HVM_DRIVER */
8537
8538 probe_failed:
8539 scsi_unprobe(devp);
8540
8541 return (DDI_FAILURE);
8542 }
8543
8544
8545 /*
8546 * Function: sd_unit_detach
8547 *
8548 * Description: Performs DDI_DETACH processing for sddetach().
8549 *
8550 * Return Code: DDI_SUCCESS
8551 * DDI_FAILURE
8552 *
8553 * Context: Kernel thread context
8554 */
8555
8556 static int
8557 sd_unit_detach(dev_info_t *devi)
8558 {
8559 struct scsi_device *devp;
8560 struct sd_lun *un;
8561 int i;
8562 int tgt;
8563 dev_t dev;
8564 dev_info_t *pdip = ddi_get_parent(devi);
8565 #ifndef XPV_HVM_DRIVER
8566 int instance = ddi_get_instance(devi);
8567 #endif /* !XPV_HVM_DRIVER */
8568
8569 mutex_enter(&sd_detach_mutex);
8570
8571 /*
8572 * Fail the detach for any of the following:
8573 * - Unable to get the sd_lun struct for the instance
8574 * - A layered driver has an outstanding open on the instance
8575 * - Another thread is already detaching this instance
8576 * - Another thread is currently performing an open
8577 */
8578 devp = ddi_get_driver_private(devi);
8579 if ((devp == NULL) ||
8580 ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8581 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8582 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8583 mutex_exit(&sd_detach_mutex);
8584 return (DDI_FAILURE);
8585 }
8586
8587 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8588
8589 /*
8590 * Mark this instance as currently in a detach, to inhibit any
8591 * opens from a layered driver.
8592 */
8593 un->un_detach_count++;
8594 mutex_exit(&sd_detach_mutex);
8595
8596 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8597 SCSI_ADDR_PROP_TARGET, -1);
8598
8599 dev = sd_make_device(SD_DEVINFO(un));
8600
8601 #ifndef lint
8602 _NOTE(COMPETING_THREADS_NOW);
8603 #endif
8604
8605 mutex_enter(SD_MUTEX(un));
8606
8607 /*
8608 * Fail the detach if there are any outstanding layered
8609 * opens on this device.
8610 */
8611 for (i = 0; i < NDKMAP; i++) {
8612 if (un->un_ocmap.lyropen[i] != 0) {
8613 goto err_notclosed;
8614 }
8615 }
8616
8617 /*
8618 * Verify there are NO outstanding commands issued to this device.
8619 * ie, un_ncmds_in_transport == 0.
8620 * It's possible to have outstanding commands through the physio
8621 * code path, even though everything's closed.
8622 */
8623 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8624 (un->un_direct_priority_timeid != NULL) ||
8625 (un->un_state == SD_STATE_RWAIT)) {
8626 mutex_exit(SD_MUTEX(un));
8627 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8628 "sd_dr_detach: Detach failure due to outstanding cmds\n");
8629 goto err_stillbusy;
8630 }
8631
8632 /*
8633 * If we have the device reserved, release the reservation.
8634 */
8635 if ((un->un_resvd_status & SD_RESERVE) &&
8636 !(un->un_resvd_status & SD_LOST_RESERVE)) {
8637 mutex_exit(SD_MUTEX(un));
8638 /*
8639 * Note: sd_reserve_release sends a command to the device
8640 * via the sd_ioctlcmd() path, and can sleep.
8641 */
8642 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8643 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8644 "sd_dr_detach: Cannot release reservation \n");
8645 }
8646 } else {
8647 mutex_exit(SD_MUTEX(un));
8648 }
8649
8650 /*
8651 * Untimeout any reserve recover, throttle reset, restart unit
8652 * and delayed broadcast timeout threads. Protect the timeout pointer
8653 * from getting nulled by their callback functions.
8654 */
8655 mutex_enter(SD_MUTEX(un));
8656 if (un->un_resvd_timeid != NULL) {
8657 timeout_id_t temp_id = un->un_resvd_timeid;
8658 un->un_resvd_timeid = NULL;
8659 mutex_exit(SD_MUTEX(un));
8660 (void) untimeout(temp_id);
8661 mutex_enter(SD_MUTEX(un));
8662 }
8663
8664 if (un->un_reset_throttle_timeid != NULL) {
8665 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8666 un->un_reset_throttle_timeid = NULL;
8667 mutex_exit(SD_MUTEX(un));
8668 (void) untimeout(temp_id);
8669 mutex_enter(SD_MUTEX(un));
8670 }
8671
8672 if (un->un_startstop_timeid != NULL) {
8673 timeout_id_t temp_id = un->un_startstop_timeid;
8674 un->un_startstop_timeid = NULL;
8675 mutex_exit(SD_MUTEX(un));
8676 (void) untimeout(temp_id);
8677 mutex_enter(SD_MUTEX(un));
8678 }
8679
8680 if (un->un_rmw_msg_timeid != NULL) {
8681 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8682 un->un_rmw_msg_timeid = NULL;
8683 mutex_exit(SD_MUTEX(un));
8684 (void) untimeout(temp_id);
8685 mutex_enter(SD_MUTEX(un));
8686 }
8687
8688 if (un->un_dcvb_timeid != NULL) {
8689 timeout_id_t temp_id = un->un_dcvb_timeid;
8690 un->un_dcvb_timeid = NULL;
8691 mutex_exit(SD_MUTEX(un));
8692 (void) untimeout(temp_id);
8693 } else {
8694 mutex_exit(SD_MUTEX(un));
8695 }
8696
8697 /* Remove any pending reservation reclaim requests for this device */
8698 sd_rmv_resv_reclaim_req(dev);
8699
8700 mutex_enter(SD_MUTEX(un));
8701
8702 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8703 if (un->un_direct_priority_timeid != NULL) {
8704 timeout_id_t temp_id = un->un_direct_priority_timeid;
8705 un->un_direct_priority_timeid = NULL;
8706 mutex_exit(SD_MUTEX(un));
8707 (void) untimeout(temp_id);
8708 mutex_enter(SD_MUTEX(un));
8709 }
8710
8711 /* Cancel any active multi-host disk watch thread requests */
8712 if (un->un_mhd_token != NULL) {
8713 mutex_exit(SD_MUTEX(un));
8714 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8715 if (scsi_watch_request_terminate(un->un_mhd_token,
8716 SCSI_WATCH_TERMINATE_NOWAIT)) {
8717 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8718 "sd_dr_detach: Cannot cancel mhd watch request\n");
8719 /*
8720 * Note: We are returning here after having removed
8721 * some driver timeouts above. This is consistent with
8722 * the legacy implementation but perhaps the watch
8723 * terminate call should be made with the wait flag set.
8724 */
8725 goto err_stillbusy;
8726 }
8727 mutex_enter(SD_MUTEX(un));
8728 un->un_mhd_token = NULL;
8729 }
8730
8731 if (un->un_swr_token != NULL) {
8732 mutex_exit(SD_MUTEX(un));
8733 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8734 if (scsi_watch_request_terminate(un->un_swr_token,
8735 SCSI_WATCH_TERMINATE_NOWAIT)) {
8736 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8737 "sd_dr_detach: Cannot cancel swr watch request\n");
8738 /*
8739 * Note: We are returning here after having removed
8740 * some driver timeouts above. This is consistent with
8741 * the legacy implementation but perhaps the watch
8742 * terminate call should be made with the wait flag set.
8743 */
8744 goto err_stillbusy;
8745 }
8746 mutex_enter(SD_MUTEX(un));
8747 un->un_swr_token = NULL;
8748 }
8749
8750 mutex_exit(SD_MUTEX(un));
8751
8752 /*
8753 * Clear any scsi_reset_notifies. We clear the reset notifies
8754 * if we have not registered one.
8755 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8756 */
8757 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8758 sd_mhd_reset_notify_cb, (caddr_t)un);
8759
8760 /*
8761 * protect the timeout pointers from getting nulled by
8762 * their callback functions during the cancellation process.
8763 * In such a scenario untimeout can be invoked with a null value.
8764 */
8765 _NOTE(NO_COMPETING_THREADS_NOW);
8766
8767 mutex_enter(&un->un_pm_mutex);
8768 if (un->un_pm_idle_timeid != NULL) {
8769 timeout_id_t temp_id = un->un_pm_idle_timeid;
8770 un->un_pm_idle_timeid = NULL;
8771 mutex_exit(&un->un_pm_mutex);
8772
8773 /*
8774 * Timeout is active; cancel it.
8775 * Note that it'll never be active on a device
8776 * that does not support PM therefore we don't
8777 * have to check before calling pm_idle_component.
8778 */
8779 (void) untimeout(temp_id);
8780 (void) pm_idle_component(SD_DEVINFO(un), 0);
8781 mutex_enter(&un->un_pm_mutex);
8782 }
8783
8784 /*
8785 * Check whether there is already a timeout scheduled for power
8786 * management. If yes then don't lower the power here, that's.
8787 * the timeout handler's job.
8788 */
8789 if (un->un_pm_timeid != NULL) {
8790 timeout_id_t temp_id = un->un_pm_timeid;
8791 un->un_pm_timeid = NULL;
8792 mutex_exit(&un->un_pm_mutex);
8793 /*
8794 * Timeout is active; cancel it.
8795 * Note that it'll never be active on a device
8796 * that does not support PM therefore we don't
8797 * have to check before calling pm_idle_component.
8798 */
8799 (void) untimeout(temp_id);
8800 (void) pm_idle_component(SD_DEVINFO(un), 0);
8801
8802 } else {
8803 mutex_exit(&un->un_pm_mutex);
8804 if ((un->un_f_pm_is_enabled == TRUE) &&
8805 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8806 != DDI_SUCCESS)) {
8807 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8808 "sd_dr_detach: Lower power request failed, ignoring.\n");
8809 /*
8810 * Fix for bug: 4297749, item # 13
8811 * The above test now includes a check to see if PM is
8812 * supported by this device before call
8813 * pm_lower_power().
8814 * Note, the following is not dead code. The call to
8815 * pm_lower_power above will generate a call back into
8816 * our sdpower routine which might result in a timeout
8817 * handler getting activated. Therefore the following
8818 * code is valid and necessary.
8819 */
8820 mutex_enter(&un->un_pm_mutex);
8821 if (un->un_pm_timeid != NULL) {
8822 timeout_id_t temp_id = un->un_pm_timeid;
8823 un->un_pm_timeid = NULL;
8824 mutex_exit(&un->un_pm_mutex);
8825 (void) untimeout(temp_id);
8826 (void) pm_idle_component(SD_DEVINFO(un), 0);
8827 } else {
8828 mutex_exit(&un->un_pm_mutex);
8829 }
8830 }
8831 }
8832
8833 /*
8834 * Cleanup from the scsi_ifsetcap() calls (437868)
8835 * Relocated here from above to be after the call to
8836 * pm_lower_power, which was getting errors.
8837 */
8838 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8839 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8840
8841 /*
8842 * Currently, tagged queuing is supported per target based by HBA.
8843 * Setting this per lun instance actually sets the capability of this
8844 * target in HBA, which affects those luns already attached on the
8845 * same target. So during detach, we can only disable this capability
8846 * only when this is the only lun left on this target. By doing
8847 * this, we assume a target has the same tagged queuing capability
8848 * for every lun. The condition can be removed when HBA is changed to
8849 * support per lun based tagged queuing capability.
8850 */
8851 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8852 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8853 }
8854
8855 if (un->un_f_is_fibre == FALSE) {
8856 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8857 }
8858
8859 /*
8860 * Remove any event callbacks, fibre only
8861 */
8862 if (un->un_f_is_fibre == TRUE) {
8863 if ((un->un_insert_event != NULL) &&
8864 (ddi_remove_event_handler(un->un_insert_cb_id) !=
8865 DDI_SUCCESS)) {
8866 /*
8867 * Note: We are returning here after having done
8868 * substantial cleanup above. This is consistent
8869 * with the legacy implementation but this may not
8870 * be the right thing to do.
8871 */
8872 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8873 "sd_dr_detach: Cannot cancel insert event\n");
8874 goto err_remove_event;
8875 }
8876 un->un_insert_event = NULL;
8877
8878 if ((un->un_remove_event != NULL) &&
8879 (ddi_remove_event_handler(un->un_remove_cb_id) !=
8880 DDI_SUCCESS)) {
8881 /*
8882 * Note: We are returning here after having done
8883 * substantial cleanup above. This is consistent
8884 * with the legacy implementation but this may not
8885 * be the right thing to do.
8886 */
8887 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8888 "sd_dr_detach: Cannot cancel remove event\n");
8889 goto err_remove_event;
8890 }
8891 un->un_remove_event = NULL;
8892 }
8893
8894 /* Do not free the softstate if the callback routine is active */
8895 sd_sync_with_callback(un);
8896
8897 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8898 cmlb_free_handle(&un->un_cmlbhandle);
8899
8900 /*
8901 * Hold the detach mutex here, to make sure that no other threads ever
8902 * can access a (partially) freed soft state structure.
8903 */
8904 mutex_enter(&sd_detach_mutex);
8905
8906 /*
8907 * Clean up the soft state struct.
8908 * Cleanup is done in reverse order of allocs/inits.
8909 * At this point there should be no competing threads anymore.
8910 */
8911
8912 scsi_fm_fini(devp);
8913
8914 /*
8915 * Deallocate memory for SCSI FMA.
8916 */
8917 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8918
8919 /*
8920 * Unregister and free device id if it was not registered
8921 * by the transport.
8922 */
8923 if (un->un_f_devid_transport_defined == FALSE)
8924 ddi_devid_unregister(devi);
8925
8926 /*
8927 * free the devid structure if allocated before (by ddi_devid_init()
8928 * or ddi_devid_get()).
8929 */
8930 if (un->un_devid) {
8931 ddi_devid_free(un->un_devid);
8932 un->un_devid = NULL;
8933 }
8934
8935 /*
8936 * Destroy wmap cache if it exists.
8937 */
8938 if (un->un_wm_cache != NULL) {
8939 kmem_cache_destroy(un->un_wm_cache);
8940 un->un_wm_cache = NULL;
8941 }
8942
8943 /*
8944 * kstat cleanup is done in detach for all device types (4363169).
8945 * We do not want to fail detach if the device kstats are not deleted
8946 * since there is a confusion about the devo_refcnt for the device.
8947 * We just delete the kstats and let detach complete successfully.
8948 */
8949 if (un->un_stats != NULL) {
8950 kstat_delete(un->un_stats);
8951 un->un_stats = NULL;
8952 }
8953 if (un->un_errstats != NULL) {
8954 kstat_delete(un->un_errstats);
8955 un->un_errstats = NULL;
8956 }
8957
8958 /* Remove partition stats */
8959 if (un->un_f_pkstats_enabled) {
8960 for (i = 0; i < NSDMAP; i++) {
8961 if (un->un_pstats[i] != NULL) {
8962 kstat_delete(un->un_pstats[i]);
8963 un->un_pstats[i] = NULL;
8964 }
8965 }
8966 }
8967
8968 /* Remove xbuf registration */
8969 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8970 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8971
8972 /* Remove driver properties */
8973 ddi_prop_remove_all(devi);
8974
8975 mutex_destroy(&un->un_pm_mutex);
8976 cv_destroy(&un->un_pm_busy_cv);
8977
8978 cv_destroy(&un->un_wcc_cv);
8979
8980 /* Open/close semaphore */
8981 sema_destroy(&un->un_semoclose);
8982
8983 /* Removable media condvar. */
8984 cv_destroy(&un->un_state_cv);
8985
8986 /* Suspend/resume condvar. */
8987 cv_destroy(&un->un_suspend_cv);
8988 cv_destroy(&un->un_disk_busy_cv);
8989
8990 sd_free_rqs(un);
8991
8992 /* Free up soft state */
8993 devp->sd_private = NULL;
8994
8995 bzero(un, sizeof (struct sd_lun));
8996 #ifndef XPV_HVM_DRIVER
8997 ddi_soft_state_free(sd_state, instance);
8998 #endif /* !XPV_HVM_DRIVER */
8999
9000 mutex_exit(&sd_detach_mutex);
9001
9002 /* This frees up the INQUIRY data associated with the device. */
9003 scsi_unprobe(devp);
9004
9005 /*
9006 * After successfully detaching an instance, we update the information
9007 * of how many luns have been attached in the relative target and
9008 * controller for parallel SCSI. This information is used when sd tries
9009 * to set the tagged queuing capability in HBA.
9010 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9011 * check if the device is parallel SCSI. However, we don't need to
9012 * check here because we've already checked during attach. No device
9013 * that is not parallel SCSI is in the chain.
9014 */
9015 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9016 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9017 }
9018
9019 return (DDI_SUCCESS);
9020
9021 err_notclosed:
9022 mutex_exit(SD_MUTEX(un));
9023
9024 err_stillbusy:
9025 _NOTE(NO_COMPETING_THREADS_NOW);
9026
9027 err_remove_event:
9028 mutex_enter(&sd_detach_mutex);
9029 un->un_detach_count--;
9030 mutex_exit(&sd_detach_mutex);
9031
9032 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9033 return (DDI_FAILURE);
9034 }
9035
9036
9037 /*
9038 * Function: sd_create_errstats
9039 *
9040 * Description: This routine instantiates the device error stats.
9041 *
9042 * Note: During attach the stats are instantiated first so they are
9043 * available for attach-time routines that utilize the driver
9044 * iopath to send commands to the device. The stats are initialized
9045 * separately so data obtained during some attach-time routines is
9046 * available. (4362483)
9047 *
9048 * Arguments: un - driver soft state (unit) structure
9049 * instance - driver instance
9050 *
9051 * Context: Kernel thread context
9052 */
9053
9054 static void
9055 sd_create_errstats(struct sd_lun *un, int instance)
9056 {
9057 struct sd_errstats *stp;
9058 char kstatmodule_err[KSTAT_STRLEN];
9059 char kstatname[KSTAT_STRLEN];
9060 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9061
9062 ASSERT(un != NULL);
9063
9064 if (un->un_errstats != NULL) {
9065 return;
9066 }
9067
9068 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9069 "%serr", sd_label);
9070 (void) snprintf(kstatname, sizeof (kstatname),
9071 "%s%d,err", sd_label, instance);
9072
9073 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9074 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9075
9076 if (un->un_errstats == NULL) {
9077 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9078 "sd_create_errstats: Failed kstat_create\n");
9079 return;
9080 }
9081
9082 stp = (struct sd_errstats *)un->un_errstats->ks_data;
9083 kstat_named_init(&stp->sd_softerrs, "Soft Errors",
9084 KSTAT_DATA_UINT32);
9085 kstat_named_init(&stp->sd_harderrs, "Hard Errors",
9086 KSTAT_DATA_UINT32);
9087 kstat_named_init(&stp->sd_transerrs, "Transport Errors",
9088 KSTAT_DATA_UINT32);
9089 kstat_named_init(&stp->sd_vid, "Vendor",
9090 KSTAT_DATA_CHAR);
9091 kstat_named_init(&stp->sd_pid, "Product",
9092 KSTAT_DATA_CHAR);
9093 kstat_named_init(&stp->sd_revision, "Revision",
9094 KSTAT_DATA_CHAR);
9095 kstat_named_init(&stp->sd_serial, "Serial No",
9096 KSTAT_DATA_CHAR);
9097 kstat_named_init(&stp->sd_capacity, "Size",
9098 KSTAT_DATA_ULONGLONG);
9099 kstat_named_init(&stp->sd_rq_media_err, "Media Error",
9100 KSTAT_DATA_UINT32);
9101 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready",
9102 KSTAT_DATA_UINT32);
9103 kstat_named_init(&stp->sd_rq_nodev_err, "No Device",
9104 KSTAT_DATA_UINT32);
9105 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable",
9106 KSTAT_DATA_UINT32);
9107 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request",
9108 KSTAT_DATA_UINT32);
9109 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis",
9110 KSTAT_DATA_UINT32);
9111
9112 un->un_errstats->ks_private = un;
9113 un->un_errstats->ks_update = nulldev;
9114
9115 kstat_install(un->un_errstats);
9116 }
9117
9118
9119 /*
9120 * Function: sd_set_errstats
9121 *
9122 * Description: This routine sets the value of the vendor id, product id,
9123 * revision, serial number, and capacity device error stats.
9124 *
9125 * Note: During attach the stats are instantiated first so they are
9126 * available for attach-time routines that utilize the driver
9127 * iopath to send commands to the device. The stats are initialized
9128 * separately so data obtained during some attach-time routines is
9129 * available. (4362483)
9130 *
9131 * Arguments: un - driver soft state (unit) structure
9132 *
9133 * Context: Kernel thread context
9134 */
9135
9136 static void
9137 sd_set_errstats(struct sd_lun *un)
9138 {
9139 struct sd_errstats *stp;
9140 char *sn;
9141
9142 ASSERT(un != NULL);
9143 ASSERT(un->un_errstats != NULL);
9144 stp = (struct sd_errstats *)un->un_errstats->ks_data;
9145 ASSERT(stp != NULL);
9146 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9147 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9148 (void) strncpy(stp->sd_revision.value.c,
9149 un->un_sd->sd_inq->inq_revision, 4);
9150
9151 /*
9152 * All the errstats are persistent across detach/attach,
9153 * so reset all the errstats here in case of the hot
9154 * replacement of disk drives, except for not changed
9155 * Sun qualified drives.
9156 */
9157 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9158 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9159 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9160 stp->sd_softerrs.value.ui32 = 0;
9161 stp->sd_harderrs.value.ui32 = 0;
9162 stp->sd_transerrs.value.ui32 = 0;
9163 stp->sd_rq_media_err.value.ui32 = 0;
9164 stp->sd_rq_ntrdy_err.value.ui32 = 0;
9165 stp->sd_rq_nodev_err.value.ui32 = 0;
9166 stp->sd_rq_recov_err.value.ui32 = 0;
9167 stp->sd_rq_illrq_err.value.ui32 = 0;
9168 stp->sd_rq_pfa_err.value.ui32 = 0;
9169 }
9170
9171 /*
9172 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9173 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9174 * (4376302))
9175 */
9176 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9177 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9178 sizeof (SD_INQUIRY(un)->inq_serial));
9179 } else {
9180 /*
9181 * Set the "Serial No" kstat for non-Sun qualified drives
9182 */
9183 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9184 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9185 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9186 (void) strlcpy(stp->sd_serial.value.c, sn,
9187 sizeof (stp->sd_serial.value.c));
9188 ddi_prop_free(sn);
9189 }
9190 }
9191
9192 if (un->un_f_blockcount_is_valid != TRUE) {
9193 /*
9194 * Set capacity error stat to 0 for no media. This ensures
9195 * a valid capacity is displayed in response to 'iostat -E'
9196 * when no media is present in the device.
9197 */
9198 stp->sd_capacity.value.ui64 = 0;
9199 } else {
9200 /*
9201 * Multiply un_blockcount by un->un_sys_blocksize to get
9202 * capacity.
9203 *
9204 * Note: for non-512 blocksize devices "un_blockcount" has been
9205 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9206 * (un_tgt_blocksize / un->un_sys_blocksize).
9207 */
9208 stp->sd_capacity.value.ui64 = (uint64_t)
9209 ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9210 }
9211 }
9212
9213
9214 /*
9215 * Function: sd_set_pstats
9216 *
9217 * Description: This routine instantiates and initializes the partition
9218 * stats for each partition with more than zero blocks.
9219 * (4363169)
9220 *
9221 * Arguments: un - driver soft state (unit) structure
9222 *
9223 * Context: Kernel thread context
9224 */
9225
9226 static void
9227 sd_set_pstats(struct sd_lun *un)
9228 {
9229 char kstatname[KSTAT_STRLEN];
9230 int instance;
9231 int i;
9232 diskaddr_t nblks = 0;
9233 char *partname = NULL;
9234
9235 ASSERT(un != NULL);
9236
9237 instance = ddi_get_instance(SD_DEVINFO(un));
9238
9239 /* Note:x86: is this a VTOC8/VTOC16 difference? */
9240 for (i = 0; i < NSDMAP; i++) {
9241
9242 if (cmlb_partinfo(un->un_cmlbhandle, i,
9243 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9244 continue;
9245 mutex_enter(SD_MUTEX(un));
9246
9247 if ((un->un_pstats[i] == NULL) &&
9248 (nblks != 0)) {
9249
9250 (void) snprintf(kstatname, sizeof (kstatname),
9251 "%s%d,%s", sd_label, instance,
9252 partname);
9253
9254 un->un_pstats[i] = kstat_create(sd_label,
9255 instance, kstatname, "partition", KSTAT_TYPE_IO,
9256 1, KSTAT_FLAG_PERSISTENT);
9257 if (un->un_pstats[i] != NULL) {
9258 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9259 kstat_install(un->un_pstats[i]);
9260 }
9261 }
9262 mutex_exit(SD_MUTEX(un));
9263 }
9264 }
9265
9266
9267 #if (defined(__fibre))
9268 /*
9269 * Function: sd_init_event_callbacks
9270 *
9271 * Description: This routine initializes the insertion and removal event
9272 * callbacks. (fibre only)
9273 *
9274 * Arguments: un - driver soft state (unit) structure
9275 *
9276 * Context: Kernel thread context
9277 */
9278
9279 static void
9280 sd_init_event_callbacks(struct sd_lun *un)
9281 {
9282 ASSERT(un != NULL);
9283
9284 if ((un->un_insert_event == NULL) &&
9285 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9286 &un->un_insert_event) == DDI_SUCCESS)) {
9287 /*
9288 * Add the callback for an insertion event
9289 */
9290 (void) ddi_add_event_handler(SD_DEVINFO(un),
9291 un->un_insert_event, sd_event_callback, (void *)un,
9292 &(un->un_insert_cb_id));
9293 }
9294
9295 if ((un->un_remove_event == NULL) &&
9296 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9297 &un->un_remove_event) == DDI_SUCCESS)) {
9298 /*
9299 * Add the callback for a removal event
9300 */
9301 (void) ddi_add_event_handler(SD_DEVINFO(un),
9302 un->un_remove_event, sd_event_callback, (void *)un,
9303 &(un->un_remove_cb_id));
9304 }
9305 }
9306
9307
9308 /*
9309 * Function: sd_event_callback
9310 *
9311 * Description: This routine handles insert/remove events (photon). The
9312 * state is changed to OFFLINE which can be used to supress
9313 * error msgs. (fibre only)
9314 *
9315 * Arguments: un - driver soft state (unit) structure
9316 *
9317 * Context: Callout thread context
9318 */
9319 /* ARGSUSED */
9320 static void
9321 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9322 void *bus_impldata)
9323 {
9324 struct sd_lun *un = (struct sd_lun *)arg;
9325
9326 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9327 if (event == un->un_insert_event) {
9328 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9329 mutex_enter(SD_MUTEX(un));
9330 if (un->un_state == SD_STATE_OFFLINE) {
9331 if (un->un_last_state != SD_STATE_SUSPENDED) {
9332 un->un_state = un->un_last_state;
9333 } else {
9334 /*
9335 * We have gone through SUSPEND/RESUME while
9336 * we were offline. Restore the last state
9337 */
9338 un->un_state = un->un_save_state;
9339 }
9340 }
9341 mutex_exit(SD_MUTEX(un));
9342
9343 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9344 } else if (event == un->un_remove_event) {
9345 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9346 mutex_enter(SD_MUTEX(un));
9347 /*
9348 * We need to handle an event callback that occurs during
9349 * the suspend operation, since we don't prevent it.
9350 */
9351 if (un->un_state != SD_STATE_OFFLINE) {
9352 if (un->un_state != SD_STATE_SUSPENDED) {
9353 New_state(un, SD_STATE_OFFLINE);
9354 } else {
9355 un->un_last_state = SD_STATE_OFFLINE;
9356 }
9357 }
9358 mutex_exit(SD_MUTEX(un));
9359 } else {
9360 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9361 "!Unknown event\n");
9362 }
9363
9364 }
9365 #endif
9366
9367 /*
9368 * Function: sd_cache_control()
9369 *
9370 * Description: This routine is the driver entry point for setting
9371 * read and write caching by modifying the WCE (write cache
9372 * enable) and RCD (read cache disable) bits of mode
9373 * page 8 (MODEPAGE_CACHING).
9374 *
9375 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
9376 * structure for this target.
9377 * rcd_flag - flag for controlling the read cache
9378 * wce_flag - flag for controlling the write cache
9379 *
9380 * Return Code: EIO
9381 * code returned by sd_send_scsi_MODE_SENSE and
9382 * sd_send_scsi_MODE_SELECT
9383 *
9384 * Context: Kernel Thread
9385 */
9386
9387 static int
9388 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9389 {
9390 struct mode_caching *mode_caching_page;
9391 uchar_t *header;
9392 size_t buflen;
9393 int hdrlen;
9394 int bd_len;
9395 int rval = 0;
9396 struct mode_header_grp2 *mhp;
9397 struct sd_lun *un;
9398 int status;
9399
9400 ASSERT(ssc != NULL);
9401 un = ssc->ssc_un;
9402 ASSERT(un != NULL);
9403
9404 /*
9405 * Do a test unit ready, otherwise a mode sense may not work if this
9406 * is the first command sent to the device after boot.
9407 */
9408 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9409 if (status != 0)
9410 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9411
9412 if (un->un_f_cfg_is_atapi == TRUE) {
9413 hdrlen = MODE_HEADER_LENGTH_GRP2;
9414 } else {
9415 hdrlen = MODE_HEADER_LENGTH;
9416 }
9417
9418 /*
9419 * Allocate memory for the retrieved mode page and its headers. Set
9420 * a pointer to the page itself. Use mode_cache_scsi3 to insure
9421 * we get all of the mode sense data otherwise, the mode select
9422 * will fail. mode_cache_scsi3 is a superset of mode_caching.
9423 */
9424 buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9425 sizeof (struct mode_cache_scsi3);
9426
9427 header = kmem_zalloc(buflen, KM_SLEEP);
9428
9429 /* Get the information from the device. */
9430 if (un->un_f_cfg_is_atapi == TRUE) {
9431 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9432 MODEPAGE_CACHING, SD_PATH_DIRECT);
9433 } else {
9434 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9435 MODEPAGE_CACHING, SD_PATH_DIRECT);
9436 }
9437
9438 if (rval != 0) {
9439 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9440 "sd_cache_control: Mode Sense Failed\n");
9441 goto mode_sense_failed;
9442 }
9443
9444 /*
9445 * Determine size of Block Descriptors in order to locate
9446 * the mode page data. ATAPI devices return 0, SCSI devices
9447 * should return MODE_BLK_DESC_LENGTH.
9448 */
9449 if (un->un_f_cfg_is_atapi == TRUE) {
9450 mhp = (struct mode_header_grp2 *)header;
9451 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9452 } else {
9453 bd_len = ((struct mode_header *)header)->bdesc_length;
9454 }
9455
9456 if (bd_len > MODE_BLK_DESC_LENGTH) {
9457 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9458 "sd_cache_control: Mode Sense returned invalid block "
9459 "descriptor length\n");
9460 rval = EIO;
9461 goto mode_sense_failed;
9462 }
9463
9464 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9465 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9466 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9467 "sd_cache_control: Mode Sense caching page code mismatch "
9468 "%d\n", mode_caching_page->mode_page.code);
9469 rval = EIO;
9470 goto mode_sense_failed;
9471 }
9472
9473 /* Check the relevant bits on successful mode sense. */
9474 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9475 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9476 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9477 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9478
9479 size_t sbuflen;
9480 uchar_t save_pg;
9481
9482 /*
9483 * Construct select buffer length based on the
9484 * length of the sense data returned.
9485 */
9486 sbuflen = hdrlen + bd_len +
9487 sizeof (struct mode_page) +
9488 (int)mode_caching_page->mode_page.length;
9489
9490 /*
9491 * Set the caching bits as requested.
9492 */
9493 if (rcd_flag == SD_CACHE_ENABLE)
9494 mode_caching_page->rcd = 0;
9495 else if (rcd_flag == SD_CACHE_DISABLE)
9496 mode_caching_page->rcd = 1;
9497
9498 if (wce_flag == SD_CACHE_ENABLE)
9499 mode_caching_page->wce = 1;
9500 else if (wce_flag == SD_CACHE_DISABLE)
9501 mode_caching_page->wce = 0;
9502
9503 /*
9504 * Save the page if the mode sense says the
9505 * drive supports it.
9506 */
9507 save_pg = mode_caching_page->mode_page.ps ?
9508 SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9509
9510 /* Clear reserved bits before mode select. */
9511 mode_caching_page->mode_page.ps = 0;
9512
9513 /*
9514 * Clear out mode header for mode select.
9515 * The rest of the retrieved page will be reused.
9516 */
9517 bzero(header, hdrlen);
9518
9519 if (un->un_f_cfg_is_atapi == TRUE) {
9520 mhp = (struct mode_header_grp2 *)header;
9521 mhp->bdesc_length_hi = bd_len >> 8;
9522 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9523 } else {
9524 ((struct mode_header *)header)->bdesc_length = bd_len;
9525 }
9526
9527 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9528
9529 /* Issue mode select to change the cache settings */
9530 if (un->un_f_cfg_is_atapi == TRUE) {
9531 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header,
9532 sbuflen, save_pg, SD_PATH_DIRECT);
9533 } else {
9534 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
9535 sbuflen, save_pg, SD_PATH_DIRECT);
9536 }
9537
9538 }
9539
9540
9541 mode_sense_failed:
9542
9543 kmem_free(header, buflen);
9544
9545 if (rval != 0) {
9546 if (rval == EIO)
9547 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9548 else
9549 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9550 }
9551 return (rval);
9552 }
9553
9554
9555 /*
9556 * Function: sd_get_write_cache_enabled()
9557 *
9558 * Description: This routine is the driver entry point for determining if
9559 * write caching is enabled. It examines the WCE (write cache
9560 * enable) bits of mode page 8 (MODEPAGE_CACHING).
9561 *
9562 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
9563 * structure for this target.
9564 * is_enabled - pointer to int where write cache enabled state
9565 * is returned (non-zero -> write cache enabled)
9566 *
9567 *
9568 * Return Code: EIO
9569 * code returned by sd_send_scsi_MODE_SENSE
9570 *
9571 * Context: Kernel Thread
9572 *
9573 * NOTE: If ioctl is added to disable write cache, this sequence should
9574 * be followed so that no locking is required for accesses to
9575 * un->un_f_write_cache_enabled:
9576 * do mode select to clear wce
9577 * do synchronize cache to flush cache
9578 * set un->un_f_write_cache_enabled = FALSE
9579 *
9580 * Conversely, an ioctl to enable the write cache should be done
9581 * in this order:
9582 * set un->un_f_write_cache_enabled = TRUE
9583 * do mode select to set wce
9584 */
9585
9586 static int
9587 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9588 {
9589 struct mode_caching *mode_caching_page;
9590 uchar_t *header;
9591 size_t buflen;
9592 int hdrlen;
9593 int bd_len;
9594 int rval = 0;
9595 struct sd_lun *un;
9596 int status;
9597
9598 ASSERT(ssc != NULL);
9599 un = ssc->ssc_un;
9600 ASSERT(un != NULL);
9601 ASSERT(is_enabled != NULL);
9602
9603 /* in case of error, flag as enabled */
9604 *is_enabled = TRUE;
9605
9606 /*
9607 * Do a test unit ready, otherwise a mode sense may not work if this
9608 * is the first command sent to the device after boot.
9609 */
9610 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9611
9612 if (status != 0)
9613 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9614
9615 if (un->un_f_cfg_is_atapi == TRUE) {
9616 hdrlen = MODE_HEADER_LENGTH_GRP2;
9617 } else {
9618 hdrlen = MODE_HEADER_LENGTH;
9619 }
9620
9621 /*
9622 * Allocate memory for the retrieved mode page and its headers. Set
9623 * a pointer to the page itself.
9624 */
9625 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9626 header = kmem_zalloc(buflen, KM_SLEEP);
9627
9628 /* Get the information from the device. */
9629 if (un->un_f_cfg_is_atapi == TRUE) {
9630 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9631 MODEPAGE_CACHING, SD_PATH_DIRECT);
9632 } else {
9633 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9634 MODEPAGE_CACHING, SD_PATH_DIRECT);
9635 }
9636
9637 if (rval != 0) {
9638 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9639 "sd_get_write_cache_enabled: Mode Sense Failed\n");
9640 goto mode_sense_failed;
9641 }
9642
9643 /*
9644 * Determine size of Block Descriptors in order to locate
9645 * the mode page data. ATAPI devices return 0, SCSI devices
9646 * should return MODE_BLK_DESC_LENGTH.
9647 */
9648 if (un->un_f_cfg_is_atapi == TRUE) {
9649 struct mode_header_grp2 *mhp;
9650 mhp = (struct mode_header_grp2 *)header;
9651 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9652 } else {
9653 bd_len = ((struct mode_header *)header)->bdesc_length;
9654 }
9655
9656 if (bd_len > MODE_BLK_DESC_LENGTH) {
9657 /* FMA should make upset complain here */
9658 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9659 "sd_get_write_cache_enabled: Mode Sense returned invalid "
9660 "block descriptor length\n");
9661 rval = EIO;
9662 goto mode_sense_failed;
9663 }
9664
9665 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9666 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9667 /* FMA could make upset complain here */
9668 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9669 "sd_get_write_cache_enabled: Mode Sense caching page "
9670 "code mismatch %d\n", mode_caching_page->mode_page.code);
9671 rval = EIO;
9672 goto mode_sense_failed;
9673 }
9674 *is_enabled = mode_caching_page->wce;
9675
9676 mode_sense_failed:
9677 if (rval == 0) {
9678 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9679 } else if (rval == EIO) {
9680 /*
9681 * Some disks do not support mode sense(6), we
9682 * should ignore this kind of error(sense key is
9683 * 0x5 - illegal request).
9684 */
9685 uint8_t *sensep;
9686 int senlen;
9687
9688 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9689 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9690 ssc->ssc_uscsi_cmd->uscsi_rqresid);
9691
9692 if (senlen > 0 &&
9693 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9694 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9695 } else {
9696 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9697 }
9698 } else {
9699 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9700 }
9701 kmem_free(header, buflen);
9702 return (rval);
9703 }
9704
9705 /*
9706 * Function: sd_get_nv_sup()
9707 *
9708 * Description: This routine is the driver entry point for
9709 * determining whether non-volatile cache is supported. This
9710 * determination process works as follows:
9711 *
9712 * 1. sd first queries sd.conf on whether
9713 * suppress_cache_flush bit is set for this device.
9714 *
9715 * 2. if not there, then queries the internal disk table.
9716 *
9717 * 3. if either sd.conf or internal disk table specifies
9718 * cache flush be suppressed, we don't bother checking
9719 * NV_SUP bit.
9720 *
9721 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9722 * the optional INQUIRY VPD page 0x86. If the device
9723 * supports VPD page 0x86, sd examines the NV_SUP
9724 * (non-volatile cache support) bit in the INQUIRY VPD page
9725 * 0x86:
9726 * o If NV_SUP bit is set, sd assumes the device has a
9727 * non-volatile cache and set the
9728 * un_f_sync_nv_supported to TRUE.
9729 * o Otherwise cache is not non-volatile,
9730 * un_f_sync_nv_supported is set to FALSE.
9731 *
9732 * Arguments: un - driver soft state (unit) structure
9733 *
9734 * Return Code:
9735 *
9736 * Context: Kernel Thread
9737 */
9738
9739 static void
9740 sd_get_nv_sup(sd_ssc_t *ssc)
9741 {
9742 int rval = 0;
9743 uchar_t *inq86 = NULL;
9744 size_t inq86_len = MAX_INQUIRY_SIZE;
9745 size_t inq86_resid = 0;
9746 struct dk_callback *dkc;
9747 struct sd_lun *un;
9748
9749 ASSERT(ssc != NULL);
9750 un = ssc->ssc_un;
9751 ASSERT(un != NULL);
9752
9753 mutex_enter(SD_MUTEX(un));
9754
9755 /*
9756 * Be conservative on the device's support of
9757 * SYNC_NV bit: un_f_sync_nv_supported is
9758 * initialized to be false.
9759 */
9760 un->un_f_sync_nv_supported = FALSE;
9761
9762 /*
9763 * If either sd.conf or internal disk table
9764 * specifies cache flush be suppressed, then
9765 * we don't bother checking NV_SUP bit.
9766 */
9767 if (un->un_f_suppress_cache_flush == TRUE) {
9768 mutex_exit(SD_MUTEX(un));
9769 return;
9770 }
9771
9772 if (sd_check_vpd_page_support(ssc) == 0 &&
9773 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9774 mutex_exit(SD_MUTEX(un));
9775 /* collect page 86 data if available */
9776 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9777
9778 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9779 0x01, 0x86, &inq86_resid);
9780
9781 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9782 SD_TRACE(SD_LOG_COMMON, un,
9783 "sd_get_nv_sup: \
9784 successfully get VPD page: %x \
9785 PAGE LENGTH: %x BYTE 6: %x\n",
9786 inq86[1], inq86[3], inq86[6]);
9787
9788 mutex_enter(SD_MUTEX(un));
9789 /*
9790 * check the value of NV_SUP bit: only if the device
9791 * reports NV_SUP bit to be 1, the
9792 * un_f_sync_nv_supported bit will be set to true.
9793 */
9794 if (inq86[6] & SD_VPD_NV_SUP) {
9795 un->un_f_sync_nv_supported = TRUE;
9796 }
9797 mutex_exit(SD_MUTEX(un));
9798 } else if (rval != 0) {
9799 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9800 }
9801
9802 kmem_free(inq86, inq86_len);
9803 } else {
9804 mutex_exit(SD_MUTEX(un));
9805 }
9806
9807 /*
9808 * Send a SYNC CACHE command to check whether
9809 * SYNC_NV bit is supported. This command should have
9810 * un_f_sync_nv_supported set to correct value.
9811 */
9812 mutex_enter(SD_MUTEX(un));
9813 if (un->un_f_sync_nv_supported) {
9814 mutex_exit(SD_MUTEX(un));
9815 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9816 dkc->dkc_flag = FLUSH_VOLATILE;
9817 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9818
9819 /*
9820 * Send a TEST UNIT READY command to the device. This should
9821 * clear any outstanding UNIT ATTENTION that may be present.
9822 */
9823 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9824 if (rval != 0)
9825 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9826
9827 kmem_free(dkc, sizeof (struct dk_callback));
9828 } else {
9829 mutex_exit(SD_MUTEX(un));
9830 }
9831
9832 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9833 un_f_suppress_cache_flush is set to %d\n",
9834 un->un_f_suppress_cache_flush);
9835 }
9836
9837 /*
9838 * Function: sd_make_device
9839 *
9840 * Description: Utility routine to return the Solaris device number from
9841 * the data in the device's dev_info structure.
9842 *
9843 * Return Code: The Solaris device number
9844 *
9845 * Context: Any
9846 */
9847
9848 static dev_t
9849 sd_make_device(dev_info_t *devi)
9850 {
9851 return (makedevice(ddi_driver_major(devi),
9852 ddi_get_instance(devi) << SDUNIT_SHIFT));
9853 }
9854
9855
9856 /*
9857 * Function: sd_pm_entry
9858 *
9859 * Description: Called at the start of a new command to manage power
9860 * and busy status of a device. This includes determining whether
9861 * the current power state of the device is sufficient for
9862 * performing the command or whether it must be changed.
9863 * The PM framework is notified appropriately.
9864 * Only with a return status of DDI_SUCCESS will the
9865 * component be busy to the framework.
9866 *
9867 * All callers of sd_pm_entry must check the return status
9868 * and only call sd_pm_exit it it was DDI_SUCCESS. A status
9869 * of DDI_FAILURE indicates the device failed to power up.
9870 * In this case un_pm_count has been adjusted so the result
9871 * on exit is still powered down, ie. count is less than 0.
9872 * Calling sd_pm_exit with this count value hits an ASSERT.
9873 *
9874 * Return Code: DDI_SUCCESS or DDI_FAILURE
9875 *
9876 * Context: Kernel thread context.
9877 */
9878
9879 static int
9880 sd_pm_entry(struct sd_lun *un)
9881 {
9882 int return_status = DDI_SUCCESS;
9883
9884 ASSERT(!mutex_owned(SD_MUTEX(un)));
9885 ASSERT(!mutex_owned(&un->un_pm_mutex));
9886
9887 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9888
9889 if (un->un_f_pm_is_enabled == FALSE) {
9890 SD_TRACE(SD_LOG_IO_PM, un,
9891 "sd_pm_entry: exiting, PM not enabled\n");
9892 return (return_status);
9893 }
9894
9895 /*
9896 * Just increment a counter if PM is enabled. On the transition from
9897 * 0 ==> 1, mark the device as busy. The iodone side will decrement
9898 * the count with each IO and mark the device as idle when the count
9899 * hits 0.
9900 *
9901 * If the count is less than 0 the device is powered down. If a powered
9902 * down device is successfully powered up then the count must be
9903 * incremented to reflect the power up. Note that it'll get incremented
9904 * a second time to become busy.
9905 *
9906 * Because the following has the potential to change the device state
9907 * and must release the un_pm_mutex to do so, only one thread can be
9908 * allowed through at a time.
9909 */
9910
9911 mutex_enter(&un->un_pm_mutex);
9912 while (un->un_pm_busy == TRUE) {
9913 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9914 }
9915 un->un_pm_busy = TRUE;
9916
9917 if (un->un_pm_count < 1) {
9918
9919 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9920
9921 /*
9922 * Indicate we are now busy so the framework won't attempt to
9923 * power down the device. This call will only fail if either
9924 * we passed a bad component number or the device has no
9925 * components. Neither of these should ever happen.
9926 */
9927 mutex_exit(&un->un_pm_mutex);
9928 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9929 ASSERT(return_status == DDI_SUCCESS);
9930
9931 mutex_enter(&un->un_pm_mutex);
9932
9933 if (un->un_pm_count < 0) {
9934 mutex_exit(&un->un_pm_mutex);
9935
9936 SD_TRACE(SD_LOG_IO_PM, un,
9937 "sd_pm_entry: power up component\n");
9938
9939 /*
9940 * pm_raise_power will cause sdpower to be called
9941 * which brings the device power level to the
9942 * desired state, If successful, un_pm_count and
9943 * un_power_level will be updated appropriately.
9944 */
9945 return_status = pm_raise_power(SD_DEVINFO(un), 0,
9946 SD_PM_STATE_ACTIVE(un));
9947
9948 mutex_enter(&un->un_pm_mutex);
9949
9950 if (return_status != DDI_SUCCESS) {
9951 /*
9952 * Power up failed.
9953 * Idle the device and adjust the count
9954 * so the result on exit is that we're
9955 * still powered down, ie. count is less than 0.
9956 */
9957 SD_TRACE(SD_LOG_IO_PM, un,
9958 "sd_pm_entry: power up failed,"
9959 " idle the component\n");
9960
9961 (void) pm_idle_component(SD_DEVINFO(un), 0);
9962 un->un_pm_count--;
9963 } else {
9964 /*
9965 * Device is powered up, verify the
9966 * count is non-negative.
9967 * This is debug only.
9968 */
9969 ASSERT(un->un_pm_count == 0);
9970 }
9971 }
9972
9973 if (return_status == DDI_SUCCESS) {
9974 /*
9975 * For performance, now that the device has been tagged
9976 * as busy, and it's known to be powered up, update the
9977 * chain types to use jump tables that do not include
9978 * pm. This significantly lowers the overhead and
9979 * therefore improves performance.
9980 */
9981
9982 mutex_exit(&un->un_pm_mutex);
9983 mutex_enter(SD_MUTEX(un));
9984 SD_TRACE(SD_LOG_IO_PM, un,
9985 "sd_pm_entry: changing uscsi_chain_type from %d\n",
9986 un->un_uscsi_chain_type);
9987
9988 if (un->un_f_non_devbsize_supported) {
9989 un->un_buf_chain_type =
9990 SD_CHAIN_INFO_RMMEDIA_NO_PM;
9991 } else {
9992 un->un_buf_chain_type =
9993 SD_CHAIN_INFO_DISK_NO_PM;
9994 }
9995 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9996
9997 SD_TRACE(SD_LOG_IO_PM, un,
9998 " changed uscsi_chain_type to %d\n",
9999 un->un_uscsi_chain_type);
10000 mutex_exit(SD_MUTEX(un));
10001 mutex_enter(&un->un_pm_mutex);
10002
10003 if (un->un_pm_idle_timeid == NULL) {
10004 /* 300 ms. */
10005 un->un_pm_idle_timeid =
10006 timeout(sd_pm_idletimeout_handler, un,
10007 (drv_usectohz((clock_t)300000)));
10008 /*
10009 * Include an extra call to busy which keeps the
10010 * device busy with-respect-to the PM layer
10011 * until the timer fires, at which time it'll
10012 * get the extra idle call.
10013 */
10014 (void) pm_busy_component(SD_DEVINFO(un), 0);
10015 }
10016 }
10017 }
10018 un->un_pm_busy = FALSE;
10019 /* Next... */
10020 cv_signal(&un->un_pm_busy_cv);
10021
10022 un->un_pm_count++;
10023
10024 SD_TRACE(SD_LOG_IO_PM, un,
10025 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10026
10027 mutex_exit(&un->un_pm_mutex);
10028
10029 return (return_status);
10030 }
10031
10032
10033 /*
10034 * Function: sd_pm_exit
10035 *
10036 * Description: Called at the completion of a command to manage busy
10037 * status for the device. If the device becomes idle the
10038 * PM framework is notified.
10039 *
10040 * Context: Kernel thread context
10041 */
10042
10043 static void
10044 sd_pm_exit(struct sd_lun *un)
10045 {
10046 ASSERT(!mutex_owned(SD_MUTEX(un)));
10047 ASSERT(!mutex_owned(&un->un_pm_mutex));
10048
10049 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10050
10051 /*
10052 * After attach the following flag is only read, so don't
10053 * take the penalty of acquiring a mutex for it.
10054 */
10055 if (un->un_f_pm_is_enabled == TRUE) {
10056
10057 mutex_enter(&un->un_pm_mutex);
10058 un->un_pm_count--;
10059
10060 SD_TRACE(SD_LOG_IO_PM, un,
10061 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10062
10063 ASSERT(un->un_pm_count >= 0);
10064 if (un->un_pm_count == 0) {
10065 mutex_exit(&un->un_pm_mutex);
10066
10067 SD_TRACE(SD_LOG_IO_PM, un,
10068 "sd_pm_exit: idle component\n");
10069
10070 (void) pm_idle_component(SD_DEVINFO(un), 0);
10071
10072 } else {
10073 mutex_exit(&un->un_pm_mutex);
10074 }
10075 }
10076
10077 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10078 }
10079
10080
10081 /*
10082 * Function: sdopen
10083 *
10084 * Description: Driver's open(9e) entry point function.
10085 *
10086 * Arguments: dev_i - pointer to device number
10087 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10088 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10089 * cred_p - user credential pointer
10090 *
10091 * Return Code: EINVAL
10092 * ENXIO
10093 * EIO
10094 * EROFS
10095 * EBUSY
10096 *
10097 * Context: Kernel thread context
10098 */
10099 /* ARGSUSED */
10100 static int
10101 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10102 {
10103 struct sd_lun *un;
10104 int nodelay;
10105 int part;
10106 uint64_t partmask;
10107 int instance;
10108 dev_t dev;
10109 int rval = EIO;
10110 diskaddr_t nblks = 0;
10111 diskaddr_t label_cap;
10112
10113 /* Validate the open type */
10114 if (otyp >= OTYPCNT) {
10115 return (EINVAL);
10116 }
10117
10118 dev = *dev_p;
10119 instance = SDUNIT(dev);
10120 mutex_enter(&sd_detach_mutex);
10121
10122 /*
10123 * Fail the open if there is no softstate for the instance, or
10124 * if another thread somewhere is trying to detach the instance.
10125 */
10126 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10127 (un->un_detach_count != 0)) {
10128 mutex_exit(&sd_detach_mutex);
10129 /*
10130 * The probe cache only needs to be cleared when open (9e) fails
10131 * with ENXIO (4238046).
10132 */
10133 /*
10134 * un-conditionally clearing probe cache is ok with
10135 * separate sd/ssd binaries
10136 * x86 platform can be an issue with both parallel
10137 * and fibre in 1 binary
10138 */
10139 sd_scsi_clear_probe_cache();
10140 return (ENXIO);
10141 }
10142
10143 /*
10144 * The un_layer_count is to prevent another thread in specfs from
10145 * trying to detach the instance, which can happen when we are
10146 * called from a higher-layer driver instead of thru specfs.
10147 * This will not be needed when DDI provides a layered driver
10148 * interface that allows specfs to know that an instance is in
10149 * use by a layered driver & should not be detached.
10150 *
10151 * Note: the semantics for layered driver opens are exactly one
10152 * close for every open.
10153 */
10154 if (otyp == OTYP_LYR) {
10155 un->un_layer_count++;
10156 }
10157
10158 /*
10159 * Keep a count of the current # of opens in progress. This is because
10160 * some layered drivers try to call us as a regular open. This can
10161 * cause problems that we cannot prevent, however by keeping this count
10162 * we can at least keep our open and detach routines from racing against
10163 * each other under such conditions.
10164 */
10165 un->un_opens_in_progress++;
10166 mutex_exit(&sd_detach_mutex);
10167
10168 nodelay = (flag & (FNDELAY | FNONBLOCK));
10169 part = SDPART(dev);
10170 partmask = 1 << part;
10171
10172 /*
10173 * We use a semaphore here in order to serialize
10174 * open and close requests on the device.
10175 */
10176 sema_p(&un->un_semoclose);
10177
10178 mutex_enter(SD_MUTEX(un));
10179
10180 /*
10181 * All device accesses go thru sdstrategy() where we check
10182 * on suspend status but there could be a scsi_poll command,
10183 * which bypasses sdstrategy(), so we need to check pm
10184 * status.
10185 */
10186
10187 if (!nodelay) {
10188 while ((un->un_state == SD_STATE_SUSPENDED) ||
10189 (un->un_state == SD_STATE_PM_CHANGING)) {
10190 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10191 }
10192
10193 mutex_exit(SD_MUTEX(un));
10194 if (sd_pm_entry(un) != DDI_SUCCESS) {
10195 rval = EIO;
10196 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10197 "sdopen: sd_pm_entry failed\n");
10198 goto open_failed_with_pm;
10199 }
10200 mutex_enter(SD_MUTEX(un));
10201 }
10202
10203 /* check for previous exclusive open */
10204 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10205 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10206 "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10207 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10208
10209 if (un->un_exclopen & (partmask)) {
10210 goto excl_open_fail;
10211 }
10212
10213 if (flag & FEXCL) {
10214 int i;
10215 if (un->un_ocmap.lyropen[part]) {
10216 goto excl_open_fail;
10217 }
10218 for (i = 0; i < (OTYPCNT - 1); i++) {
10219 if (un->un_ocmap.regopen[i] & (partmask)) {
10220 goto excl_open_fail;
10221 }
10222 }
10223 }
10224
10225 /*
10226 * Check the write permission if this is a removable media device,
10227 * NDELAY has not been set, and writable permission is requested.
10228 *
10229 * Note: If NDELAY was set and this is write-protected media the WRITE
10230 * attempt will fail with EIO as part of the I/O processing. This is a
10231 * more permissive implementation that allows the open to succeed and
10232 * WRITE attempts to fail when appropriate.
10233 */
10234 if (un->un_f_chk_wp_open) {
10235 if ((flag & FWRITE) && (!nodelay)) {
10236 mutex_exit(SD_MUTEX(un));
10237 /*
10238 * Defer the check for write permission on writable
10239 * DVD drive till sdstrategy and will not fail open even
10240 * if FWRITE is set as the device can be writable
10241 * depending upon the media and the media can change
10242 * after the call to open().
10243 */
10244 if (un->un_f_dvdram_writable_device == FALSE) {
10245 if (ISCD(un) || sr_check_wp(dev)) {
10246 rval = EROFS;
10247 mutex_enter(SD_MUTEX(un));
10248 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10249 "write to cd or write protected media\n");
10250 goto open_fail;
10251 }
10252 }
10253 mutex_enter(SD_MUTEX(un));
10254 }
10255 }
10256
10257 /*
10258 * If opening in NDELAY/NONBLOCK mode, just return.
10259 * Check if disk is ready and has a valid geometry later.
10260 */
10261 if (!nodelay) {
10262 sd_ssc_t *ssc;
10263
10264 mutex_exit(SD_MUTEX(un));
10265 ssc = sd_ssc_init(un);
10266 rval = sd_ready_and_valid(ssc, part);
10267 sd_ssc_fini(ssc);
10268 mutex_enter(SD_MUTEX(un));
10269 /*
10270 * Fail if device is not ready or if the number of disk
10271 * blocks is zero or negative for non CD devices.
10272 */
10273
10274 nblks = 0;
10275
10276 if (rval == SD_READY_VALID && (!ISCD(un))) {
10277 /* if cmlb_partinfo fails, nblks remains 0 */
10278 mutex_exit(SD_MUTEX(un));
10279 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10280 NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10281 mutex_enter(SD_MUTEX(un));
10282 }
10283
10284 if ((rval != SD_READY_VALID) ||
10285 (!ISCD(un) && nblks <= 0)) {
10286 rval = un->un_f_has_removable_media ? ENXIO : EIO;
10287 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10288 "device not ready or invalid disk block value\n");
10289 goto open_fail;
10290 }
10291 #if defined(__i386) || defined(__amd64)
10292 } else {
10293 uchar_t *cp;
10294 /*
10295 * x86 requires special nodelay handling, so that p0 is
10296 * always defined and accessible.
10297 * Invalidate geometry only if device is not already open.
10298 */
10299 cp = &un->un_ocmap.chkd[0];
10300 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10301 if (*cp != (uchar_t)0) {
10302 break;
10303 }
10304 cp++;
10305 }
10306 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10307 mutex_exit(SD_MUTEX(un));
10308 cmlb_invalidate(un->un_cmlbhandle,
10309 (void *)SD_PATH_DIRECT);
10310 mutex_enter(SD_MUTEX(un));
10311 }
10312
10313 #endif
10314 }
10315
10316 if (otyp == OTYP_LYR) {
10317 un->un_ocmap.lyropen[part]++;
10318 } else {
10319 un->un_ocmap.regopen[otyp] |= partmask;
10320 }
10321
10322 /* Set up open and exclusive open flags */
10323 if (flag & FEXCL) {
10324 un->un_exclopen |= (partmask);
10325 }
10326
10327 /*
10328 * If the lun is EFI labeled and lun capacity is greater than the
10329 * capacity contained in the label, log a sys-event to notify the
10330 * interested module.
10331 * To avoid an infinite loop of logging sys-event, we only log the
10332 * event when the lun is not opened in NDELAY mode. The event handler
10333 * should open the lun in NDELAY mode.
10334 */
10335 if (!nodelay) {
10336 mutex_exit(SD_MUTEX(un));
10337 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10338 (void*)SD_PATH_DIRECT) == 0) {
10339 mutex_enter(SD_MUTEX(un));
10340 if (un->un_f_blockcount_is_valid &&
10341 un->un_blockcount > label_cap &&
10342 un->un_f_expnevent == B_FALSE) {
10343 un->un_f_expnevent = B_TRUE;
10344 mutex_exit(SD_MUTEX(un));
10345 sd_log_lun_expansion_event(un,
10346 (nodelay ? KM_NOSLEEP : KM_SLEEP));
10347 mutex_enter(SD_MUTEX(un));
10348 }
10349 } else {
10350 mutex_enter(SD_MUTEX(un));
10351 }
10352 }
10353
10354 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10355 "open of part %d type %d\n", part, otyp);
10356
10357 mutex_exit(SD_MUTEX(un));
10358 if (!nodelay) {
10359 sd_pm_exit(un);
10360 }
10361
10362 sema_v(&un->un_semoclose);
10363
10364 mutex_enter(&sd_detach_mutex);
10365 un->un_opens_in_progress--;
10366 mutex_exit(&sd_detach_mutex);
10367
10368 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10369 return (DDI_SUCCESS);
10370
10371 excl_open_fail:
10372 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10373 rval = EBUSY;
10374
10375 open_fail:
10376 mutex_exit(SD_MUTEX(un));
10377
10378 /*
10379 * On a failed open we must exit the pm management.
10380 */
10381 if (!nodelay) {
10382 sd_pm_exit(un);
10383 }
10384 open_failed_with_pm:
10385 sema_v(&un->un_semoclose);
10386
10387 mutex_enter(&sd_detach_mutex);
10388 un->un_opens_in_progress--;
10389 if (otyp == OTYP_LYR) {
10390 un->un_layer_count--;
10391 }
10392 mutex_exit(&sd_detach_mutex);
10393
10394 return (rval);
10395 }
10396
10397
10398 /*
10399 * Function: sdclose
10400 *
10401 * Description: Driver's close(9e) entry point function.
10402 *
10403 * Arguments: dev - device number
10404 * flag - file status flag, informational only
10405 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10406 * cred_p - user credential pointer
10407 *
10408 * Return Code: ENXIO
10409 *
10410 * Context: Kernel thread context
10411 */
10412 /* ARGSUSED */
10413 static int
10414 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10415 {
10416 struct sd_lun *un;
10417 uchar_t *cp;
10418 int part;
10419 int nodelay;
10420 int rval = 0;
10421
10422 /* Validate the open type */
10423 if (otyp >= OTYPCNT) {
10424 return (ENXIO);
10425 }
10426
10427 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10428 return (ENXIO);
10429 }
10430
10431 part = SDPART(dev);
10432 nodelay = flag & (FNDELAY | FNONBLOCK);
10433
10434 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10435 "sdclose: close of part %d type %d\n", part, otyp);
10436
10437 /*
10438 * We use a semaphore here in order to serialize
10439 * open and close requests on the device.
10440 */
10441 sema_p(&un->un_semoclose);
10442
10443 mutex_enter(SD_MUTEX(un));
10444
10445 /* Don't proceed if power is being changed. */
10446 while (un->un_state == SD_STATE_PM_CHANGING) {
10447 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10448 }
10449
10450 if (un->un_exclopen & (1 << part)) {
10451 un->un_exclopen &= ~(1 << part);
10452 }
10453
10454 /* Update the open partition map */
10455 if (otyp == OTYP_LYR) {
10456 un->un_ocmap.lyropen[part] -= 1;
10457 } else {
10458 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10459 }
10460
10461 cp = &un->un_ocmap.chkd[0];
10462 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10463 if (*cp != NULL) {
10464 break;
10465 }
10466 cp++;
10467 }
10468
10469 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10470 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10471
10472 /*
10473 * We avoid persistance upon the last close, and set
10474 * the throttle back to the maximum.
10475 */
10476 un->un_throttle = un->un_saved_throttle;
10477
10478 if (un->un_state == SD_STATE_OFFLINE) {
10479 if (un->un_f_is_fibre == FALSE) {
10480 scsi_log(SD_DEVINFO(un), sd_label,
10481 CE_WARN, "offline\n");
10482 }
10483 mutex_exit(SD_MUTEX(un));
10484 cmlb_invalidate(un->un_cmlbhandle,
10485 (void *)SD_PATH_DIRECT);
10486 mutex_enter(SD_MUTEX(un));
10487
10488 } else {
10489 /*
10490 * Flush any outstanding writes in NVRAM cache.
10491 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10492 * cmd, it may not work for non-Pluto devices.
10493 * SYNCHRONIZE CACHE is not required for removables,
10494 * except DVD-RAM drives.
10495 *
10496 * Also note: because SYNCHRONIZE CACHE is currently
10497 * the only command issued here that requires the
10498 * drive be powered up, only do the power up before
10499 * sending the Sync Cache command. If additional
10500 * commands are added which require a powered up
10501 * drive, the following sequence may have to change.
10502 *
10503 * And finally, note that parallel SCSI on SPARC
10504 * only issues a Sync Cache to DVD-RAM, a newly
10505 * supported device.
10506 */
10507 #if defined(__i386) || defined(__amd64)
10508 if ((un->un_f_sync_cache_supported &&
10509 un->un_f_sync_cache_required) ||
10510 un->un_f_dvdram_writable_device == TRUE) {
10511 #else
10512 if (un->un_f_dvdram_writable_device == TRUE) {
10513 #endif
10514 mutex_exit(SD_MUTEX(un));
10515 if (sd_pm_entry(un) == DDI_SUCCESS) {
10516 rval =
10517 sd_send_scsi_SYNCHRONIZE_CACHE(un,
10518 NULL);
10519 /* ignore error if not supported */
10520 if (rval == ENOTSUP) {
10521 rval = 0;
10522 } else if (rval != 0) {
10523 rval = EIO;
10524 }
10525 sd_pm_exit(un);
10526 } else {
10527 rval = EIO;
10528 }
10529 mutex_enter(SD_MUTEX(un));
10530 }
10531
10532 /*
10533 * For devices which supports DOOR_LOCK, send an ALLOW
10534 * MEDIA REMOVAL command, but don't get upset if it
10535 * fails. We need to raise the power of the drive before
10536 * we can call sd_send_scsi_DOORLOCK()
10537 */
10538 if (un->un_f_doorlock_supported) {
10539 mutex_exit(SD_MUTEX(un));
10540 if (sd_pm_entry(un) == DDI_SUCCESS) {
10541 sd_ssc_t *ssc;
10542
10543 ssc = sd_ssc_init(un);
10544 rval = sd_send_scsi_DOORLOCK(ssc,
10545 SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10546 if (rval != 0)
10547 sd_ssc_assessment(ssc,
10548 SD_FMT_IGNORE);
10549 sd_ssc_fini(ssc);
10550
10551 sd_pm_exit(un);
10552 if (ISCD(un) && (rval != 0) &&
10553 (nodelay != 0)) {
10554 rval = ENXIO;
10555 }
10556 } else {
10557 rval = EIO;
10558 }
10559 mutex_enter(SD_MUTEX(un));
10560 }
10561
10562 /*
10563 * If a device has removable media, invalidate all
10564 * parameters related to media, such as geometry,
10565 * blocksize, and blockcount.
10566 */
10567 if (un->un_f_has_removable_media) {
10568 sr_ejected(un);
10569 }
10570
10571 /*
10572 * Destroy the cache (if it exists) which was
10573 * allocated for the write maps since this is
10574 * the last close for this media.
10575 */
10576 if (un->un_wm_cache) {
10577 /*
10578 * Check if there are pending commands.
10579 * and if there are give a warning and
10580 * do not destroy the cache.
10581 */
10582 if (un->un_ncmds_in_driver > 0) {
10583 scsi_log(SD_DEVINFO(un),
10584 sd_label, CE_WARN,
10585 "Unable to clean up memory "
10586 "because of pending I/O\n");
10587 } else {
10588 kmem_cache_destroy(
10589 un->un_wm_cache);
10590 un->un_wm_cache = NULL;
10591 }
10592 }
10593 }
10594 }
10595
10596 mutex_exit(SD_MUTEX(un));
10597 sema_v(&un->un_semoclose);
10598
10599 if (otyp == OTYP_LYR) {
10600 mutex_enter(&sd_detach_mutex);
10601 /*
10602 * The detach routine may run when the layer count
10603 * drops to zero.
10604 */
10605 un->un_layer_count--;
10606 mutex_exit(&sd_detach_mutex);
10607 }
10608
10609 return (rval);
10610 }
10611
10612
10613 /*
10614 * Function: sd_ready_and_valid
10615 *
10616 * Description: Test if device is ready and has a valid geometry.
10617 *
10618 * Arguments: ssc - sd_ssc_t will contain un
10619 * un - driver soft state (unit) structure
10620 *
10621 * Return Code: SD_READY_VALID ready and valid label
10622 * SD_NOT_READY_VALID not ready, no label
10623 * SD_RESERVED_BY_OTHERS reservation conflict
10624 *
10625 * Context: Never called at interrupt context.
10626 */
10627
10628 static int
10629 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10630 {
10631 struct sd_errstats *stp;
10632 uint64_t capacity;
10633 uint_t lbasize;
10634 int rval = SD_READY_VALID;
10635 char name_str[48];
10636 boolean_t is_valid;
10637 struct sd_lun *un;
10638 int status;
10639
10640 ASSERT(ssc != NULL);
10641 un = ssc->ssc_un;
10642 ASSERT(un != NULL);
10643 ASSERT(!mutex_owned(SD_MUTEX(un)));
10644
10645 mutex_enter(SD_MUTEX(un));
10646 /*
10647 * If a device has removable media, we must check if media is
10648 * ready when checking if this device is ready and valid.
10649 */
10650 if (un->un_f_has_removable_media) {
10651 mutex_exit(SD_MUTEX(un));
10652 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10653
10654 if (status != 0) {
10655 rval = SD_NOT_READY_VALID;
10656 mutex_enter(SD_MUTEX(un));
10657
10658 /* Ignore all failed status for removalbe media */
10659 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10660
10661 goto done;
10662 }
10663
10664 is_valid = SD_IS_VALID_LABEL(un);
10665 mutex_enter(SD_MUTEX(un));
10666 if (!is_valid ||
10667 (un->un_f_blockcount_is_valid == FALSE) ||
10668 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10669
10670 /* capacity has to be read every open. */
10671 mutex_exit(SD_MUTEX(un));
10672 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10673 &lbasize, SD_PATH_DIRECT);
10674
10675 if (status != 0) {
10676 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10677
10678 cmlb_invalidate(un->un_cmlbhandle,
10679 (void *)SD_PATH_DIRECT);
10680 mutex_enter(SD_MUTEX(un));
10681 rval = SD_NOT_READY_VALID;
10682
10683 goto done;
10684 } else {
10685 mutex_enter(SD_MUTEX(un));
10686 sd_update_block_info(un, lbasize, capacity);
10687 }
10688 }
10689
10690 /*
10691 * Check if the media in the device is writable or not.
10692 */
10693 if (!is_valid && ISCD(un)) {
10694 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10695 }
10696
10697 } else {
10698 /*
10699 * Do a test unit ready to clear any unit attention from non-cd
10700 * devices.
10701 */
10702 mutex_exit(SD_MUTEX(un));
10703
10704 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10705 if (status != 0) {
10706 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10707 }
10708
10709 mutex_enter(SD_MUTEX(un));
10710 }
10711
10712
10713 /*
10714 * If this is a non 512 block device, allocate space for
10715 * the wmap cache. This is being done here since every time
10716 * a media is changed this routine will be called and the
10717 * block size is a function of media rather than device.
10718 */
10719 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10720 un->un_f_non_devbsize_supported) &&
10721 un->un_tgt_blocksize != DEV_BSIZE) ||
10722 un->un_f_enable_rmw) {
10723 if (!(un->un_wm_cache)) {
10724 (void) snprintf(name_str, sizeof (name_str),
10725 "%s%d_cache",
10726 ddi_driver_name(SD_DEVINFO(un)),
10727 ddi_get_instance(SD_DEVINFO(un)));
10728 un->un_wm_cache = kmem_cache_create(
10729 name_str, sizeof (struct sd_w_map),
10730 8, sd_wm_cache_constructor,
10731 sd_wm_cache_destructor, NULL,
10732 (void *)un, NULL, 0);
10733 if (!(un->un_wm_cache)) {
10734 rval = ENOMEM;
10735 goto done;
10736 }
10737 }
10738 }
10739
10740 if (un->un_state == SD_STATE_NORMAL) {
10741 /*
10742 * If the target is not yet ready here (defined by a TUR
10743 * failure), invalidate the geometry and print an 'offline'
10744 * message. This is a legacy message, as the state of the
10745 * target is not actually changed to SD_STATE_OFFLINE.
10746 *
10747 * If the TUR fails for EACCES (Reservation Conflict),
10748 * SD_RESERVED_BY_OTHERS will be returned to indicate
10749 * reservation conflict. If the TUR fails for other
10750 * reasons, SD_NOT_READY_VALID will be returned.
10751 */
10752 int err;
10753
10754 mutex_exit(SD_MUTEX(un));
10755 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10756 mutex_enter(SD_MUTEX(un));
10757
10758 if (err != 0) {
10759 mutex_exit(SD_MUTEX(un));
10760 cmlb_invalidate(un->un_cmlbhandle,
10761 (void *)SD_PATH_DIRECT);
10762 mutex_enter(SD_MUTEX(un));
10763 if (err == EACCES) {
10764 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10765 "reservation conflict\n");
10766 rval = SD_RESERVED_BY_OTHERS;
10767 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10768 } else {
10769 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10770 "drive offline\n");
10771 rval = SD_NOT_READY_VALID;
10772 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10773 }
10774 goto done;
10775 }
10776 }
10777
10778 if (un->un_f_format_in_progress == FALSE) {
10779 mutex_exit(SD_MUTEX(un));
10780
10781 (void) cmlb_validate(un->un_cmlbhandle, 0,
10782 (void *)SD_PATH_DIRECT);
10783 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10784 NULL, (void *) SD_PATH_DIRECT) != 0) {
10785 rval = SD_NOT_READY_VALID;
10786 mutex_enter(SD_MUTEX(un));
10787
10788 goto done;
10789 }
10790 if (un->un_f_pkstats_enabled) {
10791 sd_set_pstats(un);
10792 SD_TRACE(SD_LOG_IO_PARTITION, un,
10793 "sd_ready_and_valid: un:0x%p pstats created and "
10794 "set\n", un);
10795 }
10796 mutex_enter(SD_MUTEX(un));
10797 }
10798
10799 /*
10800 * If this device supports DOOR_LOCK command, try and send
10801 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10802 * if it fails. For a CD, however, it is an error
10803 */
10804 if (un->un_f_doorlock_supported) {
10805 mutex_exit(SD_MUTEX(un));
10806 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10807 SD_PATH_DIRECT);
10808
10809 if ((status != 0) && ISCD(un)) {
10810 rval = SD_NOT_READY_VALID;
10811 mutex_enter(SD_MUTEX(un));
10812
10813 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10814
10815 goto done;
10816 } else if (status != 0)
10817 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10818 mutex_enter(SD_MUTEX(un));
10819 }
10820
10821 /* The state has changed, inform the media watch routines */
10822 un->un_mediastate = DKIO_INSERTED;
10823 cv_broadcast(&un->un_state_cv);
10824 rval = SD_READY_VALID;
10825
10826 done:
10827
10828 /*
10829 * Initialize the capacity kstat value, if no media previously
10830 * (capacity kstat is 0) and a media has been inserted
10831 * (un_blockcount > 0).
10832 */
10833 if (un->un_errstats != NULL) {
10834 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10835 if ((stp->sd_capacity.value.ui64 == 0) &&
10836 (un->un_f_blockcount_is_valid == TRUE)) {
10837 stp->sd_capacity.value.ui64 =
10838 (uint64_t)((uint64_t)un->un_blockcount *
10839 un->un_sys_blocksize);
10840 }
10841 }
10842
10843 mutex_exit(SD_MUTEX(un));
10844 return (rval);
10845 }
10846
10847
10848 /*
10849 * Function: sdmin
10850 *
10851 * Description: Routine to limit the size of a data transfer. Used in
10852 * conjunction with physio(9F).
10853 *
10854 * Arguments: bp - pointer to the indicated buf(9S) struct.
10855 *
10856 * Context: Kernel thread context.
10857 */
10858
10859 static void
10860 sdmin(struct buf *bp)
10861 {
10862 struct sd_lun *un;
10863 int instance;
10864
10865 instance = SDUNIT(bp->b_edev);
10866
10867 un = ddi_get_soft_state(sd_state, instance);
10868 ASSERT(un != NULL);
10869
10870 /*
10871 * We depend on buf breakup to restrict
10872 * IO size if it is enabled.
10873 */
10874 if (un->un_buf_breakup_supported) {
10875 return;
10876 }
10877
10878 if (bp->b_bcount > un->un_max_xfer_size) {
10879 bp->b_bcount = un->un_max_xfer_size;
10880 }
10881 }
10882
10883
10884 /*
10885 * Function: sdread
10886 *
10887 * Description: Driver's read(9e) entry point function.
10888 *
10889 * Arguments: dev - device number
10890 * uio - structure pointer describing where data is to be stored
10891 * in user's space
10892 * cred_p - user credential pointer
10893 *
10894 * Return Code: ENXIO
10895 * EIO
10896 * EINVAL
10897 * value returned by physio
10898 *
10899 * Context: Kernel thread context.
10900 */
10901 /* ARGSUSED */
10902 static int
10903 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10904 {
10905 struct sd_lun *un = NULL;
10906 int secmask;
10907 int err = 0;
10908 sd_ssc_t *ssc;
10909
10910 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10911 return (ENXIO);
10912 }
10913
10914 ASSERT(!mutex_owned(SD_MUTEX(un)));
10915
10916
10917 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10918 mutex_enter(SD_MUTEX(un));
10919 /*
10920 * Because the call to sd_ready_and_valid will issue I/O we
10921 * must wait here if either the device is suspended or
10922 * if it's power level is changing.
10923 */
10924 while ((un->un_state == SD_STATE_SUSPENDED) ||
10925 (un->un_state == SD_STATE_PM_CHANGING)) {
10926 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10927 }
10928 un->un_ncmds_in_driver++;
10929 mutex_exit(SD_MUTEX(un));
10930
10931 /* Initialize sd_ssc_t for internal uscsi commands */
10932 ssc = sd_ssc_init(un);
10933 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10934 err = EIO;
10935 } else {
10936 err = 0;
10937 }
10938 sd_ssc_fini(ssc);
10939
10940 mutex_enter(SD_MUTEX(un));
10941 un->un_ncmds_in_driver--;
10942 ASSERT(un->un_ncmds_in_driver >= 0);
10943 mutex_exit(SD_MUTEX(un));
10944 if (err != 0)
10945 return (err);
10946 }
10947
10948 /*
10949 * Read requests are restricted to multiples of the system block size.
10950 */
10951 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10952 !un->un_f_enable_rmw)
10953 secmask = un->un_tgt_blocksize - 1;
10954 else
10955 secmask = DEV_BSIZE - 1;
10956
10957 if (uio->uio_loffset & ((offset_t)(secmask))) {
10958 SD_ERROR(SD_LOG_READ_WRITE, un,
10959 "sdread: file offset not modulo %d\n",
10960 secmask + 1);
10961 err = EINVAL;
10962 } else if (uio->uio_iov->iov_len & (secmask)) {
10963 SD_ERROR(SD_LOG_READ_WRITE, un,
10964 "sdread: transfer length not modulo %d\n",
10965 secmask + 1);
10966 err = EINVAL;
10967 } else {
10968 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10969 }
10970
10971 return (err);
10972 }
10973
10974
10975 /*
10976 * Function: sdwrite
10977 *
10978 * Description: Driver's write(9e) entry point function.
10979 *
10980 * Arguments: dev - device number
10981 * uio - structure pointer describing where data is stored in
10982 * user's space
10983 * cred_p - user credential pointer
10984 *
10985 * Return Code: ENXIO
10986 * EIO
10987 * EINVAL
10988 * value returned by physio
10989 *
10990 * Context: Kernel thread context.
10991 */
10992 /* ARGSUSED */
10993 static int
10994 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10995 {
10996 struct sd_lun *un = NULL;
10997 int secmask;
10998 int err = 0;
10999 sd_ssc_t *ssc;
11000
11001 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11002 return (ENXIO);
11003 }
11004
11005 ASSERT(!mutex_owned(SD_MUTEX(un)));
11006
11007 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11008 mutex_enter(SD_MUTEX(un));
11009 /*
11010 * Because the call to sd_ready_and_valid will issue I/O we
11011 * must wait here if either the device is suspended or
11012 * if it's power level is changing.
11013 */
11014 while ((un->un_state == SD_STATE_SUSPENDED) ||
11015 (un->un_state == SD_STATE_PM_CHANGING)) {
11016 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11017 }
11018 un->un_ncmds_in_driver++;
11019 mutex_exit(SD_MUTEX(un));
11020
11021 /* Initialize sd_ssc_t for internal uscsi commands */
11022 ssc = sd_ssc_init(un);
11023 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11024 err = EIO;
11025 } else {
11026 err = 0;
11027 }
11028 sd_ssc_fini(ssc);
11029
11030 mutex_enter(SD_MUTEX(un));
11031 un->un_ncmds_in_driver--;
11032 ASSERT(un->un_ncmds_in_driver >= 0);
11033 mutex_exit(SD_MUTEX(un));
11034 if (err != 0)
11035 return (err);
11036 }
11037
11038 /*
11039 * Write requests are restricted to multiples of the system block size.
11040 */
11041 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11042 !un->un_f_enable_rmw)
11043 secmask = un->un_tgt_blocksize - 1;
11044 else
11045 secmask = DEV_BSIZE - 1;
11046
11047 if (uio->uio_loffset & ((offset_t)(secmask))) {
11048 SD_ERROR(SD_LOG_READ_WRITE, un,
11049 "sdwrite: file offset not modulo %d\n",
11050 secmask + 1);
11051 err = EINVAL;
11052 } else if (uio->uio_iov->iov_len & (secmask)) {
11053 SD_ERROR(SD_LOG_READ_WRITE, un,
11054 "sdwrite: transfer length not modulo %d\n",
11055 secmask + 1);
11056 err = EINVAL;
11057 } else {
11058 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11059 }
11060
11061 return (err);
11062 }
11063
11064
11065 /*
11066 * Function: sdaread
11067 *
11068 * Description: Driver's aread(9e) entry point function.
11069 *
11070 * Arguments: dev - device number
11071 * aio - structure pointer describing where data is to be stored
11072 * cred_p - user credential pointer
11073 *
11074 * Return Code: ENXIO
11075 * EIO
11076 * EINVAL
11077 * value returned by aphysio
11078 *
11079 * Context: Kernel thread context.
11080 */
11081 /* ARGSUSED */
11082 static int
11083 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11084 {
11085 struct sd_lun *un = NULL;
11086 struct uio *uio = aio->aio_uio;
11087 int secmask;
11088 int err = 0;
11089 sd_ssc_t *ssc;
11090
11091 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11092 return (ENXIO);
11093 }
11094
11095 ASSERT(!mutex_owned(SD_MUTEX(un)));
11096
11097 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11098 mutex_enter(SD_MUTEX(un));
11099 /*
11100 * Because the call to sd_ready_and_valid will issue I/O we
11101 * must wait here if either the device is suspended or
11102 * if it's power level is changing.
11103 */
11104 while ((un->un_state == SD_STATE_SUSPENDED) ||
11105 (un->un_state == SD_STATE_PM_CHANGING)) {
11106 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11107 }
11108 un->un_ncmds_in_driver++;
11109 mutex_exit(SD_MUTEX(un));
11110
11111 /* Initialize sd_ssc_t for internal uscsi commands */
11112 ssc = sd_ssc_init(un);
11113 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11114 err = EIO;
11115 } else {
11116 err = 0;
11117 }
11118 sd_ssc_fini(ssc);
11119
11120 mutex_enter(SD_MUTEX(un));
11121 un->un_ncmds_in_driver--;
11122 ASSERT(un->un_ncmds_in_driver >= 0);
11123 mutex_exit(SD_MUTEX(un));
11124 if (err != 0)
11125 return (err);
11126 }
11127
11128 /*
11129 * Read requests are restricted to multiples of the system block size.
11130 */
11131 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11132 !un->un_f_enable_rmw)
11133 secmask = un->un_tgt_blocksize - 1;
11134 else
11135 secmask = DEV_BSIZE - 1;
11136
11137 if (uio->uio_loffset & ((offset_t)(secmask))) {
11138 SD_ERROR(SD_LOG_READ_WRITE, un,
11139 "sdaread: file offset not modulo %d\n",
11140 secmask + 1);
11141 err = EINVAL;
11142 } else if (uio->uio_iov->iov_len & (secmask)) {
11143 SD_ERROR(SD_LOG_READ_WRITE, un,
11144 "sdaread: transfer length not modulo %d\n",
11145 secmask + 1);
11146 err = EINVAL;
11147 } else {
11148 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11149 }
11150
11151 return (err);
11152 }
11153
11154
11155 /*
11156 * Function: sdawrite
11157 *
11158 * Description: Driver's awrite(9e) entry point function.
11159 *
11160 * Arguments: dev - device number
11161 * aio - structure pointer describing where data is stored
11162 * cred_p - user credential pointer
11163 *
11164 * Return Code: ENXIO
11165 * EIO
11166 * EINVAL
11167 * value returned by aphysio
11168 *
11169 * Context: Kernel thread context.
11170 */
11171 /* ARGSUSED */
11172 static int
11173 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11174 {
11175 struct sd_lun *un = NULL;
11176 struct uio *uio = aio->aio_uio;
11177 int secmask;
11178 int err = 0;
11179 sd_ssc_t *ssc;
11180
11181 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11182 return (ENXIO);
11183 }
11184
11185 ASSERT(!mutex_owned(SD_MUTEX(un)));
11186
11187 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11188 mutex_enter(SD_MUTEX(un));
11189 /*
11190 * Because the call to sd_ready_and_valid will issue I/O we
11191 * must wait here if either the device is suspended or
11192 * if it's power level is changing.
11193 */
11194 while ((un->un_state == SD_STATE_SUSPENDED) ||
11195 (un->un_state == SD_STATE_PM_CHANGING)) {
11196 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11197 }
11198 un->un_ncmds_in_driver++;
11199 mutex_exit(SD_MUTEX(un));
11200
11201 /* Initialize sd_ssc_t for internal uscsi commands */
11202 ssc = sd_ssc_init(un);
11203 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11204 err = EIO;
11205 } else {
11206 err = 0;
11207 }
11208 sd_ssc_fini(ssc);
11209
11210 mutex_enter(SD_MUTEX(un));
11211 un->un_ncmds_in_driver--;
11212 ASSERT(un->un_ncmds_in_driver >= 0);
11213 mutex_exit(SD_MUTEX(un));
11214 if (err != 0)
11215 return (err);
11216 }
11217
11218 /*
11219 * Write requests are restricted to multiples of the system block size.
11220 */
11221 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11222 !un->un_f_enable_rmw)
11223 secmask = un->un_tgt_blocksize - 1;
11224 else
11225 secmask = DEV_BSIZE - 1;
11226
11227 if (uio->uio_loffset & ((offset_t)(secmask))) {
11228 SD_ERROR(SD_LOG_READ_WRITE, un,
11229 "sdawrite: file offset not modulo %d\n",
11230 secmask + 1);
11231 err = EINVAL;
11232 } else if (uio->uio_iov->iov_len & (secmask)) {
11233 SD_ERROR(SD_LOG_READ_WRITE, un,
11234 "sdawrite: transfer length not modulo %d\n",
11235 secmask + 1);
11236 err = EINVAL;
11237 } else {
11238 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11239 }
11240
11241 return (err);
11242 }
11243
11244
11245
11246
11247
11248 /*
11249 * Driver IO processing follows the following sequence:
11250 *
11251 * sdioctl(9E) sdstrategy(9E) biodone(9F)
11252 * | | ^
11253 * v v |
11254 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+
11255 * | | | |
11256 * v | | |
11257 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone()
11258 * | | ^ ^
11259 * v v | |
11260 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | |
11261 * | | | |
11262 * +---+ | +------------+ +-------+
11263 * | | | |
11264 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11265 * | v | |
11266 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() |
11267 * | | ^ |
11268 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11269 * | v | |
11270 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() |
11271 * | | ^ |
11272 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11273 * | v | |
11274 * | sd_checksum_iostart() sd_checksum_iodone() |
11275 * | | ^ |
11276 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+
11277 * | v | |
11278 * | sd_pm_iostart() sd_pm_iodone() |
11279 * | | ^ |
11280 * | | | |
11281 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+
11282 * | ^
11283 * v |
11284 * sd_core_iostart() |
11285 * | |
11286 * | +------>(*destroypkt)()
11287 * +-> sd_start_cmds() <-+ | |
11288 * | | | v
11289 * | | | scsi_destroy_pkt(9F)
11290 * | | |
11291 * +->(*initpkt)() +- sdintr()
11292 * | | | |
11293 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx()
11294 * | +-> scsi_setup_cdb(9F) |
11295 * | |
11296 * +--> scsi_transport(9F) |
11297 * | |
11298 * +----> SCSA ---->+
11299 *
11300 *
11301 * This code is based upon the following presumptions:
11302 *
11303 * - iostart and iodone functions operate on buf(9S) structures. These
11304 * functions perform the necessary operations on the buf(9S) and pass
11305 * them along to the next function in the chain by using the macros
11306 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11307 * (for iodone side functions).
11308 *
11309 * - The iostart side functions may sleep. The iodone side functions
11310 * are called under interrupt context and may NOT sleep. Therefore
11311 * iodone side functions also may not call iostart side functions.
11312 * (NOTE: iostart side functions should NOT sleep for memory, as
11313 * this could result in deadlock.)
11314 *
11315 * - An iostart side function may call its corresponding iodone side
11316 * function directly (if necessary).
11317 *
11318 * - In the event of an error, an iostart side function can return a buf(9S)
11319 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11320 * b_error in the usual way of course).
11321 *
11322 * - The taskq mechanism may be used by the iodone side functions to dispatch
11323 * requests to the iostart side functions. The iostart side functions in
11324 * this case would be called under the context of a taskq thread, so it's
11325 * OK for them to block/sleep/spin in this case.
11326 *
11327 * - iostart side functions may allocate "shadow" buf(9S) structs and
11328 * pass them along to the next function in the chain. The corresponding
11329 * iodone side functions must coalesce the "shadow" bufs and return
11330 * the "original" buf to the next higher layer.
11331 *
11332 * - The b_private field of the buf(9S) struct holds a pointer to
11333 * an sd_xbuf struct, which contains information needed to
11334 * construct the scsi_pkt for the command.
11335 *
11336 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11337 * layer must acquire & release the SD_MUTEX(un) as needed.
11338 */
11339
11340
11341 /*
11342 * Create taskq for all targets in the system. This is created at
11343 * _init(9E) and destroyed at _fini(9E).
11344 *
11345 * Note: here we set the minalloc to a reasonably high number to ensure that
11346 * we will have an adequate supply of task entries available at interrupt time.
11347 * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11348 * sd_create_taskq(). Since we do not want to sleep for allocations at
11349 * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11350 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11351 * requests any one instant in time.
11352 */
11353 #define SD_TASKQ_NUMTHREADS 8
11354 #define SD_TASKQ_MINALLOC 256
11355 #define SD_TASKQ_MAXALLOC 256
11356
11357 static taskq_t *sd_tq = NULL;
11358 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11359
11360 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11361 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11362
11363 /*
11364 * The following task queue is being created for the write part of
11365 * read-modify-write of non-512 block size devices.
11366 * Limit the number of threads to 1 for now. This number has been chosen
11367 * considering the fact that it applies only to dvd ram drives/MO drives
11368 * currently. Performance for which is not main criteria at this stage.
11369 * Note: It needs to be explored if we can use a single taskq in future
11370 */
11371 #define SD_WMR_TASKQ_NUMTHREADS 1
11372 static taskq_t *sd_wmr_tq = NULL;
11373 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11374
11375 /*
11376 * Function: sd_taskq_create
11377 *
11378 * Description: Create taskq thread(s) and preallocate task entries
11379 *
11380 * Return Code: Returns a pointer to the allocated taskq_t.
11381 *
11382 * Context: Can sleep. Requires blockable context.
11383 *
11384 * Notes: - The taskq() facility currently is NOT part of the DDI.
11385 * (definitely NOT recommeded for 3rd-party drivers!) :-)
11386 * - taskq_create() will block for memory, also it will panic
11387 * if it cannot create the requested number of threads.
11388 * - Currently taskq_create() creates threads that cannot be
11389 * swapped.
11390 * - We use TASKQ_PREPOPULATE to ensure we have an adequate
11391 * supply of taskq entries at interrupt time (ie, so that we
11392 * do not have to sleep for memory)
11393 */
11394
11395 static void
11396 sd_taskq_create(void)
11397 {
11398 char taskq_name[TASKQ_NAMELEN];
11399
11400 ASSERT(sd_tq == NULL);
11401 ASSERT(sd_wmr_tq == NULL);
11402
11403 (void) snprintf(taskq_name, sizeof (taskq_name),
11404 "%s_drv_taskq", sd_label);
11405 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11406 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11407 TASKQ_PREPOPULATE));
11408
11409 (void) snprintf(taskq_name, sizeof (taskq_name),
11410 "%s_rmw_taskq", sd_label);
11411 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11412 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11413 TASKQ_PREPOPULATE));
11414 }
11415
11416
11417 /*
11418 * Function: sd_taskq_delete
11419 *
11420 * Description: Complementary cleanup routine for sd_taskq_create().
11421 *
11422 * Context: Kernel thread context.
11423 */
11424
11425 static void
11426 sd_taskq_delete(void)
11427 {
11428 ASSERT(sd_tq != NULL);
11429 ASSERT(sd_wmr_tq != NULL);
11430 taskq_destroy(sd_tq);
11431 taskq_destroy(sd_wmr_tq);
11432 sd_tq = NULL;
11433 sd_wmr_tq = NULL;
11434 }
11435
11436
11437 /*
11438 * Function: sdstrategy
11439 *
11440 * Description: Driver's strategy (9E) entry point function.
11441 *
11442 * Arguments: bp - pointer to buf(9S)
11443 *
11444 * Return Code: Always returns zero
11445 *
11446 * Context: Kernel thread context.
11447 */
11448
11449 static int
11450 sdstrategy(struct buf *bp)
11451 {
11452 struct sd_lun *un;
11453
11454 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11455 if (un == NULL) {
11456 bioerror(bp, EIO);
11457 bp->b_resid = bp->b_bcount;
11458 biodone(bp);
11459 return (0);
11460 }
11461
11462 /* As was done in the past, fail new cmds. if state is dumping. */
11463 if (un->un_state == SD_STATE_DUMPING) {
11464 bioerror(bp, ENXIO);
11465 bp->b_resid = bp->b_bcount;
11466 biodone(bp);
11467 return (0);
11468 }
11469
11470 ASSERT(!mutex_owned(SD_MUTEX(un)));
11471
11472 /*
11473 * Commands may sneak in while we released the mutex in
11474 * DDI_SUSPEND, we should block new commands. However, old
11475 * commands that are still in the driver at this point should
11476 * still be allowed to drain.
11477 */
11478 mutex_enter(SD_MUTEX(un));
11479 /*
11480 * Must wait here if either the device is suspended or
11481 * if it's power level is changing.
11482 */
11483 while ((un->un_state == SD_STATE_SUSPENDED) ||
11484 (un->un_state == SD_STATE_PM_CHANGING)) {
11485 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11486 }
11487
11488 un->un_ncmds_in_driver++;
11489
11490 /*
11491 * atapi: Since we are running the CD for now in PIO mode we need to
11492 * call bp_mapin here to avoid bp_mapin called interrupt context under
11493 * the HBA's init_pkt routine.
11494 */
11495 if (un->un_f_cfg_is_atapi == TRUE) {
11496 mutex_exit(SD_MUTEX(un));
11497 bp_mapin(bp);
11498 mutex_enter(SD_MUTEX(un));
11499 }
11500 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11501 un->un_ncmds_in_driver);
11502
11503 if (bp->b_flags & B_WRITE)
11504 un->un_f_sync_cache_required = TRUE;
11505
11506 mutex_exit(SD_MUTEX(un));
11507
11508 /*
11509 * This will (eventually) allocate the sd_xbuf area and
11510 * call sd_xbuf_strategy(). We just want to return the
11511 * result of ddi_xbuf_qstrategy so that we have an opt-
11512 * imized tail call which saves us a stack frame.
11513 */
11514 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11515 }
11516
11517
11518 /*
11519 * Function: sd_xbuf_strategy
11520 *
11521 * Description: Function for initiating IO operations via the
11522 * ddi_xbuf_qstrategy() mechanism.
11523 *
11524 * Context: Kernel thread context.
11525 */
11526
11527 static void
11528 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11529 {
11530 struct sd_lun *un = arg;
11531
11532 ASSERT(bp != NULL);
11533 ASSERT(xp != NULL);
11534 ASSERT(un != NULL);
11535 ASSERT(!mutex_owned(SD_MUTEX(un)));
11536
11537 /*
11538 * Initialize the fields in the xbuf and save a pointer to the
11539 * xbuf in bp->b_private.
11540 */
11541 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11542
11543 /* Send the buf down the iostart chain */
11544 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11545 }
11546
11547
11548 /*
11549 * Function: sd_xbuf_init
11550 *
11551 * Description: Prepare the given sd_xbuf struct for use.
11552 *
11553 * Arguments: un - ptr to softstate
11554 * bp - ptr to associated buf(9S)
11555 * xp - ptr to associated sd_xbuf
11556 * chain_type - IO chain type to use:
11557 * SD_CHAIN_NULL
11558 * SD_CHAIN_BUFIO
11559 * SD_CHAIN_USCSI
11560 * SD_CHAIN_DIRECT
11561 * SD_CHAIN_DIRECT_PRIORITY
11562 * pktinfop - ptr to private data struct for scsi_pkt(9S)
11563 * initialization; may be NULL if none.
11564 *
11565 * Context: Kernel thread context
11566 */
11567
11568 static void
11569 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11570 uchar_t chain_type, void *pktinfop)
11571 {
11572 int index;
11573
11574 ASSERT(un != NULL);
11575 ASSERT(bp != NULL);
11576 ASSERT(xp != NULL);
11577
11578 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11579 bp, chain_type);
11580
11581 xp->xb_un = un;
11582 xp->xb_pktp = NULL;
11583 xp->xb_pktinfo = pktinfop;
11584 xp->xb_private = bp->b_private;
11585 xp->xb_blkno = (daddr_t)bp->b_blkno;
11586
11587 /*
11588 * Set up the iostart and iodone chain indexes in the xbuf, based
11589 * upon the specified chain type to use.
11590 */
11591 switch (chain_type) {
11592 case SD_CHAIN_NULL:
11593 /*
11594 * Fall thru to just use the values for the buf type, even
11595 * tho for the NULL chain these values will never be used.
11596 */
11597 /* FALLTHRU */
11598 case SD_CHAIN_BUFIO:
11599 index = un->un_buf_chain_type;
11600 if ((!un->un_f_has_removable_media) &&
11601 (un->un_tgt_blocksize != 0) &&
11602 (un->un_tgt_blocksize != DEV_BSIZE ||
11603 un->un_f_enable_rmw)) {
11604 int secmask = 0, blknomask = 0;
11605 if (un->un_f_enable_rmw) {
11606 blknomask =
11607 (un->un_phy_blocksize / DEV_BSIZE) - 1;
11608 secmask = un->un_phy_blocksize - 1;
11609 } else {
11610 blknomask =
11611 (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11612 secmask = un->un_tgt_blocksize - 1;
11613 }
11614
11615 if ((bp->b_lblkno & (blknomask)) ||
11616 (bp->b_bcount & (secmask))) {
11617 if ((un->un_f_rmw_type !=
11618 SD_RMW_TYPE_RETURN_ERROR) ||
11619 un->un_f_enable_rmw) {
11620 if (un->un_f_pm_is_enabled == FALSE)
11621 index =
11622 SD_CHAIN_INFO_MSS_DSK_NO_PM;
11623 else
11624 index =
11625 SD_CHAIN_INFO_MSS_DISK;
11626 }
11627 }
11628 }
11629 break;
11630 case SD_CHAIN_USCSI:
11631 index = un->un_uscsi_chain_type;
11632 break;
11633 case SD_CHAIN_DIRECT:
11634 index = un->un_direct_chain_type;
11635 break;
11636 case SD_CHAIN_DIRECT_PRIORITY:
11637 index = un->un_priority_chain_type;
11638 break;
11639 default:
11640 /* We're really broken if we ever get here... */
11641 panic("sd_xbuf_init: illegal chain type!");
11642 /*NOTREACHED*/
11643 }
11644
11645 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11646 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11647
11648 /*
11649 * It might be a bit easier to simply bzero the entire xbuf above,
11650 * but it turns out that since we init a fair number of members anyway,
11651 * we save a fair number cycles by doing explicit assignment of zero.
11652 */
11653 xp->xb_pkt_flags = 0;
11654 xp->xb_dma_resid = 0;
11655 xp->xb_retry_count = 0;
11656 xp->xb_victim_retry_count = 0;
11657 xp->xb_ua_retry_count = 0;
11658 xp->xb_nr_retry_count = 0;
11659 xp->xb_sense_bp = NULL;
11660 xp->xb_sense_status = 0;
11661 xp->xb_sense_state = 0;
11662 xp->xb_sense_resid = 0;
11663 xp->xb_ena = 0;
11664
11665 bp->b_private = xp;
11666 bp->b_flags &= ~(B_DONE | B_ERROR);
11667 bp->b_resid = 0;
11668 bp->av_forw = NULL;
11669 bp->av_back = NULL;
11670 bioerror(bp, 0);
11671
11672 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11673 }
11674
11675
11676 /*
11677 * Function: sd_uscsi_strategy
11678 *
11679 * Description: Wrapper for calling into the USCSI chain via physio(9F)
11680 *
11681 * Arguments: bp - buf struct ptr
11682 *
11683 * Return Code: Always returns 0
11684 *
11685 * Context: Kernel thread context
11686 */
11687
11688 static int
11689 sd_uscsi_strategy(struct buf *bp)
11690 {
11691 struct sd_lun *un;
11692 struct sd_uscsi_info *uip;
11693 struct sd_xbuf *xp;
11694 uchar_t chain_type;
11695 uchar_t cmd;
11696
11697 ASSERT(bp != NULL);
11698
11699 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11700 if (un == NULL) {
11701 bioerror(bp, EIO);
11702 bp->b_resid = bp->b_bcount;
11703 biodone(bp);
11704 return (0);
11705 }
11706
11707 ASSERT(!mutex_owned(SD_MUTEX(un)));
11708
11709 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11710
11711 /*
11712 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11713 */
11714 ASSERT(bp->b_private != NULL);
11715 uip = (struct sd_uscsi_info *)bp->b_private;
11716 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11717
11718 mutex_enter(SD_MUTEX(un));
11719 /*
11720 * atapi: Since we are running the CD for now in PIO mode we need to
11721 * call bp_mapin here to avoid bp_mapin called interrupt context under
11722 * the HBA's init_pkt routine.
11723 */
11724 if (un->un_f_cfg_is_atapi == TRUE) {
11725 mutex_exit(SD_MUTEX(un));
11726 bp_mapin(bp);
11727 mutex_enter(SD_MUTEX(un));
11728 }
11729 un->un_ncmds_in_driver++;
11730 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11731 un->un_ncmds_in_driver);
11732
11733 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11734 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11735 un->un_f_sync_cache_required = TRUE;
11736
11737 mutex_exit(SD_MUTEX(un));
11738
11739 switch (uip->ui_flags) {
11740 case SD_PATH_DIRECT:
11741 chain_type = SD_CHAIN_DIRECT;
11742 break;
11743 case SD_PATH_DIRECT_PRIORITY:
11744 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11745 break;
11746 default:
11747 chain_type = SD_CHAIN_USCSI;
11748 break;
11749 }
11750
11751 /*
11752 * We may allocate extra buf for external USCSI commands. If the
11753 * application asks for bigger than 20-byte sense data via USCSI,
11754 * SCSA layer will allocate 252 bytes sense buf for that command.
11755 */
11756 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11757 SENSE_LENGTH) {
11758 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11759 MAX_SENSE_LENGTH, KM_SLEEP);
11760 } else {
11761 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11762 }
11763
11764 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11765
11766 /* Use the index obtained within xbuf_init */
11767 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11768
11769 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11770
11771 return (0);
11772 }
11773
11774 /*
11775 * Function: sd_send_scsi_cmd
11776 *
11777 * Description: Runs a USCSI command for user (when called thru sdioctl),
11778 * or for the driver
11779 *
11780 * Arguments: dev - the dev_t for the device
11781 * incmd - ptr to a valid uscsi_cmd struct
11782 * flag - bit flag, indicating open settings, 32/64 bit type
11783 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11784 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11785 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11786 * to use the USCSI "direct" chain and bypass the normal
11787 * command waitq.
11788 *
11789 * Return Code: 0 - successful completion of the given command
11790 * EIO - scsi_uscsi_handle_command() failed
11791 * ENXIO - soft state not found for specified dev
11792 * EINVAL
11793 * EFAULT - copyin/copyout error
11794 * return code of scsi_uscsi_handle_command():
11795 * EIO
11796 * ENXIO
11797 * EACCES
11798 *
11799 * Context: Waits for command to complete. Can sleep.
11800 */
11801
11802 static int
11803 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11804 enum uio_seg dataspace, int path_flag)
11805 {
11806 struct sd_lun *un;
11807 sd_ssc_t *ssc;
11808 int rval;
11809
11810 un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11811 if (un == NULL) {
11812 return (ENXIO);
11813 }
11814
11815 /*
11816 * Using sd_ssc_send to handle uscsi cmd
11817 */
11818 ssc = sd_ssc_init(un);
11819 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11820 sd_ssc_fini(ssc);
11821
11822 return (rval);
11823 }
11824
11825 /*
11826 * Function: sd_ssc_init
11827 *
11828 * Description: Uscsi end-user call this function to initialize necessary
11829 * fields, such as uscsi_cmd and sd_uscsi_info struct.
11830 *
11831 * The return value of sd_send_scsi_cmd will be treated as a
11832 * fault in various conditions. Even it is not Zero, some
11833 * callers may ignore the return value. That is to say, we can
11834 * not make an accurate assessment in sdintr, since if a
11835 * command is failed in sdintr it does not mean the caller of
11836 * sd_send_scsi_cmd will treat it as a real failure.
11837 *
11838 * To avoid printing too many error logs for a failed uscsi
11839 * packet that the caller may not treat it as a failure, the
11840 * sd will keep silent for handling all uscsi commands.
11841 *
11842 * During detach->attach and attach-open, for some types of
11843 * problems, the driver should be providing information about
11844 * the problem encountered. Device use USCSI_SILENT, which
11845 * suppresses all driver information. The result is that no
11846 * information about the problem is available. Being
11847 * completely silent during this time is inappropriate. The
11848 * driver needs a more selective filter than USCSI_SILENT, so
11849 * that information related to faults is provided.
11850 *
11851 * To make the accurate accessment, the caller of
11852 * sd_send_scsi_USCSI_CMD should take the ownership and
11853 * get necessary information to print error messages.
11854 *
11855 * If we want to print necessary info of uscsi command, we need to
11856 * keep the uscsi_cmd and sd_uscsi_info till we can make the
11857 * assessment. We use sd_ssc_init to alloc necessary
11858 * structs for sending an uscsi command and we are also
11859 * responsible for free the memory by calling
11860 * sd_ssc_fini.
11861 *
11862 * The calling secquences will look like:
11863 * sd_ssc_init->
11864 *
11865 * ...
11866 *
11867 * sd_send_scsi_USCSI_CMD->
11868 * sd_ssc_send-> - - - sdintr
11869 * ...
11870 *
11871 * if we think the return value should be treated as a
11872 * failure, we make the accessment here and print out
11873 * necessary by retrieving uscsi_cmd and sd_uscsi_info'
11874 *
11875 * ...
11876 *
11877 * sd_ssc_fini
11878 *
11879 *
11880 * Arguments: un - pointer to driver soft state (unit) structure for this
11881 * target.
11882 *
11883 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11884 * uscsi_cmd and sd_uscsi_info.
11885 * NULL - if can not alloc memory for sd_ssc_t struct
11886 *
11887 * Context: Kernel Thread.
11888 */
11889 static sd_ssc_t *
11890 sd_ssc_init(struct sd_lun *un)
11891 {
11892 sd_ssc_t *ssc;
11893 struct uscsi_cmd *ucmdp;
11894 struct sd_uscsi_info *uip;
11895
11896 ASSERT(un != NULL);
11897 ASSERT(!mutex_owned(SD_MUTEX(un)));
11898
11899 /*
11900 * Allocate sd_ssc_t structure
11901 */
11902 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11903
11904 /*
11905 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11906 */
11907 ucmdp = scsi_uscsi_alloc();
11908
11909 /*
11910 * Allocate sd_uscsi_info structure
11911 */
11912 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11913
11914 ssc->ssc_uscsi_cmd = ucmdp;
11915 ssc->ssc_uscsi_info = uip;
11916 ssc->ssc_un = un;
11917
11918 return (ssc);
11919 }
11920
11921 /*
11922 * Function: sd_ssc_fini
11923 *
11924 * Description: To free sd_ssc_t and it's hanging off
11925 *
11926 * Arguments: ssc - struct pointer of sd_ssc_t.
11927 */
11928 static void
11929 sd_ssc_fini(sd_ssc_t *ssc)
11930 {
11931 scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11932
11933 if (ssc->ssc_uscsi_info != NULL) {
11934 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11935 ssc->ssc_uscsi_info = NULL;
11936 }
11937
11938 kmem_free(ssc, sizeof (sd_ssc_t));
11939 ssc = NULL;
11940 }
11941
11942 /*
11943 * Function: sd_ssc_send
11944 *
11945 * Description: Runs a USCSI command for user when called through sdioctl,
11946 * or for the driver.
11947 *
11948 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11949 * sd_uscsi_info in.
11950 * incmd - ptr to a valid uscsi_cmd struct
11951 * flag - bit flag, indicating open settings, 32/64 bit type
11952 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11953 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11954 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11955 * to use the USCSI "direct" chain and bypass the normal
11956 * command waitq.
11957 *
11958 * Return Code: 0 - successful completion of the given command
11959 * EIO - scsi_uscsi_handle_command() failed
11960 * ENXIO - soft state not found for specified dev
11961 * ECANCELED - command cancelled due to low power
11962 * EINVAL
11963 * EFAULT - copyin/copyout error
11964 * return code of scsi_uscsi_handle_command():
11965 * EIO
11966 * ENXIO
11967 * EACCES
11968 *
11969 * Context: Kernel Thread;
11970 * Waits for command to complete. Can sleep.
11971 */
11972 static int
11973 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11974 enum uio_seg dataspace, int path_flag)
11975 {
11976 struct sd_uscsi_info *uip;
11977 struct uscsi_cmd *uscmd;
11978 struct sd_lun *un;
11979 dev_t dev;
11980
11981 int format = 0;
11982 int rval;
11983
11984 ASSERT(ssc != NULL);
11985 un = ssc->ssc_un;
11986 ASSERT(un != NULL);
11987 uscmd = ssc->ssc_uscsi_cmd;
11988 ASSERT(uscmd != NULL);
11989 ASSERT(!mutex_owned(SD_MUTEX(un)));
11990 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11991 /*
11992 * If enter here, it indicates that the previous uscsi
11993 * command has not been processed by sd_ssc_assessment.
11994 * This is violating our rules of FMA telemetry processing.
11995 * We should print out this message and the last undisposed
11996 * uscsi command.
11997 */
11998 if (uscmd->uscsi_cdb != NULL) {
11999 SD_INFO(SD_LOG_SDTEST, un,
12000 "sd_ssc_send is missing the alternative "
12001 "sd_ssc_assessment when running command 0x%x.\n",
12002 uscmd->uscsi_cdb[0]);
12003 }
12004 /*
12005 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
12006 * the initial status.
12007 */
12008 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12009 }
12010
12011 /*
12012 * We need to make sure sd_ssc_send will have sd_ssc_assessment
12013 * followed to avoid missing FMA telemetries.
12014 */
12015 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
12016
12017 /*
12018 * if USCSI_PMFAILFAST is set and un is in low power, fail the
12019 * command immediately.
12020 */
12021 mutex_enter(SD_MUTEX(un));
12022 mutex_enter(&un->un_pm_mutex);
12023 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
12024 SD_DEVICE_IS_IN_LOW_POWER(un)) {
12025 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
12026 "un:0x%p is in low power\n", un);
12027 mutex_exit(&un->un_pm_mutex);
12028 mutex_exit(SD_MUTEX(un));
12029 return (ECANCELED);
12030 }
12031 mutex_exit(&un->un_pm_mutex);
12032 mutex_exit(SD_MUTEX(un));
12033
12034 #ifdef SDDEBUG
12035 switch (dataspace) {
12036 case UIO_USERSPACE:
12037 SD_TRACE(SD_LOG_IO, un,
12038 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12039 break;
12040 case UIO_SYSSPACE:
12041 SD_TRACE(SD_LOG_IO, un,
12042 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12043 break;
12044 default:
12045 SD_TRACE(SD_LOG_IO, un,
12046 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12047 break;
12048 }
12049 #endif
12050
12051 rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12052 SD_ADDRESS(un), &uscmd);
12053 if (rval != 0) {
12054 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12055 "scsi_uscsi_alloc_and_copyin failed\n", un);
12056 return (rval);
12057 }
12058
12059 if ((uscmd->uscsi_cdb != NULL) &&
12060 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12061 mutex_enter(SD_MUTEX(un));
12062 un->un_f_format_in_progress = TRUE;
12063 mutex_exit(SD_MUTEX(un));
12064 format = 1;
12065 }
12066
12067 /*
12068 * Allocate an sd_uscsi_info struct and fill it with the info
12069 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
12070 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
12071 * since we allocate the buf here in this function, we do not
12072 * need to preserve the prior contents of b_private.
12073 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12074 */
12075 uip = ssc->ssc_uscsi_info;
12076 uip->ui_flags = path_flag;
12077 uip->ui_cmdp = uscmd;
12078
12079 /*
12080 * Commands sent with priority are intended for error recovery
12081 * situations, and do not have retries performed.
12082 */
12083 if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12084 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12085 }
12086 uscmd->uscsi_flags &= ~USCSI_NOINTR;
12087
12088 dev = SD_GET_DEV(un);
12089 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12090 sd_uscsi_strategy, NULL, uip);
12091
12092 /*
12093 * mark ssc_flags right after handle_cmd to make sure
12094 * the uscsi has been sent
12095 */
12096 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12097
12098 #ifdef SDDEBUG
12099 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12100 "uscsi_status: 0x%02x uscsi_resid:0x%x\n",
12101 uscmd->uscsi_status, uscmd->uscsi_resid);
12102 if (uscmd->uscsi_bufaddr != NULL) {
12103 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12104 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
12105 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12106 if (dataspace == UIO_SYSSPACE) {
12107 SD_DUMP_MEMORY(un, SD_LOG_IO,
12108 "data", (uchar_t *)uscmd->uscsi_bufaddr,
12109 uscmd->uscsi_buflen, SD_LOG_HEX);
12110 }
12111 }
12112 #endif
12113
12114 if (format == 1) {
12115 mutex_enter(SD_MUTEX(un));
12116 un->un_f_format_in_progress = FALSE;
12117 mutex_exit(SD_MUTEX(un));
12118 }
12119
12120 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12121
12122 return (rval);
12123 }
12124
12125 /*
12126 * Function: sd_ssc_print
12127 *
12128 * Description: Print information available to the console.
12129 *
12130 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12131 * sd_uscsi_info in.
12132 * sd_severity - log level.
12133 * Context: Kernel thread or interrupt context.
12134 */
12135 static void
12136 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12137 {
12138 struct uscsi_cmd *ucmdp;
12139 struct scsi_device *devp;
12140 dev_info_t *devinfo;
12141 uchar_t *sensep;
12142 int senlen;
12143 union scsi_cdb *cdbp;
12144 uchar_t com;
12145 extern struct scsi_key_strings scsi_cmds[];
12146
12147 ASSERT(ssc != NULL);
12148 ASSERT(ssc->ssc_un != NULL);
12149
12150 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12151 return;
12152 ucmdp = ssc->ssc_uscsi_cmd;
12153 devp = SD_SCSI_DEVP(ssc->ssc_un);
12154 devinfo = SD_DEVINFO(ssc->ssc_un);
12155 ASSERT(ucmdp != NULL);
12156 ASSERT(devp != NULL);
12157 ASSERT(devinfo != NULL);
12158 sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12159 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12160 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12161
12162 /* In certain case (like DOORLOCK), the cdb could be NULL. */
12163 if (cdbp == NULL)
12164 return;
12165 /* We don't print log if no sense data available. */
12166 if (senlen == 0)
12167 sensep = NULL;
12168 com = cdbp->scc_cmd;
12169 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12170 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12171 }
12172
12173 /*
12174 * Function: sd_ssc_assessment
12175 *
12176 * Description: We use this function to make an assessment at the point
12177 * where SD driver may encounter a potential error.
12178 *
12179 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12180 * sd_uscsi_info in.
12181 * tp_assess - a hint of strategy for ereport posting.
12182 * Possible values of tp_assess include:
12183 * SD_FMT_IGNORE - we don't post any ereport because we're
12184 * sure that it is ok to ignore the underlying problems.
12185 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12186 * but it might be not correct to ignore the underlying hardware
12187 * error.
12188 * SD_FMT_STATUS_CHECK - we will post an ereport with the
12189 * payload driver-assessment of value "fail" or
12190 * "fatal"(depending on what information we have here). This
12191 * assessment value is usually set when SD driver think there
12192 * is a potential error occurred(Typically, when return value
12193 * of the SCSI command is EIO).
12194 * SD_FMT_STANDARD - we will post an ereport with the payload
12195 * driver-assessment of value "info". This assessment value is
12196 * set when the SCSI command returned successfully and with
12197 * sense data sent back.
12198 *
12199 * Context: Kernel thread.
12200 */
12201 static void
12202 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12203 {
12204 int senlen = 0;
12205 struct uscsi_cmd *ucmdp = NULL;
12206 struct sd_lun *un;
12207
12208 ASSERT(ssc != NULL);
12209 un = ssc->ssc_un;
12210 ASSERT(un != NULL);
12211 ucmdp = ssc->ssc_uscsi_cmd;
12212 ASSERT(ucmdp != NULL);
12213
12214 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12215 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12216 } else {
12217 /*
12218 * If enter here, it indicates that we have a wrong
12219 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12220 * both of which should be called in a pair in case of
12221 * loss of FMA telemetries.
12222 */
12223 if (ucmdp->uscsi_cdb != NULL) {
12224 SD_INFO(SD_LOG_SDTEST, un,
12225 "sd_ssc_assessment is missing the "
12226 "alternative sd_ssc_send when running 0x%x, "
12227 "or there are superfluous sd_ssc_assessment for "
12228 "the same sd_ssc_send.\n",
12229 ucmdp->uscsi_cdb[0]);
12230 }
12231 /*
12232 * Set the ssc_flags to the initial value to avoid passing
12233 * down dirty flags to the following sd_ssc_send function.
12234 */
12235 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12236 return;
12237 }
12238
12239 /*
12240 * Only handle an issued command which is waiting for assessment.
12241 * A command which is not issued will not have
12242 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12243 */
12244 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12245 sd_ssc_print(ssc, SCSI_ERR_INFO);
12246 return;
12247 } else {
12248 /*
12249 * For an issued command, we should clear this flag in
12250 * order to make the sd_ssc_t structure be used off
12251 * multiple uscsi commands.
12252 */
12253 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12254 }
12255
12256 /*
12257 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12258 * commands here. And we should clear the ssc_flags before return.
12259 */
12260 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12261 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12262 return;
12263 }
12264
12265 switch (tp_assess) {
12266 case SD_FMT_IGNORE:
12267 case SD_FMT_IGNORE_COMPROMISE:
12268 break;
12269 case SD_FMT_STATUS_CHECK:
12270 /*
12271 * For a failed command(including the succeeded command
12272 * with invalid data sent back).
12273 */
12274 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12275 break;
12276 case SD_FMT_STANDARD:
12277 /*
12278 * Always for the succeeded commands probably with sense
12279 * data sent back.
12280 * Limitation:
12281 * We can only handle a succeeded command with sense
12282 * data sent back when auto-request-sense is enabled.
12283 */
12284 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12285 ssc->ssc_uscsi_cmd->uscsi_rqresid;
12286 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12287 (un->un_f_arq_enabled == TRUE) &&
12288 senlen > 0 &&
12289 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12290 sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12291 }
12292 break;
12293 default:
12294 /*
12295 * Should not have other type of assessment.
12296 */
12297 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12298 "sd_ssc_assessment got wrong "
12299 "sd_type_assessment %d.\n", tp_assess);
12300 break;
12301 }
12302 /*
12303 * Clear up the ssc_flags before return.
12304 */
12305 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12306 }
12307
12308 /*
12309 * Function: sd_ssc_post
12310 *
12311 * Description: 1. read the driver property to get fm-scsi-log flag.
12312 * 2. print log if fm_log_capable is non-zero.
12313 * 3. call sd_ssc_ereport_post to post ereport if possible.
12314 *
12315 * Context: May be called from kernel thread or interrupt context.
12316 */
12317 static void
12318 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12319 {
12320 struct sd_lun *un;
12321 int sd_severity;
12322
12323 ASSERT(ssc != NULL);
12324 un = ssc->ssc_un;
12325 ASSERT(un != NULL);
12326
12327 /*
12328 * We may enter here from sd_ssc_assessment(for USCSI command) or
12329 * by directly called from sdintr context.
12330 * We don't handle a non-disk drive(CD-ROM, removable media).
12331 * Clear the ssc_flags before return in case we've set
12332 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12333 * driver.
12334 */
12335 if (ISCD(un) || un->un_f_has_removable_media) {
12336 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12337 return;
12338 }
12339
12340 switch (sd_assess) {
12341 case SD_FM_DRV_FATAL:
12342 sd_severity = SCSI_ERR_FATAL;
12343 break;
12344 case SD_FM_DRV_RECOVERY:
12345 sd_severity = SCSI_ERR_RECOVERED;
12346 break;
12347 case SD_FM_DRV_RETRY:
12348 sd_severity = SCSI_ERR_RETRYABLE;
12349 break;
12350 case SD_FM_DRV_NOTICE:
12351 sd_severity = SCSI_ERR_INFO;
12352 break;
12353 default:
12354 sd_severity = SCSI_ERR_UNKNOWN;
12355 }
12356 /* print log */
12357 sd_ssc_print(ssc, sd_severity);
12358
12359 /* always post ereport */
12360 sd_ssc_ereport_post(ssc, sd_assess);
12361 }
12362
12363 /*
12364 * Function: sd_ssc_set_info
12365 *
12366 * Description: Mark ssc_flags and set ssc_info which would be the
12367 * payload of uderr ereport. This function will cause
12368 * sd_ssc_ereport_post to post uderr ereport only.
12369 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12370 * the function will also call SD_ERROR or scsi_log for a
12371 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12372 *
12373 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12374 * sd_uscsi_info in.
12375 * ssc_flags - indicate the sub-category of a uderr.
12376 * comp - this argument is meaningful only when
12377 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12378 * values include:
12379 * > 0, SD_ERROR is used with comp as the driver logging
12380 * component;
12381 * = 0, scsi-log is used to log error telemetries;
12382 * < 0, no log available for this telemetry.
12383 *
12384 * Context: Kernel thread or interrupt context
12385 */
12386 static void
12387 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12388 {
12389 va_list ap;
12390
12391 ASSERT(ssc != NULL);
12392 ASSERT(ssc->ssc_un != NULL);
12393
12394 ssc->ssc_flags |= ssc_flags;
12395 va_start(ap, fmt);
12396 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12397 va_end(ap);
12398
12399 /*
12400 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12401 * with invalid data sent back. For non-uscsi command, the
12402 * following code will be bypassed.
12403 */
12404 if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12405 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12406 /*
12407 * If the error belong to certain component and we
12408 * do not want it to show up on the console, we
12409 * will use SD_ERROR, otherwise scsi_log is
12410 * preferred.
12411 */
12412 if (comp > 0) {
12413 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12414 } else if (comp == 0) {
12415 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12416 CE_WARN, ssc->ssc_info);
12417 }
12418 }
12419 }
12420 }
12421
12422 /*
12423 * Function: sd_buf_iodone
12424 *
12425 * Description: Frees the sd_xbuf & returns the buf to its originator.
12426 *
12427 * Context: May be called from interrupt context.
12428 */
12429 /* ARGSUSED */
12430 static void
12431 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12432 {
12433 struct sd_xbuf *xp;
12434
12435 ASSERT(un != NULL);
12436 ASSERT(bp != NULL);
12437 ASSERT(!mutex_owned(SD_MUTEX(un)));
12438
12439 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12440
12441 xp = SD_GET_XBUF(bp);
12442 ASSERT(xp != NULL);
12443
12444 /* xbuf is gone after this */
12445 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12446 mutex_enter(SD_MUTEX(un));
12447
12448 /*
12449 * Grab time when the cmd completed.
12450 * This is used for determining if the system has been
12451 * idle long enough to make it idle to the PM framework.
12452 * This is for lowering the overhead, and therefore improving
12453 * performance per I/O operation.
12454 */
12455 un->un_pm_idle_time = ddi_get_time();
12456
12457 un->un_ncmds_in_driver--;
12458 ASSERT(un->un_ncmds_in_driver >= 0);
12459 SD_INFO(SD_LOG_IO, un,
12460 "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12461 un->un_ncmds_in_driver);
12462
12463 mutex_exit(SD_MUTEX(un));
12464 }
12465
12466 biodone(bp); /* bp is gone after this */
12467
12468 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12469 }
12470
12471
12472 /*
12473 * Function: sd_uscsi_iodone
12474 *
12475 * Description: Frees the sd_xbuf & returns the buf to its originator.
12476 *
12477 * Context: May be called from interrupt context.
12478 */
12479 /* ARGSUSED */
12480 static void
12481 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12482 {
12483 struct sd_xbuf *xp;
12484
12485 ASSERT(un != NULL);
12486 ASSERT(bp != NULL);
12487
12488 xp = SD_GET_XBUF(bp);
12489 ASSERT(xp != NULL);
12490 ASSERT(!mutex_owned(SD_MUTEX(un)));
12491
12492 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12493
12494 bp->b_private = xp->xb_private;
12495
12496 mutex_enter(SD_MUTEX(un));
12497
12498 /*
12499 * Grab time when the cmd completed.
12500 * This is used for determining if the system has been
12501 * idle long enough to make it idle to the PM framework.
12502 * This is for lowering the overhead, and therefore improving
12503 * performance per I/O operation.
12504 */
12505 un->un_pm_idle_time = ddi_get_time();
12506
12507 un->un_ncmds_in_driver--;
12508 ASSERT(un->un_ncmds_in_driver >= 0);
12509 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12510 un->un_ncmds_in_driver);
12511
12512 mutex_exit(SD_MUTEX(un));
12513
12514 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12515 SENSE_LENGTH) {
12516 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12517 MAX_SENSE_LENGTH);
12518 } else {
12519 kmem_free(xp, sizeof (struct sd_xbuf));
12520 }
12521
12522 biodone(bp);
12523
12524 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12525 }
12526
12527
12528 /*
12529 * Function: sd_mapblockaddr_iostart
12530 *
12531 * Description: Verify request lies within the partition limits for
12532 * the indicated minor device. Issue "overrun" buf if
12533 * request would exceed partition range. Converts
12534 * partition-relative block address to absolute.
12535 *
12536 * Upon exit of this function:
12537 * 1.I/O is aligned
12538 * xp->xb_blkno represents the absolute sector address
12539 * 2.I/O is misaligned
12540 * xp->xb_blkno represents the absolute logical block address
12541 * based on DEV_BSIZE. The logical block address will be
12542 * converted to physical sector address in sd_mapblocksize_\
12543 * iostart.
12544 * 3.I/O is misaligned but is aligned in "overrun" buf
12545 * xp->xb_blkno represents the absolute logical block address
12546 * based on DEV_BSIZE. The logical block address will be
12547 * converted to physical sector address in sd_mapblocksize_\
12548 * iostart. But no RMW will be issued in this case.
12549 *
12550 * Context: Can sleep
12551 *
12552 * Issues: This follows what the old code did, in terms of accessing
12553 * some of the partition info in the unit struct without holding
12554 * the mutext. This is a general issue, if the partition info
12555 * can be altered while IO is in progress... as soon as we send
12556 * a buf, its partitioning can be invalid before it gets to the
12557 * device. Probably the right fix is to move partitioning out
12558 * of the driver entirely.
12559 */
12560
12561 static void
12562 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12563 {
12564 diskaddr_t nblocks; /* #blocks in the given partition */
12565 daddr_t blocknum; /* Block number specified by the buf */
12566 size_t requested_nblocks;
12567 size_t available_nblocks;
12568 int partition;
12569 diskaddr_t partition_offset;
12570 struct sd_xbuf *xp;
12571 int secmask = 0, blknomask = 0;
12572 ushort_t is_aligned = TRUE;
12573
12574 ASSERT(un != NULL);
12575 ASSERT(bp != NULL);
12576 ASSERT(!mutex_owned(SD_MUTEX(un)));
12577
12578 SD_TRACE(SD_LOG_IO_PARTITION, un,
12579 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12580
12581 xp = SD_GET_XBUF(bp);
12582 ASSERT(xp != NULL);
12583
12584 /*
12585 * If the geometry is not indicated as valid, attempt to access
12586 * the unit & verify the geometry/label. This can be the case for
12587 * removable-media devices, of if the device was opened in
12588 * NDELAY/NONBLOCK mode.
12589 */
12590 partition = SDPART(bp->b_edev);
12591
12592 if (!SD_IS_VALID_LABEL(un)) {
12593 sd_ssc_t *ssc;
12594 /*
12595 * Initialize sd_ssc_t for internal uscsi commands
12596 * In case of potential porformance issue, we need
12597 * to alloc memory only if there is invalid label
12598 */
12599 ssc = sd_ssc_init(un);
12600
12601 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12602 /*
12603 * For removable devices it is possible to start an
12604 * I/O without a media by opening the device in nodelay
12605 * mode. Also for writable CDs there can be many
12606 * scenarios where there is no geometry yet but volume
12607 * manager is trying to issue a read() just because
12608 * it can see TOC on the CD. So do not print a message
12609 * for removables.
12610 */
12611 if (!un->un_f_has_removable_media) {
12612 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12613 "i/o to invalid geometry\n");
12614 }
12615 bioerror(bp, EIO);
12616 bp->b_resid = bp->b_bcount;
12617 SD_BEGIN_IODONE(index, un, bp);
12618
12619 sd_ssc_fini(ssc);
12620 return;
12621 }
12622 sd_ssc_fini(ssc);
12623 }
12624
12625 nblocks = 0;
12626 (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12627 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12628
12629 if (un->un_f_enable_rmw) {
12630 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12631 secmask = un->un_phy_blocksize - 1;
12632 } else {
12633 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12634 secmask = un->un_tgt_blocksize - 1;
12635 }
12636
12637 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12638 is_aligned = FALSE;
12639 }
12640
12641 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12642 /*
12643 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12644 * Convert the logical block number to target's physical sector
12645 * number.
12646 */
12647 if (is_aligned) {
12648 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12649 } else {
12650 switch (un->un_f_rmw_type) {
12651 case SD_RMW_TYPE_RETURN_ERROR:
12652 if (un->un_f_enable_rmw)
12653 break;
12654 else {
12655 bp->b_flags |= B_ERROR;
12656 goto error_exit;
12657 }
12658
12659 case SD_RMW_TYPE_DEFAULT:
12660 mutex_enter(SD_MUTEX(un));
12661 if (!un->un_f_enable_rmw &&
12662 un->un_rmw_msg_timeid == NULL) {
12663 scsi_log(SD_DEVINFO(un), sd_label,
12664 CE_WARN, "I/O request is not "
12665 "aligned with %d disk sector size. "
12666 "It is handled through Read Modify "
12667 "Write but the performance is "
12668 "very low.\n",
12669 un->un_tgt_blocksize);
12670 un->un_rmw_msg_timeid =
12671 timeout(sd_rmw_msg_print_handler,
12672 un, SD_RMW_MSG_PRINT_TIMEOUT);
12673 } else {
12674 un->un_rmw_incre_count ++;
12675 }
12676 mutex_exit(SD_MUTEX(un));
12677 break;
12678
12679 case SD_RMW_TYPE_NO_WARNING:
12680 default:
12681 break;
12682 }
12683
12684 nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12685 partition_offset = SD_TGT2SYSBLOCK(un,
12686 partition_offset);
12687 }
12688 }
12689
12690 /*
12691 * blocknum is the starting block number of the request. At this
12692 * point it is still relative to the start of the minor device.
12693 */
12694 blocknum = xp->xb_blkno;
12695
12696 /*
12697 * Legacy: If the starting block number is one past the last block
12698 * in the partition, do not set B_ERROR in the buf.
12699 */
12700 if (blocknum == nblocks) {
12701 goto error_exit;
12702 }
12703
12704 /*
12705 * Confirm that the first block of the request lies within the
12706 * partition limits. Also the requested number of bytes must be
12707 * a multiple of the system block size.
12708 */
12709 if ((blocknum < 0) || (blocknum >= nblocks) ||
12710 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12711 bp->b_flags |= B_ERROR;
12712 goto error_exit;
12713 }
12714
12715 /*
12716 * If the requsted # blocks exceeds the available # blocks, that
12717 * is an overrun of the partition.
12718 */
12719 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12720 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12721 } else {
12722 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12723 }
12724
12725 available_nblocks = (size_t)(nblocks - blocknum);
12726 ASSERT(nblocks >= blocknum);
12727
12728 if (requested_nblocks > available_nblocks) {
12729 size_t resid;
12730
12731 /*
12732 * Allocate an "overrun" buf to allow the request to proceed
12733 * for the amount of space available in the partition. The
12734 * amount not transferred will be added into the b_resid
12735 * when the operation is complete. The overrun buf
12736 * replaces the original buf here, and the original buf
12737 * is saved inside the overrun buf, for later use.
12738 */
12739 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12740 resid = SD_TGTBLOCKS2BYTES(un,
12741 (offset_t)(requested_nblocks - available_nblocks));
12742 } else {
12743 resid = SD_SYSBLOCKS2BYTES(
12744 (offset_t)(requested_nblocks - available_nblocks));
12745 }
12746
12747 size_t count = bp->b_bcount - resid;
12748 /*
12749 * Note: count is an unsigned entity thus it'll NEVER
12750 * be less than 0 so ASSERT the original values are
12751 * correct.
12752 */
12753 ASSERT(bp->b_bcount >= resid);
12754
12755 bp = sd_bioclone_alloc(bp, count, blocknum,
12756 (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12757 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12758 ASSERT(xp != NULL);
12759 }
12760
12761 /* At this point there should be no residual for this buf. */
12762 ASSERT(bp->b_resid == 0);
12763
12764 /* Convert the block number to an absolute address. */
12765 xp->xb_blkno += partition_offset;
12766
12767 SD_NEXT_IOSTART(index, un, bp);
12768
12769 SD_TRACE(SD_LOG_IO_PARTITION, un,
12770 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12771
12772 return;
12773
12774 error_exit:
12775 bp->b_resid = bp->b_bcount;
12776 SD_BEGIN_IODONE(index, un, bp);
12777 SD_TRACE(SD_LOG_IO_PARTITION, un,
12778 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12779 }
12780
12781
12782 /*
12783 * Function: sd_mapblockaddr_iodone
12784 *
12785 * Description: Completion-side processing for partition management.
12786 *
12787 * Context: May be called under interrupt context
12788 */
12789
12790 static void
12791 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12792 {
12793 /* int partition; */ /* Not used, see below. */
12794 ASSERT(un != NULL);
12795 ASSERT(bp != NULL);
12796 ASSERT(!mutex_owned(SD_MUTEX(un)));
12797
12798 SD_TRACE(SD_LOG_IO_PARTITION, un,
12799 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12800
12801 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12802 /*
12803 * We have an "overrun" buf to deal with...
12804 */
12805 struct sd_xbuf *xp;
12806 struct buf *obp; /* ptr to the original buf */
12807
12808 xp = SD_GET_XBUF(bp);
12809 ASSERT(xp != NULL);
12810
12811 /* Retrieve the pointer to the original buf */
12812 obp = (struct buf *)xp->xb_private;
12813 ASSERT(obp != NULL);
12814
12815 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12816 bioerror(obp, bp->b_error);
12817
12818 sd_bioclone_free(bp);
12819
12820 /*
12821 * Get back the original buf.
12822 * Note that since the restoration of xb_blkno below
12823 * was removed, the sd_xbuf is not needed.
12824 */
12825 bp = obp;
12826 /*
12827 * xp = SD_GET_XBUF(bp);
12828 * ASSERT(xp != NULL);
12829 */
12830 }
12831
12832 /*
12833 * Convert sd->xb_blkno back to a minor-device relative value.
12834 * Note: this has been commented out, as it is not needed in the
12835 * current implementation of the driver (ie, since this function
12836 * is at the top of the layering chains, so the info will be
12837 * discarded) and it is in the "hot" IO path.
12838 *
12839 * partition = getminor(bp->b_edev) & SDPART_MASK;
12840 * xp->xb_blkno -= un->un_offset[partition];
12841 */
12842
12843 SD_NEXT_IODONE(index, un, bp);
12844
12845 SD_TRACE(SD_LOG_IO_PARTITION, un,
12846 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12847 }
12848
12849
12850 /*
12851 * Function: sd_mapblocksize_iostart
12852 *
12853 * Description: Convert between system block size (un->un_sys_blocksize)
12854 * and target block size (un->un_tgt_blocksize).
12855 *
12856 * Context: Can sleep to allocate resources.
12857 *
12858 * Assumptions: A higher layer has already performed any partition validation,
12859 * and converted the xp->xb_blkno to an absolute value relative
12860 * to the start of the device.
12861 *
12862 * It is also assumed that the higher layer has implemented
12863 * an "overrun" mechanism for the case where the request would
12864 * read/write beyond the end of a partition. In this case we
12865 * assume (and ASSERT) that bp->b_resid == 0.
12866 *
12867 * Note: The implementation for this routine assumes the target
12868 * block size remains constant between allocation and transport.
12869 */
12870
12871 static void
12872 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12873 {
12874 struct sd_mapblocksize_info *bsp;
12875 struct sd_xbuf *xp;
12876 offset_t first_byte;
12877 daddr_t start_block, end_block;
12878 daddr_t request_bytes;
12879 ushort_t is_aligned = FALSE;
12880
12881 ASSERT(un != NULL);
12882 ASSERT(bp != NULL);
12883 ASSERT(!mutex_owned(SD_MUTEX(un)));
12884 ASSERT(bp->b_resid == 0);
12885
12886 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12887 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12888
12889 /*
12890 * For a non-writable CD, a write request is an error
12891 */
12892 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12893 (un->un_f_mmc_writable_media == FALSE)) {
12894 bioerror(bp, EIO);
12895 bp->b_resid = bp->b_bcount;
12896 SD_BEGIN_IODONE(index, un, bp);
12897 return;
12898 }
12899
12900 /*
12901 * We do not need a shadow buf if the device is using
12902 * un->un_sys_blocksize as its block size or if bcount == 0.
12903 * In this case there is no layer-private data block allocated.
12904 */
12905 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12906 (bp->b_bcount == 0)) {
12907 goto done;
12908 }
12909
12910 #if defined(__i386) || defined(__amd64)
12911 /* We do not support non-block-aligned transfers for ROD devices */
12912 ASSERT(!ISROD(un));
12913 #endif
12914
12915 xp = SD_GET_XBUF(bp);
12916 ASSERT(xp != NULL);
12917
12918 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12919 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12920 un->un_tgt_blocksize, DEV_BSIZE);
12921 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12922 "request start block:0x%x\n", xp->xb_blkno);
12923 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12924 "request len:0x%x\n", bp->b_bcount);
12925
12926 /*
12927 * Allocate the layer-private data area for the mapblocksize layer.
12928 * Layers are allowed to use the xp_private member of the sd_xbuf
12929 * struct to store the pointer to their layer-private data block, but
12930 * each layer also has the responsibility of restoring the prior
12931 * contents of xb_private before returning the buf/xbuf to the
12932 * higher layer that sent it.
12933 *
12934 * Here we save the prior contents of xp->xb_private into the
12935 * bsp->mbs_oprivate field of our layer-private data area. This value
12936 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12937 * the layer-private area and returning the buf/xbuf to the layer
12938 * that sent it.
12939 *
12940 * Note that here we use kmem_zalloc for the allocation as there are
12941 * parts of the mapblocksize code that expect certain fields to be
12942 * zero unless explicitly set to a required value.
12943 */
12944 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12945 bsp->mbs_oprivate = xp->xb_private;
12946 xp->xb_private = bsp;
12947
12948 /*
12949 * This treats the data on the disk (target) as an array of bytes.
12950 * first_byte is the byte offset, from the beginning of the device,
12951 * to the location of the request. This is converted from a
12952 * un->un_sys_blocksize block address to a byte offset, and then back
12953 * to a block address based upon a un->un_tgt_blocksize block size.
12954 *
12955 * xp->xb_blkno should be absolute upon entry into this function,
12956 * but, but it is based upon partitions that use the "system"
12957 * block size. It must be adjusted to reflect the block size of
12958 * the target.
12959 *
12960 * Note that end_block is actually the block that follows the last
12961 * block of the request, but that's what is needed for the computation.
12962 */
12963 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12964 if (un->un_f_enable_rmw) {
12965 start_block = xp->xb_blkno =
12966 (first_byte / un->un_phy_blocksize) *
12967 (un->un_phy_blocksize / DEV_BSIZE);
12968 end_block = ((first_byte + bp->b_bcount +
12969 un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12970 (un->un_phy_blocksize / DEV_BSIZE);
12971 } else {
12972 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12973 end_block = (first_byte + bp->b_bcount +
12974 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12975 }
12976
12977 /* request_bytes is rounded up to a multiple of the target block size */
12978 request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12979
12980 /*
12981 * See if the starting address of the request and the request
12982 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12983 * then we do not need to allocate a shadow buf to handle the request.
12984 */
12985 if (un->un_f_enable_rmw) {
12986 if (((first_byte % un->un_phy_blocksize) == 0) &&
12987 ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12988 is_aligned = TRUE;
12989 }
12990 } else {
12991 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12992 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12993 is_aligned = TRUE;
12994 }
12995 }
12996
12997 if ((bp->b_flags & B_READ) == 0) {
12998 /*
12999 * Lock the range for a write operation. An aligned request is
13000 * considered a simple write; otherwise the request must be a
13001 * read-modify-write.
13002 */
13003 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
13004 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
13005 }
13006
13007 /*
13008 * Alloc a shadow buf if the request is not aligned. Also, this is
13009 * where the READ command is generated for a read-modify-write. (The
13010 * write phase is deferred until after the read completes.)
13011 */
13012 if (is_aligned == FALSE) {
13013
13014 struct sd_mapblocksize_info *shadow_bsp;
13015 struct sd_xbuf *shadow_xp;
13016 struct buf *shadow_bp;
13017
13018 /*
13019 * Allocate the shadow buf and it associated xbuf. Note that
13020 * after this call the xb_blkno value in both the original
13021 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
13022 * same: absolute relative to the start of the device, and
13023 * adjusted for the target block size. The b_blkno in the
13024 * shadow buf will also be set to this value. We should never
13025 * change b_blkno in the original bp however.
13026 *
13027 * Note also that the shadow buf will always need to be a
13028 * READ command, regardless of whether the incoming command
13029 * is a READ or a WRITE.
13030 */
13031 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13032 xp->xb_blkno,
13033 (int (*)(struct buf *)) sd_mapblocksize_iodone);
13034
13035 shadow_xp = SD_GET_XBUF(shadow_bp);
13036
13037 /*
13038 * Allocate the layer-private data for the shadow buf.
13039 * (No need to preserve xb_private in the shadow xbuf.)
13040 */
13041 shadow_xp->xb_private = shadow_bsp =
13042 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13043
13044 /*
13045 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13046 * to figure out where the start of the user data is (based upon
13047 * the system block size) in the data returned by the READ
13048 * command (which will be based upon the target blocksize). Note
13049 * that this is only really used if the request is unaligned.
13050 */
13051 if (un->un_f_enable_rmw) {
13052 bsp->mbs_copy_offset = (ssize_t)(first_byte -
13053 ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13054 ASSERT((bsp->mbs_copy_offset >= 0) &&
13055 (bsp->mbs_copy_offset < un->un_phy_blocksize));
13056 } else {
13057 bsp->mbs_copy_offset = (ssize_t)(first_byte -
13058 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13059 ASSERT((bsp->mbs_copy_offset >= 0) &&
13060 (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13061 }
13062
13063 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13064
13065 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13066
13067 /* Transfer the wmap (if any) to the shadow buf */
13068 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13069 bsp->mbs_wmp = NULL;
13070
13071 /*
13072 * The shadow buf goes on from here in place of the
13073 * original buf.
13074 */
13075 shadow_bsp->mbs_orig_bp = bp;
13076 bp = shadow_bp;
13077 }
13078
13079 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13080 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13081 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13082 "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13083 request_bytes);
13084 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13085 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13086
13087 done:
13088 SD_NEXT_IOSTART(index, un, bp);
13089
13090 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13091 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13092 }
13093
13094
13095 /*
13096 * Function: sd_mapblocksize_iodone
13097 *
13098 * Description: Completion side processing for block-size mapping.
13099 *
13100 * Context: May be called under interrupt context
13101 */
13102
13103 static void
13104 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13105 {
13106 struct sd_mapblocksize_info *bsp;
13107 struct sd_xbuf *xp;
13108 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */
13109 struct buf *orig_bp; /* ptr to the original buf */
13110 offset_t shadow_end;
13111 offset_t request_end;
13112 offset_t shadow_start;
13113 ssize_t copy_offset;
13114 size_t copy_length;
13115 size_t shortfall;
13116 uint_t is_write; /* TRUE if this bp is a WRITE */
13117 uint_t has_wmap; /* TRUE is this bp has a wmap */
13118
13119 ASSERT(un != NULL);
13120 ASSERT(bp != NULL);
13121
13122 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13123 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13124
13125 /*
13126 * There is no shadow buf or layer-private data if the target is
13127 * using un->un_sys_blocksize as its block size or if bcount == 0.
13128 */
13129 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13130 (bp->b_bcount == 0)) {
13131 goto exit;
13132 }
13133
13134 xp = SD_GET_XBUF(bp);
13135 ASSERT(xp != NULL);
13136
13137 /* Retrieve the pointer to the layer-private data area from the xbuf. */
13138 bsp = xp->xb_private;
13139
13140 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13141 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13142
13143 if (is_write) {
13144 /*
13145 * For a WRITE request we must free up the block range that
13146 * we have locked up. This holds regardless of whether this is
13147 * an aligned write request or a read-modify-write request.
13148 */
13149 sd_range_unlock(un, bsp->mbs_wmp);
13150 bsp->mbs_wmp = NULL;
13151 }
13152
13153 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13154 /*
13155 * An aligned read or write command will have no shadow buf;
13156 * there is not much else to do with it.
13157 */
13158 goto done;
13159 }
13160
13161 orig_bp = bsp->mbs_orig_bp;
13162 ASSERT(orig_bp != NULL);
13163 orig_xp = SD_GET_XBUF(orig_bp);
13164 ASSERT(orig_xp != NULL);
13165 ASSERT(!mutex_owned(SD_MUTEX(un)));
13166
13167 if (!is_write && has_wmap) {
13168 /*
13169 * A READ with a wmap means this is the READ phase of a
13170 * read-modify-write. If an error occurred on the READ then
13171 * we do not proceed with the WRITE phase or copy any data.
13172 * Just release the write maps and return with an error.
13173 */
13174 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13175 orig_bp->b_resid = orig_bp->b_bcount;
13176 bioerror(orig_bp, bp->b_error);
13177 sd_range_unlock(un, bsp->mbs_wmp);
13178 goto freebuf_done;
13179 }
13180 }
13181
13182 /*
13183 * Here is where we set up to copy the data from the shadow buf
13184 * into the space associated with the original buf.
13185 *
13186 * To deal with the conversion between block sizes, these
13187 * computations treat the data as an array of bytes, with the
13188 * first byte (byte 0) corresponding to the first byte in the
13189 * first block on the disk.
13190 */
13191
13192 /*
13193 * shadow_start and shadow_len indicate the location and size of
13194 * the data returned with the shadow IO request.
13195 */
13196 if (un->un_f_enable_rmw) {
13197 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13198 } else {
13199 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13200 }
13201 shadow_end = shadow_start + bp->b_bcount - bp->b_resid;
13202
13203 /*
13204 * copy_offset gives the offset (in bytes) from the start of the first
13205 * block of the READ request to the beginning of the data. We retrieve
13206 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13207 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13208 * data to be copied (in bytes).
13209 */
13210 copy_offset = bsp->mbs_copy_offset;
13211 if (un->un_f_enable_rmw) {
13212 ASSERT((copy_offset >= 0) &&
13213 (copy_offset < un->un_phy_blocksize));
13214 } else {
13215 ASSERT((copy_offset >= 0) &&
13216 (copy_offset < un->un_tgt_blocksize));
13217 }
13218
13219 copy_length = orig_bp->b_bcount;
13220 request_end = shadow_start + copy_offset + orig_bp->b_bcount;
13221
13222 /*
13223 * Set up the resid and error fields of orig_bp as appropriate.
13224 */
13225 if (shadow_end >= request_end) {
13226 /* We got all the requested data; set resid to zero */
13227 orig_bp->b_resid = 0;
13228 } else {
13229 /*
13230 * We failed to get enough data to fully satisfy the original
13231 * request. Just copy back whatever data we got and set
13232 * up the residual and error code as required.
13233 *
13234 * 'shortfall' is the amount by which the data received with the
13235 * shadow buf has "fallen short" of the requested amount.
13236 */
13237 shortfall = (size_t)(request_end - shadow_end);
13238
13239 if (shortfall > orig_bp->b_bcount) {
13240 /*
13241 * We did not get enough data to even partially
13242 * fulfill the original request. The residual is
13243 * equal to the amount requested.
13244 */
13245 orig_bp->b_resid = orig_bp->b_bcount;
13246 } else {
13247 /*
13248 * We did not get all the data that we requested
13249 * from the device, but we will try to return what
13250 * portion we did get.
13251 */
13252 orig_bp->b_resid = shortfall;
13253 }
13254 ASSERT(copy_length >= orig_bp->b_resid);
13255 copy_length -= orig_bp->b_resid;
13256 }
13257
13258 /* Propagate the error code from the shadow buf to the original buf */
13259 bioerror(orig_bp, bp->b_error);
13260
13261 if (is_write) {
13262 goto freebuf_done; /* No data copying for a WRITE */
13263 }
13264
13265 if (has_wmap) {
13266 /*
13267 * This is a READ command from the READ phase of a
13268 * read-modify-write request. We have to copy the data given
13269 * by the user OVER the data returned by the READ command,
13270 * then convert the command from a READ to a WRITE and send
13271 * it back to the target.
13272 */
13273 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13274 copy_length);
13275
13276 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */
13277
13278 /*
13279 * Dispatch the WRITE command to the taskq thread, which
13280 * will in turn send the command to the target. When the
13281 * WRITE command completes, we (sd_mapblocksize_iodone())
13282 * will get called again as part of the iodone chain
13283 * processing for it. Note that we will still be dealing
13284 * with the shadow buf at that point.
13285 */
13286 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13287 KM_NOSLEEP) != 0) {
13288 /*
13289 * Dispatch was successful so we are done. Return
13290 * without going any higher up the iodone chain. Do
13291 * not free up any layer-private data until after the
13292 * WRITE completes.
13293 */
13294 return;
13295 }
13296
13297 /*
13298 * Dispatch of the WRITE command failed; set up the error
13299 * condition and send this IO back up the iodone chain.
13300 */
13301 bioerror(orig_bp, EIO);
13302 orig_bp->b_resid = orig_bp->b_bcount;
13303
13304 } else {
13305 /*
13306 * This is a regular READ request (ie, not a RMW). Copy the
13307 * data from the shadow buf into the original buf. The
13308 * copy_offset compensates for any "misalignment" between the
13309 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13310 * original buf (with its un->un_sys_blocksize blocks).
13311 */
13312 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13313 copy_length);
13314 }
13315
13316 freebuf_done:
13317
13318 /*
13319 * At this point we still have both the shadow buf AND the original
13320 * buf to deal with, as well as the layer-private data area in each.
13321 * Local variables are as follows:
13322 *
13323 * bp -- points to shadow buf
13324 * xp -- points to xbuf of shadow buf
13325 * bsp -- points to layer-private data area of shadow buf
13326 * orig_bp -- points to original buf
13327 *
13328 * First free the shadow buf and its associated xbuf, then free the
13329 * layer-private data area from the shadow buf. There is no need to
13330 * restore xb_private in the shadow xbuf.
13331 */
13332 sd_shadow_buf_free(bp);
13333 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13334
13335 /*
13336 * Now update the local variables to point to the original buf, xbuf,
13337 * and layer-private area.
13338 */
13339 bp = orig_bp;
13340 xp = SD_GET_XBUF(bp);
13341 ASSERT(xp != NULL);
13342 ASSERT(xp == orig_xp);
13343 bsp = xp->xb_private;
13344 ASSERT(bsp != NULL);
13345
13346 done:
13347 /*
13348 * Restore xb_private to whatever it was set to by the next higher
13349 * layer in the chain, then free the layer-private data area.
13350 */
13351 xp->xb_private = bsp->mbs_oprivate;
13352 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13353
13354 exit:
13355 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13356 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13357
13358 SD_NEXT_IODONE(index, un, bp);
13359 }
13360
13361
13362 /*
13363 * Function: sd_checksum_iostart
13364 *
13365 * Description: A stub function for a layer that's currently not used.
13366 * For now just a placeholder.
13367 *
13368 * Context: Kernel thread context
13369 */
13370
13371 static void
13372 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13373 {
13374 ASSERT(un != NULL);
13375 ASSERT(bp != NULL);
13376 ASSERT(!mutex_owned(SD_MUTEX(un)));
13377 SD_NEXT_IOSTART(index, un, bp);
13378 }
13379
13380
13381 /*
13382 * Function: sd_checksum_iodone
13383 *
13384 * Description: A stub function for a layer that's currently not used.
13385 * For now just a placeholder.
13386 *
13387 * Context: May be called under interrupt context
13388 */
13389
13390 static void
13391 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13392 {
13393 ASSERT(un != NULL);
13394 ASSERT(bp != NULL);
13395 ASSERT(!mutex_owned(SD_MUTEX(un)));
13396 SD_NEXT_IODONE(index, un, bp);
13397 }
13398
13399
13400 /*
13401 * Function: sd_checksum_uscsi_iostart
13402 *
13403 * Description: A stub function for a layer that's currently not used.
13404 * For now just a placeholder.
13405 *
13406 * Context: Kernel thread context
13407 */
13408
13409 static void
13410 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13411 {
13412 ASSERT(un != NULL);
13413 ASSERT(bp != NULL);
13414 ASSERT(!mutex_owned(SD_MUTEX(un)));
13415 SD_NEXT_IOSTART(index, un, bp);
13416 }
13417
13418
13419 /*
13420 * Function: sd_checksum_uscsi_iodone
13421 *
13422 * Description: A stub function for a layer that's currently not used.
13423 * For now just a placeholder.
13424 *
13425 * Context: May be called under interrupt context
13426 */
13427
13428 static void
13429 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13430 {
13431 ASSERT(un != NULL);
13432 ASSERT(bp != NULL);
13433 ASSERT(!mutex_owned(SD_MUTEX(un)));
13434 SD_NEXT_IODONE(index, un, bp);
13435 }
13436
13437
13438 /*
13439 * Function: sd_pm_iostart
13440 *
13441 * Description: iostart-side routine for Power mangement.
13442 *
13443 * Context: Kernel thread context
13444 */
13445
13446 static void
13447 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13448 {
13449 ASSERT(un != NULL);
13450 ASSERT(bp != NULL);
13451 ASSERT(!mutex_owned(SD_MUTEX(un)));
13452 ASSERT(!mutex_owned(&un->un_pm_mutex));
13453
13454 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13455
13456 if (sd_pm_entry(un) != DDI_SUCCESS) {
13457 /*
13458 * Set up to return the failed buf back up the 'iodone'
13459 * side of the calling chain.
13460 */
13461 bioerror(bp, EIO);
13462 bp->b_resid = bp->b_bcount;
13463
13464 SD_BEGIN_IODONE(index, un, bp);
13465
13466 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13467 return;
13468 }
13469
13470 SD_NEXT_IOSTART(index, un, bp);
13471
13472 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13473 }
13474
13475
13476 /*
13477 * Function: sd_pm_iodone
13478 *
13479 * Description: iodone-side routine for power mangement.
13480 *
13481 * Context: may be called from interrupt context
13482 */
13483
13484 static void
13485 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13486 {
13487 ASSERT(un != NULL);
13488 ASSERT(bp != NULL);
13489 ASSERT(!mutex_owned(&un->un_pm_mutex));
13490
13491 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13492
13493 /*
13494 * After attach the following flag is only read, so don't
13495 * take the penalty of acquiring a mutex for it.
13496 */
13497 if (un->un_f_pm_is_enabled == TRUE) {
13498 sd_pm_exit(un);
13499 }
13500
13501 SD_NEXT_IODONE(index, un, bp);
13502
13503 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13504 }
13505
13506
13507 /*
13508 * Function: sd_core_iostart
13509 *
13510 * Description: Primary driver function for enqueuing buf(9S) structs from
13511 * the system and initiating IO to the target device
13512 *
13513 * Context: Kernel thread context. Can sleep.
13514 *
13515 * Assumptions: - The given xp->xb_blkno is absolute
13516 * (ie, relative to the start of the device).
13517 * - The IO is to be done using the native blocksize of
13518 * the device, as specified in un->un_tgt_blocksize.
13519 */
13520 /* ARGSUSED */
13521 static void
13522 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13523 {
13524 struct sd_xbuf *xp;
13525
13526 ASSERT(un != NULL);
13527 ASSERT(bp != NULL);
13528 ASSERT(!mutex_owned(SD_MUTEX(un)));
13529 ASSERT(bp->b_resid == 0);
13530
13531 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13532
13533 xp = SD_GET_XBUF(bp);
13534 ASSERT(xp != NULL);
13535
13536 mutex_enter(SD_MUTEX(un));
13537
13538 /*
13539 * If we are currently in the failfast state, fail any new IO
13540 * that has B_FAILFAST set, then return.
13541 */
13542 if ((bp->b_flags & B_FAILFAST) &&
13543 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13544 mutex_exit(SD_MUTEX(un));
13545 bioerror(bp, EIO);
13546 bp->b_resid = bp->b_bcount;
13547 SD_BEGIN_IODONE(index, un, bp);
13548 return;
13549 }
13550
13551 if (SD_IS_DIRECT_PRIORITY(xp)) {
13552 /*
13553 * Priority command -- transport it immediately.
13554 *
13555 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13556 * because all direct priority commands should be associated
13557 * with error recovery actions which we don't want to retry.
13558 */
13559 sd_start_cmds(un, bp);
13560 } else {
13561 /*
13562 * Normal command -- add it to the wait queue, then start
13563 * transporting commands from the wait queue.
13564 */
13565 sd_add_buf_to_waitq(un, bp);
13566 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13567 sd_start_cmds(un, NULL);
13568 }
13569
13570 mutex_exit(SD_MUTEX(un));
13571
13572 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13573 }
13574
13575
13576 /*
13577 * Function: sd_init_cdb_limits
13578 *
13579 * Description: This is to handle scsi_pkt initialization differences
13580 * between the driver platforms.
13581 *
13582 * Legacy behaviors:
13583 *
13584 * If the block number or the sector count exceeds the
13585 * capabilities of a Group 0 command, shift over to a
13586 * Group 1 command. We don't blindly use Group 1
13587 * commands because a) some drives (CDC Wren IVs) get a
13588 * bit confused, and b) there is probably a fair amount
13589 * of speed difference for a target to receive and decode
13590 * a 10 byte command instead of a 6 byte command.
13591 *
13592 * The xfer time difference of 6 vs 10 byte CDBs is
13593 * still significant so this code is still worthwhile.
13594 * 10 byte CDBs are very inefficient with the fas HBA driver
13595 * and older disks. Each CDB byte took 1 usec with some
13596 * popular disks.
13597 *
13598 * Context: Must be called at attach time
13599 */
13600
13601 static void
13602 sd_init_cdb_limits(struct sd_lun *un)
13603 {
13604 int hba_cdb_limit;
13605
13606 /*
13607 * Use CDB_GROUP1 commands for most devices except for
13608 * parallel SCSI fixed drives in which case we get better
13609 * performance using CDB_GROUP0 commands (where applicable).
13610 */
13611 un->un_mincdb = SD_CDB_GROUP1;
13612 #if !defined(__fibre)
13613 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13614 !un->un_f_has_removable_media) {
13615 un->un_mincdb = SD_CDB_GROUP0;
13616 }
13617 #endif
13618
13619 /*
13620 * Try to read the max-cdb-length supported by HBA.
13621 */
13622 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13623 if (0 >= un->un_max_hba_cdb) {
13624 un->un_max_hba_cdb = CDB_GROUP4;
13625 hba_cdb_limit = SD_CDB_GROUP4;
13626 } else if (0 < un->un_max_hba_cdb &&
13627 un->un_max_hba_cdb < CDB_GROUP1) {
13628 hba_cdb_limit = SD_CDB_GROUP0;
13629 } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13630 un->un_max_hba_cdb < CDB_GROUP5) {
13631 hba_cdb_limit = SD_CDB_GROUP1;
13632 } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13633 un->un_max_hba_cdb < CDB_GROUP4) {
13634 hba_cdb_limit = SD_CDB_GROUP5;
13635 } else {
13636 hba_cdb_limit = SD_CDB_GROUP4;
13637 }
13638
13639 /*
13640 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
13641 * commands for fixed disks unless we are building for a 32 bit
13642 * kernel.
13643 */
13644 #ifdef _LP64
13645 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13646 min(hba_cdb_limit, SD_CDB_GROUP4);
13647 #else
13648 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13649 min(hba_cdb_limit, SD_CDB_GROUP1);
13650 #endif
13651
13652 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13653 ? sizeof (struct scsi_arq_status) : 1);
13654 un->un_cmd_timeout = (ushort_t)sd_io_time;
13655 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13656 }
13657
13658
13659 /*
13660 * Function: sd_initpkt_for_buf
13661 *
13662 * Description: Allocate and initialize for transport a scsi_pkt struct,
13663 * based upon the info specified in the given buf struct.
13664 *
13665 * Assumes the xb_blkno in the request is absolute (ie,
13666 * relative to the start of the device (NOT partition!).
13667 * Also assumes that the request is using the native block
13668 * size of the device (as returned by the READ CAPACITY
13669 * command).
13670 *
13671 * Return Code: SD_PKT_ALLOC_SUCCESS
13672 * SD_PKT_ALLOC_FAILURE
13673 * SD_PKT_ALLOC_FAILURE_NO_DMA
13674 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13675 *
13676 * Context: Kernel thread and may be called from software interrupt context
13677 * as part of a sdrunout callback. This function may not block or
13678 * call routines that block
13679 */
13680
13681 static int
13682 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13683 {
13684 struct sd_xbuf *xp;
13685 struct scsi_pkt *pktp = NULL;
13686 struct sd_lun *un;
13687 size_t blockcount;
13688 daddr_t startblock;
13689 int rval;
13690 int cmd_flags;
13691
13692 ASSERT(bp != NULL);
13693 ASSERT(pktpp != NULL);
13694 xp = SD_GET_XBUF(bp);
13695 ASSERT(xp != NULL);
13696 un = SD_GET_UN(bp);
13697 ASSERT(un != NULL);
13698 ASSERT(mutex_owned(SD_MUTEX(un)));
13699 ASSERT(bp->b_resid == 0);
13700
13701 SD_TRACE(SD_LOG_IO_CORE, un,
13702 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13703
13704 mutex_exit(SD_MUTEX(un));
13705
13706 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13707 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13708 /*
13709 * Already have a scsi_pkt -- just need DMA resources.
13710 * We must recompute the CDB in case the mapping returns
13711 * a nonzero pkt_resid.
13712 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13713 * that is being retried, the unmap/remap of the DMA resouces
13714 * will result in the entire transfer starting over again
13715 * from the very first block.
13716 */
13717 ASSERT(xp->xb_pktp != NULL);
13718 pktp = xp->xb_pktp;
13719 } else {
13720 pktp = NULL;
13721 }
13722 #endif /* __i386 || __amd64 */
13723
13724 startblock = xp->xb_blkno; /* Absolute block num. */
13725 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13726
13727 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13728
13729 /*
13730 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13731 * call scsi_init_pkt, and build the CDB.
13732 */
13733 rval = sd_setup_rw_pkt(un, &pktp, bp,
13734 cmd_flags, sdrunout, (caddr_t)un,
13735 startblock, blockcount);
13736
13737 if (rval == 0) {
13738 /*
13739 * Success.
13740 *
13741 * If partial DMA is being used and required for this transfer.
13742 * set it up here.
13743 */
13744 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13745 (pktp->pkt_resid != 0)) {
13746
13747 /*
13748 * Save the CDB length and pkt_resid for the
13749 * next xfer
13750 */
13751 xp->xb_dma_resid = pktp->pkt_resid;
13752
13753 /* rezero resid */
13754 pktp->pkt_resid = 0;
13755
13756 } else {
13757 xp->xb_dma_resid = 0;
13758 }
13759
13760 pktp->pkt_flags = un->un_tagflags;
13761 pktp->pkt_time = un->un_cmd_timeout;
13762 pktp->pkt_comp = sdintr;
13763
13764 pktp->pkt_private = bp;
13765 *pktpp = pktp;
13766
13767 SD_TRACE(SD_LOG_IO_CORE, un,
13768 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13769
13770 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13771 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13772 #endif
13773
13774 mutex_enter(SD_MUTEX(un));
13775 return (SD_PKT_ALLOC_SUCCESS);
13776
13777 }
13778
13779 /*
13780 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13781 * from sd_setup_rw_pkt.
13782 */
13783 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13784
13785 if (rval == SD_PKT_ALLOC_FAILURE) {
13786 *pktpp = NULL;
13787 /*
13788 * Set the driver state to RWAIT to indicate the driver
13789 * is waiting on resource allocations. The driver will not
13790 * suspend, pm_suspend, or detatch while the state is RWAIT.
13791 */
13792 mutex_enter(SD_MUTEX(un));
13793 New_state(un, SD_STATE_RWAIT);
13794
13795 SD_ERROR(SD_LOG_IO_CORE, un,
13796 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13797
13798 if ((bp->b_flags & B_ERROR) != 0) {
13799 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13800 }
13801 return (SD_PKT_ALLOC_FAILURE);
13802 } else {
13803 /*
13804 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13805 *
13806 * This should never happen. Maybe someone messed with the
13807 * kernel's minphys?
13808 */
13809 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13810 "Request rejected: too large for CDB: "
13811 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount);
13812 SD_ERROR(SD_LOG_IO_CORE, un,
13813 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13814 mutex_enter(SD_MUTEX(un));
13815 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13816
13817 }
13818 }
13819
13820
13821 /*
13822 * Function: sd_destroypkt_for_buf
13823 *
13824 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13825 *
13826 * Context: Kernel thread or interrupt context
13827 */
13828
13829 static void
13830 sd_destroypkt_for_buf(struct buf *bp)
13831 {
13832 ASSERT(bp != NULL);
13833 ASSERT(SD_GET_UN(bp) != NULL);
13834
13835 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13836 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13837
13838 ASSERT(SD_GET_PKTP(bp) != NULL);
13839 scsi_destroy_pkt(SD_GET_PKTP(bp));
13840
13841 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13842 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13843 }
13844
13845 /*
13846 * Function: sd_setup_rw_pkt
13847 *
13848 * Description: Determines appropriate CDB group for the requested LBA
13849 * and transfer length, calls scsi_init_pkt, and builds
13850 * the CDB. Do not use for partial DMA transfers except
13851 * for the initial transfer since the CDB size must
13852 * remain constant.
13853 *
13854 * Context: Kernel thread and may be called from software interrupt
13855 * context as part of a sdrunout callback. This function may not
13856 * block or call routines that block
13857 */
13858
13859
13860 int
13861 sd_setup_rw_pkt(struct sd_lun *un,
13862 struct scsi_pkt **pktpp, struct buf *bp, int flags,
13863 int (*callback)(caddr_t), caddr_t callback_arg,
13864 diskaddr_t lba, uint32_t blockcount)
13865 {
13866 struct scsi_pkt *return_pktp;
13867 union scsi_cdb *cdbp;
13868 struct sd_cdbinfo *cp = NULL;
13869 int i;
13870
13871 /*
13872 * See which size CDB to use, based upon the request.
13873 */
13874 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13875
13876 /*
13877 * Check lba and block count against sd_cdbtab limits.
13878 * In the partial DMA case, we have to use the same size
13879 * CDB for all the transfers. Check lba + blockcount
13880 * against the max LBA so we know that segment of the
13881 * transfer can use the CDB we select.
13882 */
13883 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13884 (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13885
13886 /*
13887 * The command will fit into the CDB type
13888 * specified by sd_cdbtab[i].
13889 */
13890 cp = sd_cdbtab + i;
13891
13892 /*
13893 * Call scsi_init_pkt so we can fill in the
13894 * CDB.
13895 */
13896 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13897 bp, cp->sc_grpcode, un->un_status_len, 0,
13898 flags, callback, callback_arg);
13899
13900 if (return_pktp != NULL) {
13901
13902 /*
13903 * Return new value of pkt
13904 */
13905 *pktpp = return_pktp;
13906
13907 /*
13908 * To be safe, zero the CDB insuring there is
13909 * no leftover data from a previous command.
13910 */
13911 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13912
13913 /*
13914 * Handle partial DMA mapping
13915 */
13916 if (return_pktp->pkt_resid != 0) {
13917
13918 /*
13919 * Not going to xfer as many blocks as
13920 * originally expected
13921 */
13922 blockcount -=
13923 SD_BYTES2TGTBLOCKS(un,
13924 return_pktp->pkt_resid);
13925 }
13926
13927 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13928
13929 /*
13930 * Set command byte based on the CDB
13931 * type we matched.
13932 */
13933 cdbp->scc_cmd = cp->sc_grpmask |
13934 ((bp->b_flags & B_READ) ?
13935 SCMD_READ : SCMD_WRITE);
13936
13937 SD_FILL_SCSI1_LUN(un, return_pktp);
13938
13939 /*
13940 * Fill in LBA and length
13941 */
13942 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13943 (cp->sc_grpcode == CDB_GROUP4) ||
13944 (cp->sc_grpcode == CDB_GROUP0) ||
13945 (cp->sc_grpcode == CDB_GROUP5));
13946
13947 if (cp->sc_grpcode == CDB_GROUP1) {
13948 FORMG1ADDR(cdbp, lba);
13949 FORMG1COUNT(cdbp, blockcount);
13950 return (0);
13951 } else if (cp->sc_grpcode == CDB_GROUP4) {
13952 FORMG4LONGADDR(cdbp, lba);
13953 FORMG4COUNT(cdbp, blockcount);
13954 return (0);
13955 } else if (cp->sc_grpcode == CDB_GROUP0) {
13956 FORMG0ADDR(cdbp, lba);
13957 FORMG0COUNT(cdbp, blockcount);
13958 return (0);
13959 } else if (cp->sc_grpcode == CDB_GROUP5) {
13960 FORMG5ADDR(cdbp, lba);
13961 FORMG5COUNT(cdbp, blockcount);
13962 return (0);
13963 }
13964
13965 /*
13966 * It should be impossible to not match one
13967 * of the CDB types above, so we should never
13968 * reach this point. Set the CDB command byte
13969 * to test-unit-ready to avoid writing
13970 * to somewhere we don't intend.
13971 */
13972 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13973 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13974 } else {
13975 /*
13976 * Couldn't get scsi_pkt
13977 */
13978 return (SD_PKT_ALLOC_FAILURE);
13979 }
13980 }
13981 }
13982
13983 /*
13984 * None of the available CDB types were suitable. This really
13985 * should never happen: on a 64 bit system we support
13986 * READ16/WRITE16 which will hold an entire 64 bit disk address
13987 * and on a 32 bit system we will refuse to bind to a device
13988 * larger than 2TB so addresses will never be larger than 32 bits.
13989 */
13990 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13991 }
13992
13993 /*
13994 * Function: sd_setup_next_rw_pkt
13995 *
13996 * Description: Setup packet for partial DMA transfers, except for the
13997 * initial transfer. sd_setup_rw_pkt should be used for
13998 * the initial transfer.
13999 *
14000 * Context: Kernel thread and may be called from interrupt context.
14001 */
14002
14003 int
14004 sd_setup_next_rw_pkt(struct sd_lun *un,
14005 struct scsi_pkt *pktp, struct buf *bp,
14006 diskaddr_t lba, uint32_t blockcount)
14007 {
14008 uchar_t com;
14009 union scsi_cdb *cdbp;
14010 uchar_t cdb_group_id;
14011
14012 ASSERT(pktp != NULL);
14013 ASSERT(pktp->pkt_cdbp != NULL);
14014
14015 cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
14016 com = cdbp->scc_cmd;
14017 cdb_group_id = CDB_GROUPID(com);
14018
14019 ASSERT((cdb_group_id == CDB_GROUPID_0) ||
14020 (cdb_group_id == CDB_GROUPID_1) ||
14021 (cdb_group_id == CDB_GROUPID_4) ||
14022 (cdb_group_id == CDB_GROUPID_5));
14023
14024 /*
14025 * Move pkt to the next portion of the xfer.
14026 * func is NULL_FUNC so we do not have to release
14027 * the disk mutex here.
14028 */
14029 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14030 NULL_FUNC, NULL) == pktp) {
14031 /* Success. Handle partial DMA */
14032 if (pktp->pkt_resid != 0) {
14033 blockcount -=
14034 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14035 }
14036
14037 cdbp->scc_cmd = com;
14038 SD_FILL_SCSI1_LUN(un, pktp);
14039 if (cdb_group_id == CDB_GROUPID_1) {
14040 FORMG1ADDR(cdbp, lba);
14041 FORMG1COUNT(cdbp, blockcount);
14042 return (0);
14043 } else if (cdb_group_id == CDB_GROUPID_4) {
14044 FORMG4LONGADDR(cdbp, lba);
14045 FORMG4COUNT(cdbp, blockcount);
14046 return (0);
14047 } else if (cdb_group_id == CDB_GROUPID_0) {
14048 FORMG0ADDR(cdbp, lba);
14049 FORMG0COUNT(cdbp, blockcount);
14050 return (0);
14051 } else if (cdb_group_id == CDB_GROUPID_5) {
14052 FORMG5ADDR(cdbp, lba);
14053 FORMG5COUNT(cdbp, blockcount);
14054 return (0);
14055 }
14056
14057 /* Unreachable */
14058 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14059 }
14060
14061 /*
14062 * Error setting up next portion of cmd transfer.
14063 * Something is definitely very wrong and this
14064 * should not happen.
14065 */
14066 return (SD_PKT_ALLOC_FAILURE);
14067 }
14068
14069 /*
14070 * Function: sd_initpkt_for_uscsi
14071 *
14072 * Description: Allocate and initialize for transport a scsi_pkt struct,
14073 * based upon the info specified in the given uscsi_cmd struct.
14074 *
14075 * Return Code: SD_PKT_ALLOC_SUCCESS
14076 * SD_PKT_ALLOC_FAILURE
14077 * SD_PKT_ALLOC_FAILURE_NO_DMA
14078 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14079 *
14080 * Context: Kernel thread and may be called from software interrupt context
14081 * as part of a sdrunout callback. This function may not block or
14082 * call routines that block
14083 */
14084
14085 static int
14086 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14087 {
14088 struct uscsi_cmd *uscmd;
14089 struct sd_xbuf *xp;
14090 struct scsi_pkt *pktp;
14091 struct sd_lun *un;
14092 uint32_t flags = 0;
14093
14094 ASSERT(bp != NULL);
14095 ASSERT(pktpp != NULL);
14096 xp = SD_GET_XBUF(bp);
14097 ASSERT(xp != NULL);
14098 un = SD_GET_UN(bp);
14099 ASSERT(un != NULL);
14100 ASSERT(mutex_owned(SD_MUTEX(un)));
14101
14102 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14103 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14104 ASSERT(uscmd != NULL);
14105
14106 SD_TRACE(SD_LOG_IO_CORE, un,
14107 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14108
14109 /*
14110 * Allocate the scsi_pkt for the command.
14111 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14112 * during scsi_init_pkt time and will continue to use the
14113 * same path as long as the same scsi_pkt is used without
14114 * intervening scsi_dma_free(). Since uscsi command does
14115 * not call scsi_dmafree() before retry failed command, it
14116 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14117 * set such that scsi_vhci can use other available path for
14118 * retry. Besides, ucsci command does not allow DMA breakup,
14119 * so there is no need to set PKT_DMA_PARTIAL flag.
14120 */
14121 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14122 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14123 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14124 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14125 - sizeof (struct scsi_extended_sense)), 0,
14126 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14127 sdrunout, (caddr_t)un);
14128 } else {
14129 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14130 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14131 sizeof (struct scsi_arq_status), 0,
14132 (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14133 sdrunout, (caddr_t)un);
14134 }
14135
14136 if (pktp == NULL) {
14137 *pktpp = NULL;
14138 /*
14139 * Set the driver state to RWAIT to indicate the driver
14140 * is waiting on resource allocations. The driver will not
14141 * suspend, pm_suspend, or detatch while the state is RWAIT.
14142 */
14143 New_state(un, SD_STATE_RWAIT);
14144
14145 SD_ERROR(SD_LOG_IO_CORE, un,
14146 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14147
14148 if ((bp->b_flags & B_ERROR) != 0) {
14149 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14150 }
14151 return (SD_PKT_ALLOC_FAILURE);
14152 }
14153
14154 /*
14155 * We do not do DMA breakup for USCSI commands, so return failure
14156 * here if all the needed DMA resources were not allocated.
14157 */
14158 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14159 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14160 scsi_destroy_pkt(pktp);
14161 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14162 "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14163 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14164 }
14165
14166 /* Init the cdb from the given uscsi struct */
14167 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14168 uscmd->uscsi_cdb[0], 0, 0, 0);
14169
14170 SD_FILL_SCSI1_LUN(un, pktp);
14171
14172 /*
14173 * Set up the optional USCSI flags. See the uscsi (7I) man page
14174 * for listing of the supported flags.
14175 */
14176
14177 if (uscmd->uscsi_flags & USCSI_SILENT) {
14178 flags |= FLAG_SILENT;
14179 }
14180
14181 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14182 flags |= FLAG_DIAGNOSE;
14183 }
14184
14185 if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14186 flags |= FLAG_ISOLATE;
14187 }
14188
14189 if (un->un_f_is_fibre == FALSE) {
14190 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14191 flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14192 }
14193 }
14194
14195 /*
14196 * Set the pkt flags here so we save time later.
14197 * Note: These flags are NOT in the uscsi man page!!!
14198 */
14199 if (uscmd->uscsi_flags & USCSI_HEAD) {
14200 flags |= FLAG_HEAD;
14201 }
14202
14203 if (uscmd->uscsi_flags & USCSI_NOINTR) {
14204 flags |= FLAG_NOINTR;
14205 }
14206
14207 /*
14208 * For tagged queueing, things get a bit complicated.
14209 * Check first for head of queue and last for ordered queue.
14210 * If neither head nor order, use the default driver tag flags.
14211 */
14212 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14213 if (uscmd->uscsi_flags & USCSI_HTAG) {
14214 flags |= FLAG_HTAG;
14215 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
14216 flags |= FLAG_OTAG;
14217 } else {
14218 flags |= un->un_tagflags & FLAG_TAGMASK;
14219 }
14220 }
14221
14222 if (uscmd->uscsi_flags & USCSI_NODISCON) {
14223 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14224 }
14225
14226 pktp->pkt_flags = flags;
14227
14228 /* Transfer uscsi information to scsi_pkt */
14229 (void) scsi_uscsi_pktinit(uscmd, pktp);
14230
14231 /* Copy the caller's CDB into the pkt... */
14232 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14233
14234 if (uscmd->uscsi_timeout == 0) {
14235 pktp->pkt_time = un->un_uscsi_timeout;
14236 } else {
14237 pktp->pkt_time = uscmd->uscsi_timeout;
14238 }
14239
14240 /* need it later to identify USCSI request in sdintr */
14241 xp->xb_pkt_flags |= SD_XB_USCSICMD;
14242
14243 xp->xb_sense_resid = uscmd->uscsi_rqresid;
14244
14245 pktp->pkt_private = bp;
14246 pktp->pkt_comp = sdintr;
14247 *pktpp = pktp;
14248
14249 SD_TRACE(SD_LOG_IO_CORE, un,
14250 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14251
14252 return (SD_PKT_ALLOC_SUCCESS);
14253 }
14254
14255
14256 /*
14257 * Function: sd_destroypkt_for_uscsi
14258 *
14259 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14260 * IOs.. Also saves relevant info into the associated uscsi_cmd
14261 * struct.
14262 *
14263 * Context: May be called under interrupt context
14264 */
14265
14266 static void
14267 sd_destroypkt_for_uscsi(struct buf *bp)
14268 {
14269 struct uscsi_cmd *uscmd;
14270 struct sd_xbuf *xp;
14271 struct scsi_pkt *pktp;
14272 struct sd_lun *un;
14273 struct sd_uscsi_info *suip;
14274
14275 ASSERT(bp != NULL);
14276 xp = SD_GET_XBUF(bp);
14277 ASSERT(xp != NULL);
14278 un = SD_GET_UN(bp);
14279 ASSERT(un != NULL);
14280 ASSERT(!mutex_owned(SD_MUTEX(un)));
14281 pktp = SD_GET_PKTP(bp);
14282 ASSERT(pktp != NULL);
14283
14284 SD_TRACE(SD_LOG_IO_CORE, un,
14285 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14286
14287 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14288 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14289 ASSERT(uscmd != NULL);
14290
14291 /* Save the status and the residual into the uscsi_cmd struct */
14292 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14293 uscmd->uscsi_resid = bp->b_resid;
14294
14295 /* Transfer scsi_pkt information to uscsi */
14296 (void) scsi_uscsi_pktfini(pktp, uscmd);
14297
14298 /*
14299 * If enabled, copy any saved sense data into the area specified
14300 * by the uscsi command.
14301 */
14302 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14303 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14304 /*
14305 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14306 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14307 */
14308 uscmd->uscsi_rqstatus = xp->xb_sense_status;
14309 uscmd->uscsi_rqresid = xp->xb_sense_resid;
14310 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14311 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14312 MAX_SENSE_LENGTH);
14313 } else {
14314 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14315 SENSE_LENGTH);
14316 }
14317 }
14318 /*
14319 * The following assignments are for SCSI FMA.
14320 */
14321 ASSERT(xp->xb_private != NULL);
14322 suip = (struct sd_uscsi_info *)xp->xb_private;
14323 suip->ui_pkt_reason = pktp->pkt_reason;
14324 suip->ui_pkt_state = pktp->pkt_state;
14325 suip->ui_pkt_statistics = pktp->pkt_statistics;
14326 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14327
14328 /* We are done with the scsi_pkt; free it now */
14329 ASSERT(SD_GET_PKTP(bp) != NULL);
14330 scsi_destroy_pkt(SD_GET_PKTP(bp));
14331
14332 SD_TRACE(SD_LOG_IO_CORE, un,
14333 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14334 }
14335
14336
14337 /*
14338 * Function: sd_bioclone_alloc
14339 *
14340 * Description: Allocate a buf(9S) and init it as per the given buf
14341 * and the various arguments. The associated sd_xbuf
14342 * struct is (nearly) duplicated. The struct buf *bp
14343 * argument is saved in new_xp->xb_private.
14344 *
14345 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
14346 * datalen - size of data area for the shadow bp
14347 * blkno - starting LBA
14348 * func - function pointer for b_iodone in the shadow buf. (May
14349 * be NULL if none.)
14350 *
14351 * Return Code: Pointer to allocates buf(9S) struct
14352 *
14353 * Context: Can sleep.
14354 */
14355
14356 static struct buf *
14357 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14358 daddr_t blkno, int (*func)(struct buf *))
14359 {
14360 struct sd_lun *un;
14361 struct sd_xbuf *xp;
14362 struct sd_xbuf *new_xp;
14363 struct buf *new_bp;
14364
14365 ASSERT(bp != NULL);
14366 xp = SD_GET_XBUF(bp);
14367 ASSERT(xp != NULL);
14368 un = SD_GET_UN(bp);
14369 ASSERT(un != NULL);
14370 ASSERT(!mutex_owned(SD_MUTEX(un)));
14371
14372 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14373 NULL, KM_SLEEP);
14374
14375 new_bp->b_lblkno = blkno;
14376
14377 /*
14378 * Allocate an xbuf for the shadow bp and copy the contents of the
14379 * original xbuf into it.
14380 */
14381 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14382 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14383
14384 /*
14385 * The given bp is automatically saved in the xb_private member
14386 * of the new xbuf. Callers are allowed to depend on this.
14387 */
14388 new_xp->xb_private = bp;
14389
14390 new_bp->b_private = new_xp;
14391
14392 return (new_bp);
14393 }
14394
14395 /*
14396 * Function: sd_shadow_buf_alloc
14397 *
14398 * Description: Allocate a buf(9S) and init it as per the given buf
14399 * and the various arguments. The associated sd_xbuf
14400 * struct is (nearly) duplicated. The struct buf *bp
14401 * argument is saved in new_xp->xb_private.
14402 *
14403 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
14404 * datalen - size of data area for the shadow bp
14405 * bflags - B_READ or B_WRITE (pseudo flag)
14406 * blkno - starting LBA
14407 * func - function pointer for b_iodone in the shadow buf. (May
14408 * be NULL if none.)
14409 *
14410 * Return Code: Pointer to allocates buf(9S) struct
14411 *
14412 * Context: Can sleep.
14413 */
14414
14415 static struct buf *
14416 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14417 daddr_t blkno, int (*func)(struct buf *))
14418 {
14419 struct sd_lun *un;
14420 struct sd_xbuf *xp;
14421 struct sd_xbuf *new_xp;
14422 struct buf *new_bp;
14423
14424 ASSERT(bp != NULL);
14425 xp = SD_GET_XBUF(bp);
14426 ASSERT(xp != NULL);
14427 un = SD_GET_UN(bp);
14428 ASSERT(un != NULL);
14429 ASSERT(!mutex_owned(SD_MUTEX(un)));
14430
14431 if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14432 bp_mapin(bp);
14433 }
14434
14435 bflags &= (B_READ | B_WRITE);
14436 #if defined(__i386) || defined(__amd64)
14437 new_bp = getrbuf(KM_SLEEP);
14438 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14439 new_bp->b_bcount = datalen;
14440 new_bp->b_flags = bflags |
14441 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14442 #else
14443 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14444 datalen, bflags, SLEEP_FUNC, NULL);
14445 #endif
14446 new_bp->av_forw = NULL;
14447 new_bp->av_back = NULL;
14448 new_bp->b_dev = bp->b_dev;
14449 new_bp->b_blkno = blkno;
14450 new_bp->b_iodone = func;
14451 new_bp->b_edev = bp->b_edev;
14452 new_bp->b_resid = 0;
14453
14454 /* We need to preserve the B_FAILFAST flag */
14455 if (bp->b_flags & B_FAILFAST) {
14456 new_bp->b_flags |= B_FAILFAST;
14457 }
14458
14459 /*
14460 * Allocate an xbuf for the shadow bp and copy the contents of the
14461 * original xbuf into it.
14462 */
14463 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14464 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14465
14466 /* Need later to copy data between the shadow buf & original buf! */
14467 new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14468
14469 /*
14470 * The given bp is automatically saved in the xb_private member
14471 * of the new xbuf. Callers are allowed to depend on this.
14472 */
14473 new_xp->xb_private = bp;
14474
14475 new_bp->b_private = new_xp;
14476
14477 return (new_bp);
14478 }
14479
14480 /*
14481 * Function: sd_bioclone_free
14482 *
14483 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14484 * in the larger than partition operation.
14485 *
14486 * Context: May be called under interrupt context
14487 */
14488
14489 static void
14490 sd_bioclone_free(struct buf *bp)
14491 {
14492 struct sd_xbuf *xp;
14493
14494 ASSERT(bp != NULL);
14495 xp = SD_GET_XBUF(bp);
14496 ASSERT(xp != NULL);
14497
14498 /*
14499 * Call bp_mapout() before freeing the buf, in case a lower
14500 * layer or HBA had done a bp_mapin(). we must do this here
14501 * as we are the "originator" of the shadow buf.
14502 */
14503 bp_mapout(bp);
14504
14505 /*
14506 * Null out b_iodone before freeing the bp, to ensure that the driver
14507 * never gets confused by a stale value in this field. (Just a little
14508 * extra defensiveness here.)
14509 */
14510 bp->b_iodone = NULL;
14511
14512 freerbuf(bp);
14513
14514 kmem_free(xp, sizeof (struct sd_xbuf));
14515 }
14516
14517 /*
14518 * Function: sd_shadow_buf_free
14519 *
14520 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14521 *
14522 * Context: May be called under interrupt context
14523 */
14524
14525 static void
14526 sd_shadow_buf_free(struct buf *bp)
14527 {
14528 struct sd_xbuf *xp;
14529
14530 ASSERT(bp != NULL);
14531 xp = SD_GET_XBUF(bp);
14532 ASSERT(xp != NULL);
14533
14534 #if defined(__sparc)
14535 /*
14536 * Call bp_mapout() before freeing the buf, in case a lower
14537 * layer or HBA had done a bp_mapin(). we must do this here
14538 * as we are the "originator" of the shadow buf.
14539 */
14540 bp_mapout(bp);
14541 #endif
14542
14543 /*
14544 * Null out b_iodone before freeing the bp, to ensure that the driver
14545 * never gets confused by a stale value in this field. (Just a little
14546 * extra defensiveness here.)
14547 */
14548 bp->b_iodone = NULL;
14549
14550 #if defined(__i386) || defined(__amd64)
14551 kmem_free(bp->b_un.b_addr, bp->b_bcount);
14552 freerbuf(bp);
14553 #else
14554 scsi_free_consistent_buf(bp);
14555 #endif
14556
14557 kmem_free(xp, sizeof (struct sd_xbuf));
14558 }
14559
14560
14561 /*
14562 * Function: sd_print_transport_rejected_message
14563 *
14564 * Description: This implements the ludicrously complex rules for printing
14565 * a "transport rejected" message. This is to address the
14566 * specific problem of having a flood of this error message
14567 * produced when a failover occurs.
14568 *
14569 * Context: Any.
14570 */
14571
14572 static void
14573 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14574 int code)
14575 {
14576 ASSERT(un != NULL);
14577 ASSERT(mutex_owned(SD_MUTEX(un)));
14578 ASSERT(xp != NULL);
14579
14580 /*
14581 * Print the "transport rejected" message under the following
14582 * conditions:
14583 *
14584 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14585 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14586 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14587 * printed the FIRST time a TRAN_FATAL_ERROR is returned from
14588 * scsi_transport(9F) (which indicates that the target might have
14589 * gone off-line). This uses the un->un_tran_fatal_count
14590 * count, which is incremented whenever a TRAN_FATAL_ERROR is
14591 * received, and reset to zero whenver a TRAN_ACCEPT is returned
14592 * from scsi_transport().
14593 *
14594 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14595 * the preceeding cases in order for the message to be printed.
14596 */
14597 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14598 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14599 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14600 (code != TRAN_FATAL_ERROR) ||
14601 (un->un_tran_fatal_count == 1)) {
14602 switch (code) {
14603 case TRAN_BADPKT:
14604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14605 "transport rejected bad packet\n");
14606 break;
14607 case TRAN_FATAL_ERROR:
14608 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14609 "transport rejected fatal error\n");
14610 break;
14611 default:
14612 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14613 "transport rejected (%d)\n", code);
14614 break;
14615 }
14616 }
14617 }
14618 }
14619
14620
14621 /*
14622 * Function: sd_add_buf_to_waitq
14623 *
14624 * Description: Add the given buf(9S) struct to the wait queue for the
14625 * instance. If sorting is enabled, then the buf is added
14626 * to the queue via an elevator sort algorithm (a la
14627 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key.
14628 * If sorting is not enabled, then the buf is just added
14629 * to the end of the wait queue.
14630 *
14631 * Return Code: void
14632 *
14633 * Context: Does not sleep/block, therefore technically can be called
14634 * from any context. However if sorting is enabled then the
14635 * execution time is indeterminate, and may take long if
14636 * the wait queue grows large.
14637 */
14638
14639 static void
14640 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14641 {
14642 struct buf *ap;
14643
14644 ASSERT(bp != NULL);
14645 ASSERT(un != NULL);
14646 ASSERT(mutex_owned(SD_MUTEX(un)));
14647
14648 /* If the queue is empty, add the buf as the only entry & return. */
14649 if (un->un_waitq_headp == NULL) {
14650 ASSERT(un->un_waitq_tailp == NULL);
14651 un->un_waitq_headp = un->un_waitq_tailp = bp;
14652 bp->av_forw = NULL;
14653 return;
14654 }
14655
14656 ASSERT(un->un_waitq_tailp != NULL);
14657
14658 /*
14659 * If sorting is disabled, just add the buf to the tail end of
14660 * the wait queue and return.
14661 */
14662 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14663 un->un_waitq_tailp->av_forw = bp;
14664 un->un_waitq_tailp = bp;
14665 bp->av_forw = NULL;
14666 return;
14667 }
14668
14669 /*
14670 * Sort thru the list of requests currently on the wait queue
14671 * and add the new buf request at the appropriate position.
14672 *
14673 * The un->un_waitq_headp is an activity chain pointer on which
14674 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14675 * first queue holds those requests which are positioned after
14676 * the current SD_GET_BLKNO() (in the first request); the second holds
14677 * requests which came in after their SD_GET_BLKNO() number was passed.
14678 * Thus we implement a one way scan, retracting after reaching
14679 * the end of the drive to the first request on the second
14680 * queue, at which time it becomes the first queue.
14681 * A one-way scan is natural because of the way UNIX read-ahead
14682 * blocks are allocated.
14683 *
14684 * If we lie after the first request, then we must locate the
14685 * second request list and add ourselves to it.
14686 */
14687 ap = un->un_waitq_headp;
14688 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14689 while (ap->av_forw != NULL) {
14690 /*
14691 * Look for an "inversion" in the (normally
14692 * ascending) block numbers. This indicates
14693 * the start of the second request list.
14694 */
14695 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14696 /*
14697 * Search the second request list for the
14698 * first request at a larger block number.
14699 * We go before that; however if there is
14700 * no such request, we go at the end.
14701 */
14702 do {
14703 if (SD_GET_BLKNO(bp) <
14704 SD_GET_BLKNO(ap->av_forw)) {
14705 goto insert;
14706 }
14707 ap = ap->av_forw;
14708 } while (ap->av_forw != NULL);
14709 goto insert; /* after last */
14710 }
14711 ap = ap->av_forw;
14712 }
14713
14714 /*
14715 * No inversions... we will go after the last, and
14716 * be the first request in the second request list.
14717 */
14718 goto insert;
14719 }
14720
14721 /*
14722 * Request is at/after the current request...
14723 * sort in the first request list.
14724 */
14725 while (ap->av_forw != NULL) {
14726 /*
14727 * We want to go after the current request (1) if
14728 * there is an inversion after it (i.e. it is the end
14729 * of the first request list), or (2) if the next
14730 * request is a larger block no. than our request.
14731 */
14732 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14733 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14734 goto insert;
14735 }
14736 ap = ap->av_forw;
14737 }
14738
14739 /*
14740 * Neither a second list nor a larger request, therefore
14741 * we go at the end of the first list (which is the same
14742 * as the end of the whole schebang).
14743 */
14744 insert:
14745 bp->av_forw = ap->av_forw;
14746 ap->av_forw = bp;
14747
14748 /*
14749 * If we inserted onto the tail end of the waitq, make sure the
14750 * tail pointer is updated.
14751 */
14752 if (ap == un->un_waitq_tailp) {
14753 un->un_waitq_tailp = bp;
14754 }
14755 }
14756
14757
14758 /*
14759 * Function: sd_start_cmds
14760 *
14761 * Description: Remove and transport cmds from the driver queues.
14762 *
14763 * Arguments: un - pointer to the unit (soft state) struct for the target.
14764 *
14765 * immed_bp - ptr to a buf to be transported immediately. Only
14766 * the immed_bp is transported; bufs on the waitq are not
14767 * processed and the un_retry_bp is not checked. If immed_bp is
14768 * NULL, then normal queue processing is performed.
14769 *
14770 * Context: May be called from kernel thread context, interrupt context,
14771 * or runout callback context. This function may not block or
14772 * call routines that block.
14773 */
14774
14775 static void
14776 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14777 {
14778 struct sd_xbuf *xp;
14779 struct buf *bp;
14780 void (*statp)(kstat_io_t *);
14781 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14782 void (*saved_statp)(kstat_io_t *);
14783 #endif
14784 int rval;
14785 struct sd_fm_internal *sfip = NULL;
14786
14787 ASSERT(un != NULL);
14788 ASSERT(mutex_owned(SD_MUTEX(un)));
14789 ASSERT(un->un_ncmds_in_transport >= 0);
14790 ASSERT(un->un_throttle >= 0);
14791
14792 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14793
14794 do {
14795 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14796 saved_statp = NULL;
14797 #endif
14798
14799 /*
14800 * If we are syncing or dumping, fail the command to
14801 * avoid recursively calling back into scsi_transport().
14802 * The dump I/O itself uses a separate code path so this
14803 * only prevents non-dump I/O from being sent while dumping.
14804 * File system sync takes place before dumping begins.
14805 * During panic, filesystem I/O is allowed provided
14806 * un_in_callback is <= 1. This is to prevent recursion
14807 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14808 * sd_start_cmds and so on. See panic.c for more information
14809 * about the states the system can be in during panic.
14810 */
14811 if ((un->un_state == SD_STATE_DUMPING) ||
14812 (ddi_in_panic() && (un->un_in_callback > 1))) {
14813 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14814 "sd_start_cmds: panicking\n");
14815 goto exit;
14816 }
14817
14818 if ((bp = immed_bp) != NULL) {
14819 /*
14820 * We have a bp that must be transported immediately.
14821 * It's OK to transport the immed_bp here without doing
14822 * the throttle limit check because the immed_bp is
14823 * always used in a retry/recovery case. This means
14824 * that we know we are not at the throttle limit by
14825 * virtue of the fact that to get here we must have
14826 * already gotten a command back via sdintr(). This also
14827 * relies on (1) the command on un_retry_bp preventing
14828 * further commands from the waitq from being issued;
14829 * and (2) the code in sd_retry_command checking the
14830 * throttle limit before issuing a delayed or immediate
14831 * retry. This holds even if the throttle limit is
14832 * currently ratcheted down from its maximum value.
14833 */
14834 statp = kstat_runq_enter;
14835 if (bp == un->un_retry_bp) {
14836 ASSERT((un->un_retry_statp == NULL) ||
14837 (un->un_retry_statp == kstat_waitq_enter) ||
14838 (un->un_retry_statp ==
14839 kstat_runq_back_to_waitq));
14840 /*
14841 * If the waitq kstat was incremented when
14842 * sd_set_retry_bp() queued this bp for a retry,
14843 * then we must set up statp so that the waitq
14844 * count will get decremented correctly below.
14845 * Also we must clear un->un_retry_statp to
14846 * ensure that we do not act on a stale value
14847 * in this field.
14848 */
14849 if ((un->un_retry_statp == kstat_waitq_enter) ||
14850 (un->un_retry_statp ==
14851 kstat_runq_back_to_waitq)) {
14852 statp = kstat_waitq_to_runq;
14853 }
14854 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14855 saved_statp = un->un_retry_statp;
14856 #endif
14857 un->un_retry_statp = NULL;
14858
14859 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14860 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14861 "un_throttle:%d un_ncmds_in_transport:%d\n",
14862 un, un->un_retry_bp, un->un_throttle,
14863 un->un_ncmds_in_transport);
14864 } else {
14865 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14866 "processing priority bp:0x%p\n", bp);
14867 }
14868
14869 } else if ((bp = un->un_waitq_headp) != NULL) {
14870 /*
14871 * A command on the waitq is ready to go, but do not
14872 * send it if:
14873 *
14874 * (1) the throttle limit has been reached, or
14875 * (2) a retry is pending, or
14876 * (3) a START_STOP_UNIT callback pending, or
14877 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14878 * command is pending.
14879 *
14880 * For all of these conditions, IO processing will
14881 * restart after the condition is cleared.
14882 */
14883 if (un->un_ncmds_in_transport >= un->un_throttle) {
14884 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14885 "sd_start_cmds: exiting, "
14886 "throttle limit reached!\n");
14887 goto exit;
14888 }
14889 if (un->un_retry_bp != NULL) {
14890 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14891 "sd_start_cmds: exiting, retry pending!\n");
14892 goto exit;
14893 }
14894 if (un->un_startstop_timeid != NULL) {
14895 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14896 "sd_start_cmds: exiting, "
14897 "START_STOP pending!\n");
14898 goto exit;
14899 }
14900 if (un->un_direct_priority_timeid != NULL) {
14901 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14902 "sd_start_cmds: exiting, "
14903 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14904 goto exit;
14905 }
14906
14907 /* Dequeue the command */
14908 un->un_waitq_headp = bp->av_forw;
14909 if (un->un_waitq_headp == NULL) {
14910 un->un_waitq_tailp = NULL;
14911 }
14912 bp->av_forw = NULL;
14913 statp = kstat_waitq_to_runq;
14914 SD_TRACE(SD_LOG_IO_CORE, un,
14915 "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14916
14917 } else {
14918 /* No work to do so bail out now */
14919 SD_TRACE(SD_LOG_IO_CORE, un,
14920 "sd_start_cmds: no more work, exiting!\n");
14921 goto exit;
14922 }
14923
14924 /*
14925 * Reset the state to normal. This is the mechanism by which
14926 * the state transitions from either SD_STATE_RWAIT or
14927 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14928 * If state is SD_STATE_PM_CHANGING then this command is
14929 * part of the device power control and the state must
14930 * not be put back to normal. Doing so would would
14931 * allow new commands to proceed when they shouldn't,
14932 * the device may be going off.
14933 */
14934 if ((un->un_state != SD_STATE_SUSPENDED) &&
14935 (un->un_state != SD_STATE_PM_CHANGING)) {
14936 New_state(un, SD_STATE_NORMAL);
14937 }
14938
14939 xp = SD_GET_XBUF(bp);
14940 ASSERT(xp != NULL);
14941
14942 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14943 /*
14944 * Allocate the scsi_pkt if we need one, or attach DMA
14945 * resources if we have a scsi_pkt that needs them. The
14946 * latter should only occur for commands that are being
14947 * retried.
14948 */
14949 if ((xp->xb_pktp == NULL) ||
14950 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14951 #else
14952 if (xp->xb_pktp == NULL) {
14953 #endif
14954 /*
14955 * There is no scsi_pkt allocated for this buf. Call
14956 * the initpkt function to allocate & init one.
14957 *
14958 * The scsi_init_pkt runout callback functionality is
14959 * implemented as follows:
14960 *
14961 * 1) The initpkt function always calls
14962 * scsi_init_pkt(9F) with sdrunout specified as the
14963 * callback routine.
14964 * 2) A successful packet allocation is initialized and
14965 * the I/O is transported.
14966 * 3) The I/O associated with an allocation resource
14967 * failure is left on its queue to be retried via
14968 * runout or the next I/O.
14969 * 4) The I/O associated with a DMA error is removed
14970 * from the queue and failed with EIO. Processing of
14971 * the transport queues is also halted to be
14972 * restarted via runout or the next I/O.
14973 * 5) The I/O associated with a CDB size or packet
14974 * size error is removed from the queue and failed
14975 * with EIO. Processing of the transport queues is
14976 * continued.
14977 *
14978 * Note: there is no interface for canceling a runout
14979 * callback. To prevent the driver from detaching or
14980 * suspending while a runout is pending the driver
14981 * state is set to SD_STATE_RWAIT
14982 *
14983 * Note: using the scsi_init_pkt callback facility can
14984 * result in an I/O request persisting at the head of
14985 * the list which cannot be satisfied even after
14986 * multiple retries. In the future the driver may
14987 * implement some kind of maximum runout count before
14988 * failing an I/O.
14989 *
14990 * Note: the use of funcp below may seem superfluous,
14991 * but it helps warlock figure out the correct
14992 * initpkt function calls (see [s]sd.wlcmd).
14993 */
14994 struct scsi_pkt *pktp;
14995 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14996
14997 ASSERT(bp != un->un_rqs_bp);
14998
14999 funcp = sd_initpkt_map[xp->xb_chain_iostart];
15000 switch ((*funcp)(bp, &pktp)) {
15001 case SD_PKT_ALLOC_SUCCESS:
15002 xp->xb_pktp = pktp;
15003 SD_TRACE(SD_LOG_IO_CORE, un,
15004 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
15005 pktp);
15006 goto got_pkt;
15007
15008 case SD_PKT_ALLOC_FAILURE:
15009 /*
15010 * Temporary (hopefully) resource depletion.
15011 * Since retries and RQS commands always have a
15012 * scsi_pkt allocated, these cases should never
15013 * get here. So the only cases this needs to
15014 * handle is a bp from the waitq (which we put
15015 * back onto the waitq for sdrunout), or a bp
15016 * sent as an immed_bp (which we just fail).
15017 */
15018 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15019 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
15020
15021 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15022
15023 if (bp == immed_bp) {
15024 /*
15025 * If SD_XB_DMA_FREED is clear, then
15026 * this is a failure to allocate a
15027 * scsi_pkt, and we must fail the
15028 * command.
15029 */
15030 if ((xp->xb_pkt_flags &
15031 SD_XB_DMA_FREED) == 0) {
15032 break;
15033 }
15034
15035 /*
15036 * If this immediate command is NOT our
15037 * un_retry_bp, then we must fail it.
15038 */
15039 if (bp != un->un_retry_bp) {
15040 break;
15041 }
15042
15043 /*
15044 * We get here if this cmd is our
15045 * un_retry_bp that was DMAFREED, but
15046 * scsi_init_pkt() failed to reallocate
15047 * DMA resources when we attempted to
15048 * retry it. This can happen when an
15049 * mpxio failover is in progress, but
15050 * we don't want to just fail the
15051 * command in this case.
15052 *
15053 * Use timeout(9F) to restart it after
15054 * a 100ms delay. We don't want to
15055 * let sdrunout() restart it, because
15056 * sdrunout() is just supposed to start
15057 * commands that are sitting on the
15058 * wait queue. The un_retry_bp stays
15059 * set until the command completes, but
15060 * sdrunout can be called many times
15061 * before that happens. Since sdrunout
15062 * cannot tell if the un_retry_bp is
15063 * already in the transport, it could
15064 * end up calling scsi_transport() for
15065 * the un_retry_bp multiple times.
15066 *
15067 * Also: don't schedule the callback
15068 * if some other callback is already
15069 * pending.
15070 */
15071 if (un->un_retry_statp == NULL) {
15072 /*
15073 * restore the kstat pointer to
15074 * keep kstat counts coherent
15075 * when we do retry the command.
15076 */
15077 un->un_retry_statp =
15078 saved_statp;
15079 }
15080
15081 if ((un->un_startstop_timeid == NULL) &&
15082 (un->un_retry_timeid == NULL) &&
15083 (un->un_direct_priority_timeid ==
15084 NULL)) {
15085
15086 un->un_retry_timeid =
15087 timeout(
15088 sd_start_retry_command,
15089 un, SD_RESTART_TIMEOUT);
15090 }
15091 goto exit;
15092 }
15093
15094 #else
15095 if (bp == immed_bp) {
15096 break; /* Just fail the command */
15097 }
15098 #endif
15099
15100 /* Add the buf back to the head of the waitq */
15101 bp->av_forw = un->un_waitq_headp;
15102 un->un_waitq_headp = bp;
15103 if (un->un_waitq_tailp == NULL) {
15104 un->un_waitq_tailp = bp;
15105 }
15106 goto exit;
15107
15108 case SD_PKT_ALLOC_FAILURE_NO_DMA:
15109 /*
15110 * HBA DMA resource failure. Fail the command
15111 * and continue processing of the queues.
15112 */
15113 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15114 "sd_start_cmds: "
15115 "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15116 break;
15117
15118 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15119 /*
15120 * Note:x86: Partial DMA mapping not supported
15121 * for USCSI commands, and all the needed DMA
15122 * resources were not allocated.
15123 */
15124 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15125 "sd_start_cmds: "
15126 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15127 break;
15128
15129 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15130 /*
15131 * Note:x86: Request cannot fit into CDB based
15132 * on lba and len.
15133 */
15134 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15135 "sd_start_cmds: "
15136 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15137 break;
15138
15139 default:
15140 /* Should NEVER get here! */
15141 panic("scsi_initpkt error");
15142 /*NOTREACHED*/
15143 }
15144
15145 /*
15146 * Fatal error in allocating a scsi_pkt for this buf.
15147 * Update kstats & return the buf with an error code.
15148 * We must use sd_return_failed_command_no_restart() to
15149 * avoid a recursive call back into sd_start_cmds().
15150 * However this also means that we must keep processing
15151 * the waitq here in order to avoid stalling.
15152 */
15153 if (statp == kstat_waitq_to_runq) {
15154 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15155 }
15156 sd_return_failed_command_no_restart(un, bp, EIO);
15157 if (bp == immed_bp) {
15158 /* immed_bp is gone by now, so clear this */
15159 immed_bp = NULL;
15160 }
15161 continue;
15162 }
15163 got_pkt:
15164 if (bp == immed_bp) {
15165 /* goto the head of the class.... */
15166 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15167 }
15168
15169 un->un_ncmds_in_transport++;
15170 SD_UPDATE_KSTATS(un, statp, bp);
15171
15172 /*
15173 * Call scsi_transport() to send the command to the target.
15174 * According to SCSA architecture, we must drop the mutex here
15175 * before calling scsi_transport() in order to avoid deadlock.
15176 * Note that the scsi_pkt's completion routine can be executed
15177 * (from interrupt context) even before the call to
15178 * scsi_transport() returns.
15179 */
15180 SD_TRACE(SD_LOG_IO_CORE, un,
15181 "sd_start_cmds: calling scsi_transport()\n");
15182 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15183
15184 mutex_exit(SD_MUTEX(un));
15185 rval = scsi_transport(xp->xb_pktp);
15186 mutex_enter(SD_MUTEX(un));
15187
15188 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15189 "sd_start_cmds: scsi_transport() returned %d\n", rval);
15190
15191 switch (rval) {
15192 case TRAN_ACCEPT:
15193 /* Clear this with every pkt accepted by the HBA */
15194 un->un_tran_fatal_count = 0;
15195 break; /* Success; try the next cmd (if any) */
15196
15197 case TRAN_BUSY:
15198 un->un_ncmds_in_transport--;
15199 ASSERT(un->un_ncmds_in_transport >= 0);
15200
15201 /*
15202 * Don't retry request sense, the sense data
15203 * is lost when another request is sent.
15204 * Free up the rqs buf and retry
15205 * the original failed cmd. Update kstat.
15206 */
15207 if (bp == un->un_rqs_bp) {
15208 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15209 bp = sd_mark_rqs_idle(un, xp);
15210 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15211 NULL, NULL, EIO, un->un_busy_timeout / 500,
15212 kstat_waitq_enter);
15213 goto exit;
15214 }
15215
15216 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15217 /*
15218 * Free the DMA resources for the scsi_pkt. This will
15219 * allow mpxio to select another path the next time
15220 * we call scsi_transport() with this scsi_pkt.
15221 * See sdintr() for the rationalization behind this.
15222 */
15223 if ((un->un_f_is_fibre == TRUE) &&
15224 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15225 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15226 scsi_dmafree(xp->xb_pktp);
15227 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15228 }
15229 #endif
15230
15231 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15232 /*
15233 * Commands that are SD_PATH_DIRECT_PRIORITY
15234 * are for error recovery situations. These do
15235 * not use the normal command waitq, so if they
15236 * get a TRAN_BUSY we cannot put them back onto
15237 * the waitq for later retry. One possible
15238 * problem is that there could already be some
15239 * other command on un_retry_bp that is waiting
15240 * for this one to complete, so we would be
15241 * deadlocked if we put this command back onto
15242 * the waitq for later retry (since un_retry_bp
15243 * must complete before the driver gets back to
15244 * commands on the waitq).
15245 *
15246 * To avoid deadlock we must schedule a callback
15247 * that will restart this command after a set
15248 * interval. This should keep retrying for as
15249 * long as the underlying transport keeps
15250 * returning TRAN_BUSY (just like for other
15251 * commands). Use the same timeout interval as
15252 * for the ordinary TRAN_BUSY retry.
15253 */
15254 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15255 "sd_start_cmds: scsi_transport() returned "
15256 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15257
15258 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15259 un->un_direct_priority_timeid =
15260 timeout(sd_start_direct_priority_command,
15261 bp, un->un_busy_timeout / 500);
15262
15263 goto exit;
15264 }
15265
15266 /*
15267 * For TRAN_BUSY, we want to reduce the throttle value,
15268 * unless we are retrying a command.
15269 */
15270 if (bp != un->un_retry_bp) {
15271 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15272 }
15273
15274 /*
15275 * Set up the bp to be tried again 10 ms later.
15276 * Note:x86: Is there a timeout value in the sd_lun
15277 * for this condition?
15278 */
15279 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15280 kstat_runq_back_to_waitq);
15281 goto exit;
15282
15283 case TRAN_FATAL_ERROR:
15284 un->un_tran_fatal_count++;
15285 /* FALLTHRU */
15286
15287 case TRAN_BADPKT:
15288 default:
15289 un->un_ncmds_in_transport--;
15290 ASSERT(un->un_ncmds_in_transport >= 0);
15291
15292 /*
15293 * If this is our REQUEST SENSE command with a
15294 * transport error, we must get back the pointers
15295 * to the original buf, and mark the REQUEST
15296 * SENSE command as "available".
15297 */
15298 if (bp == un->un_rqs_bp) {
15299 bp = sd_mark_rqs_idle(un, xp);
15300 xp = SD_GET_XBUF(bp);
15301 } else {
15302 /*
15303 * Legacy behavior: do not update transport
15304 * error count for request sense commands.
15305 */
15306 SD_UPDATE_ERRSTATS(un, sd_transerrs);
15307 }
15308
15309 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15310 sd_print_transport_rejected_message(un, xp, rval);
15311
15312 /*
15313 * This command will be terminated by SD driver due
15314 * to a fatal transport error. We should post
15315 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15316 * of "fail" for any command to indicate this
15317 * situation.
15318 */
15319 if (xp->xb_ena > 0) {
15320 ASSERT(un->un_fm_private != NULL);
15321 sfip = un->un_fm_private;
15322 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15323 sd_ssc_extract_info(&sfip->fm_ssc, un,
15324 xp->xb_pktp, bp, xp);
15325 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15326 }
15327
15328 /*
15329 * We must use sd_return_failed_command_no_restart() to
15330 * avoid a recursive call back into sd_start_cmds().
15331 * However this also means that we must keep processing
15332 * the waitq here in order to avoid stalling.
15333 */
15334 sd_return_failed_command_no_restart(un, bp, EIO);
15335
15336 /*
15337 * Notify any threads waiting in sd_ddi_suspend() that
15338 * a command completion has occurred.
15339 */
15340 if (un->un_state == SD_STATE_SUSPENDED) {
15341 cv_broadcast(&un->un_disk_busy_cv);
15342 }
15343
15344 if (bp == immed_bp) {
15345 /* immed_bp is gone by now, so clear this */
15346 immed_bp = NULL;
15347 }
15348 break;
15349 }
15350
15351 } while (immed_bp == NULL);
15352
15353 exit:
15354 ASSERT(mutex_owned(SD_MUTEX(un)));
15355 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15356 }
15357
15358
15359 /*
15360 * Function: sd_return_command
15361 *
15362 * Description: Returns a command to its originator (with or without an
15363 * error). Also starts commands waiting to be transported
15364 * to the target.
15365 *
15366 * Context: May be called from interrupt, kernel, or timeout context
15367 */
15368
15369 static void
15370 sd_return_command(struct sd_lun *un, struct buf *bp)
15371 {
15372 struct sd_xbuf *xp;
15373 struct scsi_pkt *pktp;
15374 struct sd_fm_internal *sfip;
15375
15376 ASSERT(bp != NULL);
15377 ASSERT(un != NULL);
15378 ASSERT(mutex_owned(SD_MUTEX(un)));
15379 ASSERT(bp != un->un_rqs_bp);
15380 xp = SD_GET_XBUF(bp);
15381 ASSERT(xp != NULL);
15382
15383 pktp = SD_GET_PKTP(bp);
15384 sfip = (struct sd_fm_internal *)un->un_fm_private;
15385 ASSERT(sfip != NULL);
15386
15387 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15388
15389 /*
15390 * Note: check for the "sdrestart failed" case.
15391 */
15392 if ((un->un_partial_dma_supported == 1) &&
15393 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15394 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15395 (xp->xb_pktp->pkt_resid == 0)) {
15396
15397 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15398 /*
15399 * Successfully set up next portion of cmd
15400 * transfer, try sending it
15401 */
15402 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15403 NULL, NULL, 0, (clock_t)0, NULL);
15404 sd_start_cmds(un, NULL);
15405 return; /* Note:x86: need a return here? */
15406 }
15407 }
15408
15409 /*
15410 * If this is the failfast bp, clear it from un_failfast_bp. This
15411 * can happen if upon being re-tried the failfast bp either
15412 * succeeded or encountered another error (possibly even a different
15413 * error than the one that precipitated the failfast state, but in
15414 * that case it would have had to exhaust retries as well). Regardless,
15415 * this should not occur whenever the instance is in the active
15416 * failfast state.
15417 */
15418 if (bp == un->un_failfast_bp) {
15419 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15420 un->un_failfast_bp = NULL;
15421 }
15422
15423 /*
15424 * Clear the failfast state upon successful completion of ANY cmd.
15425 */
15426 if (bp->b_error == 0) {
15427 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15428 /*
15429 * If this is a successful command, but used to be retried,
15430 * we will take it as a recovered command and post an
15431 * ereport with driver-assessment of "recovered".
15432 */
15433 if (xp->xb_ena > 0) {
15434 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15435 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15436 }
15437 } else {
15438 /*
15439 * If this is a failed non-USCSI command we will post an
15440 * ereport with driver-assessment set accordingly("fail" or
15441 * "fatal").
15442 */
15443 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15444 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15445 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15446 }
15447 }
15448
15449 /*
15450 * This is used if the command was retried one or more times. Show that
15451 * we are done with it, and allow processing of the waitq to resume.
15452 */
15453 if (bp == un->un_retry_bp) {
15454 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15455 "sd_return_command: un:0x%p: "
15456 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15457 un->un_retry_bp = NULL;
15458 un->un_retry_statp = NULL;
15459 }
15460
15461 SD_UPDATE_RDWR_STATS(un, bp);
15462 SD_UPDATE_PARTITION_STATS(un, bp);
15463
15464 switch (un->un_state) {
15465 case SD_STATE_SUSPENDED:
15466 /*
15467 * Notify any threads waiting in sd_ddi_suspend() that
15468 * a command completion has occurred.
15469 */
15470 cv_broadcast(&un->un_disk_busy_cv);
15471 break;
15472 default:
15473 sd_start_cmds(un, NULL);
15474 break;
15475 }
15476
15477 /* Return this command up the iodone chain to its originator. */
15478 mutex_exit(SD_MUTEX(un));
15479
15480 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15481 xp->xb_pktp = NULL;
15482
15483 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15484
15485 ASSERT(!mutex_owned(SD_MUTEX(un)));
15486 mutex_enter(SD_MUTEX(un));
15487
15488 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15489 }
15490
15491
15492 /*
15493 * Function: sd_return_failed_command
15494 *
15495 * Description: Command completion when an error occurred.
15496 *
15497 * Context: May be called from interrupt context
15498 */
15499
15500 static void
15501 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15502 {
15503 ASSERT(bp != NULL);
15504 ASSERT(un != NULL);
15505 ASSERT(mutex_owned(SD_MUTEX(un)));
15506
15507 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15508 "sd_return_failed_command: entry\n");
15509
15510 /*
15511 * b_resid could already be nonzero due to a partial data
15512 * transfer, so do not change it here.
15513 */
15514 SD_BIOERROR(bp, errcode);
15515
15516 sd_return_command(un, bp);
15517 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15518 "sd_return_failed_command: exit\n");
15519 }
15520
15521
15522 /*
15523 * Function: sd_return_failed_command_no_restart
15524 *
15525 * Description: Same as sd_return_failed_command, but ensures that no
15526 * call back into sd_start_cmds will be issued.
15527 *
15528 * Context: May be called from interrupt context
15529 */
15530
15531 static void
15532 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15533 int errcode)
15534 {
15535 struct sd_xbuf *xp;
15536
15537 ASSERT(bp != NULL);
15538 ASSERT(un != NULL);
15539 ASSERT(mutex_owned(SD_MUTEX(un)));
15540 xp = SD_GET_XBUF(bp);
15541 ASSERT(xp != NULL);
15542 ASSERT(errcode != 0);
15543
15544 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15545 "sd_return_failed_command_no_restart: entry\n");
15546
15547 /*
15548 * b_resid could already be nonzero due to a partial data
15549 * transfer, so do not change it here.
15550 */
15551 SD_BIOERROR(bp, errcode);
15552
15553 /*
15554 * If this is the failfast bp, clear it. This can happen if the
15555 * failfast bp encounterd a fatal error when we attempted to
15556 * re-try it (such as a scsi_transport(9F) failure). However
15557 * we should NOT be in an active failfast state if the failfast
15558 * bp is not NULL.
15559 */
15560 if (bp == un->un_failfast_bp) {
15561 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15562 un->un_failfast_bp = NULL;
15563 }
15564
15565 if (bp == un->un_retry_bp) {
15566 /*
15567 * This command was retried one or more times. Show that we are
15568 * done with it, and allow processing of the waitq to resume.
15569 */
15570 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15571 "sd_return_failed_command_no_restart: "
15572 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15573 un->un_retry_bp = NULL;
15574 un->un_retry_statp = NULL;
15575 }
15576
15577 SD_UPDATE_RDWR_STATS(un, bp);
15578 SD_UPDATE_PARTITION_STATS(un, bp);
15579
15580 mutex_exit(SD_MUTEX(un));
15581
15582 if (xp->xb_pktp != NULL) {
15583 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15584 xp->xb_pktp = NULL;
15585 }
15586
15587 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15588
15589 mutex_enter(SD_MUTEX(un));
15590
15591 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15592 "sd_return_failed_command_no_restart: exit\n");
15593 }
15594
15595
15596 /*
15597 * Function: sd_retry_command
15598 *
15599 * Description: queue up a command for retry, or (optionally) fail it
15600 * if retry counts are exhausted.
15601 *
15602 * Arguments: un - Pointer to the sd_lun struct for the target.
15603 *
15604 * bp - Pointer to the buf for the command to be retried.
15605 *
15606 * retry_check_flag - Flag to see which (if any) of the retry
15607 * counts should be decremented/checked. If the indicated
15608 * retry count is exhausted, then the command will not be
15609 * retried; it will be failed instead. This should use a
15610 * value equal to one of the following:
15611 *
15612 * SD_RETRIES_NOCHECK
15613 * SD_RESD_RETRIES_STANDARD
15614 * SD_RETRIES_VICTIM
15615 *
15616 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15617 * if the check should be made to see of FLAG_ISOLATE is set
15618 * in the pkt. If FLAG_ISOLATE is set, then the command is
15619 * not retried, it is simply failed.
15620 *
15621 * user_funcp - Ptr to function to call before dispatching the
15622 * command. May be NULL if no action needs to be performed.
15623 * (Primarily intended for printing messages.)
15624 *
15625 * user_arg - Optional argument to be passed along to
15626 * the user_funcp call.
15627 *
15628 * failure_code - errno return code to set in the bp if the
15629 * command is going to be failed.
15630 *
15631 * retry_delay - Retry delay interval in (clock_t) units. May
15632 * be zero which indicates that the retry should be retried
15633 * immediately (ie, without an intervening delay).
15634 *
15635 * statp - Ptr to kstat function to be updated if the command
15636 * is queued for a delayed retry. May be NULL if no kstat
15637 * update is desired.
15638 *
15639 * Context: May be called from interrupt context.
15640 */
15641
15642 static void
15643 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15644 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15645 code), void *user_arg, int failure_code, clock_t retry_delay,
15646 void (*statp)(kstat_io_t *))
15647 {
15648 struct sd_xbuf *xp;
15649 struct scsi_pkt *pktp;
15650 struct sd_fm_internal *sfip;
15651
15652 ASSERT(un != NULL);
15653 ASSERT(mutex_owned(SD_MUTEX(un)));
15654 ASSERT(bp != NULL);
15655 xp = SD_GET_XBUF(bp);
15656 ASSERT(xp != NULL);
15657 pktp = SD_GET_PKTP(bp);
15658 ASSERT(pktp != NULL);
15659
15660 sfip = (struct sd_fm_internal *)un->un_fm_private;
15661 ASSERT(sfip != NULL);
15662
15663 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15664 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15665
15666 /*
15667 * If we are syncing or dumping, fail the command to avoid
15668 * recursively calling back into scsi_transport().
15669 */
15670 if (ddi_in_panic()) {
15671 goto fail_command_no_log;
15672 }
15673
15674 /*
15675 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15676 * log an error and fail the command.
15677 */
15678 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15679 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15680 "ERROR, retrying FLAG_DIAGNOSE command.\n");
15681 sd_dump_memory(un, SD_LOG_IO, "CDB",
15682 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15683 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15684 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15685 goto fail_command;
15686 }
15687
15688 /*
15689 * If we are suspended, then put the command onto head of the
15690 * wait queue since we don't want to start more commands, and
15691 * clear the un_retry_bp. Next time when we are resumed, will
15692 * handle the command in the wait queue.
15693 */
15694 switch (un->un_state) {
15695 case SD_STATE_SUSPENDED:
15696 case SD_STATE_DUMPING:
15697 bp->av_forw = un->un_waitq_headp;
15698 un->un_waitq_headp = bp;
15699 if (un->un_waitq_tailp == NULL) {
15700 un->un_waitq_tailp = bp;
15701 }
15702 if (bp == un->un_retry_bp) {
15703 un->un_retry_bp = NULL;
15704 un->un_retry_statp = NULL;
15705 }
15706 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15707 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15708 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15709 return;
15710 default:
15711 break;
15712 }
15713
15714 /*
15715 * If the caller wants us to check FLAG_ISOLATE, then see if that
15716 * is set; if it is then we do not want to retry the command.
15717 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15718 */
15719 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15720 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15721 goto fail_command;
15722 }
15723 }
15724
15725
15726 /*
15727 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15728 * command timeout or a selection timeout has occurred. This means
15729 * that we were unable to establish an kind of communication with
15730 * the target, and subsequent retries and/or commands are likely
15731 * to encounter similar results and take a long time to complete.
15732 *
15733 * If this is a failfast error condition, we need to update the
15734 * failfast state, even if this bp does not have B_FAILFAST set.
15735 */
15736 if (retry_check_flag & SD_RETRIES_FAILFAST) {
15737 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15738 ASSERT(un->un_failfast_bp == NULL);
15739 /*
15740 * If we are already in the active failfast state, and
15741 * another failfast error condition has been detected,
15742 * then fail this command if it has B_FAILFAST set.
15743 * If B_FAILFAST is clear, then maintain the legacy
15744 * behavior of retrying heroically, even tho this will
15745 * take a lot more time to fail the command.
15746 */
15747 if (bp->b_flags & B_FAILFAST) {
15748 goto fail_command;
15749 }
15750 } else {
15751 /*
15752 * We're not in the active failfast state, but we
15753 * have a failfast error condition, so we must begin
15754 * transition to the next state. We do this regardless
15755 * of whether or not this bp has B_FAILFAST set.
15756 */
15757 if (un->un_failfast_bp == NULL) {
15758 /*
15759 * This is the first bp to meet a failfast
15760 * condition so save it on un_failfast_bp &
15761 * do normal retry processing. Do not enter
15762 * active failfast state yet. This marks
15763 * entry into the "failfast pending" state.
15764 */
15765 un->un_failfast_bp = bp;
15766
15767 } else if (un->un_failfast_bp == bp) {
15768 /*
15769 * This is the second time *this* bp has
15770 * encountered a failfast error condition,
15771 * so enter active failfast state & flush
15772 * queues as appropriate.
15773 */
15774 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15775 un->un_failfast_bp = NULL;
15776 sd_failfast_flushq(un);
15777
15778 /*
15779 * Fail this bp now if B_FAILFAST set;
15780 * otherwise continue with retries. (It would
15781 * be pretty ironic if this bp succeeded on a
15782 * subsequent retry after we just flushed all
15783 * the queues).
15784 */
15785 if (bp->b_flags & B_FAILFAST) {
15786 goto fail_command;
15787 }
15788
15789 #if !defined(lint) && !defined(__lint)
15790 } else {
15791 /*
15792 * If neither of the preceeding conditionals
15793 * was true, it means that there is some
15794 * *other* bp that has met an inital failfast
15795 * condition and is currently either being
15796 * retried or is waiting to be retried. In
15797 * that case we should perform normal retry
15798 * processing on *this* bp, since there is a
15799 * chance that the current failfast condition
15800 * is transient and recoverable. If that does
15801 * not turn out to be the case, then retries
15802 * will be cleared when the wait queue is
15803 * flushed anyway.
15804 */
15805 #endif
15806 }
15807 }
15808 } else {
15809 /*
15810 * SD_RETRIES_FAILFAST is clear, which indicates that we
15811 * likely were able to at least establish some level of
15812 * communication with the target and subsequent commands
15813 * and/or retries are likely to get through to the target,
15814 * In this case we want to be aggressive about clearing
15815 * the failfast state. Note that this does not affect
15816 * the "failfast pending" condition.
15817 */
15818 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15819 }
15820
15821
15822 /*
15823 * Check the specified retry count to see if we can still do
15824 * any retries with this pkt before we should fail it.
15825 */
15826 switch (retry_check_flag & SD_RETRIES_MASK) {
15827 case SD_RETRIES_VICTIM:
15828 /*
15829 * Check the victim retry count. If exhausted, then fall
15830 * thru & check against the standard retry count.
15831 */
15832 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15833 /* Increment count & proceed with the retry */
15834 xp->xb_victim_retry_count++;
15835 break;
15836 }
15837 /* Victim retries exhausted, fall back to std. retries... */
15838 /* FALLTHRU */
15839
15840 case SD_RETRIES_STANDARD:
15841 if (xp->xb_retry_count >= un->un_retry_count) {
15842 /* Retries exhausted, fail the command */
15843 SD_TRACE(SD_LOG_IO_CORE, un,
15844 "sd_retry_command: retries exhausted!\n");
15845 /*
15846 * update b_resid for failed SCMD_READ & SCMD_WRITE
15847 * commands with nonzero pkt_resid.
15848 */
15849 if ((pktp->pkt_reason == CMD_CMPLT) &&
15850 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15851 (pktp->pkt_resid != 0)) {
15852 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15853 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15854 SD_UPDATE_B_RESID(bp, pktp);
15855 }
15856 }
15857 goto fail_command;
15858 }
15859 xp->xb_retry_count++;
15860 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15861 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15862 break;
15863
15864 case SD_RETRIES_UA:
15865 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15866 /* Retries exhausted, fail the command */
15867 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15868 "Unit Attention retries exhausted. "
15869 "Check the target.\n");
15870 goto fail_command;
15871 }
15872 xp->xb_ua_retry_count++;
15873 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15874 "sd_retry_command: retry count:%d\n",
15875 xp->xb_ua_retry_count);
15876 break;
15877
15878 case SD_RETRIES_BUSY:
15879 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15880 /* Retries exhausted, fail the command */
15881 SD_TRACE(SD_LOG_IO_CORE, un,
15882 "sd_retry_command: retries exhausted!\n");
15883 goto fail_command;
15884 }
15885 xp->xb_retry_count++;
15886 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15887 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15888 break;
15889
15890 case SD_RETRIES_NOCHECK:
15891 default:
15892 /* No retry count to check. Just proceed with the retry */
15893 break;
15894 }
15895
15896 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15897
15898 /*
15899 * If this is a non-USCSI command being retried
15900 * during execution last time, we should post an ereport with
15901 * driver-assessment of the value "retry".
15902 * For partial DMA, request sense and STATUS_QFULL, there are no
15903 * hardware errors, we bypass ereport posting.
15904 */
15905 if (failure_code != 0) {
15906 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15907 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15908 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15909 }
15910 }
15911
15912 /*
15913 * If we were given a zero timeout, we must attempt to retry the
15914 * command immediately (ie, without a delay).
15915 */
15916 if (retry_delay == 0) {
15917 /*
15918 * Check some limiting conditions to see if we can actually
15919 * do the immediate retry. If we cannot, then we must
15920 * fall back to queueing up a delayed retry.
15921 */
15922 if (un->un_ncmds_in_transport >= un->un_throttle) {
15923 /*
15924 * We are at the throttle limit for the target,
15925 * fall back to delayed retry.
15926 */
15927 retry_delay = un->un_busy_timeout;
15928 statp = kstat_waitq_enter;
15929 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15930 "sd_retry_command: immed. retry hit "
15931 "throttle!\n");
15932 } else {
15933 /*
15934 * We're clear to proceed with the immediate retry.
15935 * First call the user-provided function (if any)
15936 */
15937 if (user_funcp != NULL) {
15938 (*user_funcp)(un, bp, user_arg,
15939 SD_IMMEDIATE_RETRY_ISSUED);
15940 #ifdef __lock_lint
15941 sd_print_incomplete_msg(un, bp, user_arg,
15942 SD_IMMEDIATE_RETRY_ISSUED);
15943 sd_print_cmd_incomplete_msg(un, bp, user_arg,
15944 SD_IMMEDIATE_RETRY_ISSUED);
15945 sd_print_sense_failed_msg(un, bp, user_arg,
15946 SD_IMMEDIATE_RETRY_ISSUED);
15947 #endif
15948 }
15949
15950 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15951 "sd_retry_command: issuing immediate retry\n");
15952
15953 /*
15954 * Call sd_start_cmds() to transport the command to
15955 * the target.
15956 */
15957 sd_start_cmds(un, bp);
15958
15959 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15960 "sd_retry_command exit\n");
15961 return;
15962 }
15963 }
15964
15965 /*
15966 * Set up to retry the command after a delay.
15967 * First call the user-provided function (if any)
15968 */
15969 if (user_funcp != NULL) {
15970 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15971 }
15972
15973 sd_set_retry_bp(un, bp, retry_delay, statp);
15974
15975 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15976 return;
15977
15978 fail_command:
15979
15980 if (user_funcp != NULL) {
15981 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15982 }
15983
15984 fail_command_no_log:
15985
15986 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15987 "sd_retry_command: returning failed command\n");
15988
15989 sd_return_failed_command(un, bp, failure_code);
15990
15991 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15992 }
15993
15994
15995 /*
15996 * Function: sd_set_retry_bp
15997 *
15998 * Description: Set up the given bp for retry.
15999 *
16000 * Arguments: un - ptr to associated softstate
16001 * bp - ptr to buf(9S) for the command
16002 * retry_delay - time interval before issuing retry (may be 0)
16003 * statp - optional pointer to kstat function
16004 *
16005 * Context: May be called under interrupt context
16006 */
16007
16008 static void
16009 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
16010 void (*statp)(kstat_io_t *))
16011 {
16012 ASSERT(un != NULL);
16013 ASSERT(mutex_owned(SD_MUTEX(un)));
16014 ASSERT(bp != NULL);
16015
16016 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16017 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
16018
16019 /*
16020 * Indicate that the command is being retried. This will not allow any
16021 * other commands on the wait queue to be transported to the target
16022 * until this command has been completed (success or failure). The
16023 * "retry command" is not transported to the target until the given
16024 * time delay expires, unless the user specified a 0 retry_delay.
16025 *
16026 * Note: the timeout(9F) callback routine is what actually calls
16027 * sd_start_cmds() to transport the command, with the exception of a
16028 * zero retry_delay. The only current implementor of a zero retry delay
16029 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16030 */
16031 if (un->un_retry_bp == NULL) {
16032 ASSERT(un->un_retry_statp == NULL);
16033 un->un_retry_bp = bp;
16034
16035 /*
16036 * If the user has not specified a delay the command should
16037 * be queued and no timeout should be scheduled.
16038 */
16039 if (retry_delay == 0) {
16040 /*
16041 * Save the kstat pointer that will be used in the
16042 * call to SD_UPDATE_KSTATS() below, so that
16043 * sd_start_cmds() can correctly decrement the waitq
16044 * count when it is time to transport this command.
16045 */
16046 un->un_retry_statp = statp;
16047 goto done;
16048 }
16049 }
16050
16051 if (un->un_retry_bp == bp) {
16052 /*
16053 * Save the kstat pointer that will be used in the call to
16054 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16055 * correctly decrement the waitq count when it is time to
16056 * transport this command.
16057 */
16058 un->un_retry_statp = statp;
16059
16060 /*
16061 * Schedule a timeout if:
16062 * 1) The user has specified a delay.
16063 * 2) There is not a START_STOP_UNIT callback pending.
16064 *
16065 * If no delay has been specified, then it is up to the caller
16066 * to ensure that IO processing continues without stalling.
16067 * Effectively, this means that the caller will issue the
16068 * required call to sd_start_cmds(). The START_STOP_UNIT
16069 * callback does this after the START STOP UNIT command has
16070 * completed. In either of these cases we should not schedule
16071 * a timeout callback here. Also don't schedule the timeout if
16072 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16073 */
16074 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16075 (un->un_direct_priority_timeid == NULL)) {
16076 un->un_retry_timeid =
16077 timeout(sd_start_retry_command, un, retry_delay);
16078 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16079 "sd_set_retry_bp: setting timeout: un: 0x%p"
16080 " bp:0x%p un_retry_timeid:0x%p\n",
16081 un, bp, un->un_retry_timeid);
16082 }
16083 } else {
16084 /*
16085 * We only get in here if there is already another command
16086 * waiting to be retried. In this case, we just put the
16087 * given command onto the wait queue, so it can be transported
16088 * after the current retry command has completed.
16089 *
16090 * Also we have to make sure that if the command at the head
16091 * of the wait queue is the un_failfast_bp, that we do not
16092 * put ahead of it any other commands that are to be retried.
16093 */
16094 if ((un->un_failfast_bp != NULL) &&
16095 (un->un_failfast_bp == un->un_waitq_headp)) {
16096 /*
16097 * Enqueue this command AFTER the first command on
16098 * the wait queue (which is also un_failfast_bp).
16099 */
16100 bp->av_forw = un->un_waitq_headp->av_forw;
16101 un->un_waitq_headp->av_forw = bp;
16102 if (un->un_waitq_headp == un->un_waitq_tailp) {
16103 un->un_waitq_tailp = bp;
16104 }
16105 } else {
16106 /* Enqueue this command at the head of the waitq. */
16107 bp->av_forw = un->un_waitq_headp;
16108 un->un_waitq_headp = bp;
16109 if (un->un_waitq_tailp == NULL) {
16110 un->un_waitq_tailp = bp;
16111 }
16112 }
16113
16114 if (statp == NULL) {
16115 statp = kstat_waitq_enter;
16116 }
16117 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16118 "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16119 }
16120
16121 done:
16122 if (statp != NULL) {
16123 SD_UPDATE_KSTATS(un, statp, bp);
16124 }
16125
16126 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16127 "sd_set_retry_bp: exit un:0x%p\n", un);
16128 }
16129
16130
16131 /*
16132 * Function: sd_start_retry_command
16133 *
16134 * Description: Start the command that has been waiting on the target's
16135 * retry queue. Called from timeout(9F) context after the
16136 * retry delay interval has expired.
16137 *
16138 * Arguments: arg - pointer to associated softstate for the device.
16139 *
16140 * Context: timeout(9F) thread context. May not sleep.
16141 */
16142
16143 static void
16144 sd_start_retry_command(void *arg)
16145 {
16146 struct sd_lun *un = arg;
16147
16148 ASSERT(un != NULL);
16149 ASSERT(!mutex_owned(SD_MUTEX(un)));
16150
16151 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16152 "sd_start_retry_command: entry\n");
16153
16154 mutex_enter(SD_MUTEX(un));
16155
16156 un->un_retry_timeid = NULL;
16157
16158 if (un->un_retry_bp != NULL) {
16159 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16160 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16161 un, un->un_retry_bp);
16162 sd_start_cmds(un, un->un_retry_bp);
16163 }
16164
16165 mutex_exit(SD_MUTEX(un));
16166
16167 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16168 "sd_start_retry_command: exit\n");
16169 }
16170
16171 /*
16172 * Function: sd_rmw_msg_print_handler
16173 *
16174 * Description: If RMW mode is enabled and warning message is triggered
16175 * print I/O count during a fixed interval.
16176 *
16177 * Arguments: arg - pointer to associated softstate for the device.
16178 *
16179 * Context: timeout(9F) thread context. May not sleep.
16180 */
16181 static void
16182 sd_rmw_msg_print_handler(void *arg)
16183 {
16184 struct sd_lun *un = arg;
16185
16186 ASSERT(un != NULL);
16187 ASSERT(!mutex_owned(SD_MUTEX(un)));
16188
16189 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16190 "sd_rmw_msg_print_handler: entry\n");
16191
16192 mutex_enter(SD_MUTEX(un));
16193
16194 if (un->un_rmw_incre_count > 0) {
16195 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16196 "%"PRIu64" I/O requests are not aligned with %d disk "
16197 "sector size in %ld seconds. They are handled through "
16198 "Read Modify Write but the performance is very low!\n",
16199 un->un_rmw_incre_count, un->un_tgt_blocksize,
16200 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16201 un->un_rmw_incre_count = 0;
16202 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16203 un, SD_RMW_MSG_PRINT_TIMEOUT);
16204 } else {
16205 un->un_rmw_msg_timeid = NULL;
16206 }
16207
16208 mutex_exit(SD_MUTEX(un));
16209
16210 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16211 "sd_rmw_msg_print_handler: exit\n");
16212 }
16213
16214 /*
16215 * Function: sd_start_direct_priority_command
16216 *
16217 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16218 * received TRAN_BUSY when we called scsi_transport() to send it
16219 * to the underlying HBA. This function is called from timeout(9F)
16220 * context after the delay interval has expired.
16221 *
16222 * Arguments: arg - pointer to associated buf(9S) to be restarted.
16223 *
16224 * Context: timeout(9F) thread context. May not sleep.
16225 */
16226
16227 static void
16228 sd_start_direct_priority_command(void *arg)
16229 {
16230 struct buf *priority_bp = arg;
16231 struct sd_lun *un;
16232
16233 ASSERT(priority_bp != NULL);
16234 un = SD_GET_UN(priority_bp);
16235 ASSERT(un != NULL);
16236 ASSERT(!mutex_owned(SD_MUTEX(un)));
16237
16238 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16239 "sd_start_direct_priority_command: entry\n");
16240
16241 mutex_enter(SD_MUTEX(un));
16242 un->un_direct_priority_timeid = NULL;
16243 sd_start_cmds(un, priority_bp);
16244 mutex_exit(SD_MUTEX(un));
16245
16246 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16247 "sd_start_direct_priority_command: exit\n");
16248 }
16249
16250
16251 /*
16252 * Function: sd_send_request_sense_command
16253 *
16254 * Description: Sends a REQUEST SENSE command to the target
16255 *
16256 * Context: May be called from interrupt context.
16257 */
16258
16259 static void
16260 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16261 struct scsi_pkt *pktp)
16262 {
16263 ASSERT(bp != NULL);
16264 ASSERT(un != NULL);
16265 ASSERT(mutex_owned(SD_MUTEX(un)));
16266
16267 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16268 "entry: buf:0x%p\n", bp);
16269
16270 /*
16271 * If we are syncing or dumping, then fail the command to avoid a
16272 * recursive callback into scsi_transport(). Also fail the command
16273 * if we are suspended (legacy behavior).
16274 */
16275 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16276 (un->un_state == SD_STATE_DUMPING)) {
16277 sd_return_failed_command(un, bp, EIO);
16278 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16279 "sd_send_request_sense_command: syncing/dumping, exit\n");
16280 return;
16281 }
16282
16283 /*
16284 * Retry the failed command and don't issue the request sense if:
16285 * 1) the sense buf is busy
16286 * 2) we have 1 or more outstanding commands on the target
16287 * (the sense data will be cleared or invalidated any way)
16288 *
16289 * Note: There could be an issue with not checking a retry limit here,
16290 * the problem is determining which retry limit to check.
16291 */
16292 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16293 /* Don't retry if the command is flagged as non-retryable */
16294 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16295 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16296 NULL, NULL, 0, un->un_busy_timeout,
16297 kstat_waitq_enter);
16298 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16299 "sd_send_request_sense_command: "
16300 "at full throttle, retrying exit\n");
16301 } else {
16302 sd_return_failed_command(un, bp, EIO);
16303 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16304 "sd_send_request_sense_command: "
16305 "at full throttle, non-retryable exit\n");
16306 }
16307 return;
16308 }
16309
16310 sd_mark_rqs_busy(un, bp);
16311 sd_start_cmds(un, un->un_rqs_bp);
16312
16313 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16314 "sd_send_request_sense_command: exit\n");
16315 }
16316
16317
16318 /*
16319 * Function: sd_mark_rqs_busy
16320 *
16321 * Description: Indicate that the request sense bp for this instance is
16322 * in use.
16323 *
16324 * Context: May be called under interrupt context
16325 */
16326
16327 static void
16328 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16329 {
16330 struct sd_xbuf *sense_xp;
16331
16332 ASSERT(un != NULL);
16333 ASSERT(bp != NULL);
16334 ASSERT(mutex_owned(SD_MUTEX(un)));
16335 ASSERT(un->un_sense_isbusy == 0);
16336
16337 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16338 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16339
16340 sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16341 ASSERT(sense_xp != NULL);
16342
16343 SD_INFO(SD_LOG_IO, un,
16344 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16345
16346 ASSERT(sense_xp->xb_pktp != NULL);
16347 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16348 == (FLAG_SENSING | FLAG_HEAD));
16349
16350 un->un_sense_isbusy = 1;
16351 un->un_rqs_bp->b_resid = 0;
16352 sense_xp->xb_pktp->pkt_resid = 0;
16353 sense_xp->xb_pktp->pkt_reason = 0;
16354
16355 /* So we can get back the bp at interrupt time! */
16356 sense_xp->xb_sense_bp = bp;
16357
16358 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16359
16360 /*
16361 * Mark this buf as awaiting sense data. (This is already set in
16362 * the pkt_flags for the RQS packet.)
16363 */
16364 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16365
16366 /* Request sense down same path */
16367 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16368 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16369 sense_xp->xb_pktp->pkt_path_instance =
16370 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16371
16372 sense_xp->xb_retry_count = 0;
16373 sense_xp->xb_victim_retry_count = 0;
16374 sense_xp->xb_ua_retry_count = 0;
16375 sense_xp->xb_nr_retry_count = 0;
16376 sense_xp->xb_dma_resid = 0;
16377
16378 /* Clean up the fields for auto-request sense */
16379 sense_xp->xb_sense_status = 0;
16380 sense_xp->xb_sense_state = 0;
16381 sense_xp->xb_sense_resid = 0;
16382 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16383
16384 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16385 }
16386
16387
16388 /*
16389 * Function: sd_mark_rqs_idle
16390 *
16391 * Description: SD_MUTEX must be held continuously through this routine
16392 * to prevent reuse of the rqs struct before the caller can
16393 * complete it's processing.
16394 *
16395 * Return Code: Pointer to the RQS buf
16396 *
16397 * Context: May be called under interrupt context
16398 */
16399
16400 static struct buf *
16401 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16402 {
16403 struct buf *bp;
16404 ASSERT(un != NULL);
16405 ASSERT(sense_xp != NULL);
16406 ASSERT(mutex_owned(SD_MUTEX(un)));
16407 ASSERT(un->un_sense_isbusy != 0);
16408
16409 un->un_sense_isbusy = 0;
16410 bp = sense_xp->xb_sense_bp;
16411 sense_xp->xb_sense_bp = NULL;
16412
16413 /* This pkt is no longer interested in getting sense data */
16414 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16415
16416 return (bp);
16417 }
16418
16419
16420
16421 /*
16422 * Function: sd_alloc_rqs
16423 *
16424 * Description: Set up the unit to receive auto request sense data
16425 *
16426 * Return Code: DDI_SUCCESS or DDI_FAILURE
16427 *
16428 * Context: Called under attach(9E) context
16429 */
16430
16431 static int
16432 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16433 {
16434 struct sd_xbuf *xp;
16435
16436 ASSERT(un != NULL);
16437 ASSERT(!mutex_owned(SD_MUTEX(un)));
16438 ASSERT(un->un_rqs_bp == NULL);
16439 ASSERT(un->un_rqs_pktp == NULL);
16440
16441 /*
16442 * First allocate the required buf and scsi_pkt structs, then set up
16443 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16444 */
16445 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16446 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16447 if (un->un_rqs_bp == NULL) {
16448 return (DDI_FAILURE);
16449 }
16450
16451 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16452 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16453
16454 if (un->un_rqs_pktp == NULL) {
16455 sd_free_rqs(un);
16456 return (DDI_FAILURE);
16457 }
16458
16459 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16460 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16461 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16462
16463 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16464
16465 /* Set up the other needed members in the ARQ scsi_pkt. */
16466 un->un_rqs_pktp->pkt_comp = sdintr;
16467 un->un_rqs_pktp->pkt_time = sd_io_time;
16468 un->un_rqs_pktp->pkt_flags |=
16469 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16470
16471 /*
16472 * Allocate & init the sd_xbuf struct for the RQS command. Do not
16473 * provide any intpkt, destroypkt routines as we take care of
16474 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16475 */
16476 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16477 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16478 xp->xb_pktp = un->un_rqs_pktp;
16479 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16480 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
16481 un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16482
16483 /*
16484 * Save the pointer to the request sense private bp so it can
16485 * be retrieved in sdintr.
16486 */
16487 un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16488 ASSERT(un->un_rqs_bp->b_private == xp);
16489
16490 /*
16491 * See if the HBA supports auto-request sense for the specified
16492 * target/lun. If it does, then try to enable it (if not already
16493 * enabled).
16494 *
16495 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16496 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16497 * return success. However, in both of these cases ARQ is always
16498 * enabled and scsi_ifgetcap will always return true. The best approach
16499 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16500 *
16501 * The 3rd case is the HBA (adp) always return enabled on
16502 * scsi_ifgetgetcap even when it's not enable, the best approach
16503 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16504 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16505 */
16506
16507 if (un->un_f_is_fibre == TRUE) {
16508 un->un_f_arq_enabled = TRUE;
16509 } else {
16510 #if defined(__i386) || defined(__amd64)
16511 /*
16512 * Circumvent the Adaptec bug, remove this code when
16513 * the bug is fixed
16514 */
16515 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16516 #endif
16517 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16518 case 0:
16519 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16520 "sd_alloc_rqs: HBA supports ARQ\n");
16521 /*
16522 * ARQ is supported by this HBA but currently is not
16523 * enabled. Attempt to enable it and if successful then
16524 * mark this instance as ARQ enabled.
16525 */
16526 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16527 == 1) {
16528 /* Successfully enabled ARQ in the HBA */
16529 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16530 "sd_alloc_rqs: ARQ enabled\n");
16531 un->un_f_arq_enabled = TRUE;
16532 } else {
16533 /* Could not enable ARQ in the HBA */
16534 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16535 "sd_alloc_rqs: failed ARQ enable\n");
16536 un->un_f_arq_enabled = FALSE;
16537 }
16538 break;
16539 case 1:
16540 /*
16541 * ARQ is supported by this HBA and is already enabled.
16542 * Just mark ARQ as enabled for this instance.
16543 */
16544 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16545 "sd_alloc_rqs: ARQ already enabled\n");
16546 un->un_f_arq_enabled = TRUE;
16547 break;
16548 default:
16549 /*
16550 * ARQ is not supported by this HBA; disable it for this
16551 * instance.
16552 */
16553 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16554 "sd_alloc_rqs: HBA does not support ARQ\n");
16555 un->un_f_arq_enabled = FALSE;
16556 break;
16557 }
16558 }
16559
16560 return (DDI_SUCCESS);
16561 }
16562
16563
16564 /*
16565 * Function: sd_free_rqs
16566 *
16567 * Description: Cleanup for the pre-instance RQS command.
16568 *
16569 * Context: Kernel thread context
16570 */
16571
16572 static void
16573 sd_free_rqs(struct sd_lun *un)
16574 {
16575 ASSERT(un != NULL);
16576
16577 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16578
16579 /*
16580 * If consistent memory is bound to a scsi_pkt, the pkt
16581 * has to be destroyed *before* freeing the consistent memory.
16582 * Don't change the sequence of this operations.
16583 * scsi_destroy_pkt() might access memory, which isn't allowed,
16584 * after it was freed in scsi_free_consistent_buf().
16585 */
16586 if (un->un_rqs_pktp != NULL) {
16587 scsi_destroy_pkt(un->un_rqs_pktp);
16588 un->un_rqs_pktp = NULL;
16589 }
16590
16591 if (un->un_rqs_bp != NULL) {
16592 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16593 if (xp != NULL) {
16594 kmem_free(xp, sizeof (struct sd_xbuf));
16595 }
16596 scsi_free_consistent_buf(un->un_rqs_bp);
16597 un->un_rqs_bp = NULL;
16598 }
16599 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16600 }
16601
16602
16603
16604 /*
16605 * Function: sd_reduce_throttle
16606 *
16607 * Description: Reduces the maximum # of outstanding commands on a
16608 * target to the current number of outstanding commands.
16609 * Queues a tiemout(9F) callback to restore the limit
16610 * after a specified interval has elapsed.
16611 * Typically used when we get a TRAN_BUSY return code
16612 * back from scsi_transport().
16613 *
16614 * Arguments: un - ptr to the sd_lun softstate struct
16615 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16616 *
16617 * Context: May be called from interrupt context
16618 */
16619
16620 static void
16621 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16622 {
16623 ASSERT(un != NULL);
16624 ASSERT(mutex_owned(SD_MUTEX(un)));
16625 ASSERT(un->un_ncmds_in_transport >= 0);
16626
16627 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16628 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16629 un, un->un_throttle, un->un_ncmds_in_transport);
16630
16631 if (un->un_throttle > 1) {
16632 if (un->un_f_use_adaptive_throttle == TRUE) {
16633 switch (throttle_type) {
16634 case SD_THROTTLE_TRAN_BUSY:
16635 if (un->un_busy_throttle == 0) {
16636 un->un_busy_throttle = un->un_throttle;
16637 }
16638 break;
16639 case SD_THROTTLE_QFULL:
16640 un->un_busy_throttle = 0;
16641 break;
16642 default:
16643 ASSERT(FALSE);
16644 }
16645
16646 if (un->un_ncmds_in_transport > 0) {
16647 un->un_throttle = un->un_ncmds_in_transport;
16648 }
16649
16650 } else {
16651 if (un->un_ncmds_in_transport == 0) {
16652 un->un_throttle = 1;
16653 } else {
16654 un->un_throttle = un->un_ncmds_in_transport;
16655 }
16656 }
16657 }
16658
16659 /* Reschedule the timeout if none is currently active */
16660 if (un->un_reset_throttle_timeid == NULL) {
16661 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16662 un, SD_THROTTLE_RESET_INTERVAL);
16663 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16664 "sd_reduce_throttle: timeout scheduled!\n");
16665 }
16666
16667 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16668 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16669 }
16670
16671
16672
16673 /*
16674 * Function: sd_restore_throttle
16675 *
16676 * Description: Callback function for timeout(9F). Resets the current
16677 * value of un->un_throttle to its default.
16678 *
16679 * Arguments: arg - pointer to associated softstate for the device.
16680 *
16681 * Context: May be called from interrupt context
16682 */
16683
16684 static void
16685 sd_restore_throttle(void *arg)
16686 {
16687 struct sd_lun *un = arg;
16688
16689 ASSERT(un != NULL);
16690 ASSERT(!mutex_owned(SD_MUTEX(un)));
16691
16692 mutex_enter(SD_MUTEX(un));
16693
16694 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16695 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16696
16697 un->un_reset_throttle_timeid = NULL;
16698
16699 if (un->un_f_use_adaptive_throttle == TRUE) {
16700 /*
16701 * If un_busy_throttle is nonzero, then it contains the
16702 * value that un_throttle was when we got a TRAN_BUSY back
16703 * from scsi_transport(). We want to revert back to this
16704 * value.
16705 *
16706 * In the QFULL case, the throttle limit will incrementally
16707 * increase until it reaches max throttle.
16708 */
16709 if (un->un_busy_throttle > 0) {
16710 un->un_throttle = un->un_busy_throttle;
16711 un->un_busy_throttle = 0;
16712 } else {
16713 /*
16714 * increase throttle by 10% open gate slowly, schedule
16715 * another restore if saved throttle has not been
16716 * reached
16717 */
16718 short throttle;
16719 if (sd_qfull_throttle_enable) {
16720 throttle = un->un_throttle +
16721 max((un->un_throttle / 10), 1);
16722 un->un_throttle =
16723 (throttle < un->un_saved_throttle) ?
16724 throttle : un->un_saved_throttle;
16725 if (un->un_throttle < un->un_saved_throttle) {
16726 un->un_reset_throttle_timeid =
16727 timeout(sd_restore_throttle,
16728 un,
16729 SD_QFULL_THROTTLE_RESET_INTERVAL);
16730 }
16731 }
16732 }
16733
16734 /*
16735 * If un_throttle has fallen below the low-water mark, we
16736 * restore the maximum value here (and allow it to ratchet
16737 * down again if necessary).
16738 */
16739 if (un->un_throttle < un->un_min_throttle) {
16740 un->un_throttle = un->un_saved_throttle;
16741 }
16742 } else {
16743 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16744 "restoring limit from 0x%x to 0x%x\n",
16745 un->un_throttle, un->un_saved_throttle);
16746 un->un_throttle = un->un_saved_throttle;
16747 }
16748
16749 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16750 "sd_restore_throttle: calling sd_start_cmds!\n");
16751
16752 sd_start_cmds(un, NULL);
16753
16754 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16755 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16756 un, un->un_throttle);
16757
16758 mutex_exit(SD_MUTEX(un));
16759
16760 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16761 }
16762
16763 /*
16764 * Function: sdrunout
16765 *
16766 * Description: Callback routine for scsi_init_pkt when a resource allocation
16767 * fails.
16768 *
16769 * Arguments: arg - a pointer to the sd_lun unit struct for the particular
16770 * soft state instance.
16771 *
16772 * Return Code: The scsi_init_pkt routine allows for the callback function to
16773 * return a 0 indicating the callback should be rescheduled or a 1
16774 * indicating not to reschedule. This routine always returns 1
16775 * because the driver always provides a callback function to
16776 * scsi_init_pkt. This results in a callback always being scheduled
16777 * (via the scsi_init_pkt callback implementation) if a resource
16778 * failure occurs.
16779 *
16780 * Context: This callback function may not block or call routines that block
16781 *
16782 * Note: Using the scsi_init_pkt callback facility can result in an I/O
16783 * request persisting at the head of the list which cannot be
16784 * satisfied even after multiple retries. In the future the driver
16785 * may implement some time of maximum runout count before failing
16786 * an I/O.
16787 */
16788
16789 static int
16790 sdrunout(caddr_t arg)
16791 {
16792 struct sd_lun *un = (struct sd_lun *)arg;
16793
16794 ASSERT(un != NULL);
16795 ASSERT(!mutex_owned(SD_MUTEX(un)));
16796
16797 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16798
16799 mutex_enter(SD_MUTEX(un));
16800 sd_start_cmds(un, NULL);
16801 mutex_exit(SD_MUTEX(un));
16802 /*
16803 * This callback routine always returns 1 (i.e. do not reschedule)
16804 * because we always specify sdrunout as the callback handler for
16805 * scsi_init_pkt inside the call to sd_start_cmds.
16806 */
16807 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16808 return (1);
16809 }
16810
16811
16812 /*
16813 * Function: sdintr
16814 *
16815 * Description: Completion callback routine for scsi_pkt(9S) structs
16816 * sent to the HBA driver via scsi_transport(9F).
16817 *
16818 * Context: Interrupt context
16819 */
16820
16821 static void
16822 sdintr(struct scsi_pkt *pktp)
16823 {
16824 struct buf *bp;
16825 struct sd_xbuf *xp;
16826 struct sd_lun *un;
16827 size_t actual_len;
16828 sd_ssc_t *sscp;
16829
16830 ASSERT(pktp != NULL);
16831 bp = (struct buf *)pktp->pkt_private;
16832 ASSERT(bp != NULL);
16833 xp = SD_GET_XBUF(bp);
16834 ASSERT(xp != NULL);
16835 ASSERT(xp->xb_pktp != NULL);
16836 un = SD_GET_UN(bp);
16837 ASSERT(un != NULL);
16838 ASSERT(!mutex_owned(SD_MUTEX(un)));
16839
16840 #ifdef SD_FAULT_INJECTION
16841
16842 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16843 /* SD FaultInjection */
16844 sd_faultinjection(pktp);
16845
16846 #endif /* SD_FAULT_INJECTION */
16847
16848 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16849 " xp:0x%p, un:0x%p\n", bp, xp, un);
16850
16851 mutex_enter(SD_MUTEX(un));
16852
16853 ASSERT(un->un_fm_private != NULL);
16854 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16855 ASSERT(sscp != NULL);
16856
16857 /* Reduce the count of the #commands currently in transport */
16858 un->un_ncmds_in_transport--;
16859 ASSERT(un->un_ncmds_in_transport >= 0);
16860
16861 /* Increment counter to indicate that the callback routine is active */
16862 un->un_in_callback++;
16863
16864 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16865
16866 #ifdef SDDEBUG
16867 if (bp == un->un_retry_bp) {
16868 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16869 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16870 un, un->un_retry_bp, un->un_ncmds_in_transport);
16871 }
16872 #endif
16873
16874 /*
16875 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16876 * state if needed.
16877 */
16878 if (pktp->pkt_reason == CMD_DEV_GONE) {
16879 /* Prevent multiple console messages for the same failure. */
16880 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16881 un->un_last_pkt_reason = CMD_DEV_GONE;
16882 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16883 "Command failed to complete...Device is gone\n");
16884 }
16885 if (un->un_mediastate != DKIO_DEV_GONE) {
16886 un->un_mediastate = DKIO_DEV_GONE;
16887 cv_broadcast(&un->un_state_cv);
16888 }
16889 /*
16890 * If the command happens to be the REQUEST SENSE command,
16891 * free up the rqs buf and fail the original command.
16892 */
16893 if (bp == un->un_rqs_bp) {
16894 bp = sd_mark_rqs_idle(un, xp);
16895 }
16896 sd_return_failed_command(un, bp, EIO);
16897 goto exit;
16898 }
16899
16900 if (pktp->pkt_state & STATE_XARQ_DONE) {
16901 SD_TRACE(SD_LOG_COMMON, un,
16902 "sdintr: extra sense data received. pkt=%p\n", pktp);
16903 }
16904
16905 /*
16906 * First see if the pkt has auto-request sense data with it....
16907 * Look at the packet state first so we don't take a performance
16908 * hit looking at the arq enabled flag unless absolutely necessary.
16909 */
16910 if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16911 (un->un_f_arq_enabled == TRUE)) {
16912 /*
16913 * The HBA did an auto request sense for this command so check
16914 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16915 * driver command that should not be retried.
16916 */
16917 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16918 /*
16919 * Save the relevant sense info into the xp for the
16920 * original cmd.
16921 */
16922 struct scsi_arq_status *asp;
16923 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16924 xp->xb_sense_status =
16925 *((uchar_t *)(&(asp->sts_rqpkt_status)));
16926 xp->xb_sense_state = asp->sts_rqpkt_state;
16927 xp->xb_sense_resid = asp->sts_rqpkt_resid;
16928 if (pktp->pkt_state & STATE_XARQ_DONE) {
16929 actual_len = MAX_SENSE_LENGTH -
16930 xp->xb_sense_resid;
16931 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16932 MAX_SENSE_LENGTH);
16933 } else {
16934 if (xp->xb_sense_resid > SENSE_LENGTH) {
16935 actual_len = MAX_SENSE_LENGTH -
16936 xp->xb_sense_resid;
16937 } else {
16938 actual_len = SENSE_LENGTH -
16939 xp->xb_sense_resid;
16940 }
16941 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16942 if ((((struct uscsi_cmd *)
16943 (xp->xb_pktinfo))->uscsi_rqlen) >
16944 actual_len) {
16945 xp->xb_sense_resid =
16946 (((struct uscsi_cmd *)
16947 (xp->xb_pktinfo))->
16948 uscsi_rqlen) - actual_len;
16949 } else {
16950 xp->xb_sense_resid = 0;
16951 }
16952 }
16953 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16954 SENSE_LENGTH);
16955 }
16956
16957 /* fail the command */
16958 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16959 "sdintr: arq done and FLAG_DIAGNOSE set\n");
16960 sd_return_failed_command(un, bp, EIO);
16961 goto exit;
16962 }
16963
16964 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
16965 /*
16966 * We want to either retry or fail this command, so free
16967 * the DMA resources here. If we retry the command then
16968 * the DMA resources will be reallocated in sd_start_cmds().
16969 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16970 * causes the *entire* transfer to start over again from the
16971 * beginning of the request, even for PARTIAL chunks that
16972 * have already transferred successfully.
16973 */
16974 if ((un->un_f_is_fibre == TRUE) &&
16975 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16976 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16977 scsi_dmafree(pktp);
16978 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16979 }
16980 #endif
16981
16982 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16983 "sdintr: arq done, sd_handle_auto_request_sense\n");
16984
16985 sd_handle_auto_request_sense(un, bp, xp, pktp);
16986 goto exit;
16987 }
16988
16989 /* Next see if this is the REQUEST SENSE pkt for the instance */
16990 if (pktp->pkt_flags & FLAG_SENSING) {
16991 /* This pktp is from the unit's REQUEST_SENSE command */
16992 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16993 "sdintr: sd_handle_request_sense\n");
16994 sd_handle_request_sense(un, bp, xp, pktp);
16995 goto exit;
16996 }
16997
16998 /*
16999 * Check to see if the command successfully completed as requested;
17000 * this is the most common case (and also the hot performance path).
17001 *
17002 * Requirements for successful completion are:
17003 * pkt_reason is CMD_CMPLT and packet status is status good.
17004 * In addition:
17005 * - A residual of zero indicates successful completion no matter what
17006 * the command is.
17007 * - If the residual is not zero and the command is not a read or
17008 * write, then it's still defined as successful completion. In other
17009 * words, if the command is a read or write the residual must be
17010 * zero for successful completion.
17011 * - If the residual is not zero and the command is a read or
17012 * write, and it's a USCSICMD, then it's still defined as
17013 * successful completion.
17014 */
17015 if ((pktp->pkt_reason == CMD_CMPLT) &&
17016 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
17017
17018 /*
17019 * Since this command is returned with a good status, we
17020 * can reset the count for Sonoma failover.
17021 */
17022 un->un_sonoma_failure_count = 0;
17023
17024 /*
17025 * Return all USCSI commands on good status
17026 */
17027 if (pktp->pkt_resid == 0) {
17028 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17029 "sdintr: returning command for resid == 0\n");
17030 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17031 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17032 SD_UPDATE_B_RESID(bp, pktp);
17033 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17034 "sdintr: returning command for resid != 0\n");
17035 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17036 SD_UPDATE_B_RESID(bp, pktp);
17037 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17038 "sdintr: returning uscsi command\n");
17039 } else {
17040 goto not_successful;
17041 }
17042 sd_return_command(un, bp);
17043
17044 /*
17045 * Decrement counter to indicate that the callback routine
17046 * is done.
17047 */
17048 un->un_in_callback--;
17049 ASSERT(un->un_in_callback >= 0);
17050 mutex_exit(SD_MUTEX(un));
17051
17052 return;
17053 }
17054
17055 not_successful:
17056
17057 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
17058 /*
17059 * The following is based upon knowledge of the underlying transport
17060 * and its use of DMA resources. This code should be removed when
17061 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17062 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17063 * and sd_start_cmds().
17064 *
17065 * Free any DMA resources associated with this command if there
17066 * is a chance it could be retried or enqueued for later retry.
17067 * If we keep the DMA binding then mpxio cannot reissue the
17068 * command on another path whenever a path failure occurs.
17069 *
17070 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17071 * causes the *entire* transfer to start over again from the
17072 * beginning of the request, even for PARTIAL chunks that
17073 * have already transferred successfully.
17074 *
17075 * This is only done for non-uscsi commands (and also skipped for the
17076 * driver's internal RQS command). Also just do this for Fibre Channel
17077 * devices as these are the only ones that support mpxio.
17078 */
17079 if ((un->un_f_is_fibre == TRUE) &&
17080 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17081 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
17082 scsi_dmafree(pktp);
17083 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17084 }
17085 #endif
17086
17087 /*
17088 * The command did not successfully complete as requested so check
17089 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17090 * driver command that should not be retried so just return. If
17091 * FLAG_DIAGNOSE is not set the error will be processed below.
17092 */
17093 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17094 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17095 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17096 /*
17097 * Issue a request sense if a check condition caused the error
17098 * (we handle the auto request sense case above), otherwise
17099 * just fail the command.
17100 */
17101 if ((pktp->pkt_reason == CMD_CMPLT) &&
17102 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17103 sd_send_request_sense_command(un, bp, pktp);
17104 } else {
17105 sd_return_failed_command(un, bp, EIO);
17106 }
17107 goto exit;
17108 }
17109
17110 /*
17111 * The command did not successfully complete as requested so process
17112 * the error, retry, and/or attempt recovery.
17113 */
17114 switch (pktp->pkt_reason) {
17115 case CMD_CMPLT:
17116 switch (SD_GET_PKT_STATUS(pktp)) {
17117 case STATUS_GOOD:
17118 /*
17119 * The command completed successfully with a non-zero
17120 * residual
17121 */
17122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17123 "sdintr: STATUS_GOOD \n");
17124 sd_pkt_status_good(un, bp, xp, pktp);
17125 break;
17126
17127 case STATUS_CHECK:
17128 case STATUS_TERMINATED:
17129 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17130 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17131 sd_pkt_status_check_condition(un, bp, xp, pktp);
17132 break;
17133
17134 case STATUS_BUSY:
17135 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17136 "sdintr: STATUS_BUSY\n");
17137 sd_pkt_status_busy(un, bp, xp, pktp);
17138 break;
17139
17140 case STATUS_RESERVATION_CONFLICT:
17141 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17142 "sdintr: STATUS_RESERVATION_CONFLICT\n");
17143 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17144 break;
17145
17146 case STATUS_QFULL:
17147 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17148 "sdintr: STATUS_QFULL\n");
17149 sd_pkt_status_qfull(un, bp, xp, pktp);
17150 break;
17151
17152 case STATUS_MET:
17153 case STATUS_INTERMEDIATE:
17154 case STATUS_SCSI2:
17155 case STATUS_INTERMEDIATE_MET:
17156 case STATUS_ACA_ACTIVE:
17157 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17158 "Unexpected SCSI status received: 0x%x\n",
17159 SD_GET_PKT_STATUS(pktp));
17160 /*
17161 * Mark the ssc_flags when detected invalid status
17162 * code for non-USCSI command.
17163 */
17164 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17165 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17166 0, "stat-code");
17167 }
17168 sd_return_failed_command(un, bp, EIO);
17169 break;
17170
17171 default:
17172 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17173 "Invalid SCSI status received: 0x%x\n",
17174 SD_GET_PKT_STATUS(pktp));
17175 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17176 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17177 0, "stat-code");
17178 }
17179 sd_return_failed_command(un, bp, EIO);
17180 break;
17181
17182 }
17183 break;
17184
17185 case CMD_INCOMPLETE:
17186 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17187 "sdintr: CMD_INCOMPLETE\n");
17188 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17189 break;
17190 case CMD_TRAN_ERR:
17191 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17192 "sdintr: CMD_TRAN_ERR\n");
17193 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17194 break;
17195 case CMD_RESET:
17196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17197 "sdintr: CMD_RESET \n");
17198 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17199 break;
17200 case CMD_ABORTED:
17201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17202 "sdintr: CMD_ABORTED \n");
17203 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17204 break;
17205 case CMD_TIMEOUT:
17206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17207 "sdintr: CMD_TIMEOUT\n");
17208 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17209 break;
17210 case CMD_UNX_BUS_FREE:
17211 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17212 "sdintr: CMD_UNX_BUS_FREE \n");
17213 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17214 break;
17215 case CMD_TAG_REJECT:
17216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17217 "sdintr: CMD_TAG_REJECT\n");
17218 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17219 break;
17220 default:
17221 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17222 "sdintr: default\n");
17223 /*
17224 * Mark the ssc_flags for detecting invliad pkt_reason.
17225 */
17226 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17227 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17228 0, "pkt-reason");
17229 }
17230 sd_pkt_reason_default(un, bp, xp, pktp);
17231 break;
17232 }
17233
17234 exit:
17235 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17236
17237 /* Decrement counter to indicate that the callback routine is done. */
17238 un->un_in_callback--;
17239 ASSERT(un->un_in_callback >= 0);
17240
17241 /*
17242 * At this point, the pkt has been dispatched, ie, it is either
17243 * being re-tried or has been returned to its caller and should
17244 * not be referenced.
17245 */
17246
17247 mutex_exit(SD_MUTEX(un));
17248 }
17249
17250
17251 /*
17252 * Function: sd_print_incomplete_msg
17253 *
17254 * Description: Prints the error message for a CMD_INCOMPLETE error.
17255 *
17256 * Arguments: un - ptr to associated softstate for the device.
17257 * bp - ptr to the buf(9S) for the command.
17258 * arg - message string ptr
17259 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17260 * or SD_NO_RETRY_ISSUED.
17261 *
17262 * Context: May be called under interrupt context
17263 */
17264
17265 static void
17266 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17267 {
17268 struct scsi_pkt *pktp;
17269 char *msgp;
17270 char *cmdp = arg;
17271
17272 ASSERT(un != NULL);
17273 ASSERT(mutex_owned(SD_MUTEX(un)));
17274 ASSERT(bp != NULL);
17275 ASSERT(arg != NULL);
17276 pktp = SD_GET_PKTP(bp);
17277 ASSERT(pktp != NULL);
17278
17279 switch (code) {
17280 case SD_DELAYED_RETRY_ISSUED:
17281 case SD_IMMEDIATE_RETRY_ISSUED:
17282 msgp = "retrying";
17283 break;
17284 case SD_NO_RETRY_ISSUED:
17285 default:
17286 msgp = "giving up";
17287 break;
17288 }
17289
17290 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17292 "incomplete %s- %s\n", cmdp, msgp);
17293 }
17294 }
17295
17296
17297
17298 /*
17299 * Function: sd_pkt_status_good
17300 *
17301 * Description: Processing for a STATUS_GOOD code in pkt_status.
17302 *
17303 * Context: May be called under interrupt context
17304 */
17305
17306 static void
17307 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17308 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17309 {
17310 char *cmdp;
17311
17312 ASSERT(un != NULL);
17313 ASSERT(mutex_owned(SD_MUTEX(un)));
17314 ASSERT(bp != NULL);
17315 ASSERT(xp != NULL);
17316 ASSERT(pktp != NULL);
17317 ASSERT(pktp->pkt_reason == CMD_CMPLT);
17318 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17319 ASSERT(pktp->pkt_resid != 0);
17320
17321 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17322
17323 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17324 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17325 case SCMD_READ:
17326 cmdp = "read";
17327 break;
17328 case SCMD_WRITE:
17329 cmdp = "write";
17330 break;
17331 default:
17332 SD_UPDATE_B_RESID(bp, pktp);
17333 sd_return_command(un, bp);
17334 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17335 return;
17336 }
17337
17338 /*
17339 * See if we can retry the read/write, preferrably immediately.
17340 * If retries are exhaused, then sd_retry_command() will update
17341 * the b_resid count.
17342 */
17343 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17344 cmdp, EIO, (clock_t)0, NULL);
17345
17346 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17347 }
17348
17349
17350
17351
17352
17353 /*
17354 * Function: sd_handle_request_sense
17355 *
17356 * Description: Processing for non-auto Request Sense command.
17357 *
17358 * Arguments: un - ptr to associated softstate
17359 * sense_bp - ptr to buf(9S) for the RQS command
17360 * sense_xp - ptr to the sd_xbuf for the RQS command
17361 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17362 *
17363 * Context: May be called under interrupt context
17364 */
17365
17366 static void
17367 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17368 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17369 {
17370 struct buf *cmd_bp; /* buf for the original command */
17371 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */
17372 struct scsi_pkt *cmd_pktp; /* pkt for the original command */
17373 size_t actual_len; /* actual sense data length */
17374
17375 ASSERT(un != NULL);
17376 ASSERT(mutex_owned(SD_MUTEX(un)));
17377 ASSERT(sense_bp != NULL);
17378 ASSERT(sense_xp != NULL);
17379 ASSERT(sense_pktp != NULL);
17380
17381 /*
17382 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17383 * RQS command and not the original command.
17384 */
17385 ASSERT(sense_pktp == un->un_rqs_pktp);
17386 ASSERT(sense_bp == un->un_rqs_bp);
17387 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17388 (FLAG_SENSING | FLAG_HEAD));
17389 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17390 FLAG_SENSING) == FLAG_SENSING);
17391
17392 /* These are the bp, xp, and pktp for the original command */
17393 cmd_bp = sense_xp->xb_sense_bp;
17394 cmd_xp = SD_GET_XBUF(cmd_bp);
17395 cmd_pktp = SD_GET_PKTP(cmd_bp);
17396
17397 if (sense_pktp->pkt_reason != CMD_CMPLT) {
17398 /*
17399 * The REQUEST SENSE command failed. Release the REQUEST
17400 * SENSE command for re-use, get back the bp for the original
17401 * command, and attempt to re-try the original command if
17402 * FLAG_DIAGNOSE is not set in the original packet.
17403 */
17404 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17405 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17406 cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17407 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17408 NULL, NULL, EIO, (clock_t)0, NULL);
17409 return;
17410 }
17411 }
17412
17413 /*
17414 * Save the relevant sense info into the xp for the original cmd.
17415 *
17416 * Note: if the request sense failed the state info will be zero
17417 * as set in sd_mark_rqs_busy()
17418 */
17419 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17420 cmd_xp->xb_sense_state = sense_pktp->pkt_state;
17421 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17422 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17423 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17424 SENSE_LENGTH)) {
17425 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17426 MAX_SENSE_LENGTH);
17427 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17428 } else {
17429 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17430 SENSE_LENGTH);
17431 if (actual_len < SENSE_LENGTH) {
17432 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17433 } else {
17434 cmd_xp->xb_sense_resid = 0;
17435 }
17436 }
17437
17438 /*
17439 * Free up the RQS command....
17440 * NOTE:
17441 * Must do this BEFORE calling sd_validate_sense_data!
17442 * sd_validate_sense_data may return the original command in
17443 * which case the pkt will be freed and the flags can no
17444 * longer be touched.
17445 * SD_MUTEX is held through this process until the command
17446 * is dispatched based upon the sense data, so there are
17447 * no race conditions.
17448 */
17449 (void) sd_mark_rqs_idle(un, sense_xp);
17450
17451 /*
17452 * For a retryable command see if we have valid sense data, if so then
17453 * turn it over to sd_decode_sense() to figure out the right course of
17454 * action. Just fail a non-retryable command.
17455 */
17456 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17457 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17458 SD_SENSE_DATA_IS_VALID) {
17459 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17460 }
17461 } else {
17462 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17463 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17464 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17465 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17466 sd_return_failed_command(un, cmd_bp, EIO);
17467 }
17468 }
17469
17470
17471
17472
17473 /*
17474 * Function: sd_handle_auto_request_sense
17475 *
17476 * Description: Processing for auto-request sense information.
17477 *
17478 * Arguments: un - ptr to associated softstate
17479 * bp - ptr to buf(9S) for the command
17480 * xp - ptr to the sd_xbuf for the command
17481 * pktp - ptr to the scsi_pkt(9S) for the command
17482 *
17483 * Context: May be called under interrupt context
17484 */
17485
17486 static void
17487 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17488 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17489 {
17490 struct scsi_arq_status *asp;
17491 size_t actual_len;
17492
17493 ASSERT(un != NULL);
17494 ASSERT(mutex_owned(SD_MUTEX(un)));
17495 ASSERT(bp != NULL);
17496 ASSERT(xp != NULL);
17497 ASSERT(pktp != NULL);
17498 ASSERT(pktp != un->un_rqs_pktp);
17499 ASSERT(bp != un->un_rqs_bp);
17500
17501 /*
17502 * For auto-request sense, we get a scsi_arq_status back from
17503 * the HBA, with the sense data in the sts_sensedata member.
17504 * The pkt_scbp of the packet points to this scsi_arq_status.
17505 */
17506 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17507
17508 if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17509 /*
17510 * The auto REQUEST SENSE failed; see if we can re-try
17511 * the original command.
17512 */
17513 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17514 "auto request sense failed (reason=%s)\n",
17515 scsi_rname(asp->sts_rqpkt_reason));
17516
17517 sd_reset_target(un, pktp);
17518
17519 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17520 NULL, NULL, EIO, (clock_t)0, NULL);
17521 return;
17522 }
17523
17524 /* Save the relevant sense info into the xp for the original cmd. */
17525 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17526 xp->xb_sense_state = asp->sts_rqpkt_state;
17527 xp->xb_sense_resid = asp->sts_rqpkt_resid;
17528 if (xp->xb_sense_state & STATE_XARQ_DONE) {
17529 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17530 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17531 MAX_SENSE_LENGTH);
17532 } else {
17533 if (xp->xb_sense_resid > SENSE_LENGTH) {
17534 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17535 } else {
17536 actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17537 }
17538 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17539 if ((((struct uscsi_cmd *)
17540 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17541 xp->xb_sense_resid = (((struct uscsi_cmd *)
17542 (xp->xb_pktinfo))->uscsi_rqlen) -
17543 actual_len;
17544 } else {
17545 xp->xb_sense_resid = 0;
17546 }
17547 }
17548 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17549 }
17550
17551 /*
17552 * See if we have valid sense data, if so then turn it over to
17553 * sd_decode_sense() to figure out the right course of action.
17554 */
17555 if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17556 SD_SENSE_DATA_IS_VALID) {
17557 sd_decode_sense(un, bp, xp, pktp);
17558 }
17559 }
17560
17561
17562 /*
17563 * Function: sd_print_sense_failed_msg
17564 *
17565 * Description: Print log message when RQS has failed.
17566 *
17567 * Arguments: un - ptr to associated softstate
17568 * bp - ptr to buf(9S) for the command
17569 * arg - generic message string ptr
17570 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17571 * or SD_NO_RETRY_ISSUED
17572 *
17573 * Context: May be called from interrupt context
17574 */
17575
17576 static void
17577 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17578 int code)
17579 {
17580 char *msgp = arg;
17581
17582 ASSERT(un != NULL);
17583 ASSERT(mutex_owned(SD_MUTEX(un)));
17584 ASSERT(bp != NULL);
17585
17586 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17587 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17588 }
17589 }
17590
17591
17592 /*
17593 * Function: sd_validate_sense_data
17594 *
17595 * Description: Check the given sense data for validity.
17596 * If the sense data is not valid, the command will
17597 * be either failed or retried!
17598 *
17599 * Return Code: SD_SENSE_DATA_IS_INVALID
17600 * SD_SENSE_DATA_IS_VALID
17601 *
17602 * Context: May be called from interrupt context
17603 */
17604
17605 static int
17606 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17607 size_t actual_len)
17608 {
17609 struct scsi_extended_sense *esp;
17610 struct scsi_pkt *pktp;
17611 char *msgp = NULL;
17612 sd_ssc_t *sscp;
17613
17614 ASSERT(un != NULL);
17615 ASSERT(mutex_owned(SD_MUTEX(un)));
17616 ASSERT(bp != NULL);
17617 ASSERT(bp != un->un_rqs_bp);
17618 ASSERT(xp != NULL);
17619 ASSERT(un->un_fm_private != NULL);
17620
17621 pktp = SD_GET_PKTP(bp);
17622 ASSERT(pktp != NULL);
17623
17624 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17625 ASSERT(sscp != NULL);
17626
17627 /*
17628 * Check the status of the RQS command (auto or manual).
17629 */
17630 switch (xp->xb_sense_status & STATUS_MASK) {
17631 case STATUS_GOOD:
17632 break;
17633
17634 case STATUS_RESERVATION_CONFLICT:
17635 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17636 return (SD_SENSE_DATA_IS_INVALID);
17637
17638 case STATUS_BUSY:
17639 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17640 "Busy Status on REQUEST SENSE\n");
17641 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17642 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17643 return (SD_SENSE_DATA_IS_INVALID);
17644
17645 case STATUS_QFULL:
17646 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17647 "QFULL Status on REQUEST SENSE\n");
17648 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17649 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17650 return (SD_SENSE_DATA_IS_INVALID);
17651
17652 case STATUS_CHECK:
17653 case STATUS_TERMINATED:
17654 msgp = "Check Condition on REQUEST SENSE\n";
17655 goto sense_failed;
17656
17657 default:
17658 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17659 goto sense_failed;
17660 }
17661
17662 /*
17663 * See if we got the minimum required amount of sense data.
17664 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17665 * or less.
17666 */
17667 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17668 (actual_len == 0)) {
17669 msgp = "Request Sense couldn't get sense data\n";
17670 goto sense_failed;
17671 }
17672
17673 if (actual_len < SUN_MIN_SENSE_LENGTH) {
17674 msgp = "Not enough sense information\n";
17675 /* Mark the ssc_flags for detecting invalid sense data */
17676 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17677 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17678 "sense-data");
17679 }
17680 goto sense_failed;
17681 }
17682
17683 /*
17684 * We require the extended sense data
17685 */
17686 esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17687 if (esp->es_class != CLASS_EXTENDED_SENSE) {
17688 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17689 static char tmp[8];
17690 static char buf[148];
17691 char *p = (char *)(xp->xb_sense_data);
17692 int i;
17693
17694 mutex_enter(&sd_sense_mutex);
17695 (void) strcpy(buf, "undecodable sense information:");
17696 for (i = 0; i < actual_len; i++) {
17697 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17698 (void) strcpy(&buf[strlen(buf)], tmp);
17699 }
17700 i = strlen(buf);
17701 (void) strcpy(&buf[i], "-(assumed fatal)\n");
17702
17703 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17704 scsi_log(SD_DEVINFO(un), sd_label,
17705 CE_WARN, buf);
17706 }
17707 mutex_exit(&sd_sense_mutex);
17708 }
17709
17710 /* Mark the ssc_flags for detecting invalid sense data */
17711 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17712 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17713 "sense-data");
17714 }
17715
17716 /* Note: Legacy behavior, fail the command with no retry */
17717 sd_return_failed_command(un, bp, EIO);
17718 return (SD_SENSE_DATA_IS_INVALID);
17719 }
17720
17721 /*
17722 * Check that es_code is valid (es_class concatenated with es_code
17723 * make up the "response code" field. es_class will always be 7, so
17724 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the
17725 * format.
17726 */
17727 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17728 (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17729 (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17730 (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17731 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17732 /* Mark the ssc_flags for detecting invalid sense data */
17733 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17734 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17735 "sense-data");
17736 }
17737 goto sense_failed;
17738 }
17739
17740 return (SD_SENSE_DATA_IS_VALID);
17741
17742 sense_failed:
17743 /*
17744 * If the request sense failed (for whatever reason), attempt
17745 * to retry the original command.
17746 */
17747 #if defined(__i386) || defined(__amd64)
17748 /*
17749 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17750 * sddef.h for Sparc platform, and x86 uses 1 binary
17751 * for both SCSI/FC.
17752 * The SD_RETRY_DELAY value need to be adjusted here
17753 * when SD_RETRY_DELAY change in sddef.h
17754 */
17755 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17756 sd_print_sense_failed_msg, msgp, EIO,
17757 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17758 #else
17759 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17760 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17761 #endif
17762
17763 return (SD_SENSE_DATA_IS_INVALID);
17764 }
17765
17766 /*
17767 * Function: sd_decode_sense
17768 *
17769 * Description: Take recovery action(s) when SCSI Sense Data is received.
17770 *
17771 * Context: Interrupt context.
17772 */
17773
17774 static void
17775 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17776 struct scsi_pkt *pktp)
17777 {
17778 uint8_t sense_key;
17779
17780 ASSERT(un != NULL);
17781 ASSERT(mutex_owned(SD_MUTEX(un)));
17782 ASSERT(bp != NULL);
17783 ASSERT(bp != un->un_rqs_bp);
17784 ASSERT(xp != NULL);
17785 ASSERT(pktp != NULL);
17786
17787 sense_key = scsi_sense_key(xp->xb_sense_data);
17788
17789 switch (sense_key) {
17790 case KEY_NO_SENSE:
17791 sd_sense_key_no_sense(un, bp, xp, pktp);
17792 break;
17793 case KEY_RECOVERABLE_ERROR:
17794 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17795 bp, xp, pktp);
17796 break;
17797 case KEY_NOT_READY:
17798 sd_sense_key_not_ready(un, xp->xb_sense_data,
17799 bp, xp, pktp);
17800 break;
17801 case KEY_MEDIUM_ERROR:
17802 case KEY_HARDWARE_ERROR:
17803 sd_sense_key_medium_or_hardware_error(un,
17804 xp->xb_sense_data, bp, xp, pktp);
17805 break;
17806 case KEY_ILLEGAL_REQUEST:
17807 sd_sense_key_illegal_request(un, bp, xp, pktp);
17808 break;
17809 case KEY_UNIT_ATTENTION:
17810 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17811 bp, xp, pktp);
17812 break;
17813 case KEY_WRITE_PROTECT:
17814 case KEY_VOLUME_OVERFLOW:
17815 case KEY_MISCOMPARE:
17816 sd_sense_key_fail_command(un, bp, xp, pktp);
17817 break;
17818 case KEY_BLANK_CHECK:
17819 sd_sense_key_blank_check(un, bp, xp, pktp);
17820 break;
17821 case KEY_ABORTED_COMMAND:
17822 sd_sense_key_aborted_command(un, bp, xp, pktp);
17823 break;
17824 case KEY_VENDOR_UNIQUE:
17825 case KEY_COPY_ABORTED:
17826 case KEY_EQUAL:
17827 case KEY_RESERVED:
17828 default:
17829 sd_sense_key_default(un, xp->xb_sense_data,
17830 bp, xp, pktp);
17831 break;
17832 }
17833 }
17834
17835
17836 /*
17837 * Function: sd_dump_memory
17838 *
17839 * Description: Debug logging routine to print the contents of a user provided
17840 * buffer. The output of the buffer is broken up into 256 byte
17841 * segments due to a size constraint of the scsi_log.
17842 * implementation.
17843 *
17844 * Arguments: un - ptr to softstate
17845 * comp - component mask
17846 * title - "title" string to preceed data when printed
17847 * data - ptr to data block to be printed
17848 * len - size of data block to be printed
17849 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17850 *
17851 * Context: May be called from interrupt context
17852 */
17853
17854 #define SD_DUMP_MEMORY_BUF_SIZE 256
17855
17856 static char *sd_dump_format_string[] = {
17857 " 0x%02x",
17858 " %c"
17859 };
17860
17861 static void
17862 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17863 int len, int fmt)
17864 {
17865 int i, j;
17866 int avail_count;
17867 int start_offset;
17868 int end_offset;
17869 size_t entry_len;
17870 char *bufp;
17871 char *local_buf;
17872 char *format_string;
17873
17874 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17875
17876 /*
17877 * In the debug version of the driver, this function is called from a
17878 * number of places which are NOPs in the release driver.
17879 * The debug driver therefore has additional methods of filtering
17880 * debug output.
17881 */
17882 #ifdef SDDEBUG
17883 /*
17884 * In the debug version of the driver we can reduce the amount of debug
17885 * messages by setting sd_error_level to something other than
17886 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17887 * sd_component_mask.
17888 */
17889 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17890 (sd_error_level != SCSI_ERR_ALL)) {
17891 return;
17892 }
17893 if (((sd_component_mask & comp) == 0) ||
17894 (sd_error_level != SCSI_ERR_ALL)) {
17895 return;
17896 }
17897 #else
17898 if (sd_error_level != SCSI_ERR_ALL) {
17899 return;
17900 }
17901 #endif
17902
17903 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17904 bufp = local_buf;
17905 /*
17906 * Available length is the length of local_buf[], minus the
17907 * length of the title string, minus one for the ":", minus
17908 * one for the newline, minus one for the NULL terminator.
17909 * This gives the #bytes available for holding the printed
17910 * values from the given data buffer.
17911 */
17912 if (fmt == SD_LOG_HEX) {
17913 format_string = sd_dump_format_string[0];
17914 } else /* SD_LOG_CHAR */ {
17915 format_string = sd_dump_format_string[1];
17916 }
17917 /*
17918 * Available count is the number of elements from the given
17919 * data buffer that we can fit into the available length.
17920 * This is based upon the size of the format string used.
17921 * Make one entry and find it's size.
17922 */
17923 (void) sprintf(bufp, format_string, data[0]);
17924 entry_len = strlen(bufp);
17925 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17926
17927 j = 0;
17928 while (j < len) {
17929 bufp = local_buf;
17930 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17931 start_offset = j;
17932
17933 end_offset = start_offset + avail_count;
17934
17935 (void) sprintf(bufp, "%s:", title);
17936 bufp += strlen(bufp);
17937 for (i = start_offset; ((i < end_offset) && (j < len));
17938 i++, j++) {
17939 (void) sprintf(bufp, format_string, data[i]);
17940 bufp += entry_len;
17941 }
17942 (void) sprintf(bufp, "\n");
17943
17944 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17945 }
17946 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17947 }
17948
17949 /*
17950 * Function: sd_print_sense_msg
17951 *
17952 * Description: Log a message based upon the given sense data.
17953 *
17954 * Arguments: un - ptr to associated softstate
17955 * bp - ptr to buf(9S) for the command
17956 * arg - ptr to associate sd_sense_info struct
17957 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17958 * or SD_NO_RETRY_ISSUED
17959 *
17960 * Context: May be called from interrupt context
17961 */
17962
17963 static void
17964 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17965 {
17966 struct sd_xbuf *xp;
17967 struct scsi_pkt *pktp;
17968 uint8_t *sensep;
17969 daddr_t request_blkno;
17970 diskaddr_t err_blkno;
17971 int severity;
17972 int pfa_flag;
17973 extern struct scsi_key_strings scsi_cmds[];
17974
17975 ASSERT(un != NULL);
17976 ASSERT(mutex_owned(SD_MUTEX(un)));
17977 ASSERT(bp != NULL);
17978 xp = SD_GET_XBUF(bp);
17979 ASSERT(xp != NULL);
17980 pktp = SD_GET_PKTP(bp);
17981 ASSERT(pktp != NULL);
17982 ASSERT(arg != NULL);
17983
17984 severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17985 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17986
17987 if ((code == SD_DELAYED_RETRY_ISSUED) ||
17988 (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17989 severity = SCSI_ERR_RETRYABLE;
17990 }
17991
17992 /* Use absolute block number for the request block number */
17993 request_blkno = xp->xb_blkno;
17994
17995 /*
17996 * Now try to get the error block number from the sense data
17997 */
17998 sensep = xp->xb_sense_data;
17999
18000 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
18001 (uint64_t *)&err_blkno)) {
18002 /*
18003 * We retrieved the error block number from the information
18004 * portion of the sense data.
18005 *
18006 * For USCSI commands we are better off using the error
18007 * block no. as the requested block no. (This is the best
18008 * we can estimate.)
18009 */
18010 if ((SD_IS_BUFIO(xp) == FALSE) &&
18011 ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
18012 request_blkno = err_blkno;
18013 }
18014 } else {
18015 /*
18016 * Without the es_valid bit set (for fixed format) or an
18017 * information descriptor (for descriptor format) we cannot
18018 * be certain of the error blkno, so just use the
18019 * request_blkno.
18020 */
18021 err_blkno = (diskaddr_t)request_blkno;
18022 }
18023
18024 /*
18025 * The following will log the buffer contents for the release driver
18026 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18027 * level is set to verbose.
18028 */
18029 sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18030 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18031 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18032 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18033
18034 if (pfa_flag == FALSE) {
18035 /* This is normally only set for USCSI */
18036 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18037 return;
18038 }
18039
18040 if ((SD_IS_BUFIO(xp) == TRUE) &&
18041 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18042 (severity < sd_error_level))) {
18043 return;
18044 }
18045 }
18046 /*
18047 * Check for Sonoma Failover and keep a count of how many failed I/O's
18048 */
18049 if ((SD_IS_LSI(un)) &&
18050 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18051 (scsi_sense_asc(sensep) == 0x94) &&
18052 (scsi_sense_ascq(sensep) == 0x01)) {
18053 un->un_sonoma_failure_count++;
18054 if (un->un_sonoma_failure_count > 1) {
18055 return;
18056 }
18057 }
18058
18059 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18060 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18061 (pktp->pkt_resid == 0))) {
18062 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18063 request_blkno, err_blkno, scsi_cmds,
18064 (struct scsi_extended_sense *)sensep,
18065 un->un_additional_codes, NULL);
18066 }
18067 }
18068
18069 /*
18070 * Function: sd_sense_key_no_sense
18071 *
18072 * Description: Recovery action when sense data was not received.
18073 *
18074 * Context: May be called from interrupt context
18075 */
18076
18077 static void
18078 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
18079 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18080 {
18081 struct sd_sense_info si;
18082
18083 ASSERT(un != NULL);
18084 ASSERT(mutex_owned(SD_MUTEX(un)));
18085 ASSERT(bp != NULL);
18086 ASSERT(xp != NULL);
18087 ASSERT(pktp != NULL);
18088
18089 si.ssi_severity = SCSI_ERR_FATAL;
18090 si.ssi_pfa_flag = FALSE;
18091
18092 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18093
18094 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18095 &si, EIO, (clock_t)0, NULL);
18096 }
18097
18098
18099 /*
18100 * Function: sd_sense_key_recoverable_error
18101 *
18102 * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18103 *
18104 * Context: May be called from interrupt context
18105 */
18106
18107 static void
18108 sd_sense_key_recoverable_error(struct sd_lun *un,
18109 uint8_t *sense_datap,
18110 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18111 {
18112 struct sd_sense_info si;
18113 uint8_t asc = scsi_sense_asc(sense_datap);
18114
18115 ASSERT(un != NULL);
18116 ASSERT(mutex_owned(SD_MUTEX(un)));
18117 ASSERT(bp != NULL);
18118 ASSERT(xp != NULL);
18119 ASSERT(pktp != NULL);
18120
18121 /*
18122 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18123 */
18124 if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18125 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18126 si.ssi_severity = SCSI_ERR_INFO;
18127 si.ssi_pfa_flag = TRUE;
18128 } else {
18129 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18130 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18131 si.ssi_severity = SCSI_ERR_RECOVERED;
18132 si.ssi_pfa_flag = FALSE;
18133 }
18134
18135 if (pktp->pkt_resid == 0) {
18136 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18137 sd_return_command(un, bp);
18138 return;
18139 }
18140
18141 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18142 &si, EIO, (clock_t)0, NULL);
18143 }
18144
18145
18146
18147
18148 /*
18149 * Function: sd_sense_key_not_ready
18150 *
18151 * Description: Recovery actions for a SCSI "Not Ready" sense key.
18152 *
18153 * Context: May be called from interrupt context
18154 */
18155
18156 static void
18157 sd_sense_key_not_ready(struct sd_lun *un,
18158 uint8_t *sense_datap,
18159 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18160 {
18161 struct sd_sense_info si;
18162 uint8_t asc = scsi_sense_asc(sense_datap);
18163 uint8_t ascq = scsi_sense_ascq(sense_datap);
18164
18165 ASSERT(un != NULL);
18166 ASSERT(mutex_owned(SD_MUTEX(un)));
18167 ASSERT(bp != NULL);
18168 ASSERT(xp != NULL);
18169 ASSERT(pktp != NULL);
18170
18171 si.ssi_severity = SCSI_ERR_FATAL;
18172 si.ssi_pfa_flag = FALSE;
18173
18174 /*
18175 * Update error stats after first NOT READY error. Disks may have
18176 * been powered down and may need to be restarted. For CDROMs,
18177 * report NOT READY errors only if media is present.
18178 */
18179 if ((ISCD(un) && (asc == 0x3A)) ||
18180 (xp->xb_nr_retry_count > 0)) {
18181 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18182 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18183 }
18184
18185 /*
18186 * Just fail if the "not ready" retry limit has been reached.
18187 */
18188 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18189 /* Special check for error message printing for removables. */
18190 if (un->un_f_has_removable_media && (asc == 0x04) &&
18191 (ascq >= 0x04)) {
18192 si.ssi_severity = SCSI_ERR_ALL;
18193 }
18194 goto fail_command;
18195 }
18196
18197 /*
18198 * Check the ASC and ASCQ in the sense data as needed, to determine
18199 * what to do.
18200 */
18201 switch (asc) {
18202 case 0x04: /* LOGICAL UNIT NOT READY */
18203 /*
18204 * disk drives that don't spin up result in a very long delay
18205 * in format without warning messages. We will log a message
18206 * if the error level is set to verbose.
18207 */
18208 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18209 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18210 "logical unit not ready, resetting disk\n");
18211 }
18212
18213 /*
18214 * There are different requirements for CDROMs and disks for
18215 * the number of retries. If a CD-ROM is giving this, it is
18216 * probably reading TOC and is in the process of getting
18217 * ready, so we should keep on trying for a long time to make
18218 * sure that all types of media are taken in account (for
18219 * some media the drive takes a long time to read TOC). For
18220 * disks we do not want to retry this too many times as this
18221 * can cause a long hang in format when the drive refuses to
18222 * spin up (a very common failure).
18223 */
18224 switch (ascq) {
18225 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */
18226 /*
18227 * Disk drives frequently refuse to spin up which
18228 * results in a very long hang in format without
18229 * warning messages.
18230 *
18231 * Note: This code preserves the legacy behavior of
18232 * comparing xb_nr_retry_count against zero for fibre
18233 * channel targets instead of comparing against the
18234 * un_reset_retry_count value. The reason for this
18235 * discrepancy has been so utterly lost beneath the
18236 * Sands of Time that even Indiana Jones could not
18237 * find it.
18238 */
18239 if (un->un_f_is_fibre == TRUE) {
18240 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18241 (xp->xb_nr_retry_count > 0)) &&
18242 (un->un_startstop_timeid == NULL)) {
18243 scsi_log(SD_DEVINFO(un), sd_label,
18244 CE_WARN, "logical unit not ready, "
18245 "resetting disk\n");
18246 sd_reset_target(un, pktp);
18247 }
18248 } else {
18249 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18250 (xp->xb_nr_retry_count >
18251 un->un_reset_retry_count)) &&
18252 (un->un_startstop_timeid == NULL)) {
18253 scsi_log(SD_DEVINFO(un), sd_label,
18254 CE_WARN, "logical unit not ready, "
18255 "resetting disk\n");
18256 sd_reset_target(un, pktp);
18257 }
18258 }
18259 break;
18260
18261 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */
18262 /*
18263 * If the target is in the process of becoming
18264 * ready, just proceed with the retry. This can
18265 * happen with CD-ROMs that take a long time to
18266 * read TOC after a power cycle or reset.
18267 */
18268 goto do_retry;
18269
18270 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18271 break;
18272
18273 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18274 /*
18275 * Retries cannot help here so just fail right away.
18276 */
18277 goto fail_command;
18278
18279 case 0x88:
18280 /*
18281 * Vendor-unique code for T3/T4: it indicates a
18282 * path problem in a mutipathed config, but as far as
18283 * the target driver is concerned it equates to a fatal
18284 * error, so we should just fail the command right away
18285 * (without printing anything to the console). If this
18286 * is not a T3/T4, fall thru to the default recovery
18287 * action.
18288 * T3/T4 is FC only, don't need to check is_fibre
18289 */
18290 if (SD_IS_T3(un) || SD_IS_T4(un)) {
18291 sd_return_failed_command(un, bp, EIO);
18292 return;
18293 }
18294 /* FALLTHRU */
18295
18296 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */
18297 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */
18298 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */
18299 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */
18300 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */
18301 default: /* Possible future codes in SCSI spec? */
18302 /*
18303 * For removable-media devices, do not retry if
18304 * ASCQ > 2 as these result mostly from USCSI commands
18305 * on MMC devices issued to check status of an
18306 * operation initiated in immediate mode. Also for
18307 * ASCQ >= 4 do not print console messages as these
18308 * mainly represent a user-initiated operation
18309 * instead of a system failure.
18310 */
18311 if (un->un_f_has_removable_media) {
18312 si.ssi_severity = SCSI_ERR_ALL;
18313 goto fail_command;
18314 }
18315 break;
18316 }
18317
18318 /*
18319 * As part of our recovery attempt for the NOT READY
18320 * condition, we issue a START STOP UNIT command. However
18321 * we want to wait for a short delay before attempting this
18322 * as there may still be more commands coming back from the
18323 * target with the check condition. To do this we use
18324 * timeout(9F) to call sd_start_stop_unit_callback() after
18325 * the delay interval expires. (sd_start_stop_unit_callback()
18326 * dispatches sd_start_stop_unit_task(), which will issue
18327 * the actual START STOP UNIT command. The delay interval
18328 * is one-half of the delay that we will use to retry the
18329 * command that generated the NOT READY condition.
18330 *
18331 * Note that we could just dispatch sd_start_stop_unit_task()
18332 * from here and allow it to sleep for the delay interval,
18333 * but then we would be tying up the taskq thread
18334 * uncesessarily for the duration of the delay.
18335 *
18336 * Do not issue the START STOP UNIT if the current command
18337 * is already a START STOP UNIT.
18338 */
18339 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18340 break;
18341 }
18342
18343 /*
18344 * Do not schedule the timeout if one is already pending.
18345 */
18346 if (un->un_startstop_timeid != NULL) {
18347 SD_INFO(SD_LOG_ERROR, un,
18348 "sd_sense_key_not_ready: restart already issued to"
18349 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18350 ddi_get_instance(SD_DEVINFO(un)));
18351 break;
18352 }
18353
18354 /*
18355 * Schedule the START STOP UNIT command, then queue the command
18356 * for a retry.
18357 *
18358 * Note: A timeout is not scheduled for this retry because we
18359 * want the retry to be serial with the START_STOP_UNIT. The
18360 * retry will be started when the START_STOP_UNIT is completed
18361 * in sd_start_stop_unit_task.
18362 */
18363 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18364 un, un->un_busy_timeout / 2);
18365 xp->xb_nr_retry_count++;
18366 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18367 return;
18368
18369 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18370 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18371 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18372 "unit does not respond to selection\n");
18373 }
18374 break;
18375
18376 case 0x3A: /* MEDIUM NOT PRESENT */
18377 if (sd_error_level >= SCSI_ERR_FATAL) {
18378 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18379 "Caddy not inserted in drive\n");
18380 }
18381
18382 sr_ejected(un);
18383 un->un_mediastate = DKIO_EJECTED;
18384 /* The state has changed, inform the media watch routines */
18385 cv_broadcast(&un->un_state_cv);
18386 /* Just fail if no media is present in the drive. */
18387 goto fail_command;
18388
18389 default:
18390 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18391 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18392 "Unit not Ready. Additional sense code 0x%x\n",
18393 asc);
18394 }
18395 break;
18396 }
18397
18398 do_retry:
18399
18400 /*
18401 * Retry the command, as some targets may report NOT READY for
18402 * several seconds after being reset.
18403 */
18404 xp->xb_nr_retry_count++;
18405 si.ssi_severity = SCSI_ERR_RETRYABLE;
18406 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18407 &si, EIO, un->un_busy_timeout, NULL);
18408
18409 return;
18410
18411 fail_command:
18412 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18413 sd_return_failed_command(un, bp, EIO);
18414 }
18415
18416
18417
18418 /*
18419 * Function: sd_sense_key_medium_or_hardware_error
18420 *
18421 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18422 * sense key.
18423 *
18424 * Context: May be called from interrupt context
18425 */
18426
18427 static void
18428 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18429 uint8_t *sense_datap,
18430 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18431 {
18432 struct sd_sense_info si;
18433 uint8_t sense_key = scsi_sense_key(sense_datap);
18434 uint8_t asc = scsi_sense_asc(sense_datap);
18435
18436 ASSERT(un != NULL);
18437 ASSERT(mutex_owned(SD_MUTEX(un)));
18438 ASSERT(bp != NULL);
18439 ASSERT(xp != NULL);
18440 ASSERT(pktp != NULL);
18441
18442 si.ssi_severity = SCSI_ERR_FATAL;
18443 si.ssi_pfa_flag = FALSE;
18444
18445 if (sense_key == KEY_MEDIUM_ERROR) {
18446 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18447 }
18448
18449 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18450
18451 if ((un->un_reset_retry_count != 0) &&
18452 (xp->xb_retry_count == un->un_reset_retry_count)) {
18453 mutex_exit(SD_MUTEX(un));
18454 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18455 if (un->un_f_allow_bus_device_reset == TRUE) {
18456
18457 boolean_t try_resetting_target = B_TRUE;
18458
18459 /*
18460 * We need to be able to handle specific ASC when we are
18461 * handling a KEY_HARDWARE_ERROR. In particular
18462 * taking the default action of resetting the target may
18463 * not be the appropriate way to attempt recovery.
18464 * Resetting a target because of a single LUN failure
18465 * victimizes all LUNs on that target.
18466 *
18467 * This is true for the LSI arrays, if an LSI
18468 * array controller returns an ASC of 0x84 (LUN Dead) we
18469 * should trust it.
18470 */
18471
18472 if (sense_key == KEY_HARDWARE_ERROR) {
18473 switch (asc) {
18474 case 0x84:
18475 if (SD_IS_LSI(un)) {
18476 try_resetting_target = B_FALSE;
18477 }
18478 break;
18479 default:
18480 break;
18481 }
18482 }
18483
18484 if (try_resetting_target == B_TRUE) {
18485 int reset_retval = 0;
18486 if (un->un_f_lun_reset_enabled == TRUE) {
18487 SD_TRACE(SD_LOG_IO_CORE, un,
18488 "sd_sense_key_medium_or_hardware_"
18489 "error: issuing RESET_LUN\n");
18490 reset_retval =
18491 scsi_reset(SD_ADDRESS(un),
18492 RESET_LUN);
18493 }
18494 if (reset_retval == 0) {
18495 SD_TRACE(SD_LOG_IO_CORE, un,
18496 "sd_sense_key_medium_or_hardware_"
18497 "error: issuing RESET_TARGET\n");
18498 (void) scsi_reset(SD_ADDRESS(un),
18499 RESET_TARGET);
18500 }
18501 }
18502 }
18503 mutex_enter(SD_MUTEX(un));
18504 }
18505
18506 /*
18507 * This really ought to be a fatal error, but we will retry anyway
18508 * as some drives report this as a spurious error.
18509 */
18510 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18511 &si, EIO, (clock_t)0, NULL);
18512 }
18513
18514
18515
18516 /*
18517 * Function: sd_sense_key_illegal_request
18518 *
18519 * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18520 *
18521 * Context: May be called from interrupt context
18522 */
18523
18524 static void
18525 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18526 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18527 {
18528 struct sd_sense_info si;
18529
18530 ASSERT(un != NULL);
18531 ASSERT(mutex_owned(SD_MUTEX(un)));
18532 ASSERT(bp != NULL);
18533 ASSERT(xp != NULL);
18534 ASSERT(pktp != NULL);
18535
18536 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18537
18538 si.ssi_severity = SCSI_ERR_INFO;
18539 si.ssi_pfa_flag = FALSE;
18540
18541 /* Pointless to retry if the target thinks it's an illegal request */
18542 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18543 sd_return_failed_command(un, bp, EIO);
18544 }
18545
18546
18547
18548
18549 /*
18550 * Function: sd_sense_key_unit_attention
18551 *
18552 * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18553 *
18554 * Context: May be called from interrupt context
18555 */
18556
18557 static void
18558 sd_sense_key_unit_attention(struct sd_lun *un,
18559 uint8_t *sense_datap,
18560 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18561 {
18562 /*
18563 * For UNIT ATTENTION we allow retries for one minute. Devices
18564 * like Sonoma can return UNIT ATTENTION close to a minute
18565 * under certain conditions.
18566 */
18567 int retry_check_flag = SD_RETRIES_UA;
18568 boolean_t kstat_updated = B_FALSE;
18569 struct sd_sense_info si;
18570 uint8_t asc = scsi_sense_asc(sense_datap);
18571 uint8_t ascq = scsi_sense_ascq(sense_datap);
18572
18573 ASSERT(un != NULL);
18574 ASSERT(mutex_owned(SD_MUTEX(un)));
18575 ASSERT(bp != NULL);
18576 ASSERT(xp != NULL);
18577 ASSERT(pktp != NULL);
18578
18579 si.ssi_severity = SCSI_ERR_INFO;
18580 si.ssi_pfa_flag = FALSE;
18581
18582
18583 switch (asc) {
18584 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18585 if (sd_report_pfa != 0) {
18586 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18587 si.ssi_pfa_flag = TRUE;
18588 retry_check_flag = SD_RETRIES_STANDARD;
18589 goto do_retry;
18590 }
18591
18592 break;
18593
18594 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18595 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18596 un->un_resvd_status |=
18597 (SD_LOST_RESERVE | SD_WANT_RESERVE);
18598 }
18599 #ifdef _LP64
18600 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18601 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18602 un, KM_NOSLEEP) == 0) {
18603 /*
18604 * If we can't dispatch the task we'll just
18605 * live without descriptor sense. We can
18606 * try again on the next "unit attention"
18607 */
18608 SD_ERROR(SD_LOG_ERROR, un,
18609 "sd_sense_key_unit_attention: "
18610 "Could not dispatch "
18611 "sd_reenable_dsense_task\n");
18612 }
18613 }
18614 #endif /* _LP64 */
18615 /* FALLTHRU */
18616
18617 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18618 if (!un->un_f_has_removable_media) {
18619 break;
18620 }
18621
18622 /*
18623 * When we get a unit attention from a removable-media device,
18624 * it may be in a state that will take a long time to recover
18625 * (e.g., from a reset). Since we are executing in interrupt
18626 * context here, we cannot wait around for the device to come
18627 * back. So hand this command off to sd_media_change_task()
18628 * for deferred processing under taskq thread context. (Note
18629 * that the command still may be failed if a problem is
18630 * encountered at a later time.)
18631 */
18632 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18633 KM_NOSLEEP) == 0) {
18634 /*
18635 * Cannot dispatch the request so fail the command.
18636 */
18637 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18638 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18639 si.ssi_severity = SCSI_ERR_FATAL;
18640 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18641 sd_return_failed_command(un, bp, EIO);
18642 }
18643
18644 /*
18645 * If failed to dispatch sd_media_change_task(), we already
18646 * updated kstat. If succeed to dispatch sd_media_change_task(),
18647 * we should update kstat later if it encounters an error. So,
18648 * we update kstat_updated flag here.
18649 */
18650 kstat_updated = B_TRUE;
18651
18652 /*
18653 * Either the command has been successfully dispatched to a
18654 * task Q for retrying, or the dispatch failed. In either case
18655 * do NOT retry again by calling sd_retry_command. This sets up
18656 * two retries of the same command and when one completes and
18657 * frees the resources the other will access freed memory,
18658 * a bad thing.
18659 */
18660 return;
18661
18662 default:
18663 break;
18664 }
18665
18666 /*
18667 * ASC ASCQ
18668 * 2A 09 Capacity data has changed
18669 * 2A 01 Mode parameters changed
18670 * 3F 0E Reported luns data has changed
18671 * Arrays that support logical unit expansion should report
18672 * capacity changes(2Ah/09). Mode parameters changed and
18673 * reported luns data has changed are the approximation.
18674 */
18675 if (((asc == 0x2a) && (ascq == 0x09)) ||
18676 ((asc == 0x2a) && (ascq == 0x01)) ||
18677 ((asc == 0x3f) && (ascq == 0x0e))) {
18678 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18679 KM_NOSLEEP) == 0) {
18680 SD_ERROR(SD_LOG_ERROR, un,
18681 "sd_sense_key_unit_attention: "
18682 "Could not dispatch sd_target_change_task\n");
18683 }
18684 }
18685
18686 /*
18687 * Update kstat if we haven't done that.
18688 */
18689 if (!kstat_updated) {
18690 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18691 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18692 }
18693
18694 do_retry:
18695 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18696 EIO, SD_UA_RETRY_DELAY, NULL);
18697 }
18698
18699
18700
18701 /*
18702 * Function: sd_sense_key_fail_command
18703 *
18704 * Description: Use to fail a command when we don't like the sense key that
18705 * was returned.
18706 *
18707 * Context: May be called from interrupt context
18708 */
18709
18710 static void
18711 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18712 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18713 {
18714 struct sd_sense_info si;
18715
18716 ASSERT(un != NULL);
18717 ASSERT(mutex_owned(SD_MUTEX(un)));
18718 ASSERT(bp != NULL);
18719 ASSERT(xp != NULL);
18720 ASSERT(pktp != NULL);
18721
18722 si.ssi_severity = SCSI_ERR_FATAL;
18723 si.ssi_pfa_flag = FALSE;
18724
18725 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18726 sd_return_failed_command(un, bp, EIO);
18727 }
18728
18729
18730
18731 /*
18732 * Function: sd_sense_key_blank_check
18733 *
18734 * Description: Recovery actions for a SCSI "Blank Check" sense key.
18735 * Has no monetary connotation.
18736 *
18737 * Context: May be called from interrupt context
18738 */
18739
18740 static void
18741 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18742 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18743 {
18744 struct sd_sense_info si;
18745
18746 ASSERT(un != NULL);
18747 ASSERT(mutex_owned(SD_MUTEX(un)));
18748 ASSERT(bp != NULL);
18749 ASSERT(xp != NULL);
18750 ASSERT(pktp != NULL);
18751
18752 /*
18753 * Blank check is not fatal for removable devices, therefore
18754 * it does not require a console message.
18755 */
18756 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18757 SCSI_ERR_FATAL;
18758 si.ssi_pfa_flag = FALSE;
18759
18760 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18761 sd_return_failed_command(un, bp, EIO);
18762 }
18763
18764
18765
18766
18767 /*
18768 * Function: sd_sense_key_aborted_command
18769 *
18770 * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18771 *
18772 * Context: May be called from interrupt context
18773 */
18774
18775 static void
18776 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18777 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18778 {
18779 struct sd_sense_info si;
18780
18781 ASSERT(un != NULL);
18782 ASSERT(mutex_owned(SD_MUTEX(un)));
18783 ASSERT(bp != NULL);
18784 ASSERT(xp != NULL);
18785 ASSERT(pktp != NULL);
18786
18787 si.ssi_severity = SCSI_ERR_FATAL;
18788 si.ssi_pfa_flag = FALSE;
18789
18790 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18791
18792 /*
18793 * This really ought to be a fatal error, but we will retry anyway
18794 * as some drives report this as a spurious error.
18795 */
18796 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18797 &si, EIO, drv_usectohz(100000), NULL);
18798 }
18799
18800
18801
18802 /*
18803 * Function: sd_sense_key_default
18804 *
18805 * Description: Default recovery action for several SCSI sense keys (basically
18806 * attempts a retry).
18807 *
18808 * Context: May be called from interrupt context
18809 */
18810
18811 static void
18812 sd_sense_key_default(struct sd_lun *un,
18813 uint8_t *sense_datap,
18814 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18815 {
18816 struct sd_sense_info si;
18817 uint8_t sense_key = scsi_sense_key(sense_datap);
18818
18819 ASSERT(un != NULL);
18820 ASSERT(mutex_owned(SD_MUTEX(un)));
18821 ASSERT(bp != NULL);
18822 ASSERT(xp != NULL);
18823 ASSERT(pktp != NULL);
18824
18825 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18826
18827 /*
18828 * Undecoded sense key. Attempt retries and hope that will fix
18829 * the problem. Otherwise, we're dead.
18830 */
18831 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18832 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18833 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18834 }
18835
18836 si.ssi_severity = SCSI_ERR_FATAL;
18837 si.ssi_pfa_flag = FALSE;
18838
18839 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18840 &si, EIO, (clock_t)0, NULL);
18841 }
18842
18843
18844
18845 /*
18846 * Function: sd_print_retry_msg
18847 *
18848 * Description: Print a message indicating the retry action being taken.
18849 *
18850 * Arguments: un - ptr to associated softstate
18851 * bp - ptr to buf(9S) for the command
18852 * arg - not used.
18853 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18854 * or SD_NO_RETRY_ISSUED
18855 *
18856 * Context: May be called from interrupt context
18857 */
18858 /* ARGSUSED */
18859 static void
18860 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18861 {
18862 struct sd_xbuf *xp;
18863 struct scsi_pkt *pktp;
18864 char *reasonp;
18865 char *msgp;
18866
18867 ASSERT(un != NULL);
18868 ASSERT(mutex_owned(SD_MUTEX(un)));
18869 ASSERT(bp != NULL);
18870 pktp = SD_GET_PKTP(bp);
18871 ASSERT(pktp != NULL);
18872 xp = SD_GET_XBUF(bp);
18873 ASSERT(xp != NULL);
18874
18875 ASSERT(!mutex_owned(&un->un_pm_mutex));
18876 mutex_enter(&un->un_pm_mutex);
18877 if ((un->un_state == SD_STATE_SUSPENDED) ||
18878 (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18879 (pktp->pkt_flags & FLAG_SILENT)) {
18880 mutex_exit(&un->un_pm_mutex);
18881 goto update_pkt_reason;
18882 }
18883 mutex_exit(&un->un_pm_mutex);
18884
18885 /*
18886 * Suppress messages if they are all the same pkt_reason; with
18887 * TQ, many (up to 256) are returned with the same pkt_reason.
18888 * If we are in panic, then suppress the retry messages.
18889 */
18890 switch (flag) {
18891 case SD_NO_RETRY_ISSUED:
18892 msgp = "giving up";
18893 break;
18894 case SD_IMMEDIATE_RETRY_ISSUED:
18895 case SD_DELAYED_RETRY_ISSUED:
18896 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18897 ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18898 (sd_error_level != SCSI_ERR_ALL))) {
18899 return;
18900 }
18901 msgp = "retrying command";
18902 break;
18903 default:
18904 goto update_pkt_reason;
18905 }
18906
18907 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18908 scsi_rname(pktp->pkt_reason));
18909
18910 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18911 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18912 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18913 }
18914
18915 update_pkt_reason:
18916 /*
18917 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18918 * This is to prevent multiple console messages for the same failure
18919 * condition. Note that un->un_last_pkt_reason is NOT restored if &
18920 * when the command is retried successfully because there still may be
18921 * more commands coming back with the same value of pktp->pkt_reason.
18922 */
18923 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18924 un->un_last_pkt_reason = pktp->pkt_reason;
18925 }
18926 }
18927
18928
18929 /*
18930 * Function: sd_print_cmd_incomplete_msg
18931 *
18932 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18933 *
18934 * Arguments: un - ptr to associated softstate
18935 * bp - ptr to buf(9S) for the command
18936 * arg - passed to sd_print_retry_msg()
18937 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18938 * or SD_NO_RETRY_ISSUED
18939 *
18940 * Context: May be called from interrupt context
18941 */
18942
18943 static void
18944 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18945 int code)
18946 {
18947 dev_info_t *dip;
18948
18949 ASSERT(un != NULL);
18950 ASSERT(mutex_owned(SD_MUTEX(un)));
18951 ASSERT(bp != NULL);
18952
18953 switch (code) {
18954 case SD_NO_RETRY_ISSUED:
18955 /* Command was failed. Someone turned off this target? */
18956 if (un->un_state != SD_STATE_OFFLINE) {
18957 /*
18958 * Suppress message if we are detaching and
18959 * device has been disconnected
18960 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18961 * private interface and not part of the DDI
18962 */
18963 dip = un->un_sd->sd_dev;
18964 if (!(DEVI_IS_DETACHING(dip) &&
18965 DEVI_IS_DEVICE_REMOVED(dip))) {
18966 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18967 "disk not responding to selection\n");
18968 }
18969 New_state(un, SD_STATE_OFFLINE);
18970 }
18971 break;
18972
18973 case SD_DELAYED_RETRY_ISSUED:
18974 case SD_IMMEDIATE_RETRY_ISSUED:
18975 default:
18976 /* Command was successfully queued for retry */
18977 sd_print_retry_msg(un, bp, arg, code);
18978 break;
18979 }
18980 }
18981
18982
18983 /*
18984 * Function: sd_pkt_reason_cmd_incomplete
18985 *
18986 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18987 *
18988 * Context: May be called from interrupt context
18989 */
18990
18991 static void
18992 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18993 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18994 {
18995 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18996
18997 ASSERT(un != NULL);
18998 ASSERT(mutex_owned(SD_MUTEX(un)));
18999 ASSERT(bp != NULL);
19000 ASSERT(xp != NULL);
19001 ASSERT(pktp != NULL);
19002
19003 /* Do not do a reset if selection did not complete */
19004 /* Note: Should this not just check the bit? */
19005 if (pktp->pkt_state != STATE_GOT_BUS) {
19006 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19007 sd_reset_target(un, pktp);
19008 }
19009
19010 /*
19011 * If the target was not successfully selected, then set
19012 * SD_RETRIES_FAILFAST to indicate that we lost communication
19013 * with the target, and further retries and/or commands are
19014 * likely to take a long time.
19015 */
19016 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
19017 flag |= SD_RETRIES_FAILFAST;
19018 }
19019
19020 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19021
19022 sd_retry_command(un, bp, flag,
19023 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19024 }
19025
19026
19027
19028 /*
19029 * Function: sd_pkt_reason_cmd_tran_err
19030 *
19031 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19032 *
19033 * Context: May be called from interrupt context
19034 */
19035
19036 static void
19037 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19038 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19039 {
19040 ASSERT(un != NULL);
19041 ASSERT(mutex_owned(SD_MUTEX(un)));
19042 ASSERT(bp != NULL);
19043 ASSERT(xp != NULL);
19044 ASSERT(pktp != NULL);
19045
19046 /*
19047 * Do not reset if we got a parity error, or if
19048 * selection did not complete.
19049 */
19050 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19051 /* Note: Should this not just check the bit for pkt_state? */
19052 if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19053 (pktp->pkt_state != STATE_GOT_BUS)) {
19054 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19055 sd_reset_target(un, pktp);
19056 }
19057
19058 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19059
19060 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19061 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19062 }
19063
19064
19065
19066 /*
19067 * Function: sd_pkt_reason_cmd_reset
19068 *
19069 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19070 *
19071 * Context: May be called from interrupt context
19072 */
19073
19074 static void
19075 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
19076 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19077 {
19078 ASSERT(un != NULL);
19079 ASSERT(mutex_owned(SD_MUTEX(un)));
19080 ASSERT(bp != NULL);
19081 ASSERT(xp != NULL);
19082 ASSERT(pktp != NULL);
19083
19084 /* The target may still be running the command, so try to reset. */
19085 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19086 sd_reset_target(un, pktp);
19087
19088 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19089
19090 /*
19091 * If pkt_reason is CMD_RESET chances are that this pkt got
19092 * reset because another target on this bus caused it. The target
19093 * that caused it should get CMD_TIMEOUT with pkt_statistics
19094 * of STAT_TIMEOUT/STAT_DEV_RESET.
19095 */
19096
19097 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19098 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19099 }
19100
19101
19102
19103
19104 /*
19105 * Function: sd_pkt_reason_cmd_aborted
19106 *
19107 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19108 *
19109 * Context: May be called from interrupt context
19110 */
19111
19112 static void
19113 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
19114 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19115 {
19116 ASSERT(un != NULL);
19117 ASSERT(mutex_owned(SD_MUTEX(un)));
19118 ASSERT(bp != NULL);
19119 ASSERT(xp != NULL);
19120 ASSERT(pktp != NULL);
19121
19122 /* The target may still be running the command, so try to reset. */
19123 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19124 sd_reset_target(un, pktp);
19125
19126 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19127
19128 /*
19129 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19130 * aborted because another target on this bus caused it. The target
19131 * that caused it should get CMD_TIMEOUT with pkt_statistics
19132 * of STAT_TIMEOUT/STAT_DEV_RESET.
19133 */
19134
19135 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19136 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19137 }
19138
19139
19140
19141 /*
19142 * Function: sd_pkt_reason_cmd_timeout
19143 *
19144 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19145 *
19146 * Context: May be called from interrupt context
19147 */
19148
19149 static void
19150 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19151 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19152 {
19153 ASSERT(un != NULL);
19154 ASSERT(mutex_owned(SD_MUTEX(un)));
19155 ASSERT(bp != NULL);
19156 ASSERT(xp != NULL);
19157 ASSERT(pktp != NULL);
19158
19159
19160 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19161 sd_reset_target(un, pktp);
19162
19163 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19164
19165 /*
19166 * A command timeout indicates that we could not establish
19167 * communication with the target, so set SD_RETRIES_FAILFAST
19168 * as further retries/commands are likely to take a long time.
19169 */
19170 sd_retry_command(un, bp,
19171 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19172 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19173 }
19174
19175
19176
19177 /*
19178 * Function: sd_pkt_reason_cmd_unx_bus_free
19179 *
19180 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19181 *
19182 * Context: May be called from interrupt context
19183 */
19184
19185 static void
19186 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19187 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19188 {
19189 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19190
19191 ASSERT(un != NULL);
19192 ASSERT(mutex_owned(SD_MUTEX(un)));
19193 ASSERT(bp != NULL);
19194 ASSERT(xp != NULL);
19195 ASSERT(pktp != NULL);
19196
19197 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19198 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19199
19200 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19201 sd_print_retry_msg : NULL;
19202
19203 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19204 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19205 }
19206
19207
19208 /*
19209 * Function: sd_pkt_reason_cmd_tag_reject
19210 *
19211 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19212 *
19213 * Context: May be called from interrupt context
19214 */
19215
19216 static void
19217 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19218 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19219 {
19220 ASSERT(un != NULL);
19221 ASSERT(mutex_owned(SD_MUTEX(un)));
19222 ASSERT(bp != NULL);
19223 ASSERT(xp != NULL);
19224 ASSERT(pktp != NULL);
19225
19226 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19227 pktp->pkt_flags = 0;
19228 un->un_tagflags = 0;
19229 if (un->un_f_opt_queueing == TRUE) {
19230 un->un_throttle = min(un->un_throttle, 3);
19231 } else {
19232 un->un_throttle = 1;
19233 }
19234 mutex_exit(SD_MUTEX(un));
19235 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19236 mutex_enter(SD_MUTEX(un));
19237
19238 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19239
19240 /* Legacy behavior not to check retry counts here. */
19241 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19242 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19243 }
19244
19245
19246 /*
19247 * Function: sd_pkt_reason_default
19248 *
19249 * Description: Default recovery actions for SCSA pkt_reason values that
19250 * do not have more explicit recovery actions.
19251 *
19252 * Context: May be called from interrupt context
19253 */
19254
19255 static void
19256 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19257 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19258 {
19259 ASSERT(un != NULL);
19260 ASSERT(mutex_owned(SD_MUTEX(un)));
19261 ASSERT(bp != NULL);
19262 ASSERT(xp != NULL);
19263 ASSERT(pktp != NULL);
19264
19265 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19266 sd_reset_target(un, pktp);
19267
19268 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19269
19270 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19271 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19272 }
19273
19274
19275
19276 /*
19277 * Function: sd_pkt_status_check_condition
19278 *
19279 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19280 *
19281 * Context: May be called from interrupt context
19282 */
19283
19284 static void
19285 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19286 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19287 {
19288 ASSERT(un != NULL);
19289 ASSERT(mutex_owned(SD_MUTEX(un)));
19290 ASSERT(bp != NULL);
19291 ASSERT(xp != NULL);
19292 ASSERT(pktp != NULL);
19293
19294 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19295 "entry: buf:0x%p xp:0x%p\n", bp, xp);
19296
19297 /*
19298 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19299 * command will be retried after the request sense). Otherwise, retry
19300 * the command. Note: we are issuing the request sense even though the
19301 * retry limit may have been reached for the failed command.
19302 */
19303 if (un->un_f_arq_enabled == FALSE) {
19304 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19305 "no ARQ, sending request sense command\n");
19306 sd_send_request_sense_command(un, bp, pktp);
19307 } else {
19308 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19309 "ARQ,retrying request sense command\n");
19310 #if defined(__i386) || defined(__amd64)
19311 /*
19312 * The SD_RETRY_DELAY value need to be adjusted here
19313 * when SD_RETRY_DELAY change in sddef.h
19314 */
19315 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19316 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19317 NULL);
19318 #else
19319 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19320 EIO, SD_RETRY_DELAY, NULL);
19321 #endif
19322 }
19323
19324 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19325 }
19326
19327
19328 /*
19329 * Function: sd_pkt_status_busy
19330 *
19331 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19332 *
19333 * Context: May be called from interrupt context
19334 */
19335
19336 static void
19337 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19338 struct scsi_pkt *pktp)
19339 {
19340 ASSERT(un != NULL);
19341 ASSERT(mutex_owned(SD_MUTEX(un)));
19342 ASSERT(bp != NULL);
19343 ASSERT(xp != NULL);
19344 ASSERT(pktp != NULL);
19345
19346 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19347 "sd_pkt_status_busy: entry\n");
19348
19349 /* If retries are exhausted, just fail the command. */
19350 if (xp->xb_retry_count >= un->un_busy_retry_count) {
19351 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19352 "device busy too long\n");
19353 sd_return_failed_command(un, bp, EIO);
19354 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19355 "sd_pkt_status_busy: exit\n");
19356 return;
19357 }
19358 xp->xb_retry_count++;
19359
19360 /*
19361 * Try to reset the target. However, we do not want to perform
19362 * more than one reset if the device continues to fail. The reset
19363 * will be performed when the retry count reaches the reset
19364 * threshold. This threshold should be set such that at least
19365 * one retry is issued before the reset is performed.
19366 */
19367 if (xp->xb_retry_count ==
19368 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19369 int rval = 0;
19370 mutex_exit(SD_MUTEX(un));
19371 if (un->un_f_allow_bus_device_reset == TRUE) {
19372 /*
19373 * First try to reset the LUN; if we cannot then
19374 * try to reset the target.
19375 */
19376 if (un->un_f_lun_reset_enabled == TRUE) {
19377 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19378 "sd_pkt_status_busy: RESET_LUN\n");
19379 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19380 }
19381 if (rval == 0) {
19382 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19383 "sd_pkt_status_busy: RESET_TARGET\n");
19384 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19385 }
19386 }
19387 if (rval == 0) {
19388 /*
19389 * If the RESET_LUN and/or RESET_TARGET failed,
19390 * try RESET_ALL
19391 */
19392 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19393 "sd_pkt_status_busy: RESET_ALL\n");
19394 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19395 }
19396 mutex_enter(SD_MUTEX(un));
19397 if (rval == 0) {
19398 /*
19399 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19400 * At this point we give up & fail the command.
19401 */
19402 sd_return_failed_command(un, bp, EIO);
19403 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19404 "sd_pkt_status_busy: exit (failed cmd)\n");
19405 return;
19406 }
19407 }
19408
19409 /*
19410 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19411 * we have already checked the retry counts above.
19412 */
19413 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19414 EIO, un->un_busy_timeout, NULL);
19415
19416 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19417 "sd_pkt_status_busy: exit\n");
19418 }
19419
19420
19421 /*
19422 * Function: sd_pkt_status_reservation_conflict
19423 *
19424 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19425 * command status.
19426 *
19427 * Context: May be called from interrupt context
19428 */
19429
19430 static void
19431 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19432 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19433 {
19434 ASSERT(un != NULL);
19435 ASSERT(mutex_owned(SD_MUTEX(un)));
19436 ASSERT(bp != NULL);
19437 ASSERT(xp != NULL);
19438 ASSERT(pktp != NULL);
19439
19440 /*
19441 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19442 * conflict could be due to various reasons like incorrect keys, not
19443 * registered or not reserved etc. So, we return EACCES to the caller.
19444 */
19445 if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19446 int cmd = SD_GET_PKT_OPCODE(pktp);
19447 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19448 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19449 sd_return_failed_command(un, bp, EACCES);
19450 return;
19451 }
19452 }
19453
19454 un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19455
19456 if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19457 if (sd_failfast_enable != 0) {
19458 /* By definition, we must panic here.... */
19459 sd_panic_for_res_conflict(un);
19460 /*NOTREACHED*/
19461 }
19462 SD_ERROR(SD_LOG_IO, un,
19463 "sd_handle_resv_conflict: Disk Reserved\n");
19464 sd_return_failed_command(un, bp, EACCES);
19465 return;
19466 }
19467
19468 /*
19469 * 1147670: retry only if sd_retry_on_reservation_conflict
19470 * property is set (default is 1). Retries will not succeed
19471 * on a disk reserved by another initiator. HA systems
19472 * may reset this via sd.conf to avoid these retries.
19473 *
19474 * Note: The legacy return code for this failure is EIO, however EACCES
19475 * seems more appropriate for a reservation conflict.
19476 */
19477 if (sd_retry_on_reservation_conflict == 0) {
19478 SD_ERROR(SD_LOG_IO, un,
19479 "sd_handle_resv_conflict: Device Reserved\n");
19480 sd_return_failed_command(un, bp, EIO);
19481 return;
19482 }
19483
19484 /*
19485 * Retry the command if we can.
19486 *
19487 * Note: The legacy return code for this failure is EIO, however EACCES
19488 * seems more appropriate for a reservation conflict.
19489 */
19490 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19491 (clock_t)2, NULL);
19492 }
19493
19494
19495
19496 /*
19497 * Function: sd_pkt_status_qfull
19498 *
19499 * Description: Handle a QUEUE FULL condition from the target. This can
19500 * occur if the HBA does not handle the queue full condition.
19501 * (Basically this means third-party HBAs as Sun HBAs will
19502 * handle the queue full condition.) Note that if there are
19503 * some commands already in the transport, then the queue full
19504 * has occurred because the queue for this nexus is actually
19505 * full. If there are no commands in the transport, then the
19506 * queue full is resulting from some other initiator or lun
19507 * consuming all the resources at the target.
19508 *
19509 * Context: May be called from interrupt context
19510 */
19511
19512 static void
19513 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19514 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19515 {
19516 ASSERT(un != NULL);
19517 ASSERT(mutex_owned(SD_MUTEX(un)));
19518 ASSERT(bp != NULL);
19519 ASSERT(xp != NULL);
19520 ASSERT(pktp != NULL);
19521
19522 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19523 "sd_pkt_status_qfull: entry\n");
19524
19525 /*
19526 * Just lower the QFULL throttle and retry the command. Note that
19527 * we do not limit the number of retries here.
19528 */
19529 sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19530 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19531 SD_RESTART_TIMEOUT, NULL);
19532
19533 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19534 "sd_pkt_status_qfull: exit\n");
19535 }
19536
19537
19538 /*
19539 * Function: sd_reset_target
19540 *
19541 * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19542 * RESET_TARGET, or RESET_ALL.
19543 *
19544 * Context: May be called under interrupt context.
19545 */
19546
19547 static void
19548 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19549 {
19550 int rval = 0;
19551
19552 ASSERT(un != NULL);
19553 ASSERT(mutex_owned(SD_MUTEX(un)));
19554 ASSERT(pktp != NULL);
19555
19556 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19557
19558 /*
19559 * No need to reset if the transport layer has already done so.
19560 */
19561 if ((pktp->pkt_statistics &
19562 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19563 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19564 "sd_reset_target: no reset\n");
19565 return;
19566 }
19567
19568 mutex_exit(SD_MUTEX(un));
19569
19570 if (un->un_f_allow_bus_device_reset == TRUE) {
19571 if (un->un_f_lun_reset_enabled == TRUE) {
19572 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19573 "sd_reset_target: RESET_LUN\n");
19574 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19575 }
19576 if (rval == 0) {
19577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19578 "sd_reset_target: RESET_TARGET\n");
19579 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19580 }
19581 }
19582
19583 if (rval == 0) {
19584 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19585 "sd_reset_target: RESET_ALL\n");
19586 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19587 }
19588
19589 mutex_enter(SD_MUTEX(un));
19590
19591 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19592 }
19593
19594 /*
19595 * Function: sd_target_change_task
19596 *
19597 * Description: Handle dynamic target change
19598 *
19599 * Context: Executes in a taskq() thread context
19600 */
19601 static void
19602 sd_target_change_task(void *arg)
19603 {
19604 struct sd_lun *un = arg;
19605 uint64_t capacity;
19606 diskaddr_t label_cap;
19607 uint_t lbasize;
19608 sd_ssc_t *ssc;
19609
19610 ASSERT(un != NULL);
19611 ASSERT(!mutex_owned(SD_MUTEX(un)));
19612
19613 if ((un->un_f_blockcount_is_valid == FALSE) ||
19614 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19615 return;
19616 }
19617
19618 ssc = sd_ssc_init(un);
19619
19620 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19621 &lbasize, SD_PATH_DIRECT) != 0) {
19622 SD_ERROR(SD_LOG_ERROR, un,
19623 "sd_target_change_task: fail to read capacity\n");
19624 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19625 goto task_exit;
19626 }
19627
19628 mutex_enter(SD_MUTEX(un));
19629 if (capacity <= un->un_blockcount) {
19630 mutex_exit(SD_MUTEX(un));
19631 goto task_exit;
19632 }
19633
19634 sd_update_block_info(un, lbasize, capacity);
19635 mutex_exit(SD_MUTEX(un));
19636
19637 /*
19638 * If lun is EFI labeled and lun capacity is greater than the
19639 * capacity contained in the label, log a sys event.
19640 */
19641 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19642 (void*)SD_PATH_DIRECT) == 0) {
19643 mutex_enter(SD_MUTEX(un));
19644 if (un->un_f_blockcount_is_valid &&
19645 un->un_blockcount > label_cap) {
19646 mutex_exit(SD_MUTEX(un));
19647 sd_log_lun_expansion_event(un, KM_SLEEP);
19648 } else {
19649 mutex_exit(SD_MUTEX(un));
19650 }
19651 }
19652
19653 task_exit:
19654 sd_ssc_fini(ssc);
19655 }
19656
19657
19658 /*
19659 * Function: sd_log_dev_status_event
19660 *
19661 * Description: Log EC_dev_status sysevent
19662 *
19663 * Context: Never called from interrupt context
19664 */
19665 static void
19666 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19667 {
19668 int err;
19669 char *path;
19670 nvlist_t *attr_list;
19671
19672 /* Allocate and build sysevent attribute list */
19673 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19674 if (err != 0) {
19675 SD_ERROR(SD_LOG_ERROR, un,
19676 "sd_log_dev_status_event: fail to allocate space\n");
19677 return;
19678 }
19679
19680 path = kmem_alloc(MAXPATHLEN, km_flag);
19681 if (path == NULL) {
19682 nvlist_free(attr_list);
19683 SD_ERROR(SD_LOG_ERROR, un,
19684 "sd_log_dev_status_event: fail to allocate space\n");
19685 return;
19686 }
19687 /*
19688 * Add path attribute to identify the lun.
19689 * We are using minor node 'a' as the sysevent attribute.
19690 */
19691 (void) snprintf(path, MAXPATHLEN, "/devices");
19692 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19693 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19694 ":a");
19695
19696 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19697 if (err != 0) {
19698 nvlist_free(attr_list);
19699 kmem_free(path, MAXPATHLEN);
19700 SD_ERROR(SD_LOG_ERROR, un,
19701 "sd_log_dev_status_event: fail to add attribute\n");
19702 return;
19703 }
19704
19705 /* Log dynamic lun expansion sysevent */
19706 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19707 esc, attr_list, NULL, km_flag);
19708 if (err != DDI_SUCCESS) {
19709 SD_ERROR(SD_LOG_ERROR, un,
19710 "sd_log_dev_status_event: fail to log sysevent\n");
19711 }
19712
19713 nvlist_free(attr_list);
19714 kmem_free(path, MAXPATHLEN);
19715 }
19716
19717
19718 /*
19719 * Function: sd_log_lun_expansion_event
19720 *
19721 * Description: Log lun expansion sys event
19722 *
19723 * Context: Never called from interrupt context
19724 */
19725 static void
19726 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19727 {
19728 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19729 }
19730
19731
19732 /*
19733 * Function: sd_log_eject_request_event
19734 *
19735 * Description: Log eject request sysevent
19736 *
19737 * Context: Never called from interrupt context
19738 */
19739 static void
19740 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19741 {
19742 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19743 }
19744
19745
19746 /*
19747 * Function: sd_media_change_task
19748 *
19749 * Description: Recovery action for CDROM to become available.
19750 *
19751 * Context: Executes in a taskq() thread context
19752 */
19753
19754 static void
19755 sd_media_change_task(void *arg)
19756 {
19757 struct scsi_pkt *pktp = arg;
19758 struct sd_lun *un;
19759 struct buf *bp;
19760 struct sd_xbuf *xp;
19761 int err = 0;
19762 int retry_count = 0;
19763 int retry_limit = SD_UNIT_ATTENTION_RETRY/10;
19764 struct sd_sense_info si;
19765
19766 ASSERT(pktp != NULL);
19767 bp = (struct buf *)pktp->pkt_private;
19768 ASSERT(bp != NULL);
19769 xp = SD_GET_XBUF(bp);
19770 ASSERT(xp != NULL);
19771 un = SD_GET_UN(bp);
19772 ASSERT(un != NULL);
19773 ASSERT(!mutex_owned(SD_MUTEX(un)));
19774 ASSERT(un->un_f_monitor_media_state);
19775
19776 si.ssi_severity = SCSI_ERR_INFO;
19777 si.ssi_pfa_flag = FALSE;
19778
19779 /*
19780 * When a reset is issued on a CDROM, it takes a long time to
19781 * recover. First few attempts to read capacity and other things
19782 * related to handling unit attention fail (with a ASC 0x4 and
19783 * ASCQ 0x1). In that case we want to do enough retries and we want
19784 * to limit the retries in other cases of genuine failures like
19785 * no media in drive.
19786 */
19787 while (retry_count++ < retry_limit) {
19788 if ((err = sd_handle_mchange(un)) == 0) {
19789 break;
19790 }
19791 if (err == EAGAIN) {
19792 retry_limit = SD_UNIT_ATTENTION_RETRY;
19793 }
19794 /* Sleep for 0.5 sec. & try again */
19795 delay(drv_usectohz(500000));
19796 }
19797
19798 /*
19799 * Dispatch (retry or fail) the original command here,
19800 * along with appropriate console messages....
19801 *
19802 * Must grab the mutex before calling sd_retry_command,
19803 * sd_print_sense_msg and sd_return_failed_command.
19804 */
19805 mutex_enter(SD_MUTEX(un));
19806 if (err != SD_CMD_SUCCESS) {
19807 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19808 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19809 si.ssi_severity = SCSI_ERR_FATAL;
19810 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19811 sd_return_failed_command(un, bp, EIO);
19812 } else {
19813 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19814 &si, EIO, (clock_t)0, NULL);
19815 }
19816 mutex_exit(SD_MUTEX(un));
19817 }
19818
19819
19820
19821 /*
19822 * Function: sd_handle_mchange
19823 *
19824 * Description: Perform geometry validation & other recovery when CDROM
19825 * has been removed from drive.
19826 *
19827 * Return Code: 0 for success
19828 * errno-type return code of either sd_send_scsi_DOORLOCK() or
19829 * sd_send_scsi_READ_CAPACITY()
19830 *
19831 * Context: Executes in a taskq() thread context
19832 */
19833
19834 static int
19835 sd_handle_mchange(struct sd_lun *un)
19836 {
19837 uint64_t capacity;
19838 uint32_t lbasize;
19839 int rval;
19840 sd_ssc_t *ssc;
19841
19842 ASSERT(!mutex_owned(SD_MUTEX(un)));
19843 ASSERT(un->un_f_monitor_media_state);
19844
19845 ssc = sd_ssc_init(un);
19846 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19847 SD_PATH_DIRECT_PRIORITY);
19848
19849 if (rval != 0)
19850 goto failed;
19851
19852 mutex_enter(SD_MUTEX(un));
19853 sd_update_block_info(un, lbasize, capacity);
19854
19855 if (un->un_errstats != NULL) {
19856 struct sd_errstats *stp =
19857 (struct sd_errstats *)un->un_errstats->ks_data;
19858 stp->sd_capacity.value.ui64 = (uint64_t)
19859 ((uint64_t)un->un_blockcount *
19860 (uint64_t)un->un_tgt_blocksize);
19861 }
19862
19863 /*
19864 * Check if the media in the device is writable or not
19865 */
19866 if (ISCD(un)) {
19867 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19868 }
19869
19870 /*
19871 * Note: Maybe let the strategy/partitioning chain worry about getting
19872 * valid geometry.
19873 */
19874 mutex_exit(SD_MUTEX(un));
19875 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19876
19877
19878 if (cmlb_validate(un->un_cmlbhandle, 0,
19879 (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19880 sd_ssc_fini(ssc);
19881 return (EIO);
19882 } else {
19883 if (un->un_f_pkstats_enabled) {
19884 sd_set_pstats(un);
19885 SD_TRACE(SD_LOG_IO_PARTITION, un,
19886 "sd_handle_mchange: un:0x%p pstats created and "
19887 "set\n", un);
19888 }
19889 }
19890
19891 /*
19892 * Try to lock the door
19893 */
19894 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19895 SD_PATH_DIRECT_PRIORITY);
19896 failed:
19897 if (rval != 0)
19898 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19899 sd_ssc_fini(ssc);
19900 return (rval);
19901 }
19902
19903
19904 /*
19905 * Function: sd_send_scsi_DOORLOCK
19906 *
19907 * Description: Issue the scsi DOOR LOCK command
19908 *
19909 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
19910 * structure for this target.
19911 * flag - SD_REMOVAL_ALLOW
19912 * SD_REMOVAL_PREVENT
19913 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19914 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19915 * to use the USCSI "direct" chain and bypass the normal
19916 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19917 * command is issued as part of an error recovery action.
19918 *
19919 * Return Code: 0 - Success
19920 * errno return code from sd_ssc_send()
19921 *
19922 * Context: Can sleep.
19923 */
19924
19925 static int
19926 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19927 {
19928 struct scsi_extended_sense sense_buf;
19929 union scsi_cdb cdb;
19930 struct uscsi_cmd ucmd_buf;
19931 int status;
19932 struct sd_lun *un;
19933
19934 ASSERT(ssc != NULL);
19935 un = ssc->ssc_un;
19936 ASSERT(un != NULL);
19937 ASSERT(!mutex_owned(SD_MUTEX(un)));
19938
19939 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19940
19941 /* already determined doorlock is not supported, fake success */
19942 if (un->un_f_doorlock_supported == FALSE) {
19943 return (0);
19944 }
19945
19946 /*
19947 * If we are ejecting and see an SD_REMOVAL_PREVENT
19948 * ignore the command so we can complete the eject
19949 * operation.
19950 */
19951 if (flag == SD_REMOVAL_PREVENT) {
19952 mutex_enter(SD_MUTEX(un));
19953 if (un->un_f_ejecting == TRUE) {
19954 mutex_exit(SD_MUTEX(un));
19955 return (EAGAIN);
19956 }
19957 mutex_exit(SD_MUTEX(un));
19958 }
19959
19960 bzero(&cdb, sizeof (cdb));
19961 bzero(&ucmd_buf, sizeof (ucmd_buf));
19962
19963 cdb.scc_cmd = SCMD_DOORLOCK;
19964 cdb.cdb_opaque[4] = (uchar_t)flag;
19965
19966 ucmd_buf.uscsi_cdb = (char *)&cdb;
19967 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
19968 ucmd_buf.uscsi_bufaddr = NULL;
19969 ucmd_buf.uscsi_buflen = 0;
19970 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19971 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19972 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
19973 ucmd_buf.uscsi_timeout = 15;
19974
19975 SD_TRACE(SD_LOG_IO, un,
19976 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19977
19978 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19979 UIO_SYSSPACE, path_flag);
19980
19981 if (status == 0)
19982 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19983
19984 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19985 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19986 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19987 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19988
19989 /* fake success and skip subsequent doorlock commands */
19990 un->un_f_doorlock_supported = FALSE;
19991 return (0);
19992 }
19993
19994 return (status);
19995 }
19996
19997 /*
19998 * Function: sd_send_scsi_READ_CAPACITY
19999 *
20000 * Description: This routine uses the scsi READ CAPACITY command to determine
20001 * the device capacity in number of blocks and the device native
20002 * block size. If this function returns a failure, then the
20003 * values in *capp and *lbap are undefined. If the capacity
20004 * returned is 0xffffffff then the lun is too large for a
20005 * normal READ CAPACITY command and the results of a
20006 * READ CAPACITY 16 will be used instead.
20007 *
20008 * Arguments: ssc - ssc contains ptr to soft state struct for the target
20009 * capp - ptr to unsigned 64-bit variable to receive the
20010 * capacity value from the command.
20011 * lbap - ptr to unsigned 32-bit varaible to receive the
20012 * block size value from the command
20013 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20014 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20015 * to use the USCSI "direct" chain and bypass the normal
20016 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20017 * command is issued as part of an error recovery action.
20018 *
20019 * Return Code: 0 - Success
20020 * EIO - IO error
20021 * EACCES - Reservation conflict detected
20022 * EAGAIN - Device is becoming ready
20023 * errno return code from sd_ssc_send()
20024 *
20025 * Context: Can sleep. Blocks until command completes.
20026 */
20027
20028 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity)
20029
20030 static int
20031 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20032 int path_flag)
20033 {
20034 struct scsi_extended_sense sense_buf;
20035 struct uscsi_cmd ucmd_buf;
20036 union scsi_cdb cdb;
20037 uint32_t *capacity_buf;
20038 uint64_t capacity;
20039 uint32_t lbasize;
20040 uint32_t pbsize;
20041 int status;
20042 struct sd_lun *un;
20043
20044 ASSERT(ssc != NULL);
20045
20046 un = ssc->ssc_un;
20047 ASSERT(un != NULL);
20048 ASSERT(!mutex_owned(SD_MUTEX(un)));
20049 ASSERT(capp != NULL);
20050 ASSERT(lbap != NULL);
20051
20052 SD_TRACE(SD_LOG_IO, un,
20053 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20054
20055 /*
20056 * First send a READ_CAPACITY command to the target.
20057 * (This command is mandatory under SCSI-2.)
20058 *
20059 * Set up the CDB for the READ_CAPACITY command. The Partial
20060 * Medium Indicator bit is cleared. The address field must be
20061 * zero if the PMI bit is zero.
20062 */
20063 bzero(&cdb, sizeof (cdb));
20064 bzero(&ucmd_buf, sizeof (ucmd_buf));
20065
20066 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20067
20068 cdb.scc_cmd = SCMD_READ_CAPACITY;
20069
20070 ucmd_buf.uscsi_cdb = (char *)&cdb;
20071 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20072 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
20073 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
20074 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20075 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20076 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20077 ucmd_buf.uscsi_timeout = 60;
20078
20079 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20080 UIO_SYSSPACE, path_flag);
20081
20082 switch (status) {
20083 case 0:
20084 /* Return failure if we did not get valid capacity data. */
20085 if (ucmd_buf.uscsi_resid != 0) {
20086 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20087 "sd_send_scsi_READ_CAPACITY received invalid "
20088 "capacity data");
20089 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20090 return (EIO);
20091 }
20092 /*
20093 * Read capacity and block size from the READ CAPACITY 10 data.
20094 * This data may be adjusted later due to device specific
20095 * issues.
20096 *
20097 * According to the SCSI spec, the READ CAPACITY 10
20098 * command returns the following:
20099 *
20100 * bytes 0-3: Maximum logical block address available.
20101 * (MSB in byte:0 & LSB in byte:3)
20102 *
20103 * bytes 4-7: Block length in bytes
20104 * (MSB in byte:4 & LSB in byte:7)
20105 *
20106 */
20107 capacity = BE_32(capacity_buf[0]);
20108 lbasize = BE_32(capacity_buf[1]);
20109
20110 /*
20111 * Done with capacity_buf
20112 */
20113 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20114
20115 /*
20116 * if the reported capacity is set to all 0xf's, then
20117 * this disk is too large and requires SBC-2 commands.
20118 * Reissue the request using READ CAPACITY 16.
20119 */
20120 if (capacity == 0xffffffff) {
20121 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20122 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20123 &lbasize, &pbsize, path_flag);
20124 if (status != 0) {
20125 return (status);
20126 } else {
20127 goto rc16_done;
20128 }
20129 }
20130 break; /* Success! */
20131 case EIO:
20132 switch (ucmd_buf.uscsi_status) {
20133 case STATUS_RESERVATION_CONFLICT:
20134 status = EACCES;
20135 break;
20136 case STATUS_CHECK:
20137 /*
20138 * Check condition; look for ASC/ASCQ of 0x04/0x01
20139 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20140 */
20141 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20142 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20143 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20144 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20145 return (EAGAIN);
20146 }
20147 break;
20148 default:
20149 break;
20150 }
20151 /* FALLTHRU */
20152 default:
20153 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20154 return (status);
20155 }
20156
20157 /*
20158 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20159 * (2352 and 0 are common) so for these devices always force the value
20160 * to 2048 as required by the ATAPI specs.
20161 */
20162 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20163 lbasize = 2048;
20164 }
20165
20166 /*
20167 * Get the maximum LBA value from the READ CAPACITY data.
20168 * Here we assume that the Partial Medium Indicator (PMI) bit
20169 * was cleared when issuing the command. This means that the LBA
20170 * returned from the device is the LBA of the last logical block
20171 * on the logical unit. The actual logical block count will be
20172 * this value plus one.
20173 */
20174 capacity += 1;
20175
20176 /*
20177 * Currently, for removable media, the capacity is saved in terms
20178 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20179 */
20180 if (un->un_f_has_removable_media)
20181 capacity *= (lbasize / un->un_sys_blocksize);
20182
20183 rc16_done:
20184
20185 /*
20186 * Copy the values from the READ CAPACITY command into the space
20187 * provided by the caller.
20188 */
20189 *capp = capacity;
20190 *lbap = lbasize;
20191
20192 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20193 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize);
20194
20195 /*
20196 * Both the lbasize and capacity from the device must be nonzero,
20197 * otherwise we assume that the values are not valid and return
20198 * failure to the caller. (4203735)
20199 */
20200 if ((capacity == 0) || (lbasize == 0)) {
20201 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20202 "sd_send_scsi_READ_CAPACITY received invalid value "
20203 "capacity %llu lbasize %d", capacity, lbasize);
20204 return (EIO);
20205 }
20206 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20207 return (0);
20208 }
20209
20210 /*
20211 * Function: sd_send_scsi_READ_CAPACITY_16
20212 *
20213 * Description: This routine uses the scsi READ CAPACITY 16 command to
20214 * determine the device capacity in number of blocks and the
20215 * device native block size. If this function returns a failure,
20216 * then the values in *capp and *lbap are undefined.
20217 * This routine should be called by sd_send_scsi_READ_CAPACITY
20218 * which will apply any device specific adjustments to capacity
20219 * and lbasize. One exception is it is also called by
20220 * sd_get_media_info_ext. In that function, there is no need to
20221 * adjust the capacity and lbasize.
20222 *
20223 * Arguments: ssc - ssc contains ptr to soft state struct for the target
20224 * capp - ptr to unsigned 64-bit variable to receive the
20225 * capacity value from the command.
20226 * lbap - ptr to unsigned 32-bit varaible to receive the
20227 * block size value from the command
20228 * psp - ptr to unsigned 32-bit variable to receive the
20229 * physical block size value from the command
20230 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20231 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20232 * to use the USCSI "direct" chain and bypass the normal
20233 * command waitq. SD_PATH_DIRECT_PRIORITY is used when
20234 * this command is issued as part of an error recovery
20235 * action.
20236 *
20237 * Return Code: 0 - Success
20238 * EIO - IO error
20239 * EACCES - Reservation conflict detected
20240 * EAGAIN - Device is becoming ready
20241 * errno return code from sd_ssc_send()
20242 *
20243 * Context: Can sleep. Blocks until command completes.
20244 */
20245
20246 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16)
20247
20248 static int
20249 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20250 uint32_t *lbap, uint32_t *psp, int path_flag)
20251 {
20252 struct scsi_extended_sense sense_buf;
20253 struct uscsi_cmd ucmd_buf;
20254 union scsi_cdb cdb;
20255 uint64_t *capacity16_buf;
20256 uint64_t capacity;
20257 uint32_t lbasize;
20258 uint32_t pbsize;
20259 uint32_t lbpb_exp;
20260 int status;
20261 struct sd_lun *un;
20262
20263 ASSERT(ssc != NULL);
20264
20265 un = ssc->ssc_un;
20266 ASSERT(un != NULL);
20267 ASSERT(!mutex_owned(SD_MUTEX(un)));
20268 ASSERT(capp != NULL);
20269 ASSERT(lbap != NULL);
20270
20271 SD_TRACE(SD_LOG_IO, un,
20272 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20273
20274 /*
20275 * First send a READ_CAPACITY_16 command to the target.
20276 *
20277 * Set up the CDB for the READ_CAPACITY_16 command. The Partial
20278 * Medium Indicator bit is cleared. The address field must be
20279 * zero if the PMI bit is zero.
20280 */
20281 bzero(&cdb, sizeof (cdb));
20282 bzero(&ucmd_buf, sizeof (ucmd_buf));
20283
20284 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20285
20286 ucmd_buf.uscsi_cdb = (char *)&cdb;
20287 ucmd_buf.uscsi_cdblen = CDB_GROUP4;
20288 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
20289 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
20290 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20291 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20292 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20293 ucmd_buf.uscsi_timeout = 60;
20294
20295 /*
20296 * Read Capacity (16) is a Service Action In command. One
20297 * command byte (0x9E) is overloaded for multiple operations,
20298 * with the second CDB byte specifying the desired operation
20299 */
20300 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20301 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20302
20303 /*
20304 * Fill in allocation length field
20305 */
20306 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20307
20308 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20309 UIO_SYSSPACE, path_flag);
20310
20311 switch (status) {
20312 case 0:
20313 /* Return failure if we did not get valid capacity data. */
20314 if (ucmd_buf.uscsi_resid > 20) {
20315 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20316 "sd_send_scsi_READ_CAPACITY_16 received invalid "
20317 "capacity data");
20318 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20319 return (EIO);
20320 }
20321
20322 /*
20323 * Read capacity and block size from the READ CAPACITY 16 data.
20324 * This data may be adjusted later due to device specific
20325 * issues.
20326 *
20327 * According to the SCSI spec, the READ CAPACITY 16
20328 * command returns the following:
20329 *
20330 * bytes 0-7: Maximum logical block address available.
20331 * (MSB in byte:0 & LSB in byte:7)
20332 *
20333 * bytes 8-11: Block length in bytes
20334 * (MSB in byte:8 & LSB in byte:11)
20335 *
20336 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20337 */
20338 capacity = BE_64(capacity16_buf[0]);
20339 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20340 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20341
20342 pbsize = lbasize << lbpb_exp;
20343
20344 /*
20345 * Done with capacity16_buf
20346 */
20347 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20348
20349 /*
20350 * if the reported capacity is set to all 0xf's, then
20351 * this disk is too large. This could only happen with
20352 * a device that supports LBAs larger than 64 bits which
20353 * are not defined by any current T10 standards.
20354 */
20355 if (capacity == 0xffffffffffffffff) {
20356 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20357 "disk is too large");
20358 return (EIO);
20359 }
20360 break; /* Success! */
20361 case EIO:
20362 switch (ucmd_buf.uscsi_status) {
20363 case STATUS_RESERVATION_CONFLICT:
20364 status = EACCES;
20365 break;
20366 case STATUS_CHECK:
20367 /*
20368 * Check condition; look for ASC/ASCQ of 0x04/0x01
20369 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20370 */
20371 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20372 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20373 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20374 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20375 return (EAGAIN);
20376 }
20377 break;
20378 default:
20379 break;
20380 }
20381 /* FALLTHRU */
20382 default:
20383 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20384 return (status);
20385 }
20386
20387 /*
20388 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20389 * (2352 and 0 are common) so for these devices always force the value
20390 * to 2048 as required by the ATAPI specs.
20391 */
20392 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20393 lbasize = 2048;
20394 }
20395
20396 /*
20397 * Get the maximum LBA value from the READ CAPACITY 16 data.
20398 * Here we assume that the Partial Medium Indicator (PMI) bit
20399 * was cleared when issuing the command. This means that the LBA
20400 * returned from the device is the LBA of the last logical block
20401 * on the logical unit. The actual logical block count will be
20402 * this value plus one.
20403 */
20404 capacity += 1;
20405
20406 /*
20407 * Currently, for removable media, the capacity is saved in terms
20408 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20409 */
20410 if (un->un_f_has_removable_media)
20411 capacity *= (lbasize / un->un_sys_blocksize);
20412
20413 *capp = capacity;
20414 *lbap = lbasize;
20415 *psp = pbsize;
20416
20417 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20418 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n",
20419 capacity, lbasize, pbsize);
20420
20421 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20422 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20423 "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20424 "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20425 return (EIO);
20426 }
20427
20428 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20429 return (0);
20430 }
20431
20432
20433 /*
20434 * Function: sd_send_scsi_START_STOP_UNIT
20435 *
20436 * Description: Issue a scsi START STOP UNIT command to the target.
20437 *
20438 * Arguments: ssc - ssc contatins pointer to driver soft state (unit)
20439 * structure for this target.
20440 * pc_flag - SD_POWER_CONDITION
20441 * SD_START_STOP
20442 * flag - SD_TARGET_START
20443 * SD_TARGET_STOP
20444 * SD_TARGET_EJECT
20445 * SD_TARGET_CLOSE
20446 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20447 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20448 * to use the USCSI "direct" chain and bypass the normal
20449 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20450 * command is issued as part of an error recovery action.
20451 *
20452 * Return Code: 0 - Success
20453 * EIO - IO error
20454 * EACCES - Reservation conflict detected
20455 * ENXIO - Not Ready, medium not present
20456 * errno return code from sd_ssc_send()
20457 *
20458 * Context: Can sleep.
20459 */
20460
20461 static int
20462 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20463 int path_flag)
20464 {
20465 struct scsi_extended_sense sense_buf;
20466 union scsi_cdb cdb;
20467 struct uscsi_cmd ucmd_buf;
20468 int status;
20469 struct sd_lun *un;
20470
20471 ASSERT(ssc != NULL);
20472 un = ssc->ssc_un;
20473 ASSERT(un != NULL);
20474 ASSERT(!mutex_owned(SD_MUTEX(un)));
20475
20476 SD_TRACE(SD_LOG_IO, un,
20477 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20478
20479 if (un->un_f_check_start_stop &&
20480 (pc_flag == SD_START_STOP) &&
20481 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20482 (un->un_f_start_stop_supported != TRUE)) {
20483 return (0);
20484 }
20485
20486 /*
20487 * If we are performing an eject operation and
20488 * we receive any command other than SD_TARGET_EJECT
20489 * we should immediately return.
20490 */
20491 if (flag != SD_TARGET_EJECT) {
20492 mutex_enter(SD_MUTEX(un));
20493 if (un->un_f_ejecting == TRUE) {
20494 mutex_exit(SD_MUTEX(un));
20495 return (EAGAIN);
20496 }
20497 mutex_exit(SD_MUTEX(un));
20498 }
20499
20500 bzero(&cdb, sizeof (cdb));
20501 bzero(&ucmd_buf, sizeof (ucmd_buf));
20502 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20503
20504 cdb.scc_cmd = SCMD_START_STOP;
20505 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20506 (uchar_t)(flag << 4) : (uchar_t)flag;
20507
20508 ucmd_buf.uscsi_cdb = (char *)&cdb;
20509 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20510 ucmd_buf.uscsi_bufaddr = NULL;
20511 ucmd_buf.uscsi_buflen = 0;
20512 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20513 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20514 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20515 ucmd_buf.uscsi_timeout = 200;
20516
20517 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20518 UIO_SYSSPACE, path_flag);
20519
20520 switch (status) {
20521 case 0:
20522 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20523 break; /* Success! */
20524 case EIO:
20525 switch (ucmd_buf.uscsi_status) {
20526 case STATUS_RESERVATION_CONFLICT:
20527 status = EACCES;
20528 break;
20529 case STATUS_CHECK:
20530 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20531 switch (scsi_sense_key(
20532 (uint8_t *)&sense_buf)) {
20533 case KEY_ILLEGAL_REQUEST:
20534 status = ENOTSUP;
20535 break;
20536 case KEY_NOT_READY:
20537 if (scsi_sense_asc(
20538 (uint8_t *)&sense_buf)
20539 == 0x3A) {
20540 status = ENXIO;
20541 }
20542 break;
20543 default:
20544 break;
20545 }
20546 }
20547 break;
20548 default:
20549 break;
20550 }
20551 break;
20552 default:
20553 break;
20554 }
20555
20556 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20557
20558 return (status);
20559 }
20560
20561
20562 /*
20563 * Function: sd_start_stop_unit_callback
20564 *
20565 * Description: timeout(9F) callback to begin recovery process for a
20566 * device that has spun down.
20567 *
20568 * Arguments: arg - pointer to associated softstate struct.
20569 *
20570 * Context: Executes in a timeout(9F) thread context
20571 */
20572
20573 static void
20574 sd_start_stop_unit_callback(void *arg)
20575 {
20576 struct sd_lun *un = arg;
20577 ASSERT(un != NULL);
20578 ASSERT(!mutex_owned(SD_MUTEX(un)));
20579
20580 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20581
20582 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20583 }
20584
20585
20586 /*
20587 * Function: sd_start_stop_unit_task
20588 *
20589 * Description: Recovery procedure when a drive is spun down.
20590 *
20591 * Arguments: arg - pointer to associated softstate struct.
20592 *
20593 * Context: Executes in a taskq() thread context
20594 */
20595
20596 static void
20597 sd_start_stop_unit_task(void *arg)
20598 {
20599 struct sd_lun *un = arg;
20600 sd_ssc_t *ssc;
20601 int power_level;
20602 int rval;
20603
20604 ASSERT(un != NULL);
20605 ASSERT(!mutex_owned(SD_MUTEX(un)));
20606
20607 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20608
20609 /*
20610 * Some unformatted drives report not ready error, no need to
20611 * restart if format has been initiated.
20612 */
20613 mutex_enter(SD_MUTEX(un));
20614 if (un->un_f_format_in_progress == TRUE) {
20615 mutex_exit(SD_MUTEX(un));
20616 return;
20617 }
20618 mutex_exit(SD_MUTEX(un));
20619
20620 ssc = sd_ssc_init(un);
20621 /*
20622 * When a START STOP command is issued from here, it is part of a
20623 * failure recovery operation and must be issued before any other
20624 * commands, including any pending retries. Thus it must be sent
20625 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20626 * succeeds or not, we will start I/O after the attempt.
20627 * If power condition is supported and the current power level
20628 * is capable of performing I/O, we should set the power condition
20629 * to that level. Otherwise, set the power condition to ACTIVE.
20630 */
20631 if (un->un_f_power_condition_supported) {
20632 mutex_enter(SD_MUTEX(un));
20633 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20634 power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20635 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20636 mutex_exit(SD_MUTEX(un));
20637 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20638 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20639 } else {
20640 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20641 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20642 }
20643
20644 if (rval != 0)
20645 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20646 sd_ssc_fini(ssc);
20647 /*
20648 * The above call blocks until the START_STOP_UNIT command completes.
20649 * Now that it has completed, we must re-try the original IO that
20650 * received the NOT READY condition in the first place. There are
20651 * three possible conditions here:
20652 *
20653 * (1) The original IO is on un_retry_bp.
20654 * (2) The original IO is on the regular wait queue, and un_retry_bp
20655 * is NULL.
20656 * (3) The original IO is on the regular wait queue, and un_retry_bp
20657 * points to some other, unrelated bp.
20658 *
20659 * For each case, we must call sd_start_cmds() with un_retry_bp
20660 * as the argument. If un_retry_bp is NULL, this will initiate
20661 * processing of the regular wait queue. If un_retry_bp is not NULL,
20662 * then this will process the bp on un_retry_bp. That may or may not
20663 * be the original IO, but that does not matter: the important thing
20664 * is to keep the IO processing going at this point.
20665 *
20666 * Note: This is a very specific error recovery sequence associated
20667 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20668 * serialize the I/O with completion of the spin-up.
20669 */
20670 mutex_enter(SD_MUTEX(un));
20671 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20672 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20673 un, un->un_retry_bp);
20674 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */
20675 sd_start_cmds(un, un->un_retry_bp);
20676 mutex_exit(SD_MUTEX(un));
20677
20678 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20679 }
20680
20681
20682 /*
20683 * Function: sd_send_scsi_INQUIRY
20684 *
20685 * Description: Issue the scsi INQUIRY command.
20686 *
20687 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20688 * structure for this target.
20689 * bufaddr
20690 * buflen
20691 * evpd
20692 * page_code
20693 * page_length
20694 *
20695 * Return Code: 0 - Success
20696 * errno return code from sd_ssc_send()
20697 *
20698 * Context: Can sleep. Does not return until command is completed.
20699 */
20700
20701 static int
20702 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20703 uchar_t evpd, uchar_t page_code, size_t *residp)
20704 {
20705 union scsi_cdb cdb;
20706 struct uscsi_cmd ucmd_buf;
20707 int status;
20708 struct sd_lun *un;
20709
20710 ASSERT(ssc != NULL);
20711 un = ssc->ssc_un;
20712 ASSERT(un != NULL);
20713 ASSERT(!mutex_owned(SD_MUTEX(un)));
20714 ASSERT(bufaddr != NULL);
20715
20716 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20717
20718 bzero(&cdb, sizeof (cdb));
20719 bzero(&ucmd_buf, sizeof (ucmd_buf));
20720 bzero(bufaddr, buflen);
20721
20722 cdb.scc_cmd = SCMD_INQUIRY;
20723 cdb.cdb_opaque[1] = evpd;
20724 cdb.cdb_opaque[2] = page_code;
20725 FORMG0COUNT(&cdb, buflen);
20726
20727 ucmd_buf.uscsi_cdb = (char *)&cdb;
20728 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20729 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
20730 ucmd_buf.uscsi_buflen = buflen;
20731 ucmd_buf.uscsi_rqbuf = NULL;
20732 ucmd_buf.uscsi_rqlen = 0;
20733 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
20734 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */
20735
20736 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20737 UIO_SYSSPACE, SD_PATH_DIRECT);
20738
20739 /*
20740 * Only handle status == 0, the upper-level caller
20741 * will put different assessment based on the context.
20742 */
20743 if (status == 0)
20744 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20745
20746 if ((status == 0) && (residp != NULL)) {
20747 *residp = ucmd_buf.uscsi_resid;
20748 }
20749
20750 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20751
20752 return (status);
20753 }
20754
20755
20756 /*
20757 * Function: sd_send_scsi_TEST_UNIT_READY
20758 *
20759 * Description: Issue the scsi TEST UNIT READY command.
20760 * This routine can be told to set the flag USCSI_DIAGNOSE to
20761 * prevent retrying failed commands. Use this when the intent
20762 * is either to check for device readiness, to clear a Unit
20763 * Attention, or to clear any outstanding sense data.
20764 * However under specific conditions the expected behavior
20765 * is for retries to bring a device ready, so use the flag
20766 * with caution.
20767 *
20768 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20769 * structure for this target.
20770 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20771 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20772 * 0: dont check for media present, do retries on cmd.
20773 *
20774 * Return Code: 0 - Success
20775 * EIO - IO error
20776 * EACCES - Reservation conflict detected
20777 * ENXIO - Not Ready, medium not present
20778 * errno return code from sd_ssc_send()
20779 *
20780 * Context: Can sleep. Does not return until command is completed.
20781 */
20782
20783 static int
20784 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20785 {
20786 struct scsi_extended_sense sense_buf;
20787 union scsi_cdb cdb;
20788 struct uscsi_cmd ucmd_buf;
20789 int status;
20790 struct sd_lun *un;
20791
20792 ASSERT(ssc != NULL);
20793 un = ssc->ssc_un;
20794 ASSERT(un != NULL);
20795 ASSERT(!mutex_owned(SD_MUTEX(un)));
20796
20797 SD_TRACE(SD_LOG_IO, un,
20798 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20799
20800 /*
20801 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20802 * timeouts when they receive a TUR and the queue is not empty. Check
20803 * the configuration flag set during attach (indicating the drive has
20804 * this firmware bug) and un_ncmds_in_transport before issuing the
20805 * TUR. If there are
20806 * pending commands return success, this is a bit arbitrary but is ok
20807 * for non-removables (i.e. the eliteI disks) and non-clustering
20808 * configurations.
20809 */
20810 if (un->un_f_cfg_tur_check == TRUE) {
20811 mutex_enter(SD_MUTEX(un));
20812 if (un->un_ncmds_in_transport != 0) {
20813 mutex_exit(SD_MUTEX(un));
20814 return (0);
20815 }
20816 mutex_exit(SD_MUTEX(un));
20817 }
20818
20819 bzero(&cdb, sizeof (cdb));
20820 bzero(&ucmd_buf, sizeof (ucmd_buf));
20821 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20822
20823 cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20824
20825 ucmd_buf.uscsi_cdb = (char *)&cdb;
20826 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20827 ucmd_buf.uscsi_bufaddr = NULL;
20828 ucmd_buf.uscsi_buflen = 0;
20829 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20830 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20831 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20832
20833 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20834 if ((flag & SD_DONT_RETRY_TUR) != 0) {
20835 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20836 }
20837 ucmd_buf.uscsi_timeout = 60;
20838
20839 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20840 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20841 SD_PATH_STANDARD));
20842
20843 switch (status) {
20844 case 0:
20845 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20846 break; /* Success! */
20847 case EIO:
20848 switch (ucmd_buf.uscsi_status) {
20849 case STATUS_RESERVATION_CONFLICT:
20850 status = EACCES;
20851 break;
20852 case STATUS_CHECK:
20853 if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20854 break;
20855 }
20856 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20857 (scsi_sense_key((uint8_t *)&sense_buf) ==
20858 KEY_NOT_READY) &&
20859 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20860 status = ENXIO;
20861 }
20862 break;
20863 default:
20864 break;
20865 }
20866 break;
20867 default:
20868 break;
20869 }
20870
20871 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20872
20873 return (status);
20874 }
20875
20876 /*
20877 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20878 *
20879 * Description: Issue the scsi PERSISTENT RESERVE IN command.
20880 *
20881 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20882 * structure for this target.
20883 *
20884 * Return Code: 0 - Success
20885 * EACCES
20886 * ENOTSUP
20887 * errno return code from sd_ssc_send()
20888 *
20889 * Context: Can sleep. Does not return until command is completed.
20890 */
20891
20892 static int
20893 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
20894 uint16_t data_len, uchar_t *data_bufp)
20895 {
20896 struct scsi_extended_sense sense_buf;
20897 union scsi_cdb cdb;
20898 struct uscsi_cmd ucmd_buf;
20899 int status;
20900 int no_caller_buf = FALSE;
20901 struct sd_lun *un;
20902
20903 ASSERT(ssc != NULL);
20904 un = ssc->ssc_un;
20905 ASSERT(un != NULL);
20906 ASSERT(!mutex_owned(SD_MUTEX(un)));
20907 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20908
20909 SD_TRACE(SD_LOG_IO, un,
20910 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20911
20912 bzero(&cdb, sizeof (cdb));
20913 bzero(&ucmd_buf, sizeof (ucmd_buf));
20914 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20915 if (data_bufp == NULL) {
20916 /* Allocate a default buf if the caller did not give one */
20917 ASSERT(data_len == 0);
20918 data_len = MHIOC_RESV_KEY_SIZE;
20919 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20920 no_caller_buf = TRUE;
20921 }
20922
20923 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20924 cdb.cdb_opaque[1] = usr_cmd;
20925 FORMG1COUNT(&cdb, data_len);
20926
20927 ucmd_buf.uscsi_cdb = (char *)&cdb;
20928 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20929 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp;
20930 ucmd_buf.uscsi_buflen = data_len;
20931 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20932 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20933 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20934 ucmd_buf.uscsi_timeout = 60;
20935
20936 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20937 UIO_SYSSPACE, SD_PATH_STANDARD);
20938
20939 switch (status) {
20940 case 0:
20941 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20942
20943 break; /* Success! */
20944 case EIO:
20945 switch (ucmd_buf.uscsi_status) {
20946 case STATUS_RESERVATION_CONFLICT:
20947 status = EACCES;
20948 break;
20949 case STATUS_CHECK:
20950 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20951 (scsi_sense_key((uint8_t *)&sense_buf) ==
20952 KEY_ILLEGAL_REQUEST)) {
20953 status = ENOTSUP;
20954 }
20955 break;
20956 default:
20957 break;
20958 }
20959 break;
20960 default:
20961 break;
20962 }
20963
20964 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20965
20966 if (no_caller_buf == TRUE) {
20967 kmem_free(data_bufp, data_len);
20968 }
20969
20970 return (status);
20971 }
20972
20973
20974 /*
20975 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20976 *
20977 * Description: This routine is the driver entry point for handling CD-ROM
20978 * multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20979 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20980 * device.
20981 *
20982 * Arguments: ssc - ssc contains un - pointer to soft state struct
20983 * for the target.
20984 * usr_cmd SCSI-3 reservation facility command (one of
20985 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20986 * SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20987 * usr_bufp - user provided pointer register, reserve descriptor or
20988 * preempt and abort structure (mhioc_register_t,
20989 * mhioc_resv_desc_t, mhioc_preemptandabort_t)
20990 *
20991 * Return Code: 0 - Success
20992 * EACCES
20993 * ENOTSUP
20994 * errno return code from sd_ssc_send()
20995 *
20996 * Context: Can sleep. Does not return until command is completed.
20997 */
20998
20999 static int
21000 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
21001 uchar_t *usr_bufp)
21002 {
21003 struct scsi_extended_sense sense_buf;
21004 union scsi_cdb cdb;
21005 struct uscsi_cmd ucmd_buf;
21006 int status;
21007 uchar_t data_len = sizeof (sd_prout_t);
21008 sd_prout_t *prp;
21009 struct sd_lun *un;
21010
21011 ASSERT(ssc != NULL);
21012 un = ssc->ssc_un;
21013 ASSERT(un != NULL);
21014 ASSERT(!mutex_owned(SD_MUTEX(un)));
21015 ASSERT(data_len == 24); /* required by scsi spec */
21016
21017 SD_TRACE(SD_LOG_IO, un,
21018 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
21019
21020 if (usr_bufp == NULL) {
21021 return (EINVAL);
21022 }
21023
21024 bzero(&cdb, sizeof (cdb));
21025 bzero(&ucmd_buf, sizeof (ucmd_buf));
21026 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21027 prp = kmem_zalloc(data_len, KM_SLEEP);
21028
21029 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21030 cdb.cdb_opaque[1] = usr_cmd;
21031 FORMG1COUNT(&cdb, data_len);
21032
21033 ucmd_buf.uscsi_cdb = (char *)&cdb;
21034 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21035 ucmd_buf.uscsi_bufaddr = (caddr_t)prp;
21036 ucmd_buf.uscsi_buflen = data_len;
21037 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21038 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21039 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21040 ucmd_buf.uscsi_timeout = 60;
21041
21042 switch (usr_cmd) {
21043 case SD_SCSI3_REGISTER: {
21044 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21045
21046 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21047 bcopy(ptr->newkey.key, prp->service_key,
21048 MHIOC_RESV_KEY_SIZE);
21049 prp->aptpl = ptr->aptpl;
21050 break;
21051 }
21052 case SD_SCSI3_CLEAR: {
21053 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21054
21055 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21056 break;
21057 }
21058 case SD_SCSI3_RESERVE:
21059 case SD_SCSI3_RELEASE: {
21060 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21061
21062 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21063 prp->scope_address = BE_32(ptr->scope_specific_addr);
21064 cdb.cdb_opaque[2] = ptr->type;
21065 break;
21066 }
21067 case SD_SCSI3_PREEMPTANDABORT: {
21068 mhioc_preemptandabort_t *ptr =
21069 (mhioc_preemptandabort_t *)usr_bufp;
21070
21071 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21072 bcopy(ptr->victim_key.key, prp->service_key,
21073 MHIOC_RESV_KEY_SIZE);
21074 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21075 cdb.cdb_opaque[2] = ptr->resvdesc.type;
21076 ucmd_buf.uscsi_flags |= USCSI_HEAD;
21077 break;
21078 }
21079 case SD_SCSI3_REGISTERANDIGNOREKEY:
21080 {
21081 mhioc_registerandignorekey_t *ptr;
21082 ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21083 bcopy(ptr->newkey.key,
21084 prp->service_key, MHIOC_RESV_KEY_SIZE);
21085 prp->aptpl = ptr->aptpl;
21086 break;
21087 }
21088 default:
21089 ASSERT(FALSE);
21090 break;
21091 }
21092
21093 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21094 UIO_SYSSPACE, SD_PATH_STANDARD);
21095
21096 switch (status) {
21097 case 0:
21098 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21099 break; /* Success! */
21100 case EIO:
21101 switch (ucmd_buf.uscsi_status) {
21102 case STATUS_RESERVATION_CONFLICT:
21103 status = EACCES;
21104 break;
21105 case STATUS_CHECK:
21106 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21107 (scsi_sense_key((uint8_t *)&sense_buf) ==
21108 KEY_ILLEGAL_REQUEST)) {
21109 status = ENOTSUP;
21110 }
21111 break;
21112 default:
21113 break;
21114 }
21115 break;
21116 default:
21117 break;
21118 }
21119
21120 kmem_free(prp, data_len);
21121 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21122 return (status);
21123 }
21124
21125
21126 /*
21127 * Function: sd_send_scsi_SYNCHRONIZE_CACHE
21128 *
21129 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21130 *
21131 * Arguments: un - pointer to the target's soft state struct
21132 * dkc - pointer to the callback structure
21133 *
21134 * Return Code: 0 - success
21135 * errno-type error code
21136 *
21137 * Context: kernel thread context only.
21138 *
21139 * _______________________________________________________________
21140 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE |
21141 * |FLUSH_VOLATILE| | operation |
21142 * |______________|______________|_________________________________|
21143 * | 0 | NULL | Synchronous flush on both |
21144 * | | | volatile and non-volatile cache |
21145 * |______________|______________|_________________________________|
21146 * | 1 | NULL | Synchronous flush on volatile |
21147 * | | | cache; disk drivers may suppress|
21148 * | | | flush if disk table indicates |
21149 * | | | non-volatile cache |
21150 * |______________|______________|_________________________________|
21151 * | 0 | !NULL | Asynchronous flush on both |
21152 * | | | volatile and non-volatile cache;|
21153 * |______________|______________|_________________________________|
21154 * | 1 | !NULL | Asynchronous flush on volatile |
21155 * | | | cache; disk drivers may suppress|
21156 * | | | flush if disk table indicates |
21157 * | | | non-volatile cache |
21158 * |______________|______________|_________________________________|
21159 *
21160 */
21161
21162 static int
21163 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21164 {
21165 struct sd_uscsi_info *uip;
21166 struct uscsi_cmd *uscmd;
21167 union scsi_cdb *cdb;
21168 struct buf *bp;
21169 int rval = 0;
21170 int is_async;
21171
21172 SD_TRACE(SD_LOG_IO, un,
21173 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21174
21175 ASSERT(un != NULL);
21176 ASSERT(!mutex_owned(SD_MUTEX(un)));
21177
21178 if (dkc == NULL || dkc->dkc_callback == NULL) {
21179 is_async = FALSE;
21180 } else {
21181 is_async = TRUE;
21182 }
21183
21184 mutex_enter(SD_MUTEX(un));
21185 /* check whether cache flush should be suppressed */
21186 if (un->un_f_suppress_cache_flush == TRUE) {
21187 mutex_exit(SD_MUTEX(un));
21188 /*
21189 * suppress the cache flush if the device is told to do
21190 * so by sd.conf or disk table
21191 */
21192 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21193 skip the cache flush since suppress_cache_flush is %d!\n",
21194 un->un_f_suppress_cache_flush);
21195
21196 if (is_async == TRUE) {
21197 /* invoke callback for asynchronous flush */
21198 (*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21199 }
21200 return (rval);
21201 }
21202 mutex_exit(SD_MUTEX(un));
21203
21204 /*
21205 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21206 * set properly
21207 */
21208 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21209 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21210
21211 mutex_enter(SD_MUTEX(un));
21212 if (dkc != NULL && un->un_f_sync_nv_supported &&
21213 (dkc->dkc_flag & FLUSH_VOLATILE)) {
21214 /*
21215 * if the device supports SYNC_NV bit, turn on
21216 * the SYNC_NV bit to only flush volatile cache
21217 */
21218 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21219 }
21220 mutex_exit(SD_MUTEX(un));
21221
21222 /*
21223 * First get some memory for the uscsi_cmd struct and cdb
21224 * and initialize for SYNCHRONIZE_CACHE cmd.
21225 */
21226 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21227 uscmd->uscsi_cdblen = CDB_GROUP1;
21228 uscmd->uscsi_cdb = (caddr_t)cdb;
21229 uscmd->uscsi_bufaddr = NULL;
21230 uscmd->uscsi_buflen = 0;
21231 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21232 uscmd->uscsi_rqlen = SENSE_LENGTH;
21233 uscmd->uscsi_rqresid = SENSE_LENGTH;
21234 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21235 uscmd->uscsi_timeout = sd_io_time;
21236
21237 /*
21238 * Allocate an sd_uscsi_info struct and fill it with the info
21239 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
21240 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
21241 * since we allocate the buf here in this function, we do not
21242 * need to preserve the prior contents of b_private.
21243 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21244 */
21245 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21246 uip->ui_flags = SD_PATH_DIRECT;
21247 uip->ui_cmdp = uscmd;
21248
21249 bp = getrbuf(KM_SLEEP);
21250 bp->b_private = uip;
21251
21252 /*
21253 * Setup buffer to carry uscsi request.
21254 */
21255 bp->b_flags = B_BUSY;
21256 bp->b_bcount = 0;
21257 bp->b_blkno = 0;
21258
21259 if (is_async == TRUE) {
21260 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21261 uip->ui_dkc = *dkc;
21262 }
21263
21264 bp->b_edev = SD_GET_DEV(un);
21265 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */
21266
21267 /*
21268 * Unset un_f_sync_cache_required flag
21269 */
21270 mutex_enter(SD_MUTEX(un));
21271 un->un_f_sync_cache_required = FALSE;
21272 mutex_exit(SD_MUTEX(un));
21273
21274 (void) sd_uscsi_strategy(bp);
21275
21276 /*
21277 * If synchronous request, wait for completion
21278 * If async just return and let b_iodone callback
21279 * cleanup.
21280 * NOTE: On return, u_ncmds_in_driver will be decremented,
21281 * but it was also incremented in sd_uscsi_strategy(), so
21282 * we should be ok.
21283 */
21284 if (is_async == FALSE) {
21285 (void) biowait(bp);
21286 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21287 }
21288
21289 return (rval);
21290 }
21291
21292
21293 static int
21294 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21295 {
21296 struct sd_uscsi_info *uip;
21297 struct uscsi_cmd *uscmd;
21298 uint8_t *sense_buf;
21299 struct sd_lun *un;
21300 int status;
21301 union scsi_cdb *cdb;
21302
21303 uip = (struct sd_uscsi_info *)(bp->b_private);
21304 ASSERT(uip != NULL);
21305
21306 uscmd = uip->ui_cmdp;
21307 ASSERT(uscmd != NULL);
21308
21309 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21310 ASSERT(sense_buf != NULL);
21311
21312 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21313 ASSERT(un != NULL);
21314
21315 cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21316
21317 status = geterror(bp);
21318 switch (status) {
21319 case 0:
21320 break; /* Success! */
21321 case EIO:
21322 switch (uscmd->uscsi_status) {
21323 case STATUS_RESERVATION_CONFLICT:
21324 /* Ignore reservation conflict */
21325 status = 0;
21326 goto done;
21327
21328 case STATUS_CHECK:
21329 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21330 (scsi_sense_key(sense_buf) ==
21331 KEY_ILLEGAL_REQUEST)) {
21332 /* Ignore Illegal Request error */
21333 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21334 mutex_enter(SD_MUTEX(un));
21335 un->un_f_sync_nv_supported = FALSE;
21336 mutex_exit(SD_MUTEX(un));
21337 status = 0;
21338 SD_TRACE(SD_LOG_IO, un,
21339 "un_f_sync_nv_supported \
21340 is set to false.\n");
21341 goto done;
21342 }
21343
21344 mutex_enter(SD_MUTEX(un));
21345 un->un_f_sync_cache_supported = FALSE;
21346 mutex_exit(SD_MUTEX(un));
21347 SD_TRACE(SD_LOG_IO, un,
21348 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21349 un_f_sync_cache_supported set to false \
21350 with asc = %x, ascq = %x\n",
21351 scsi_sense_asc(sense_buf),
21352 scsi_sense_ascq(sense_buf));
21353 status = ENOTSUP;
21354 goto done;
21355 }
21356 break;
21357 default:
21358 break;
21359 }
21360 /* FALLTHRU */
21361 default:
21362 /*
21363 * Turn on the un_f_sync_cache_required flag
21364 * since the SYNC CACHE command failed
21365 */
21366 mutex_enter(SD_MUTEX(un));
21367 un->un_f_sync_cache_required = TRUE;
21368 mutex_exit(SD_MUTEX(un));
21369
21370 /*
21371 * Don't log an error message if this device
21372 * has removable media.
21373 */
21374 if (!un->un_f_has_removable_media) {
21375 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21376 "SYNCHRONIZE CACHE command failed (%d)\n", status);
21377 }
21378 break;
21379 }
21380
21381 done:
21382 if (uip->ui_dkc.dkc_callback != NULL) {
21383 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21384 }
21385
21386 ASSERT((bp->b_flags & B_REMAPPED) == 0);
21387 freerbuf(bp);
21388 kmem_free(uip, sizeof (struct sd_uscsi_info));
21389 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21390 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21391 kmem_free(uscmd, sizeof (struct uscsi_cmd));
21392
21393 return (status);
21394 }
21395
21396
21397 /*
21398 * Function: sd_send_scsi_GET_CONFIGURATION
21399 *
21400 * Description: Issues the get configuration command to the device.
21401 * Called from sd_check_for_writable_cd & sd_get_media_info
21402 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21403 * Arguments: ssc
21404 * ucmdbuf
21405 * rqbuf
21406 * rqbuflen
21407 * bufaddr
21408 * buflen
21409 * path_flag
21410 *
21411 * Return Code: 0 - Success
21412 * errno return code from sd_ssc_send()
21413 *
21414 * Context: Can sleep. Does not return until command is completed.
21415 *
21416 */
21417
21418 static int
21419 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21420 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21421 int path_flag)
21422 {
21423 char cdb[CDB_GROUP1];
21424 int status;
21425 struct sd_lun *un;
21426
21427 ASSERT(ssc != NULL);
21428 un = ssc->ssc_un;
21429 ASSERT(un != NULL);
21430 ASSERT(!mutex_owned(SD_MUTEX(un)));
21431 ASSERT(bufaddr != NULL);
21432 ASSERT(ucmdbuf != NULL);
21433 ASSERT(rqbuf != NULL);
21434
21435 SD_TRACE(SD_LOG_IO, un,
21436 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21437
21438 bzero(cdb, sizeof (cdb));
21439 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21440 bzero(rqbuf, rqbuflen);
21441 bzero(bufaddr, buflen);
21442
21443 /*
21444 * Set up cdb field for the get configuration command.
21445 */
21446 cdb[0] = SCMD_GET_CONFIGURATION;
21447 cdb[1] = 0x02; /* Requested Type */
21448 cdb[8] = SD_PROFILE_HEADER_LEN;
21449 ucmdbuf->uscsi_cdb = cdb;
21450 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21451 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21452 ucmdbuf->uscsi_buflen = buflen;
21453 ucmdbuf->uscsi_timeout = sd_io_time;
21454 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21455 ucmdbuf->uscsi_rqlen = rqbuflen;
21456 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21457
21458 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21459 UIO_SYSSPACE, path_flag);
21460
21461 switch (status) {
21462 case 0:
21463 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21464 break; /* Success! */
21465 case EIO:
21466 switch (ucmdbuf->uscsi_status) {
21467 case STATUS_RESERVATION_CONFLICT:
21468 status = EACCES;
21469 break;
21470 default:
21471 break;
21472 }
21473 break;
21474 default:
21475 break;
21476 }
21477
21478 if (status == 0) {
21479 SD_DUMP_MEMORY(un, SD_LOG_IO,
21480 "sd_send_scsi_GET_CONFIGURATION: data",
21481 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21482 }
21483
21484 SD_TRACE(SD_LOG_IO, un,
21485 "sd_send_scsi_GET_CONFIGURATION: exit\n");
21486
21487 return (status);
21488 }
21489
21490 /*
21491 * Function: sd_send_scsi_feature_GET_CONFIGURATION
21492 *
21493 * Description: Issues the get configuration command to the device to
21494 * retrieve a specific feature. Called from
21495 * sd_check_for_writable_cd & sd_set_mmc_caps.
21496 * Arguments: ssc
21497 * ucmdbuf
21498 * rqbuf
21499 * rqbuflen
21500 * bufaddr
21501 * buflen
21502 * feature
21503 *
21504 * Return Code: 0 - Success
21505 * errno return code from sd_ssc_send()
21506 *
21507 * Context: Can sleep. Does not return until command is completed.
21508 *
21509 */
21510 static int
21511 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21512 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21513 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21514 {
21515 char cdb[CDB_GROUP1];
21516 int status;
21517 struct sd_lun *un;
21518
21519 ASSERT(ssc != NULL);
21520 un = ssc->ssc_un;
21521 ASSERT(un != NULL);
21522 ASSERT(!mutex_owned(SD_MUTEX(un)));
21523 ASSERT(bufaddr != NULL);
21524 ASSERT(ucmdbuf != NULL);
21525 ASSERT(rqbuf != NULL);
21526
21527 SD_TRACE(SD_LOG_IO, un,
21528 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21529
21530 bzero(cdb, sizeof (cdb));
21531 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21532 bzero(rqbuf, rqbuflen);
21533 bzero(bufaddr, buflen);
21534
21535 /*
21536 * Set up cdb field for the get configuration command.
21537 */
21538 cdb[0] = SCMD_GET_CONFIGURATION;
21539 cdb[1] = 0x02; /* Requested Type */
21540 cdb[3] = feature;
21541 cdb[8] = buflen;
21542 ucmdbuf->uscsi_cdb = cdb;
21543 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21544 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21545 ucmdbuf->uscsi_buflen = buflen;
21546 ucmdbuf->uscsi_timeout = sd_io_time;
21547 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21548 ucmdbuf->uscsi_rqlen = rqbuflen;
21549 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21550
21551 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21552 UIO_SYSSPACE, path_flag);
21553
21554 switch (status) {
21555 case 0:
21556
21557 break; /* Success! */
21558 case EIO:
21559 switch (ucmdbuf->uscsi_status) {
21560 case STATUS_RESERVATION_CONFLICT:
21561 status = EACCES;
21562 break;
21563 default:
21564 break;
21565 }
21566 break;
21567 default:
21568 break;
21569 }
21570
21571 if (status == 0) {
21572 SD_DUMP_MEMORY(un, SD_LOG_IO,
21573 "sd_send_scsi_feature_GET_CONFIGURATION: data",
21574 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21575 }
21576
21577 SD_TRACE(SD_LOG_IO, un,
21578 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21579
21580 return (status);
21581 }
21582
21583
21584 /*
21585 * Function: sd_send_scsi_MODE_SENSE
21586 *
21587 * Description: Utility function for issuing a scsi MODE SENSE command.
21588 * Note: This routine uses a consistent implementation for Group0,
21589 * Group1, and Group2 commands across all platforms. ATAPI devices
21590 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21591 *
21592 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21593 * structure for this target.
21594 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21595 * CDB_GROUP[1|2] (10 byte).
21596 * bufaddr - buffer for page data retrieved from the target.
21597 * buflen - size of page to be retrieved.
21598 * page_code - page code of data to be retrieved from the target.
21599 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21600 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21601 * to use the USCSI "direct" chain and bypass the normal
21602 * command waitq.
21603 *
21604 * Return Code: 0 - Success
21605 * errno return code from sd_ssc_send()
21606 *
21607 * Context: Can sleep. Does not return until command is completed.
21608 */
21609
21610 static int
21611 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21612 size_t buflen, uchar_t page_code, int path_flag)
21613 {
21614 struct scsi_extended_sense sense_buf;
21615 union scsi_cdb cdb;
21616 struct uscsi_cmd ucmd_buf;
21617 int status;
21618 int headlen;
21619 struct sd_lun *un;
21620
21621 ASSERT(ssc != NULL);
21622 un = ssc->ssc_un;
21623 ASSERT(un != NULL);
21624 ASSERT(!mutex_owned(SD_MUTEX(un)));
21625 ASSERT(bufaddr != NULL);
21626 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21627 (cdbsize == CDB_GROUP2));
21628
21629 SD_TRACE(SD_LOG_IO, un,
21630 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21631
21632 bzero(&cdb, sizeof (cdb));
21633 bzero(&ucmd_buf, sizeof (ucmd_buf));
21634 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21635 bzero(bufaddr, buflen);
21636
21637 if (cdbsize == CDB_GROUP0) {
21638 cdb.scc_cmd = SCMD_MODE_SENSE;
21639 cdb.cdb_opaque[2] = page_code;
21640 FORMG0COUNT(&cdb, buflen);
21641 headlen = MODE_HEADER_LENGTH;
21642 } else {
21643 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21644 cdb.cdb_opaque[2] = page_code;
21645 FORMG1COUNT(&cdb, buflen);
21646 headlen = MODE_HEADER_LENGTH_GRP2;
21647 }
21648
21649 ASSERT(headlen <= buflen);
21650 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21651
21652 ucmd_buf.uscsi_cdb = (char *)&cdb;
21653 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21654 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21655 ucmd_buf.uscsi_buflen = buflen;
21656 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21657 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21658 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21659 ucmd_buf.uscsi_timeout = 60;
21660
21661 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21662 UIO_SYSSPACE, path_flag);
21663
21664 switch (status) {
21665 case 0:
21666 /*
21667 * sr_check_wp() uses 0x3f page code and check the header of
21668 * mode page to determine if target device is write-protected.
21669 * But some USB devices return 0 bytes for 0x3f page code. For
21670 * this case, make sure that mode page header is returned at
21671 * least.
21672 */
21673 if (buflen - ucmd_buf.uscsi_resid < headlen) {
21674 status = EIO;
21675 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21676 "mode page header is not returned");
21677 }
21678 break; /* Success! */
21679 case EIO:
21680 switch (ucmd_buf.uscsi_status) {
21681 case STATUS_RESERVATION_CONFLICT:
21682 status = EACCES;
21683 break;
21684 default:
21685 break;
21686 }
21687 break;
21688 default:
21689 break;
21690 }
21691
21692 if (status == 0) {
21693 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21694 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21695 }
21696 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21697
21698 return (status);
21699 }
21700
21701
21702 /*
21703 * Function: sd_send_scsi_MODE_SELECT
21704 *
21705 * Description: Utility function for issuing a scsi MODE SELECT command.
21706 * Note: This routine uses a consistent implementation for Group0,
21707 * Group1, and Group2 commands across all platforms. ATAPI devices
21708 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21709 *
21710 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21711 * structure for this target.
21712 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21713 * CDB_GROUP[1|2] (10 byte).
21714 * bufaddr - buffer for page data retrieved from the target.
21715 * buflen - size of page to be retrieved.
21716 * save_page - boolean to determin if SP bit should be set.
21717 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21718 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21719 * to use the USCSI "direct" chain and bypass the normal
21720 * command waitq.
21721 *
21722 * Return Code: 0 - Success
21723 * errno return code from sd_ssc_send()
21724 *
21725 * Context: Can sleep. Does not return until command is completed.
21726 */
21727
21728 static int
21729 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21730 size_t buflen, uchar_t save_page, int path_flag)
21731 {
21732 struct scsi_extended_sense sense_buf;
21733 union scsi_cdb cdb;
21734 struct uscsi_cmd ucmd_buf;
21735 int status;
21736 struct sd_lun *un;
21737
21738 ASSERT(ssc != NULL);
21739 un = ssc->ssc_un;
21740 ASSERT(un != NULL);
21741 ASSERT(!mutex_owned(SD_MUTEX(un)));
21742 ASSERT(bufaddr != NULL);
21743 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21744 (cdbsize == CDB_GROUP2));
21745
21746 SD_TRACE(SD_LOG_IO, un,
21747 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21748
21749 bzero(&cdb, sizeof (cdb));
21750 bzero(&ucmd_buf, sizeof (ucmd_buf));
21751 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21752
21753 /* Set the PF bit for many third party drives */
21754 cdb.cdb_opaque[1] = 0x10;
21755
21756 /* Set the savepage(SP) bit if given */
21757 if (save_page == SD_SAVE_PAGE) {
21758 cdb.cdb_opaque[1] |= 0x01;
21759 }
21760
21761 if (cdbsize == CDB_GROUP0) {
21762 cdb.scc_cmd = SCMD_MODE_SELECT;
21763 FORMG0COUNT(&cdb, buflen);
21764 } else {
21765 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21766 FORMG1COUNT(&cdb, buflen);
21767 }
21768
21769 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21770
21771 ucmd_buf.uscsi_cdb = (char *)&cdb;
21772 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21773 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21774 ucmd_buf.uscsi_buflen = buflen;
21775 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21776 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21777 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21778 ucmd_buf.uscsi_timeout = 60;
21779
21780 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21781 UIO_SYSSPACE, path_flag);
21782
21783 switch (status) {
21784 case 0:
21785 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21786 break; /* Success! */
21787 case EIO:
21788 switch (ucmd_buf.uscsi_status) {
21789 case STATUS_RESERVATION_CONFLICT:
21790 status = EACCES;
21791 break;
21792 default:
21793 break;
21794 }
21795 break;
21796 default:
21797 break;
21798 }
21799
21800 if (status == 0) {
21801 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21802 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21803 }
21804 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21805
21806 return (status);
21807 }
21808
21809
21810 /*
21811 * Function: sd_send_scsi_RDWR
21812 *
21813 * Description: Issue a scsi READ or WRITE command with the given parameters.
21814 *
21815 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21816 * structure for this target.
21817 * cmd: SCMD_READ or SCMD_WRITE
21818 * bufaddr: Address of caller's buffer to receive the RDWR data
21819 * buflen: Length of caller's buffer receive the RDWR data.
21820 * start_block: Block number for the start of the RDWR operation.
21821 * (Assumes target-native block size.)
21822 * residp: Pointer to variable to receive the redisual of the
21823 * RDWR operation (may be NULL of no residual requested).
21824 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21825 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21826 * to use the USCSI "direct" chain and bypass the normal
21827 * command waitq.
21828 *
21829 * Return Code: 0 - Success
21830 * errno return code from sd_ssc_send()
21831 *
21832 * Context: Can sleep. Does not return until command is completed.
21833 */
21834
21835 static int
21836 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21837 size_t buflen, daddr_t start_block, int path_flag)
21838 {
21839 struct scsi_extended_sense sense_buf;
21840 union scsi_cdb cdb;
21841 struct uscsi_cmd ucmd_buf;
21842 uint32_t block_count;
21843 int status;
21844 int cdbsize;
21845 uchar_t flag;
21846 struct sd_lun *un;
21847
21848 ASSERT(ssc != NULL);
21849 un = ssc->ssc_un;
21850 ASSERT(un != NULL);
21851 ASSERT(!mutex_owned(SD_MUTEX(un)));
21852 ASSERT(bufaddr != NULL);
21853 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21854
21855 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21856
21857 if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21858 return (EINVAL);
21859 }
21860
21861 mutex_enter(SD_MUTEX(un));
21862 block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21863 mutex_exit(SD_MUTEX(un));
21864
21865 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21866
21867 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21868 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21869 bufaddr, buflen, start_block, block_count);
21870
21871 bzero(&cdb, sizeof (cdb));
21872 bzero(&ucmd_buf, sizeof (ucmd_buf));
21873 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21874
21875 /* Compute CDB size to use */
21876 if (start_block > 0xffffffff)
21877 cdbsize = CDB_GROUP4;
21878 else if ((start_block & 0xFFE00000) ||
21879 (un->un_f_cfg_is_atapi == TRUE))
21880 cdbsize = CDB_GROUP1;
21881 else
21882 cdbsize = CDB_GROUP0;
21883
21884 switch (cdbsize) {
21885 case CDB_GROUP0: /* 6-byte CDBs */
21886 cdb.scc_cmd = cmd;
21887 FORMG0ADDR(&cdb, start_block);
21888 FORMG0COUNT(&cdb, block_count);
21889 break;
21890 case CDB_GROUP1: /* 10-byte CDBs */
21891 cdb.scc_cmd = cmd | SCMD_GROUP1;
21892 FORMG1ADDR(&cdb, start_block);
21893 FORMG1COUNT(&cdb, block_count);
21894 break;
21895 case CDB_GROUP4: /* 16-byte CDBs */
21896 cdb.scc_cmd = cmd | SCMD_GROUP4;
21897 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21898 FORMG4COUNT(&cdb, block_count);
21899 break;
21900 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */
21901 default:
21902 /* All others reserved */
21903 return (EINVAL);
21904 }
21905
21906 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21907 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21908
21909 ucmd_buf.uscsi_cdb = (char *)&cdb;
21910 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21911 ucmd_buf.uscsi_bufaddr = bufaddr;
21912 ucmd_buf.uscsi_buflen = buflen;
21913 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21914 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21915 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT;
21916 ucmd_buf.uscsi_timeout = 60;
21917 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21918 UIO_SYSSPACE, path_flag);
21919
21920 switch (status) {
21921 case 0:
21922 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21923 break; /* Success! */
21924 case EIO:
21925 switch (ucmd_buf.uscsi_status) {
21926 case STATUS_RESERVATION_CONFLICT:
21927 status = EACCES;
21928 break;
21929 default:
21930 break;
21931 }
21932 break;
21933 default:
21934 break;
21935 }
21936
21937 if (status == 0) {
21938 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21939 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21940 }
21941
21942 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21943
21944 return (status);
21945 }
21946
21947
21948 /*
21949 * Function: sd_send_scsi_LOG_SENSE
21950 *
21951 * Description: Issue a scsi LOG_SENSE command with the given parameters.
21952 *
21953 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21954 * structure for this target.
21955 *
21956 * Return Code: 0 - Success
21957 * errno return code from sd_ssc_send()
21958 *
21959 * Context: Can sleep. Does not return until command is completed.
21960 */
21961
21962 static int
21963 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21964 uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21965 int path_flag)
21966
21967 {
21968 struct scsi_extended_sense sense_buf;
21969 union scsi_cdb cdb;
21970 struct uscsi_cmd ucmd_buf;
21971 int status;
21972 struct sd_lun *un;
21973
21974 ASSERT(ssc != NULL);
21975 un = ssc->ssc_un;
21976 ASSERT(un != NULL);
21977 ASSERT(!mutex_owned(SD_MUTEX(un)));
21978
21979 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21980
21981 bzero(&cdb, sizeof (cdb));
21982 bzero(&ucmd_buf, sizeof (ucmd_buf));
21983 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21984
21985 cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21986 cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21987 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21988 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF);
21989 FORMG1COUNT(&cdb, buflen);
21990
21991 ucmd_buf.uscsi_cdb = (char *)&cdb;
21992 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21993 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21994 ucmd_buf.uscsi_buflen = buflen;
21995 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21996 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21997 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21998 ucmd_buf.uscsi_timeout = 60;
21999
22000 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22001 UIO_SYSSPACE, path_flag);
22002
22003 switch (status) {
22004 case 0:
22005 break;
22006 case EIO:
22007 switch (ucmd_buf.uscsi_status) {
22008 case STATUS_RESERVATION_CONFLICT:
22009 status = EACCES;
22010 break;
22011 case STATUS_CHECK:
22012 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
22013 (scsi_sense_key((uint8_t *)&sense_buf) ==
22014 KEY_ILLEGAL_REQUEST) &&
22015 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
22016 /*
22017 * ASC 0x24: INVALID FIELD IN CDB
22018 */
22019 switch (page_code) {
22020 case START_STOP_CYCLE_PAGE:
22021 /*
22022 * The start stop cycle counter is
22023 * implemented as page 0x31 in earlier
22024 * generation disks. In new generation
22025 * disks the start stop cycle counter is
22026 * implemented as page 0xE. To properly
22027 * handle this case if an attempt for
22028 * log page 0xE is made and fails we
22029 * will try again using page 0x31.
22030 *
22031 * Network storage BU committed to
22032 * maintain the page 0x31 for this
22033 * purpose and will not have any other
22034 * page implemented with page code 0x31
22035 * until all disks transition to the
22036 * standard page.
22037 */
22038 mutex_enter(SD_MUTEX(un));
22039 un->un_start_stop_cycle_page =
22040 START_STOP_CYCLE_VU_PAGE;
22041 cdb.cdb_opaque[2] =
22042 (char)(page_control << 6) |
22043 un->un_start_stop_cycle_page;
22044 mutex_exit(SD_MUTEX(un));
22045 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22046 status = sd_ssc_send(
22047 ssc, &ucmd_buf, FKIOCTL,
22048 UIO_SYSSPACE, path_flag);
22049
22050 break;
22051 case TEMPERATURE_PAGE:
22052 status = ENOTTY;
22053 break;
22054 default:
22055 break;
22056 }
22057 }
22058 break;
22059 default:
22060 break;
22061 }
22062 break;
22063 default:
22064 break;
22065 }
22066
22067 if (status == 0) {
22068 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22069 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22070 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22071 }
22072
22073 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22074
22075 return (status);
22076 }
22077
22078
22079 /*
22080 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22081 *
22082 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22083 *
22084 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
22085 * structure for this target.
22086 * bufaddr
22087 * buflen
22088 * class_req
22089 *
22090 * Return Code: 0 - Success
22091 * errno return code from sd_ssc_send()
22092 *
22093 * Context: Can sleep. Does not return until command is completed.
22094 */
22095
22096 static int
22097 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22098 size_t buflen, uchar_t class_req)
22099 {
22100 union scsi_cdb cdb;
22101 struct uscsi_cmd ucmd_buf;
22102 int status;
22103 struct sd_lun *un;
22104
22105 ASSERT(ssc != NULL);
22106 un = ssc->ssc_un;
22107 ASSERT(un != NULL);
22108 ASSERT(!mutex_owned(SD_MUTEX(un)));
22109 ASSERT(bufaddr != NULL);
22110
22111 SD_TRACE(SD_LOG_IO, un,
22112 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22113
22114 bzero(&cdb, sizeof (cdb));
22115 bzero(&ucmd_buf, sizeof (ucmd_buf));
22116 bzero(bufaddr, buflen);
22117
22118 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22119 cdb.cdb_opaque[1] = 1; /* polled */
22120 cdb.cdb_opaque[4] = class_req;
22121 FORMG1COUNT(&cdb, buflen);
22122
22123 ucmd_buf.uscsi_cdb = (char *)&cdb;
22124 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
22125 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
22126 ucmd_buf.uscsi_buflen = buflen;
22127 ucmd_buf.uscsi_rqbuf = NULL;
22128 ucmd_buf.uscsi_rqlen = 0;
22129 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
22130 ucmd_buf.uscsi_timeout = 60;
22131
22132 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22133 UIO_SYSSPACE, SD_PATH_DIRECT);
22134
22135 /*
22136 * Only handle status == 0, the upper-level caller
22137 * will put different assessment based on the context.
22138 */
22139 if (status == 0) {
22140 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22141
22142 if (ucmd_buf.uscsi_resid != 0) {
22143 status = EIO;
22144 }
22145 }
22146
22147 SD_TRACE(SD_LOG_IO, un,
22148 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22149
22150 return (status);
22151 }
22152
22153
22154 static boolean_t
22155 sd_gesn_media_data_valid(uchar_t *data)
22156 {
22157 uint16_t len;
22158
22159 len = (data[1] << 8) | data[0];
22160 return ((len >= 6) &&
22161 ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22162 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22163 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22164 }
22165
22166
22167 /*
22168 * Function: sdioctl
22169 *
22170 * Description: Driver's ioctl(9e) entry point function.
22171 *
22172 * Arguments: dev - device number
22173 * cmd - ioctl operation to be performed
22174 * arg - user argument, contains data to be set or reference
22175 * parameter for get
22176 * flag - bit flag, indicating open settings, 32/64 bit type
22177 * cred_p - user credential pointer
22178 * rval_p - calling process return value (OPT)
22179 *
22180 * Return Code: EINVAL
22181 * ENOTTY
22182 * ENXIO
22183 * EIO
22184 * EFAULT
22185 * ENOTSUP
22186 * EPERM
22187 *
22188 * Context: Called from the device switch at normal priority.
22189 */
22190
22191 static int
22192 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22193 {
22194 struct sd_lun *un = NULL;
22195 int err = 0;
22196 int i = 0;
22197 cred_t *cr;
22198 int tmprval = EINVAL;
22199 boolean_t is_valid;
22200 sd_ssc_t *ssc;
22201
22202 /*
22203 * All device accesses go thru sdstrategy where we check on suspend
22204 * status
22205 */
22206 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22207 return (ENXIO);
22208 }
22209
22210 ASSERT(!mutex_owned(SD_MUTEX(un)));
22211
22212 /* Initialize sd_ssc_t for internal uscsi commands */
22213 ssc = sd_ssc_init(un);
22214
22215 is_valid = SD_IS_VALID_LABEL(un);
22216
22217 /*
22218 * Moved this wait from sd_uscsi_strategy to here for
22219 * reasons of deadlock prevention. Internal driver commands,
22220 * specifically those to change a devices power level, result
22221 * in a call to sd_uscsi_strategy.
22222 */
22223 mutex_enter(SD_MUTEX(un));
22224 while ((un->un_state == SD_STATE_SUSPENDED) ||
22225 (un->un_state == SD_STATE_PM_CHANGING)) {
22226 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22227 }
22228 /*
22229 * Twiddling the counter here protects commands from now
22230 * through to the top of sd_uscsi_strategy. Without the
22231 * counter inc. a power down, for example, could get in
22232 * after the above check for state is made and before
22233 * execution gets to the top of sd_uscsi_strategy.
22234 * That would cause problems.
22235 */
22236 un->un_ncmds_in_driver++;
22237
22238 if (!is_valid &&
22239 (flag & (FNDELAY | FNONBLOCK))) {
22240 switch (cmd) {
22241 case DKIOCGGEOM: /* SD_PATH_DIRECT */
22242 case DKIOCGVTOC:
22243 case DKIOCGEXTVTOC:
22244 case DKIOCGAPART:
22245 case DKIOCPARTINFO:
22246 case DKIOCEXTPARTINFO:
22247 case DKIOCSGEOM:
22248 case DKIOCSAPART:
22249 case DKIOCGETEFI:
22250 case DKIOCPARTITION:
22251 case DKIOCSVTOC:
22252 case DKIOCSEXTVTOC:
22253 case DKIOCSETEFI:
22254 case DKIOCGMBOOT:
22255 case DKIOCSMBOOT:
22256 case DKIOCG_PHYGEOM:
22257 case DKIOCG_VIRTGEOM:
22258 #if defined(__i386) || defined(__amd64)
22259 case DKIOCSETEXTPART:
22260 #endif
22261 /* let cmlb handle it */
22262 goto skip_ready_valid;
22263
22264 case CDROMPAUSE:
22265 case CDROMRESUME:
22266 case CDROMPLAYMSF:
22267 case CDROMPLAYTRKIND:
22268 case CDROMREADTOCHDR:
22269 case CDROMREADTOCENTRY:
22270 case CDROMSTOP:
22271 case CDROMSTART:
22272 case CDROMVOLCTRL:
22273 case CDROMSUBCHNL:
22274 case CDROMREADMODE2:
22275 case CDROMREADMODE1:
22276 case CDROMREADOFFSET:
22277 case CDROMSBLKMODE:
22278 case CDROMGBLKMODE:
22279 case CDROMGDRVSPEED:
22280 case CDROMSDRVSPEED:
22281 case CDROMCDDA:
22282 case CDROMCDXA:
22283 case CDROMSUBCODE:
22284 if (!ISCD(un)) {
22285 un->un_ncmds_in_driver--;
22286 ASSERT(un->un_ncmds_in_driver >= 0);
22287 mutex_exit(SD_MUTEX(un));
22288 err = ENOTTY;
22289 goto done_without_assess;
22290 }
22291 break;
22292 case FDEJECT:
22293 case DKIOCEJECT:
22294 case CDROMEJECT:
22295 if (!un->un_f_eject_media_supported) {
22296 un->un_ncmds_in_driver--;
22297 ASSERT(un->un_ncmds_in_driver >= 0);
22298 mutex_exit(SD_MUTEX(un));
22299 err = ENOTTY;
22300 goto done_without_assess;
22301 }
22302 break;
22303 case DKIOCFLUSHWRITECACHE:
22304 mutex_exit(SD_MUTEX(un));
22305 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22306 if (err != 0) {
22307 mutex_enter(SD_MUTEX(un));
22308 un->un_ncmds_in_driver--;
22309 ASSERT(un->un_ncmds_in_driver >= 0);
22310 mutex_exit(SD_MUTEX(un));
22311 err = EIO;
22312 goto done_quick_assess;
22313 }
22314 mutex_enter(SD_MUTEX(un));
22315 /* FALLTHROUGH */
22316 case DKIOCREMOVABLE:
22317 case DKIOCHOTPLUGGABLE:
22318 case DKIOCINFO:
22319 case DKIOCGMEDIAINFO:
22320 case DKIOCGMEDIAINFOEXT:
22321 case MHIOCENFAILFAST:
22322 case MHIOCSTATUS:
22323 case MHIOCTKOWN:
22324 case MHIOCRELEASE:
22325 case MHIOCGRP_INKEYS:
22326 case MHIOCGRP_INRESV:
22327 case MHIOCGRP_REGISTER:
22328 case MHIOCGRP_CLEAR:
22329 case MHIOCGRP_RESERVE:
22330 case MHIOCGRP_PREEMPTANDABORT:
22331 case MHIOCGRP_REGISTERANDIGNOREKEY:
22332 case CDROMCLOSETRAY:
22333 case USCSICMD:
22334 goto skip_ready_valid;
22335 default:
22336 break;
22337 }
22338
22339 mutex_exit(SD_MUTEX(un));
22340 err = sd_ready_and_valid(ssc, SDPART(dev));
22341 mutex_enter(SD_MUTEX(un));
22342
22343 if (err != SD_READY_VALID) {
22344 switch (cmd) {
22345 case DKIOCSTATE:
22346 case CDROMGDRVSPEED:
22347 case CDROMSDRVSPEED:
22348 case FDEJECT: /* for eject command */
22349 case DKIOCEJECT:
22350 case CDROMEJECT:
22351 case DKIOCREMOVABLE:
22352 case DKIOCHOTPLUGGABLE:
22353 break;
22354 default:
22355 if (un->un_f_has_removable_media) {
22356 err = ENXIO;
22357 } else {
22358 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22359 if (err == SD_RESERVED_BY_OTHERS) {
22360 err = EACCES;
22361 } else {
22362 err = EIO;
22363 }
22364 }
22365 un->un_ncmds_in_driver--;
22366 ASSERT(un->un_ncmds_in_driver >= 0);
22367 mutex_exit(SD_MUTEX(un));
22368
22369 goto done_without_assess;
22370 }
22371 }
22372 }
22373
22374 skip_ready_valid:
22375 mutex_exit(SD_MUTEX(un));
22376
22377 switch (cmd) {
22378 case DKIOCINFO:
22379 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22380 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22381 break;
22382
22383 case DKIOCGMEDIAINFO:
22384 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22385 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22386 break;
22387
22388 case DKIOCGMEDIAINFOEXT:
22389 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22390 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22391 break;
22392
22393 case DKIOCGGEOM:
22394 case DKIOCGVTOC:
22395 case DKIOCGEXTVTOC:
22396 case DKIOCGAPART:
22397 case DKIOCPARTINFO:
22398 case DKIOCEXTPARTINFO:
22399 case DKIOCSGEOM:
22400 case DKIOCSAPART:
22401 case DKIOCGETEFI:
22402 case DKIOCPARTITION:
22403 case DKIOCSVTOC:
22404 case DKIOCSEXTVTOC:
22405 case DKIOCSETEFI:
22406 case DKIOCGMBOOT:
22407 case DKIOCSMBOOT:
22408 case DKIOCG_PHYGEOM:
22409 case DKIOCG_VIRTGEOM:
22410 #if defined(__i386) || defined(__amd64)
22411 case DKIOCSETEXTPART:
22412 #endif
22413 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22414
22415 /* TUR should spin up */
22416
22417 if (un->un_f_has_removable_media)
22418 err = sd_send_scsi_TEST_UNIT_READY(ssc,
22419 SD_CHECK_FOR_MEDIA);
22420
22421 else
22422 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22423
22424 if (err != 0)
22425 goto done_with_assess;
22426
22427 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22428 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22429
22430 if ((err == 0) &&
22431 ((cmd == DKIOCSETEFI) ||
22432 (un->un_f_pkstats_enabled) &&
22433 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22434 cmd == DKIOCSEXTVTOC))) {
22435
22436 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22437 (void *)SD_PATH_DIRECT);
22438 if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22439 sd_set_pstats(un);
22440 SD_TRACE(SD_LOG_IO_PARTITION, un,
22441 "sd_ioctl: un:0x%p pstats created and "
22442 "set\n", un);
22443 }
22444 }
22445
22446 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22447 ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22448
22449 mutex_enter(SD_MUTEX(un));
22450 if (un->un_f_devid_supported &&
22451 (un->un_f_opt_fab_devid == TRUE)) {
22452 if (un->un_devid == NULL) {
22453 sd_register_devid(ssc, SD_DEVINFO(un),
22454 SD_TARGET_IS_UNRESERVED);
22455 } else {
22456 /*
22457 * The device id for this disk
22458 * has been fabricated. The
22459 * device id must be preserved
22460 * by writing it back out to
22461 * disk.
22462 */
22463 if (sd_write_deviceid(ssc) != 0) {
22464 ddi_devid_free(un->un_devid);
22465 un->un_devid = NULL;
22466 }
22467 }
22468 }
22469 mutex_exit(SD_MUTEX(un));
22470 }
22471
22472 break;
22473
22474 case DKIOCLOCK:
22475 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22476 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22477 SD_PATH_STANDARD);
22478 goto done_with_assess;
22479
22480 case DKIOCUNLOCK:
22481 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22482 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22483 SD_PATH_STANDARD);
22484 goto done_with_assess;
22485
22486 case DKIOCSTATE: {
22487 enum dkio_state state;
22488 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22489
22490 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22491 err = EFAULT;
22492 } else {
22493 err = sd_check_media(dev, state);
22494 if (err == 0) {
22495 if (ddi_copyout(&un->un_mediastate, (void *)arg,
22496 sizeof (int), flag) != 0)
22497 err = EFAULT;
22498 }
22499 }
22500 break;
22501 }
22502
22503 case DKIOCREMOVABLE:
22504 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22505 i = un->un_f_has_removable_media ? 1 : 0;
22506 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22507 err = EFAULT;
22508 } else {
22509 err = 0;
22510 }
22511 break;
22512
22513 case DKIOCHOTPLUGGABLE:
22514 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22515 i = un->un_f_is_hotpluggable ? 1 : 0;
22516 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22517 err = EFAULT;
22518 } else {
22519 err = 0;
22520 }
22521 break;
22522
22523 case DKIOCREADONLY:
22524 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22525 i = 0;
22526 if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22527 (sr_check_wp(dev) != 0)) {
22528 i = 1;
22529 }
22530 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22531 err = EFAULT;
22532 } else {
22533 err = 0;
22534 }
22535 break;
22536
22537 case DKIOCGTEMPERATURE:
22538 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22539 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22540 break;
22541
22542 case MHIOCENFAILFAST:
22543 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22544 if ((err = drv_priv(cred_p)) == 0) {
22545 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22546 }
22547 break;
22548
22549 case MHIOCTKOWN:
22550 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22551 if ((err = drv_priv(cred_p)) == 0) {
22552 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22553 }
22554 break;
22555
22556 case MHIOCRELEASE:
22557 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22558 if ((err = drv_priv(cred_p)) == 0) {
22559 err = sd_mhdioc_release(dev);
22560 }
22561 break;
22562
22563 case MHIOCSTATUS:
22564 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22565 if ((err = drv_priv(cred_p)) == 0) {
22566 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22567 case 0:
22568 err = 0;
22569 break;
22570 case EACCES:
22571 *rval_p = 1;
22572 err = 0;
22573 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22574 break;
22575 default:
22576 err = EIO;
22577 goto done_with_assess;
22578 }
22579 }
22580 break;
22581
22582 case MHIOCQRESERVE:
22583 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22584 if ((err = drv_priv(cred_p)) == 0) {
22585 err = sd_reserve_release(dev, SD_RESERVE);
22586 }
22587 break;
22588
22589 case MHIOCREREGISTERDEVID:
22590 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22591 if (drv_priv(cred_p) == EPERM) {
22592 err = EPERM;
22593 } else if (!un->un_f_devid_supported) {
22594 err = ENOTTY;
22595 } else {
22596 err = sd_mhdioc_register_devid(dev);
22597 }
22598 break;
22599
22600 case MHIOCGRP_INKEYS:
22601 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22602 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22603 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22604 err = ENOTSUP;
22605 } else {
22606 err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22607 flag);
22608 }
22609 }
22610 break;
22611
22612 case MHIOCGRP_INRESV:
22613 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22614 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22615 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22616 err = ENOTSUP;
22617 } else {
22618 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22619 }
22620 }
22621 break;
22622
22623 case MHIOCGRP_REGISTER:
22624 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22625 if ((err = drv_priv(cred_p)) != EPERM) {
22626 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22627 err = ENOTSUP;
22628 } else if (arg != NULL) {
22629 mhioc_register_t reg;
22630 if (ddi_copyin((void *)arg, ®,
22631 sizeof (mhioc_register_t), flag) != 0) {
22632 err = EFAULT;
22633 } else {
22634 err =
22635 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22636 ssc, SD_SCSI3_REGISTER,
22637 (uchar_t *)®);
22638 if (err != 0)
22639 goto done_with_assess;
22640 }
22641 }
22642 }
22643 break;
22644
22645 case MHIOCGRP_CLEAR:
22646 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22647 if ((err = drv_priv(cred_p)) != EPERM) {
22648 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22649 err = ENOTSUP;
22650 } else if (arg != NULL) {
22651 mhioc_register_t reg;
22652 if (ddi_copyin((void *)arg, ®,
22653 sizeof (mhioc_register_t), flag) != 0) {
22654 err = EFAULT;
22655 } else {
22656 err =
22657 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22658 ssc, SD_SCSI3_CLEAR,
22659 (uchar_t *)®);
22660 if (err != 0)
22661 goto done_with_assess;
22662 }
22663 }
22664 }
22665 break;
22666
22667 case MHIOCGRP_RESERVE:
22668 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22669 if ((err = drv_priv(cred_p)) != EPERM) {
22670 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22671 err = ENOTSUP;
22672 } else if (arg != NULL) {
22673 mhioc_resv_desc_t resv_desc;
22674 if (ddi_copyin((void *)arg, &resv_desc,
22675 sizeof (mhioc_resv_desc_t), flag) != 0) {
22676 err = EFAULT;
22677 } else {
22678 err =
22679 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22680 ssc, SD_SCSI3_RESERVE,
22681 (uchar_t *)&resv_desc);
22682 if (err != 0)
22683 goto done_with_assess;
22684 }
22685 }
22686 }
22687 break;
22688
22689 case MHIOCGRP_PREEMPTANDABORT:
22690 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22691 if ((err = drv_priv(cred_p)) != EPERM) {
22692 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22693 err = ENOTSUP;
22694 } else if (arg != NULL) {
22695 mhioc_preemptandabort_t preempt_abort;
22696 if (ddi_copyin((void *)arg, &preempt_abort,
22697 sizeof (mhioc_preemptandabort_t),
22698 flag) != 0) {
22699 err = EFAULT;
22700 } else {
22701 err =
22702 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22703 ssc, SD_SCSI3_PREEMPTANDABORT,
22704 (uchar_t *)&preempt_abort);
22705 if (err != 0)
22706 goto done_with_assess;
22707 }
22708 }
22709 }
22710 break;
22711
22712 case MHIOCGRP_REGISTERANDIGNOREKEY:
22713 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22714 if ((err = drv_priv(cred_p)) != EPERM) {
22715 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22716 err = ENOTSUP;
22717 } else if (arg != NULL) {
22718 mhioc_registerandignorekey_t r_and_i;
22719 if (ddi_copyin((void *)arg, (void *)&r_and_i,
22720 sizeof (mhioc_registerandignorekey_t),
22721 flag) != 0) {
22722 err = EFAULT;
22723 } else {
22724 err =
22725 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22726 ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22727 (uchar_t *)&r_and_i);
22728 if (err != 0)
22729 goto done_with_assess;
22730 }
22731 }
22732 }
22733 break;
22734
22735 case USCSICMD:
22736 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22737 cr = ddi_get_cred();
22738 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22739 err = EPERM;
22740 } else {
22741 enum uio_seg uioseg;
22742
22743 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22744 UIO_USERSPACE;
22745 if (un->un_f_format_in_progress == TRUE) {
22746 err = EAGAIN;
22747 break;
22748 }
22749
22750 err = sd_ssc_send(ssc,
22751 (struct uscsi_cmd *)arg,
22752 flag, uioseg, SD_PATH_STANDARD);
22753 if (err != 0)
22754 goto done_with_assess;
22755 else
22756 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22757 }
22758 break;
22759
22760 case CDROMPAUSE:
22761 case CDROMRESUME:
22762 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22763 if (!ISCD(un)) {
22764 err = ENOTTY;
22765 } else {
22766 err = sr_pause_resume(dev, cmd);
22767 }
22768 break;
22769
22770 case CDROMPLAYMSF:
22771 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22772 if (!ISCD(un)) {
22773 err = ENOTTY;
22774 } else {
22775 err = sr_play_msf(dev, (caddr_t)arg, flag);
22776 }
22777 break;
22778
22779 case CDROMPLAYTRKIND:
22780 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22781 #if defined(__i386) || defined(__amd64)
22782 /*
22783 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22784 */
22785 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22786 #else
22787 if (!ISCD(un)) {
22788 #endif
22789 err = ENOTTY;
22790 } else {
22791 err = sr_play_trkind(dev, (caddr_t)arg, flag);
22792 }
22793 break;
22794
22795 case CDROMREADTOCHDR:
22796 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22797 if (!ISCD(un)) {
22798 err = ENOTTY;
22799 } else {
22800 err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22801 }
22802 break;
22803
22804 case CDROMREADTOCENTRY:
22805 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22806 if (!ISCD(un)) {
22807 err = ENOTTY;
22808 } else {
22809 err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22810 }
22811 break;
22812
22813 case CDROMSTOP:
22814 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22815 if (!ISCD(un)) {
22816 err = ENOTTY;
22817 } else {
22818 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22819 SD_TARGET_STOP, SD_PATH_STANDARD);
22820 goto done_with_assess;
22821 }
22822 break;
22823
22824 case CDROMSTART:
22825 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22826 if (!ISCD(un)) {
22827 err = ENOTTY;
22828 } else {
22829 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22830 SD_TARGET_START, SD_PATH_STANDARD);
22831 goto done_with_assess;
22832 }
22833 break;
22834
22835 case CDROMCLOSETRAY:
22836 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22837 if (!ISCD(un)) {
22838 err = ENOTTY;
22839 } else {
22840 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22841 SD_TARGET_CLOSE, SD_PATH_STANDARD);
22842 goto done_with_assess;
22843 }
22844 break;
22845
22846 case FDEJECT: /* for eject command */
22847 case DKIOCEJECT:
22848 case CDROMEJECT:
22849 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22850 if (!un->un_f_eject_media_supported) {
22851 err = ENOTTY;
22852 } else {
22853 err = sr_eject(dev);
22854 }
22855 break;
22856
22857 case CDROMVOLCTRL:
22858 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22859 if (!ISCD(un)) {
22860 err = ENOTTY;
22861 } else {
22862 err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22863 }
22864 break;
22865
22866 case CDROMSUBCHNL:
22867 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22868 if (!ISCD(un)) {
22869 err = ENOTTY;
22870 } else {
22871 err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22872 }
22873 break;
22874
22875 case CDROMREADMODE2:
22876 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22877 if (!ISCD(un)) {
22878 err = ENOTTY;
22879 } else if (un->un_f_cfg_is_atapi == TRUE) {
22880 /*
22881 * If the drive supports READ CD, use that instead of
22882 * switching the LBA size via a MODE SELECT
22883 * Block Descriptor
22884 */
22885 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22886 } else {
22887 err = sr_read_mode2(dev, (caddr_t)arg, flag);
22888 }
22889 break;
22890
22891 case CDROMREADMODE1:
22892 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22893 if (!ISCD(un)) {
22894 err = ENOTTY;
22895 } else {
22896 err = sr_read_mode1(dev, (caddr_t)arg, flag);
22897 }
22898 break;
22899
22900 case CDROMREADOFFSET:
22901 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22902 if (!ISCD(un)) {
22903 err = ENOTTY;
22904 } else {
22905 err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22906 flag);
22907 }
22908 break;
22909
22910 case CDROMSBLKMODE:
22911 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22912 /*
22913 * There is no means of changing block size in case of atapi
22914 * drives, thus return ENOTTY if drive type is atapi
22915 */
22916 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22917 err = ENOTTY;
22918 } else if (un->un_f_mmc_cap == TRUE) {
22919
22920 /*
22921 * MMC Devices do not support changing the
22922 * logical block size
22923 *
22924 * Note: EINVAL is being returned instead of ENOTTY to
22925 * maintain consistancy with the original mmc
22926 * driver update.
22927 */
22928 err = EINVAL;
22929 } else {
22930 mutex_enter(SD_MUTEX(un));
22931 if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22932 (un->un_ncmds_in_transport > 0)) {
22933 mutex_exit(SD_MUTEX(un));
22934 err = EINVAL;
22935 } else {
22936 mutex_exit(SD_MUTEX(un));
22937 err = sr_change_blkmode(dev, cmd, arg, flag);
22938 }
22939 }
22940 break;
22941
22942 case CDROMGBLKMODE:
22943 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22944 if (!ISCD(un)) {
22945 err = ENOTTY;
22946 } else if ((un->un_f_cfg_is_atapi != FALSE) &&
22947 (un->un_f_blockcount_is_valid != FALSE)) {
22948 /*
22949 * Drive is an ATAPI drive so return target block
22950 * size for ATAPI drives since we cannot change the
22951 * blocksize on ATAPI drives. Used primarily to detect
22952 * if an ATAPI cdrom is present.
22953 */
22954 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22955 sizeof (int), flag) != 0) {
22956 err = EFAULT;
22957 } else {
22958 err = 0;
22959 }
22960
22961 } else {
22962 /*
22963 * Drive supports changing block sizes via a Mode
22964 * Select.
22965 */
22966 err = sr_change_blkmode(dev, cmd, arg, flag);
22967 }
22968 break;
22969
22970 case CDROMGDRVSPEED:
22971 case CDROMSDRVSPEED:
22972 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22973 if (!ISCD(un)) {
22974 err = ENOTTY;
22975 } else if (un->un_f_mmc_cap == TRUE) {
22976 /*
22977 * Note: In the future the driver implementation
22978 * for getting and
22979 * setting cd speed should entail:
22980 * 1) If non-mmc try the Toshiba mode page
22981 * (sr_change_speed)
22982 * 2) If mmc but no support for Real Time Streaming try
22983 * the SET CD SPEED (0xBB) command
22984 * (sr_atapi_change_speed)
22985 * 3) If mmc and support for Real Time Streaming
22986 * try the GET PERFORMANCE and SET STREAMING
22987 * commands (not yet implemented, 4380808)
22988 */
22989 /*
22990 * As per recent MMC spec, CD-ROM speed is variable
22991 * and changes with LBA. Since there is no such
22992 * things as drive speed now, fail this ioctl.
22993 *
22994 * Note: EINVAL is returned for consistancy of original
22995 * implementation which included support for getting
22996 * the drive speed of mmc devices but not setting
22997 * the drive speed. Thus EINVAL would be returned
22998 * if a set request was made for an mmc device.
22999 * We no longer support get or set speed for
23000 * mmc but need to remain consistent with regard
23001 * to the error code returned.
23002 */
23003 err = EINVAL;
23004 } else if (un->un_f_cfg_is_atapi == TRUE) {
23005 err = sr_atapi_change_speed(dev, cmd, arg, flag);
23006 } else {
23007 err = sr_change_speed(dev, cmd, arg, flag);
23008 }
23009 break;
23010
23011 case CDROMCDDA:
23012 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
23013 if (!ISCD(un)) {
23014 err = ENOTTY;
23015 } else {
23016 err = sr_read_cdda(dev, (void *)arg, flag);
23017 }
23018 break;
23019
23020 case CDROMCDXA:
23021 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23022 if (!ISCD(un)) {
23023 err = ENOTTY;
23024 } else {
23025 err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23026 }
23027 break;
23028
23029 case CDROMSUBCODE:
23030 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23031 if (!ISCD(un)) {
23032 err = ENOTTY;
23033 } else {
23034 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23035 }
23036 break;
23037
23038
23039 #ifdef SDDEBUG
23040 /* RESET/ABORTS testing ioctls */
23041 case DKIOCRESET: {
23042 int reset_level;
23043
23044 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23045 err = EFAULT;
23046 } else {
23047 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23048 "reset_level = 0x%lx\n", reset_level);
23049 if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23050 err = 0;
23051 } else {
23052 err = EIO;
23053 }
23054 }
23055 break;
23056 }
23057
23058 case DKIOCABORT:
23059 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23060 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23061 err = 0;
23062 } else {
23063 err = EIO;
23064 }
23065 break;
23066 #endif
23067
23068 #ifdef SD_FAULT_INJECTION
23069 /* SDIOC FaultInjection testing ioctls */
23070 case SDIOCSTART:
23071 case SDIOCSTOP:
23072 case SDIOCINSERTPKT:
23073 case SDIOCINSERTXB:
23074 case SDIOCINSERTUN:
23075 case SDIOCINSERTARQ:
23076 case SDIOCPUSH:
23077 case SDIOCRETRIEVE:
23078 case SDIOCRUN:
23079 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23080 "SDIOC detected cmd:0x%X:\n", cmd);
23081 /* call error generator */
23082 sd_faultinjection_ioctl(cmd, arg, un);
23083 err = 0;
23084 break;
23085
23086 #endif /* SD_FAULT_INJECTION */
23087
23088 case DKIOCFLUSHWRITECACHE:
23089 {
23090 struct dk_callback *dkc = (struct dk_callback *)arg;
23091
23092 mutex_enter(SD_MUTEX(un));
23093 if (!un->un_f_sync_cache_supported ||
23094 !un->un_f_write_cache_enabled) {
23095 err = un->un_f_sync_cache_supported ?
23096 0 : ENOTSUP;
23097 mutex_exit(SD_MUTEX(un));
23098 if ((flag & FKIOCTL) && dkc != NULL &&
23099 dkc->dkc_callback != NULL) {
23100 (*dkc->dkc_callback)(dkc->dkc_cookie,
23101 err);
23102 /*
23103 * Did callback and reported error.
23104 * Since we did a callback, ioctl
23105 * should return 0.
23106 */
23107 err = 0;
23108 }
23109 break;
23110 }
23111 mutex_exit(SD_MUTEX(un));
23112
23113 if ((flag & FKIOCTL) && dkc != NULL &&
23114 dkc->dkc_callback != NULL) {
23115 /* async SYNC CACHE request */
23116 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23117 } else {
23118 /* synchronous SYNC CACHE request */
23119 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23120 }
23121 }
23122 break;
23123
23124 case DKIOCGETWCE: {
23125
23126 int wce;
23127
23128 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23129 break;
23130 }
23131
23132 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23133 err = EFAULT;
23134 }
23135 break;
23136 }
23137
23138 case DKIOCSETWCE: {
23139
23140 int wce, sync_supported;
23141 int cur_wce = 0;
23142
23143 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23144 err = EFAULT;
23145 break;
23146 }
23147
23148 /*
23149 * Synchronize multiple threads trying to enable
23150 * or disable the cache via the un_f_wcc_cv
23151 * condition variable.
23152 */
23153 mutex_enter(SD_MUTEX(un));
23154
23155 /*
23156 * Don't allow the cache to be enabled if the
23157 * config file has it disabled.
23158 */
23159 if (un->un_f_opt_disable_cache && wce) {
23160 mutex_exit(SD_MUTEX(un));
23161 err = EINVAL;
23162 break;
23163 }
23164
23165 /*
23166 * Wait for write cache change in progress
23167 * bit to be clear before proceeding.
23168 */
23169 while (un->un_f_wcc_inprog)
23170 cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23171
23172 un->un_f_wcc_inprog = 1;
23173
23174 mutex_exit(SD_MUTEX(un));
23175
23176 /*
23177 * Get the current write cache state
23178 */
23179 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23180 mutex_enter(SD_MUTEX(un));
23181 un->un_f_wcc_inprog = 0;
23182 cv_broadcast(&un->un_wcc_cv);
23183 mutex_exit(SD_MUTEX(un));
23184 break;
23185 }
23186
23187 mutex_enter(SD_MUTEX(un));
23188 un->un_f_write_cache_enabled = (cur_wce != 0);
23189
23190 if (un->un_f_write_cache_enabled && wce == 0) {
23191 /*
23192 * Disable the write cache. Don't clear
23193 * un_f_write_cache_enabled until after
23194 * the mode select and flush are complete.
23195 */
23196 sync_supported = un->un_f_sync_cache_supported;
23197
23198 /*
23199 * If cache flush is suppressed, we assume that the
23200 * controller firmware will take care of managing the
23201 * write cache for us: no need to explicitly
23202 * disable it.
23203 */
23204 if (!un->un_f_suppress_cache_flush) {
23205 mutex_exit(SD_MUTEX(un));
23206 if ((err = sd_cache_control(ssc,
23207 SD_CACHE_NOCHANGE,
23208 SD_CACHE_DISABLE)) == 0 &&
23209 sync_supported) {
23210 err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23211 NULL);
23212 }
23213 } else {
23214 mutex_exit(SD_MUTEX(un));
23215 }
23216
23217 mutex_enter(SD_MUTEX(un));
23218 if (err == 0) {
23219 un->un_f_write_cache_enabled = 0;
23220 }
23221
23222 } else if (!un->un_f_write_cache_enabled && wce != 0) {
23223 /*
23224 * Set un_f_write_cache_enabled first, so there is
23225 * no window where the cache is enabled, but the
23226 * bit says it isn't.
23227 */
23228 un->un_f_write_cache_enabled = 1;
23229
23230 /*
23231 * If cache flush is suppressed, we assume that the
23232 * controller firmware will take care of managing the
23233 * write cache for us: no need to explicitly
23234 * enable it.
23235 */
23236 if (!un->un_f_suppress_cache_flush) {
23237 mutex_exit(SD_MUTEX(un));
23238 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23239 SD_CACHE_ENABLE);
23240 } else {
23241 mutex_exit(SD_MUTEX(un));
23242 }
23243
23244 mutex_enter(SD_MUTEX(un));
23245
23246 if (err) {
23247 un->un_f_write_cache_enabled = 0;
23248 }
23249 }
23250
23251 un->un_f_wcc_inprog = 0;
23252 cv_broadcast(&un->un_wcc_cv);
23253 mutex_exit(SD_MUTEX(un));
23254 break;
23255 }
23256
23257 default:
23258 err = ENOTTY;
23259 break;
23260 }
23261 mutex_enter(SD_MUTEX(un));
23262 un->un_ncmds_in_driver--;
23263 ASSERT(un->un_ncmds_in_driver >= 0);
23264 mutex_exit(SD_MUTEX(un));
23265
23266
23267 done_without_assess:
23268 sd_ssc_fini(ssc);
23269
23270 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23271 return (err);
23272
23273 done_with_assess:
23274 mutex_enter(SD_MUTEX(un));
23275 un->un_ncmds_in_driver--;
23276 ASSERT(un->un_ncmds_in_driver >= 0);
23277 mutex_exit(SD_MUTEX(un));
23278
23279 done_quick_assess:
23280 if (err != 0)
23281 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23282 /* Uninitialize sd_ssc_t pointer */
23283 sd_ssc_fini(ssc);
23284
23285 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23286 return (err);
23287 }
23288
23289
23290 /*
23291 * Function: sd_dkio_ctrl_info
23292 *
23293 * Description: This routine is the driver entry point for handling controller
23294 * information ioctl requests (DKIOCINFO).
23295 *
23296 * Arguments: dev - the device number
23297 * arg - pointer to user provided dk_cinfo structure
23298 * specifying the controller type and attributes.
23299 * flag - this argument is a pass through to ddi_copyxxx()
23300 * directly from the mode argument of ioctl().
23301 *
23302 * Return Code: 0
23303 * EFAULT
23304 * ENXIO
23305 */
23306
23307 static int
23308 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23309 {
23310 struct sd_lun *un = NULL;
23311 struct dk_cinfo *info;
23312 dev_info_t *pdip;
23313 int lun, tgt;
23314
23315 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23316 return (ENXIO);
23317 }
23318
23319 info = (struct dk_cinfo *)
23320 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23321
23322 switch (un->un_ctype) {
23323 case CTYPE_CDROM:
23324 info->dki_ctype = DKC_CDROM;
23325 break;
23326 default:
23327 info->dki_ctype = DKC_SCSI_CCS;
23328 break;
23329 }
23330 pdip = ddi_get_parent(SD_DEVINFO(un));
23331 info->dki_cnum = ddi_get_instance(pdip);
23332 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23333 (void) strcpy(info->dki_cname, ddi_get_name(pdip));
23334 } else {
23335 (void) strncpy(info->dki_cname, ddi_node_name(pdip),
23336 DK_DEVLEN - 1);
23337 }
23338
23339 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23340 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23341 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23342 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23343
23344 /* Unit Information */
23345 info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23346 info->dki_slave = ((tgt << 3) | lun);
23347 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23348 DK_DEVLEN - 1);
23349 info->dki_flags = DKI_FMTVOL;
23350 info->dki_partition = SDPART(dev);
23351
23352 /* Max Transfer size of this device in blocks */
23353 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23354 info->dki_addr = 0;
23355 info->dki_space = 0;
23356 info->dki_prio = 0;
23357 info->dki_vec = 0;
23358
23359 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23360 kmem_free(info, sizeof (struct dk_cinfo));
23361 return (EFAULT);
23362 } else {
23363 kmem_free(info, sizeof (struct dk_cinfo));
23364 return (0);
23365 }
23366 }
23367
23368 /*
23369 * Function: sd_get_media_info_com
23370 *
23371 * Description: This routine returns the information required to populate
23372 * the fields for the dk_minfo/dk_minfo_ext structures.
23373 *
23374 * Arguments: dev - the device number
23375 * dki_media_type - media_type
23376 * dki_lbsize - logical block size
23377 * dki_capacity - capacity in blocks
23378 * dki_pbsize - physical block size (if requested)
23379 *
23380 * Return Code: 0
23381 * EACCESS
23382 * EFAULT
23383 * ENXIO
23384 * EIO
23385 */
23386 static int
23387 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23388 diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23389 {
23390 struct sd_lun *un = NULL;
23391 struct uscsi_cmd com;
23392 struct scsi_inquiry *sinq;
23393 u_longlong_t media_capacity;
23394 uint64_t capacity;
23395 uint_t lbasize;
23396 uint_t pbsize;
23397 uchar_t *out_data;
23398 uchar_t *rqbuf;
23399 int rval = 0;
23400 int rtn;
23401 sd_ssc_t *ssc;
23402
23403 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23404 (un->un_state == SD_STATE_OFFLINE)) {
23405 return (ENXIO);
23406 }
23407
23408 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23409
23410 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23411 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23412 ssc = sd_ssc_init(un);
23413
23414 /* Issue a TUR to determine if the drive is ready with media present */
23415 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23416 if (rval == ENXIO) {
23417 goto done;
23418 } else if (rval != 0) {
23419 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23420 }
23421
23422 /* Now get configuration data */
23423 if (ISCD(un)) {
23424 *dki_media_type = DK_CDROM;
23425
23426 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23427 if (un->un_f_mmc_cap == TRUE) {
23428 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23429 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23430 SD_PATH_STANDARD);
23431
23432 if (rtn) {
23433 /*
23434 * We ignore all failures for CD and need to
23435 * put the assessment before processing code
23436 * to avoid missing assessment for FMA.
23437 */
23438 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23439 /*
23440 * Failed for other than an illegal request
23441 * or command not supported
23442 */
23443 if ((com.uscsi_status == STATUS_CHECK) &&
23444 (com.uscsi_rqstatus == STATUS_GOOD)) {
23445 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23446 (rqbuf[12] != 0x20)) {
23447 rval = EIO;
23448 goto no_assessment;
23449 }
23450 }
23451 } else {
23452 /*
23453 * The GET CONFIGURATION command succeeded
23454 * so set the media type according to the
23455 * returned data
23456 */
23457 *dki_media_type = out_data[6];
23458 *dki_media_type <<= 8;
23459 *dki_media_type |= out_data[7];
23460 }
23461 }
23462 } else {
23463 /*
23464 * The profile list is not available, so we attempt to identify
23465 * the media type based on the inquiry data
23466 */
23467 sinq = un->un_sd->sd_inq;
23468 if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23469 (sinq->inq_dtype == DTYPE_OPTICAL)) {
23470 /* This is a direct access device or optical disk */
23471 *dki_media_type = DK_FIXED_DISK;
23472
23473 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23474 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23475 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23476 *dki_media_type = DK_ZIP;
23477 } else if (
23478 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23479 *dki_media_type = DK_JAZ;
23480 }
23481 }
23482 } else {
23483 /*
23484 * Not a CD, direct access or optical disk so return
23485 * unknown media
23486 */
23487 *dki_media_type = DK_UNKNOWN;
23488 }
23489 }
23490
23491 /*
23492 * Now read the capacity so we can provide the lbasize,
23493 * pbsize and capacity.
23494 */
23495 if (dki_pbsize && un->un_f_descr_format_supported) {
23496 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23497 &pbsize, SD_PATH_DIRECT);
23498
23499 /*
23500 * Override the physical blocksize if the instance already
23501 * has a larger value.
23502 */
23503 pbsize = MAX(pbsize, un->un_phy_blocksize);
23504 }
23505
23506 if (dki_pbsize == NULL || rval != 0 ||
23507 !un->un_f_descr_format_supported) {
23508 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23509 SD_PATH_DIRECT);
23510
23511 switch (rval) {
23512 case 0:
23513 if (un->un_f_enable_rmw &&
23514 un->un_phy_blocksize != 0) {
23515 pbsize = un->un_phy_blocksize;
23516 } else {
23517 pbsize = lbasize;
23518 }
23519 media_capacity = capacity;
23520
23521 /*
23522 * sd_send_scsi_READ_CAPACITY() reports capacity in
23523 * un->un_sys_blocksize chunks. So we need to convert
23524 * it into cap.lbsize chunks.
23525 */
23526 if (un->un_f_has_removable_media) {
23527 media_capacity *= un->un_sys_blocksize;
23528 media_capacity /= lbasize;
23529 }
23530 break;
23531 case EACCES:
23532 rval = EACCES;
23533 goto done;
23534 default:
23535 rval = EIO;
23536 goto done;
23537 }
23538 } else {
23539 if (un->un_f_enable_rmw &&
23540 !ISP2(pbsize % DEV_BSIZE)) {
23541 pbsize = SSD_SECSIZE;
23542 } else if (!ISP2(lbasize % DEV_BSIZE) ||
23543 !ISP2(pbsize % DEV_BSIZE)) {
23544 pbsize = lbasize = DEV_BSIZE;
23545 }
23546 media_capacity = capacity;
23547 }
23548
23549 /*
23550 * If lun is expanded dynamically, update the un structure.
23551 */
23552 mutex_enter(SD_MUTEX(un));
23553 if ((un->un_f_blockcount_is_valid == TRUE) &&
23554 (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23555 (capacity > un->un_blockcount)) {
23556 un->un_f_expnevent = B_FALSE;
23557 sd_update_block_info(un, lbasize, capacity);
23558 }
23559 mutex_exit(SD_MUTEX(un));
23560
23561 *dki_lbsize = lbasize;
23562 *dki_capacity = media_capacity;
23563 if (dki_pbsize)
23564 *dki_pbsize = pbsize;
23565
23566 done:
23567 if (rval != 0) {
23568 if (rval == EIO)
23569 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23570 else
23571 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23572 }
23573 no_assessment:
23574 sd_ssc_fini(ssc);
23575 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23576 kmem_free(rqbuf, SENSE_LENGTH);
23577 return (rval);
23578 }
23579
23580 /*
23581 * Function: sd_get_media_info
23582 *
23583 * Description: This routine is the driver entry point for handling ioctl
23584 * requests for the media type or command set profile used by the
23585 * drive to operate on the media (DKIOCGMEDIAINFO).
23586 *
23587 * Arguments: dev - the device number
23588 * arg - pointer to user provided dk_minfo structure
23589 * specifying the media type, logical block size and
23590 * drive capacity.
23591 * flag - this argument is a pass through to ddi_copyxxx()
23592 * directly from the mode argument of ioctl().
23593 *
23594 * Return Code: returns the value from sd_get_media_info_com
23595 */
23596 static int
23597 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23598 {
23599 struct dk_minfo mi;
23600 int rval;
23601
23602 rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23603 &mi.dki_lbsize, &mi.dki_capacity, NULL);
23604
23605 if (rval)
23606 return (rval);
23607 if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23608 rval = EFAULT;
23609 return (rval);
23610 }
23611
23612 /*
23613 * Function: sd_get_media_info_ext
23614 *
23615 * Description: This routine is the driver entry point for handling ioctl
23616 * requests for the media type or command set profile used by the
23617 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23618 * difference this ioctl and DKIOCGMEDIAINFO is the return value
23619 * of this ioctl contains both logical block size and physical
23620 * block size.
23621 *
23622 *
23623 * Arguments: dev - the device number
23624 * arg - pointer to user provided dk_minfo_ext structure
23625 * specifying the media type, logical block size,
23626 * physical block size and disk capacity.
23627 * flag - this argument is a pass through to ddi_copyxxx()
23628 * directly from the mode argument of ioctl().
23629 *
23630 * Return Code: returns the value from sd_get_media_info_com
23631 */
23632 static int
23633 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23634 {
23635 struct dk_minfo_ext mie;
23636 int rval = 0;
23637
23638 rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23639 &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23640
23641 if (rval)
23642 return (rval);
23643 if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23644 rval = EFAULT;
23645 return (rval);
23646
23647 }
23648
23649 /*
23650 * Function: sd_watch_request_submit
23651 *
23652 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23653 * depending on which is supported by device.
23654 */
23655 static opaque_t
23656 sd_watch_request_submit(struct sd_lun *un)
23657 {
23658 dev_t dev;
23659
23660 /* All submissions are unified to use same device number */
23661 dev = sd_make_device(SD_DEVINFO(un));
23662
23663 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23664 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23665 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23666 (caddr_t)dev));
23667 } else {
23668 return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23669 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23670 (caddr_t)dev));
23671 }
23672 }
23673
23674
23675 /*
23676 * Function: sd_check_media
23677 *
23678 * Description: This utility routine implements the functionality for the
23679 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23680 * driver state changes from that specified by the user
23681 * (inserted or ejected). For example, if the user specifies
23682 * DKIO_EJECTED and the current media state is inserted this
23683 * routine will immediately return DKIO_INSERTED. However, if the
23684 * current media state is not inserted the user thread will be
23685 * blocked until the drive state changes. If DKIO_NONE is specified
23686 * the user thread will block until a drive state change occurs.
23687 *
23688 * Arguments: dev - the device number
23689 * state - user pointer to a dkio_state, updated with the current
23690 * drive state at return.
23691 *
23692 * Return Code: ENXIO
23693 * EIO
23694 * EAGAIN
23695 * EINTR
23696 */
23697
23698 static int
23699 sd_check_media(dev_t dev, enum dkio_state state)
23700 {
23701 struct sd_lun *un = NULL;
23702 enum dkio_state prev_state;
23703 opaque_t token = NULL;
23704 int rval = 0;
23705 sd_ssc_t *ssc;
23706
23707 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23708 return (ENXIO);
23709 }
23710
23711 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23712
23713 ssc = sd_ssc_init(un);
23714
23715 mutex_enter(SD_MUTEX(un));
23716
23717 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23718 "state=%x, mediastate=%x\n", state, un->un_mediastate);
23719
23720 prev_state = un->un_mediastate;
23721
23722 /* is there anything to do? */
23723 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23724 /*
23725 * submit the request to the scsi_watch service;
23726 * scsi_media_watch_cb() does the real work
23727 */
23728 mutex_exit(SD_MUTEX(un));
23729
23730 /*
23731 * This change handles the case where a scsi watch request is
23732 * added to a device that is powered down. To accomplish this
23733 * we power up the device before adding the scsi watch request,
23734 * since the scsi watch sends a TUR directly to the device
23735 * which the device cannot handle if it is powered down.
23736 */
23737 if (sd_pm_entry(un) != DDI_SUCCESS) {
23738 mutex_enter(SD_MUTEX(un));
23739 goto done;
23740 }
23741
23742 token = sd_watch_request_submit(un);
23743
23744 sd_pm_exit(un);
23745
23746 mutex_enter(SD_MUTEX(un));
23747 if (token == NULL) {
23748 rval = EAGAIN;
23749 goto done;
23750 }
23751
23752 /*
23753 * This is a special case IOCTL that doesn't return
23754 * until the media state changes. Routine sdpower
23755 * knows about and handles this so don't count it
23756 * as an active cmd in the driver, which would
23757 * keep the device busy to the pm framework.
23758 * If the count isn't decremented the device can't
23759 * be powered down.
23760 */
23761 un->un_ncmds_in_driver--;
23762 ASSERT(un->un_ncmds_in_driver >= 0);
23763
23764 /*
23765 * if a prior request had been made, this will be the same
23766 * token, as scsi_watch was designed that way.
23767 */
23768 un->un_swr_token = token;
23769 un->un_specified_mediastate = state;
23770
23771 /*
23772 * now wait for media change
23773 * we will not be signalled unless mediastate == state but it is
23774 * still better to test for this condition, since there is a
23775 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23776 */
23777 SD_TRACE(SD_LOG_COMMON, un,
23778 "sd_check_media: waiting for media state change\n");
23779 while (un->un_mediastate == state) {
23780 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23781 SD_TRACE(SD_LOG_COMMON, un,
23782 "sd_check_media: waiting for media state "
23783 "was interrupted\n");
23784 un->un_ncmds_in_driver++;
23785 rval = EINTR;
23786 goto done;
23787 }
23788 SD_TRACE(SD_LOG_COMMON, un,
23789 "sd_check_media: received signal, state=%x\n",
23790 un->un_mediastate);
23791 }
23792 /*
23793 * Inc the counter to indicate the device once again
23794 * has an active outstanding cmd.
23795 */
23796 un->un_ncmds_in_driver++;
23797 }
23798
23799 /* invalidate geometry */
23800 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23801 sr_ejected(un);
23802 }
23803
23804 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23805 uint64_t capacity;
23806 uint_t lbasize;
23807
23808 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23809 mutex_exit(SD_MUTEX(un));
23810 /*
23811 * Since the following routines use SD_PATH_DIRECT, we must
23812 * call PM directly before the upcoming disk accesses. This
23813 * may cause the disk to be power/spin up.
23814 */
23815
23816 if (sd_pm_entry(un) == DDI_SUCCESS) {
23817 rval = sd_send_scsi_READ_CAPACITY(ssc,
23818 &capacity, &lbasize, SD_PATH_DIRECT);
23819 if (rval != 0) {
23820 sd_pm_exit(un);
23821 if (rval == EIO)
23822 sd_ssc_assessment(ssc,
23823 SD_FMT_STATUS_CHECK);
23824 else
23825 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23826 mutex_enter(SD_MUTEX(un));
23827 goto done;
23828 }
23829 } else {
23830 rval = EIO;
23831 mutex_enter(SD_MUTEX(un));
23832 goto done;
23833 }
23834 mutex_enter(SD_MUTEX(un));
23835
23836 sd_update_block_info(un, lbasize, capacity);
23837
23838 /*
23839 * Check if the media in the device is writable or not
23840 */
23841 if (ISCD(un)) {
23842 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23843 }
23844
23845 mutex_exit(SD_MUTEX(un));
23846 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23847 if ((cmlb_validate(un->un_cmlbhandle, 0,
23848 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23849 sd_set_pstats(un);
23850 SD_TRACE(SD_LOG_IO_PARTITION, un,
23851 "sd_check_media: un:0x%p pstats created and "
23852 "set\n", un);
23853 }
23854
23855 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23856 SD_PATH_DIRECT);
23857
23858 sd_pm_exit(un);
23859
23860 if (rval != 0) {
23861 if (rval == EIO)
23862 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23863 else
23864 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23865 }
23866
23867 mutex_enter(SD_MUTEX(un));
23868 }
23869 done:
23870 sd_ssc_fini(ssc);
23871 un->un_f_watcht_stopped = FALSE;
23872 if (token != NULL && un->un_swr_token != NULL) {
23873 /*
23874 * Use of this local token and the mutex ensures that we avoid
23875 * some race conditions associated with terminating the
23876 * scsi watch.
23877 */
23878 token = un->un_swr_token;
23879 mutex_exit(SD_MUTEX(un));
23880 (void) scsi_watch_request_terminate(token,
23881 SCSI_WATCH_TERMINATE_WAIT);
23882 if (scsi_watch_get_ref_count(token) == 0) {
23883 mutex_enter(SD_MUTEX(un));
23884 un->un_swr_token = (opaque_t)NULL;
23885 } else {
23886 mutex_enter(SD_MUTEX(un));
23887 }
23888 }
23889
23890 /*
23891 * Update the capacity kstat value, if no media previously
23892 * (capacity kstat is 0) and a media has been inserted
23893 * (un_f_blockcount_is_valid == TRUE)
23894 */
23895 if (un->un_errstats) {
23896 struct sd_errstats *stp = NULL;
23897
23898 stp = (struct sd_errstats *)un->un_errstats->ks_data;
23899 if ((stp->sd_capacity.value.ui64 == 0) &&
23900 (un->un_f_blockcount_is_valid == TRUE)) {
23901 stp->sd_capacity.value.ui64 =
23902 (uint64_t)((uint64_t)un->un_blockcount *
23903 un->un_sys_blocksize);
23904 }
23905 }
23906 mutex_exit(SD_MUTEX(un));
23907 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23908 return (rval);
23909 }
23910
23911
23912 /*
23913 * Function: sd_delayed_cv_broadcast
23914 *
23915 * Description: Delayed cv_broadcast to allow for target to recover from media
23916 * insertion.
23917 *
23918 * Arguments: arg - driver soft state (unit) structure
23919 */
23920
23921 static void
23922 sd_delayed_cv_broadcast(void *arg)
23923 {
23924 struct sd_lun *un = arg;
23925
23926 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23927
23928 mutex_enter(SD_MUTEX(un));
23929 un->un_dcvb_timeid = NULL;
23930 cv_broadcast(&un->un_state_cv);
23931 mutex_exit(SD_MUTEX(un));
23932 }
23933
23934
23935 /*
23936 * Function: sd_media_watch_cb
23937 *
23938 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23939 * routine processes the TUR sense data and updates the driver
23940 * state if a transition has occurred. The user thread
23941 * (sd_check_media) is then signalled.
23942 *
23943 * Arguments: arg - the device 'dev_t' is used for context to discriminate
23944 * among multiple watches that share this callback function
23945 * resultp - scsi watch facility result packet containing scsi
23946 * packet, status byte and sense data
23947 *
23948 * Return Code: 0 for success, -1 for failure
23949 */
23950
23951 static int
23952 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23953 {
23954 struct sd_lun *un;
23955 struct scsi_status *statusp = resultp->statusp;
23956 uint8_t *sensep = (uint8_t *)resultp->sensep;
23957 enum dkio_state state = DKIO_NONE;
23958 dev_t dev = (dev_t)arg;
23959 uchar_t actual_sense_length;
23960 uint8_t skey, asc, ascq;
23961
23962 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23963 return (-1);
23964 }
23965 actual_sense_length = resultp->actual_sense_length;
23966
23967 mutex_enter(SD_MUTEX(un));
23968 SD_TRACE(SD_LOG_COMMON, un,
23969 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23970 *((char *)statusp), (void *)sensep, actual_sense_length);
23971
23972 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23973 un->un_mediastate = DKIO_DEV_GONE;
23974 cv_broadcast(&un->un_state_cv);
23975 mutex_exit(SD_MUTEX(un));
23976
23977 return (0);
23978 }
23979
23980 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23981 if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23982 if ((resultp->mmc_data[5] &
23983 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23984 state = DKIO_INSERTED;
23985 } else {
23986 state = DKIO_EJECTED;
23987 }
23988 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23989 SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23990 sd_log_eject_request_event(un, KM_NOSLEEP);
23991 }
23992 }
23993 } else if (sensep != NULL) {
23994 /*
23995 * If there was a check condition then sensep points to valid
23996 * sense data. If status was not a check condition but a
23997 * reservation or busy status then the new state is DKIO_NONE.
23998 */
23999 skey = scsi_sense_key(sensep);
24000 asc = scsi_sense_asc(sensep);
24001 ascq = scsi_sense_ascq(sensep);
24002
24003 SD_INFO(SD_LOG_COMMON, un,
24004 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24005 skey, asc, ascq);
24006 /* This routine only uses up to 13 bytes of sense data. */
24007 if (actual_sense_length >= 13) {
24008 if (skey == KEY_UNIT_ATTENTION) {
24009 if (asc == 0x28) {
24010 state = DKIO_INSERTED;
24011 }
24012 } else if (skey == KEY_NOT_READY) {
24013 /*
24014 * Sense data of 02/06/00 means that the
24015 * drive could not read the media (No
24016 * reference position found). In this case
24017 * to prevent a hang on the DKIOCSTATE IOCTL
24018 * we set the media state to DKIO_INSERTED.
24019 */
24020 if (asc == 0x06 && ascq == 0x00)
24021 state = DKIO_INSERTED;
24022
24023 /*
24024 * if 02/04/02 means that the host
24025 * should send start command. Explicitly
24026 * leave the media state as is
24027 * (inserted) as the media is inserted
24028 * and host has stopped device for PM
24029 * reasons. Upon next true read/write
24030 * to this media will bring the
24031 * device to the right state good for
24032 * media access.
24033 */
24034 if (asc == 0x3a) {
24035 state = DKIO_EJECTED;
24036 } else {
24037 /*
24038 * If the drive is busy with an
24039 * operation or long write, keep the
24040 * media in an inserted state.
24041 */
24042
24043 if ((asc == 0x04) &&
24044 ((ascq == 0x02) ||
24045 (ascq == 0x07) ||
24046 (ascq == 0x08))) {
24047 state = DKIO_INSERTED;
24048 }
24049 }
24050 } else if (skey == KEY_NO_SENSE) {
24051 if ((asc == 0x00) && (ascq == 0x00)) {
24052 /*
24053 * Sense Data 00/00/00 does not provide
24054 * any information about the state of
24055 * the media. Ignore it.
24056 */
24057 mutex_exit(SD_MUTEX(un));
24058 return (0);
24059 }
24060 }
24061 }
24062 } else if ((*((char *)statusp) == STATUS_GOOD) &&
24063 (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24064 state = DKIO_INSERTED;
24065 }
24066
24067 SD_TRACE(SD_LOG_COMMON, un,
24068 "sd_media_watch_cb: state=%x, specified=%x\n",
24069 state, un->un_specified_mediastate);
24070
24071 /*
24072 * now signal the waiting thread if this is *not* the specified state;
24073 * delay the signal if the state is DKIO_INSERTED to allow the target
24074 * to recover
24075 */
24076 if (state != un->un_specified_mediastate) {
24077 un->un_mediastate = state;
24078 if (state == DKIO_INSERTED) {
24079 /*
24080 * delay the signal to give the drive a chance
24081 * to do what it apparently needs to do
24082 */
24083 SD_TRACE(SD_LOG_COMMON, un,
24084 "sd_media_watch_cb: delayed cv_broadcast\n");
24085 if (un->un_dcvb_timeid == NULL) {
24086 un->un_dcvb_timeid =
24087 timeout(sd_delayed_cv_broadcast, un,
24088 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24089 }
24090 } else {
24091 SD_TRACE(SD_LOG_COMMON, un,
24092 "sd_media_watch_cb: immediate cv_broadcast\n");
24093 cv_broadcast(&un->un_state_cv);
24094 }
24095 }
24096 mutex_exit(SD_MUTEX(un));
24097 return (0);
24098 }
24099
24100
24101 /*
24102 * Function: sd_dkio_get_temp
24103 *
24104 * Description: This routine is the driver entry point for handling ioctl
24105 * requests to get the disk temperature.
24106 *
24107 * Arguments: dev - the device number
24108 * arg - pointer to user provided dk_temperature structure.
24109 * flag - this argument is a pass through to ddi_copyxxx()
24110 * directly from the mode argument of ioctl().
24111 *
24112 * Return Code: 0
24113 * EFAULT
24114 * ENXIO
24115 * EAGAIN
24116 */
24117
24118 static int
24119 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24120 {
24121 struct sd_lun *un = NULL;
24122 struct dk_temperature *dktemp = NULL;
24123 uchar_t *temperature_page;
24124 int rval = 0;
24125 int path_flag = SD_PATH_STANDARD;
24126 sd_ssc_t *ssc;
24127
24128 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24129 return (ENXIO);
24130 }
24131
24132 ssc = sd_ssc_init(un);
24133 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24134
24135 /* copyin the disk temp argument to get the user flags */
24136 if (ddi_copyin((void *)arg, dktemp,
24137 sizeof (struct dk_temperature), flag) != 0) {
24138 rval = EFAULT;
24139 goto done;
24140 }
24141
24142 /* Initialize the temperature to invalid. */
24143 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24144 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24145
24146 /*
24147 * Note: Investigate removing the "bypass pm" semantic.
24148 * Can we just bypass PM always?
24149 */
24150 if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24151 path_flag = SD_PATH_DIRECT;
24152 ASSERT(!mutex_owned(&un->un_pm_mutex));
24153 mutex_enter(&un->un_pm_mutex);
24154 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24155 /*
24156 * If DKT_BYPASS_PM is set, and the drive happens to be
24157 * in low power mode, we can not wake it up, Need to
24158 * return EAGAIN.
24159 */
24160 mutex_exit(&un->un_pm_mutex);
24161 rval = EAGAIN;
24162 goto done;
24163 } else {
24164 /*
24165 * Indicate to PM the device is busy. This is required
24166 * to avoid a race - i.e. the ioctl is issuing a
24167 * command and the pm framework brings down the device
24168 * to low power mode (possible power cut-off on some
24169 * platforms).
24170 */
24171 mutex_exit(&un->un_pm_mutex);
24172 if (sd_pm_entry(un) != DDI_SUCCESS) {
24173 rval = EAGAIN;
24174 goto done;
24175 }
24176 }
24177 }
24178
24179 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24180
24181 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24182 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24183 if (rval != 0)
24184 goto done2;
24185
24186 /*
24187 * For the current temperature verify that the parameter length is 0x02
24188 * and the parameter code is 0x00
24189 */
24190 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24191 (temperature_page[5] == 0x00)) {
24192 if (temperature_page[9] == 0xFF) {
24193 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24194 } else {
24195 dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24196 }
24197 }
24198
24199 /*
24200 * For the reference temperature verify that the parameter
24201 * length is 0x02 and the parameter code is 0x01
24202 */
24203 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24204 (temperature_page[11] == 0x01)) {
24205 if (temperature_page[15] == 0xFF) {
24206 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24207 } else {
24208 dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24209 }
24210 }
24211
24212 /* Do the copyout regardless of the temperature commands status. */
24213 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24214 flag) != 0) {
24215 rval = EFAULT;
24216 goto done1;
24217 }
24218
24219 done2:
24220 if (rval != 0) {
24221 if (rval == EIO)
24222 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24223 else
24224 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24225 }
24226 done1:
24227 if (path_flag == SD_PATH_DIRECT) {
24228 sd_pm_exit(un);
24229 }
24230
24231 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24232 done:
24233 sd_ssc_fini(ssc);
24234 if (dktemp != NULL) {
24235 kmem_free(dktemp, sizeof (struct dk_temperature));
24236 }
24237
24238 return (rval);
24239 }
24240
24241
24242 /*
24243 * Function: sd_log_page_supported
24244 *
24245 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24246 * supported log pages.
24247 *
24248 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
24249 * structure for this target.
24250 * log_page -
24251 *
24252 * Return Code: -1 - on error (log sense is optional and may not be supported).
24253 * 0 - log page not found.
24254 * 1 - log page found.
24255 */
24256
24257 static int
24258 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24259 {
24260 uchar_t *log_page_data;
24261 int i;
24262 int match = 0;
24263 int log_size;
24264 int status = 0;
24265 struct sd_lun *un;
24266
24267 ASSERT(ssc != NULL);
24268 un = ssc->ssc_un;
24269 ASSERT(un != NULL);
24270
24271 log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24272
24273 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24274 SD_PATH_DIRECT);
24275
24276 if (status != 0) {
24277 if (status == EIO) {
24278 /*
24279 * Some disks do not support log sense, we
24280 * should ignore this kind of error(sense key is
24281 * 0x5 - illegal request).
24282 */
24283 uint8_t *sensep;
24284 int senlen;
24285
24286 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24287 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24288 ssc->ssc_uscsi_cmd->uscsi_rqresid);
24289
24290 if (senlen > 0 &&
24291 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24292 sd_ssc_assessment(ssc,
24293 SD_FMT_IGNORE_COMPROMISE);
24294 } else {
24295 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24296 }
24297 } else {
24298 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24299 }
24300
24301 SD_ERROR(SD_LOG_COMMON, un,
24302 "sd_log_page_supported: failed log page retrieval\n");
24303 kmem_free(log_page_data, 0xFF);
24304 return (-1);
24305 }
24306
24307 log_size = log_page_data[3];
24308
24309 /*
24310 * The list of supported log pages start from the fourth byte. Check
24311 * until we run out of log pages or a match is found.
24312 */
24313 for (i = 4; (i < (log_size + 4)) && !match; i++) {
24314 if (log_page_data[i] == log_page) {
24315 match++;
24316 }
24317 }
24318 kmem_free(log_page_data, 0xFF);
24319 return (match);
24320 }
24321
24322
24323 /*
24324 * Function: sd_mhdioc_failfast
24325 *
24326 * Description: This routine is the driver entry point for handling ioctl
24327 * requests to enable/disable the multihost failfast option.
24328 * (MHIOCENFAILFAST)
24329 *
24330 * Arguments: dev - the device number
24331 * arg - user specified probing interval.
24332 * flag - this argument is a pass through to ddi_copyxxx()
24333 * directly from the mode argument of ioctl().
24334 *
24335 * Return Code: 0
24336 * EFAULT
24337 * ENXIO
24338 */
24339
24340 static int
24341 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24342 {
24343 struct sd_lun *un = NULL;
24344 int mh_time;
24345 int rval = 0;
24346
24347 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24348 return (ENXIO);
24349 }
24350
24351 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24352 return (EFAULT);
24353
24354 if (mh_time) {
24355 mutex_enter(SD_MUTEX(un));
24356 un->un_resvd_status |= SD_FAILFAST;
24357 mutex_exit(SD_MUTEX(un));
24358 /*
24359 * If mh_time is INT_MAX, then this ioctl is being used for
24360 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24361 */
24362 if (mh_time != INT_MAX) {
24363 rval = sd_check_mhd(dev, mh_time);
24364 }
24365 } else {
24366 (void) sd_check_mhd(dev, 0);
24367 mutex_enter(SD_MUTEX(un));
24368 un->un_resvd_status &= ~SD_FAILFAST;
24369 mutex_exit(SD_MUTEX(un));
24370 }
24371 return (rval);
24372 }
24373
24374
24375 /*
24376 * Function: sd_mhdioc_takeown
24377 *
24378 * Description: This routine is the driver entry point for handling ioctl
24379 * requests to forcefully acquire exclusive access rights to the
24380 * multihost disk (MHIOCTKOWN).
24381 *
24382 * Arguments: dev - the device number
24383 * arg - user provided structure specifying the delay
24384 * parameters in milliseconds
24385 * flag - this argument is a pass through to ddi_copyxxx()
24386 * directly from the mode argument of ioctl().
24387 *
24388 * Return Code: 0
24389 * EFAULT
24390 * ENXIO
24391 */
24392
24393 static int
24394 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24395 {
24396 struct sd_lun *un = NULL;
24397 struct mhioctkown *tkown = NULL;
24398 int rval = 0;
24399
24400 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24401 return (ENXIO);
24402 }
24403
24404 if (arg != NULL) {
24405 tkown = (struct mhioctkown *)
24406 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24407 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24408 if (rval != 0) {
24409 rval = EFAULT;
24410 goto error;
24411 }
24412 }
24413
24414 rval = sd_take_ownership(dev, tkown);
24415 mutex_enter(SD_MUTEX(un));
24416 if (rval == 0) {
24417 un->un_resvd_status |= SD_RESERVE;
24418 if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24419 sd_reinstate_resv_delay =
24420 tkown->reinstate_resv_delay * 1000;
24421 } else {
24422 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24423 }
24424 /*
24425 * Give the scsi_watch routine interval set by
24426 * the MHIOCENFAILFAST ioctl precedence here.
24427 */
24428 if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24429 mutex_exit(SD_MUTEX(un));
24430 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24431 SD_TRACE(SD_LOG_IOCTL_MHD, un,
24432 "sd_mhdioc_takeown : %d\n",
24433 sd_reinstate_resv_delay);
24434 } else {
24435 mutex_exit(SD_MUTEX(un));
24436 }
24437 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24438 sd_mhd_reset_notify_cb, (caddr_t)un);
24439 } else {
24440 un->un_resvd_status &= ~SD_RESERVE;
24441 mutex_exit(SD_MUTEX(un));
24442 }
24443
24444 error:
24445 if (tkown != NULL) {
24446 kmem_free(tkown, sizeof (struct mhioctkown));
24447 }
24448 return (rval);
24449 }
24450
24451
24452 /*
24453 * Function: sd_mhdioc_release
24454 *
24455 * Description: This routine is the driver entry point for handling ioctl
24456 * requests to release exclusive access rights to the multihost
24457 * disk (MHIOCRELEASE).
24458 *
24459 * Arguments: dev - the device number
24460 *
24461 * Return Code: 0
24462 * ENXIO
24463 */
24464
24465 static int
24466 sd_mhdioc_release(dev_t dev)
24467 {
24468 struct sd_lun *un = NULL;
24469 timeout_id_t resvd_timeid_save;
24470 int resvd_status_save;
24471 int rval = 0;
24472
24473 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24474 return (ENXIO);
24475 }
24476
24477 mutex_enter(SD_MUTEX(un));
24478 resvd_status_save = un->un_resvd_status;
24479 un->un_resvd_status &=
24480 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24481 if (un->un_resvd_timeid) {
24482 resvd_timeid_save = un->un_resvd_timeid;
24483 un->un_resvd_timeid = NULL;
24484 mutex_exit(SD_MUTEX(un));
24485 (void) untimeout(resvd_timeid_save);
24486 } else {
24487 mutex_exit(SD_MUTEX(un));
24488 }
24489
24490 /*
24491 * destroy any pending timeout thread that may be attempting to
24492 * reinstate reservation on this device.
24493 */
24494 sd_rmv_resv_reclaim_req(dev);
24495
24496 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24497 mutex_enter(SD_MUTEX(un));
24498 if ((un->un_mhd_token) &&
24499 ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24500 mutex_exit(SD_MUTEX(un));
24501 (void) sd_check_mhd(dev, 0);
24502 } else {
24503 mutex_exit(SD_MUTEX(un));
24504 }
24505 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24506 sd_mhd_reset_notify_cb, (caddr_t)un);
24507 } else {
24508 /*
24509 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24510 */
24511 mutex_enter(SD_MUTEX(un));
24512 un->un_resvd_status = resvd_status_save;
24513 mutex_exit(SD_MUTEX(un));
24514 }
24515 return (rval);
24516 }
24517
24518
24519 /*
24520 * Function: sd_mhdioc_register_devid
24521 *
24522 * Description: This routine is the driver entry point for handling ioctl
24523 * requests to register the device id (MHIOCREREGISTERDEVID).
24524 *
24525 * Note: The implementation for this ioctl has been updated to
24526 * be consistent with the original PSARC case (1999/357)
24527 * (4375899, 4241671, 4220005)
24528 *
24529 * Arguments: dev - the device number
24530 *
24531 * Return Code: 0
24532 * ENXIO
24533 */
24534
24535 static int
24536 sd_mhdioc_register_devid(dev_t dev)
24537 {
24538 struct sd_lun *un = NULL;
24539 int rval = 0;
24540 sd_ssc_t *ssc;
24541
24542 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24543 return (ENXIO);
24544 }
24545
24546 ASSERT(!mutex_owned(SD_MUTEX(un)));
24547
24548 mutex_enter(SD_MUTEX(un));
24549
24550 /* If a devid already exists, de-register it */
24551 if (un->un_devid != NULL) {
24552 ddi_devid_unregister(SD_DEVINFO(un));
24553 /*
24554 * After unregister devid, needs to free devid memory
24555 */
24556 ddi_devid_free(un->un_devid);
24557 un->un_devid = NULL;
24558 }
24559
24560 /* Check for reservation conflict */
24561 mutex_exit(SD_MUTEX(un));
24562 ssc = sd_ssc_init(un);
24563 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24564 mutex_enter(SD_MUTEX(un));
24565
24566 switch (rval) {
24567 case 0:
24568 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24569 break;
24570 case EACCES:
24571 break;
24572 default:
24573 rval = EIO;
24574 }
24575
24576 mutex_exit(SD_MUTEX(un));
24577 if (rval != 0) {
24578 if (rval == EIO)
24579 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24580 else
24581 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24582 }
24583 sd_ssc_fini(ssc);
24584 return (rval);
24585 }
24586
24587
24588 /*
24589 * Function: sd_mhdioc_inkeys
24590 *
24591 * Description: This routine is the driver entry point for handling ioctl
24592 * requests to issue the SCSI-3 Persistent In Read Keys command
24593 * to the device (MHIOCGRP_INKEYS).
24594 *
24595 * Arguments: dev - the device number
24596 * arg - user provided in_keys structure
24597 * flag - this argument is a pass through to ddi_copyxxx()
24598 * directly from the mode argument of ioctl().
24599 *
24600 * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24601 * ENXIO
24602 * EFAULT
24603 */
24604
24605 static int
24606 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24607 {
24608 struct sd_lun *un;
24609 mhioc_inkeys_t inkeys;
24610 int rval = 0;
24611
24612 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24613 return (ENXIO);
24614 }
24615
24616 #ifdef _MULTI_DATAMODEL
24617 switch (ddi_model_convert_from(flag & FMODELS)) {
24618 case DDI_MODEL_ILP32: {
24619 struct mhioc_inkeys32 inkeys32;
24620
24621 if (ddi_copyin(arg, &inkeys32,
24622 sizeof (struct mhioc_inkeys32), flag) != 0) {
24623 return (EFAULT);
24624 }
24625 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24626 if ((rval = sd_persistent_reservation_in_read_keys(un,
24627 &inkeys, flag)) != 0) {
24628 return (rval);
24629 }
24630 inkeys32.generation = inkeys.generation;
24631 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24632 flag) != 0) {
24633 return (EFAULT);
24634 }
24635 break;
24636 }
24637 case DDI_MODEL_NONE:
24638 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24639 flag) != 0) {
24640 return (EFAULT);
24641 }
24642 if ((rval = sd_persistent_reservation_in_read_keys(un,
24643 &inkeys, flag)) != 0) {
24644 return (rval);
24645 }
24646 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24647 flag) != 0) {
24648 return (EFAULT);
24649 }
24650 break;
24651 }
24652
24653 #else /* ! _MULTI_DATAMODEL */
24654
24655 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24656 return (EFAULT);
24657 }
24658 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24659 if (rval != 0) {
24660 return (rval);
24661 }
24662 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24663 return (EFAULT);
24664 }
24665
24666 #endif /* _MULTI_DATAMODEL */
24667
24668 return (rval);
24669 }
24670
24671
24672 /*
24673 * Function: sd_mhdioc_inresv
24674 *
24675 * Description: This routine is the driver entry point for handling ioctl
24676 * requests to issue the SCSI-3 Persistent In Read Reservations
24677 * command to the device (MHIOCGRP_INKEYS).
24678 *
24679 * Arguments: dev - the device number
24680 * arg - user provided in_resv structure
24681 * flag - this argument is a pass through to ddi_copyxxx()
24682 * directly from the mode argument of ioctl().
24683 *
24684 * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24685 * ENXIO
24686 * EFAULT
24687 */
24688
24689 static int
24690 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24691 {
24692 struct sd_lun *un;
24693 mhioc_inresvs_t inresvs;
24694 int rval = 0;
24695
24696 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24697 return (ENXIO);
24698 }
24699
24700 #ifdef _MULTI_DATAMODEL
24701
24702 switch (ddi_model_convert_from(flag & FMODELS)) {
24703 case DDI_MODEL_ILP32: {
24704 struct mhioc_inresvs32 inresvs32;
24705
24706 if (ddi_copyin(arg, &inresvs32,
24707 sizeof (struct mhioc_inresvs32), flag) != 0) {
24708 return (EFAULT);
24709 }
24710 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24711 if ((rval = sd_persistent_reservation_in_read_resv(un,
24712 &inresvs, flag)) != 0) {
24713 return (rval);
24714 }
24715 inresvs32.generation = inresvs.generation;
24716 if (ddi_copyout(&inresvs32, arg,
24717 sizeof (struct mhioc_inresvs32), flag) != 0) {
24718 return (EFAULT);
24719 }
24720 break;
24721 }
24722 case DDI_MODEL_NONE:
24723 if (ddi_copyin(arg, &inresvs,
24724 sizeof (mhioc_inresvs_t), flag) != 0) {
24725 return (EFAULT);
24726 }
24727 if ((rval = sd_persistent_reservation_in_read_resv(un,
24728 &inresvs, flag)) != 0) {
24729 return (rval);
24730 }
24731 if (ddi_copyout(&inresvs, arg,
24732 sizeof (mhioc_inresvs_t), flag) != 0) {
24733 return (EFAULT);
24734 }
24735 break;
24736 }
24737
24738 #else /* ! _MULTI_DATAMODEL */
24739
24740 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24741 return (EFAULT);
24742 }
24743 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24744 if (rval != 0) {
24745 return (rval);
24746 }
24747 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24748 return (EFAULT);
24749 }
24750
24751 #endif /* ! _MULTI_DATAMODEL */
24752
24753 return (rval);
24754 }
24755
24756
24757 /*
24758 * The following routines support the clustering functionality described below
24759 * and implement lost reservation reclaim functionality.
24760 *
24761 * Clustering
24762 * ----------
24763 * The clustering code uses two different, independent forms of SCSI
24764 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24765 * Persistent Group Reservations. For any particular disk, it will use either
24766 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24767 *
24768 * SCSI-2
24769 * The cluster software takes ownership of a multi-hosted disk by issuing the
24770 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24771 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a
24772 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24773 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the
24774 * driver. The meaning of failfast is that if the driver (on this host) ever
24775 * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24776 * it should immediately panic the host. The motivation for this ioctl is that
24777 * if this host does encounter reservation conflict, the underlying cause is
24778 * that some other host of the cluster has decided that this host is no longer
24779 * in the cluster and has seized control of the disks for itself. Since this
24780 * host is no longer in the cluster, it ought to panic itself. The
24781 * MHIOCENFAILFAST ioctl does two things:
24782 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24783 * error to panic the host
24784 * (b) it sets up a periodic timer to test whether this host still has
24785 * "access" (in that no other host has reserved the device): if the
24786 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24787 * purpose of that periodic timer is to handle scenarios where the host is
24788 * otherwise temporarily quiescent, temporarily doing no real i/o.
24789 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24790 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for
24791 * the device itself.
24792 *
24793 * SCSI-3 PGR
24794 * A direct semantic implementation of the SCSI-3 Persistent Reservation
24795 * facility is supported through the shared multihost disk ioctls
24796 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24797 * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24798 *
24799 * Reservation Reclaim:
24800 * --------------------
24801 * To support the lost reservation reclaim operations this driver creates a
24802 * single thread to handle reinstating reservations on all devices that have
24803 * lost reservations sd_resv_reclaim_requests are logged for all devices that
24804 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24805 * and the reservation reclaim thread loops through the requests to regain the
24806 * lost reservations.
24807 */
24808
24809 /*
24810 * Function: sd_check_mhd()
24811 *
24812 * Description: This function sets up and submits a scsi watch request or
24813 * terminates an existing watch request. This routine is used in
24814 * support of reservation reclaim.
24815 *
24816 * Arguments: dev - the device 'dev_t' is used for context to discriminate
24817 * among multiple watches that share the callback function
24818 * interval - the number of microseconds specifying the watch
24819 * interval for issuing TEST UNIT READY commands. If
24820 * set to 0 the watch should be terminated. If the
24821 * interval is set to 0 and if the device is required
24822 * to hold reservation while disabling failfast, the
24823 * watch is restarted with an interval of
24824 * reinstate_resv_delay.
24825 *
24826 * Return Code: 0 - Successful submit/terminate of scsi watch request
24827 * ENXIO - Indicates an invalid device was specified
24828 * EAGAIN - Unable to submit the scsi watch request
24829 */
24830
24831 static int
24832 sd_check_mhd(dev_t dev, int interval)
24833 {
24834 struct sd_lun *un;
24835 opaque_t token;
24836
24837 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24838 return (ENXIO);
24839 }
24840
24841 /* is this a watch termination request? */
24842 if (interval == 0) {
24843 mutex_enter(SD_MUTEX(un));
24844 /* if there is an existing watch task then terminate it */
24845 if (un->un_mhd_token) {
24846 token = un->un_mhd_token;
24847 un->un_mhd_token = NULL;
24848 mutex_exit(SD_MUTEX(un));
24849 (void) scsi_watch_request_terminate(token,
24850 SCSI_WATCH_TERMINATE_ALL_WAIT);
24851 mutex_enter(SD_MUTEX(un));
24852 } else {
24853 mutex_exit(SD_MUTEX(un));
24854 /*
24855 * Note: If we return here we don't check for the
24856 * failfast case. This is the original legacy
24857 * implementation but perhaps we should be checking
24858 * the failfast case.
24859 */
24860 return (0);
24861 }
24862 /*
24863 * If the device is required to hold reservation while
24864 * disabling failfast, we need to restart the scsi_watch
24865 * routine with an interval of reinstate_resv_delay.
24866 */
24867 if (un->un_resvd_status & SD_RESERVE) {
24868 interval = sd_reinstate_resv_delay/1000;
24869 } else {
24870 /* no failfast so bail */
24871 mutex_exit(SD_MUTEX(un));
24872 return (0);
24873 }
24874 mutex_exit(SD_MUTEX(un));
24875 }
24876
24877 /*
24878 * adjust minimum time interval to 1 second,
24879 * and convert from msecs to usecs
24880 */
24881 if (interval > 0 && interval < 1000) {
24882 interval = 1000;
24883 }
24884 interval *= 1000;
24885
24886 /*
24887 * submit the request to the scsi_watch service
24888 */
24889 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24890 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24891 if (token == NULL) {
24892 return (EAGAIN);
24893 }
24894
24895 /*
24896 * save token for termination later on
24897 */
24898 mutex_enter(SD_MUTEX(un));
24899 un->un_mhd_token = token;
24900 mutex_exit(SD_MUTEX(un));
24901 return (0);
24902 }
24903
24904
24905 /*
24906 * Function: sd_mhd_watch_cb()
24907 *
24908 * Description: This function is the call back function used by the scsi watch
24909 * facility. The scsi watch facility sends the "Test Unit Ready"
24910 * and processes the status. If applicable (i.e. a "Unit Attention"
24911 * status and automatic "Request Sense" not used) the scsi watch
24912 * facility will send a "Request Sense" and retrieve the sense data
24913 * to be passed to this callback function. In either case the
24914 * automatic "Request Sense" or the facility submitting one, this
24915 * callback is passed the status and sense data.
24916 *
24917 * Arguments: arg - the device 'dev_t' is used for context to discriminate
24918 * among multiple watches that share this callback function
24919 * resultp - scsi watch facility result packet containing scsi
24920 * packet, status byte and sense data
24921 *
24922 * Return Code: 0 - continue the watch task
24923 * non-zero - terminate the watch task
24924 */
24925
24926 static int
24927 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24928 {
24929 struct sd_lun *un;
24930 struct scsi_status *statusp;
24931 uint8_t *sensep;
24932 struct scsi_pkt *pkt;
24933 uchar_t actual_sense_length;
24934 dev_t dev = (dev_t)arg;
24935
24936 ASSERT(resultp != NULL);
24937 statusp = resultp->statusp;
24938 sensep = (uint8_t *)resultp->sensep;
24939 pkt = resultp->pkt;
24940 actual_sense_length = resultp->actual_sense_length;
24941
24942 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24943 return (ENXIO);
24944 }
24945
24946 SD_TRACE(SD_LOG_IOCTL_MHD, un,
24947 "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24948 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24949
24950 /* Begin processing of the status and/or sense data */
24951 if (pkt->pkt_reason != CMD_CMPLT) {
24952 /* Handle the incomplete packet */
24953 sd_mhd_watch_incomplete(un, pkt);
24954 return (0);
24955 } else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24956 if (*((unsigned char *)statusp)
24957 == STATUS_RESERVATION_CONFLICT) {
24958 /*
24959 * Handle a reservation conflict by panicking if
24960 * configured for failfast or by logging the conflict
24961 * and updating the reservation status
24962 */
24963 mutex_enter(SD_MUTEX(un));
24964 if ((un->un_resvd_status & SD_FAILFAST) &&
24965 (sd_failfast_enable)) {
24966 sd_panic_for_res_conflict(un);
24967 /*NOTREACHED*/
24968 }
24969 SD_INFO(SD_LOG_IOCTL_MHD, un,
24970 "sd_mhd_watch_cb: Reservation Conflict\n");
24971 un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24972 mutex_exit(SD_MUTEX(un));
24973 }
24974 }
24975
24976 if (sensep != NULL) {
24977 if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24978 mutex_enter(SD_MUTEX(un));
24979 if ((scsi_sense_asc(sensep) ==
24980 SD_SCSI_RESET_SENSE_CODE) &&
24981 (un->un_resvd_status & SD_RESERVE)) {
24982 /*
24983 * The additional sense code indicates a power
24984 * on or bus device reset has occurred; update
24985 * the reservation status.
24986 */
24987 un->un_resvd_status |=
24988 (SD_LOST_RESERVE | SD_WANT_RESERVE);
24989 SD_INFO(SD_LOG_IOCTL_MHD, un,
24990 "sd_mhd_watch_cb: Lost Reservation\n");
24991 }
24992 } else {
24993 return (0);
24994 }
24995 } else {
24996 mutex_enter(SD_MUTEX(un));
24997 }
24998
24999 if ((un->un_resvd_status & SD_RESERVE) &&
25000 (un->un_resvd_status & SD_LOST_RESERVE)) {
25001 if (un->un_resvd_status & SD_WANT_RESERVE) {
25002 /*
25003 * A reset occurred in between the last probe and this
25004 * one so if a timeout is pending cancel it.
25005 */
25006 if (un->un_resvd_timeid) {
25007 timeout_id_t temp_id = un->un_resvd_timeid;
25008 un->un_resvd_timeid = NULL;
25009 mutex_exit(SD_MUTEX(un));
25010 (void) untimeout(temp_id);
25011 mutex_enter(SD_MUTEX(un));
25012 }
25013 un->un_resvd_status &= ~SD_WANT_RESERVE;
25014 }
25015 if (un->un_resvd_timeid == 0) {
25016 /* Schedule a timeout to handle the lost reservation */
25017 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25018 (void *)dev,
25019 drv_usectohz(sd_reinstate_resv_delay));
25020 }
25021 }
25022 mutex_exit(SD_MUTEX(un));
25023 return (0);
25024 }
25025
25026
25027 /*
25028 * Function: sd_mhd_watch_incomplete()
25029 *
25030 * Description: This function is used to find out why a scsi pkt sent by the
25031 * scsi watch facility was not completed. Under some scenarios this
25032 * routine will return. Otherwise it will send a bus reset to see
25033 * if the drive is still online.
25034 *
25035 * Arguments: un - driver soft state (unit) structure
25036 * pkt - incomplete scsi pkt
25037 */
25038
25039 static void
25040 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25041 {
25042 int be_chatty;
25043 int perr;
25044
25045 ASSERT(pkt != NULL);
25046 ASSERT(un != NULL);
25047 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT));
25048 perr = (pkt->pkt_statistics & STAT_PERR);
25049
25050 mutex_enter(SD_MUTEX(un));
25051 if (un->un_state == SD_STATE_DUMPING) {
25052 mutex_exit(SD_MUTEX(un));
25053 return;
25054 }
25055
25056 switch (pkt->pkt_reason) {
25057 case CMD_UNX_BUS_FREE:
25058 /*
25059 * If we had a parity error that caused the target to drop BSY*,
25060 * don't be chatty about it.
25061 */
25062 if (perr && be_chatty) {
25063 be_chatty = 0;
25064 }
25065 break;
25066 case CMD_TAG_REJECT:
25067 /*
25068 * The SCSI-2 spec states that a tag reject will be sent by the
25069 * target if tagged queuing is not supported. A tag reject may
25070 * also be sent during certain initialization periods or to
25071 * control internal resources. For the latter case the target
25072 * may also return Queue Full.
25073 *
25074 * If this driver receives a tag reject from a target that is
25075 * going through an init period or controlling internal
25076 * resources tagged queuing will be disabled. This is a less
25077 * than optimal behavior but the driver is unable to determine
25078 * the target state and assumes tagged queueing is not supported
25079 */
25080 pkt->pkt_flags = 0;
25081 un->un_tagflags = 0;
25082
25083 if (un->un_f_opt_queueing == TRUE) {
25084 un->un_throttle = min(un->un_throttle, 3);
25085 } else {
25086 un->un_throttle = 1;
25087 }
25088 mutex_exit(SD_MUTEX(un));
25089 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25090 mutex_enter(SD_MUTEX(un));
25091 break;
25092 case CMD_INCOMPLETE:
25093 /*
25094 * The transport stopped with an abnormal state, fallthrough and
25095 * reset the target and/or bus unless selection did not complete
25096 * (indicated by STATE_GOT_BUS) in which case we don't want to
25097 * go through a target/bus reset
25098 */
25099 if (pkt->pkt_state == STATE_GOT_BUS) {
25100 break;
25101 }
25102 /*FALLTHROUGH*/
25103
25104 case CMD_TIMEOUT:
25105 default:
25106 /*
25107 * The lun may still be running the command, so a lun reset
25108 * should be attempted. If the lun reset fails or cannot be
25109 * issued, than try a target reset. Lastly try a bus reset.
25110 */
25111 if ((pkt->pkt_statistics &
25112 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25113 int reset_retval = 0;
25114 mutex_exit(SD_MUTEX(un));
25115 if (un->un_f_allow_bus_device_reset == TRUE) {
25116 if (un->un_f_lun_reset_enabled == TRUE) {
25117 reset_retval =
25118 scsi_reset(SD_ADDRESS(un),
25119 RESET_LUN);
25120 }
25121 if (reset_retval == 0) {
25122 reset_retval =
25123 scsi_reset(SD_ADDRESS(un),
25124 RESET_TARGET);
25125 }
25126 }
25127 if (reset_retval == 0) {
25128 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25129 }
25130 mutex_enter(SD_MUTEX(un));
25131 }
25132 break;
25133 }
25134
25135 /* A device/bus reset has occurred; update the reservation status. */
25136 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25137 (STAT_BUS_RESET | STAT_DEV_RESET))) {
25138 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25139 un->un_resvd_status |=
25140 (SD_LOST_RESERVE | SD_WANT_RESERVE);
25141 SD_INFO(SD_LOG_IOCTL_MHD, un,
25142 "sd_mhd_watch_incomplete: Lost Reservation\n");
25143 }
25144 }
25145
25146 /*
25147 * The disk has been turned off; Update the device state.
25148 *
25149 * Note: Should we be offlining the disk here?
25150 */
25151 if (pkt->pkt_state == STATE_GOT_BUS) {
25152 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25153 "Disk not responding to selection\n");
25154 if (un->un_state != SD_STATE_OFFLINE) {
25155 New_state(un, SD_STATE_OFFLINE);
25156 }
25157 } else if (be_chatty) {
25158 /*
25159 * suppress messages if they are all the same pkt reason;
25160 * with TQ, many (up to 256) are returned with the same
25161 * pkt_reason
25162 */
25163 if (pkt->pkt_reason != un->un_last_pkt_reason) {
25164 SD_ERROR(SD_LOG_IOCTL_MHD, un,
25165 "sd_mhd_watch_incomplete: "
25166 "SCSI transport failed: reason '%s'\n",
25167 scsi_rname(pkt->pkt_reason));
25168 }
25169 }
25170 un->un_last_pkt_reason = pkt->pkt_reason;
25171 mutex_exit(SD_MUTEX(un));
25172 }
25173
25174
25175 /*
25176 * Function: sd_sname()
25177 *
25178 * Description: This is a simple little routine to return a string containing
25179 * a printable description of command status byte for use in
25180 * logging.
25181 *
25182 * Arguments: status - pointer to a status byte
25183 *
25184 * Return Code: char * - string containing status description.
25185 */
25186
25187 static char *
25188 sd_sname(uchar_t status)
25189 {
25190 switch (status & STATUS_MASK) {
25191 case STATUS_GOOD:
25192 return ("good status");
25193 case STATUS_CHECK:
25194 return ("check condition");
25195 case STATUS_MET:
25196 return ("condition met");
25197 case STATUS_BUSY:
25198 return ("busy");
25199 case STATUS_INTERMEDIATE:
25200 return ("intermediate");
25201 case STATUS_INTERMEDIATE_MET:
25202 return ("intermediate - condition met");
25203 case STATUS_RESERVATION_CONFLICT:
25204 return ("reservation_conflict");
25205 case STATUS_TERMINATED:
25206 return ("command terminated");
25207 case STATUS_QFULL:
25208 return ("queue full");
25209 default:
25210 return ("<unknown status>");
25211 }
25212 }
25213
25214
25215 /*
25216 * Function: sd_mhd_resvd_recover()
25217 *
25218 * Description: This function adds a reservation entry to the
25219 * sd_resv_reclaim_request list and signals the reservation
25220 * reclaim thread that there is work pending. If the reservation
25221 * reclaim thread has not been previously created this function
25222 * will kick it off.
25223 *
25224 * Arguments: arg - the device 'dev_t' is used for context to discriminate
25225 * among multiple watches that share this callback function
25226 *
25227 * Context: This routine is called by timeout() and is run in interrupt
25228 * context. It must not sleep or call other functions which may
25229 * sleep.
25230 */
25231
25232 static void
25233 sd_mhd_resvd_recover(void *arg)
25234 {
25235 dev_t dev = (dev_t)arg;
25236 struct sd_lun *un;
25237 struct sd_thr_request *sd_treq = NULL;
25238 struct sd_thr_request *sd_cur = NULL;
25239 struct sd_thr_request *sd_prev = NULL;
25240 int already_there = 0;
25241
25242 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25243 return;
25244 }
25245
25246 mutex_enter(SD_MUTEX(un));
25247 un->un_resvd_timeid = NULL;
25248 if (un->un_resvd_status & SD_WANT_RESERVE) {
25249 /*
25250 * There was a reset so don't issue the reserve, allow the
25251 * sd_mhd_watch_cb callback function to notice this and
25252 * reschedule the timeout for reservation.
25253 */
25254 mutex_exit(SD_MUTEX(un));
25255 return;
25256 }
25257 mutex_exit(SD_MUTEX(un));
25258
25259 /*
25260 * Add this device to the sd_resv_reclaim_request list and the
25261 * sd_resv_reclaim_thread should take care of the rest.
25262 *
25263 * Note: We can't sleep in this context so if the memory allocation
25264 * fails allow the sd_mhd_watch_cb callback function to notice this and
25265 * reschedule the timeout for reservation. (4378460)
25266 */
25267 sd_treq = (struct sd_thr_request *)
25268 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25269 if (sd_treq == NULL) {
25270 return;
25271 }
25272
25273 sd_treq->sd_thr_req_next = NULL;
25274 sd_treq->dev = dev;
25275 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25276 if (sd_tr.srq_thr_req_head == NULL) {
25277 sd_tr.srq_thr_req_head = sd_treq;
25278 } else {
25279 sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25280 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25281 if (sd_cur->dev == dev) {
25282 /*
25283 * already in Queue so don't log
25284 * another request for the device
25285 */
25286 already_there = 1;
25287 break;
25288 }
25289 sd_prev = sd_cur;
25290 }
25291 if (!already_there) {
25292 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25293 "logging request for %lx\n", dev);
25294 sd_prev->sd_thr_req_next = sd_treq;
25295 } else {
25296 kmem_free(sd_treq, sizeof (struct sd_thr_request));
25297 }
25298 }
25299
25300 /*
25301 * Create a kernel thread to do the reservation reclaim and free up this
25302 * thread. We cannot block this thread while we go away to do the
25303 * reservation reclaim
25304 */
25305 if (sd_tr.srq_resv_reclaim_thread == NULL)
25306 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25307 sd_resv_reclaim_thread, NULL,
25308 0, &p0, TS_RUN, v.v_maxsyspri - 2);
25309
25310 /* Tell the reservation reclaim thread that it has work to do */
25311 cv_signal(&sd_tr.srq_resv_reclaim_cv);
25312 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25313 }
25314
25315 /*
25316 * Function: sd_resv_reclaim_thread()
25317 *
25318 * Description: This function implements the reservation reclaim operations
25319 *
25320 * Arguments: arg - the device 'dev_t' is used for context to discriminate
25321 * among multiple watches that share this callback function
25322 */
25323
25324 static void
25325 sd_resv_reclaim_thread()
25326 {
25327 struct sd_lun *un;
25328 struct sd_thr_request *sd_mhreq;
25329
25330 /* Wait for work */
25331 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25332 if (sd_tr.srq_thr_req_head == NULL) {
25333 cv_wait(&sd_tr.srq_resv_reclaim_cv,
25334 &sd_tr.srq_resv_reclaim_mutex);
25335 }
25336
25337 /* Loop while we have work */
25338 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25339 un = ddi_get_soft_state(sd_state,
25340 SDUNIT(sd_tr.srq_thr_cur_req->dev));
25341 if (un == NULL) {
25342 /*
25343 * softstate structure is NULL so just
25344 * dequeue the request and continue
25345 */
25346 sd_tr.srq_thr_req_head =
25347 sd_tr.srq_thr_cur_req->sd_thr_req_next;
25348 kmem_free(sd_tr.srq_thr_cur_req,
25349 sizeof (struct sd_thr_request));
25350 continue;
25351 }
25352
25353 /* dequeue the request */
25354 sd_mhreq = sd_tr.srq_thr_cur_req;
25355 sd_tr.srq_thr_req_head =
25356 sd_tr.srq_thr_cur_req->sd_thr_req_next;
25357 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25358
25359 /*
25360 * Reclaim reservation only if SD_RESERVE is still set. There
25361 * may have been a call to MHIOCRELEASE before we got here.
25362 */
25363 mutex_enter(SD_MUTEX(un));
25364 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25365 /*
25366 * Note: The SD_LOST_RESERVE flag is cleared before
25367 * reclaiming the reservation. If this is done after the
25368 * call to sd_reserve_release a reservation loss in the
25369 * window between pkt completion of reserve cmd and
25370 * mutex_enter below may not be recognized
25371 */
25372 un->un_resvd_status &= ~SD_LOST_RESERVE;
25373 mutex_exit(SD_MUTEX(un));
25374
25375 if (sd_reserve_release(sd_mhreq->dev,
25376 SD_RESERVE) == 0) {
25377 mutex_enter(SD_MUTEX(un));
25378 un->un_resvd_status |= SD_RESERVE;
25379 mutex_exit(SD_MUTEX(un));
25380 SD_INFO(SD_LOG_IOCTL_MHD, un,
25381 "sd_resv_reclaim_thread: "
25382 "Reservation Recovered\n");
25383 } else {
25384 mutex_enter(SD_MUTEX(un));
25385 un->un_resvd_status |= SD_LOST_RESERVE;
25386 mutex_exit(SD_MUTEX(un));
25387 SD_INFO(SD_LOG_IOCTL_MHD, un,
25388 "sd_resv_reclaim_thread: Failed "
25389 "Reservation Recovery\n");
25390 }
25391 } else {
25392 mutex_exit(SD_MUTEX(un));
25393 }
25394 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25395 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25396 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25397 sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25398 /*
25399 * wakeup the destroy thread if anyone is waiting on
25400 * us to complete.
25401 */
25402 cv_signal(&sd_tr.srq_inprocess_cv);
25403 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25404 "sd_resv_reclaim_thread: cv_signalling current request \n");
25405 }
25406
25407 /*
25408 * cleanup the sd_tr structure now that this thread will not exist
25409 */
25410 ASSERT(sd_tr.srq_thr_req_head == NULL);
25411 ASSERT(sd_tr.srq_thr_cur_req == NULL);
25412 sd_tr.srq_resv_reclaim_thread = NULL;
25413 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25414 thread_exit();
25415 }
25416
25417
25418 /*
25419 * Function: sd_rmv_resv_reclaim_req()
25420 *
25421 * Description: This function removes any pending reservation reclaim requests
25422 * for the specified device.
25423 *
25424 * Arguments: dev - the device 'dev_t'
25425 */
25426
25427 static void
25428 sd_rmv_resv_reclaim_req(dev_t dev)
25429 {
25430 struct sd_thr_request *sd_mhreq;
25431 struct sd_thr_request *sd_prev;
25432
25433 /* Remove a reservation reclaim request from the list */
25434 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25435 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25436 /*
25437 * We are attempting to reinstate reservation for
25438 * this device. We wait for sd_reserve_release()
25439 * to return before we return.
25440 */
25441 cv_wait(&sd_tr.srq_inprocess_cv,
25442 &sd_tr.srq_resv_reclaim_mutex);
25443 } else {
25444 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25445 if (sd_mhreq && sd_mhreq->dev == dev) {
25446 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25447 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25448 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25449 return;
25450 }
25451 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25452 if (sd_mhreq && sd_mhreq->dev == dev) {
25453 break;
25454 }
25455 sd_prev = sd_mhreq;
25456 }
25457 if (sd_mhreq != NULL) {
25458 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25459 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25460 }
25461 }
25462 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25463 }
25464
25465
25466 /*
25467 * Function: sd_mhd_reset_notify_cb()
25468 *
25469 * Description: This is a call back function for scsi_reset_notify. This
25470 * function updates the softstate reserved status and logs the
25471 * reset. The driver scsi watch facility callback function
25472 * (sd_mhd_watch_cb) and reservation reclaim thread functionality
25473 * will reclaim the reservation.
25474 *
25475 * Arguments: arg - driver soft state (unit) structure
25476 */
25477
25478 static void
25479 sd_mhd_reset_notify_cb(caddr_t arg)
25480 {
25481 struct sd_lun *un = (struct sd_lun *)arg;
25482
25483 mutex_enter(SD_MUTEX(un));
25484 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25485 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25486 SD_INFO(SD_LOG_IOCTL_MHD, un,
25487 "sd_mhd_reset_notify_cb: Lost Reservation\n");
25488 }
25489 mutex_exit(SD_MUTEX(un));
25490 }
25491
25492
25493 /*
25494 * Function: sd_take_ownership()
25495 *
25496 * Description: This routine implements an algorithm to achieve a stable
25497 * reservation on disks which don't implement priority reserve,
25498 * and makes sure that other host lose re-reservation attempts.
25499 * This algorithm contains of a loop that keeps issuing the RESERVE
25500 * for some period of time (min_ownership_delay, default 6 seconds)
25501 * During that loop, it looks to see if there has been a bus device
25502 * reset or bus reset (both of which cause an existing reservation
25503 * to be lost). If the reservation is lost issue RESERVE until a
25504 * period of min_ownership_delay with no resets has gone by, or
25505 * until max_ownership_delay has expired. This loop ensures that
25506 * the host really did manage to reserve the device, in spite of
25507 * resets. The looping for min_ownership_delay (default six
25508 * seconds) is important to early generation clustering products,
25509 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25510 * MHIOCENFAILFAST periodic timer of two seconds. By having
25511 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25512 * MHIOCENFAILFAST poll every two seconds, the idea is that by the
25513 * time the MHIOCTKOWN ioctl returns, the other host (if any) will
25514 * have already noticed, via the MHIOCENFAILFAST polling, that it
25515 * no longer "owns" the disk and will have panicked itself. Thus,
25516 * the host issuing the MHIOCTKOWN is assured (with timing
25517 * dependencies) that by the time it actually starts to use the
25518 * disk for real work, the old owner is no longer accessing it.
25519 *
25520 * min_ownership_delay is the minimum amount of time for which the
25521 * disk must be reserved continuously devoid of resets before the
25522 * MHIOCTKOWN ioctl will return success.
25523 *
25524 * max_ownership_delay indicates the amount of time by which the
25525 * take ownership should succeed or timeout with an error.
25526 *
25527 * Arguments: dev - the device 'dev_t'
25528 * *p - struct containing timing info.
25529 *
25530 * Return Code: 0 for success or error code
25531 */
25532
25533 static int
25534 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25535 {
25536 struct sd_lun *un;
25537 int rval;
25538 int err;
25539 int reservation_count = 0;
25540 int min_ownership_delay = 6000000; /* in usec */
25541 int max_ownership_delay = 30000000; /* in usec */
25542 clock_t start_time; /* starting time of this algorithm */
25543 clock_t end_time; /* time limit for giving up */
25544 clock_t ownership_time; /* time limit for stable ownership */
25545 clock_t current_time;
25546 clock_t previous_current_time;
25547
25548 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25549 return (ENXIO);
25550 }
25551
25552 /*
25553 * Attempt a device reservation. A priority reservation is requested.
25554 */
25555 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25556 != SD_SUCCESS) {
25557 SD_ERROR(SD_LOG_IOCTL_MHD, un,
25558 "sd_take_ownership: return(1)=%d\n", rval);
25559 return (rval);
25560 }
25561
25562 /* Update the softstate reserved status to indicate the reservation */
25563 mutex_enter(SD_MUTEX(un));
25564 un->un_resvd_status |= SD_RESERVE;
25565 un->un_resvd_status &=
25566 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25567 mutex_exit(SD_MUTEX(un));
25568
25569 if (p != NULL) {
25570 if (p->min_ownership_delay != 0) {
25571 min_ownership_delay = p->min_ownership_delay * 1000;
25572 }
25573 if (p->max_ownership_delay != 0) {
25574 max_ownership_delay = p->max_ownership_delay * 1000;
25575 }
25576 }
25577 SD_INFO(SD_LOG_IOCTL_MHD, un,
25578 "sd_take_ownership: min, max delays: %d, %d\n",
25579 min_ownership_delay, max_ownership_delay);
25580
25581 start_time = ddi_get_lbolt();
25582 current_time = start_time;
25583 ownership_time = current_time + drv_usectohz(min_ownership_delay);
25584 end_time = start_time + drv_usectohz(max_ownership_delay);
25585
25586 while (current_time - end_time < 0) {
25587 delay(drv_usectohz(500000));
25588
25589 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25590 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25591 mutex_enter(SD_MUTEX(un));
25592 rval = (un->un_resvd_status &
25593 SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25594 mutex_exit(SD_MUTEX(un));
25595 break;
25596 }
25597 }
25598 previous_current_time = current_time;
25599 current_time = ddi_get_lbolt();
25600 mutex_enter(SD_MUTEX(un));
25601 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25602 ownership_time = ddi_get_lbolt() +
25603 drv_usectohz(min_ownership_delay);
25604 reservation_count = 0;
25605 } else {
25606 reservation_count++;
25607 }
25608 un->un_resvd_status |= SD_RESERVE;
25609 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25610 mutex_exit(SD_MUTEX(un));
25611
25612 SD_INFO(SD_LOG_IOCTL_MHD, un,
25613 "sd_take_ownership: ticks for loop iteration=%ld, "
25614 "reservation=%s\n", (current_time - previous_current_time),
25615 reservation_count ? "ok" : "reclaimed");
25616
25617 if (current_time - ownership_time >= 0 &&
25618 reservation_count >= 4) {
25619 rval = 0; /* Achieved a stable ownership */
25620 break;
25621 }
25622 if (current_time - end_time >= 0) {
25623 rval = EACCES; /* No ownership in max possible time */
25624 break;
25625 }
25626 }
25627 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25628 "sd_take_ownership: return(2)=%d\n", rval);
25629 return (rval);
25630 }
25631
25632
25633 /*
25634 * Function: sd_reserve_release()
25635 *
25636 * Description: This function builds and sends scsi RESERVE, RELEASE, and
25637 * PRIORITY RESERVE commands based on a user specified command type
25638 *
25639 * Arguments: dev - the device 'dev_t'
25640 * cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25641 * SD_RESERVE, SD_RELEASE
25642 *
25643 * Return Code: 0 or Error Code
25644 */
25645
25646 static int
25647 sd_reserve_release(dev_t dev, int cmd)
25648 {
25649 struct uscsi_cmd *com = NULL;
25650 struct sd_lun *un = NULL;
25651 char cdb[CDB_GROUP0];
25652 int rval;
25653
25654 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25655 (cmd == SD_PRIORITY_RESERVE));
25656
25657 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25658 return (ENXIO);
25659 }
25660
25661 /* instantiate and initialize the command and cdb */
25662 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25663 bzero(cdb, CDB_GROUP0);
25664 com->uscsi_flags = USCSI_SILENT;
25665 com->uscsi_timeout = un->un_reserve_release_time;
25666 com->uscsi_cdblen = CDB_GROUP0;
25667 com->uscsi_cdb = cdb;
25668 if (cmd == SD_RELEASE) {
25669 cdb[0] = SCMD_RELEASE;
25670 } else {
25671 cdb[0] = SCMD_RESERVE;
25672 }
25673
25674 /* Send the command. */
25675 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25676 SD_PATH_STANDARD);
25677
25678 /*
25679 * "break" a reservation that is held by another host, by issuing a
25680 * reset if priority reserve is desired, and we could not get the
25681 * device.
25682 */
25683 if ((cmd == SD_PRIORITY_RESERVE) &&
25684 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25685 /*
25686 * First try to reset the LUN. If we cannot, then try a target
25687 * reset, followed by a bus reset if the target reset fails.
25688 */
25689 int reset_retval = 0;
25690 if (un->un_f_lun_reset_enabled == TRUE) {
25691 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25692 }
25693 if (reset_retval == 0) {
25694 /* The LUN reset either failed or was not issued */
25695 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25696 }
25697 if ((reset_retval == 0) &&
25698 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25699 rval = EIO;
25700 kmem_free(com, sizeof (*com));
25701 return (rval);
25702 }
25703
25704 bzero(com, sizeof (struct uscsi_cmd));
25705 com->uscsi_flags = USCSI_SILENT;
25706 com->uscsi_cdb = cdb;
25707 com->uscsi_cdblen = CDB_GROUP0;
25708 com->uscsi_timeout = 5;
25709
25710 /*
25711 * Reissue the last reserve command, this time without request
25712 * sense. Assume that it is just a regular reserve command.
25713 */
25714 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25715 SD_PATH_STANDARD);
25716 }
25717
25718 /* Return an error if still getting a reservation conflict. */
25719 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25720 rval = EACCES;
25721 }
25722
25723 kmem_free(com, sizeof (*com));
25724 return (rval);
25725 }
25726
25727
25728 #define SD_NDUMP_RETRIES 12
25729 /*
25730 * System Crash Dump routine
25731 */
25732
25733 static int
25734 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25735 {
25736 int instance;
25737 int partition;
25738 int i;
25739 int err;
25740 struct sd_lun *un;
25741 struct scsi_pkt *wr_pktp;
25742 struct buf *wr_bp;
25743 struct buf wr_buf;
25744 daddr_t tgt_byte_offset; /* rmw - byte offset for target */
25745 daddr_t tgt_blkno; /* rmw - blkno for target */
25746 size_t tgt_byte_count; /* rmw - # of bytes to xfer */
25747 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */
25748 size_t io_start_offset;
25749 int doing_rmw = FALSE;
25750 int rval;
25751 ssize_t dma_resid;
25752 daddr_t oblkno;
25753 diskaddr_t nblks = 0;
25754 diskaddr_t start_block;
25755
25756 instance = SDUNIT(dev);
25757 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25758 !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25759 return (ENXIO);
25760 }
25761
25762 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25763
25764 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25765
25766 partition = SDPART(dev);
25767 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25768
25769 if (!(NOT_DEVBSIZE(un))) {
25770 int secmask = 0;
25771 int blknomask = 0;
25772
25773 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25774 secmask = un->un_tgt_blocksize - 1;
25775
25776 if (blkno & blknomask) {
25777 SD_TRACE(SD_LOG_DUMP, un,
25778 "sddump: dump start block not modulo %d\n",
25779 un->un_tgt_blocksize);
25780 return (EINVAL);
25781 }
25782
25783 if ((nblk * DEV_BSIZE) & secmask) {
25784 SD_TRACE(SD_LOG_DUMP, un,
25785 "sddump: dump length not modulo %d\n",
25786 un->un_tgt_blocksize);
25787 return (EINVAL);
25788 }
25789
25790 }
25791
25792 /* Validate blocks to dump at against partition size. */
25793
25794 (void) cmlb_partinfo(un->un_cmlbhandle, partition,
25795 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25796
25797 if (NOT_DEVBSIZE(un)) {
25798 if ((blkno + nblk) > nblks) {
25799 SD_TRACE(SD_LOG_DUMP, un,
25800 "sddump: dump range larger than partition: "
25801 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25802 blkno, nblk, nblks);
25803 return (EINVAL);
25804 }
25805 } else {
25806 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25807 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25808 SD_TRACE(SD_LOG_DUMP, un,
25809 "sddump: dump range larger than partition: "
25810 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25811 blkno, nblk, nblks);
25812 return (EINVAL);
25813 }
25814 }
25815
25816 mutex_enter(&un->un_pm_mutex);
25817 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25818 struct scsi_pkt *start_pktp;
25819
25820 mutex_exit(&un->un_pm_mutex);
25821
25822 /*
25823 * use pm framework to power on HBA 1st
25824 */
25825 (void) pm_raise_power(SD_DEVINFO(un), 0,
25826 SD_PM_STATE_ACTIVE(un));
25827
25828 /*
25829 * Dump no long uses sdpower to power on a device, it's
25830 * in-line here so it can be done in polled mode.
25831 */
25832
25833 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25834
25835 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25836 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25837
25838 if (start_pktp == NULL) {
25839 /* We were not given a SCSI packet, fail. */
25840 return (EIO);
25841 }
25842 bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25843 start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25844 start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25845 start_pktp->pkt_flags = FLAG_NOINTR;
25846
25847 mutex_enter(SD_MUTEX(un));
25848 SD_FILL_SCSI1_LUN(un, start_pktp);
25849 mutex_exit(SD_MUTEX(un));
25850 /*
25851 * Scsi_poll returns 0 (success) if the command completes and
25852 * the status block is STATUS_GOOD.
25853 */
25854 if (sd_scsi_poll(un, start_pktp) != 0) {
25855 scsi_destroy_pkt(start_pktp);
25856 return (EIO);
25857 }
25858 scsi_destroy_pkt(start_pktp);
25859 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25860 SD_PM_STATE_CHANGE);
25861 } else {
25862 mutex_exit(&un->un_pm_mutex);
25863 }
25864
25865 mutex_enter(SD_MUTEX(un));
25866 un->un_throttle = 0;
25867
25868 /*
25869 * The first time through, reset the specific target device.
25870 * However, when cpr calls sddump we know that sd is in a
25871 * a good state so no bus reset is required.
25872 * Clear sense data via Request Sense cmd.
25873 * In sddump we don't care about allow_bus_device_reset anymore
25874 */
25875
25876 if ((un->un_state != SD_STATE_SUSPENDED) &&
25877 (un->un_state != SD_STATE_DUMPING)) {
25878
25879 New_state(un, SD_STATE_DUMPING);
25880
25881 if (un->un_f_is_fibre == FALSE) {
25882 mutex_exit(SD_MUTEX(un));
25883 /*
25884 * Attempt a bus reset for parallel scsi.
25885 *
25886 * Note: A bus reset is required because on some host
25887 * systems (i.e. E420R) a bus device reset is
25888 * insufficient to reset the state of the target.
25889 *
25890 * Note: Don't issue the reset for fibre-channel,
25891 * because this tends to hang the bus (loop) for
25892 * too long while everyone is logging out and in
25893 * and the deadman timer for dumping will fire
25894 * before the dump is complete.
25895 */
25896 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25897 mutex_enter(SD_MUTEX(un));
25898 Restore_state(un);
25899 mutex_exit(SD_MUTEX(un));
25900 return (EIO);
25901 }
25902
25903 /* Delay to give the device some recovery time. */
25904 drv_usecwait(10000);
25905
25906 if (sd_send_polled_RQS(un) == SD_FAILURE) {
25907 SD_INFO(SD_LOG_DUMP, un,
25908 "sddump: sd_send_polled_RQS failed\n");
25909 }
25910 mutex_enter(SD_MUTEX(un));
25911 }
25912 }
25913
25914 /*
25915 * Convert the partition-relative block number to a
25916 * disk physical block number.
25917 */
25918 if (NOT_DEVBSIZE(un)) {
25919 blkno += start_block;
25920 } else {
25921 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25922 blkno += start_block;
25923 }
25924
25925 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25926
25927
25928 /*
25929 * Check if the device has a non-512 block size.
25930 */
25931 wr_bp = NULL;
25932 if (NOT_DEVBSIZE(un)) {
25933 tgt_byte_offset = blkno * un->un_sys_blocksize;
25934 tgt_byte_count = nblk * un->un_sys_blocksize;
25935 if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25936 (tgt_byte_count % un->un_tgt_blocksize)) {
25937 doing_rmw = TRUE;
25938 /*
25939 * Calculate the block number and number of block
25940 * in terms of the media block size.
25941 */
25942 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25943 tgt_nblk =
25944 ((tgt_byte_offset + tgt_byte_count +
25945 (un->un_tgt_blocksize - 1)) /
25946 un->un_tgt_blocksize) - tgt_blkno;
25947
25948 /*
25949 * Invoke the routine which is going to do read part
25950 * of read-modify-write.
25951 * Note that this routine returns a pointer to
25952 * a valid bp in wr_bp.
25953 */
25954 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25955 &wr_bp);
25956 if (err) {
25957 mutex_exit(SD_MUTEX(un));
25958 return (err);
25959 }
25960 /*
25961 * Offset is being calculated as -
25962 * (original block # * system block size) -
25963 * (new block # * target block size)
25964 */
25965 io_start_offset =
25966 ((uint64_t)(blkno * un->un_sys_blocksize)) -
25967 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25968
25969 ASSERT((io_start_offset >= 0) &&
25970 (io_start_offset < un->un_tgt_blocksize));
25971 /*
25972 * Do the modify portion of read modify write.
25973 */
25974 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25975 (size_t)nblk * un->un_sys_blocksize);
25976 } else {
25977 doing_rmw = FALSE;
25978 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25979 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25980 }
25981
25982 /* Convert blkno and nblk to target blocks */
25983 blkno = tgt_blkno;
25984 nblk = tgt_nblk;
25985 } else {
25986 wr_bp = &wr_buf;
25987 bzero(wr_bp, sizeof (struct buf));
25988 wr_bp->b_flags = B_BUSY;
25989 wr_bp->b_un.b_addr = addr;
25990 wr_bp->b_bcount = nblk << DEV_BSHIFT;
25991 wr_bp->b_resid = 0;
25992 }
25993
25994 mutex_exit(SD_MUTEX(un));
25995
25996 /*
25997 * Obtain a SCSI packet for the write command.
25998 * It should be safe to call the allocator here without
25999 * worrying about being locked for DVMA mapping because
26000 * the address we're passed is already a DVMA mapping
26001 *
26002 * We are also not going to worry about semaphore ownership
26003 * in the dump buffer. Dumping is single threaded at present.
26004 */
26005
26006 wr_pktp = NULL;
26007
26008 dma_resid = wr_bp->b_bcount;
26009 oblkno = blkno;
26010
26011 if (!(NOT_DEVBSIZE(un))) {
26012 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
26013 }
26014
26015 while (dma_resid != 0) {
26016
26017 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26018 wr_bp->b_flags &= ~B_ERROR;
26019
26020 if (un->un_partial_dma_supported == 1) {
26021 blkno = oblkno +
26022 ((wr_bp->b_bcount - dma_resid) /
26023 un->un_tgt_blocksize);
26024 nblk = dma_resid / un->un_tgt_blocksize;
26025
26026 if (wr_pktp) {
26027 /*
26028 * Partial DMA transfers after initial transfer
26029 */
26030 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26031 blkno, nblk);
26032 } else {
26033 /* Initial transfer */
26034 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26035 un->un_pkt_flags, NULL_FUNC, NULL,
26036 blkno, nblk);
26037 }
26038 } else {
26039 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26040 0, NULL_FUNC, NULL, blkno, nblk);
26041 }
26042
26043 if (rval == 0) {
26044 /* We were given a SCSI packet, continue. */
26045 break;
26046 }
26047
26048 if (i == 0) {
26049 if (wr_bp->b_flags & B_ERROR) {
26050 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26051 "no resources for dumping; "
26052 "error code: 0x%x, retrying",
26053 geterror(wr_bp));
26054 } else {
26055 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26056 "no resources for dumping; retrying");
26057 }
26058 } else if (i != (SD_NDUMP_RETRIES - 1)) {
26059 if (wr_bp->b_flags & B_ERROR) {
26060 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26061 "no resources for dumping; error code: "
26062 "0x%x, retrying\n", geterror(wr_bp));
26063 }
26064 } else {
26065 if (wr_bp->b_flags & B_ERROR) {
26066 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26067 "no resources for dumping; "
26068 "error code: 0x%x, retries failed, "
26069 "giving up.\n", geterror(wr_bp));
26070 } else {
26071 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26072 "no resources for dumping; "
26073 "retries failed, giving up.\n");
26074 }
26075 mutex_enter(SD_MUTEX(un));
26076 Restore_state(un);
26077 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26078 mutex_exit(SD_MUTEX(un));
26079 scsi_free_consistent_buf(wr_bp);
26080 } else {
26081 mutex_exit(SD_MUTEX(un));
26082 }
26083 return (EIO);
26084 }
26085 drv_usecwait(10000);
26086 }
26087
26088 if (un->un_partial_dma_supported == 1) {
26089 /*
26090 * save the resid from PARTIAL_DMA
26091 */
26092 dma_resid = wr_pktp->pkt_resid;
26093 if (dma_resid != 0)
26094 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26095 wr_pktp->pkt_resid = 0;
26096 } else {
26097 dma_resid = 0;
26098 }
26099
26100 /* SunBug 1222170 */
26101 wr_pktp->pkt_flags = FLAG_NOINTR;
26102
26103 err = EIO;
26104 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26105
26106 /*
26107 * Scsi_poll returns 0 (success) if the command completes and
26108 * the status block is STATUS_GOOD. We should only check
26109 * errors if this condition is not true. Even then we should
26110 * send our own request sense packet only if we have a check
26111 * condition and auto request sense has not been performed by
26112 * the hba.
26113 */
26114 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26115
26116 if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26117 (wr_pktp->pkt_resid == 0)) {
26118 err = SD_SUCCESS;
26119 break;
26120 }
26121
26122 /*
26123 * Check CMD_DEV_GONE 1st, give up if device is gone.
26124 */
26125 if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26126 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26127 "Error while dumping state...Device is gone\n");
26128 break;
26129 }
26130
26131 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26132 SD_INFO(SD_LOG_DUMP, un,
26133 "sddump: write failed with CHECK, try # %d\n", i);
26134 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26135 (void) sd_send_polled_RQS(un);
26136 }
26137
26138 continue;
26139 }
26140
26141 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26142 int reset_retval = 0;
26143
26144 SD_INFO(SD_LOG_DUMP, un,
26145 "sddump: write failed with BUSY, try # %d\n", i);
26146
26147 if (un->un_f_lun_reset_enabled == TRUE) {
26148 reset_retval = scsi_reset(SD_ADDRESS(un),
26149 RESET_LUN);
26150 }
26151 if (reset_retval == 0) {
26152 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26153 }
26154 (void) sd_send_polled_RQS(un);
26155
26156 } else {
26157 SD_INFO(SD_LOG_DUMP, un,
26158 "sddump: write failed with 0x%x, try # %d\n",
26159 SD_GET_PKT_STATUS(wr_pktp), i);
26160 mutex_enter(SD_MUTEX(un));
26161 sd_reset_target(un, wr_pktp);
26162 mutex_exit(SD_MUTEX(un));
26163 }
26164
26165 /*
26166 * If we are not getting anywhere with lun/target resets,
26167 * let's reset the bus.
26168 */
26169 if (i == SD_NDUMP_RETRIES/2) {
26170 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26171 (void) sd_send_polled_RQS(un);
26172 }
26173 }
26174 }
26175
26176 scsi_destroy_pkt(wr_pktp);
26177 mutex_enter(SD_MUTEX(un));
26178 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26179 mutex_exit(SD_MUTEX(un));
26180 scsi_free_consistent_buf(wr_bp);
26181 } else {
26182 mutex_exit(SD_MUTEX(un));
26183 }
26184 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26185 return (err);
26186 }
26187
26188 /*
26189 * Function: sd_scsi_poll()
26190 *
26191 * Description: This is a wrapper for the scsi_poll call.
26192 *
26193 * Arguments: sd_lun - The unit structure
26194 * scsi_pkt - The scsi packet being sent to the device.
26195 *
26196 * Return Code: 0 - Command completed successfully with good status
26197 * -1 - Command failed. This could indicate a check condition
26198 * or other status value requiring recovery action.
26199 *
26200 * NOTE: This code is only called off sddump().
26201 */
26202
26203 static int
26204 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26205 {
26206 int status;
26207
26208 ASSERT(un != NULL);
26209 ASSERT(!mutex_owned(SD_MUTEX(un)));
26210 ASSERT(pktp != NULL);
26211
26212 status = SD_SUCCESS;
26213
26214 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26215 pktp->pkt_flags |= un->un_tagflags;
26216 pktp->pkt_flags &= ~FLAG_NODISCON;
26217 }
26218
26219 status = sd_ddi_scsi_poll(pktp);
26220 /*
26221 * Scsi_poll returns 0 (success) if the command completes and the
26222 * status block is STATUS_GOOD. We should only check errors if this
26223 * condition is not true. Even then we should send our own request
26224 * sense packet only if we have a check condition and auto
26225 * request sense has not been performed by the hba.
26226 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26227 */
26228 if ((status != SD_SUCCESS) &&
26229 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26230 (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26231 (pktp->pkt_reason != CMD_DEV_GONE))
26232 (void) sd_send_polled_RQS(un);
26233
26234 return (status);
26235 }
26236
26237 /*
26238 * Function: sd_send_polled_RQS()
26239 *
26240 * Description: This sends the request sense command to a device.
26241 *
26242 * Arguments: sd_lun - The unit structure
26243 *
26244 * Return Code: 0 - Command completed successfully with good status
26245 * -1 - Command failed.
26246 *
26247 */
26248
26249 static int
26250 sd_send_polled_RQS(struct sd_lun *un)
26251 {
26252 int ret_val;
26253 struct scsi_pkt *rqs_pktp;
26254 struct buf *rqs_bp;
26255
26256 ASSERT(un != NULL);
26257 ASSERT(!mutex_owned(SD_MUTEX(un)));
26258
26259 ret_val = SD_SUCCESS;
26260
26261 rqs_pktp = un->un_rqs_pktp;
26262 rqs_bp = un->un_rqs_bp;
26263
26264 mutex_enter(SD_MUTEX(un));
26265
26266 if (un->un_sense_isbusy) {
26267 ret_val = SD_FAILURE;
26268 mutex_exit(SD_MUTEX(un));
26269 return (ret_val);
26270 }
26271
26272 /*
26273 * If the request sense buffer (and packet) is not in use,
26274 * let's set the un_sense_isbusy and send our packet
26275 */
26276 un->un_sense_isbusy = 1;
26277 rqs_pktp->pkt_resid = 0;
26278 rqs_pktp->pkt_reason = 0;
26279 rqs_pktp->pkt_flags |= FLAG_NOINTR;
26280 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26281
26282 mutex_exit(SD_MUTEX(un));
26283
26284 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26285 " 0x%p\n", rqs_bp->b_un.b_addr);
26286
26287 /*
26288 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26289 * axle - it has a call into us!
26290 */
26291 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26292 SD_INFO(SD_LOG_COMMON, un,
26293 "sd_send_polled_RQS: RQS failed\n");
26294 }
26295
26296 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26297 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26298
26299 mutex_enter(SD_MUTEX(un));
26300 un->un_sense_isbusy = 0;
26301 mutex_exit(SD_MUTEX(un));
26302
26303 return (ret_val);
26304 }
26305
26306 /*
26307 * Defines needed for localized version of the scsi_poll routine.
26308 */
26309 #define CSEC 10000 /* usecs */
26310 #define SEC_TO_CSEC (1000000/CSEC)
26311
26312 /*
26313 * Function: sd_ddi_scsi_poll()
26314 *
26315 * Description: Localized version of the scsi_poll routine. The purpose is to
26316 * send a scsi_pkt to a device as a polled command. This version
26317 * is to ensure more robust handling of transport errors.
26318 * Specifically this routine cures not ready, coming ready
26319 * transition for power up and reset of sonoma's. This can take
26320 * up to 45 seconds for power-on and 20 seconds for reset of a
26321 * sonoma lun.
26322 *
26323 * Arguments: scsi_pkt - The scsi_pkt being sent to a device
26324 *
26325 * Return Code: 0 - Command completed successfully with good status
26326 * -1 - Command failed.
26327 *
26328 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26329 * be fixed (removing this code), we need to determine how to handle the
26330 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26331 *
26332 * NOTE: This code is only called off sddump().
26333 */
26334 static int
26335 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26336 {
26337 int rval = -1;
26338 int savef;
26339 long savet;
26340 void (*savec)();
26341 int timeout;
26342 int busy_count;
26343 int poll_delay;
26344 int rc;
26345 uint8_t *sensep;
26346 struct scsi_arq_status *arqstat;
26347 extern int do_polled_io;
26348
26349 ASSERT(pkt->pkt_scbp);
26350
26351 /*
26352 * save old flags..
26353 */
26354 savef = pkt->pkt_flags;
26355 savec = pkt->pkt_comp;
26356 savet = pkt->pkt_time;
26357
26358 pkt->pkt_flags |= FLAG_NOINTR;
26359
26360 /*
26361 * XXX there is nothing in the SCSA spec that states that we should not
26362 * do a callback for polled cmds; however, removing this will break sd
26363 * and probably other target drivers
26364 */
26365 pkt->pkt_comp = NULL;
26366
26367 /*
26368 * we don't like a polled command without timeout.
26369 * 60 seconds seems long enough.
26370 */
26371 if (pkt->pkt_time == 0)
26372 pkt->pkt_time = SCSI_POLL_TIMEOUT;
26373
26374 /*
26375 * Send polled cmd.
26376 *
26377 * We do some error recovery for various errors. Tran_busy,
26378 * queue full, and non-dispatched commands are retried every 10 msec.
26379 * as they are typically transient failures. Busy status and Not
26380 * Ready are retried every second as this status takes a while to
26381 * change.
26382 */
26383 timeout = pkt->pkt_time * SEC_TO_CSEC;
26384
26385 for (busy_count = 0; busy_count < timeout; busy_count++) {
26386 /*
26387 * Initialize pkt status variables.
26388 */
26389 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26390
26391 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26392 if (rc != TRAN_BUSY) {
26393 /* Transport failed - give up. */
26394 break;
26395 } else {
26396 /* Transport busy - try again. */
26397 poll_delay = 1 * CSEC; /* 10 msec. */
26398 }
26399 } else {
26400 /*
26401 * Transport accepted - check pkt status.
26402 */
26403 rc = (*pkt->pkt_scbp) & STATUS_MASK;
26404 if ((pkt->pkt_reason == CMD_CMPLT) &&
26405 (rc == STATUS_CHECK) &&
26406 (pkt->pkt_state & STATE_ARQ_DONE)) {
26407 arqstat =
26408 (struct scsi_arq_status *)(pkt->pkt_scbp);
26409 sensep = (uint8_t *)&arqstat->sts_sensedata;
26410 } else {
26411 sensep = NULL;
26412 }
26413
26414 if ((pkt->pkt_reason == CMD_CMPLT) &&
26415 (rc == STATUS_GOOD)) {
26416 /* No error - we're done */
26417 rval = 0;
26418 break;
26419
26420 } else if (pkt->pkt_reason == CMD_DEV_GONE) {
26421 /* Lost connection - give up */
26422 break;
26423
26424 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26425 (pkt->pkt_state == 0)) {
26426 /* Pkt not dispatched - try again. */
26427 poll_delay = 1 * CSEC; /* 10 msec. */
26428
26429 } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26430 (rc == STATUS_QFULL)) {
26431 /* Queue full - try again. */
26432 poll_delay = 1 * CSEC; /* 10 msec. */
26433
26434 } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26435 (rc == STATUS_BUSY)) {
26436 /* Busy - try again. */
26437 poll_delay = 100 * CSEC; /* 1 sec. */
26438 busy_count += (SEC_TO_CSEC - 1);
26439
26440 } else if ((sensep != NULL) &&
26441 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26442 /*
26443 * Unit Attention - try again.
26444 * Pretend it took 1 sec.
26445 * NOTE: 'continue' avoids poll_delay
26446 */
26447 busy_count += (SEC_TO_CSEC - 1);
26448 continue;
26449
26450 } else if ((sensep != NULL) &&
26451 (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26452 (scsi_sense_asc(sensep) == 0x04) &&
26453 (scsi_sense_ascq(sensep) == 0x01)) {
26454 /*
26455 * Not ready -> ready - try again.
26456 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26457 * ...same as STATUS_BUSY
26458 */
26459 poll_delay = 100 * CSEC; /* 1 sec. */
26460 busy_count += (SEC_TO_CSEC - 1);
26461
26462 } else {
26463 /* BAD status - give up. */
26464 break;
26465 }
26466 }
26467
26468 if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26469 !do_polled_io) {
26470 delay(drv_usectohz(poll_delay));
26471 } else {
26472 /* we busy wait during cpr_dump or interrupt threads */
26473 drv_usecwait(poll_delay);
26474 }
26475 }
26476
26477 pkt->pkt_flags = savef;
26478 pkt->pkt_comp = savec;
26479 pkt->pkt_time = savet;
26480
26481 /* return on error */
26482 if (rval)
26483 return (rval);
26484
26485 /*
26486 * This is not a performance critical code path.
26487 *
26488 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26489 * issues associated with looking at DMA memory prior to
26490 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26491 */
26492 scsi_sync_pkt(pkt);
26493 return (0);
26494 }
26495
26496
26497
26498 /*
26499 * Function: sd_persistent_reservation_in_read_keys
26500 *
26501 * Description: This routine is the driver entry point for handling CD-ROM
26502 * multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26503 * by sending the SCSI-3 PRIN commands to the device.
26504 * Processes the read keys command response by copying the
26505 * reservation key information into the user provided buffer.
26506 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26507 *
26508 * Arguments: un - Pointer to soft state struct for the target.
26509 * usrp - user provided pointer to multihost Persistent In Read
26510 * Keys structure (mhioc_inkeys_t)
26511 * flag - this argument is a pass through to ddi_copyxxx()
26512 * directly from the mode argument of ioctl().
26513 *
26514 * Return Code: 0 - Success
26515 * EACCES
26516 * ENOTSUP
26517 * errno return code from sd_send_scsi_cmd()
26518 *
26519 * Context: Can sleep. Does not return until command is completed.
26520 */
26521
26522 static int
26523 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26524 mhioc_inkeys_t *usrp, int flag)
26525 {
26526 #ifdef _MULTI_DATAMODEL
26527 struct mhioc_key_list32 li32;
26528 #endif
26529 sd_prin_readkeys_t *in;
26530 mhioc_inkeys_t *ptr;
26531 mhioc_key_list_t li;
26532 uchar_t *data_bufp;
26533 int data_len;
26534 int rval = 0;
26535 size_t copysz;
26536 sd_ssc_t *ssc;
26537
26538 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26539 return (EINVAL);
26540 }
26541 bzero(&li, sizeof (mhioc_key_list_t));
26542
26543 ssc = sd_ssc_init(un);
26544
26545 /*
26546 * Get the listsize from user
26547 */
26548 #ifdef _MULTI_DATAMODEL
26549
26550 switch (ddi_model_convert_from(flag & FMODELS)) {
26551 case DDI_MODEL_ILP32:
26552 copysz = sizeof (struct mhioc_key_list32);
26553 if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26554 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26555 "sd_persistent_reservation_in_read_keys: "
26556 "failed ddi_copyin: mhioc_key_list32_t\n");
26557 rval = EFAULT;
26558 goto done;
26559 }
26560 li.listsize = li32.listsize;
26561 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26562 break;
26563
26564 case DDI_MODEL_NONE:
26565 copysz = sizeof (mhioc_key_list_t);
26566 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26567 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26568 "sd_persistent_reservation_in_read_keys: "
26569 "failed ddi_copyin: mhioc_key_list_t\n");
26570 rval = EFAULT;
26571 goto done;
26572 }
26573 break;
26574 }
26575
26576 #else /* ! _MULTI_DATAMODEL */
26577 copysz = sizeof (mhioc_key_list_t);
26578 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26579 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26580 "sd_persistent_reservation_in_read_keys: "
26581 "failed ddi_copyin: mhioc_key_list_t\n");
26582 rval = EFAULT;
26583 goto done;
26584 }
26585 #endif
26586
26587 data_len = li.listsize * MHIOC_RESV_KEY_SIZE;
26588 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26589 data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26590
26591 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26592 data_len, data_bufp);
26593 if (rval != 0) {
26594 if (rval == EIO)
26595 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26596 else
26597 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26598 goto done;
26599 }
26600 in = (sd_prin_readkeys_t *)data_bufp;
26601 ptr->generation = BE_32(in->generation);
26602 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26603
26604 /*
26605 * Return the min(listsize, listlen) keys
26606 */
26607 #ifdef _MULTI_DATAMODEL
26608
26609 switch (ddi_model_convert_from(flag & FMODELS)) {
26610 case DDI_MODEL_ILP32:
26611 li32.listlen = li.listlen;
26612 if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26613 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26614 "sd_persistent_reservation_in_read_keys: "
26615 "failed ddi_copyout: mhioc_key_list32_t\n");
26616 rval = EFAULT;
26617 goto done;
26618 }
26619 break;
26620
26621 case DDI_MODEL_NONE:
26622 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26623 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26624 "sd_persistent_reservation_in_read_keys: "
26625 "failed ddi_copyout: mhioc_key_list_t\n");
26626 rval = EFAULT;
26627 goto done;
26628 }
26629 break;
26630 }
26631
26632 #else /* ! _MULTI_DATAMODEL */
26633
26634 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26635 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26636 "sd_persistent_reservation_in_read_keys: "
26637 "failed ddi_copyout: mhioc_key_list_t\n");
26638 rval = EFAULT;
26639 goto done;
26640 }
26641
26642 #endif /* _MULTI_DATAMODEL */
26643
26644 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26645 li.listsize * MHIOC_RESV_KEY_SIZE);
26646 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26647 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26648 "sd_persistent_reservation_in_read_keys: "
26649 "failed ddi_copyout: keylist\n");
26650 rval = EFAULT;
26651 }
26652 done:
26653 sd_ssc_fini(ssc);
26654 kmem_free(data_bufp, data_len);
26655 return (rval);
26656 }
26657
26658
26659 /*
26660 * Function: sd_persistent_reservation_in_read_resv
26661 *
26662 * Description: This routine is the driver entry point for handling CD-ROM
26663 * multi-host persistent reservation requests (MHIOCGRP_INRESV)
26664 * by sending the SCSI-3 PRIN commands to the device.
26665 * Process the read persistent reservations command response by
26666 * copying the reservation information into the user provided
26667 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26668 *
26669 * Arguments: un - Pointer to soft state struct for the target.
26670 * usrp - user provided pointer to multihost Persistent In Read
26671 * Keys structure (mhioc_inkeys_t)
26672 * flag - this argument is a pass through to ddi_copyxxx()
26673 * directly from the mode argument of ioctl().
26674 *
26675 * Return Code: 0 - Success
26676 * EACCES
26677 * ENOTSUP
26678 * errno return code from sd_send_scsi_cmd()
26679 *
26680 * Context: Can sleep. Does not return until command is completed.
26681 */
26682
26683 static int
26684 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26685 mhioc_inresvs_t *usrp, int flag)
26686 {
26687 #ifdef _MULTI_DATAMODEL
26688 struct mhioc_resv_desc_list32 resvlist32;
26689 #endif
26690 sd_prin_readresv_t *in;
26691 mhioc_inresvs_t *ptr;
26692 sd_readresv_desc_t *readresv_ptr;
26693 mhioc_resv_desc_list_t resvlist;
26694 mhioc_resv_desc_t resvdesc;
26695 uchar_t *data_bufp = NULL;
26696 int data_len;
26697 int rval = 0;
26698 int i;
26699 size_t copysz;
26700 mhioc_resv_desc_t *bufp;
26701 sd_ssc_t *ssc;
26702
26703 if ((ptr = usrp) == NULL) {
26704 return (EINVAL);
26705 }
26706
26707 ssc = sd_ssc_init(un);
26708
26709 /*
26710 * Get the listsize from user
26711 */
26712 #ifdef _MULTI_DATAMODEL
26713 switch (ddi_model_convert_from(flag & FMODELS)) {
26714 case DDI_MODEL_ILP32:
26715 copysz = sizeof (struct mhioc_resv_desc_list32);
26716 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26717 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26718 "sd_persistent_reservation_in_read_resv: "
26719 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26720 rval = EFAULT;
26721 goto done;
26722 }
26723 resvlist.listsize = resvlist32.listsize;
26724 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26725 break;
26726
26727 case DDI_MODEL_NONE:
26728 copysz = sizeof (mhioc_resv_desc_list_t);
26729 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26730 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26731 "sd_persistent_reservation_in_read_resv: "
26732 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26733 rval = EFAULT;
26734 goto done;
26735 }
26736 break;
26737 }
26738 #else /* ! _MULTI_DATAMODEL */
26739 copysz = sizeof (mhioc_resv_desc_list_t);
26740 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26741 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26742 "sd_persistent_reservation_in_read_resv: "
26743 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26744 rval = EFAULT;
26745 goto done;
26746 }
26747 #endif /* ! _MULTI_DATAMODEL */
26748
26749 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26750 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26751 data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26752
26753 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26754 data_len, data_bufp);
26755 if (rval != 0) {
26756 if (rval == EIO)
26757 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26758 else
26759 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26760 goto done;
26761 }
26762 in = (sd_prin_readresv_t *)data_bufp;
26763 ptr->generation = BE_32(in->generation);
26764 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26765
26766 /*
26767 * Return the min(listsize, listlen( keys
26768 */
26769 #ifdef _MULTI_DATAMODEL
26770
26771 switch (ddi_model_convert_from(flag & FMODELS)) {
26772 case DDI_MODEL_ILP32:
26773 resvlist32.listlen = resvlist.listlen;
26774 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26775 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26776 "sd_persistent_reservation_in_read_resv: "
26777 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26778 rval = EFAULT;
26779 goto done;
26780 }
26781 break;
26782
26783 case DDI_MODEL_NONE:
26784 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26785 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26786 "sd_persistent_reservation_in_read_resv: "
26787 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26788 rval = EFAULT;
26789 goto done;
26790 }
26791 break;
26792 }
26793
26794 #else /* ! _MULTI_DATAMODEL */
26795
26796 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26797 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26798 "sd_persistent_reservation_in_read_resv: "
26799 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26800 rval = EFAULT;
26801 goto done;
26802 }
26803
26804 #endif /* ! _MULTI_DATAMODEL */
26805
26806 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26807 bufp = resvlist.list;
26808 copysz = sizeof (mhioc_resv_desc_t);
26809 for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26810 i++, readresv_ptr++, bufp++) {
26811
26812 bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26813 MHIOC_RESV_KEY_SIZE);
26814 resvdesc.type = readresv_ptr->type;
26815 resvdesc.scope = readresv_ptr->scope;
26816 resvdesc.scope_specific_addr =
26817 BE_32(readresv_ptr->scope_specific_addr);
26818
26819 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26820 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26821 "sd_persistent_reservation_in_read_resv: "
26822 "failed ddi_copyout: resvlist\n");
26823 rval = EFAULT;
26824 goto done;
26825 }
26826 }
26827 done:
26828 sd_ssc_fini(ssc);
26829 /* only if data_bufp is allocated, we need to free it */
26830 if (data_bufp) {
26831 kmem_free(data_bufp, data_len);
26832 }
26833 return (rval);
26834 }
26835
26836
26837 /*
26838 * Function: sr_change_blkmode()
26839 *
26840 * Description: This routine is the driver entry point for handling CD-ROM
26841 * block mode ioctl requests. Support for returning and changing
26842 * the current block size in use by the device is implemented. The
26843 * LBA size is changed via a MODE SELECT Block Descriptor.
26844 *
26845 * This routine issues a mode sense with an allocation length of
26846 * 12 bytes for the mode page header and a single block descriptor.
26847 *
26848 * Arguments: dev - the device 'dev_t'
26849 * cmd - the request type; one of CDROMGBLKMODE (get) or
26850 * CDROMSBLKMODE (set)
26851 * data - current block size or requested block size
26852 * flag - this argument is a pass through to ddi_copyxxx() directly
26853 * from the mode argument of ioctl().
26854 *
26855 * Return Code: the code returned by sd_send_scsi_cmd()
26856 * EINVAL if invalid arguments are provided
26857 * EFAULT if ddi_copyxxx() fails
26858 * ENXIO if fail ddi_get_soft_state
26859 * EIO if invalid mode sense block descriptor length
26860 *
26861 */
26862
26863 static int
26864 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26865 {
26866 struct sd_lun *un = NULL;
26867 struct mode_header *sense_mhp, *select_mhp;
26868 struct block_descriptor *sense_desc, *select_desc;
26869 int current_bsize;
26870 int rval = EINVAL;
26871 uchar_t *sense = NULL;
26872 uchar_t *select = NULL;
26873 sd_ssc_t *ssc;
26874
26875 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26876
26877 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26878 return (ENXIO);
26879 }
26880
26881 /*
26882 * The block length is changed via the Mode Select block descriptor, the
26883 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26884 * required as part of this routine. Therefore the mode sense allocation
26885 * length is specified to be the length of a mode page header and a
26886 * block descriptor.
26887 */
26888 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26889
26890 ssc = sd_ssc_init(un);
26891 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26892 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26893 sd_ssc_fini(ssc);
26894 if (rval != 0) {
26895 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26896 "sr_change_blkmode: Mode Sense Failed\n");
26897 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26898 return (rval);
26899 }
26900
26901 /* Check the block descriptor len to handle only 1 block descriptor */
26902 sense_mhp = (struct mode_header *)sense;
26903 if ((sense_mhp->bdesc_length == 0) ||
26904 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26905 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26906 "sr_change_blkmode: Mode Sense returned invalid block"
26907 " descriptor length\n");
26908 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26909 return (EIO);
26910 }
26911 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26912 current_bsize = ((sense_desc->blksize_hi << 16) |
26913 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26914
26915 /* Process command */
26916 switch (cmd) {
26917 case CDROMGBLKMODE:
26918 /* Return the block size obtained during the mode sense */
26919 if (ddi_copyout(¤t_bsize, (void *)data,
26920 sizeof (int), flag) != 0)
26921 rval = EFAULT;
26922 break;
26923 case CDROMSBLKMODE:
26924 /* Validate the requested block size */
26925 switch (data) {
26926 case CDROM_BLK_512:
26927 case CDROM_BLK_1024:
26928 case CDROM_BLK_2048:
26929 case CDROM_BLK_2056:
26930 case CDROM_BLK_2336:
26931 case CDROM_BLK_2340:
26932 case CDROM_BLK_2352:
26933 case CDROM_BLK_2368:
26934 case CDROM_BLK_2448:
26935 case CDROM_BLK_2646:
26936 case CDROM_BLK_2647:
26937 break;
26938 default:
26939 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26940 "sr_change_blkmode: "
26941 "Block Size '%ld' Not Supported\n", data);
26942 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26943 return (EINVAL);
26944 }
26945
26946 /*
26947 * The current block size matches the requested block size so
26948 * there is no need to send the mode select to change the size
26949 */
26950 if (current_bsize == data) {
26951 break;
26952 }
26953
26954 /* Build the select data for the requested block size */
26955 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26956 select_mhp = (struct mode_header *)select;
26957 select_desc =
26958 (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26959 /*
26960 * The LBA size is changed via the block descriptor, so the
26961 * descriptor is built according to the user data
26962 */
26963 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26964 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16);
26965 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26966 select_desc->blksize_lo = (char)((data) & 0x000000ff);
26967
26968 /* Send the mode select for the requested block size */
26969 ssc = sd_ssc_init(un);
26970 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26971 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26972 SD_PATH_STANDARD);
26973 sd_ssc_fini(ssc);
26974 if (rval != 0) {
26975 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26976 "sr_change_blkmode: Mode Select Failed\n");
26977 /*
26978 * The mode select failed for the requested block size,
26979 * so reset the data for the original block size and
26980 * send it to the target. The error is indicated by the
26981 * return value for the failed mode select.
26982 */
26983 select_desc->blksize_hi = sense_desc->blksize_hi;
26984 select_desc->blksize_mid = sense_desc->blksize_mid;
26985 select_desc->blksize_lo = sense_desc->blksize_lo;
26986 ssc = sd_ssc_init(un);
26987 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26988 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26989 SD_PATH_STANDARD);
26990 sd_ssc_fini(ssc);
26991 } else {
26992 ASSERT(!mutex_owned(SD_MUTEX(un)));
26993 mutex_enter(SD_MUTEX(un));
26994 sd_update_block_info(un, (uint32_t)data, 0);
26995 mutex_exit(SD_MUTEX(un));
26996 }
26997 break;
26998 default:
26999 /* should not reach here, but check anyway */
27000 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27001 "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27002 rval = EINVAL;
27003 break;
27004 }
27005
27006 if (select) {
27007 kmem_free(select, BUFLEN_CHG_BLK_MODE);
27008 }
27009 if (sense) {
27010 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27011 }
27012 return (rval);
27013 }
27014
27015
27016 /*
27017 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27018 * implement driver support for getting and setting the CD speed. The command
27019 * set used will be based on the device type. If the device has not been
27020 * identified as MMC the Toshiba vendor specific mode page will be used. If
27021 * the device is MMC but does not support the Real Time Streaming feature
27022 * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27023 * be used to read the speed.
27024 */
27025
27026 /*
27027 * Function: sr_change_speed()
27028 *
27029 * Description: This routine is the driver entry point for handling CD-ROM
27030 * drive speed ioctl requests for devices supporting the Toshiba
27031 * vendor specific drive speed mode page. Support for returning
27032 * and changing the current drive speed in use by the device is
27033 * implemented.
27034 *
27035 * Arguments: dev - the device 'dev_t'
27036 * cmd - the request type; one of CDROMGDRVSPEED (get) or
27037 * CDROMSDRVSPEED (set)
27038 * data - current drive speed or requested drive speed
27039 * flag - this argument is a pass through to ddi_copyxxx() directly
27040 * from the mode argument of ioctl().
27041 *
27042 * Return Code: the code returned by sd_send_scsi_cmd()
27043 * EINVAL if invalid arguments are provided
27044 * EFAULT if ddi_copyxxx() fails
27045 * ENXIO if fail ddi_get_soft_state
27046 * EIO if invalid mode sense block descriptor length
27047 */
27048
27049 static int
27050 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27051 {
27052 struct sd_lun *un = NULL;
27053 struct mode_header *sense_mhp, *select_mhp;
27054 struct mode_speed *sense_page, *select_page;
27055 int current_speed;
27056 int rval = EINVAL;
27057 int bd_len;
27058 uchar_t *sense = NULL;
27059 uchar_t *select = NULL;
27060 sd_ssc_t *ssc;
27061
27062 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27063 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27064 return (ENXIO);
27065 }
27066
27067 /*
27068 * Note: The drive speed is being modified here according to a Toshiba
27069 * vendor specific mode page (0x31).
27070 */
27071 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27072
27073 ssc = sd_ssc_init(un);
27074 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27075 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27076 SD_PATH_STANDARD);
27077 sd_ssc_fini(ssc);
27078 if (rval != 0) {
27079 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27080 "sr_change_speed: Mode Sense Failed\n");
27081 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27082 return (rval);
27083 }
27084 sense_mhp = (struct mode_header *)sense;
27085
27086 /* Check the block descriptor len to handle only 1 block descriptor */
27087 bd_len = sense_mhp->bdesc_length;
27088 if (bd_len > MODE_BLK_DESC_LENGTH) {
27089 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27090 "sr_change_speed: Mode Sense returned invalid block "
27091 "descriptor length\n");
27092 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27093 return (EIO);
27094 }
27095
27096 sense_page = (struct mode_speed *)
27097 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27098 current_speed = sense_page->speed;
27099
27100 /* Process command */
27101 switch (cmd) {
27102 case CDROMGDRVSPEED:
27103 /* Return the drive speed obtained during the mode sense */
27104 if (current_speed == 0x2) {
27105 current_speed = CDROM_TWELVE_SPEED;
27106 }
27107 if (ddi_copyout(¤t_speed, (void *)data,
27108 sizeof (int), flag) != 0) {
27109 rval = EFAULT;
27110 }
27111 break;
27112 case CDROMSDRVSPEED:
27113 /* Validate the requested drive speed */
27114 switch ((uchar_t)data) {
27115 case CDROM_TWELVE_SPEED:
27116 data = 0x2;
27117 /*FALLTHROUGH*/
27118 case CDROM_NORMAL_SPEED:
27119 case CDROM_DOUBLE_SPEED:
27120 case CDROM_QUAD_SPEED:
27121 case CDROM_MAXIMUM_SPEED:
27122 break;
27123 default:
27124 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27125 "sr_change_speed: "
27126 "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27127 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27128 return (EINVAL);
27129 }
27130
27131 /*
27132 * The current drive speed matches the requested drive speed so
27133 * there is no need to send the mode select to change the speed
27134 */
27135 if (current_speed == data) {
27136 break;
27137 }
27138
27139 /* Build the select data for the requested drive speed */
27140 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27141 select_mhp = (struct mode_header *)select;
27142 select_mhp->bdesc_length = 0;
27143 select_page =
27144 (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27145 select_page =
27146 (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27147 select_page->mode_page.code = CDROM_MODE_SPEED;
27148 select_page->mode_page.length = 2;
27149 select_page->speed = (uchar_t)data;
27150
27151 /* Send the mode select for the requested block size */
27152 ssc = sd_ssc_init(un);
27153 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27154 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27155 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27156 sd_ssc_fini(ssc);
27157 if (rval != 0) {
27158 /*
27159 * The mode select failed for the requested drive speed,
27160 * so reset the data for the original drive speed and
27161 * send it to the target. The error is indicated by the
27162 * return value for the failed mode select.
27163 */
27164 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27165 "sr_drive_speed: Mode Select Failed\n");
27166 select_page->speed = sense_page->speed;
27167 ssc = sd_ssc_init(un);
27168 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27169 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27170 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27171 sd_ssc_fini(ssc);
27172 }
27173 break;
27174 default:
27175 /* should not reach here, but check anyway */
27176 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27177 "sr_change_speed: Command '%x' Not Supported\n", cmd);
27178 rval = EINVAL;
27179 break;
27180 }
27181
27182 if (select) {
27183 kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27184 }
27185 if (sense) {
27186 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27187 }
27188
27189 return (rval);
27190 }
27191
27192
27193 /*
27194 * Function: sr_atapi_change_speed()
27195 *
27196 * Description: This routine is the driver entry point for handling CD-ROM
27197 * drive speed ioctl requests for MMC devices that do not support
27198 * the Real Time Streaming feature (0x107).
27199 *
27200 * Note: This routine will use the SET SPEED command which may not
27201 * be supported by all devices.
27202 *
27203 * Arguments: dev- the device 'dev_t'
27204 * cmd- the request type; one of CDROMGDRVSPEED (get) or
27205 * CDROMSDRVSPEED (set)
27206 * data- current drive speed or requested drive speed
27207 * flag- this argument is a pass through to ddi_copyxxx() directly
27208 * from the mode argument of ioctl().
27209 *
27210 * Return Code: the code returned by sd_send_scsi_cmd()
27211 * EINVAL if invalid arguments are provided
27212 * EFAULT if ddi_copyxxx() fails
27213 * ENXIO if fail ddi_get_soft_state
27214 * EIO if invalid mode sense block descriptor length
27215 */
27216
27217 static int
27218 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27219 {
27220 struct sd_lun *un;
27221 struct uscsi_cmd *com = NULL;
27222 struct mode_header_grp2 *sense_mhp;
27223 uchar_t *sense_page;
27224 uchar_t *sense = NULL;
27225 char cdb[CDB_GROUP5];
27226 int bd_len;
27227 int current_speed = 0;
27228 int max_speed = 0;
27229 int rval;
27230 sd_ssc_t *ssc;
27231
27232 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27233
27234 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27235 return (ENXIO);
27236 }
27237
27238 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27239
27240 ssc = sd_ssc_init(un);
27241 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27242 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27243 SD_PATH_STANDARD);
27244 sd_ssc_fini(ssc);
27245 if (rval != 0) {
27246 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27247 "sr_atapi_change_speed: Mode Sense Failed\n");
27248 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27249 return (rval);
27250 }
27251
27252 /* Check the block descriptor len to handle only 1 block descriptor */
27253 sense_mhp = (struct mode_header_grp2 *)sense;
27254 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27255 if (bd_len > MODE_BLK_DESC_LENGTH) {
27256 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27257 "sr_atapi_change_speed: Mode Sense returned invalid "
27258 "block descriptor length\n");
27259 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27260 return (EIO);
27261 }
27262
27263 /* Calculate the current and maximum drive speeds */
27264 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27265 current_speed = (sense_page[14] << 8) | sense_page[15];
27266 max_speed = (sense_page[8] << 8) | sense_page[9];
27267
27268 /* Process the command */
27269 switch (cmd) {
27270 case CDROMGDRVSPEED:
27271 current_speed /= SD_SPEED_1X;
27272 if (ddi_copyout(¤t_speed, (void *)data,
27273 sizeof (int), flag) != 0)
27274 rval = EFAULT;
27275 break;
27276 case CDROMSDRVSPEED:
27277 /* Convert the speed code to KB/sec */
27278 switch ((uchar_t)data) {
27279 case CDROM_NORMAL_SPEED:
27280 current_speed = SD_SPEED_1X;
27281 break;
27282 case CDROM_DOUBLE_SPEED:
27283 current_speed = 2 * SD_SPEED_1X;
27284 break;
27285 case CDROM_QUAD_SPEED:
27286 current_speed = 4 * SD_SPEED_1X;
27287 break;
27288 case CDROM_TWELVE_SPEED:
27289 current_speed = 12 * SD_SPEED_1X;
27290 break;
27291 case CDROM_MAXIMUM_SPEED:
27292 current_speed = 0xffff;
27293 break;
27294 default:
27295 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27296 "sr_atapi_change_speed: invalid drive speed %d\n",
27297 (uchar_t)data);
27298 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27299 return (EINVAL);
27300 }
27301
27302 /* Check the request against the drive's max speed. */
27303 if (current_speed != 0xffff) {
27304 if (current_speed > max_speed) {
27305 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27306 return (EINVAL);
27307 }
27308 }
27309
27310 /*
27311 * Build and send the SET SPEED command
27312 *
27313 * Note: The SET SPEED (0xBB) command used in this routine is
27314 * obsolete per the SCSI MMC spec but still supported in the
27315 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27316 * therefore the command is still implemented in this routine.
27317 */
27318 bzero(cdb, sizeof (cdb));
27319 cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27320 cdb[2] = (uchar_t)(current_speed >> 8);
27321 cdb[3] = (uchar_t)current_speed;
27322 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27323 com->uscsi_cdb = (caddr_t)cdb;
27324 com->uscsi_cdblen = CDB_GROUP5;
27325 com->uscsi_bufaddr = NULL;
27326 com->uscsi_buflen = 0;
27327 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27328 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27329 break;
27330 default:
27331 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27332 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27333 rval = EINVAL;
27334 }
27335
27336 if (sense) {
27337 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27338 }
27339 if (com) {
27340 kmem_free(com, sizeof (*com));
27341 }
27342 return (rval);
27343 }
27344
27345
27346 /*
27347 * Function: sr_pause_resume()
27348 *
27349 * Description: This routine is the driver entry point for handling CD-ROM
27350 * pause/resume ioctl requests. This only affects the audio play
27351 * operation.
27352 *
27353 * Arguments: dev - the device 'dev_t'
27354 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27355 * for setting the resume bit of the cdb.
27356 *
27357 * Return Code: the code returned by sd_send_scsi_cmd()
27358 * EINVAL if invalid mode specified
27359 *
27360 */
27361
27362 static int
27363 sr_pause_resume(dev_t dev, int cmd)
27364 {
27365 struct sd_lun *un;
27366 struct uscsi_cmd *com;
27367 char cdb[CDB_GROUP1];
27368 int rval;
27369
27370 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27371 return (ENXIO);
27372 }
27373
27374 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27375 bzero(cdb, CDB_GROUP1);
27376 cdb[0] = SCMD_PAUSE_RESUME;
27377 switch (cmd) {
27378 case CDROMRESUME:
27379 cdb[8] = 1;
27380 break;
27381 case CDROMPAUSE:
27382 cdb[8] = 0;
27383 break;
27384 default:
27385 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27386 " Command '%x' Not Supported\n", cmd);
27387 rval = EINVAL;
27388 goto done;
27389 }
27390
27391 com->uscsi_cdb = cdb;
27392 com->uscsi_cdblen = CDB_GROUP1;
27393 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27394
27395 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27396 SD_PATH_STANDARD);
27397
27398 done:
27399 kmem_free(com, sizeof (*com));
27400 return (rval);
27401 }
27402
27403
27404 /*
27405 * Function: sr_play_msf()
27406 *
27407 * Description: This routine is the driver entry point for handling CD-ROM
27408 * ioctl requests to output the audio signals at the specified
27409 * starting address and continue the audio play until the specified
27410 * ending address (CDROMPLAYMSF) The address is in Minute Second
27411 * Frame (MSF) format.
27412 *
27413 * Arguments: dev - the device 'dev_t'
27414 * data - pointer to user provided audio msf structure,
27415 * specifying start/end addresses.
27416 * flag - this argument is a pass through to ddi_copyxxx()
27417 * directly from the mode argument of ioctl().
27418 *
27419 * Return Code: the code returned by sd_send_scsi_cmd()
27420 * EFAULT if ddi_copyxxx() fails
27421 * ENXIO if fail ddi_get_soft_state
27422 * EINVAL if data pointer is NULL
27423 */
27424
27425 static int
27426 sr_play_msf(dev_t dev, caddr_t data, int flag)
27427 {
27428 struct sd_lun *un;
27429 struct uscsi_cmd *com;
27430 struct cdrom_msf msf_struct;
27431 struct cdrom_msf *msf = &msf_struct;
27432 char cdb[CDB_GROUP1];
27433 int rval;
27434
27435 if (data == NULL) {
27436 return (EINVAL);
27437 }
27438
27439 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27440 return (ENXIO);
27441 }
27442
27443 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27444 return (EFAULT);
27445 }
27446
27447 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27448 bzero(cdb, CDB_GROUP1);
27449 cdb[0] = SCMD_PLAYAUDIO_MSF;
27450 if (un->un_f_cfg_playmsf_bcd == TRUE) {
27451 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27452 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27453 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27454 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27455 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27456 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27457 } else {
27458 cdb[3] = msf->cdmsf_min0;
27459 cdb[4] = msf->cdmsf_sec0;
27460 cdb[5] = msf->cdmsf_frame0;
27461 cdb[6] = msf->cdmsf_min1;
27462 cdb[7] = msf->cdmsf_sec1;
27463 cdb[8] = msf->cdmsf_frame1;
27464 }
27465 com->uscsi_cdb = cdb;
27466 com->uscsi_cdblen = CDB_GROUP1;
27467 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27468 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27469 SD_PATH_STANDARD);
27470 kmem_free(com, sizeof (*com));
27471 return (rval);
27472 }
27473
27474
27475 /*
27476 * Function: sr_play_trkind()
27477 *
27478 * Description: This routine is the driver entry point for handling CD-ROM
27479 * ioctl requests to output the audio signals at the specified
27480 * starting address and continue the audio play until the specified
27481 * ending address (CDROMPLAYTRKIND). The address is in Track Index
27482 * format.
27483 *
27484 * Arguments: dev - the device 'dev_t'
27485 * data - pointer to user provided audio track/index structure,
27486 * specifying start/end addresses.
27487 * flag - this argument is a pass through to ddi_copyxxx()
27488 * directly from the mode argument of ioctl().
27489 *
27490 * Return Code: the code returned by sd_send_scsi_cmd()
27491 * EFAULT if ddi_copyxxx() fails
27492 * ENXIO if fail ddi_get_soft_state
27493 * EINVAL if data pointer is NULL
27494 */
27495
27496 static int
27497 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27498 {
27499 struct cdrom_ti ti_struct;
27500 struct cdrom_ti *ti = &ti_struct;
27501 struct uscsi_cmd *com = NULL;
27502 char cdb[CDB_GROUP1];
27503 int rval;
27504
27505 if (data == NULL) {
27506 return (EINVAL);
27507 }
27508
27509 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27510 return (EFAULT);
27511 }
27512
27513 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27514 bzero(cdb, CDB_GROUP1);
27515 cdb[0] = SCMD_PLAYAUDIO_TI;
27516 cdb[4] = ti->cdti_trk0;
27517 cdb[5] = ti->cdti_ind0;
27518 cdb[7] = ti->cdti_trk1;
27519 cdb[8] = ti->cdti_ind1;
27520 com->uscsi_cdb = cdb;
27521 com->uscsi_cdblen = CDB_GROUP1;
27522 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27523 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27524 SD_PATH_STANDARD);
27525 kmem_free(com, sizeof (*com));
27526 return (rval);
27527 }
27528
27529
27530 /*
27531 * Function: sr_read_all_subcodes()
27532 *
27533 * Description: This routine is the driver entry point for handling CD-ROM
27534 * ioctl requests to return raw subcode data while the target is
27535 * playing audio (CDROMSUBCODE).
27536 *
27537 * Arguments: dev - the device 'dev_t'
27538 * data - pointer to user provided cdrom subcode structure,
27539 * specifying the transfer length and address.
27540 * flag - this argument is a pass through to ddi_copyxxx()
27541 * directly from the mode argument of ioctl().
27542 *
27543 * Return Code: the code returned by sd_send_scsi_cmd()
27544 * EFAULT if ddi_copyxxx() fails
27545 * ENXIO if fail ddi_get_soft_state
27546 * EINVAL if data pointer is NULL
27547 */
27548
27549 static int
27550 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27551 {
27552 struct sd_lun *un = NULL;
27553 struct uscsi_cmd *com = NULL;
27554 struct cdrom_subcode *subcode = NULL;
27555 int rval;
27556 size_t buflen;
27557 char cdb[CDB_GROUP5];
27558
27559 #ifdef _MULTI_DATAMODEL
27560 /* To support ILP32 applications in an LP64 world */
27561 struct cdrom_subcode32 cdrom_subcode32;
27562 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32;
27563 #endif
27564 if (data == NULL) {
27565 return (EINVAL);
27566 }
27567
27568 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27569 return (ENXIO);
27570 }
27571
27572 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27573
27574 #ifdef _MULTI_DATAMODEL
27575 switch (ddi_model_convert_from(flag & FMODELS)) {
27576 case DDI_MODEL_ILP32:
27577 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27578 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27579 "sr_read_all_subcodes: ddi_copyin Failed\n");
27580 kmem_free(subcode, sizeof (struct cdrom_subcode));
27581 return (EFAULT);
27582 }
27583 /* Convert the ILP32 uscsi data from the application to LP64 */
27584 cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27585 break;
27586 case DDI_MODEL_NONE:
27587 if (ddi_copyin(data, subcode,
27588 sizeof (struct cdrom_subcode), flag)) {
27589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27590 "sr_read_all_subcodes: ddi_copyin Failed\n");
27591 kmem_free(subcode, sizeof (struct cdrom_subcode));
27592 return (EFAULT);
27593 }
27594 break;
27595 }
27596 #else /* ! _MULTI_DATAMODEL */
27597 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27598 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27599 "sr_read_all_subcodes: ddi_copyin Failed\n");
27600 kmem_free(subcode, sizeof (struct cdrom_subcode));
27601 return (EFAULT);
27602 }
27603 #endif /* _MULTI_DATAMODEL */
27604
27605 /*
27606 * Since MMC-2 expects max 3 bytes for length, check if the
27607 * length input is greater than 3 bytes
27608 */
27609 if ((subcode->cdsc_length & 0xFF000000) != 0) {
27610 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27611 "sr_read_all_subcodes: "
27612 "cdrom transfer length too large: %d (limit %d)\n",
27613 subcode->cdsc_length, 0xFFFFFF);
27614 kmem_free(subcode, sizeof (struct cdrom_subcode));
27615 return (EINVAL);
27616 }
27617
27618 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27619 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27620 bzero(cdb, CDB_GROUP5);
27621
27622 if (un->un_f_mmc_cap == TRUE) {
27623 cdb[0] = (char)SCMD_READ_CD;
27624 cdb[2] = (char)0xff;
27625 cdb[3] = (char)0xff;
27626 cdb[4] = (char)0xff;
27627 cdb[5] = (char)0xff;
27628 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27629 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27630 cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27631 cdb[10] = 1;
27632 } else {
27633 /*
27634 * Note: A vendor specific command (0xDF) is being used her to
27635 * request a read of all subcodes.
27636 */
27637 cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27638 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27639 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27640 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27641 cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27642 }
27643 com->uscsi_cdb = cdb;
27644 com->uscsi_cdblen = CDB_GROUP5;
27645 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27646 com->uscsi_buflen = buflen;
27647 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27648 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27649 SD_PATH_STANDARD);
27650 kmem_free(subcode, sizeof (struct cdrom_subcode));
27651 kmem_free(com, sizeof (*com));
27652 return (rval);
27653 }
27654
27655
27656 /*
27657 * Function: sr_read_subchannel()
27658 *
27659 * Description: This routine is the driver entry point for handling CD-ROM
27660 * ioctl requests to return the Q sub-channel data of the CD
27661 * current position block. (CDROMSUBCHNL) The data includes the
27662 * track number, index number, absolute CD-ROM address (LBA or MSF
27663 * format per the user) , track relative CD-ROM address (LBA or MSF
27664 * format per the user), control data and audio status.
27665 *
27666 * Arguments: dev - the device 'dev_t'
27667 * data - pointer to user provided cdrom sub-channel structure
27668 * flag - this argument is a pass through to ddi_copyxxx()
27669 * directly from the mode argument of ioctl().
27670 *
27671 * Return Code: the code returned by sd_send_scsi_cmd()
27672 * EFAULT if ddi_copyxxx() fails
27673 * ENXIO if fail ddi_get_soft_state
27674 * EINVAL if data pointer is NULL
27675 */
27676
27677 static int
27678 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27679 {
27680 struct sd_lun *un;
27681 struct uscsi_cmd *com;
27682 struct cdrom_subchnl subchanel;
27683 struct cdrom_subchnl *subchnl = &subchanel;
27684 char cdb[CDB_GROUP1];
27685 caddr_t buffer;
27686 int rval;
27687
27688 if (data == NULL) {
27689 return (EINVAL);
27690 }
27691
27692 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27693 (un->un_state == SD_STATE_OFFLINE)) {
27694 return (ENXIO);
27695 }
27696
27697 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27698 return (EFAULT);
27699 }
27700
27701 buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27702 bzero(cdb, CDB_GROUP1);
27703 cdb[0] = SCMD_READ_SUBCHANNEL;
27704 /* Set the MSF bit based on the user requested address format */
27705 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27706 /*
27707 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27708 * returned
27709 */
27710 cdb[2] = 0x40;
27711 /*
27712 * Set byte 3 to specify the return data format. A value of 0x01
27713 * indicates that the CD-ROM current position should be returned.
27714 */
27715 cdb[3] = 0x01;
27716 cdb[8] = 0x10;
27717 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27718 com->uscsi_cdb = cdb;
27719 com->uscsi_cdblen = CDB_GROUP1;
27720 com->uscsi_bufaddr = buffer;
27721 com->uscsi_buflen = 16;
27722 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27723 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27724 SD_PATH_STANDARD);
27725 if (rval != 0) {
27726 kmem_free(buffer, 16);
27727 kmem_free(com, sizeof (*com));
27728 return (rval);
27729 }
27730
27731 /* Process the returned Q sub-channel data */
27732 subchnl->cdsc_audiostatus = buffer[1];
27733 subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4;
27734 subchnl->cdsc_ctrl = (buffer[5] & 0x0F);
27735 subchnl->cdsc_trk = buffer[6];
27736 subchnl->cdsc_ind = buffer[7];
27737 if (subchnl->cdsc_format & CDROM_LBA) {
27738 subchnl->cdsc_absaddr.lba =
27739 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27740 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27741 subchnl->cdsc_reladdr.lba =
27742 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27743 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27744 } else if (un->un_f_cfg_readsub_bcd == TRUE) {
27745 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27746 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27747 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]);
27748 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27749 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27750 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]);
27751 } else {
27752 subchnl->cdsc_absaddr.msf.minute = buffer[9];
27753 subchnl->cdsc_absaddr.msf.second = buffer[10];
27754 subchnl->cdsc_absaddr.msf.frame = buffer[11];
27755 subchnl->cdsc_reladdr.msf.minute = buffer[13];
27756 subchnl->cdsc_reladdr.msf.second = buffer[14];
27757 subchnl->cdsc_reladdr.msf.frame = buffer[15];
27758 }
27759 kmem_free(buffer, 16);
27760 kmem_free(com, sizeof (*com));
27761 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27762 != 0) {
27763 return (EFAULT);
27764 }
27765 return (rval);
27766 }
27767
27768
27769 /*
27770 * Function: sr_read_tocentry()
27771 *
27772 * Description: This routine is the driver entry point for handling CD-ROM
27773 * ioctl requests to read from the Table of Contents (TOC)
27774 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27775 * fields, the starting address (LBA or MSF format per the user)
27776 * and the data mode if the user specified track is a data track.
27777 *
27778 * Note: The READ HEADER (0x44) command used in this routine is
27779 * obsolete per the SCSI MMC spec but still supported in the
27780 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27781 * therefore the command is still implemented in this routine.
27782 *
27783 * Arguments: dev - the device 'dev_t'
27784 * data - pointer to user provided toc entry structure,
27785 * specifying the track # and the address format
27786 * (LBA or MSF).
27787 * flag - this argument is a pass through to ddi_copyxxx()
27788 * directly from the mode argument of ioctl().
27789 *
27790 * Return Code: the code returned by sd_send_scsi_cmd()
27791 * EFAULT if ddi_copyxxx() fails
27792 * ENXIO if fail ddi_get_soft_state
27793 * EINVAL if data pointer is NULL
27794 */
27795
27796 static int
27797 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27798 {
27799 struct sd_lun *un = NULL;
27800 struct uscsi_cmd *com;
27801 struct cdrom_tocentry toc_entry;
27802 struct cdrom_tocentry *entry = &toc_entry;
27803 caddr_t buffer;
27804 int rval;
27805 char cdb[CDB_GROUP1];
27806
27807 if (data == NULL) {
27808 return (EINVAL);
27809 }
27810
27811 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27812 (un->un_state == SD_STATE_OFFLINE)) {
27813 return (ENXIO);
27814 }
27815
27816 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27817 return (EFAULT);
27818 }
27819
27820 /* Validate the requested track and address format */
27821 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27822 return (EINVAL);
27823 }
27824
27825 if (entry->cdte_track == 0) {
27826 return (EINVAL);
27827 }
27828
27829 buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27830 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27831 bzero(cdb, CDB_GROUP1);
27832
27833 cdb[0] = SCMD_READ_TOC;
27834 /* Set the MSF bit based on the user requested address format */
27835 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27836 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27837 cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27838 } else {
27839 cdb[6] = entry->cdte_track;
27840 }
27841
27842 /*
27843 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27844 * (4 byte TOC response header + 8 byte track descriptor)
27845 */
27846 cdb[8] = 12;
27847 com->uscsi_cdb = cdb;
27848 com->uscsi_cdblen = CDB_GROUP1;
27849 com->uscsi_bufaddr = buffer;
27850 com->uscsi_buflen = 0x0C;
27851 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27852 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27853 SD_PATH_STANDARD);
27854 if (rval != 0) {
27855 kmem_free(buffer, 12);
27856 kmem_free(com, sizeof (*com));
27857 return (rval);
27858 }
27859
27860 /* Process the toc entry */
27861 entry->cdte_adr = (buffer[5] & 0xF0) >> 4;
27862 entry->cdte_ctrl = (buffer[5] & 0x0F);
27863 if (entry->cdte_format & CDROM_LBA) {
27864 entry->cdte_addr.lba =
27865 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27866 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27867 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27868 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]);
27869 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]);
27870 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]);
27871 /*
27872 * Send a READ TOC command using the LBA address format to get
27873 * the LBA for the track requested so it can be used in the
27874 * READ HEADER request
27875 *
27876 * Note: The MSF bit of the READ HEADER command specifies the
27877 * output format. The block address specified in that command
27878 * must be in LBA format.
27879 */
27880 cdb[1] = 0;
27881 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27882 SD_PATH_STANDARD);
27883 if (rval != 0) {
27884 kmem_free(buffer, 12);
27885 kmem_free(com, sizeof (*com));
27886 return (rval);
27887 }
27888 } else {
27889 entry->cdte_addr.msf.minute = buffer[9];
27890 entry->cdte_addr.msf.second = buffer[10];
27891 entry->cdte_addr.msf.frame = buffer[11];
27892 /*
27893 * Send a READ TOC command using the LBA address format to get
27894 * the LBA for the track requested so it can be used in the
27895 * READ HEADER request
27896 *
27897 * Note: The MSF bit of the READ HEADER command specifies the
27898 * output format. The block address specified in that command
27899 * must be in LBA format.
27900 */
27901 cdb[1] = 0;
27902 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27903 SD_PATH_STANDARD);
27904 if (rval != 0) {
27905 kmem_free(buffer, 12);
27906 kmem_free(com, sizeof (*com));
27907 return (rval);
27908 }
27909 }
27910
27911 /*
27912 * Build and send the READ HEADER command to determine the data mode of
27913 * the user specified track.
27914 */
27915 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27916 (entry->cdte_track != CDROM_LEADOUT)) {
27917 bzero(cdb, CDB_GROUP1);
27918 cdb[0] = SCMD_READ_HEADER;
27919 cdb[2] = buffer[8];
27920 cdb[3] = buffer[9];
27921 cdb[4] = buffer[10];
27922 cdb[5] = buffer[11];
27923 cdb[8] = 0x08;
27924 com->uscsi_buflen = 0x08;
27925 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27926 SD_PATH_STANDARD);
27927 if (rval == 0) {
27928 entry->cdte_datamode = buffer[0];
27929 } else {
27930 /*
27931 * READ HEADER command failed, since this is
27932 * obsoleted in one spec, its better to return
27933 * -1 for an invlid track so that we can still
27934 * receive the rest of the TOC data.
27935 */
27936 entry->cdte_datamode = (uchar_t)-1;
27937 }
27938 } else {
27939 entry->cdte_datamode = (uchar_t)-1;
27940 }
27941
27942 kmem_free(buffer, 12);
27943 kmem_free(com, sizeof (*com));
27944 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27945 return (EFAULT);
27946
27947 return (rval);
27948 }
27949
27950
27951 /*
27952 * Function: sr_read_tochdr()
27953 *
27954 * Description: This routine is the driver entry point for handling CD-ROM
27955 * ioctl requests to read the Table of Contents (TOC) header
27956 * (CDROMREADTOHDR). The TOC header consists of the disk starting
27957 * and ending track numbers
27958 *
27959 * Arguments: dev - the device 'dev_t'
27960 * data - pointer to user provided toc header structure,
27961 * specifying the starting and ending track numbers.
27962 * flag - this argument is a pass through to ddi_copyxxx()
27963 * directly from the mode argument of ioctl().
27964 *
27965 * Return Code: the code returned by sd_send_scsi_cmd()
27966 * EFAULT if ddi_copyxxx() fails
27967 * ENXIO if fail ddi_get_soft_state
27968 * EINVAL if data pointer is NULL
27969 */
27970
27971 static int
27972 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27973 {
27974 struct sd_lun *un;
27975 struct uscsi_cmd *com;
27976 struct cdrom_tochdr toc_header;
27977 struct cdrom_tochdr *hdr = &toc_header;
27978 char cdb[CDB_GROUP1];
27979 int rval;
27980 caddr_t buffer;
27981
27982 if (data == NULL) {
27983 return (EINVAL);
27984 }
27985
27986 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27987 (un->un_state == SD_STATE_OFFLINE)) {
27988 return (ENXIO);
27989 }
27990
27991 buffer = kmem_zalloc(4, KM_SLEEP);
27992 bzero(cdb, CDB_GROUP1);
27993 cdb[0] = SCMD_READ_TOC;
27994 /*
27995 * Specifying a track number of 0x00 in the READ TOC command indicates
27996 * that the TOC header should be returned
27997 */
27998 cdb[6] = 0x00;
27999 /*
28000 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28001 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28002 */
28003 cdb[8] = 0x04;
28004 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28005 com->uscsi_cdb = cdb;
28006 com->uscsi_cdblen = CDB_GROUP1;
28007 com->uscsi_bufaddr = buffer;
28008 com->uscsi_buflen = 0x04;
28009 com->uscsi_timeout = 300;
28010 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28011
28012 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28013 SD_PATH_STANDARD);
28014 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28015 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28016 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28017 } else {
28018 hdr->cdth_trk0 = buffer[2];
28019 hdr->cdth_trk1 = buffer[3];
28020 }
28021 kmem_free(buffer, 4);
28022 kmem_free(com, sizeof (*com));
28023 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28024 return (EFAULT);
28025 }
28026 return (rval);
28027 }
28028
28029
28030 /*
28031 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28032 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28033 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28034 * digital audio and extended architecture digital audio. These modes are
28035 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28036 * MMC specs.
28037 *
28038 * In addition to support for the various data formats these routines also
28039 * include support for devices that implement only the direct access READ
28040 * commands (0x08, 0x28), devices that implement the READ_CD commands
28041 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28042 * READ CDXA commands (0xD8, 0xDB)
28043 */
28044
28045 /*
28046 * Function: sr_read_mode1()
28047 *
28048 * Description: This routine is the driver entry point for handling CD-ROM
28049 * ioctl read mode1 requests (CDROMREADMODE1).
28050 *
28051 * Arguments: dev - the device 'dev_t'
28052 * data - pointer to user provided cd read structure specifying
28053 * the lba buffer address and length.
28054 * flag - this argument is a pass through to ddi_copyxxx()
28055 * directly from the mode argument of ioctl().
28056 *
28057 * Return Code: the code returned by sd_send_scsi_cmd()
28058 * EFAULT if ddi_copyxxx() fails
28059 * ENXIO if fail ddi_get_soft_state
28060 * EINVAL if data pointer is NULL
28061 */
28062
28063 static int
28064 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28065 {
28066 struct sd_lun *un;
28067 struct cdrom_read mode1_struct;
28068 struct cdrom_read *mode1 = &mode1_struct;
28069 int rval;
28070 sd_ssc_t *ssc;
28071
28072 #ifdef _MULTI_DATAMODEL
28073 /* To support ILP32 applications in an LP64 world */
28074 struct cdrom_read32 cdrom_read32;
28075 struct cdrom_read32 *cdrd32 = &cdrom_read32;
28076 #endif /* _MULTI_DATAMODEL */
28077
28078 if (data == NULL) {
28079 return (EINVAL);
28080 }
28081
28082 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28083 (un->un_state == SD_STATE_OFFLINE)) {
28084 return (ENXIO);
28085 }
28086
28087 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28088 "sd_read_mode1: entry: un:0x%p\n", un);
28089
28090 #ifdef _MULTI_DATAMODEL
28091 switch (ddi_model_convert_from(flag & FMODELS)) {
28092 case DDI_MODEL_ILP32:
28093 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28094 return (EFAULT);
28095 }
28096 /* Convert the ILP32 uscsi data from the application to LP64 */
28097 cdrom_read32tocdrom_read(cdrd32, mode1);
28098 break;
28099 case DDI_MODEL_NONE:
28100 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28101 return (EFAULT);
28102 }
28103 }
28104 #else /* ! _MULTI_DATAMODEL */
28105 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28106 return (EFAULT);
28107 }
28108 #endif /* _MULTI_DATAMODEL */
28109
28110 ssc = sd_ssc_init(un);
28111 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28112 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28113 sd_ssc_fini(ssc);
28114
28115 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28116 "sd_read_mode1: exit: un:0x%p\n", un);
28117
28118 return (rval);
28119 }
28120
28121
28122 /*
28123 * Function: sr_read_cd_mode2()
28124 *
28125 * Description: This routine is the driver entry point for handling CD-ROM
28126 * ioctl read mode2 requests (CDROMREADMODE2) for devices that
28127 * support the READ CD (0xBE) command or the 1st generation
28128 * READ CD (0xD4) command.
28129 *
28130 * Arguments: dev - the device 'dev_t'
28131 * data - pointer to user provided cd read structure specifying
28132 * the lba buffer address and length.
28133 * flag - this argument is a pass through to ddi_copyxxx()
28134 * directly from the mode argument of ioctl().
28135 *
28136 * Return Code: the code returned by sd_send_scsi_cmd()
28137 * EFAULT if ddi_copyxxx() fails
28138 * ENXIO if fail ddi_get_soft_state
28139 * EINVAL if data pointer is NULL
28140 */
28141
28142 static int
28143 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28144 {
28145 struct sd_lun *un;
28146 struct uscsi_cmd *com;
28147 struct cdrom_read mode2_struct;
28148 struct cdrom_read *mode2 = &mode2_struct;
28149 uchar_t cdb[CDB_GROUP5];
28150 int nblocks;
28151 int rval;
28152 #ifdef _MULTI_DATAMODEL
28153 /* To support ILP32 applications in an LP64 world */
28154 struct cdrom_read32 cdrom_read32;
28155 struct cdrom_read32 *cdrd32 = &cdrom_read32;
28156 #endif /* _MULTI_DATAMODEL */
28157
28158 if (data == NULL) {
28159 return (EINVAL);
28160 }
28161
28162 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28163 (un->un_state == SD_STATE_OFFLINE)) {
28164 return (ENXIO);
28165 }
28166
28167 #ifdef _MULTI_DATAMODEL
28168 switch (ddi_model_convert_from(flag & FMODELS)) {
28169 case DDI_MODEL_ILP32:
28170 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28171 return (EFAULT);
28172 }
28173 /* Convert the ILP32 uscsi data from the application to LP64 */
28174 cdrom_read32tocdrom_read(cdrd32, mode2);
28175 break;
28176 case DDI_MODEL_NONE:
28177 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28178 return (EFAULT);
28179 }
28180 break;
28181 }
28182
28183 #else /* ! _MULTI_DATAMODEL */
28184 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28185 return (EFAULT);
28186 }
28187 #endif /* _MULTI_DATAMODEL */
28188
28189 bzero(cdb, sizeof (cdb));
28190 if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28191 /* Read command supported by 1st generation atapi drives */
28192 cdb[0] = SCMD_READ_CDD4;
28193 } else {
28194 /* Universal CD Access Command */
28195 cdb[0] = SCMD_READ_CD;
28196 }
28197
28198 /*
28199 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28200 */
28201 cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28202
28203 /* set the start address */
28204 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28205 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28206 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28207 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28208
28209 /* set the transfer length */
28210 nblocks = mode2->cdread_buflen / 2336;
28211 cdb[6] = (uchar_t)(nblocks >> 16);
28212 cdb[7] = (uchar_t)(nblocks >> 8);
28213 cdb[8] = (uchar_t)nblocks;
28214
28215 /* set the filter bits */
28216 cdb[9] = CDROM_READ_CD_USERDATA;
28217
28218 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28219 com->uscsi_cdb = (caddr_t)cdb;
28220 com->uscsi_cdblen = sizeof (cdb);
28221 com->uscsi_bufaddr = mode2->cdread_bufaddr;
28222 com->uscsi_buflen = mode2->cdread_buflen;
28223 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28224
28225 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28226 SD_PATH_STANDARD);
28227 kmem_free(com, sizeof (*com));
28228 return (rval);
28229 }
28230
28231
28232 /*
28233 * Function: sr_read_mode2()
28234 *
28235 * Description: This routine is the driver entry point for handling CD-ROM
28236 * ioctl read mode2 requests (CDROMREADMODE2) for devices that
28237 * do not support the READ CD (0xBE) command.
28238 *
28239 * Arguments: dev - the device 'dev_t'
28240 * data - pointer to user provided cd read structure specifying
28241 * the lba buffer address and length.
28242 * flag - this argument is a pass through to ddi_copyxxx()
28243 * directly from the mode argument of ioctl().
28244 *
28245 * Return Code: the code returned by sd_send_scsi_cmd()
28246 * EFAULT if ddi_copyxxx() fails
28247 * ENXIO if fail ddi_get_soft_state
28248 * EINVAL if data pointer is NULL
28249 * EIO if fail to reset block size
28250 * EAGAIN if commands are in progress in the driver
28251 */
28252
28253 static int
28254 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28255 {
28256 struct sd_lun *un;
28257 struct cdrom_read mode2_struct;
28258 struct cdrom_read *mode2 = &mode2_struct;
28259 int rval;
28260 uint32_t restore_blksize;
28261 struct uscsi_cmd *com;
28262 uchar_t cdb[CDB_GROUP0];
28263 int nblocks;
28264
28265 #ifdef _MULTI_DATAMODEL
28266 /* To support ILP32 applications in an LP64 world */
28267 struct cdrom_read32 cdrom_read32;
28268 struct cdrom_read32 *cdrd32 = &cdrom_read32;
28269 #endif /* _MULTI_DATAMODEL */
28270
28271 if (data == NULL) {
28272 return (EINVAL);
28273 }
28274
28275 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28276 (un->un_state == SD_STATE_OFFLINE)) {
28277 return (ENXIO);
28278 }
28279
28280 /*
28281 * Because this routine will update the device and driver block size
28282 * being used we want to make sure there are no commands in progress.
28283 * If commands are in progress the user will have to try again.
28284 *
28285 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28286 * in sdioctl to protect commands from sdioctl through to the top of
28287 * sd_uscsi_strategy. See sdioctl for details.
28288 */
28289 mutex_enter(SD_MUTEX(un));
28290 if (un->un_ncmds_in_driver != 1) {
28291 mutex_exit(SD_MUTEX(un));
28292 return (EAGAIN);
28293 }
28294 mutex_exit(SD_MUTEX(un));
28295
28296 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28297 "sd_read_mode2: entry: un:0x%p\n", un);
28298
28299 #ifdef _MULTI_DATAMODEL
28300 switch (ddi_model_convert_from(flag & FMODELS)) {
28301 case DDI_MODEL_ILP32:
28302 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28303 return (EFAULT);
28304 }
28305 /* Convert the ILP32 uscsi data from the application to LP64 */
28306 cdrom_read32tocdrom_read(cdrd32, mode2);
28307 break;
28308 case DDI_MODEL_NONE:
28309 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28310 return (EFAULT);
28311 }
28312 break;
28313 }
28314 #else /* ! _MULTI_DATAMODEL */
28315 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28316 return (EFAULT);
28317 }
28318 #endif /* _MULTI_DATAMODEL */
28319
28320 /* Store the current target block size for restoration later */
28321 restore_blksize = un->un_tgt_blocksize;
28322
28323 /* Change the device and soft state target block size to 2336 */
28324 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28325 rval = EIO;
28326 goto done;
28327 }
28328
28329
28330 bzero(cdb, sizeof (cdb));
28331
28332 /* set READ operation */
28333 cdb[0] = SCMD_READ;
28334
28335 /* adjust lba for 2kbyte blocks from 512 byte blocks */
28336 mode2->cdread_lba >>= 2;
28337
28338 /* set the start address */
28339 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28340 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28341 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28342
28343 /* set the transfer length */
28344 nblocks = mode2->cdread_buflen / 2336;
28345 cdb[4] = (uchar_t)nblocks & 0xFF;
28346
28347 /* build command */
28348 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28349 com->uscsi_cdb = (caddr_t)cdb;
28350 com->uscsi_cdblen = sizeof (cdb);
28351 com->uscsi_bufaddr = mode2->cdread_bufaddr;
28352 com->uscsi_buflen = mode2->cdread_buflen;
28353 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28354
28355 /*
28356 * Issue SCSI command with user space address for read buffer.
28357 *
28358 * This sends the command through main channel in the driver.
28359 *
28360 * Since this is accessed via an IOCTL call, we go through the
28361 * standard path, so that if the device was powered down, then
28362 * it would be 'awakened' to handle the command.
28363 */
28364 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28365 SD_PATH_STANDARD);
28366
28367 kmem_free(com, sizeof (*com));
28368
28369 /* Restore the device and soft state target block size */
28370 if (sr_sector_mode(dev, restore_blksize) != 0) {
28371 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28372 "can't do switch back to mode 1\n");
28373 /*
28374 * If sd_send_scsi_READ succeeded we still need to report
28375 * an error because we failed to reset the block size
28376 */
28377 if (rval == 0) {
28378 rval = EIO;
28379 }
28380 }
28381
28382 done:
28383 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28384 "sd_read_mode2: exit: un:0x%p\n", un);
28385
28386 return (rval);
28387 }
28388
28389
28390 /*
28391 * Function: sr_sector_mode()
28392 *
28393 * Description: This utility function is used by sr_read_mode2 to set the target
28394 * block size based on the user specified size. This is a legacy
28395 * implementation based upon a vendor specific mode page
28396 *
28397 * Arguments: dev - the device 'dev_t'
28398 * data - flag indicating if block size is being set to 2336 or
28399 * 512.
28400 *
28401 * Return Code: the code returned by sd_send_scsi_cmd()
28402 * EFAULT if ddi_copyxxx() fails
28403 * ENXIO if fail ddi_get_soft_state
28404 * EINVAL if data pointer is NULL
28405 */
28406
28407 static int
28408 sr_sector_mode(dev_t dev, uint32_t blksize)
28409 {
28410 struct sd_lun *un;
28411 uchar_t *sense;
28412 uchar_t *select;
28413 int rval;
28414 sd_ssc_t *ssc;
28415
28416 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28417 (un->un_state == SD_STATE_OFFLINE)) {
28418 return (ENXIO);
28419 }
28420
28421 sense = kmem_zalloc(20, KM_SLEEP);
28422
28423 /* Note: This is a vendor specific mode page (0x81) */
28424 ssc = sd_ssc_init(un);
28425 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28426 SD_PATH_STANDARD);
28427 sd_ssc_fini(ssc);
28428 if (rval != 0) {
28429 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28430 "sr_sector_mode: Mode Sense failed\n");
28431 kmem_free(sense, 20);
28432 return (rval);
28433 }
28434 select = kmem_zalloc(20, KM_SLEEP);
28435 select[3] = 0x08;
28436 select[10] = ((blksize >> 8) & 0xff);
28437 select[11] = (blksize & 0xff);
28438 select[12] = 0x01;
28439 select[13] = 0x06;
28440 select[14] = sense[14];
28441 select[15] = sense[15];
28442 if (blksize == SD_MODE2_BLKSIZE) {
28443 select[14] |= 0x01;
28444 }
28445
28446 ssc = sd_ssc_init(un);
28447 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28448 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28449 sd_ssc_fini(ssc);
28450 if (rval != 0) {
28451 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28452 "sr_sector_mode: Mode Select failed\n");
28453 } else {
28454 /*
28455 * Only update the softstate block size if we successfully
28456 * changed the device block mode.
28457 */
28458 mutex_enter(SD_MUTEX(un));
28459 sd_update_block_info(un, blksize, 0);
28460 mutex_exit(SD_MUTEX(un));
28461 }
28462 kmem_free(sense, 20);
28463 kmem_free(select, 20);
28464 return (rval);
28465 }
28466
28467
28468 /*
28469 * Function: sr_read_cdda()
28470 *
28471 * Description: This routine is the driver entry point for handling CD-ROM
28472 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28473 * the target supports CDDA these requests are handled via a vendor
28474 * specific command (0xD8) If the target does not support CDDA
28475 * these requests are handled via the READ CD command (0xBE).
28476 *
28477 * Arguments: dev - the device 'dev_t'
28478 * data - pointer to user provided CD-DA structure specifying
28479 * the track starting address, transfer length, and
28480 * subcode options.
28481 * flag - this argument is a pass through to ddi_copyxxx()
28482 * directly from the mode argument of ioctl().
28483 *
28484 * Return Code: the code returned by sd_send_scsi_cmd()
28485 * EFAULT if ddi_copyxxx() fails
28486 * ENXIO if fail ddi_get_soft_state
28487 * EINVAL if invalid arguments are provided
28488 * ENOTTY
28489 */
28490
28491 static int
28492 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28493 {
28494 struct sd_lun *un;
28495 struct uscsi_cmd *com;
28496 struct cdrom_cdda *cdda;
28497 int rval;
28498 size_t buflen;
28499 char cdb[CDB_GROUP5];
28500
28501 #ifdef _MULTI_DATAMODEL
28502 /* To support ILP32 applications in an LP64 world */
28503 struct cdrom_cdda32 cdrom_cdda32;
28504 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32;
28505 #endif /* _MULTI_DATAMODEL */
28506
28507 if (data == NULL) {
28508 return (EINVAL);
28509 }
28510
28511 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28512 return (ENXIO);
28513 }
28514
28515 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28516
28517 #ifdef _MULTI_DATAMODEL
28518 switch (ddi_model_convert_from(flag & FMODELS)) {
28519 case DDI_MODEL_ILP32:
28520 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28521 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28522 "sr_read_cdda: ddi_copyin Failed\n");
28523 kmem_free(cdda, sizeof (struct cdrom_cdda));
28524 return (EFAULT);
28525 }
28526 /* Convert the ILP32 uscsi data from the application to LP64 */
28527 cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28528 break;
28529 case DDI_MODEL_NONE:
28530 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28531 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28532 "sr_read_cdda: ddi_copyin Failed\n");
28533 kmem_free(cdda, sizeof (struct cdrom_cdda));
28534 return (EFAULT);
28535 }
28536 break;
28537 }
28538 #else /* ! _MULTI_DATAMODEL */
28539 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28540 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28541 "sr_read_cdda: ddi_copyin Failed\n");
28542 kmem_free(cdda, sizeof (struct cdrom_cdda));
28543 return (EFAULT);
28544 }
28545 #endif /* _MULTI_DATAMODEL */
28546
28547 /*
28548 * Since MMC-2 expects max 3 bytes for length, check if the
28549 * length input is greater than 3 bytes
28550 */
28551 if ((cdda->cdda_length & 0xFF000000) != 0) {
28552 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28553 "cdrom transfer length too large: %d (limit %d)\n",
28554 cdda->cdda_length, 0xFFFFFF);
28555 kmem_free(cdda, sizeof (struct cdrom_cdda));
28556 return (EINVAL);
28557 }
28558
28559 switch (cdda->cdda_subcode) {
28560 case CDROM_DA_NO_SUBCODE:
28561 buflen = CDROM_BLK_2352 * cdda->cdda_length;
28562 break;
28563 case CDROM_DA_SUBQ:
28564 buflen = CDROM_BLK_2368 * cdda->cdda_length;
28565 break;
28566 case CDROM_DA_ALL_SUBCODE:
28567 buflen = CDROM_BLK_2448 * cdda->cdda_length;
28568 break;
28569 case CDROM_DA_SUBCODE_ONLY:
28570 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28571 break;
28572 default:
28573 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28574 "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28575 cdda->cdda_subcode);
28576 kmem_free(cdda, sizeof (struct cdrom_cdda));
28577 return (EINVAL);
28578 }
28579
28580 /* Build and send the command */
28581 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28582 bzero(cdb, CDB_GROUP5);
28583
28584 if (un->un_f_cfg_cdda == TRUE) {
28585 cdb[0] = (char)SCMD_READ_CD;
28586 cdb[1] = 0x04;
28587 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28588 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28589 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28590 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28591 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28592 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28593 cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28594 cdb[9] = 0x10;
28595 switch (cdda->cdda_subcode) {
28596 case CDROM_DA_NO_SUBCODE :
28597 cdb[10] = 0x0;
28598 break;
28599 case CDROM_DA_SUBQ :
28600 cdb[10] = 0x2;
28601 break;
28602 case CDROM_DA_ALL_SUBCODE :
28603 cdb[10] = 0x1;
28604 break;
28605 case CDROM_DA_SUBCODE_ONLY :
28606 /* FALLTHROUGH */
28607 default :
28608 kmem_free(cdda, sizeof (struct cdrom_cdda));
28609 kmem_free(com, sizeof (*com));
28610 return (ENOTTY);
28611 }
28612 } else {
28613 cdb[0] = (char)SCMD_READ_CDDA;
28614 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28615 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28616 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28617 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28618 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28619 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28620 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28621 cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28622 cdb[10] = cdda->cdda_subcode;
28623 }
28624
28625 com->uscsi_cdb = cdb;
28626 com->uscsi_cdblen = CDB_GROUP5;
28627 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28628 com->uscsi_buflen = buflen;
28629 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28630
28631 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28632 SD_PATH_STANDARD);
28633
28634 kmem_free(cdda, sizeof (struct cdrom_cdda));
28635 kmem_free(com, sizeof (*com));
28636 return (rval);
28637 }
28638
28639
28640 /*
28641 * Function: sr_read_cdxa()
28642 *
28643 * Description: This routine is the driver entry point for handling CD-ROM
28644 * ioctl requests to return CD-XA (Extended Architecture) data.
28645 * (CDROMCDXA).
28646 *
28647 * Arguments: dev - the device 'dev_t'
28648 * data - pointer to user provided CD-XA structure specifying
28649 * the data starting address, transfer length, and format
28650 * flag - this argument is a pass through to ddi_copyxxx()
28651 * directly from the mode argument of ioctl().
28652 *
28653 * Return Code: the code returned by sd_send_scsi_cmd()
28654 * EFAULT if ddi_copyxxx() fails
28655 * ENXIO if fail ddi_get_soft_state
28656 * EINVAL if data pointer is NULL
28657 */
28658
28659 static int
28660 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28661 {
28662 struct sd_lun *un;
28663 struct uscsi_cmd *com;
28664 struct cdrom_cdxa *cdxa;
28665 int rval;
28666 size_t buflen;
28667 char cdb[CDB_GROUP5];
28668 uchar_t read_flags;
28669
28670 #ifdef _MULTI_DATAMODEL
28671 /* To support ILP32 applications in an LP64 world */
28672 struct cdrom_cdxa32 cdrom_cdxa32;
28673 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32;
28674 #endif /* _MULTI_DATAMODEL */
28675
28676 if (data == NULL) {
28677 return (EINVAL);
28678 }
28679
28680 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28681 return (ENXIO);
28682 }
28683
28684 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28685
28686 #ifdef _MULTI_DATAMODEL
28687 switch (ddi_model_convert_from(flag & FMODELS)) {
28688 case DDI_MODEL_ILP32:
28689 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28690 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28691 return (EFAULT);
28692 }
28693 /*
28694 * Convert the ILP32 uscsi data from the
28695 * application to LP64 for internal use.
28696 */
28697 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28698 break;
28699 case DDI_MODEL_NONE:
28700 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28701 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28702 return (EFAULT);
28703 }
28704 break;
28705 }
28706 #else /* ! _MULTI_DATAMODEL */
28707 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28708 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28709 return (EFAULT);
28710 }
28711 #endif /* _MULTI_DATAMODEL */
28712
28713 /*
28714 * Since MMC-2 expects max 3 bytes for length, check if the
28715 * length input is greater than 3 bytes
28716 */
28717 if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28718 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28719 "cdrom transfer length too large: %d (limit %d)\n",
28720 cdxa->cdxa_length, 0xFFFFFF);
28721 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28722 return (EINVAL);
28723 }
28724
28725 switch (cdxa->cdxa_format) {
28726 case CDROM_XA_DATA:
28727 buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28728 read_flags = 0x10;
28729 break;
28730 case CDROM_XA_SECTOR_DATA:
28731 buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28732 read_flags = 0xf8;
28733 break;
28734 case CDROM_XA_DATA_W_ERROR:
28735 buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28736 read_flags = 0xfc;
28737 break;
28738 default:
28739 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28740 "sr_read_cdxa: Format '0x%x' Not Supported\n",
28741 cdxa->cdxa_format);
28742 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28743 return (EINVAL);
28744 }
28745
28746 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28747 bzero(cdb, CDB_GROUP5);
28748 if (un->un_f_mmc_cap == TRUE) {
28749 cdb[0] = (char)SCMD_READ_CD;
28750 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28751 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28752 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28753 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28754 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28755 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28756 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28757 cdb[9] = (char)read_flags;
28758 } else {
28759 /*
28760 * Note: A vendor specific command (0xDB) is being used her to
28761 * request a read of all subcodes.
28762 */
28763 cdb[0] = (char)SCMD_READ_CDXA;
28764 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28765 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28766 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28767 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28768 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28769 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28770 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28771 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28772 cdb[10] = cdxa->cdxa_format;
28773 }
28774 com->uscsi_cdb = cdb;
28775 com->uscsi_cdblen = CDB_GROUP5;
28776 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28777 com->uscsi_buflen = buflen;
28778 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28779 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28780 SD_PATH_STANDARD);
28781 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28782 kmem_free(com, sizeof (*com));
28783 return (rval);
28784 }
28785
28786
28787 /*
28788 * Function: sr_eject()
28789 *
28790 * Description: This routine is the driver entry point for handling CD-ROM
28791 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28792 *
28793 * Arguments: dev - the device 'dev_t'
28794 *
28795 * Return Code: the code returned by sd_send_scsi_cmd()
28796 */
28797
28798 static int
28799 sr_eject(dev_t dev)
28800 {
28801 struct sd_lun *un;
28802 int rval;
28803 sd_ssc_t *ssc;
28804
28805 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28806 (un->un_state == SD_STATE_OFFLINE)) {
28807 return (ENXIO);
28808 }
28809
28810 /*
28811 * To prevent race conditions with the eject
28812 * command, keep track of an eject command as
28813 * it progresses. If we are already handling
28814 * an eject command in the driver for the given
28815 * unit and another request to eject is received
28816 * immediately return EAGAIN so we don't lose
28817 * the command if the current eject command fails.
28818 */
28819 mutex_enter(SD_MUTEX(un));
28820 if (un->un_f_ejecting == TRUE) {
28821 mutex_exit(SD_MUTEX(un));
28822 return (EAGAIN);
28823 }
28824 un->un_f_ejecting = TRUE;
28825 mutex_exit(SD_MUTEX(un));
28826
28827 ssc = sd_ssc_init(un);
28828 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28829 SD_PATH_STANDARD);
28830 sd_ssc_fini(ssc);
28831
28832 if (rval != 0) {
28833 mutex_enter(SD_MUTEX(un));
28834 un->un_f_ejecting = FALSE;
28835 mutex_exit(SD_MUTEX(un));
28836 return (rval);
28837 }
28838
28839 ssc = sd_ssc_init(un);
28840 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28841 SD_TARGET_EJECT, SD_PATH_STANDARD);
28842 sd_ssc_fini(ssc);
28843
28844 if (rval == 0) {
28845 mutex_enter(SD_MUTEX(un));
28846 sr_ejected(un);
28847 un->un_mediastate = DKIO_EJECTED;
28848 un->un_f_ejecting = FALSE;
28849 cv_broadcast(&un->un_state_cv);
28850 mutex_exit(SD_MUTEX(un));
28851 } else {
28852 mutex_enter(SD_MUTEX(un));
28853 un->un_f_ejecting = FALSE;
28854 mutex_exit(SD_MUTEX(un));
28855 }
28856 return (rval);
28857 }
28858
28859
28860 /*
28861 * Function: sr_ejected()
28862 *
28863 * Description: This routine updates the soft state structure to invalidate the
28864 * geometry information after the media has been ejected or a
28865 * media eject has been detected.
28866 *
28867 * Arguments: un - driver soft state (unit) structure
28868 */
28869
28870 static void
28871 sr_ejected(struct sd_lun *un)
28872 {
28873 struct sd_errstats *stp;
28874
28875 ASSERT(un != NULL);
28876 ASSERT(mutex_owned(SD_MUTEX(un)));
28877
28878 un->un_f_blockcount_is_valid = FALSE;
28879 un->un_f_tgt_blocksize_is_valid = FALSE;
28880 mutex_exit(SD_MUTEX(un));
28881 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28882 mutex_enter(SD_MUTEX(un));
28883
28884 if (un->un_errstats != NULL) {
28885 stp = (struct sd_errstats *)un->un_errstats->ks_data;
28886 stp->sd_capacity.value.ui64 = 0;
28887 }
28888 }
28889
28890
28891 /*
28892 * Function: sr_check_wp()
28893 *
28894 * Description: This routine checks the write protection of a removable
28895 * media disk and hotpluggable devices via the write protect bit of
28896 * the Mode Page Header device specific field. Some devices choke
28897 * on unsupported mode page. In order to workaround this issue,
28898 * this routine has been implemented to use 0x3f mode page(request
28899 * for all pages) for all device types.
28900 *
28901 * Arguments: dev - the device 'dev_t'
28902 *
28903 * Return Code: int indicating if the device is write protected (1) or not (0)
28904 *
28905 * Context: Kernel thread.
28906 *
28907 */
28908
28909 static int
28910 sr_check_wp(dev_t dev)
28911 {
28912 struct sd_lun *un;
28913 uchar_t device_specific;
28914 uchar_t *sense;
28915 int hdrlen;
28916 int rval = FALSE;
28917 int status;
28918 sd_ssc_t *ssc;
28919
28920 /*
28921 * Note: The return codes for this routine should be reworked to
28922 * properly handle the case of a NULL softstate.
28923 */
28924 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28925 return (FALSE);
28926 }
28927
28928 if (un->un_f_cfg_is_atapi == TRUE) {
28929 /*
28930 * The mode page contents are not required; set the allocation
28931 * length for the mode page header only
28932 */
28933 hdrlen = MODE_HEADER_LENGTH_GRP2;
28934 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28935 ssc = sd_ssc_init(un);
28936 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28937 MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28938 sd_ssc_fini(ssc);
28939 if (status != 0)
28940 goto err_exit;
28941 device_specific =
28942 ((struct mode_header_grp2 *)sense)->device_specific;
28943 } else {
28944 hdrlen = MODE_HEADER_LENGTH;
28945 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28946 ssc = sd_ssc_init(un);
28947 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28948 MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28949 sd_ssc_fini(ssc);
28950 if (status != 0)
28951 goto err_exit;
28952 device_specific =
28953 ((struct mode_header *)sense)->device_specific;
28954 }
28955
28956
28957 /*
28958 * Write protect mode sense failed; not all disks
28959 * understand this query. Return FALSE assuming that
28960 * these devices are not writable.
28961 */
28962 if (device_specific & WRITE_PROTECT) {
28963 rval = TRUE;
28964 }
28965
28966 err_exit:
28967 kmem_free(sense, hdrlen);
28968 return (rval);
28969 }
28970
28971 /*
28972 * Function: sr_volume_ctrl()
28973 *
28974 * Description: This routine is the driver entry point for handling CD-ROM
28975 * audio output volume ioctl requests. (CDROMVOLCTRL)
28976 *
28977 * Arguments: dev - the device 'dev_t'
28978 * data - pointer to user audio volume control structure
28979 * flag - this argument is a pass through to ddi_copyxxx()
28980 * directly from the mode argument of ioctl().
28981 *
28982 * Return Code: the code returned by sd_send_scsi_cmd()
28983 * EFAULT if ddi_copyxxx() fails
28984 * ENXIO if fail ddi_get_soft_state
28985 * EINVAL if data pointer is NULL
28986 *
28987 */
28988
28989 static int
28990 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28991 {
28992 struct sd_lun *un;
28993 struct cdrom_volctrl volume;
28994 struct cdrom_volctrl *vol = &volume;
28995 uchar_t *sense_page;
28996 uchar_t *select_page;
28997 uchar_t *sense;
28998 uchar_t *select;
28999 int sense_buflen;
29000 int select_buflen;
29001 int rval;
29002 sd_ssc_t *ssc;
29003
29004 if (data == NULL) {
29005 return (EINVAL);
29006 }
29007
29008 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29009 (un->un_state == SD_STATE_OFFLINE)) {
29010 return (ENXIO);
29011 }
29012
29013 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29014 return (EFAULT);
29015 }
29016
29017 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29018 struct mode_header_grp2 *sense_mhp;
29019 struct mode_header_grp2 *select_mhp;
29020 int bd_len;
29021
29022 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29023 select_buflen = MODE_HEADER_LENGTH_GRP2 +
29024 MODEPAGE_AUDIO_CTRL_LEN;
29025 sense = kmem_zalloc(sense_buflen, KM_SLEEP);
29026 select = kmem_zalloc(select_buflen, KM_SLEEP);
29027 ssc = sd_ssc_init(un);
29028 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29029 sense_buflen, MODEPAGE_AUDIO_CTRL,
29030 SD_PATH_STANDARD);
29031 sd_ssc_fini(ssc);
29032
29033 if (rval != 0) {
29034 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29035 "sr_volume_ctrl: Mode Sense Failed\n");
29036 kmem_free(sense, sense_buflen);
29037 kmem_free(select, select_buflen);
29038 return (rval);
29039 }
29040 sense_mhp = (struct mode_header_grp2 *)sense;
29041 select_mhp = (struct mode_header_grp2 *)select;
29042 bd_len = (sense_mhp->bdesc_length_hi << 8) |
29043 sense_mhp->bdesc_length_lo;
29044 if (bd_len > MODE_BLK_DESC_LENGTH) {
29045 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29046 "sr_volume_ctrl: Mode Sense returned invalid "
29047 "block descriptor length\n");
29048 kmem_free(sense, sense_buflen);
29049 kmem_free(select, select_buflen);
29050 return (EIO);
29051 }
29052 sense_page = (uchar_t *)
29053 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29054 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29055 select_mhp->length_msb = 0;
29056 select_mhp->length_lsb = 0;
29057 select_mhp->bdesc_length_hi = 0;
29058 select_mhp->bdesc_length_lo = 0;
29059 } else {
29060 struct mode_header *sense_mhp, *select_mhp;
29061
29062 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29063 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29064 sense = kmem_zalloc(sense_buflen, KM_SLEEP);
29065 select = kmem_zalloc(select_buflen, KM_SLEEP);
29066 ssc = sd_ssc_init(un);
29067 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29068 sense_buflen, MODEPAGE_AUDIO_CTRL,
29069 SD_PATH_STANDARD);
29070 sd_ssc_fini(ssc);
29071
29072 if (rval != 0) {
29073 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29074 "sr_volume_ctrl: Mode Sense Failed\n");
29075 kmem_free(sense, sense_buflen);
29076 kmem_free(select, select_buflen);
29077 return (rval);
29078 }
29079 sense_mhp = (struct mode_header *)sense;
29080 select_mhp = (struct mode_header *)select;
29081 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29082 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29083 "sr_volume_ctrl: Mode Sense returned invalid "
29084 "block descriptor length\n");
29085 kmem_free(sense, sense_buflen);
29086 kmem_free(select, select_buflen);
29087 return (EIO);
29088 }
29089 sense_page = (uchar_t *)
29090 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29091 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29092 select_mhp->length = 0;
29093 select_mhp->bdesc_length = 0;
29094 }
29095 /*
29096 * Note: An audio control data structure could be created and overlayed
29097 * on the following in place of the array indexing method implemented.
29098 */
29099
29100 /* Build the select data for the user volume data */
29101 select_page[0] = MODEPAGE_AUDIO_CTRL;
29102 select_page[1] = 0xE;
29103 /* Set the immediate bit */
29104 select_page[2] = 0x04;
29105 /* Zero out reserved fields */
29106 select_page[3] = 0x00;
29107 select_page[4] = 0x00;
29108 /* Return sense data for fields not to be modified */
29109 select_page[5] = sense_page[5];
29110 select_page[6] = sense_page[6];
29111 select_page[7] = sense_page[7];
29112 /* Set the user specified volume levels for channel 0 and 1 */
29113 select_page[8] = 0x01;
29114 select_page[9] = vol->channel0;
29115 select_page[10] = 0x02;
29116 select_page[11] = vol->channel1;
29117 /* Channel 2 and 3 are currently unsupported so return the sense data */
29118 select_page[12] = sense_page[12];
29119 select_page[13] = sense_page[13];
29120 select_page[14] = sense_page[14];
29121 select_page[15] = sense_page[15];
29122
29123 ssc = sd_ssc_init(un);
29124 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29125 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29126 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29127 } else {
29128 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29129 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29130 }
29131 sd_ssc_fini(ssc);
29132
29133 kmem_free(sense, sense_buflen);
29134 kmem_free(select, select_buflen);
29135 return (rval);
29136 }
29137
29138
29139 /*
29140 * Function: sr_read_sony_session_offset()
29141 *
29142 * Description: This routine is the driver entry point for handling CD-ROM
29143 * ioctl requests for session offset information. (CDROMREADOFFSET)
29144 * The address of the first track in the last session of a
29145 * multi-session CD-ROM is returned
29146 *
29147 * Note: This routine uses a vendor specific key value in the
29148 * command control field without implementing any vendor check here
29149 * or in the ioctl routine.
29150 *
29151 * Arguments: dev - the device 'dev_t'
29152 * data - pointer to an int to hold the requested address
29153 * flag - this argument is a pass through to ddi_copyxxx()
29154 * directly from the mode argument of ioctl().
29155 *
29156 * Return Code: the code returned by sd_send_scsi_cmd()
29157 * EFAULT if ddi_copyxxx() fails
29158 * ENXIO if fail ddi_get_soft_state
29159 * EINVAL if data pointer is NULL
29160 */
29161
29162 static int
29163 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29164 {
29165 struct sd_lun *un;
29166 struct uscsi_cmd *com;
29167 caddr_t buffer;
29168 char cdb[CDB_GROUP1];
29169 int session_offset = 0;
29170 int rval;
29171
29172 if (data == NULL) {
29173 return (EINVAL);
29174 }
29175
29176 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29177 (un->un_state == SD_STATE_OFFLINE)) {
29178 return (ENXIO);
29179 }
29180
29181 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29182 bzero(cdb, CDB_GROUP1);
29183 cdb[0] = SCMD_READ_TOC;
29184 /*
29185 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29186 * (4 byte TOC response header + 8 byte response data)
29187 */
29188 cdb[8] = SONY_SESSION_OFFSET_LEN;
29189 /* Byte 9 is the control byte. A vendor specific value is used */
29190 cdb[9] = SONY_SESSION_OFFSET_KEY;
29191 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29192 com->uscsi_cdb = cdb;
29193 com->uscsi_cdblen = CDB_GROUP1;
29194 com->uscsi_bufaddr = buffer;
29195 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29196 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29197
29198 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29199 SD_PATH_STANDARD);
29200 if (rval != 0) {
29201 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29202 kmem_free(com, sizeof (*com));
29203 return (rval);
29204 }
29205 if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29206 session_offset =
29207 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29208 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29209 /*
29210 * Offset returned offset in current lbasize block's. Convert to
29211 * 2k block's to return to the user
29212 */
29213 if (un->un_tgt_blocksize == CDROM_BLK_512) {
29214 session_offset >>= 2;
29215 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29216 session_offset >>= 1;
29217 }
29218 }
29219
29220 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29221 rval = EFAULT;
29222 }
29223
29224 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29225 kmem_free(com, sizeof (*com));
29226 return (rval);
29227 }
29228
29229
29230 /*
29231 * Function: sd_wm_cache_constructor()
29232 *
29233 * Description: Cache Constructor for the wmap cache for the read/modify/write
29234 * devices.
29235 *
29236 * Arguments: wm - A pointer to the sd_w_map to be initialized.
29237 * un - sd_lun structure for the device.
29238 * flag - the km flags passed to constructor
29239 *
29240 * Return Code: 0 on success.
29241 * -1 on failure.
29242 */
29243
29244 /*ARGSUSED*/
29245 static int
29246 sd_wm_cache_constructor(void *wm, void *un, int flags)
29247 {
29248 bzero(wm, sizeof (struct sd_w_map));
29249 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29250 return (0);
29251 }
29252
29253
29254 /*
29255 * Function: sd_wm_cache_destructor()
29256 *
29257 * Description: Cache destructor for the wmap cache for the read/modify/write
29258 * devices.
29259 *
29260 * Arguments: wm - A pointer to the sd_w_map to be initialized.
29261 * un - sd_lun structure for the device.
29262 */
29263 /*ARGSUSED*/
29264 static void
29265 sd_wm_cache_destructor(void *wm, void *un)
29266 {
29267 cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29268 }
29269
29270
29271 /*
29272 * Function: sd_range_lock()
29273 *
29274 * Description: Lock the range of blocks specified as parameter to ensure
29275 * that read, modify write is atomic and no other i/o writes
29276 * to the same location. The range is specified in terms
29277 * of start and end blocks. Block numbers are the actual
29278 * media block numbers and not system.
29279 *
29280 * Arguments: un - sd_lun structure for the device.
29281 * startb - The starting block number
29282 * endb - The end block number
29283 * typ - type of i/o - simple/read_modify_write
29284 *
29285 * Return Code: wm - pointer to the wmap structure.
29286 *
29287 * Context: This routine can sleep.
29288 */
29289
29290 static struct sd_w_map *
29291 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29292 {
29293 struct sd_w_map *wmp = NULL;
29294 struct sd_w_map *sl_wmp = NULL;
29295 struct sd_w_map *tmp_wmp;
29296 wm_state state = SD_WM_CHK_LIST;
29297
29298
29299 ASSERT(un != NULL);
29300 ASSERT(!mutex_owned(SD_MUTEX(un)));
29301
29302 mutex_enter(SD_MUTEX(un));
29303
29304 while (state != SD_WM_DONE) {
29305
29306 switch (state) {
29307 case SD_WM_CHK_LIST:
29308 /*
29309 * This is the starting state. Check the wmap list
29310 * to see if the range is currently available.
29311 */
29312 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29313 /*
29314 * If this is a simple write and no rmw
29315 * i/o is pending then try to lock the
29316 * range as the range should be available.
29317 */
29318 state = SD_WM_LOCK_RANGE;
29319 } else {
29320 tmp_wmp = sd_get_range(un, startb, endb);
29321 if (tmp_wmp != NULL) {
29322 if ((wmp != NULL) && ONLIST(un, wmp)) {
29323 /*
29324 * Should not keep onlist wmps
29325 * while waiting this macro
29326 * will also do wmp = NULL;
29327 */
29328 FREE_ONLIST_WMAP(un, wmp);
29329 }
29330 /*
29331 * sl_wmp is the wmap on which wait
29332 * is done, since the tmp_wmp points
29333 * to the inuse wmap, set sl_wmp to
29334 * tmp_wmp and change the state to sleep
29335 */
29336 sl_wmp = tmp_wmp;
29337 state = SD_WM_WAIT_MAP;
29338 } else {
29339 state = SD_WM_LOCK_RANGE;
29340 }
29341
29342 }
29343 break;
29344
29345 case SD_WM_LOCK_RANGE:
29346 ASSERT(un->un_wm_cache);
29347 /*
29348 * The range need to be locked, try to get a wmap.
29349 * First attempt it with NO_SLEEP, want to avoid a sleep
29350 * if possible as we will have to release the sd mutex
29351 * if we have to sleep.
29352 */
29353 if (wmp == NULL)
29354 wmp = kmem_cache_alloc(un->un_wm_cache,
29355 KM_NOSLEEP);
29356 if (wmp == NULL) {
29357 mutex_exit(SD_MUTEX(un));
29358 _NOTE(DATA_READABLE_WITHOUT_LOCK
29359 (sd_lun::un_wm_cache))
29360 wmp = kmem_cache_alloc(un->un_wm_cache,
29361 KM_SLEEP);
29362 mutex_enter(SD_MUTEX(un));
29363 /*
29364 * we released the mutex so recheck and go to
29365 * check list state.
29366 */
29367 state = SD_WM_CHK_LIST;
29368 } else {
29369 /*
29370 * We exit out of state machine since we
29371 * have the wmap. Do the housekeeping first.
29372 * place the wmap on the wmap list if it is not
29373 * on it already and then set the state to done.
29374 */
29375 wmp->wm_start = startb;
29376 wmp->wm_end = endb;
29377 wmp->wm_flags = typ | SD_WM_BUSY;
29378 if (typ & SD_WTYPE_RMW) {
29379 un->un_rmw_count++;
29380 }
29381 /*
29382 * If not already on the list then link
29383 */
29384 if (!ONLIST(un, wmp)) {
29385 wmp->wm_next = un->un_wm;
29386 wmp->wm_prev = NULL;
29387 if (wmp->wm_next)
29388 wmp->wm_next->wm_prev = wmp;
29389 un->un_wm = wmp;
29390 }
29391 state = SD_WM_DONE;
29392 }
29393 break;
29394
29395 case SD_WM_WAIT_MAP:
29396 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29397 /*
29398 * Wait is done on sl_wmp, which is set in the
29399 * check_list state.
29400 */
29401 sl_wmp->wm_wanted_count++;
29402 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29403 sl_wmp->wm_wanted_count--;
29404 /*
29405 * We can reuse the memory from the completed sl_wmp
29406 * lock range for our new lock, but only if noone is
29407 * waiting for it.
29408 */
29409 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29410 if (sl_wmp->wm_wanted_count == 0) {
29411 if (wmp != NULL)
29412 CHK_N_FREEWMP(un, wmp);
29413 wmp = sl_wmp;
29414 }
29415 sl_wmp = NULL;
29416 /*
29417 * After waking up, need to recheck for availability of
29418 * range.
29419 */
29420 state = SD_WM_CHK_LIST;
29421 break;
29422
29423 default:
29424 panic("sd_range_lock: "
29425 "Unknown state %d in sd_range_lock", state);
29426 /*NOTREACHED*/
29427 } /* switch(state) */
29428
29429 } /* while(state != SD_WM_DONE) */
29430
29431 mutex_exit(SD_MUTEX(un));
29432
29433 ASSERT(wmp != NULL);
29434
29435 return (wmp);
29436 }
29437
29438
29439 /*
29440 * Function: sd_get_range()
29441 *
29442 * Description: Find if there any overlapping I/O to this one
29443 * Returns the write-map of 1st such I/O, NULL otherwise.
29444 *
29445 * Arguments: un - sd_lun structure for the device.
29446 * startb - The starting block number
29447 * endb - The end block number
29448 *
29449 * Return Code: wm - pointer to the wmap structure.
29450 */
29451
29452 static struct sd_w_map *
29453 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29454 {
29455 struct sd_w_map *wmp;
29456
29457 ASSERT(un != NULL);
29458
29459 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29460 if (!(wmp->wm_flags & SD_WM_BUSY)) {
29461 continue;
29462 }
29463 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29464 break;
29465 }
29466 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29467 break;
29468 }
29469 }
29470
29471 return (wmp);
29472 }
29473
29474
29475 /*
29476 * Function: sd_free_inlist_wmap()
29477 *
29478 * Description: Unlink and free a write map struct.
29479 *
29480 * Arguments: un - sd_lun structure for the device.
29481 * wmp - sd_w_map which needs to be unlinked.
29482 */
29483
29484 static void
29485 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29486 {
29487 ASSERT(un != NULL);
29488
29489 if (un->un_wm == wmp) {
29490 un->un_wm = wmp->wm_next;
29491 } else {
29492 wmp->wm_prev->wm_next = wmp->wm_next;
29493 }
29494
29495 if (wmp->wm_next) {
29496 wmp->wm_next->wm_prev = wmp->wm_prev;
29497 }
29498
29499 wmp->wm_next = wmp->wm_prev = NULL;
29500
29501 kmem_cache_free(un->un_wm_cache, wmp);
29502 }
29503
29504
29505 /*
29506 * Function: sd_range_unlock()
29507 *
29508 * Description: Unlock the range locked by wm.
29509 * Free write map if nobody else is waiting on it.
29510 *
29511 * Arguments: un - sd_lun structure for the device.
29512 * wmp - sd_w_map which needs to be unlinked.
29513 */
29514
29515 static void
29516 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29517 {
29518 ASSERT(un != NULL);
29519 ASSERT(wm != NULL);
29520 ASSERT(!mutex_owned(SD_MUTEX(un)));
29521
29522 mutex_enter(SD_MUTEX(un));
29523
29524 if (wm->wm_flags & SD_WTYPE_RMW) {
29525 un->un_rmw_count--;
29526 }
29527
29528 if (wm->wm_wanted_count) {
29529 wm->wm_flags = 0;
29530 /*
29531 * Broadcast that the wmap is available now.
29532 */
29533 cv_broadcast(&wm->wm_avail);
29534 } else {
29535 /*
29536 * If no one is waiting on the map, it should be free'ed.
29537 */
29538 sd_free_inlist_wmap(un, wm);
29539 }
29540
29541 mutex_exit(SD_MUTEX(un));
29542 }
29543
29544
29545 /*
29546 * Function: sd_read_modify_write_task
29547 *
29548 * Description: Called from a taskq thread to initiate the write phase of
29549 * a read-modify-write request. This is used for targets where
29550 * un->un_sys_blocksize != un->un_tgt_blocksize.
29551 *
29552 * Arguments: arg - a pointer to the buf(9S) struct for the write command.
29553 *
29554 * Context: Called under taskq thread context.
29555 */
29556
29557 static void
29558 sd_read_modify_write_task(void *arg)
29559 {
29560 struct sd_mapblocksize_info *bsp;
29561 struct buf *bp;
29562 struct sd_xbuf *xp;
29563 struct sd_lun *un;
29564
29565 bp = arg; /* The bp is given in arg */
29566 ASSERT(bp != NULL);
29567
29568 /* Get the pointer to the layer-private data struct */
29569 xp = SD_GET_XBUF(bp);
29570 ASSERT(xp != NULL);
29571 bsp = xp->xb_private;
29572 ASSERT(bsp != NULL);
29573
29574 un = SD_GET_UN(bp);
29575 ASSERT(un != NULL);
29576 ASSERT(!mutex_owned(SD_MUTEX(un)));
29577
29578 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29579 "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29580
29581 /*
29582 * This is the write phase of a read-modify-write request, called
29583 * under the context of a taskq thread in response to the completion
29584 * of the read portion of the rmw request completing under interrupt
29585 * context. The write request must be sent from here down the iostart
29586 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29587 * we use the layer index saved in the layer-private data area.
29588 */
29589 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29590
29591 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29592 "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29593 }
29594
29595
29596 /*
29597 * Function: sddump_do_read_of_rmw()
29598 *
29599 * Description: This routine will be called from sddump, If sddump is called
29600 * with an I/O which not aligned on device blocksize boundary
29601 * then the write has to be converted to read-modify-write.
29602 * Do the read part here in order to keep sddump simple.
29603 * Note - That the sd_mutex is held across the call to this
29604 * routine.
29605 *
29606 * Arguments: un - sd_lun
29607 * blkno - block number in terms of media block size.
29608 * nblk - number of blocks.
29609 * bpp - pointer to pointer to the buf structure. On return
29610 * from this function, *bpp points to the valid buffer
29611 * to which the write has to be done.
29612 *
29613 * Return Code: 0 for success or errno-type return code
29614 */
29615
29616 static int
29617 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29618 struct buf **bpp)
29619 {
29620 int err;
29621 int i;
29622 int rval;
29623 struct buf *bp;
29624 struct scsi_pkt *pkt = NULL;
29625 uint32_t target_blocksize;
29626
29627 ASSERT(un != NULL);
29628 ASSERT(mutex_owned(SD_MUTEX(un)));
29629
29630 target_blocksize = un->un_tgt_blocksize;
29631
29632 mutex_exit(SD_MUTEX(un));
29633
29634 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29635 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29636 if (bp == NULL) {
29637 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29638 "no resources for dumping; giving up");
29639 err = ENOMEM;
29640 goto done;
29641 }
29642
29643 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29644 blkno, nblk);
29645 if (rval != 0) {
29646 scsi_free_consistent_buf(bp);
29647 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29648 "no resources for dumping; giving up");
29649 err = ENOMEM;
29650 goto done;
29651 }
29652
29653 pkt->pkt_flags |= FLAG_NOINTR;
29654
29655 err = EIO;
29656 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29657
29658 /*
29659 * Scsi_poll returns 0 (success) if the command completes and
29660 * the status block is STATUS_GOOD. We should only check
29661 * errors if this condition is not true. Even then we should
29662 * send our own request sense packet only if we have a check
29663 * condition and auto request sense has not been performed by
29664 * the hba.
29665 */
29666 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29667
29668 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29669 err = 0;
29670 break;
29671 }
29672
29673 /*
29674 * Check CMD_DEV_GONE 1st, give up if device is gone,
29675 * no need to read RQS data.
29676 */
29677 if (pkt->pkt_reason == CMD_DEV_GONE) {
29678 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29679 "Error while dumping state with rmw..."
29680 "Device is gone\n");
29681 break;
29682 }
29683
29684 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29685 SD_INFO(SD_LOG_DUMP, un,
29686 "sddump: read failed with CHECK, try # %d\n", i);
29687 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29688 (void) sd_send_polled_RQS(un);
29689 }
29690
29691 continue;
29692 }
29693
29694 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29695 int reset_retval = 0;
29696
29697 SD_INFO(SD_LOG_DUMP, un,
29698 "sddump: read failed with BUSY, try # %d\n", i);
29699
29700 if (un->un_f_lun_reset_enabled == TRUE) {
29701 reset_retval = scsi_reset(SD_ADDRESS(un),
29702 RESET_LUN);
29703 }
29704 if (reset_retval == 0) {
29705 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29706 }
29707 (void) sd_send_polled_RQS(un);
29708
29709 } else {
29710 SD_INFO(SD_LOG_DUMP, un,
29711 "sddump: read failed with 0x%x, try # %d\n",
29712 SD_GET_PKT_STATUS(pkt), i);
29713 mutex_enter(SD_MUTEX(un));
29714 sd_reset_target(un, pkt);
29715 mutex_exit(SD_MUTEX(un));
29716 }
29717
29718 /*
29719 * If we are not getting anywhere with lun/target resets,
29720 * let's reset the bus.
29721 */
29722 if (i > SD_NDUMP_RETRIES/2) {
29723 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29724 (void) sd_send_polled_RQS(un);
29725 }
29726
29727 }
29728 scsi_destroy_pkt(pkt);
29729
29730 if (err != 0) {
29731 scsi_free_consistent_buf(bp);
29732 *bpp = NULL;
29733 } else {
29734 *bpp = bp;
29735 }
29736
29737 done:
29738 mutex_enter(SD_MUTEX(un));
29739 return (err);
29740 }
29741
29742
29743 /*
29744 * Function: sd_failfast_flushq
29745 *
29746 * Description: Take all bp's on the wait queue that have B_FAILFAST set
29747 * in b_flags and move them onto the failfast queue, then kick
29748 * off a thread to return all bp's on the failfast queue to
29749 * their owners with an error set.
29750 *
29751 * Arguments: un - pointer to the soft state struct for the instance.
29752 *
29753 * Context: may execute in interrupt context.
29754 */
29755
29756 static void
29757 sd_failfast_flushq(struct sd_lun *un)
29758 {
29759 struct buf *bp;
29760 struct buf *next_waitq_bp;
29761 struct buf *prev_waitq_bp = NULL;
29762
29763 ASSERT(un != NULL);
29764 ASSERT(mutex_owned(SD_MUTEX(un)));
29765 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29766 ASSERT(un->un_failfast_bp == NULL);
29767
29768 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29769 "sd_failfast_flushq: entry: un:0x%p\n", un);
29770
29771 /*
29772 * Check if we should flush all bufs when entering failfast state, or
29773 * just those with B_FAILFAST set.
29774 */
29775 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29776 /*
29777 * Move *all* bp's on the wait queue to the failfast flush
29778 * queue, including those that do NOT have B_FAILFAST set.
29779 */
29780 if (un->un_failfast_headp == NULL) {
29781 ASSERT(un->un_failfast_tailp == NULL);
29782 un->un_failfast_headp = un->un_waitq_headp;
29783 } else {
29784 ASSERT(un->un_failfast_tailp != NULL);
29785 un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29786 }
29787
29788 un->un_failfast_tailp = un->un_waitq_tailp;
29789
29790 /* update kstat for each bp moved out of the waitq */
29791 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29792 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29793 }
29794
29795 /* empty the waitq */
29796 un->un_waitq_headp = un->un_waitq_tailp = NULL;
29797
29798 } else {
29799 /*
29800 * Go thru the wait queue, pick off all entries with
29801 * B_FAILFAST set, and move these onto the failfast queue.
29802 */
29803 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29804 /*
29805 * Save the pointer to the next bp on the wait queue,
29806 * so we get to it on the next iteration of this loop.
29807 */
29808 next_waitq_bp = bp->av_forw;
29809
29810 /*
29811 * If this bp from the wait queue does NOT have
29812 * B_FAILFAST set, just move on to the next element
29813 * in the wait queue. Note, this is the only place
29814 * where it is correct to set prev_waitq_bp.
29815 */
29816 if ((bp->b_flags & B_FAILFAST) == 0) {
29817 prev_waitq_bp = bp;
29818 continue;
29819 }
29820
29821 /*
29822 * Remove the bp from the wait queue.
29823 */
29824 if (bp == un->un_waitq_headp) {
29825 /* The bp is the first element of the waitq. */
29826 un->un_waitq_headp = next_waitq_bp;
29827 if (un->un_waitq_headp == NULL) {
29828 /* The wait queue is now empty */
29829 un->un_waitq_tailp = NULL;
29830 }
29831 } else {
29832 /*
29833 * The bp is either somewhere in the middle
29834 * or at the end of the wait queue.
29835 */
29836 ASSERT(un->un_waitq_headp != NULL);
29837 ASSERT(prev_waitq_bp != NULL);
29838 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29839 == 0);
29840 if (bp == un->un_waitq_tailp) {
29841 /* bp is the last entry on the waitq. */
29842 ASSERT(next_waitq_bp == NULL);
29843 un->un_waitq_tailp = prev_waitq_bp;
29844 }
29845 prev_waitq_bp->av_forw = next_waitq_bp;
29846 }
29847 bp->av_forw = NULL;
29848
29849 /*
29850 * update kstat since the bp is moved out of
29851 * the waitq
29852 */
29853 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29854
29855 /*
29856 * Now put the bp onto the failfast queue.
29857 */
29858 if (un->un_failfast_headp == NULL) {
29859 /* failfast queue is currently empty */
29860 ASSERT(un->un_failfast_tailp == NULL);
29861 un->un_failfast_headp =
29862 un->un_failfast_tailp = bp;
29863 } else {
29864 /* Add the bp to the end of the failfast q */
29865 ASSERT(un->un_failfast_tailp != NULL);
29866 ASSERT(un->un_failfast_tailp->b_flags &
29867 B_FAILFAST);
29868 un->un_failfast_tailp->av_forw = bp;
29869 un->un_failfast_tailp = bp;
29870 }
29871 }
29872 }
29873
29874 /*
29875 * Now return all bp's on the failfast queue to their owners.
29876 */
29877 while ((bp = un->un_failfast_headp) != NULL) {
29878
29879 un->un_failfast_headp = bp->av_forw;
29880 if (un->un_failfast_headp == NULL) {
29881 un->un_failfast_tailp = NULL;
29882 }
29883
29884 /*
29885 * We want to return the bp with a failure error code, but
29886 * we do not want a call to sd_start_cmds() to occur here,
29887 * so use sd_return_failed_command_no_restart() instead of
29888 * sd_return_failed_command().
29889 */
29890 sd_return_failed_command_no_restart(un, bp, EIO);
29891 }
29892
29893 /* Flush the xbuf queues if required. */
29894 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29895 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29896 }
29897
29898 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29899 "sd_failfast_flushq: exit: un:0x%p\n", un);
29900 }
29901
29902
29903 /*
29904 * Function: sd_failfast_flushq_callback
29905 *
29906 * Description: Return TRUE if the given bp meets the criteria for failfast
29907 * flushing. Used with ddi_xbuf_flushq(9F).
29908 *
29909 * Arguments: bp - ptr to buf struct to be examined.
29910 *
29911 * Context: Any
29912 */
29913
29914 static int
29915 sd_failfast_flushq_callback(struct buf *bp)
29916 {
29917 /*
29918 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29919 * state is entered; OR (2) the given bp has B_FAILFAST set.
29920 */
29921 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29922 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29923 }
29924
29925
29926
29927 /*
29928 * Function: sd_setup_next_xfer
29929 *
29930 * Description: Prepare next I/O operation using DMA_PARTIAL
29931 *
29932 */
29933
29934 static int
29935 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29936 struct scsi_pkt *pkt, struct sd_xbuf *xp)
29937 {
29938 ssize_t num_blks_not_xfered;
29939 daddr_t strt_blk_num;
29940 ssize_t bytes_not_xfered;
29941 int rval;
29942
29943 ASSERT(pkt->pkt_resid == 0);
29944
29945 /*
29946 * Calculate next block number and amount to be transferred.
29947 *
29948 * How much data NOT transfered to the HBA yet.
29949 */
29950 bytes_not_xfered = xp->xb_dma_resid;
29951
29952 /*
29953 * figure how many blocks NOT transfered to the HBA yet.
29954 */
29955 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29956
29957 /*
29958 * set starting block number to the end of what WAS transfered.
29959 */
29960 strt_blk_num = xp->xb_blkno +
29961 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29962
29963 /*
29964 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt
29965 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29966 * the disk mutex here.
29967 */
29968 rval = sd_setup_next_rw_pkt(un, pkt, bp,
29969 strt_blk_num, num_blks_not_xfered);
29970
29971 if (rval == 0) {
29972
29973 /*
29974 * Success.
29975 *
29976 * Adjust things if there are still more blocks to be
29977 * transfered.
29978 */
29979 xp->xb_dma_resid = pkt->pkt_resid;
29980 pkt->pkt_resid = 0;
29981
29982 return (1);
29983 }
29984
29985 /*
29986 * There's really only one possible return value from
29987 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29988 * returns NULL.
29989 */
29990 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29991
29992 bp->b_resid = bp->b_bcount;
29993 bp->b_flags |= B_ERROR;
29994
29995 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29996 "Error setting up next portion of DMA transfer\n");
29997
29998 return (0);
29999 }
30000
30001 /*
30002 * Function: sd_panic_for_res_conflict
30003 *
30004 * Description: Call panic with a string formatted with "Reservation Conflict"
30005 * and a human readable identifier indicating the SD instance
30006 * that experienced the reservation conflict.
30007 *
30008 * Arguments: un - pointer to the soft state struct for the instance.
30009 *
30010 * Context: may execute in interrupt context.
30011 */
30012
30013 #define SD_RESV_CONFLICT_FMT_LEN 40
30014 void
30015 sd_panic_for_res_conflict(struct sd_lun *un)
30016 {
30017 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30018 char path_str[MAXPATHLEN];
30019
30020 (void) snprintf(panic_str, sizeof (panic_str),
30021 "Reservation Conflict\nDisk: %s",
30022 ddi_pathname(SD_DEVINFO(un), path_str));
30023
30024 panic(panic_str);
30025 }
30026
30027 /*
30028 * Note: The following sd_faultinjection_ioctl( ) routines implement
30029 * driver support for handling fault injection for error analysis
30030 * causing faults in multiple layers of the driver.
30031 *
30032 */
30033
30034 #ifdef SD_FAULT_INJECTION
30035 static uint_t sd_fault_injection_on = 0;
30036
30037 /*
30038 * Function: sd_faultinjection_ioctl()
30039 *
30040 * Description: This routine is the driver entry point for handling
30041 * faultinjection ioctls to inject errors into the
30042 * layer model
30043 *
30044 * Arguments: cmd - the ioctl cmd received
30045 * arg - the arguments from user and returns
30046 */
30047
30048 static void
30049 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) {
30050
30051 uint_t i = 0;
30052 uint_t rval;
30053
30054 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30055
30056 mutex_enter(SD_MUTEX(un));
30057
30058 switch (cmd) {
30059 case SDIOCRUN:
30060 /* Allow pushed faults to be injected */
30061 SD_INFO(SD_LOG_SDTEST, un,
30062 "sd_faultinjection_ioctl: Injecting Fault Run\n");
30063
30064 sd_fault_injection_on = 1;
30065
30066 SD_INFO(SD_LOG_IOERR, un,
30067 "sd_faultinjection_ioctl: run finished\n");
30068 break;
30069
30070 case SDIOCSTART:
30071 /* Start Injection Session */
30072 SD_INFO(SD_LOG_SDTEST, un,
30073 "sd_faultinjection_ioctl: Injecting Fault Start\n");
30074
30075 sd_fault_injection_on = 0;
30076 un->sd_injection_mask = 0xFFFFFFFF;
30077 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30078 un->sd_fi_fifo_pkt[i] = NULL;
30079 un->sd_fi_fifo_xb[i] = NULL;
30080 un->sd_fi_fifo_un[i] = NULL;
30081 un->sd_fi_fifo_arq[i] = NULL;
30082 }
30083 un->sd_fi_fifo_start = 0;
30084 un->sd_fi_fifo_end = 0;
30085
30086 mutex_enter(&(un->un_fi_mutex));
30087 un->sd_fi_log[0] = '\0';
30088 un->sd_fi_buf_len = 0;
30089 mutex_exit(&(un->un_fi_mutex));
30090
30091 SD_INFO(SD_LOG_IOERR, un,
30092 "sd_faultinjection_ioctl: start finished\n");
30093 break;
30094
30095 case SDIOCSTOP:
30096 /* Stop Injection Session */
30097 SD_INFO(SD_LOG_SDTEST, un,
30098 "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30099 sd_fault_injection_on = 0;
30100 un->sd_injection_mask = 0x0;
30101
30102 /* Empty stray or unuseds structs from fifo */
30103 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30104 if (un->sd_fi_fifo_pkt[i] != NULL) {
30105 kmem_free(un->sd_fi_fifo_pkt[i],
30106 sizeof (struct sd_fi_pkt));
30107 }
30108 if (un->sd_fi_fifo_xb[i] != NULL) {
30109 kmem_free(un->sd_fi_fifo_xb[i],
30110 sizeof (struct sd_fi_xb));
30111 }
30112 if (un->sd_fi_fifo_un[i] != NULL) {
30113 kmem_free(un->sd_fi_fifo_un[i],
30114 sizeof (struct sd_fi_un));
30115 }
30116 if (un->sd_fi_fifo_arq[i] != NULL) {
30117 kmem_free(un->sd_fi_fifo_arq[i],
30118 sizeof (struct sd_fi_arq));
30119 }
30120 un->sd_fi_fifo_pkt[i] = NULL;
30121 un->sd_fi_fifo_un[i] = NULL;
30122 un->sd_fi_fifo_xb[i] = NULL;
30123 un->sd_fi_fifo_arq[i] = NULL;
30124 }
30125 un->sd_fi_fifo_start = 0;
30126 un->sd_fi_fifo_end = 0;
30127
30128 SD_INFO(SD_LOG_IOERR, un,
30129 "sd_faultinjection_ioctl: stop finished\n");
30130 break;
30131
30132 case SDIOCINSERTPKT:
30133 /* Store a packet struct to be pushed onto fifo */
30134 SD_INFO(SD_LOG_SDTEST, un,
30135 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30136
30137 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30138
30139 sd_fault_injection_on = 0;
30140
30141 /* No more that SD_FI_MAX_ERROR allowed in Queue */
30142 if (un->sd_fi_fifo_pkt[i] != NULL) {
30143 kmem_free(un->sd_fi_fifo_pkt[i],
30144 sizeof (struct sd_fi_pkt));
30145 }
30146 if (arg != NULL) {
30147 un->sd_fi_fifo_pkt[i] =
30148 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30149 if (un->sd_fi_fifo_pkt[i] == NULL) {
30150 /* Alloc failed don't store anything */
30151 break;
30152 }
30153 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30154 sizeof (struct sd_fi_pkt), 0);
30155 if (rval == -1) {
30156 kmem_free(un->sd_fi_fifo_pkt[i],
30157 sizeof (struct sd_fi_pkt));
30158 un->sd_fi_fifo_pkt[i] = NULL;
30159 }
30160 } else {
30161 SD_INFO(SD_LOG_IOERR, un,
30162 "sd_faultinjection_ioctl: pkt null\n");
30163 }
30164 break;
30165
30166 case SDIOCINSERTXB:
30167 /* Store a xb struct to be pushed onto fifo */
30168 SD_INFO(SD_LOG_SDTEST, un,
30169 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30170
30171 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30172
30173 sd_fault_injection_on = 0;
30174
30175 if (un->sd_fi_fifo_xb[i] != NULL) {
30176 kmem_free(un->sd_fi_fifo_xb[i],
30177 sizeof (struct sd_fi_xb));
30178 un->sd_fi_fifo_xb[i] = NULL;
30179 }
30180 if (arg != NULL) {
30181 un->sd_fi_fifo_xb[i] =
30182 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30183 if (un->sd_fi_fifo_xb[i] == NULL) {
30184 /* Alloc failed don't store anything */
30185 break;
30186 }
30187 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30188 sizeof (struct sd_fi_xb), 0);
30189
30190 if (rval == -1) {
30191 kmem_free(un->sd_fi_fifo_xb[i],
30192 sizeof (struct sd_fi_xb));
30193 un->sd_fi_fifo_xb[i] = NULL;
30194 }
30195 } else {
30196 SD_INFO(SD_LOG_IOERR, un,
30197 "sd_faultinjection_ioctl: xb null\n");
30198 }
30199 break;
30200
30201 case SDIOCINSERTUN:
30202 /* Store a un struct to be pushed onto fifo */
30203 SD_INFO(SD_LOG_SDTEST, un,
30204 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30205
30206 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30207
30208 sd_fault_injection_on = 0;
30209
30210 if (un->sd_fi_fifo_un[i] != NULL) {
30211 kmem_free(un->sd_fi_fifo_un[i],
30212 sizeof (struct sd_fi_un));
30213 un->sd_fi_fifo_un[i] = NULL;
30214 }
30215 if (arg != NULL) {
30216 un->sd_fi_fifo_un[i] =
30217 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30218 if (un->sd_fi_fifo_un[i] == NULL) {
30219 /* Alloc failed don't store anything */
30220 break;
30221 }
30222 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30223 sizeof (struct sd_fi_un), 0);
30224 if (rval == -1) {
30225 kmem_free(un->sd_fi_fifo_un[i],
30226 sizeof (struct sd_fi_un));
30227 un->sd_fi_fifo_un[i] = NULL;
30228 }
30229
30230 } else {
30231 SD_INFO(SD_LOG_IOERR, un,
30232 "sd_faultinjection_ioctl: un null\n");
30233 }
30234
30235 break;
30236
30237 case SDIOCINSERTARQ:
30238 /* Store a arq struct to be pushed onto fifo */
30239 SD_INFO(SD_LOG_SDTEST, un,
30240 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30241 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30242
30243 sd_fault_injection_on = 0;
30244
30245 if (un->sd_fi_fifo_arq[i] != NULL) {
30246 kmem_free(un->sd_fi_fifo_arq[i],
30247 sizeof (struct sd_fi_arq));
30248 un->sd_fi_fifo_arq[i] = NULL;
30249 }
30250 if (arg != NULL) {
30251 un->sd_fi_fifo_arq[i] =
30252 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30253 if (un->sd_fi_fifo_arq[i] == NULL) {
30254 /* Alloc failed don't store anything */
30255 break;
30256 }
30257 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30258 sizeof (struct sd_fi_arq), 0);
30259 if (rval == -1) {
30260 kmem_free(un->sd_fi_fifo_arq[i],
30261 sizeof (struct sd_fi_arq));
30262 un->sd_fi_fifo_arq[i] = NULL;
30263 }
30264
30265 } else {
30266 SD_INFO(SD_LOG_IOERR, un,
30267 "sd_faultinjection_ioctl: arq null\n");
30268 }
30269
30270 break;
30271
30272 case SDIOCPUSH:
30273 /* Push stored xb, pkt, un, and arq onto fifo */
30274 sd_fault_injection_on = 0;
30275
30276 if (arg != NULL) {
30277 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30278 if (rval != -1 &&
30279 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30280 un->sd_fi_fifo_end += i;
30281 }
30282 } else {
30283 SD_INFO(SD_LOG_IOERR, un,
30284 "sd_faultinjection_ioctl: push arg null\n");
30285 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30286 un->sd_fi_fifo_end++;
30287 }
30288 }
30289 SD_INFO(SD_LOG_IOERR, un,
30290 "sd_faultinjection_ioctl: push to end=%d\n",
30291 un->sd_fi_fifo_end);
30292 break;
30293
30294 case SDIOCRETRIEVE:
30295 /* Return buffer of log from Injection session */
30296 SD_INFO(SD_LOG_SDTEST, un,
30297 "sd_faultinjection_ioctl: Injecting Fault Retreive");
30298
30299 sd_fault_injection_on = 0;
30300
30301 mutex_enter(&(un->un_fi_mutex));
30302 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30303 un->sd_fi_buf_len+1, 0);
30304 mutex_exit(&(un->un_fi_mutex));
30305
30306 if (rval == -1) {
30307 /*
30308 * arg is possibly invalid setting
30309 * it to NULL for return
30310 */
30311 arg = NULL;
30312 }
30313 break;
30314 }
30315
30316 mutex_exit(SD_MUTEX(un));
30317 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30318 " exit\n");
30319 }
30320
30321
30322 /*
30323 * Function: sd_injection_log()
30324 *
30325 * Description: This routine adds buff to the already existing injection log
30326 * for retrieval via faultinjection_ioctl for use in fault
30327 * detection and recovery
30328 *
30329 * Arguments: buf - the string to add to the log
30330 */
30331
30332 static void
30333 sd_injection_log(char *buf, struct sd_lun *un)
30334 {
30335 uint_t len;
30336
30337 ASSERT(un != NULL);
30338 ASSERT(buf != NULL);
30339
30340 mutex_enter(&(un->un_fi_mutex));
30341
30342 len = min(strlen(buf), 255);
30343 /* Add logged value to Injection log to be returned later */
30344 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30345 uint_t offset = strlen((char *)un->sd_fi_log);
30346 char *destp = (char *)un->sd_fi_log + offset;
30347 int i;
30348 for (i = 0; i < len; i++) {
30349 *destp++ = *buf++;
30350 }
30351 un->sd_fi_buf_len += len;
30352 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30353 }
30354
30355 mutex_exit(&(un->un_fi_mutex));
30356 }
30357
30358
30359 /*
30360 * Function: sd_faultinjection()
30361 *
30362 * Description: This routine takes the pkt and changes its
30363 * content based on error injection scenerio.
30364 *
30365 * Arguments: pktp - packet to be changed
30366 */
30367
30368 static void
30369 sd_faultinjection(struct scsi_pkt *pktp)
30370 {
30371 uint_t i;
30372 struct sd_fi_pkt *fi_pkt;
30373 struct sd_fi_xb *fi_xb;
30374 struct sd_fi_un *fi_un;
30375 struct sd_fi_arq *fi_arq;
30376 struct buf *bp;
30377 struct sd_xbuf *xb;
30378 struct sd_lun *un;
30379
30380 ASSERT(pktp != NULL);
30381
30382 /* pull bp xb and un from pktp */
30383 bp = (struct buf *)pktp->pkt_private;
30384 xb = SD_GET_XBUF(bp);
30385 un = SD_GET_UN(bp);
30386
30387 ASSERT(un != NULL);
30388
30389 mutex_enter(SD_MUTEX(un));
30390
30391 SD_TRACE(SD_LOG_SDTEST, un,
30392 "sd_faultinjection: entry Injection from sdintr\n");
30393
30394 /* if injection is off return */
30395 if (sd_fault_injection_on == 0 ||
30396 un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30397 mutex_exit(SD_MUTEX(un));
30398 return;
30399 }
30400
30401 SD_INFO(SD_LOG_SDTEST, un,
30402 "sd_faultinjection: is working for copying\n");
30403
30404 /* take next set off fifo */
30405 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30406
30407 fi_pkt = un->sd_fi_fifo_pkt[i];
30408 fi_xb = un->sd_fi_fifo_xb[i];
30409 fi_un = un->sd_fi_fifo_un[i];
30410 fi_arq = un->sd_fi_fifo_arq[i];
30411
30412
30413 /* set variables accordingly */
30414 /* set pkt if it was on fifo */
30415 if (fi_pkt != NULL) {
30416 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30417 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30418 if (fi_pkt->pkt_cdbp != 0xff)
30419 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30420 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30421 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30422 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30423
30424 }
30425 /* set xb if it was on fifo */
30426 if (fi_xb != NULL) {
30427 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30428 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30429 if (fi_xb->xb_retry_count != 0)
30430 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30431 SD_CONDSET(xb, xb, xb_victim_retry_count,
30432 "xb_victim_retry_count");
30433 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30434 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30435 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30436
30437 /* copy in block data from sense */
30438 /*
30439 * if (fi_xb->xb_sense_data[0] != -1) {
30440 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30441 * SENSE_LENGTH);
30442 * }
30443 */
30444 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30445
30446 /* copy in extended sense codes */
30447 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30448 xb, es_code, "es_code");
30449 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30450 xb, es_key, "es_key");
30451 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30452 xb, es_add_code, "es_add_code");
30453 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30454 xb, es_qual_code, "es_qual_code");
30455 struct scsi_extended_sense *esp;
30456 esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30457 esp->es_class = CLASS_EXTENDED_SENSE;
30458 }
30459
30460 /* set un if it was on fifo */
30461 if (fi_un != NULL) {
30462 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30463 SD_CONDSET(un, un, un_ctype, "un_ctype");
30464 SD_CONDSET(un, un, un_reset_retry_count,
30465 "un_reset_retry_count");
30466 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30467 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30468 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30469 SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30470 "un_f_allow_bus_device_reset");
30471 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30472
30473 }
30474
30475 /* copy in auto request sense if it was on fifo */
30476 if (fi_arq != NULL) {
30477 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30478 }
30479
30480 /* free structs */
30481 if (un->sd_fi_fifo_pkt[i] != NULL) {
30482 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30483 }
30484 if (un->sd_fi_fifo_xb[i] != NULL) {
30485 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30486 }
30487 if (un->sd_fi_fifo_un[i] != NULL) {
30488 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30489 }
30490 if (un->sd_fi_fifo_arq[i] != NULL) {
30491 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30492 }
30493
30494 /*
30495 * kmem_free does not gurantee to set to NULL
30496 * since we uses these to determine if we set
30497 * values or not lets confirm they are always
30498 * NULL after free
30499 */
30500 un->sd_fi_fifo_pkt[i] = NULL;
30501 un->sd_fi_fifo_un[i] = NULL;
30502 un->sd_fi_fifo_xb[i] = NULL;
30503 un->sd_fi_fifo_arq[i] = NULL;
30504
30505 un->sd_fi_fifo_start++;
30506
30507 mutex_exit(SD_MUTEX(un));
30508
30509 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30510 }
30511
30512 #endif /* SD_FAULT_INJECTION */
30513
30514 /*
30515 * This routine is invoked in sd_unit_attach(). Before calling it, the
30516 * properties in conf file should be processed already, and "hotpluggable"
30517 * property was processed also.
30518 *
30519 * The sd driver distinguishes 3 different type of devices: removable media,
30520 * non-removable media, and hotpluggable. Below the differences are defined:
30521 *
30522 * 1. Device ID
30523 *
30524 * The device ID of a device is used to identify this device. Refer to
30525 * ddi_devid_register(9F).
30526 *
30527 * For a non-removable media disk device which can provide 0x80 or 0x83
30528 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30529 * device ID is created to identify this device. For other non-removable
30530 * media devices, a default device ID is created only if this device has
30531 * at least 2 alter cylinders. Otherwise, this device has no devid.
30532 *
30533 * -------------------------------------------------------
30534 * removable media hotpluggable | Can Have Device ID
30535 * -------------------------------------------------------
30536 * false false | Yes
30537 * false true | Yes
30538 * true x | No
30539 * ------------------------------------------------------
30540 *
30541 *
30542 * 2. SCSI group 4 commands
30543 *
30544 * In SCSI specs, only some commands in group 4 command set can use
30545 * 8-byte addresses that can be used to access >2TB storage spaces.
30546 * Other commands have no such capability. Without supporting group4,
30547 * it is impossible to make full use of storage spaces of a disk with
30548 * capacity larger than 2TB.
30549 *
30550 * -----------------------------------------------
30551 * removable media hotpluggable LP64 | Group
30552 * -----------------------------------------------
30553 * false false false | 1
30554 * false false true | 4
30555 * false true false | 1
30556 * false true true | 4
30557 * true x x | 5
30558 * -----------------------------------------------
30559 *
30560 *
30561 * 3. Check for VTOC Label
30562 *
30563 * If a direct-access disk has no EFI label, sd will check if it has a
30564 * valid VTOC label. Now, sd also does that check for removable media
30565 * and hotpluggable devices.
30566 *
30567 * --------------------------------------------------------------
30568 * Direct-Access removable media hotpluggable | Check Label
30569 * -------------------------------------------------------------
30570 * false false false | No
30571 * false false true | No
30572 * false true false | Yes
30573 * false true true | Yes
30574 * true x x | Yes
30575 * --------------------------------------------------------------
30576 *
30577 *
30578 * 4. Building default VTOC label
30579 *
30580 * As section 3 says, sd checks if some kinds of devices have VTOC label.
30581 * If those devices have no valid VTOC label, sd(7d) will attempt to
30582 * create default VTOC for them. Currently sd creates default VTOC label
30583 * for all devices on x86 platform (VTOC_16), but only for removable
30584 * media devices on SPARC (VTOC_8).
30585 *
30586 * -----------------------------------------------------------
30587 * removable media hotpluggable platform | Default Label
30588 * -----------------------------------------------------------
30589 * false false sparc | No
30590 * false true x86 | Yes
30591 * false true sparc | Yes
30592 * true x x | Yes
30593 * ----------------------------------------------------------
30594 *
30595 *
30596 * 5. Supported blocksizes of target devices
30597 *
30598 * Sd supports non-512-byte blocksize for removable media devices only.
30599 * For other devices, only 512-byte blocksize is supported. This may be
30600 * changed in near future because some RAID devices require non-512-byte
30601 * blocksize
30602 *
30603 * -----------------------------------------------------------
30604 * removable media hotpluggable | non-512-byte blocksize
30605 * -----------------------------------------------------------
30606 * false false | No
30607 * false true | No
30608 * true x | Yes
30609 * -----------------------------------------------------------
30610 *
30611 *
30612 * 6. Automatic mount & unmount
30613 *
30614 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30615 * if a device is removable media device. It return 1 for removable media
30616 * devices, and 0 for others.
30617 *
30618 * The automatic mounting subsystem should distinguish between the types
30619 * of devices and apply automounting policies to each.
30620 *
30621 *
30622 * 7. fdisk partition management
30623 *
30624 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30625 * just supports fdisk partitions on x86 platform. On sparc platform, sd
30626 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30627 * fdisk partitions on both x86 and SPARC platform.
30628 *
30629 * -----------------------------------------------------------
30630 * platform removable media USB/1394 | fdisk supported
30631 * -----------------------------------------------------------
30632 * x86 X X | true
30633 * ------------------------------------------------------------
30634 * sparc X X | false
30635 * ------------------------------------------------------------
30636 *
30637 *
30638 * 8. MBOOT/MBR
30639 *
30640 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30641 * read/write mboot for removable media devices on sparc platform.
30642 *
30643 * -----------------------------------------------------------
30644 * platform removable media USB/1394 | mboot supported
30645 * -----------------------------------------------------------
30646 * x86 X X | true
30647 * ------------------------------------------------------------
30648 * sparc false false | false
30649 * sparc false true | true
30650 * sparc true false | true
30651 * sparc true true | true
30652 * ------------------------------------------------------------
30653 *
30654 *
30655 * 9. error handling during opening device
30656 *
30657 * If failed to open a disk device, an errno is returned. For some kinds
30658 * of errors, different errno is returned depending on if this device is
30659 * a removable media device. This brings USB/1394 hard disks in line with
30660 * expected hard disk behavior. It is not expected that this breaks any
30661 * application.
30662 *
30663 * ------------------------------------------------------
30664 * removable media hotpluggable | errno
30665 * ------------------------------------------------------
30666 * false false | EIO
30667 * false true | EIO
30668 * true x | ENXIO
30669 * ------------------------------------------------------
30670 *
30671 *
30672 * 11. ioctls: DKIOCEJECT, CDROMEJECT
30673 *
30674 * These IOCTLs are applicable only to removable media devices.
30675 *
30676 * -----------------------------------------------------------
30677 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT
30678 * -----------------------------------------------------------
30679 * false false | No
30680 * false true | No
30681 * true x | Yes
30682 * -----------------------------------------------------------
30683 *
30684 *
30685 * 12. Kstats for partitions
30686 *
30687 * sd creates partition kstat for non-removable media devices. USB and
30688 * Firewire hard disks now have partition kstats
30689 *
30690 * ------------------------------------------------------
30691 * removable media hotpluggable | kstat
30692 * ------------------------------------------------------
30693 * false false | Yes
30694 * false true | Yes
30695 * true x | No
30696 * ------------------------------------------------------
30697 *
30698 *
30699 * 13. Removable media & hotpluggable properties
30700 *
30701 * Sd driver creates a "removable-media" property for removable media
30702 * devices. Parent nexus drivers create a "hotpluggable" property if
30703 * it supports hotplugging.
30704 *
30705 * ---------------------------------------------------------------------
30706 * removable media hotpluggable | "removable-media" " hotpluggable"
30707 * ---------------------------------------------------------------------
30708 * false false | No No
30709 * false true | No Yes
30710 * true false | Yes No
30711 * true true | Yes Yes
30712 * ---------------------------------------------------------------------
30713 *
30714 *
30715 * 14. Power Management
30716 *
30717 * sd only power manages removable media devices or devices that support
30718 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250)
30719 *
30720 * A parent nexus that supports hotplugging can also set "pm-capable"
30721 * if the disk can be power managed.
30722 *
30723 * ------------------------------------------------------------
30724 * removable media hotpluggable pm-capable | power manage
30725 * ------------------------------------------------------------
30726 * false false false | No
30727 * false false true | Yes
30728 * false true false | No
30729 * false true true | Yes
30730 * true x x | Yes
30731 * ------------------------------------------------------------
30732 *
30733 * USB and firewire hard disks can now be power managed independently
30734 * of the framebuffer
30735 *
30736 *
30737 * 15. Support for USB disks with capacity larger than 1TB
30738 *
30739 * Currently, sd doesn't permit a fixed disk device with capacity
30740 * larger than 1TB to be used in a 32-bit operating system environment.
30741 * However, sd doesn't do that for removable media devices. Instead, it
30742 * assumes that removable media devices cannot have a capacity larger
30743 * than 1TB. Therefore, using those devices on 32-bit system is partially
30744 * supported, which can cause some unexpected results.
30745 *
30746 * ---------------------------------------------------------------------
30747 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env
30748 * ---------------------------------------------------------------------
30749 * false false | true | no
30750 * false true | true | no
30751 * true false | true | Yes
30752 * true true | true | Yes
30753 * ---------------------------------------------------------------------
30754 *
30755 *
30756 * 16. Check write-protection at open time
30757 *
30758 * When a removable media device is being opened for writing without NDELAY
30759 * flag, sd will check if this device is writable. If attempting to open
30760 * without NDELAY flag a write-protected device, this operation will abort.
30761 *
30762 * ------------------------------------------------------------
30763 * removable media USB/1394 | WP Check
30764 * ------------------------------------------------------------
30765 * false false | No
30766 * false true | No
30767 * true false | Yes
30768 * true true | Yes
30769 * ------------------------------------------------------------
30770 *
30771 *
30772 * 17. syslog when corrupted VTOC is encountered
30773 *
30774 * Currently, if an invalid VTOC is encountered, sd only print syslog
30775 * for fixed SCSI disks.
30776 * ------------------------------------------------------------
30777 * removable media USB/1394 | print syslog
30778 * ------------------------------------------------------------
30779 * false false | Yes
30780 * false true | No
30781 * true false | No
30782 * true true | No
30783 * ------------------------------------------------------------
30784 */
30785 static void
30786 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30787 {
30788 int pm_cap;
30789
30790 ASSERT(un->un_sd);
30791 ASSERT(un->un_sd->sd_inq);
30792
30793 /*
30794 * Enable SYNC CACHE support for all devices.
30795 */
30796 un->un_f_sync_cache_supported = TRUE;
30797
30798 /*
30799 * Set the sync cache required flag to false.
30800 * This would ensure that there is no SYNC CACHE
30801 * sent when there are no writes
30802 */
30803 un->un_f_sync_cache_required = FALSE;
30804
30805 if (un->un_sd->sd_inq->inq_rmb) {
30806 /*
30807 * The media of this device is removable. And for this kind
30808 * of devices, it is possible to change medium after opening
30809 * devices. Thus we should support this operation.
30810 */
30811 un->un_f_has_removable_media = TRUE;
30812
30813 /*
30814 * support non-512-byte blocksize of removable media devices
30815 */
30816 un->un_f_non_devbsize_supported = TRUE;
30817
30818 /*
30819 * Assume that all removable media devices support DOOR_LOCK
30820 */
30821 un->un_f_doorlock_supported = TRUE;
30822
30823 /*
30824 * For a removable media device, it is possible to be opened
30825 * with NDELAY flag when there is no media in drive, in this
30826 * case we don't care if device is writable. But if without
30827 * NDELAY flag, we need to check if media is write-protected.
30828 */
30829 un->un_f_chk_wp_open = TRUE;
30830
30831 /*
30832 * need to start a SCSI watch thread to monitor media state,
30833 * when media is being inserted or ejected, notify syseventd.
30834 */
30835 un->un_f_monitor_media_state = TRUE;
30836
30837 /*
30838 * Some devices don't support START_STOP_UNIT command.
30839 * Therefore, we'd better check if a device supports it
30840 * before sending it.
30841 */
30842 un->un_f_check_start_stop = TRUE;
30843
30844 /*
30845 * support eject media ioctl:
30846 * FDEJECT, DKIOCEJECT, CDROMEJECT
30847 */
30848 un->un_f_eject_media_supported = TRUE;
30849
30850 /*
30851 * Because many removable-media devices don't support
30852 * LOG_SENSE, we couldn't use this command to check if
30853 * a removable media device support power-management.
30854 * We assume that they support power-management via
30855 * START_STOP_UNIT command and can be spun up and down
30856 * without limitations.
30857 */
30858 un->un_f_pm_supported = TRUE;
30859
30860 /*
30861 * Need to create a zero length (Boolean) property
30862 * removable-media for the removable media devices.
30863 * Note that the return value of the property is not being
30864 * checked, since if unable to create the property
30865 * then do not want the attach to fail altogether. Consistent
30866 * with other property creation in attach.
30867 */
30868 (void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30869 DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30870
30871 } else {
30872 /*
30873 * create device ID for device
30874 */
30875 un->un_f_devid_supported = TRUE;
30876
30877 /*
30878 * Spin up non-removable-media devices once it is attached
30879 */
30880 un->un_f_attach_spinup = TRUE;
30881
30882 /*
30883 * According to SCSI specification, Sense data has two kinds of
30884 * format: fixed format, and descriptor format. At present, we
30885 * don't support descriptor format sense data for removable
30886 * media.
30887 */
30888 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30889 un->un_f_descr_format_supported = TRUE;
30890 }
30891
30892 /*
30893 * kstats are created only for non-removable media devices.
30894 *
30895 * Set this in sd.conf to 0 in order to disable kstats. The
30896 * default is 1, so they are enabled by default.
30897 */
30898 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30899 SD_DEVINFO(un), DDI_PROP_DONTPASS,
30900 "enable-partition-kstats", 1));
30901
30902 /*
30903 * Check if HBA has set the "pm-capable" property.
30904 * If "pm-capable" exists and is non-zero then we can
30905 * power manage the device without checking the start/stop
30906 * cycle count log sense page.
30907 *
30908 * If "pm-capable" exists and is set to be false (0),
30909 * then we should not power manage the device.
30910 *
30911 * If "pm-capable" doesn't exist then pm_cap will
30912 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case,
30913 * sd will check the start/stop cycle count log sense page
30914 * and power manage the device if the cycle count limit has
30915 * not been exceeded.
30916 */
30917 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30918 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30919 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30920 un->un_f_log_sense_supported = TRUE;
30921 if (!un->un_f_power_condition_disabled &&
30922 SD_INQUIRY(un)->inq_ansi == 6) {
30923 un->un_f_power_condition_supported = TRUE;
30924 }
30925 } else {
30926 /*
30927 * pm-capable property exists.
30928 *
30929 * Convert "TRUE" values for pm_cap to
30930 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30931 * later. "TRUE" values are any values defined in
30932 * inquiry.h.
30933 */
30934 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30935 un->un_f_log_sense_supported = FALSE;
30936 } else {
30937 /* SD_PM_CAPABLE_IS_TRUE case */
30938 un->un_f_pm_supported = TRUE;
30939 if (!un->un_f_power_condition_disabled &&
30940 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30941 un->un_f_power_condition_supported =
30942 TRUE;
30943 }
30944 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30945 un->un_f_log_sense_supported = TRUE;
30946 un->un_f_pm_log_sense_smart =
30947 SD_PM_CAP_SMART_LOG(pm_cap);
30948 }
30949 }
30950
30951 SD_INFO(SD_LOG_ATTACH_DETACH, un,
30952 "sd_unit_attach: un:0x%p pm-capable "
30953 "property set to %d.\n", un, un->un_f_pm_supported);
30954 }
30955 }
30956
30957 if (un->un_f_is_hotpluggable) {
30958
30959 /*
30960 * Have to watch hotpluggable devices as well, since
30961 * that's the only way for userland applications to
30962 * detect hot removal while device is busy/mounted.
30963 */
30964 un->un_f_monitor_media_state = TRUE;
30965
30966 un->un_f_check_start_stop = TRUE;
30967
30968 }
30969 }
30970
30971 /*
30972 * sd_tg_rdwr:
30973 * Provides rdwr access for cmlb via sd_tgops. The start_block is
30974 * in sys block size, req_length in bytes.
30975 *
30976 */
30977 static int
30978 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30979 diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30980 {
30981 struct sd_lun *un;
30982 int path_flag = (int)(uintptr_t)tg_cookie;
30983 char *dkl = NULL;
30984 diskaddr_t real_addr = start_block;
30985 diskaddr_t first_byte, end_block;
30986
30987 size_t buffer_size = reqlength;
30988 int rval = 0;
30989 diskaddr_t cap;
30990 uint32_t lbasize;
30991 sd_ssc_t *ssc;
30992
30993 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30994 if (un == NULL)
30995 return (ENXIO);
30996
30997 if (cmd != TG_READ && cmd != TG_WRITE)
30998 return (EINVAL);
30999
31000 ssc = sd_ssc_init(un);
31001 mutex_enter(SD_MUTEX(un));
31002 if (un->un_f_tgt_blocksize_is_valid == FALSE) {
31003 mutex_exit(SD_MUTEX(un));
31004 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31005 &lbasize, path_flag);
31006 if (rval != 0)
31007 goto done1;
31008 mutex_enter(SD_MUTEX(un));
31009 sd_update_block_info(un, lbasize, cap);
31010 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
31011 mutex_exit(SD_MUTEX(un));
31012 rval = EIO;
31013 goto done;
31014 }
31015 }
31016
31017 if (NOT_DEVBSIZE(un)) {
31018 /*
31019 * sys_blocksize != tgt_blocksize, need to re-adjust
31020 * blkno and save the index to beginning of dk_label
31021 */
31022 first_byte = SD_SYSBLOCKS2BYTES(start_block);
31023 real_addr = first_byte / un->un_tgt_blocksize;
31024
31025 end_block = (first_byte + reqlength +
31026 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31027
31028 /* round up buffer size to multiple of target block size */
31029 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31030
31031 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31032 "label_addr: 0x%x allocation size: 0x%x\n",
31033 real_addr, buffer_size);
31034
31035 if (((first_byte % un->un_tgt_blocksize) != 0) ||
31036 (reqlength % un->un_tgt_blocksize) != 0)
31037 /* the request is not aligned */
31038 dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31039 }
31040
31041 /*
31042 * The MMC standard allows READ CAPACITY to be
31043 * inaccurate by a bounded amount (in the interest of
31044 * response latency). As a result, failed READs are
31045 * commonplace (due to the reading of metadata and not
31046 * data). Depending on the per-Vendor/drive Sense data,
31047 * the failed READ can cause many (unnecessary) retries.
31048 */
31049
31050 if (ISCD(un) && (cmd == TG_READ) &&
31051 (un->un_f_blockcount_is_valid == TRUE) &&
31052 ((start_block == (un->un_blockcount - 1))||
31053 (start_block == (un->un_blockcount - 2)))) {
31054 path_flag = SD_PATH_DIRECT_PRIORITY;
31055 }
31056
31057 mutex_exit(SD_MUTEX(un));
31058 if (cmd == TG_READ) {
31059 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31060 buffer_size, real_addr, path_flag);
31061 if (dkl != NULL)
31062 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31063 real_addr), bufaddr, reqlength);
31064 } else {
31065 if (dkl) {
31066 rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31067 real_addr, path_flag);
31068 if (rval) {
31069 goto done1;
31070 }
31071 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31072 real_addr), reqlength);
31073 }
31074 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31075 buffer_size, real_addr, path_flag);
31076 }
31077
31078 done1:
31079 if (dkl != NULL)
31080 kmem_free(dkl, buffer_size);
31081
31082 if (rval != 0) {
31083 if (rval == EIO)
31084 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31085 else
31086 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31087 }
31088 done:
31089 sd_ssc_fini(ssc);
31090 return (rval);
31091 }
31092
31093
31094 static int
31095 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31096 {
31097
31098 struct sd_lun *un;
31099 diskaddr_t cap;
31100 uint32_t lbasize;
31101 int path_flag = (int)(uintptr_t)tg_cookie;
31102 int ret = 0;
31103
31104 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31105 if (un == NULL)
31106 return (ENXIO);
31107
31108 switch (cmd) {
31109 case TG_GETPHYGEOM:
31110 case TG_GETVIRTGEOM:
31111 case TG_GETCAPACITY:
31112 case TG_GETBLOCKSIZE:
31113 mutex_enter(SD_MUTEX(un));
31114
31115 if ((un->un_f_blockcount_is_valid == TRUE) &&
31116 (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31117 cap = un->un_blockcount;
31118 lbasize = un->un_tgt_blocksize;
31119 mutex_exit(SD_MUTEX(un));
31120 } else {
31121 sd_ssc_t *ssc;
31122 mutex_exit(SD_MUTEX(un));
31123 ssc = sd_ssc_init(un);
31124 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31125 &lbasize, path_flag);
31126 if (ret != 0) {
31127 if (ret == EIO)
31128 sd_ssc_assessment(ssc,
31129 SD_FMT_STATUS_CHECK);
31130 else
31131 sd_ssc_assessment(ssc,
31132 SD_FMT_IGNORE);
31133 sd_ssc_fini(ssc);
31134 return (ret);
31135 }
31136 sd_ssc_fini(ssc);
31137 mutex_enter(SD_MUTEX(un));
31138 sd_update_block_info(un, lbasize, cap);
31139 if ((un->un_f_blockcount_is_valid == FALSE) ||
31140 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31141 mutex_exit(SD_MUTEX(un));
31142 return (EIO);
31143 }
31144 mutex_exit(SD_MUTEX(un));
31145 }
31146
31147 if (cmd == TG_GETCAPACITY) {
31148 *(diskaddr_t *)arg = cap;
31149 return (0);
31150 }
31151
31152 if (cmd == TG_GETBLOCKSIZE) {
31153 *(uint32_t *)arg = lbasize;
31154 return (0);
31155 }
31156
31157 if (cmd == TG_GETPHYGEOM)
31158 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31159 cap, lbasize, path_flag);
31160 else
31161 /* TG_GETVIRTGEOM */
31162 ret = sd_get_virtual_geometry(un,
31163 (cmlb_geom_t *)arg, cap, lbasize);
31164
31165 return (ret);
31166
31167 case TG_GETATTR:
31168 mutex_enter(SD_MUTEX(un));
31169 ((tg_attribute_t *)arg)->media_is_writable =
31170 un->un_f_mmc_writable_media;
31171 ((tg_attribute_t *)arg)->media_is_solid_state =
31172 un->un_f_is_solid_state;
31173 mutex_exit(SD_MUTEX(un));
31174 return (0);
31175 default:
31176 return (ENOTTY);
31177
31178 }
31179 }
31180
31181 /*
31182 * Function: sd_ssc_ereport_post
31183 *
31184 * Description: Will be called when SD driver need to post an ereport.
31185 *
31186 * Context: Kernel thread or interrupt context.
31187 */
31188
31189 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31190
31191 static void
31192 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31193 {
31194 int uscsi_path_instance = 0;
31195 uchar_t uscsi_pkt_reason;
31196 uint32_t uscsi_pkt_state;
31197 uint32_t uscsi_pkt_statistics;
31198 uint64_t uscsi_ena;
31199 uchar_t op_code;
31200 uint8_t *sensep;
31201 union scsi_cdb *cdbp;
31202 uint_t cdblen = 0;
31203 uint_t senlen = 0;
31204 struct sd_lun *un;
31205 dev_info_t *dip;
31206 char *devid;
31207 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31208 SSC_FLAGS_INVALID_STATUS |
31209 SSC_FLAGS_INVALID_SENSE |
31210 SSC_FLAGS_INVALID_DATA;
31211 char assessment[16];
31212
31213 ASSERT(ssc != NULL);
31214 ASSERT(ssc->ssc_uscsi_cmd != NULL);
31215 ASSERT(ssc->ssc_uscsi_info != NULL);
31216
31217 un = ssc->ssc_un;
31218 ASSERT(un != NULL);
31219
31220 dip = un->un_sd->sd_dev;
31221
31222 /*
31223 * Get the devid:
31224 * devid will only be passed to non-transport error reports.
31225 */
31226 devid = DEVI(dip)->devi_devid_str;
31227
31228 /*
31229 * If we are syncing or dumping, the command will not be executed
31230 * so we bypass this situation.
31231 */
31232 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31233 (un->un_state == SD_STATE_DUMPING))
31234 return;
31235
31236 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31237 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31238 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31239 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31240 uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31241
31242 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31243 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31244
31245 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31246 if (cdbp == NULL) {
31247 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31248 "sd_ssc_ereport_post meet empty cdb\n");
31249 return;
31250 }
31251
31252 op_code = cdbp->scc_cmd;
31253
31254 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31255 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31256 ssc->ssc_uscsi_cmd->uscsi_rqresid);
31257
31258 if (senlen > 0)
31259 ASSERT(sensep != NULL);
31260
31261 /*
31262 * Initialize drv_assess to corresponding values.
31263 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31264 * on the sense-key returned back.
31265 */
31266 switch (drv_assess) {
31267 case SD_FM_DRV_RECOVERY:
31268 (void) sprintf(assessment, "%s", "recovered");
31269 break;
31270 case SD_FM_DRV_RETRY:
31271 (void) sprintf(assessment, "%s", "retry");
31272 break;
31273 case SD_FM_DRV_NOTICE:
31274 (void) sprintf(assessment, "%s", "info");
31275 break;
31276 case SD_FM_DRV_FATAL:
31277 default:
31278 (void) sprintf(assessment, "%s", "unknown");
31279 }
31280 /*
31281 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31282 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31283 * driver-assessment will always be "recovered" here.
31284 */
31285 if (drv_assess == SD_FM_DRV_RECOVERY) {
31286 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31287 "cmd.disk.recovered", uscsi_ena, devid, NULL,
31288 DDI_NOSLEEP, NULL,
31289 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31290 DEVID_IF_KNOWN(devid),
31291 "driver-assessment", DATA_TYPE_STRING, assessment,
31292 "op-code", DATA_TYPE_UINT8, op_code,
31293 "cdb", DATA_TYPE_UINT8_ARRAY,
31294 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31295 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31296 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31297 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31298 NULL);
31299 return;
31300 }
31301
31302 /*
31303 * If there is un-expected/un-decodable data, we should post
31304 * ereport.io.scsi.cmd.disk.dev.uderr.
31305 * driver-assessment will be set based on parameter drv_assess.
31306 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31307 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31308 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31309 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31310 */
31311 if (ssc->ssc_flags & ssc_invalid_flags) {
31312 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31313 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31314 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31315 NULL, DDI_NOSLEEP, NULL,
31316 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31317 DEVID_IF_KNOWN(devid),
31318 "driver-assessment", DATA_TYPE_STRING,
31319 drv_assess == SD_FM_DRV_FATAL ?
31320 "fail" : assessment,
31321 "op-code", DATA_TYPE_UINT8, op_code,
31322 "cdb", DATA_TYPE_UINT8_ARRAY,
31323 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31324 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31325 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31326 "pkt-stats", DATA_TYPE_UINT32,
31327 uscsi_pkt_statistics,
31328 "stat-code", DATA_TYPE_UINT8,
31329 ssc->ssc_uscsi_cmd->uscsi_status,
31330 "un-decode-info", DATA_TYPE_STRING,
31331 ssc->ssc_info,
31332 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31333 senlen, sensep,
31334 NULL);
31335 } else {
31336 /*
31337 * For other type of invalid data, the
31338 * un-decode-value field would be empty because the
31339 * un-decodable content could be seen from upper
31340 * level payload or inside un-decode-info.
31341 */
31342 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31343 NULL,
31344 "cmd.disk.dev.uderr", uscsi_ena, devid,
31345 NULL, DDI_NOSLEEP, NULL,
31346 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31347 DEVID_IF_KNOWN(devid),
31348 "driver-assessment", DATA_TYPE_STRING,
31349 drv_assess == SD_FM_DRV_FATAL ?
31350 "fail" : assessment,
31351 "op-code", DATA_TYPE_UINT8, op_code,
31352 "cdb", DATA_TYPE_UINT8_ARRAY,
31353 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31354 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31355 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31356 "pkt-stats", DATA_TYPE_UINT32,
31357 uscsi_pkt_statistics,
31358 "stat-code", DATA_TYPE_UINT8,
31359 ssc->ssc_uscsi_cmd->uscsi_status,
31360 "un-decode-info", DATA_TYPE_STRING,
31361 ssc->ssc_info,
31362 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31363 0, NULL,
31364 NULL);
31365 }
31366 ssc->ssc_flags &= ~ssc_invalid_flags;
31367 return;
31368 }
31369
31370 if (uscsi_pkt_reason != CMD_CMPLT ||
31371 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31372 /*
31373 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31374 * set inside sd_start_cmds due to errors(bad packet or
31375 * fatal transport error), we should take it as a
31376 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31377 * driver-assessment will be set based on drv_assess.
31378 * We will set devid to NULL because it is a transport
31379 * error.
31380 */
31381 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31382 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31383
31384 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31385 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31386 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31387 DEVID_IF_KNOWN(devid),
31388 "driver-assessment", DATA_TYPE_STRING,
31389 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31390 "op-code", DATA_TYPE_UINT8, op_code,
31391 "cdb", DATA_TYPE_UINT8_ARRAY,
31392 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31393 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31394 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31395 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31396 NULL);
31397 } else {
31398 /*
31399 * If we got here, we have a completed command, and we need
31400 * to further investigate the sense data to see what kind
31401 * of ereport we should post.
31402 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr
31403 * if sense-key == 0x3.
31404 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31405 * driver-assessment will be set based on the parameter
31406 * drv_assess.
31407 */
31408 if (senlen > 0) {
31409 /*
31410 * Here we have sense data available.
31411 */
31412 uint8_t sense_key;
31413 sense_key = scsi_sense_key(sensep);
31414 if (sense_key == 0x3) {
31415 /*
31416 * sense-key == 0x3(medium error),
31417 * driver-assessment should be "fatal" if
31418 * drv_assess is SD_FM_DRV_FATAL.
31419 */
31420 scsi_fm_ereport_post(un->un_sd,
31421 uscsi_path_instance, NULL,
31422 "cmd.disk.dev.rqs.merr",
31423 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31424 FM_VERSION, DATA_TYPE_UINT8,
31425 FM_EREPORT_VERS0,
31426 DEVID_IF_KNOWN(devid),
31427 "driver-assessment",
31428 DATA_TYPE_STRING,
31429 drv_assess == SD_FM_DRV_FATAL ?
31430 "fatal" : assessment,
31431 "op-code",
31432 DATA_TYPE_UINT8, op_code,
31433 "cdb",
31434 DATA_TYPE_UINT8_ARRAY, cdblen,
31435 ssc->ssc_uscsi_cmd->uscsi_cdb,
31436 "pkt-reason",
31437 DATA_TYPE_UINT8, uscsi_pkt_reason,
31438 "pkt-state",
31439 DATA_TYPE_UINT8, uscsi_pkt_state,
31440 "pkt-stats",
31441 DATA_TYPE_UINT32,
31442 uscsi_pkt_statistics,
31443 "stat-code",
31444 DATA_TYPE_UINT8,
31445 ssc->ssc_uscsi_cmd->uscsi_status,
31446 "key",
31447 DATA_TYPE_UINT8,
31448 scsi_sense_key(sensep),
31449 "asc",
31450 DATA_TYPE_UINT8,
31451 scsi_sense_asc(sensep),
31452 "ascq",
31453 DATA_TYPE_UINT8,
31454 scsi_sense_ascq(sensep),
31455 "sense-data",
31456 DATA_TYPE_UINT8_ARRAY,
31457 senlen, sensep,
31458 "lba",
31459 DATA_TYPE_UINT64,
31460 ssc->ssc_uscsi_info->ui_lba,
31461 NULL);
31462 } else {
31463 /*
31464 * if sense-key == 0x4(hardware
31465 * error), driver-assessment should
31466 * be "fatal" if drv_assess is
31467 * SD_FM_DRV_FATAL.
31468 */
31469 scsi_fm_ereport_post(un->un_sd,
31470 uscsi_path_instance, NULL,
31471 "cmd.disk.dev.rqs.derr",
31472 uscsi_ena, devid,
31473 NULL, DDI_NOSLEEP, NULL,
31474 FM_VERSION,
31475 DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31476 DEVID_IF_KNOWN(devid),
31477 "driver-assessment",
31478 DATA_TYPE_STRING,
31479 drv_assess == SD_FM_DRV_FATAL ?
31480 (sense_key == 0x4 ?
31481 "fatal" : "fail") : assessment,
31482 "op-code",
31483 DATA_TYPE_UINT8, op_code,
31484 "cdb",
31485 DATA_TYPE_UINT8_ARRAY, cdblen,
31486 ssc->ssc_uscsi_cmd->uscsi_cdb,
31487 "pkt-reason",
31488 DATA_TYPE_UINT8, uscsi_pkt_reason,
31489 "pkt-state",
31490 DATA_TYPE_UINT8, uscsi_pkt_state,
31491 "pkt-stats",
31492 DATA_TYPE_UINT32,
31493 uscsi_pkt_statistics,
31494 "stat-code",
31495 DATA_TYPE_UINT8,
31496 ssc->ssc_uscsi_cmd->uscsi_status,
31497 "key",
31498 DATA_TYPE_UINT8,
31499 scsi_sense_key(sensep),
31500 "asc",
31501 DATA_TYPE_UINT8,
31502 scsi_sense_asc(sensep),
31503 "ascq",
31504 DATA_TYPE_UINT8,
31505 scsi_sense_ascq(sensep),
31506 "sense-data",
31507 DATA_TYPE_UINT8_ARRAY,
31508 senlen, sensep,
31509 NULL);
31510 }
31511 } else {
31512 /*
31513 * For stat_code == STATUS_GOOD, this is not a
31514 * hardware error.
31515 */
31516 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31517 return;
31518
31519 /*
31520 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31521 * stat-code but with sense data unavailable.
31522 * driver-assessment will be set based on parameter
31523 * drv_assess.
31524 */
31525 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31526 NULL,
31527 "cmd.disk.dev.serr", uscsi_ena,
31528 devid, NULL, DDI_NOSLEEP, NULL,
31529 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31530 DEVID_IF_KNOWN(devid),
31531 "driver-assessment", DATA_TYPE_STRING,
31532 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31533 "op-code", DATA_TYPE_UINT8, op_code,
31534 "cdb",
31535 DATA_TYPE_UINT8_ARRAY,
31536 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31537 "pkt-reason",
31538 DATA_TYPE_UINT8, uscsi_pkt_reason,
31539 "pkt-state",
31540 DATA_TYPE_UINT8, uscsi_pkt_state,
31541 "pkt-stats",
31542 DATA_TYPE_UINT32, uscsi_pkt_statistics,
31543 "stat-code",
31544 DATA_TYPE_UINT8,
31545 ssc->ssc_uscsi_cmd->uscsi_status,
31546 NULL);
31547 }
31548 }
31549 }
31550
31551 /*
31552 * Function: sd_ssc_extract_info
31553 *
31554 * Description: Extract information available to help generate ereport.
31555 *
31556 * Context: Kernel thread or interrupt context.
31557 */
31558 static void
31559 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31560 struct buf *bp, struct sd_xbuf *xp)
31561 {
31562 size_t senlen = 0;
31563 union scsi_cdb *cdbp;
31564 int path_instance;
31565 /*
31566 * Need scsi_cdb_size array to determine the cdb length.
31567 */
31568 extern uchar_t scsi_cdb_size[];
31569
31570 ASSERT(un != NULL);
31571 ASSERT(pktp != NULL);
31572 ASSERT(bp != NULL);
31573 ASSERT(xp != NULL);
31574 ASSERT(ssc != NULL);
31575 ASSERT(mutex_owned(SD_MUTEX(un)));
31576
31577 /*
31578 * Transfer the cdb buffer pointer here.
31579 */
31580 cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31581
31582 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31583 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31584
31585 /*
31586 * Transfer the sense data buffer pointer if sense data is available,
31587 * calculate the sense data length first.
31588 */
31589 if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31590 (xp->xb_sense_state & STATE_ARQ_DONE)) {
31591 /*
31592 * For arq case, we will enter here.
31593 */
31594 if (xp->xb_sense_state & STATE_XARQ_DONE) {
31595 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31596 } else {
31597 senlen = SENSE_LENGTH;
31598 }
31599 } else {
31600 /*
31601 * For non-arq case, we will enter this branch.
31602 */
31603 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31604 (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31605 senlen = SENSE_LENGTH - xp->xb_sense_resid;
31606 }
31607
31608 }
31609
31610 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31611 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31612 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31613
31614 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31615
31616 /*
31617 * Only transfer path_instance when scsi_pkt was properly allocated.
31618 */
31619 path_instance = pktp->pkt_path_instance;
31620 if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31621 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31622 else
31623 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31624
31625 /*
31626 * Copy in the other fields we may need when posting ereport.
31627 */
31628 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31629 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31630 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31631 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31632
31633 /*
31634 * For partially read/write command, we will not create ena
31635 * in case of a successful command be reconized as recovered.
31636 */
31637 if ((pktp->pkt_reason == CMD_CMPLT) &&
31638 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31639 (senlen == 0)) {
31640 return;
31641 }
31642
31643 /*
31644 * To associate ereports of a single command execution flow, we
31645 * need a shared ena for a specific command.
31646 */
31647 if (xp->xb_ena == 0)
31648 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31649 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31650 }
31651
31652
31653 /*
31654 * Function: sd_check_solid_state
31655 *
31656 * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31657 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31658 * RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31659 * device is a solid state drive.
31660 *
31661 * Context: Kernel thread or interrupt context.
31662 */
31663
31664 static void
31665 sd_check_solid_state(sd_ssc_t *ssc)
31666 {
31667 int rval = 0;
31668 uchar_t *inqb1 = NULL;
31669 size_t inqb1_len = MAX_INQUIRY_SIZE;
31670 size_t inqb1_resid = 0;
31671 struct sd_lun *un;
31672
31673 ASSERT(ssc != NULL);
31674 un = ssc->ssc_un;
31675 ASSERT(un != NULL);
31676 ASSERT(!mutex_owned(SD_MUTEX(un)));
31677
31678 mutex_enter(SD_MUTEX(un));
31679 un->un_f_is_solid_state = FALSE;
31680
31681 if (ISCD(un)) {
31682 mutex_exit(SD_MUTEX(un));
31683 return;
31684 }
31685
31686 if (sd_check_vpd_page_support(ssc) == 0 &&
31687 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31688 mutex_exit(SD_MUTEX(un));
31689 /* collect page b1 data */
31690 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31691
31692 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31693 0x01, 0xB1, &inqb1_resid);
31694
31695 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31696 SD_TRACE(SD_LOG_COMMON, un,
31697 "sd_check_solid_state: \
31698 successfully get VPD page: %x \
31699 PAGE LENGTH: %x BYTE 4: %x \
31700 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31701 inqb1[5]);
31702
31703 mutex_enter(SD_MUTEX(un));
31704 /*
31705 * Check the MEDIUM ROTATION RATE. If it is set
31706 * to 1, the device is a solid state drive.
31707 */
31708 if (inqb1[4] == 0 && inqb1[5] == 1) {
31709 un->un_f_is_solid_state = TRUE;
31710 /* solid state drives don't need disksort */
31711 un->un_f_disksort_disabled = TRUE;
31712 }
31713 mutex_exit(SD_MUTEX(un));
31714 } else if (rval != 0) {
31715 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31716 }
31717
31718 kmem_free(inqb1, inqb1_len);
31719 } else {
31720 mutex_exit(SD_MUTEX(un));
31721 }
31722 }
31723
31724 /*
31725 * Function: sd_check_emulation_mode
31726 *
31727 * Description: Check whether the SSD is at emulation mode
31728 * by issuing READ_CAPACITY_16 to see whether
31729 * we can get physical block size of the drive.
31730 *
31731 * Context: Kernel thread or interrupt context.
31732 */
31733
31734 static void
31735 sd_check_emulation_mode(sd_ssc_t *ssc)
31736 {
31737 int rval = 0;
31738 uint64_t capacity;
31739 uint_t lbasize;
31740 uint_t pbsize;
31741 int i;
31742 int devid_len;
31743 struct sd_lun *un;
31744
31745 ASSERT(ssc != NULL);
31746 un = ssc->ssc_un;
31747 ASSERT(un != NULL);
31748 ASSERT(!mutex_owned(SD_MUTEX(un)));
31749
31750 mutex_enter(SD_MUTEX(un));
31751 if (ISCD(un)) {
31752 mutex_exit(SD_MUTEX(un));
31753 return;
31754 }
31755
31756 if (un->un_f_descr_format_supported) {
31757 mutex_exit(SD_MUTEX(un));
31758 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31759 &pbsize, SD_PATH_DIRECT);
31760 mutex_enter(SD_MUTEX(un));
31761
31762 if (rval != 0) {
31763 un->un_phy_blocksize = DEV_BSIZE;
31764 } else {
31765 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31766 un->un_phy_blocksize = DEV_BSIZE;
31767 } else if (pbsize > un->un_phy_blocksize) {
31768 /*
31769 * Don't reset the physical blocksize
31770 * unless we've detected a larger value.
31771 */
31772 un->un_phy_blocksize = pbsize;
31773 }
31774 }
31775 }
31776
31777 for (i = 0; i < sd_flash_dev_table_size; i++) {
31778 devid_len = (int)strlen(sd_flash_dev_table[i]);
31779 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31780 == SD_SUCCESS) {
31781 un->un_phy_blocksize = SSD_SECSIZE;
31782 if (un->un_f_is_solid_state &&
31783 un->un_phy_blocksize != un->un_tgt_blocksize)
31784 un->un_f_enable_rmw = TRUE;
31785 }
31786 }
31787
31788 mutex_exit(SD_MUTEX(un));
31789 }