Move everything from /var/adm to /var/log
[unleashed.git] / kernel / drivers / scsi / targets / sd / sd.c
blobdbef185afedb23d4f9eae915719b56ae3038768b
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
27 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
29 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2011 cyril.galibern@opensvc.com
36 * SCSI disk target driver.
38 #include <sys/scsi/scsi.h>
39 #include <sys/dkbad.h>
40 #include <sys/dklabel.h>
41 #include <sys/dkio.h>
42 #include <sys/fdio.h>
43 #include <sys/cdio.h>
44 #include <sys/mhd.h>
45 #include <sys/vtoc.h>
46 #include <sys/dktp/fdisk.h>
47 #include <sys/kstat.h>
48 #include <sys/vtrace.h>
49 #include <sys/note.h>
50 #include <sys/thread.h>
51 #include <sys/proc.h>
52 #include <sys/efi_partition.h>
53 #include <sys/var.h>
54 #include <sys/aio_req.h>
55 #include <sys/dkioc_free_util.h>
57 #include <sys/taskq.h>
58 #include <sys/uuid.h>
59 #include <sys/byteorder.h>
60 #include <sys/sdt.h>
62 #include "sd_xbuf.h"
64 #include <sys/scsi/targets/sddef.h>
65 #include <sys/cmlb.h>
66 #include <sys/sysevent/eventdefs.h>
67 #include <sys/sysevent/dev.h>
69 #include <sys/fm/protocol.h>
72 * Loadable module info.
74 #define SD_MODULE_NAME "SCSI Disk Driver"
77 * Define the interconnect type, to allow the driver to distinguish
78 * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
80 * This is really for backward compatibility. In the future, the driver
81 * should actually check the "interconnect-type" property as reported by
82 * the HBA; however at present this property is not defined by all HBAs,
83 * so we will use this #define (1) to permit the driver to run in
84 * backward-compatibility mode; and (2) to print a notification message
85 * if an FC HBA does not support the "interconnect-type" property. The
86 * behavior of the driver will be to assume parallel SCSI behaviors unless
87 * the "interconnect-type" property is defined by the HBA **AND** has a
88 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
89 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
90 * Channel behaviors (as per the old ssd). (Note that the
91 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
92 * will result in the driver assuming parallel SCSI behaviors.)
94 * (see include/sys/scsi/impl/services.h)
96 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
97 * since some FC HBAs may already support that, and there is some code in
98 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the
99 * default would confuse that code, and besides things should work fine
100 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
101 * "interconnect_type" property.
104 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL
107 * The name of the driver, established from the module name in _init.
109 static char *sd_label = NULL;
112 * Driver name is unfortunately prefixed on some driver.conf properties.
114 static char *sd_max_xfer_size = "sd_max_xfer_size";
115 static char *sd_config_list = "sd-config-list";
118 * Driver global variables
120 #ifdef SDDEBUG
121 int sd_force_pm_supported = 0;
122 #endif /* SDDEBUG */
124 void *sd_state = NULL;
125 int sd_io_time = SD_IO_TIME;
126 int sd_failfast_enable = 1;
127 int sd_ua_retry_count = SD_UA_RETRY_COUNT;
128 int sd_report_pfa = 1;
129 int sd_max_throttle = SD_MAX_THROTTLE;
130 int sd_min_throttle = SD_MIN_THROTTLE;
131 int sd_rot_delay = 4; /* Default 4ms Rotation delay */
132 int sd_qfull_throttle_enable = TRUE;
134 int sd_retry_on_reservation_conflict = 1;
135 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
136 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
138 static int sd_dtype_optical_bind = -1;
140 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
141 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict";
144 * Global data for debug logging. To enable debug printing, sd_component_mask
145 * and sd_level_mask should be set to the desired bit patterns as outlined in
146 * sddef.h.
148 uint_t sd_component_mask = 0x0;
149 uint_t sd_level_mask = 0x0;
150 struct sd_lun *sd_debug_un = NULL;
151 uint_t sd_error_level = SCSI_ERR_RETRYABLE;
153 /* Note: these may go away in the future... */
154 static uint32_t sd_xbuf_active_limit = 512;
155 static uint32_t sd_xbuf_reserve_limit = 16;
157 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
160 * Timer value used to reset the throttle after it has been reduced
161 * (typically in response to TRAN_BUSY or STATUS_QFULL)
163 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT;
164 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT;
167 * Interval value associated with the media change scsi watch.
169 static int sd_check_media_time = 3000000;
172 * Wait value used for in progress operations during a DDI_SUSPEND
174 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
177 * sd_label_mutex protects a static buffer used in the disk label
178 * component of the driver
180 static kmutex_t sd_label_mutex;
183 * sd_detach_mutex protects un_layer_count, un_detach_count, and
184 * un_opens_in_progress in the sd_lun structure.
186 static kmutex_t sd_detach_mutex;
188 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
189 sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
192 * Global buffer and mutex for debug logging
194 static char sd_log_buf[1024];
195 static kmutex_t sd_log_mutex;
198 * Structs and globals for recording attached lun information.
199 * This maintains a chain. Each node in the chain represents a SCSI controller.
200 * The structure records the number of luns attached to each target connected
201 * with the controller.
202 * For parallel scsi device only.
204 struct sd_scsi_hba_tgt_lun {
205 struct sd_scsi_hba_tgt_lun *next;
206 dev_info_t *pdip;
207 int nlun[NTARGETS_WIDE];
211 * Flag to indicate the lun is attached or detached
213 #define SD_SCSI_LUN_ATTACH 0
214 #define SD_SCSI_LUN_DETACH 1
216 static kmutex_t sd_scsi_target_lun_mutex;
217 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
219 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
220 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
222 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
223 sd_scsi_target_lun_head))
226 * "Smart" Probe Caching structs, globals, #defines, etc.
227 * For parallel scsi and non-self-identify device only.
231 * The following resources and routines are implemented to support
232 * "smart" probing, which caches the scsi_probe() results in an array,
233 * in order to help avoid long probe times.
235 struct sd_scsi_probe_cache {
236 struct sd_scsi_probe_cache *next;
237 dev_info_t *pdip;
238 int cache[NTARGETS_WIDE];
241 static kmutex_t sd_scsi_probe_cache_mutex;
242 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
245 * Really we only need protection on the head of the linked list, but
246 * better safe than sorry.
248 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
249 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
251 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
252 sd_scsi_probe_cache_head))
255 * Power attribute table
257 static sd_power_attr_ss sd_pwr_ss = {
258 { "NAME=spindle-motor", "0=off", "1=on", NULL },
259 {0, 100},
260 {30, 0},
261 {20000, 0}
264 static sd_power_attr_pc sd_pwr_pc = {
265 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
266 "3=active", NULL },
267 {0, 0, 0, 100},
268 {90, 90, 20, 0},
269 {15000, 15000, 1000, 0}
273 * Power level to power condition
275 static int sd_pl2pc[] = {
276 SD_TARGET_START_VALID,
277 SD_TARGET_STANDBY,
278 SD_TARGET_IDLE,
279 SD_TARGET_ACTIVE
283 * Vendor specific data name property declarations
286 #if defined(__i386) || defined(__amd64)
288 static sd_tunables seagate_properties = {
289 SEAGATE_THROTTLE_VALUE,
301 static sd_tunables fujitsu_properties = {
302 FUJITSU_THROTTLE_VALUE,
313 static sd_tunables ibm_properties = {
314 IBM_THROTTLE_VALUE,
325 static sd_tunables purple_properties = {
326 PURPLE_THROTTLE_VALUE,
329 PURPLE_BUSY_RETRIES,
330 PURPLE_RESET_RETRY_COUNT,
331 PURPLE_RESERVE_RELEASE_TIME,
337 static sd_tunables sve_properties = {
338 SVE_THROTTLE_VALUE,
341 SVE_BUSY_RETRIES,
342 SVE_RESET_RETRY_COUNT,
343 SVE_RESERVE_RELEASE_TIME,
344 SVE_MIN_THROTTLE_VALUE,
345 SVE_DISKSORT_DISABLED_FLAG,
349 static sd_tunables maserati_properties = {
357 MASERATI_DISKSORT_DISABLED_FLAG,
358 MASERATI_LUN_RESET_ENABLED_FLAG
361 static sd_tunables pirus_properties = {
362 PIRUS_THROTTLE_VALUE,
364 PIRUS_NRR_COUNT,
365 PIRUS_BUSY_RETRIES,
366 PIRUS_RESET_RETRY_COUNT,
368 PIRUS_MIN_THROTTLE_VALUE,
369 PIRUS_DISKSORT_DISABLED_FLAG,
370 PIRUS_LUN_RESET_ENABLED_FLAG
373 #endif
375 #if (defined(__i386) || defined(__amd64))
378 static sd_tunables elite_properties = {
379 ELITE_THROTTLE_VALUE,
390 static sd_tunables st31200n_properties = {
391 ST31200N_THROTTLE_VALUE,
402 #endif /* Fibre or not */
404 static sd_tunables lsi_properties_scsi = {
405 LSI_THROTTLE_VALUE,
407 LSI_NOTREADY_RETRIES,
416 static sd_tunables symbios_properties = {
417 SYMBIOS_THROTTLE_VALUE,
419 SYMBIOS_NOTREADY_RETRIES,
428 static sd_tunables lsi_properties = {
431 LSI_NOTREADY_RETRIES,
440 static sd_tunables lsi_oem_properties = {
443 LSI_OEM_NOTREADY_RETRIES,
455 #if (defined(SD_PROP_TST))
457 #define SD_TST_CTYPE_VAL CTYPE_CDROM
458 #define SD_TST_THROTTLE_VAL 16
459 #define SD_TST_NOTREADY_VAL 12
460 #define SD_TST_BUSY_VAL 60
461 #define SD_TST_RST_RETRY_VAL 36
462 #define SD_TST_RSV_REL_TIME 60
464 static sd_tunables tst_properties = {
465 SD_TST_THROTTLE_VAL,
466 SD_TST_CTYPE_VAL,
467 SD_TST_NOTREADY_VAL,
468 SD_TST_BUSY_VAL,
469 SD_TST_RST_RETRY_VAL,
470 SD_TST_RSV_REL_TIME,
475 #endif
477 /* This is similar to the ANSI toupper implementation */
478 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
481 * Static Driver Configuration Table
483 * This is the table of disks which need throttle adjustment (or, perhaps
484 * something else as defined by the flags at a future time.) device_id
485 * is a string consisting of concatenated vid (vendor), pid (product/model)
486 * and revision strings as defined in the scsi_inquiry structure. Offsets of
487 * the parts of the string are as defined by the sizes in the scsi_inquiry
488 * structure. Device type is searched as far as the device_id string is
489 * defined. Flags defines which values are to be set in the driver from the
490 * properties list.
492 * Entries below which begin and end with a "*" are a special case.
493 * These do not have a specific vendor, and the string which follows
494 * can appear anywhere in the 16 byte PID portion of the inquiry data.
496 * Entries below which begin and end with a " " (blank) are a special
497 * case. The comparison function will treat multiple consecutive blanks
498 * as equivalent to a single blank. For example, this causes a
499 * sd_disk_table entry of " NEC CDROM " to match a device's id string
500 * of "NEC CDROM".
502 * Note: The MD21 controller type has been obsoleted.
503 * ST318202F is a Legacy device
504 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
505 * made with an FC connection. The entries here are a legacy.
507 static sd_disk_config_t sd_disk_table[] = {
508 #if defined(__i386) || defined(__amd64)
509 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
510 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
511 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
512 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
513 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
514 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
515 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
516 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
517 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
518 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
519 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
520 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
521 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
522 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
523 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
524 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
525 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
526 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
527 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
528 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
529 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
530 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
531 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
532 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties },
533 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties },
534 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties },
535 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties },
536 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
537 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
538 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
539 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
540 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
541 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
542 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
543 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
544 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
545 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
546 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
547 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
548 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
549 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
550 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
551 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
552 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
553 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
554 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
555 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
556 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
557 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
558 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT |
559 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
560 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT |
561 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
562 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
563 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
564 { "SUN T3", SD_CONF_BSET_THROTTLE |
565 SD_CONF_BSET_BSY_RETRY_COUNT|
566 SD_CONF_BSET_RST_RETRIES|
567 SD_CONF_BSET_RSV_REL_TIME,
568 &purple_properties },
569 { "SUN SESS01", SD_CONF_BSET_THROTTLE |
570 SD_CONF_BSET_BSY_RETRY_COUNT|
571 SD_CONF_BSET_RST_RETRIES|
572 SD_CONF_BSET_RSV_REL_TIME|
573 SD_CONF_BSET_MIN_THROTTLE|
574 SD_CONF_BSET_DISKSORT_DISABLED,
575 &sve_properties },
576 { "SUN T4", SD_CONF_BSET_THROTTLE |
577 SD_CONF_BSET_BSY_RETRY_COUNT|
578 SD_CONF_BSET_RST_RETRIES|
579 SD_CONF_BSET_RSV_REL_TIME,
580 &purple_properties },
581 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
582 SD_CONF_BSET_LUN_RESET_ENABLED,
583 &maserati_properties },
584 { "SUN SE6920", SD_CONF_BSET_THROTTLE |
585 SD_CONF_BSET_NRR_COUNT|
586 SD_CONF_BSET_BSY_RETRY_COUNT|
587 SD_CONF_BSET_RST_RETRIES|
588 SD_CONF_BSET_MIN_THROTTLE|
589 SD_CONF_BSET_DISKSORT_DISABLED|
590 SD_CONF_BSET_LUN_RESET_ENABLED,
591 &pirus_properties },
592 { "SUN SE6940", SD_CONF_BSET_THROTTLE |
593 SD_CONF_BSET_NRR_COUNT|
594 SD_CONF_BSET_BSY_RETRY_COUNT|
595 SD_CONF_BSET_RST_RETRIES|
596 SD_CONF_BSET_MIN_THROTTLE|
597 SD_CONF_BSET_DISKSORT_DISABLED|
598 SD_CONF_BSET_LUN_RESET_ENABLED,
599 &pirus_properties },
600 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE |
601 SD_CONF_BSET_NRR_COUNT|
602 SD_CONF_BSET_BSY_RETRY_COUNT|
603 SD_CONF_BSET_RST_RETRIES|
604 SD_CONF_BSET_MIN_THROTTLE|
605 SD_CONF_BSET_DISKSORT_DISABLED|
606 SD_CONF_BSET_LUN_RESET_ENABLED,
607 &pirus_properties },
608 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE |
609 SD_CONF_BSET_NRR_COUNT|
610 SD_CONF_BSET_BSY_RETRY_COUNT|
611 SD_CONF_BSET_RST_RETRIES|
612 SD_CONF_BSET_MIN_THROTTLE|
613 SD_CONF_BSET_DISKSORT_DISABLED|
614 SD_CONF_BSET_LUN_RESET_ENABLED,
615 &pirus_properties },
616 { "SUN PSX1000", SD_CONF_BSET_THROTTLE |
617 SD_CONF_BSET_NRR_COUNT|
618 SD_CONF_BSET_BSY_RETRY_COUNT|
619 SD_CONF_BSET_RST_RETRIES|
620 SD_CONF_BSET_MIN_THROTTLE|
621 SD_CONF_BSET_DISKSORT_DISABLED|
622 SD_CONF_BSET_LUN_RESET_ENABLED,
623 &pirus_properties },
624 { "SUN SE6330", SD_CONF_BSET_THROTTLE |
625 SD_CONF_BSET_NRR_COUNT|
626 SD_CONF_BSET_BSY_RETRY_COUNT|
627 SD_CONF_BSET_RST_RETRIES|
628 SD_CONF_BSET_MIN_THROTTLE|
629 SD_CONF_BSET_DISKSORT_DISABLED|
630 SD_CONF_BSET_LUN_RESET_ENABLED,
631 &pirus_properties },
632 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
633 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
634 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
635 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
636 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
637 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
638 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
639 #endif /* fibre or NON-sparc platforms */
640 #if (defined(__i386) || defined(__amd64))
641 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
642 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
643 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
644 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
645 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
646 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
647 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
648 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
649 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
650 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
651 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
652 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL },
653 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
654 &symbios_properties },
655 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
656 &lsi_properties_scsi },
657 #if defined(__i386) || defined(__amd64)
658 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
659 | SD_CONF_BSET_READSUB_BCD
660 | SD_CONF_BSET_READ_TOC_ADDR_BCD
661 | SD_CONF_BSET_NO_READ_HEADER
662 | SD_CONF_BSET_READ_CD_XD4), NULL },
664 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
665 | SD_CONF_BSET_READSUB_BCD
666 | SD_CONF_BSET_READ_TOC_ADDR_BCD
667 | SD_CONF_BSET_NO_READ_HEADER
668 | SD_CONF_BSET_READ_CD_XD4), NULL },
669 #endif /* __i386 || __amd64 */
670 #endif /* sparc NON-fibre or NON-sparc platforms */
672 #if (defined(SD_PROP_TST))
673 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE
674 | SD_CONF_BSET_CTYPE
675 | SD_CONF_BSET_NRR_COUNT
676 | SD_CONF_BSET_FAB_DEVID
677 | SD_CONF_BSET_NOCACHE
678 | SD_CONF_BSET_BSY_RETRY_COUNT
679 | SD_CONF_BSET_PLAYMSF_BCD
680 | SD_CONF_BSET_READSUB_BCD
681 | SD_CONF_BSET_READ_TOC_TRK_BCD
682 | SD_CONF_BSET_READ_TOC_ADDR_BCD
683 | SD_CONF_BSET_NO_READ_HEADER
684 | SD_CONF_BSET_READ_CD_XD4
685 | SD_CONF_BSET_RST_RETRIES
686 | SD_CONF_BSET_RSV_REL_TIME
687 | SD_CONF_BSET_TUR_CHECK), &tst_properties},
688 #endif
691 static const int sd_disk_table_size =
692 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
695 * Emulation mode disk drive VID/PID table
697 static char sd_flash_dev_table[][25] = {
698 "ATA MARVELL SD88SA02",
699 "MARVELL SD88SA02",
700 "TOSHIBA THNSNV05",
703 static const int sd_flash_dev_table_size =
704 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
706 #define SD_INTERCONNECT_PARALLEL 0
707 #define SD_INTERCONNECT_FABRIC 1
708 #define SD_INTERCONNECT_FIBRE 2
709 #define SD_INTERCONNECT_SSA 3
710 #define SD_INTERCONNECT_SATA 4
711 #define SD_INTERCONNECT_SAS 5
713 #define SD_IS_PARALLEL_SCSI(un) \
714 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
715 #define SD_IS_SERIAL(un) \
716 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
717 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
720 * Definitions used by device id registration routines
722 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */
723 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */
724 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */
726 static kmutex_t sd_sense_mutex = {0};
729 * Macros for updates of the driver state
731 #define New_state(un, s) \
732 (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
733 #define Restore_state(un) \
734 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
736 static struct sd_cdbinfo sd_cdbtab[] = {
737 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
738 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
739 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
740 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
744 * Specifies the number of seconds that must have elapsed since the last
745 * cmd. has completed for a device to be declared idle to the PM framework.
747 static int sd_pm_idletime = 1;
750 * Internal function prototypes
753 typedef struct unmap_param_hdr_s {
754 uint16_t uph_data_len;
755 uint16_t uph_descr_data_len;
756 uint32_t uph_reserved;
757 } unmap_param_hdr_t;
759 typedef struct unmap_blk_descr_s {
760 uint64_t ubd_lba;
761 uint32_t ubd_lba_cnt;
762 uint32_t ubd_reserved;
763 } unmap_blk_descr_t;
765 /* Max number of block descriptors in UNMAP command */
766 #define SD_UNMAP_MAX_DESCR \
767 ((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
768 /* Max size of the UNMAP parameter list in bytes */
769 #define SD_UNMAP_PARAM_LIST_MAXSZ (sizeof (unmap_param_hdr_t) + \
770 SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
772 int _init(void);
773 int _fini(void);
774 int _info(struct modinfo *modinfop);
776 /*PRINTFLIKE3*/
777 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
778 /*PRINTFLIKE3*/
779 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
780 /*PRINTFLIKE3*/
781 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
783 static int sdprobe(dev_info_t *devi);
784 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
785 void **result);
786 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
787 int mod_flags, char *name, caddr_t valuep, int *lengthp);
790 * Smart probe for parallel scsi
792 static void sd_scsi_probe_cache_init(void);
793 static void sd_scsi_probe_cache_fini(void);
794 static void sd_scsi_clear_probe_cache(void);
795 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
798 * Attached luns on target for parallel scsi
800 static void sd_scsi_target_lun_init(void);
801 static void sd_scsi_target_lun_fini(void);
802 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
803 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
805 static int sd_spin_up_unit(sd_ssc_t *ssc);
808 * Using sd_ssc_init to establish sd_ssc_t struct
809 * Using sd_ssc_send to send uscsi internal command
810 * Using sd_ssc_fini to free sd_ssc_t struct
812 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
813 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
814 int flag, enum uio_seg dataspace, int path_flag);
815 static void sd_ssc_fini(sd_ssc_t *ssc);
818 * Using sd_ssc_assessment to set correct type-of-assessment
819 * Using sd_ssc_post to post ereport & system log
820 * sd_ssc_post will call sd_ssc_print to print system log
821 * sd_ssc_post will call sd_ssd_ereport_post to post ereport
823 static void sd_ssc_assessment(sd_ssc_t *ssc,
824 enum sd_type_assessment tp_assess);
826 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
827 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
828 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
829 enum sd_driver_assessment drv_assess);
832 * Using sd_ssc_set_info to mark an un-decodable-data error.
833 * Using sd_ssc_extract_info to transfer information from internal
834 * data structures to sd_ssc_t.
836 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
837 const char *fmt, ...);
838 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
839 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
841 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
842 enum uio_seg dataspace, int path_flag);
844 #ifdef _LP64
845 static void sd_enable_descr_sense(sd_ssc_t *ssc);
846 static void sd_reenable_dsense_task(void *arg);
847 #endif /* _LP64 */
849 static void sd_set_mmc_caps(sd_ssc_t *ssc);
851 static void sd_read_unit_properties(struct sd_lun *un);
852 static int sd_process_sdconf_file(struct sd_lun *un);
853 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
854 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
855 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
856 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
857 int *data_list, sd_tunables *values);
858 static void sd_process_sdconf_table(struct sd_lun *un);
859 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
860 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
861 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
862 int list_len, char *dataname_ptr);
863 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
864 sd_tunables *prop_list);
866 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
867 int reservation_flag);
868 static int sd_get_devid(sd_ssc_t *ssc);
869 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
870 static int sd_write_deviceid(sd_ssc_t *ssc);
871 static int sd_check_vpd_page_support(sd_ssc_t *ssc);
873 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
874 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
876 static int sd_ddi_suspend(dev_info_t *devi);
877 static int sd_ddi_resume(dev_info_t *devi);
878 static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
879 static int sdpower(dev_info_t *devi, int component, int level);
881 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
882 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
883 static int sd_unit_attach(dev_info_t *devi);
884 static int sd_unit_detach(dev_info_t *devi);
886 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
887 static void sd_create_errstats(struct sd_lun *un, int instance);
888 static void sd_set_errstats(struct sd_lun *un);
889 static void sd_set_pstats(struct sd_lun *un);
891 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
892 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
893 static int sd_send_polled_RQS(struct sd_lun *un);
894 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
898 * Defines for sd_cache_control
901 #define SD_CACHE_ENABLE 1
902 #define SD_CACHE_DISABLE 0
903 #define SD_CACHE_NOCHANGE -1
905 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
906 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
907 static void sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
908 static void sd_get_nv_sup(sd_ssc_t *ssc);
909 static dev_t sd_make_device(dev_info_t *devi);
910 static void sd_check_bdc_vpd(sd_ssc_t *ssc);
911 static void sd_check_emulation_mode(sd_ssc_t *ssc);
912 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
913 uint64_t capacity);
916 * Driver entry point functions.
918 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
919 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
920 static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
922 static void sdmin(struct buf *bp);
923 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
924 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
925 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
926 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
928 static int sdstrategy(struct buf *bp);
929 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
932 * Function prototypes for layering functions in the iostart chain.
934 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
935 struct buf *bp);
936 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
937 struct buf *bp);
938 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
939 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
940 struct buf *bp);
941 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
942 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
945 * Function prototypes for layering functions in the iodone chain.
947 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
948 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
949 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
950 struct buf *bp);
951 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
952 struct buf *bp);
953 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
954 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
955 struct buf *bp);
956 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
959 * Prototypes for functions to support buf(9S) based IO.
961 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
962 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
963 static void sd_destroypkt_for_buf(struct buf *);
964 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
965 struct buf *bp, int flags,
966 int (*callback)(caddr_t), caddr_t callback_arg,
967 diskaddr_t lba, uint32_t blockcount);
968 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
969 struct buf *bp, diskaddr_t lba, uint32_t blockcount);
972 * Prototypes for functions to support USCSI IO.
974 static int sd_uscsi_strategy(struct buf *bp);
975 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
976 static void sd_destroypkt_for_uscsi(struct buf *);
978 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
979 uchar_t chain_type, void *pktinfop);
981 static int sd_pm_entry(struct sd_lun *un);
982 static void sd_pm_exit(struct sd_lun *un);
984 static void sd_pm_idletimeout_handler(void *arg);
987 * sd_core internal functions (used at the sd_core_io layer).
989 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
990 static void sdintr(struct scsi_pkt *pktp);
991 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
993 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
994 enum uio_seg dataspace, int path_flag);
996 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
997 daddr_t blkno, int (*func)(struct buf *));
998 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
999 uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1000 static void sd_bioclone_free(struct buf *bp);
1001 static void sd_shadow_buf_free(struct buf *bp);
1003 static void sd_print_transport_rejected_message(struct sd_lun *un,
1004 struct sd_xbuf *xp, int code);
1005 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1006 void *arg, int code);
1007 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1008 void *arg, int code);
1009 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1010 void *arg, int code);
1012 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1013 int retry_check_flag,
1014 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1015 int c),
1016 void *user_arg, int failure_code, clock_t retry_delay,
1017 void (*statp)(kstat_io_t *));
1019 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1020 clock_t retry_delay, void (*statp)(kstat_io_t *));
1022 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1023 struct scsi_pkt *pktp);
1024 static void sd_start_retry_command(void *arg);
1025 static void sd_start_direct_priority_command(void *arg);
1026 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1027 int errcode);
1028 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1029 struct buf *bp, int errcode);
1030 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1031 static void sd_sync_with_callback(struct sd_lun *un);
1032 static int sdrunout(caddr_t arg);
1034 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1035 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1037 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1038 static void sd_restore_throttle(void *arg);
1040 static void sd_init_cdb_limits(struct sd_lun *un);
1042 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1043 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1046 * Error handling functions
1048 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1049 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1050 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1051 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1052 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1053 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1054 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1055 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1057 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1058 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1059 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1060 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1061 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1062 struct sd_xbuf *xp, size_t actual_len);
1063 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1064 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1066 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1067 void *arg, int code);
1069 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1070 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1071 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1072 uint8_t *sense_datap,
1073 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1074 static void sd_sense_key_not_ready(struct sd_lun *un,
1075 uint8_t *sense_datap,
1076 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1077 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1078 uint8_t *sense_datap,
1079 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1080 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1081 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1082 static void sd_sense_key_unit_attention(struct sd_lun *un,
1083 uint8_t *sense_datap,
1084 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1085 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1086 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1087 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1088 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1089 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1090 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1091 static void sd_sense_key_default(struct sd_lun *un,
1092 uint8_t *sense_datap,
1093 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1095 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1096 void *arg, int flag);
1098 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1099 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1100 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1101 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1102 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1103 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1104 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1105 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1106 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1107 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1108 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1109 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1110 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1111 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1112 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1113 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1115 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1117 static void sd_start_stop_unit_callback(void *arg);
1118 static void sd_start_stop_unit_task(void *arg);
1120 static void sd_taskq_create(void);
1121 static void sd_taskq_delete(void);
1122 static void sd_target_change_task(void *arg);
1123 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1124 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1125 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1126 static void sd_media_change_task(void *arg);
1128 static int sd_handle_mchange(struct sd_lun *un);
1129 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1130 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1131 uint32_t *lbap, int path_flag);
1132 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1133 uint32_t *lbap, uint32_t *psp, int path_flag);
1134 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1135 int flag, int path_flag);
1136 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1137 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1138 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1139 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1140 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1141 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1142 uchar_t usr_cmd, uchar_t *usr_bufp);
1143 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1144 struct dk_callback *dkc);
1145 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1146 static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1147 int flag);
1148 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1149 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1150 uchar_t *bufaddr, uint_t buflen, int path_flag);
1151 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1152 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1153 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1154 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1155 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1156 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1157 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1158 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1159 size_t buflen, daddr_t start_block, int path_flag);
1160 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1161 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1162 path_flag)
1163 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1164 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1165 path_flag)
1167 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1168 uint16_t buflen, uchar_t page_code, uchar_t page_control,
1169 uint16_t param_ptr, int path_flag);
1170 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1171 uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1172 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1174 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1175 static void sd_free_rqs(struct sd_lun *un);
1177 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1178 uchar_t *data, int len, int fmt);
1179 static void sd_panic_for_res_conflict(struct sd_lun *un);
1182 * Disk Ioctl Function Prototypes
1184 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1185 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1186 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1187 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1190 * Multi-host Ioctl Prototypes
1192 static int sd_check_mhd(dev_t dev, int interval);
1193 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1194 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1195 static char *sd_sname(uchar_t status);
1196 static void sd_mhd_resvd_recover(void *arg);
1197 static void sd_resv_reclaim_thread();
1198 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1199 static int sd_reserve_release(dev_t dev, int cmd);
1200 static void sd_rmv_resv_reclaim_req(dev_t dev);
1201 static void sd_mhd_reset_notify_cb(caddr_t arg);
1202 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1203 mhioc_inkeys_t *usrp, int flag);
1204 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1205 mhioc_inresvs_t *usrp, int flag);
1206 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1207 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1208 static int sd_mhdioc_release(dev_t dev);
1209 static int sd_mhdioc_register_devid(dev_t dev);
1210 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1211 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1214 * SCSI removable prototypes
1216 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1217 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1218 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1219 static int sr_pause_resume(dev_t dev, int mode);
1220 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1221 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1222 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1223 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1224 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1225 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1226 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1227 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1228 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1229 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1230 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1231 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1232 static int sr_eject(dev_t dev);
1233 static void sr_ejected(register struct sd_lun *un);
1234 static int sr_check_wp(dev_t dev);
1235 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1236 static int sd_check_media(dev_t dev, enum dkio_state state);
1237 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1238 static void sd_delayed_cv_broadcast(void *arg);
1239 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1240 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1242 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1245 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1247 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1248 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1249 static void sd_wm_cache_destructor(void *wm, void *un);
1250 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1251 daddr_t endb, ushort_t typ);
1252 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1253 daddr_t endb);
1254 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1255 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1256 static void sd_read_modify_write_task(void * arg);
1257 static int
1258 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1259 struct buf **bpp);
1263 * Function prototypes for failfast support.
1265 static void sd_failfast_flushq(struct sd_lun *un);
1266 static int sd_failfast_flushq_callback(struct buf *bp);
1269 * Function prototypes to check for lsi devices
1271 static void sd_is_lsi(struct sd_lun *un);
1274 * Function prototypes for partial DMA support
1276 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1277 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1280 /* Function prototypes for cmlb */
1281 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1282 diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1284 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1287 * For printing RMW warning message timely
1289 static void sd_rmw_msg_print_handler(void *arg);
1292 * Constants for failfast support:
1294 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1295 * failfast processing being performed.
1297 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1298 * failfast processing on all bufs with B_FAILFAST set.
1301 #define SD_FAILFAST_INACTIVE 0
1302 #define SD_FAILFAST_ACTIVE 1
1305 * Bitmask to control behavior of buf(9S) flushes when a transition to
1306 * the failfast state occurs. Optional bits include:
1308 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1309 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1310 * be flushed.
1312 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1313 * driver, in addition to the regular wait queue. This includes the xbuf
1314 * queues. When clear, only the driver's wait queue will be flushed.
1316 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
1317 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
1320 * The default behavior is to only flush bufs that have B_FAILFAST set, but
1321 * to flush all queues within the driver.
1323 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1327 * SD Testing Fault Injection
1329 #ifdef SD_FAULT_INJECTION
1330 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1331 static void sd_faultinjection(struct scsi_pkt *pktp);
1332 static void sd_injection_log(char *buf, struct sd_lun *un);
1333 #endif
1336 * Device driver ops vector
1338 static struct cb_ops sd_cb_ops = {
1339 sdopen, /* open */
1340 sdclose, /* close */
1341 sdstrategy, /* strategy */
1342 nodev, /* print */
1343 sddump, /* dump */
1344 sdread, /* read */
1345 sdwrite, /* write */
1346 sdioctl, /* ioctl */
1347 nodev, /* devmap */
1348 nodev, /* mmap */
1349 nodev, /* segmap */
1350 nochpoll, /* poll */
1351 sd_prop_op, /* cb_prop_op */
1352 0, /* streamtab */
1353 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1354 CB_REV, /* cb_rev */
1355 sdaread, /* async I/O read entry point */
1356 sdawrite /* async I/O write entry point */
1359 struct dev_ops sd_ops = {
1360 DEVO_REV, /* devo_rev, */
1361 0, /* refcnt */
1362 sdinfo, /* info */
1363 nulldev, /* identify */
1364 sdprobe, /* probe */
1365 sdattach, /* attach */
1366 sddetach, /* detach */
1367 nodev, /* reset */
1368 &sd_cb_ops, /* driver operations */
1369 NULL, /* bus operations */
1370 sdpower, /* power */
1371 ddi_quiesce_not_needed, /* quiesce */
1375 * This is the loadable module wrapper.
1377 #include <sys/modctl.h>
1379 static struct modldrv modldrv = {
1380 &mod_driverops, /* Type of module. This one is a driver */
1381 SD_MODULE_NAME, /* Module name. */
1382 &sd_ops /* driver ops */
1385 static struct modlinkage modlinkage = {
1386 MODREV_1, &modldrv, NULL
1389 static cmlb_tg_ops_t sd_tgops = {
1390 TG_DK_OPS_VERSION_1,
1391 sd_tg_rdwr,
1392 sd_tg_getinfo
1395 static struct scsi_asq_key_strings sd_additional_codes[] = {
1396 0x81, 0, "Logical Unit is Reserved",
1397 0x85, 0, "Audio Address Not Valid",
1398 0xb6, 0, "Media Load Mechanism Failed",
1399 0xB9, 0, "Audio Play Operation Aborted",
1400 0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1401 0x53, 2, "Medium removal prevented",
1402 0x6f, 0, "Authentication failed during key exchange",
1403 0x6f, 1, "Key not present",
1404 0x6f, 2, "Key not established",
1405 0x6f, 3, "Read without proper authentication",
1406 0x6f, 4, "Mismatched region to this logical unit",
1407 0x6f, 5, "Region reset count error",
1408 0xffff, 0x0, NULL
1413 * Struct for passing printing information for sense data messages
1415 struct sd_sense_info {
1416 int ssi_severity;
1417 int ssi_pfa_flag;
1421 * Table of function pointers for iostart-side routines. Separate "chains"
1422 * of layered function calls are formed by placing the function pointers
1423 * sequentially in the desired order. Functions are called according to an
1424 * incrementing table index ordering. The last function in each chain must
1425 * be sd_core_iostart(). The corresponding iodone-side routines are expected
1426 * in the sd_iodone_chain[] array.
1428 * Note: It may seem more natural to organize both the iostart and iodone
1429 * functions together, into an array of structures (or some similar
1430 * organization) with a common index, rather than two separate arrays which
1431 * must be maintained in synchronization. The purpose of this division is
1432 * to achieve improved performance: individual arrays allows for more
1433 * effective cache line utilization on certain platforms.
1436 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1439 static sd_chain_t sd_iostart_chain[] = {
1441 /* Chain for buf IO for disk drive targets (PM enabled) */
1442 sd_mapblockaddr_iostart, /* Index: 0 */
1443 sd_pm_iostart, /* Index: 1 */
1444 sd_core_iostart, /* Index: 2 */
1446 /* Chain for buf IO for disk drive targets (PM disabled) */
1447 sd_mapblockaddr_iostart, /* Index: 3 */
1448 sd_core_iostart, /* Index: 4 */
1451 * Chain for buf IO for removable-media or large sector size
1452 * disk drive targets with RMW needed (PM enabled)
1454 sd_mapblockaddr_iostart, /* Index: 5 */
1455 sd_mapblocksize_iostart, /* Index: 6 */
1456 sd_pm_iostart, /* Index: 7 */
1457 sd_core_iostart, /* Index: 8 */
1460 * Chain for buf IO for removable-media or large sector size
1461 * disk drive targets with RMW needed (PM disabled)
1463 sd_mapblockaddr_iostart, /* Index: 9 */
1464 sd_mapblocksize_iostart, /* Index: 10 */
1465 sd_core_iostart, /* Index: 11 */
1467 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1468 sd_mapblockaddr_iostart, /* Index: 12 */
1469 sd_checksum_iostart, /* Index: 13 */
1470 sd_pm_iostart, /* Index: 14 */
1471 sd_core_iostart, /* Index: 15 */
1473 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1474 sd_mapblockaddr_iostart, /* Index: 16 */
1475 sd_checksum_iostart, /* Index: 17 */
1476 sd_core_iostart, /* Index: 18 */
1478 /* Chain for USCSI commands (all targets) */
1479 sd_pm_iostart, /* Index: 19 */
1480 sd_core_iostart, /* Index: 20 */
1482 /* Chain for checksumming USCSI commands (all targets) */
1483 sd_checksum_uscsi_iostart, /* Index: 21 */
1484 sd_pm_iostart, /* Index: 22 */
1485 sd_core_iostart, /* Index: 23 */
1487 /* Chain for "direct" USCSI commands (all targets) */
1488 sd_core_iostart, /* Index: 24 */
1490 /* Chain for "direct priority" USCSI commands (all targets) */
1491 sd_core_iostart, /* Index: 25 */
1494 * Chain for buf IO for large sector size disk drive targets
1495 * with RMW needed with checksumming (PM enabled)
1497 sd_mapblockaddr_iostart, /* Index: 26 */
1498 sd_mapblocksize_iostart, /* Index: 27 */
1499 sd_checksum_iostart, /* Index: 28 */
1500 sd_pm_iostart, /* Index: 29 */
1501 sd_core_iostart, /* Index: 30 */
1504 * Chain for buf IO for large sector size disk drive targets
1505 * with RMW needed with checksumming (PM disabled)
1507 sd_mapblockaddr_iostart, /* Index: 31 */
1508 sd_mapblocksize_iostart, /* Index: 32 */
1509 sd_checksum_iostart, /* Index: 33 */
1510 sd_core_iostart, /* Index: 34 */
1515 * Macros to locate the first function of each iostart chain in the
1516 * sd_iostart_chain[] array. These are located by the index in the array.
1518 #define SD_CHAIN_DISK_IOSTART 0
1519 #define SD_CHAIN_DISK_IOSTART_NO_PM 3
1520 #define SD_CHAIN_MSS_DISK_IOSTART 5
1521 #define SD_CHAIN_RMMEDIA_IOSTART 5
1522 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9
1523 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9
1524 #define SD_CHAIN_CHKSUM_IOSTART 12
1525 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16
1526 #define SD_CHAIN_USCSI_CMD_IOSTART 19
1527 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21
1528 #define SD_CHAIN_DIRECT_CMD_IOSTART 24
1529 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25
1530 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26
1531 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31
1535 * Table of function pointers for the iodone-side routines for the driver-
1536 * internal layering mechanism. The calling sequence for iodone routines
1537 * uses a decrementing table index, so the last routine called in a chain
1538 * must be at the lowest array index location for that chain. The last
1539 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1540 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering
1541 * of the functions in an iodone side chain must correspond to the ordering
1542 * of the iostart routines for that chain. Note that there is no iodone
1543 * side routine that corresponds to sd_core_iostart(), so there is no
1544 * entry in the table for this.
1547 static sd_chain_t sd_iodone_chain[] = {
1549 /* Chain for buf IO for disk drive targets (PM enabled) */
1550 sd_buf_iodone, /* Index: 0 */
1551 sd_mapblockaddr_iodone, /* Index: 1 */
1552 sd_pm_iodone, /* Index: 2 */
1554 /* Chain for buf IO for disk drive targets (PM disabled) */
1555 sd_buf_iodone, /* Index: 3 */
1556 sd_mapblockaddr_iodone, /* Index: 4 */
1559 * Chain for buf IO for removable-media or large sector size
1560 * disk drive targets with RMW needed (PM enabled)
1562 sd_buf_iodone, /* Index: 5 */
1563 sd_mapblockaddr_iodone, /* Index: 6 */
1564 sd_mapblocksize_iodone, /* Index: 7 */
1565 sd_pm_iodone, /* Index: 8 */
1568 * Chain for buf IO for removable-media or large sector size
1569 * disk drive targets with RMW needed (PM disabled)
1571 sd_buf_iodone, /* Index: 9 */
1572 sd_mapblockaddr_iodone, /* Index: 10 */
1573 sd_mapblocksize_iodone, /* Index: 11 */
1575 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1576 sd_buf_iodone, /* Index: 12 */
1577 sd_mapblockaddr_iodone, /* Index: 13 */
1578 sd_checksum_iodone, /* Index: 14 */
1579 sd_pm_iodone, /* Index: 15 */
1581 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1582 sd_buf_iodone, /* Index: 16 */
1583 sd_mapblockaddr_iodone, /* Index: 17 */
1584 sd_checksum_iodone, /* Index: 18 */
1586 /* Chain for USCSI commands (non-checksum targets) */
1587 sd_uscsi_iodone, /* Index: 19 */
1588 sd_pm_iodone, /* Index: 20 */
1590 /* Chain for USCSI commands (checksum targets) */
1591 sd_uscsi_iodone, /* Index: 21 */
1592 sd_checksum_uscsi_iodone, /* Index: 22 */
1593 sd_pm_iodone, /* Index: 22 */
1595 /* Chain for "direct" USCSI commands (all targets) */
1596 sd_uscsi_iodone, /* Index: 24 */
1598 /* Chain for "direct priority" USCSI commands (all targets) */
1599 sd_uscsi_iodone, /* Index: 25 */
1602 * Chain for buf IO for large sector size disk drive targets
1603 * with checksumming (PM enabled)
1605 sd_buf_iodone, /* Index: 26 */
1606 sd_mapblockaddr_iodone, /* Index: 27 */
1607 sd_mapblocksize_iodone, /* Index: 28 */
1608 sd_checksum_iodone, /* Index: 29 */
1609 sd_pm_iodone, /* Index: 30 */
1612 * Chain for buf IO for large sector size disk drive targets
1613 * with checksumming (PM disabled)
1615 sd_buf_iodone, /* Index: 31 */
1616 sd_mapblockaddr_iodone, /* Index: 32 */
1617 sd_mapblocksize_iodone, /* Index: 33 */
1618 sd_checksum_iodone, /* Index: 34 */
1623 * Macros to locate the "first" function in the sd_iodone_chain[] array for
1624 * each iodone-side chain. These are located by the array index, but as the
1625 * iodone side functions are called in a decrementing-index order, the
1626 * highest index number in each chain must be specified (as these correspond
1627 * to the first function in the iodone chain that will be called by the core
1628 * at IO completion time).
1631 #define SD_CHAIN_DISK_IODONE 2
1632 #define SD_CHAIN_DISK_IODONE_NO_PM 4
1633 #define SD_CHAIN_RMMEDIA_IODONE 8
1634 #define SD_CHAIN_MSS_DISK_IODONE 8
1635 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11
1636 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11
1637 #define SD_CHAIN_CHKSUM_IODONE 15
1638 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18
1639 #define SD_CHAIN_USCSI_CMD_IODONE 20
1640 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22
1641 #define SD_CHAIN_DIRECT_CMD_IODONE 24
1642 #define SD_CHAIN_PRIORITY_CMD_IODONE 25
1643 #define SD_CHAIN_MSS_CHKSUM_IODONE 30
1644 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34
1649 * Array to map a layering chain index to the appropriate initpkt routine.
1650 * The redundant entries are present so that the index used for accessing
1651 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1652 * with this table as well.
1654 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1656 static sd_initpkt_t sd_initpkt_map[] = {
1658 /* Chain for buf IO for disk drive targets (PM enabled) */
1659 sd_initpkt_for_buf, /* Index: 0 */
1660 sd_initpkt_for_buf, /* Index: 1 */
1661 sd_initpkt_for_buf, /* Index: 2 */
1663 /* Chain for buf IO for disk drive targets (PM disabled) */
1664 sd_initpkt_for_buf, /* Index: 3 */
1665 sd_initpkt_for_buf, /* Index: 4 */
1668 * Chain for buf IO for removable-media or large sector size
1669 * disk drive targets (PM enabled)
1671 sd_initpkt_for_buf, /* Index: 5 */
1672 sd_initpkt_for_buf, /* Index: 6 */
1673 sd_initpkt_for_buf, /* Index: 7 */
1674 sd_initpkt_for_buf, /* Index: 8 */
1677 * Chain for buf IO for removable-media or large sector size
1678 * disk drive targets (PM disabled)
1680 sd_initpkt_for_buf, /* Index: 9 */
1681 sd_initpkt_for_buf, /* Index: 10 */
1682 sd_initpkt_for_buf, /* Index: 11 */
1684 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1685 sd_initpkt_for_buf, /* Index: 12 */
1686 sd_initpkt_for_buf, /* Index: 13 */
1687 sd_initpkt_for_buf, /* Index: 14 */
1688 sd_initpkt_for_buf, /* Index: 15 */
1690 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1691 sd_initpkt_for_buf, /* Index: 16 */
1692 sd_initpkt_for_buf, /* Index: 17 */
1693 sd_initpkt_for_buf, /* Index: 18 */
1695 /* Chain for USCSI commands (non-checksum targets) */
1696 sd_initpkt_for_uscsi, /* Index: 19 */
1697 sd_initpkt_for_uscsi, /* Index: 20 */
1699 /* Chain for USCSI commands (checksum targets) */
1700 sd_initpkt_for_uscsi, /* Index: 21 */
1701 sd_initpkt_for_uscsi, /* Index: 22 */
1702 sd_initpkt_for_uscsi, /* Index: 22 */
1704 /* Chain for "direct" USCSI commands (all targets) */
1705 sd_initpkt_for_uscsi, /* Index: 24 */
1707 /* Chain for "direct priority" USCSI commands (all targets) */
1708 sd_initpkt_for_uscsi, /* Index: 25 */
1711 * Chain for buf IO for large sector size disk drive targets
1712 * with checksumming (PM enabled)
1714 sd_initpkt_for_buf, /* Index: 26 */
1715 sd_initpkt_for_buf, /* Index: 27 */
1716 sd_initpkt_for_buf, /* Index: 28 */
1717 sd_initpkt_for_buf, /* Index: 29 */
1718 sd_initpkt_for_buf, /* Index: 30 */
1721 * Chain for buf IO for large sector size disk drive targets
1722 * with checksumming (PM disabled)
1724 sd_initpkt_for_buf, /* Index: 31 */
1725 sd_initpkt_for_buf, /* Index: 32 */
1726 sd_initpkt_for_buf, /* Index: 33 */
1727 sd_initpkt_for_buf, /* Index: 34 */
1732 * Array to map a layering chain index to the appropriate destroypktpkt routine.
1733 * The redundant entries are present so that the index used for accessing
1734 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1735 * with this table as well.
1737 typedef void (*sd_destroypkt_t)(struct buf *);
1739 static sd_destroypkt_t sd_destroypkt_map[] = {
1741 /* Chain for buf IO for disk drive targets (PM enabled) */
1742 sd_destroypkt_for_buf, /* Index: 0 */
1743 sd_destroypkt_for_buf, /* Index: 1 */
1744 sd_destroypkt_for_buf, /* Index: 2 */
1746 /* Chain for buf IO for disk drive targets (PM disabled) */
1747 sd_destroypkt_for_buf, /* Index: 3 */
1748 sd_destroypkt_for_buf, /* Index: 4 */
1751 * Chain for buf IO for removable-media or large sector size
1752 * disk drive targets (PM enabled)
1754 sd_destroypkt_for_buf, /* Index: 5 */
1755 sd_destroypkt_for_buf, /* Index: 6 */
1756 sd_destroypkt_for_buf, /* Index: 7 */
1757 sd_destroypkt_for_buf, /* Index: 8 */
1760 * Chain for buf IO for removable-media or large sector size
1761 * disk drive targets (PM disabled)
1763 sd_destroypkt_for_buf, /* Index: 9 */
1764 sd_destroypkt_for_buf, /* Index: 10 */
1765 sd_destroypkt_for_buf, /* Index: 11 */
1767 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1768 sd_destroypkt_for_buf, /* Index: 12 */
1769 sd_destroypkt_for_buf, /* Index: 13 */
1770 sd_destroypkt_for_buf, /* Index: 14 */
1771 sd_destroypkt_for_buf, /* Index: 15 */
1773 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1774 sd_destroypkt_for_buf, /* Index: 16 */
1775 sd_destroypkt_for_buf, /* Index: 17 */
1776 sd_destroypkt_for_buf, /* Index: 18 */
1778 /* Chain for USCSI commands (non-checksum targets) */
1779 sd_destroypkt_for_uscsi, /* Index: 19 */
1780 sd_destroypkt_for_uscsi, /* Index: 20 */
1782 /* Chain for USCSI commands (checksum targets) */
1783 sd_destroypkt_for_uscsi, /* Index: 21 */
1784 sd_destroypkt_for_uscsi, /* Index: 22 */
1785 sd_destroypkt_for_uscsi, /* Index: 22 */
1787 /* Chain for "direct" USCSI commands (all targets) */
1788 sd_destroypkt_for_uscsi, /* Index: 24 */
1790 /* Chain for "direct priority" USCSI commands (all targets) */
1791 sd_destroypkt_for_uscsi, /* Index: 25 */
1794 * Chain for buf IO for large sector size disk drive targets
1795 * with checksumming (PM disabled)
1797 sd_destroypkt_for_buf, /* Index: 26 */
1798 sd_destroypkt_for_buf, /* Index: 27 */
1799 sd_destroypkt_for_buf, /* Index: 28 */
1800 sd_destroypkt_for_buf, /* Index: 29 */
1801 sd_destroypkt_for_buf, /* Index: 30 */
1804 * Chain for buf IO for large sector size disk drive targets
1805 * with checksumming (PM enabled)
1807 sd_destroypkt_for_buf, /* Index: 31 */
1808 sd_destroypkt_for_buf, /* Index: 32 */
1809 sd_destroypkt_for_buf, /* Index: 33 */
1810 sd_destroypkt_for_buf, /* Index: 34 */
1816 * Array to map a layering chain index to the appropriate chain "type".
1817 * The chain type indicates a specific property/usage of the chain.
1818 * The redundant entries are present so that the index used for accessing
1819 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1820 * with this table as well.
1823 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */
1824 #define SD_CHAIN_BUFIO 1 /* regular buf IO */
1825 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */
1826 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */
1827 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */
1828 /* (for error recovery) */
1830 static int sd_chain_type_map[] = {
1832 /* Chain for buf IO for disk drive targets (PM enabled) */
1833 SD_CHAIN_BUFIO, /* Index: 0 */
1834 SD_CHAIN_BUFIO, /* Index: 1 */
1835 SD_CHAIN_BUFIO, /* Index: 2 */
1837 /* Chain for buf IO for disk drive targets (PM disabled) */
1838 SD_CHAIN_BUFIO, /* Index: 3 */
1839 SD_CHAIN_BUFIO, /* Index: 4 */
1842 * Chain for buf IO for removable-media or large sector size
1843 * disk drive targets (PM enabled)
1845 SD_CHAIN_BUFIO, /* Index: 5 */
1846 SD_CHAIN_BUFIO, /* Index: 6 */
1847 SD_CHAIN_BUFIO, /* Index: 7 */
1848 SD_CHAIN_BUFIO, /* Index: 8 */
1851 * Chain for buf IO for removable-media or large sector size
1852 * disk drive targets (PM disabled)
1854 SD_CHAIN_BUFIO, /* Index: 9 */
1855 SD_CHAIN_BUFIO, /* Index: 10 */
1856 SD_CHAIN_BUFIO, /* Index: 11 */
1858 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1859 SD_CHAIN_BUFIO, /* Index: 12 */
1860 SD_CHAIN_BUFIO, /* Index: 13 */
1861 SD_CHAIN_BUFIO, /* Index: 14 */
1862 SD_CHAIN_BUFIO, /* Index: 15 */
1864 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1865 SD_CHAIN_BUFIO, /* Index: 16 */
1866 SD_CHAIN_BUFIO, /* Index: 17 */
1867 SD_CHAIN_BUFIO, /* Index: 18 */
1869 /* Chain for USCSI commands (non-checksum targets) */
1870 SD_CHAIN_USCSI, /* Index: 19 */
1871 SD_CHAIN_USCSI, /* Index: 20 */
1873 /* Chain for USCSI commands (checksum targets) */
1874 SD_CHAIN_USCSI, /* Index: 21 */
1875 SD_CHAIN_USCSI, /* Index: 22 */
1876 SD_CHAIN_USCSI, /* Index: 23 */
1878 /* Chain for "direct" USCSI commands (all targets) */
1879 SD_CHAIN_DIRECT, /* Index: 24 */
1881 /* Chain for "direct priority" USCSI commands (all targets) */
1882 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */
1885 * Chain for buf IO for large sector size disk drive targets
1886 * with checksumming (PM enabled)
1888 SD_CHAIN_BUFIO, /* Index: 26 */
1889 SD_CHAIN_BUFIO, /* Index: 27 */
1890 SD_CHAIN_BUFIO, /* Index: 28 */
1891 SD_CHAIN_BUFIO, /* Index: 29 */
1892 SD_CHAIN_BUFIO, /* Index: 30 */
1895 * Chain for buf IO for large sector size disk drive targets
1896 * with checksumming (PM disabled)
1898 SD_CHAIN_BUFIO, /* Index: 31 */
1899 SD_CHAIN_BUFIO, /* Index: 32 */
1900 SD_CHAIN_BUFIO, /* Index: 33 */
1901 SD_CHAIN_BUFIO, /* Index: 34 */
1905 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
1906 #define SD_IS_BUFIO(xp) \
1907 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
1909 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
1910 #define SD_IS_DIRECT_PRIORITY(xp) \
1911 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
1916 * Struct, array, and macros to map a specific chain to the appropriate
1917 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
1919 * The sd_chain_index_map[] array is used at attach time to set the various
1920 * un_xxx_chain type members of the sd_lun softstate to the specific layering
1921 * chain to be used with the instance. This allows different instances to use
1922 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
1923 * and xb_chain_iodone index values in the sd_xbuf are initialized to these
1924 * values at sd_xbuf init time, this allows (1) layering chains may be changed
1925 * dynamically & without the use of locking; and (2) a layer may update the
1926 * xb_chain_io[start|done] member in a given xbuf with its current index value,
1927 * to allow for deferred processing of an IO within the same chain from a
1928 * different execution context.
1931 struct sd_chain_index {
1932 int sci_iostart_index;
1933 int sci_iodone_index;
1936 static struct sd_chain_index sd_chain_index_map[] = {
1937 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE },
1938 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM },
1939 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE },
1940 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM },
1941 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE },
1942 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM },
1943 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE },
1944 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE },
1945 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE },
1946 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE },
1947 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE },
1948 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
1954 * The following are indexes into the sd_chain_index_map[] array.
1957 /* un->un_buf_chain_type must be set to one of these */
1958 #define SD_CHAIN_INFO_DISK 0
1959 #define SD_CHAIN_INFO_DISK_NO_PM 1
1960 #define SD_CHAIN_INFO_RMMEDIA 2
1961 #define SD_CHAIN_INFO_MSS_DISK 2
1962 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3
1963 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3
1964 #define SD_CHAIN_INFO_CHKSUM 4
1965 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5
1966 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10
1967 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
1969 /* un->un_uscsi_chain_type must be set to one of these */
1970 #define SD_CHAIN_INFO_USCSI_CMD 6
1971 /* USCSI with PM disabled is the same as DIRECT */
1972 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
1973 #define SD_CHAIN_INFO_USCSI_CHKSUM 7
1975 /* un->un_direct_chain_type must be set to one of these */
1976 #define SD_CHAIN_INFO_DIRECT_CMD 8
1978 /* un->un_priority_chain_type must be set to one of these */
1979 #define SD_CHAIN_INFO_PRIORITY_CMD 9
1981 /* size for devid inquiries */
1982 #define MAX_INQUIRY_SIZE 0xF0
1985 * Macros used by functions to pass a given buf(9S) struct along to the
1986 * next function in the layering chain for further processing.
1988 * In the following macros, passing more than three arguments to the called
1989 * routines causes the optimizer for the SPARC compiler to stop doing tail
1990 * call elimination which results in significant performance degradation.
1992 #define SD_BEGIN_IOSTART(index, un, bp) \
1993 ((*(sd_iostart_chain[index]))(index, un, bp))
1995 #define SD_BEGIN_IODONE(index, un, bp) \
1996 ((*(sd_iodone_chain[index]))(index, un, bp))
1998 #define SD_NEXT_IOSTART(index, un, bp) \
1999 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2001 #define SD_NEXT_IODONE(index, un, bp) \
2002 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2005 * Function: _init
2007 * Description: This is the driver _init(9E) entry point.
2009 * Return Code: Returns the value from mod_install(9F) or
2010 * ddi_soft_state_init(9F) as appropriate.
2012 * Context: Called when driver module loaded.
2016 _init(void)
2018 int err;
2020 /* establish driver name from module name */
2021 sd_label = (char *)mod_modname(&modlinkage);
2023 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2024 SD_MAXUNIT);
2025 if (err != 0) {
2026 return (err);
2029 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2030 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL);
2031 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL);
2033 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2034 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2035 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2038 * it's ok to init here even for fibre device
2040 sd_scsi_probe_cache_init();
2042 sd_scsi_target_lun_init();
2045 * Creating taskq before mod_install ensures that all callers (threads)
2046 * that enter the module after a successful mod_install encounter
2047 * a valid taskq.
2049 sd_taskq_create();
2051 err = mod_install(&modlinkage);
2052 if (err != 0) {
2053 /* delete taskq if install fails */
2054 sd_taskq_delete();
2056 mutex_destroy(&sd_detach_mutex);
2057 mutex_destroy(&sd_log_mutex);
2058 mutex_destroy(&sd_label_mutex);
2060 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2061 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2062 cv_destroy(&sd_tr.srq_inprocess_cv);
2064 sd_scsi_probe_cache_fini();
2066 sd_scsi_target_lun_fini();
2068 ddi_soft_state_fini(&sd_state);
2070 return (err);
2073 return (err);
2078 * Function: _fini
2080 * Description: This is the driver _fini(9E) entry point.
2082 * Return Code: Returns the value from mod_remove(9F)
2084 * Context: Called when driver module is unloaded.
2088 _fini(void)
2090 int err;
2092 if ((err = mod_remove(&modlinkage)) != 0) {
2093 return (err);
2096 sd_taskq_delete();
2098 mutex_destroy(&sd_detach_mutex);
2099 mutex_destroy(&sd_log_mutex);
2100 mutex_destroy(&sd_label_mutex);
2101 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2103 sd_scsi_probe_cache_fini();
2105 sd_scsi_target_lun_fini();
2107 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2108 cv_destroy(&sd_tr.srq_inprocess_cv);
2110 ddi_soft_state_fini(&sd_state);
2112 return (err);
2117 * Function: _info
2119 * Description: This is the driver _info(9E) entry point.
2121 * Arguments: modinfop - pointer to the driver modinfo structure
2123 * Return Code: Returns the value from mod_info(9F).
2125 * Context: Kernel thread context
2129 _info(struct modinfo *modinfop)
2131 return (mod_info(&modlinkage, modinfop));
2136 * The following routines implement the driver message logging facility.
2137 * They provide component- and level- based debug output filtering.
2138 * Output may also be restricted to messages for a single instance by
2139 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2140 * to NULL, then messages for all instances are printed.
2142 * These routines have been cloned from each other due to the language
2143 * constraints of macros and variable argument list processing.
2148 * Function: sd_log_err
2150 * Description: This routine is called by the SD_ERROR macro for debug
2151 * logging of error conditions.
2153 * Arguments: comp - driver component being logged
2154 * dev - pointer to driver info structure
2155 * fmt - error string and format to be logged
2158 static void
2159 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2161 va_list ap;
2162 dev_info_t *dev;
2164 ASSERT(un != NULL);
2165 dev = SD_DEVINFO(un);
2166 ASSERT(dev != NULL);
2169 * Filter messages based on the global component and level masks.
2170 * Also print if un matches the value of sd_debug_un, or if
2171 * sd_debug_un is set to NULL.
2173 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2174 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2175 mutex_enter(&sd_log_mutex);
2176 va_start(ap, fmt);
2177 (void) vsprintf(sd_log_buf, fmt, ap);
2178 va_end(ap);
2179 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2180 mutex_exit(&sd_log_mutex);
2182 #ifdef SD_FAULT_INJECTION
2183 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2184 if (un->sd_injection_mask & comp) {
2185 mutex_enter(&sd_log_mutex);
2186 va_start(ap, fmt);
2187 (void) vsprintf(sd_log_buf, fmt, ap);
2188 va_end(ap);
2189 sd_injection_log(sd_log_buf, un);
2190 mutex_exit(&sd_log_mutex);
2192 #endif
2197 * Function: sd_log_info
2199 * Description: This routine is called by the SD_INFO macro for debug
2200 * logging of general purpose informational conditions.
2202 * Arguments: comp - driver component being logged
2203 * dev - pointer to driver info structure
2204 * fmt - info string and format to be logged
2207 static void
2208 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2210 va_list ap;
2211 dev_info_t *dev;
2213 ASSERT(un != NULL);
2214 dev = SD_DEVINFO(un);
2215 ASSERT(dev != NULL);
2218 * Filter messages based on the global component and level masks.
2219 * Also print if un matches the value of sd_debug_un, or if
2220 * sd_debug_un is set to NULL.
2222 if ((sd_component_mask & component) &&
2223 (sd_level_mask & SD_LOGMASK_INFO) &&
2224 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2225 mutex_enter(&sd_log_mutex);
2226 va_start(ap, fmt);
2227 (void) vsprintf(sd_log_buf, fmt, ap);
2228 va_end(ap);
2229 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2230 mutex_exit(&sd_log_mutex);
2232 #ifdef SD_FAULT_INJECTION
2233 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2234 if (un->sd_injection_mask & component) {
2235 mutex_enter(&sd_log_mutex);
2236 va_start(ap, fmt);
2237 (void) vsprintf(sd_log_buf, fmt, ap);
2238 va_end(ap);
2239 sd_injection_log(sd_log_buf, un);
2240 mutex_exit(&sd_log_mutex);
2242 #endif
2247 * Function: sd_log_trace
2249 * Description: This routine is called by the SD_TRACE macro for debug
2250 * logging of trace conditions (i.e. function entry/exit).
2252 * Arguments: comp - driver component being logged
2253 * dev - pointer to driver info structure
2254 * fmt - trace string and format to be logged
2257 static void
2258 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2260 va_list ap;
2261 dev_info_t *dev;
2263 ASSERT(un != NULL);
2264 dev = SD_DEVINFO(un);
2265 ASSERT(dev != NULL);
2268 * Filter messages based on the global component and level masks.
2269 * Also print if un matches the value of sd_debug_un, or if
2270 * sd_debug_un is set to NULL.
2272 if ((sd_component_mask & component) &&
2273 (sd_level_mask & SD_LOGMASK_TRACE) &&
2274 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2275 mutex_enter(&sd_log_mutex);
2276 va_start(ap, fmt);
2277 (void) vsprintf(sd_log_buf, fmt, ap);
2278 va_end(ap);
2279 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2280 mutex_exit(&sd_log_mutex);
2282 #ifdef SD_FAULT_INJECTION
2283 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2284 if (un->sd_injection_mask & component) {
2285 mutex_enter(&sd_log_mutex);
2286 va_start(ap, fmt);
2287 (void) vsprintf(sd_log_buf, fmt, ap);
2288 va_end(ap);
2289 sd_injection_log(sd_log_buf, un);
2290 mutex_exit(&sd_log_mutex);
2292 #endif
2297 * Function: sdprobe
2299 * Description: This is the driver probe(9e) entry point function.
2301 * Arguments: devi - opaque device info handle
2303 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2304 * DDI_PROBE_FAILURE: If the probe failed.
2305 * DDI_PROBE_PARTIAL: If the instance is not present now,
2306 * but may be present in the future.
2309 static int
2310 sdprobe(dev_info_t *devi)
2312 struct scsi_device *devp;
2313 int rval;
2314 int instance = ddi_get_instance(devi);
2316 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2317 return (DDI_PROBE_DONTCARE);
2320 devp = ddi_get_driver_private(devi);
2322 if (devp == NULL) {
2323 /* Ooops... nexus driver is mis-configured... */
2324 return (DDI_PROBE_FAILURE);
2327 if (ddi_get_soft_state(sd_state, instance) != NULL) {
2328 return (DDI_PROBE_PARTIAL);
2332 * Call the SCSA utility probe routine to see if we actually
2333 * have a target at this SCSI nexus.
2335 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2336 case SCSIPROBE_EXISTS:
2337 switch (devp->sd_inq->inq_dtype) {
2338 case DTYPE_DIRECT:
2339 rval = DDI_PROBE_SUCCESS;
2340 break;
2341 case DTYPE_RODIRECT:
2342 /* CDs etc. Can be removable media */
2343 rval = DDI_PROBE_SUCCESS;
2344 break;
2345 case DTYPE_OPTICAL:
2347 * Rewritable optical driver HP115AA
2348 * Can also be removable media
2352 * Do not attempt to bind to DTYPE_OPTICAL if
2353 * pre solaris 9 sparc sd behavior is required
2355 * If first time through and sd_dtype_optical_bind
2356 * has not been set in /etc/system check properties
2359 if (sd_dtype_optical_bind < 0) {
2360 sd_dtype_optical_bind = ddi_prop_get_int
2361 (DDI_DEV_T_ANY, devi, 0,
2362 "optical-device-bind", 1);
2365 if (sd_dtype_optical_bind == 0) {
2366 rval = DDI_PROBE_FAILURE;
2367 } else {
2368 rval = DDI_PROBE_SUCCESS;
2370 break;
2372 case DTYPE_NOTPRESENT:
2373 default:
2374 rval = DDI_PROBE_FAILURE;
2375 break;
2377 break;
2378 default:
2379 rval = DDI_PROBE_PARTIAL;
2380 break;
2384 * This routine checks for resource allocation prior to freeing,
2385 * so it will take care of the "smart probing" case where a
2386 * scsi_probe() may or may not have been issued and will *not*
2387 * free previously-freed resources.
2389 scsi_unprobe(devp);
2390 return (rval);
2395 * Function: sdinfo
2397 * Description: This is the driver getinfo(9e) entry point function.
2398 * Given the device number, return the devinfo pointer from
2399 * the scsi_device structure or the instance number
2400 * associated with the dev_t.
2402 * Arguments: dip - pointer to device info structure
2403 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2404 * DDI_INFO_DEVT2INSTANCE)
2405 * arg - driver dev_t
2406 * resultp - user buffer for request response
2408 * Return Code: DDI_SUCCESS
2409 * DDI_FAILURE
2411 /* ARGSUSED */
2412 static int
2413 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2415 struct sd_lun *un;
2416 dev_t dev;
2417 int instance;
2418 int error;
2420 switch (infocmd) {
2421 case DDI_INFO_DEVT2DEVINFO:
2422 dev = (dev_t)arg;
2423 instance = SDUNIT(dev);
2424 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2425 return (DDI_FAILURE);
2427 *result = (void *) SD_DEVINFO(un);
2428 error = DDI_SUCCESS;
2429 break;
2430 case DDI_INFO_DEVT2INSTANCE:
2431 dev = (dev_t)arg;
2432 instance = SDUNIT(dev);
2433 *result = (void *)(uintptr_t)instance;
2434 error = DDI_SUCCESS;
2435 break;
2436 default:
2437 error = DDI_FAILURE;
2439 return (error);
2443 * Function: sd_prop_op
2445 * Description: This is the driver prop_op(9e) entry point function.
2446 * Return the number of blocks for the partition in question
2447 * or forward the request to the property facilities.
2449 * Arguments: dev - device number
2450 * dip - pointer to device info structure
2451 * prop_op - property operator
2452 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2453 * name - pointer to property name
2454 * valuep - pointer or address of the user buffer
2455 * lengthp - property length
2457 * Return Code: DDI_PROP_SUCCESS
2458 * DDI_PROP_NOT_FOUND
2459 * DDI_PROP_UNDEFINED
2460 * DDI_PROP_NO_MEMORY
2461 * DDI_PROP_BUF_TOO_SMALL
2464 static int
2465 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2466 char *name, caddr_t valuep, int *lengthp)
2468 struct sd_lun *un;
2470 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2471 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2472 name, valuep, lengthp));
2474 return (cmlb_prop_op(un->un_cmlbhandle,
2475 dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2476 SDPART(dev), (void *)SD_PATH_DIRECT));
2480 * The following functions are for smart probing:
2481 * sd_scsi_probe_cache_init()
2482 * sd_scsi_probe_cache_fini()
2483 * sd_scsi_clear_probe_cache()
2484 * sd_scsi_probe_with_cache()
2488 * Function: sd_scsi_probe_cache_init
2490 * Description: Initializes the probe response cache mutex and head pointer.
2492 * Context: Kernel thread context
2495 static void
2496 sd_scsi_probe_cache_init(void)
2498 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2499 sd_scsi_probe_cache_head = NULL;
2504 * Function: sd_scsi_probe_cache_fini
2506 * Description: Frees all resources associated with the probe response cache.
2508 * Context: Kernel thread context
2511 static void
2512 sd_scsi_probe_cache_fini(void)
2514 struct sd_scsi_probe_cache *cp;
2515 struct sd_scsi_probe_cache *ncp;
2517 /* Clean up our smart probing linked list */
2518 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2519 ncp = cp->next;
2520 kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2522 sd_scsi_probe_cache_head = NULL;
2523 mutex_destroy(&sd_scsi_probe_cache_mutex);
2528 * Function: sd_scsi_clear_probe_cache
2530 * Description: This routine clears the probe response cache. This is
2531 * done when open() returns ENXIO so that when deferred
2532 * attach is attempted (possibly after a device has been
2533 * turned on) we will retry the probe. Since we don't know
2534 * which target we failed to open, we just clear the
2535 * entire cache.
2537 * Context: Kernel thread context
2540 static void
2541 sd_scsi_clear_probe_cache(void)
2543 struct sd_scsi_probe_cache *cp;
2544 int i;
2546 mutex_enter(&sd_scsi_probe_cache_mutex);
2547 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2549 * Reset all entries to SCSIPROBE_EXISTS. This will
2550 * force probing to be performed the next time
2551 * sd_scsi_probe_with_cache is called.
2553 for (i = 0; i < NTARGETS_WIDE; i++) {
2554 cp->cache[i] = SCSIPROBE_EXISTS;
2557 mutex_exit(&sd_scsi_probe_cache_mutex);
2562 * Function: sd_scsi_probe_with_cache
2564 * Description: This routine implements support for a scsi device probe
2565 * with cache. The driver maintains a cache of the target
2566 * responses to scsi probes. If we get no response from a
2567 * target during a probe inquiry, we remember that, and we
2568 * avoid additional calls to scsi_probe on non-zero LUNs
2569 * on the same target until the cache is cleared. By doing
2570 * so we avoid the 1/4 sec selection timeout for nonzero
2571 * LUNs. lun0 of a target is always probed.
2573 * Arguments: devp - Pointer to a scsi_device(9S) structure
2574 * waitfunc - indicates what the allocator routines should
2575 * do when resources are not available. This value
2576 * is passed on to scsi_probe() when that routine
2577 * is called.
2579 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2580 * otherwise the value returned by scsi_probe(9F).
2582 * Context: Kernel thread context
2585 static int
2586 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2588 struct sd_scsi_probe_cache *cp;
2589 dev_info_t *pdip = ddi_get_parent(devp->sd_dev);
2590 int lun, tgt;
2592 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2593 SCSI_ADDR_PROP_LUN, 0);
2594 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2595 SCSI_ADDR_PROP_TARGET, -1);
2597 /* Make sure caching enabled and target in range */
2598 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2599 /* do it the old way (no cache) */
2600 return (scsi_probe(devp, waitfn));
2603 mutex_enter(&sd_scsi_probe_cache_mutex);
2605 /* Find the cache for this scsi bus instance */
2606 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2607 if (cp->pdip == pdip) {
2608 break;
2612 /* If we can't find a cache for this pdip, create one */
2613 if (cp == NULL) {
2614 int i;
2616 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2617 KM_SLEEP);
2618 cp->pdip = pdip;
2619 cp->next = sd_scsi_probe_cache_head;
2620 sd_scsi_probe_cache_head = cp;
2621 for (i = 0; i < NTARGETS_WIDE; i++) {
2622 cp->cache[i] = SCSIPROBE_EXISTS;
2626 mutex_exit(&sd_scsi_probe_cache_mutex);
2628 /* Recompute the cache for this target if LUN zero */
2629 if (lun == 0) {
2630 cp->cache[tgt] = SCSIPROBE_EXISTS;
2633 /* Don't probe if cache remembers a NORESP from a previous LUN. */
2634 if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2635 return (SCSIPROBE_NORESP);
2638 /* Do the actual probe; save & return the result */
2639 return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2644 * Function: sd_scsi_target_lun_init
2646 * Description: Initializes the attached lun chain mutex and head pointer.
2648 * Context: Kernel thread context
2651 static void
2652 sd_scsi_target_lun_init(void)
2654 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
2655 sd_scsi_target_lun_head = NULL;
2660 * Function: sd_scsi_target_lun_fini
2662 * Description: Frees all resources associated with the attached lun
2663 * chain
2665 * Context: Kernel thread context
2668 static void
2669 sd_scsi_target_lun_fini(void)
2671 struct sd_scsi_hba_tgt_lun *cp;
2672 struct sd_scsi_hba_tgt_lun *ncp;
2674 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
2675 ncp = cp->next;
2676 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
2678 sd_scsi_target_lun_head = NULL;
2679 mutex_destroy(&sd_scsi_target_lun_mutex);
2684 * Function: sd_scsi_get_target_lun_count
2686 * Description: This routine will check in the attached lun chain to see
2687 * how many luns are attached on the required SCSI controller
2688 * and target. Currently, some capabilities like tagged queue
2689 * are supported per target based by HBA. So all luns in a
2690 * target have the same capabilities. Based on this assumption,
2691 * sd should only set these capabilities once per target. This
2692 * function is called when sd needs to decide how many luns
2693 * already attached on a target.
2695 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
2696 * controller device.
2697 * target - The target ID on the controller's SCSI bus.
2699 * Return Code: The number of luns attached on the required target and
2700 * controller.
2701 * -1 if target ID is not in parallel SCSI scope or the given
2702 * dip is not in the chain.
2704 * Context: Kernel thread context
2707 static int
2708 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
2710 struct sd_scsi_hba_tgt_lun *cp;
2712 if ((target < 0) || (target >= NTARGETS_WIDE)) {
2713 return (-1);
2716 mutex_enter(&sd_scsi_target_lun_mutex);
2718 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2719 if (cp->pdip == dip) {
2720 break;
2724 mutex_exit(&sd_scsi_target_lun_mutex);
2726 if (cp == NULL) {
2727 return (-1);
2730 return (cp->nlun[target]);
2735 * Function: sd_scsi_update_lun_on_target
2737 * Description: This routine is used to update the attached lun chain when a
2738 * lun is attached or detached on a target.
2740 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
2741 * controller device.
2742 * target - The target ID on the controller's SCSI bus.
2743 * flag - Indicate the lun is attached or detached.
2745 * Context: Kernel thread context
2748 static void
2749 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
2751 struct sd_scsi_hba_tgt_lun *cp;
2753 mutex_enter(&sd_scsi_target_lun_mutex);
2755 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2756 if (cp->pdip == dip) {
2757 break;
2761 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
2762 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
2763 KM_SLEEP);
2764 cp->pdip = dip;
2765 cp->next = sd_scsi_target_lun_head;
2766 sd_scsi_target_lun_head = cp;
2769 mutex_exit(&sd_scsi_target_lun_mutex);
2771 if (cp != NULL) {
2772 if (flag == SD_SCSI_LUN_ATTACH) {
2773 cp->nlun[target] ++;
2774 } else {
2775 cp->nlun[target] --;
2782 * Function: sd_spin_up_unit
2784 * Description: Issues the following commands to spin-up the device:
2785 * START STOP UNIT, and INQUIRY.
2787 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
2788 * structure for this target.
2790 * Return Code: 0 - success
2791 * EIO - failure
2792 * EACCES - reservation conflict
2794 * Context: Kernel thread context
2797 static int
2798 sd_spin_up_unit(sd_ssc_t *ssc)
2800 size_t resid = 0;
2801 int has_conflict = FALSE;
2802 uchar_t *bufaddr;
2803 int status;
2804 struct sd_lun *un;
2806 ASSERT(ssc != NULL);
2807 un = ssc->ssc_un;
2808 ASSERT(un != NULL);
2811 * Send a throwaway START UNIT command.
2813 * If we fail on this, we don't care presently what precisely
2814 * is wrong. EMC's arrays will also fail this with a check
2815 * condition (0x2/0x4/0x3) if the device is "inactive," but
2816 * we don't want to fail the attach because it may become
2817 * "active" later.
2818 * We don't know if power condition is supported or not at
2819 * this stage, use START STOP bit.
2821 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
2822 SD_TARGET_START, SD_PATH_DIRECT);
2824 if (status != 0) {
2825 if (status == EACCES)
2826 has_conflict = TRUE;
2827 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2831 * Send another INQUIRY command to the target. This is necessary for
2832 * non-removable media direct access devices because their INQUIRY data
2833 * may not be fully qualified until they are spun up (perhaps via the
2834 * START command above). Note: This seems to be needed for some
2835 * legacy devices only.) The INQUIRY command should succeed even if a
2836 * Reservation Conflict is present.
2838 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2840 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
2841 != 0) {
2842 kmem_free(bufaddr, SUN_INQSIZE);
2843 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
2844 return (EIO);
2848 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
2849 * Note that this routine does not return a failure here even if the
2850 * INQUIRY command did not return any data. This is a legacy behavior.
2852 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
2853 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
2856 kmem_free(bufaddr, SUN_INQSIZE);
2858 /* If we hit a reservation conflict above, tell the caller. */
2859 if (has_conflict == TRUE) {
2860 return (EACCES);
2863 return (0);
2866 #ifdef _LP64
2868 * Function: sd_enable_descr_sense
2870 * Description: This routine attempts to select descriptor sense format
2871 * using the Control mode page. Devices that support 64 bit
2872 * LBAs (for >2TB luns) should also implement descriptor
2873 * sense data so we will call this function whenever we see
2874 * a lun larger than 2TB. If for some reason the device
2875 * supports 64 bit LBAs but doesn't support descriptor sense
2876 * presumably the mode select will fail. Everything will
2877 * continue to work normally except that we will not get
2878 * complete sense data for commands that fail with an LBA
2879 * larger than 32 bits.
2881 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
2882 * structure for this target.
2884 * Context: Kernel thread context only
2887 static void
2888 sd_enable_descr_sense(sd_ssc_t *ssc)
2890 uchar_t *header;
2891 struct mode_control_scsi3 *ctrl_bufp;
2892 size_t buflen;
2893 size_t bd_len;
2894 int status;
2895 struct sd_lun *un;
2897 ASSERT(ssc != NULL);
2898 un = ssc->ssc_un;
2899 ASSERT(un != NULL);
2902 * Read MODE SENSE page 0xA, Control Mode Page
2904 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
2905 sizeof (struct mode_control_scsi3);
2906 header = kmem_zalloc(buflen, KM_SLEEP);
2908 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
2909 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
2911 if (status != 0) {
2912 SD_ERROR(SD_LOG_COMMON, un,
2913 "sd_enable_descr_sense: mode sense ctrl page failed\n");
2914 goto eds_exit;
2918 * Determine size of Block Descriptors in order to locate
2919 * the mode page data. ATAPI devices return 0, SCSI devices
2920 * should return MODE_BLK_DESC_LENGTH.
2922 bd_len = ((struct mode_header *)header)->bdesc_length;
2924 /* Clear the mode data length field for MODE SELECT */
2925 ((struct mode_header *)header)->length = 0;
2927 ctrl_bufp = (struct mode_control_scsi3 *)
2928 (header + MODE_HEADER_LENGTH + bd_len);
2931 * If the page length is smaller than the expected value,
2932 * the target device doesn't support D_SENSE. Bail out here.
2934 if (ctrl_bufp->mode_page.length <
2935 sizeof (struct mode_control_scsi3) - 2) {
2936 SD_ERROR(SD_LOG_COMMON, un,
2937 "sd_enable_descr_sense: enable D_SENSE failed\n");
2938 goto eds_exit;
2942 * Clear PS bit for MODE SELECT
2944 ctrl_bufp->mode_page.ps = 0;
2947 * Set D_SENSE to enable descriptor sense format.
2949 ctrl_bufp->d_sense = 1;
2951 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2954 * Use MODE SELECT to commit the change to the D_SENSE bit
2956 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
2957 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
2959 if (status != 0) {
2960 SD_INFO(SD_LOG_COMMON, un,
2961 "sd_enable_descr_sense: mode select ctrl page failed\n");
2962 } else {
2963 kmem_free(header, buflen);
2964 return;
2967 eds_exit:
2968 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2969 kmem_free(header, buflen);
2973 * Function: sd_reenable_dsense_task
2975 * Description: Re-enable descriptor sense after device or bus reset
2977 * Context: Executes in a taskq() thread context
2979 static void
2980 sd_reenable_dsense_task(void *arg)
2982 struct sd_lun *un = arg;
2983 sd_ssc_t *ssc;
2985 ASSERT(un != NULL);
2987 ssc = sd_ssc_init(un);
2988 sd_enable_descr_sense(ssc);
2989 sd_ssc_fini(ssc);
2991 #endif /* _LP64 */
2994 * Function: sd_set_mmc_caps
2996 * Description: This routine determines if the device is MMC compliant and if
2997 * the device supports CDDA via a mode sense of the CDVD
2998 * capabilities mode page. Also checks if the device is a
2999 * dvdram writable device.
3001 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3002 * structure for this target.
3004 * Context: Kernel thread context only
3007 static void
3008 sd_set_mmc_caps(sd_ssc_t *ssc)
3010 struct mode_header_grp2 *sense_mhp;
3011 uchar_t *sense_page;
3012 caddr_t buf;
3013 int bd_len;
3014 int status;
3015 struct uscsi_cmd com;
3016 int rtn;
3017 uchar_t *out_data_rw, *out_data_hd;
3018 uchar_t *rqbuf_rw, *rqbuf_hd;
3019 uchar_t *out_data_gesn;
3020 int gesn_len;
3021 struct sd_lun *un;
3023 ASSERT(ssc != NULL);
3024 un = ssc->ssc_un;
3025 ASSERT(un != NULL);
3028 * The flags which will be set in this function are - mmc compliant,
3029 * dvdram writable device, cdda support. Initialize them to FALSE
3030 * and if a capability is detected - it will be set to TRUE.
3032 un->un_f_mmc_cap = FALSE;
3033 un->un_f_dvdram_writable_device = FALSE;
3034 un->un_f_cfg_cdda = FALSE;
3036 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3037 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3038 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3040 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3042 if (status != 0) {
3043 /* command failed; just return */
3044 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3045 return;
3048 * If the mode sense request for the CDROM CAPABILITIES
3049 * page (0x2A) succeeds the device is assumed to be MMC.
3051 un->un_f_mmc_cap = TRUE;
3053 /* See if GET STATUS EVENT NOTIFICATION is supported */
3054 if (un->un_f_mmc_gesn_polling) {
3055 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3056 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3058 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3059 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3061 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3063 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3064 un->un_f_mmc_gesn_polling = FALSE;
3065 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3066 "sd_set_mmc_caps: gesn not supported "
3067 "%d %x %x %x %x\n", rtn,
3068 out_data_gesn[0], out_data_gesn[1],
3069 out_data_gesn[2], out_data_gesn[3]);
3072 kmem_free(out_data_gesn, gesn_len);
3075 /* Get to the page data */
3076 sense_mhp = (struct mode_header_grp2 *)buf;
3077 bd_len = (sense_mhp->bdesc_length_hi << 8) |
3078 sense_mhp->bdesc_length_lo;
3079 if (bd_len > MODE_BLK_DESC_LENGTH) {
3081 * We did not get back the expected block descriptor
3082 * length so we cannot determine if the device supports
3083 * CDDA. However, we still indicate the device is MMC
3084 * according to the successful response to the page
3085 * 0x2A mode sense request.
3087 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3088 "sd_set_mmc_caps: Mode Sense returned "
3089 "invalid block descriptor length\n");
3090 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3091 return;
3094 /* See if read CDDA is supported */
3095 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3096 bd_len);
3097 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3099 /* See if writing DVD RAM is supported. */
3100 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3101 if (un->un_f_dvdram_writable_device == TRUE) {
3102 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3103 return;
3107 * If the device presents DVD or CD capabilities in the mode
3108 * page, we can return here since a RRD will not have
3109 * these capabilities.
3111 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3112 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3113 return;
3115 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3118 * If un->un_f_dvdram_writable_device is still FALSE,
3119 * check for a Removable Rigid Disk (RRD). A RRD
3120 * device is identified by the features RANDOM_WRITABLE and
3121 * HARDWARE_DEFECT_MANAGEMENT.
3123 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3124 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3126 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3127 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3128 RANDOM_WRITABLE, SD_PATH_STANDARD);
3130 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3132 if (rtn != 0) {
3133 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3134 kmem_free(rqbuf_rw, SENSE_LENGTH);
3135 return;
3138 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3139 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3141 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3142 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3143 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3145 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3147 if (rtn == 0) {
3149 * We have good information, check for random writable
3150 * and hardware defect features.
3152 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3153 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3154 un->un_f_dvdram_writable_device = TRUE;
3158 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3159 kmem_free(rqbuf_rw, SENSE_LENGTH);
3160 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3161 kmem_free(rqbuf_hd, SENSE_LENGTH);
3165 * Function: sd_check_for_writable_cd
3167 * Description: This routine determines if the media in the device is
3168 * writable or not. It uses the get configuration command (0x46)
3169 * to determine if the media is writable
3171 * Arguments: un - driver soft state (unit) structure
3172 * path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3173 * chain and the normal command waitq, or
3174 * SD_PATH_DIRECT_PRIORITY to use the USCSI
3175 * "direct" chain and bypass the normal command
3176 * waitq.
3178 * Context: Never called at interrupt context.
3181 static void
3182 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3184 struct uscsi_cmd com;
3185 uchar_t *out_data;
3186 uchar_t *rqbuf;
3187 int rtn;
3188 uchar_t *out_data_rw, *out_data_hd;
3189 uchar_t *rqbuf_rw, *rqbuf_hd;
3190 struct mode_header_grp2 *sense_mhp;
3191 uchar_t *sense_page;
3192 caddr_t buf;
3193 int bd_len;
3194 int status;
3195 struct sd_lun *un;
3197 ASSERT(ssc != NULL);
3198 un = ssc->ssc_un;
3199 ASSERT(un != NULL);
3200 ASSERT(mutex_owned(SD_MUTEX(un)));
3203 * Initialize the writable media to false, if configuration info.
3204 * tells us otherwise then only we will set it.
3206 un->un_f_mmc_writable_media = FALSE;
3207 mutex_exit(SD_MUTEX(un));
3209 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3210 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3212 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3213 out_data, SD_PROFILE_HEADER_LEN, path_flag);
3215 if (rtn != 0)
3216 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3218 mutex_enter(SD_MUTEX(un));
3219 if (rtn == 0) {
3221 * We have good information, check for writable DVD.
3223 if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3224 un->un_f_mmc_writable_media = TRUE;
3225 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3226 kmem_free(rqbuf, SENSE_LENGTH);
3227 return;
3231 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3232 kmem_free(rqbuf, SENSE_LENGTH);
3235 * Determine if this is a RRD type device.
3237 mutex_exit(SD_MUTEX(un));
3238 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3239 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3240 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3242 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3244 mutex_enter(SD_MUTEX(un));
3245 if (status != 0) {
3246 /* command failed; just return */
3247 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3248 return;
3251 /* Get to the page data */
3252 sense_mhp = (struct mode_header_grp2 *)buf;
3253 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3254 if (bd_len > MODE_BLK_DESC_LENGTH) {
3256 * We did not get back the expected block descriptor length so
3257 * we cannot check the mode page.
3259 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3260 "sd_check_for_writable_cd: Mode Sense returned "
3261 "invalid block descriptor length\n");
3262 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3263 return;
3267 * If the device presents DVD or CD capabilities in the mode
3268 * page, we can return here since a RRD device will not have
3269 * these capabilities.
3271 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3272 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3273 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3274 return;
3276 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3279 * If un->un_f_mmc_writable_media is still FALSE,
3280 * check for RRD type media. A RRD device is identified
3281 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3283 mutex_exit(SD_MUTEX(un));
3284 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3285 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3287 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3288 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3289 RANDOM_WRITABLE, path_flag);
3291 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3292 if (rtn != 0) {
3293 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3294 kmem_free(rqbuf_rw, SENSE_LENGTH);
3295 mutex_enter(SD_MUTEX(un));
3296 return;
3299 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3300 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3302 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3303 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3304 HARDWARE_DEFECT_MANAGEMENT, path_flag);
3306 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3307 mutex_enter(SD_MUTEX(un));
3308 if (rtn == 0) {
3310 * We have good information, check for random writable
3311 * and hardware defect features as current.
3313 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3314 (out_data_rw[10] & 0x1) &&
3315 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3316 (out_data_hd[10] & 0x1)) {
3317 un->un_f_mmc_writable_media = TRUE;
3321 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3322 kmem_free(rqbuf_rw, SENSE_LENGTH);
3323 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3324 kmem_free(rqbuf_hd, SENSE_LENGTH);
3328 * Function: sd_read_unit_properties
3330 * Description: The following implements a property lookup mechanism.
3331 * Properties for particular disks (keyed on vendor, model
3332 * and rev numbers) are sought in the sd.conf file via
3333 * sd_process_sdconf_file(), and if not found there, are
3334 * looked for in a list hardcoded in this driver via
3335 * sd_process_sdconf_table() Once located the properties
3336 * are used to update the driver unit structure.
3338 * Arguments: un - driver soft state (unit) structure
3341 static void
3342 sd_read_unit_properties(struct sd_lun *un)
3345 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3346 * the "sd-config-list" property (from the sd.conf file) or if
3347 * there was not a match for the inquiry vid/pid. If this event
3348 * occurs the static driver configuration table is searched for
3349 * a match.
3351 ASSERT(un != NULL);
3352 if (sd_process_sdconf_file(un) == SD_FAILURE) {
3353 sd_process_sdconf_table(un);
3356 /* check for LSI device */
3357 sd_is_lsi(un);
3364 * Function: sd_process_sdconf_file
3366 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3367 * driver's config file (ie, sd.conf) and update the driver
3368 * soft state structure accordingly.
3370 * Arguments: un - driver soft state (unit) structure
3372 * Return Code: SD_SUCCESS - The properties were successfully set according
3373 * to the driver configuration file.
3374 * SD_FAILURE - The driver config list was not obtained or
3375 * there was no vid/pid match. This indicates that
3376 * the static config table should be used.
3378 * The config file has a property, "sd-config-list". Currently we support
3379 * two kinds of formats. For both formats, the value of this property
3380 * is a list of duplets:
3382 * sd-config-list=
3383 * <duplet>,
3384 * [,<duplet>]*;
3386 * For the improved format, where
3388 * <duplet>:= "<vid+pid>","<tunable-list>"
3390 * and
3392 * <tunable-list>:= <tunable> [, <tunable> ]*;
3393 * <tunable> = <name> : <value>
3395 * The <vid+pid> is the string that is returned by the target device on a
3396 * SCSI inquiry command, the <tunable-list> contains one or more tunables
3397 * to apply to all target devices with the specified <vid+pid>.
3399 * Each <tunable> is a "<name> : <value>" pair.
3401 * For the old format, the structure of each duplet is as follows:
3403 * <duplet>:= "<vid+pid>","<data-property-name_list>"
3405 * The first entry of the duplet is the device ID string (the concatenated
3406 * vid & pid; not to be confused with a device_id). This is defined in
3407 * the same way as in the sd_disk_table.
3409 * The second part of the duplet is a string that identifies a
3410 * data-property-name-list. The data-property-name-list is defined as
3411 * follows:
3413 * <data-property-name-list>:=<data-property-name> [<data-property-name>]
3415 * The syntax of <data-property-name> depends on the <version> field.
3417 * If version = SD_CONF_VERSION_1 we have the following syntax:
3419 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3421 * where the prop0 value will be used to set prop0 if bit0 set in the
3422 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3426 static int
3427 sd_process_sdconf_file(struct sd_lun *un)
3429 char **config_list = NULL;
3430 uint_t nelements;
3431 char *vidptr;
3432 int vidlen;
3433 char *dnlist_ptr;
3434 char *dataname_ptr;
3435 char *dataname_lasts;
3436 int *data_list = NULL;
3437 uint_t data_list_len;
3438 int rval = SD_FAILURE;
3439 int i;
3441 ASSERT(un != NULL);
3443 /* Obtain the configuration list associated with the .conf file */
3444 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3445 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3446 &config_list, &nelements) != DDI_PROP_SUCCESS) {
3447 return (SD_FAILURE);
3451 * Compare vids in each duplet to the inquiry vid - if a match is
3452 * made, get the data value and update the soft state structure
3453 * accordingly.
3455 * Each duplet should show as a pair of strings, return SD_FAILURE
3456 * otherwise.
3458 if (nelements & 1) {
3459 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3460 "sd-config-list should show as pairs of strings.\n");
3461 if (config_list)
3462 ddi_prop_free(config_list);
3463 return (SD_FAILURE);
3466 for (i = 0; i < nelements; i += 2) {
3468 * Note: The assumption here is that each vid entry is on
3469 * a unique line from its associated duplet.
3471 vidptr = config_list[i];
3472 vidlen = (int)strlen(vidptr);
3473 if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3474 continue;
3478 * dnlist contains 1 or more blank separated
3479 * data-property-name entries
3481 dnlist_ptr = config_list[i + 1];
3483 if (strchr(dnlist_ptr, ':') != NULL) {
3485 * Decode the improved format sd-config-list.
3487 sd_nvpair_str_decode(un, dnlist_ptr);
3488 } else {
3490 * The old format sd-config-list, loop through all
3491 * data-property-name entries in the
3492 * data-property-name-list
3493 * setting the properties for each.
3495 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3496 &dataname_lasts); dataname_ptr != NULL;
3497 dataname_ptr = sd_strtok_r(NULL, " \t",
3498 &dataname_lasts)) {
3499 int version;
3501 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3502 "sd_process_sdconf_file: disk:%s, "
3503 "data:%s\n", vidptr, dataname_ptr);
3505 /* Get the data list */
3506 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3507 SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3508 &data_list_len) != DDI_PROP_SUCCESS) {
3509 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3510 "sd_process_sdconf_file: data "
3511 "property (%s) has no value\n",
3512 dataname_ptr);
3513 continue;
3516 version = data_list[0];
3518 if (version == SD_CONF_VERSION_1) {
3519 sd_tunables values;
3521 /* Set the properties */
3522 if (sd_chk_vers1_data(un, data_list[1],
3523 &data_list[2], data_list_len,
3524 dataname_ptr) == SD_SUCCESS) {
3525 sd_get_tunables_from_conf(un,
3526 data_list[1], &data_list[2],
3527 &values);
3528 sd_set_vers1_properties(un,
3529 data_list[1], &values);
3530 rval = SD_SUCCESS;
3531 } else {
3532 rval = SD_FAILURE;
3534 } else {
3535 scsi_log(SD_DEVINFO(un), sd_label,
3536 CE_WARN, "data property %s version "
3537 "0x%x is invalid.",
3538 dataname_ptr, version);
3539 rval = SD_FAILURE;
3541 if (data_list)
3542 ddi_prop_free(data_list);
3547 /* free up the memory allocated by ddi_prop_lookup_string_array(). */
3548 if (config_list) {
3549 ddi_prop_free(config_list);
3552 return (rval);
3556 * Function: sd_nvpair_str_decode()
3558 * Description: Parse the improved format sd-config-list to get
3559 * each entry of tunable, which includes a name-value pair.
3560 * Then call sd_set_properties() to set the property.
3562 * Arguments: un - driver soft state (unit) structure
3563 * nvpair_str - the tunable list
3565 static void
3566 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3568 char *nv, *name, *value, *token;
3569 char *nv_lasts, *v_lasts, *x_lasts;
3571 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3572 nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3573 token = sd_strtok_r(nv, ":", &v_lasts);
3574 name = sd_strtok_r(token, " \t", &x_lasts);
3575 token = sd_strtok_r(NULL, ":", &v_lasts);
3576 value = sd_strtok_r(token, " \t", &x_lasts);
3577 if (name == NULL || value == NULL) {
3578 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3579 "sd_nvpair_str_decode: "
3580 "name or value is not valid!\n");
3581 } else {
3582 sd_set_properties(un, name, value);
3588 * Function: sd_strtok_r()
3590 * Description: This function uses strpbrk and strspn to break
3591 * string into tokens on sequentially subsequent calls. Return
3592 * NULL when no non-separator characters remain. The first
3593 * argument is NULL for subsequent calls.
3595 static char *
3596 sd_strtok_r(char *string, const char *sepset, char **lasts)
3598 char *q, *r;
3600 /* First or subsequent call */
3601 if (string == NULL)
3602 string = *lasts;
3604 if (string == NULL)
3605 return (NULL);
3607 /* Skip leading separators */
3608 q = string + strspn(string, sepset);
3610 if (*q == '\0')
3611 return (NULL);
3613 if ((r = strpbrk(q, sepset)) == NULL)
3614 *lasts = NULL;
3615 else {
3616 *r = '\0';
3617 *lasts = r + 1;
3619 return (q);
3623 * Function: sd_set_properties()
3625 * Description: Set device properties based on the improved
3626 * format sd-config-list.
3628 * Arguments: un - driver soft state (unit) structure
3629 * name - supported tunable name
3630 * value - tunable value
3632 static void
3633 sd_set_properties(struct sd_lun *un, char *name, char *value)
3635 char *endptr = NULL;
3636 long val = 0;
3638 if (strcasecmp(name, "cache-nonvolatile") == 0) {
3639 if (strcasecmp(value, "true") == 0) {
3640 un->un_f_suppress_cache_flush = TRUE;
3641 } else if (strcasecmp(value, "false") == 0) {
3642 un->un_f_suppress_cache_flush = FALSE;
3643 } else {
3644 goto value_invalid;
3646 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3647 "suppress_cache_flush flag set to %d\n",
3648 un->un_f_suppress_cache_flush);
3649 return;
3652 if (strcasecmp(name, "controller-type") == 0) {
3653 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3654 un->un_ctype = val;
3655 } else {
3656 goto value_invalid;
3658 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3659 "ctype set to %d\n", un->un_ctype);
3660 return;
3663 if (strcasecmp(name, "delay-busy") == 0) {
3664 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3665 un->un_busy_timeout = drv_usectohz(val / 1000);
3666 } else {
3667 goto value_invalid;
3669 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3670 "busy_timeout set to %d\n", un->un_busy_timeout);
3671 return;
3674 if (strcasecmp(name, "disksort") == 0) {
3675 if (strcasecmp(value, "true") == 0) {
3676 un->un_f_disksort_disabled = FALSE;
3677 } else if (strcasecmp(value, "false") == 0) {
3678 un->un_f_disksort_disabled = TRUE;
3679 } else {
3680 goto value_invalid;
3682 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3683 "disksort disabled flag set to %d\n",
3684 un->un_f_disksort_disabled);
3685 return;
3688 if (strcasecmp(name, "power-condition") == 0) {
3689 if (strcasecmp(value, "true") == 0) {
3690 un->un_f_power_condition_disabled = FALSE;
3691 } else if (strcasecmp(value, "false") == 0) {
3692 un->un_f_power_condition_disabled = TRUE;
3693 } else {
3694 goto value_invalid;
3696 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3697 "power condition disabled flag set to %d\n",
3698 un->un_f_power_condition_disabled);
3699 return;
3702 if (strcasecmp(name, "timeout-releasereservation") == 0) {
3703 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3704 un->un_reserve_release_time = val;
3705 } else {
3706 goto value_invalid;
3708 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3709 "reservation release timeout set to %d\n",
3710 un->un_reserve_release_time);
3711 return;
3714 if (strcasecmp(name, "reset-lun") == 0) {
3715 if (strcasecmp(value, "true") == 0) {
3716 un->un_f_lun_reset_enabled = TRUE;
3717 } else if (strcasecmp(value, "false") == 0) {
3718 un->un_f_lun_reset_enabled = FALSE;
3719 } else {
3720 goto value_invalid;
3722 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3723 "lun reset enabled flag set to %d\n",
3724 un->un_f_lun_reset_enabled);
3725 return;
3728 if (strcasecmp(name, "retries-busy") == 0) {
3729 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3730 un->un_busy_retry_count = val;
3731 } else {
3732 goto value_invalid;
3734 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3735 "busy retry count set to %d\n", un->un_busy_retry_count);
3736 return;
3739 if (strcasecmp(name, "retries-timeout") == 0) {
3740 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3741 un->un_retry_count = val;
3742 } else {
3743 goto value_invalid;
3745 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3746 "timeout retry count set to %d\n", un->un_retry_count);
3747 return;
3750 if (strcasecmp(name, "retries-notready") == 0) {
3751 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3752 un->un_notready_retry_count = val;
3753 } else {
3754 goto value_invalid;
3756 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3757 "notready retry count set to %d\n",
3758 un->un_notready_retry_count);
3759 return;
3762 if (strcasecmp(name, "retries-reset") == 0) {
3763 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3764 un->un_reset_retry_count = val;
3765 } else {
3766 goto value_invalid;
3768 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3769 "reset retry count set to %d\n",
3770 un->un_reset_retry_count);
3771 return;
3774 if (strcasecmp(name, "throttle-max") == 0) {
3775 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3776 un->un_saved_throttle = un->un_throttle = val;
3777 } else {
3778 goto value_invalid;
3780 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3781 "throttle set to %d\n", un->un_throttle);
3784 if (strcasecmp(name, "throttle-min") == 0) {
3785 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3786 un->un_min_throttle = val;
3787 } else {
3788 goto value_invalid;
3790 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3791 "min throttle set to %d\n", un->un_min_throttle);
3794 if (strcasecmp(name, "rmw-type") == 0) {
3795 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3796 un->un_f_rmw_type = val;
3797 } else {
3798 goto value_invalid;
3800 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3801 "RMW type set to %d\n", un->un_f_rmw_type);
3804 if (strcasecmp(name, "physical-block-size") == 0) {
3805 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
3806 ISP2(val) && val >= un->un_tgt_blocksize &&
3807 val >= un->un_sys_blocksize) {
3808 un->un_phy_blocksize = val;
3809 } else {
3810 goto value_invalid;
3812 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3813 "physical block size set to %d\n", un->un_phy_blocksize);
3816 if (strcasecmp(name, "retries-victim") == 0) {
3817 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3818 un->un_victim_retry_count = val;
3819 } else {
3820 goto value_invalid;
3822 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3823 "victim retry count set to %d\n",
3824 un->un_victim_retry_count);
3825 return;
3829 * Validate the throttle values.
3830 * If any of the numbers are invalid, set everything to defaults.
3832 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
3833 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
3834 (un->un_min_throttle > un->un_throttle)) {
3835 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
3836 un->un_min_throttle = sd_min_throttle;
3839 if (strcasecmp(name, "mmc-gesn-polling") == 0) {
3840 if (strcasecmp(value, "true") == 0) {
3841 un->un_f_mmc_gesn_polling = TRUE;
3842 } else if (strcasecmp(value, "false") == 0) {
3843 un->un_f_mmc_gesn_polling = FALSE;
3844 } else {
3845 goto value_invalid;
3847 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3848 "mmc-gesn-polling set to %d\n",
3849 un->un_f_mmc_gesn_polling);
3852 return;
3854 value_invalid:
3855 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3856 "value of prop %s is invalid\n", name);
3860 * Function: sd_get_tunables_from_conf()
3863 * This function reads the data list from the sd.conf file and pulls
3864 * the values that can have numeric values as arguments and places
3865 * the values in the appropriate sd_tunables member.
3866 * Since the order of the data list members varies across platforms
3867 * This function reads them from the data list in a platform specific
3868 * order and places them into the correct sd_tunable member that is
3869 * consistent across all platforms.
3871 static void
3872 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3873 sd_tunables *values)
3875 int i;
3876 int mask;
3878 bzero(values, sizeof (sd_tunables));
3880 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3882 mask = 1 << i;
3883 if (mask > flags) {
3884 break;
3887 switch (mask & flags) {
3888 case 0: /* This mask bit not set in flags */
3889 continue;
3890 case SD_CONF_BSET_THROTTLE:
3891 values->sdt_throttle = data_list[i];
3892 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3893 "sd_get_tunables_from_conf: throttle = %d\n",
3894 values->sdt_throttle);
3895 break;
3896 case SD_CONF_BSET_CTYPE:
3897 values->sdt_ctype = data_list[i];
3898 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3899 "sd_get_tunables_from_conf: ctype = %d\n",
3900 values->sdt_ctype);
3901 break;
3902 case SD_CONF_BSET_NRR_COUNT:
3903 values->sdt_not_rdy_retries = data_list[i];
3904 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3905 "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3906 values->sdt_not_rdy_retries);
3907 break;
3908 case SD_CONF_BSET_BSY_RETRY_COUNT:
3909 values->sdt_busy_retries = data_list[i];
3910 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3911 "sd_get_tunables_from_conf: busy_retries = %d\n",
3912 values->sdt_busy_retries);
3913 break;
3914 case SD_CONF_BSET_RST_RETRIES:
3915 values->sdt_reset_retries = data_list[i];
3916 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3917 "sd_get_tunables_from_conf: reset_retries = %d\n",
3918 values->sdt_reset_retries);
3919 break;
3920 case SD_CONF_BSET_RSV_REL_TIME:
3921 values->sdt_reserv_rel_time = data_list[i];
3922 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3923 "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3924 values->sdt_reserv_rel_time);
3925 break;
3926 case SD_CONF_BSET_MIN_THROTTLE:
3927 values->sdt_min_throttle = data_list[i];
3928 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3929 "sd_get_tunables_from_conf: min_throttle = %d\n",
3930 values->sdt_min_throttle);
3931 break;
3932 case SD_CONF_BSET_DISKSORT_DISABLED:
3933 values->sdt_disk_sort_dis = data_list[i];
3934 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3935 "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3936 values->sdt_disk_sort_dis);
3937 break;
3938 case SD_CONF_BSET_LUN_RESET_ENABLED:
3939 values->sdt_lun_reset_enable = data_list[i];
3940 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3941 "sd_get_tunables_from_conf: lun_reset_enable = %d"
3942 "\n", values->sdt_lun_reset_enable);
3943 break;
3944 case SD_CONF_BSET_CACHE_IS_NV:
3945 values->sdt_suppress_cache_flush = data_list[i];
3946 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3947 "sd_get_tunables_from_conf: \
3948 suppress_cache_flush = %d"
3949 "\n", values->sdt_suppress_cache_flush);
3950 break;
3951 case SD_CONF_BSET_PC_DISABLED:
3952 values->sdt_disk_sort_dis = data_list[i];
3953 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3954 "sd_get_tunables_from_conf: power_condition_dis = "
3955 "%d\n", values->sdt_power_condition_dis);
3956 break;
3962 * Function: sd_process_sdconf_table
3964 * Description: Search the static configuration table for a match on the
3965 * inquiry vid/pid and update the driver soft state structure
3966 * according to the table property values for the device.
3968 * The form of a configuration table entry is:
3969 * <vid+pid>,<flags>,<property-data>
3970 * "SEAGATE ST42400N",1,0x40000,
3971 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
3973 * Arguments: un - driver soft state (unit) structure
3976 static void
3977 sd_process_sdconf_table(struct sd_lun *un)
3979 char *id = NULL;
3980 int table_index;
3981 int idlen;
3983 ASSERT(un != NULL);
3984 for (table_index = 0; table_index < sd_disk_table_size;
3985 table_index++) {
3986 id = sd_disk_table[table_index].device_id;
3987 idlen = strlen(id);
3990 * The static configuration table currently does not
3991 * implement version 10 properties. Additionally,
3992 * multiple data-property-name entries are not
3993 * implemented in the static configuration table.
3995 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3996 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3997 "sd_process_sdconf_table: disk %s\n", id);
3998 sd_set_vers1_properties(un,
3999 sd_disk_table[table_index].flags,
4000 sd_disk_table[table_index].properties);
4001 break;
4008 * Function: sd_sdconf_id_match
4010 * Description: This local function implements a case sensitive vid/pid
4011 * comparison as well as the boundary cases of wild card and
4012 * multiple blanks.
4014 * Note: An implicit assumption made here is that the scsi
4015 * inquiry structure will always keep the vid, pid and
4016 * revision strings in consecutive sequence, so they can be
4017 * read as a single string. If this assumption is not the
4018 * case, a separate string, to be used for the check, needs
4019 * to be built with these strings concatenated.
4021 * Arguments: un - driver soft state (unit) structure
4022 * id - table or config file vid/pid
4023 * idlen - length of the vid/pid (bytes)
4025 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4026 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4029 static int
4030 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4032 struct scsi_inquiry *sd_inq;
4033 int rval = SD_SUCCESS;
4035 ASSERT(un != NULL);
4036 sd_inq = un->un_sd->sd_inq;
4037 ASSERT(id != NULL);
4040 * We use the inq_vid as a pointer to a buffer containing the
4041 * vid and pid and use the entire vid/pid length of the table
4042 * entry for the comparison. This works because the inq_pid
4043 * data member follows inq_vid in the scsi_inquiry structure.
4045 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4047 * The user id string is compared to the inquiry vid/pid
4048 * using a case insensitive comparison and ignoring
4049 * multiple spaces.
4051 rval = sd_blank_cmp(un, id, idlen);
4052 if (rval != SD_SUCCESS) {
4054 * User id strings that start and end with a "*"
4055 * are a special case. These do not have a
4056 * specific vendor, and the product string can
4057 * appear anywhere in the 16 byte PID portion of
4058 * the inquiry data. This is a simple strstr()
4059 * type search for the user id in the inquiry data.
4061 if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4062 char *pidptr = &id[1];
4063 int i;
4064 int j;
4065 int pidstrlen = idlen - 2;
4066 j = sizeof (SD_INQUIRY(un)->inq_pid) -
4067 pidstrlen;
4069 if (j < 0) {
4070 return (SD_FAILURE);
4072 for (i = 0; i < j; i++) {
4073 if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4074 pidptr, pidstrlen) == 0) {
4075 rval = SD_SUCCESS;
4076 break;
4082 return (rval);
4087 * Function: sd_blank_cmp
4089 * Description: If the id string starts and ends with a space, treat
4090 * multiple consecutive spaces as equivalent to a single
4091 * space. For example, this causes a sd_disk_table entry
4092 * of " NEC CDROM " to match a device's id string of
4093 * "NEC CDROM".
4095 * Note: The success exit condition for this routine is if
4096 * the pointer to the table entry is '\0' and the cnt of
4097 * the inquiry length is zero. This will happen if the inquiry
4098 * string returned by the device is padded with spaces to be
4099 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4100 * SCSI spec states that the inquiry string is to be padded with
4101 * spaces.
4103 * Arguments: un - driver soft state (unit) structure
4104 * id - table or config file vid/pid
4105 * idlen - length of the vid/pid (bytes)
4107 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4108 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4111 static int
4112 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4114 char *p1;
4115 char *p2;
4116 int cnt;
4117 cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4118 sizeof (SD_INQUIRY(un)->inq_pid);
4120 ASSERT(un != NULL);
4121 p2 = un->un_sd->sd_inq->inq_vid;
4122 ASSERT(id != NULL);
4123 p1 = id;
4125 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4127 * Note: string p1 is terminated by a NUL but string p2
4128 * isn't. The end of p2 is determined by cnt.
4130 for (;;) {
4131 /* skip over any extra blanks in both strings */
4132 while ((*p1 != '\0') && (*p1 == ' ')) {
4133 p1++;
4135 while ((cnt != 0) && (*p2 == ' ')) {
4136 p2++;
4137 cnt--;
4140 /* compare the two strings */
4141 if ((cnt == 0) ||
4142 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4143 break;
4145 while ((cnt > 0) &&
4146 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4147 p1++;
4148 p2++;
4149 cnt--;
4154 /* return SD_SUCCESS if both strings match */
4155 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4160 * Function: sd_chk_vers1_data
4162 * Description: Verify the version 1 device properties provided by the
4163 * user via the configuration file
4165 * Arguments: un - driver soft state (unit) structure
4166 * flags - integer mask indicating properties to be set
4167 * prop_list - integer list of property values
4168 * list_len - number of the elements
4170 * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4171 * SD_FAILURE - Indicates the user provided data is invalid
4174 static int
4175 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4176 int list_len, char *dataname_ptr)
4178 int i;
4179 int mask = 1;
4180 int index = 0;
4182 ASSERT(un != NULL);
4184 /* Check for a NULL property name and list */
4185 if (dataname_ptr == NULL) {
4186 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4187 "sd_chk_vers1_data: NULL data property name.");
4188 return (SD_FAILURE);
4190 if (prop_list == NULL) {
4191 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4192 "sd_chk_vers1_data: %s NULL data property list.",
4193 dataname_ptr);
4194 return (SD_FAILURE);
4197 /* Display a warning if undefined bits are set in the flags */
4198 if (flags & ~SD_CONF_BIT_MASK) {
4199 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4200 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4201 "Properties not set.",
4202 (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4203 return (SD_FAILURE);
4207 * Verify the length of the list by identifying the highest bit set
4208 * in the flags and validating that the property list has a length
4209 * up to the index of this bit.
4211 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4212 if (flags & mask) {
4213 index++;
4215 mask = 1 << i;
4217 if (list_len < (index + 2)) {
4218 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4219 "sd_chk_vers1_data: "
4220 "Data property list %s size is incorrect. "
4221 "Properties not set.", dataname_ptr);
4222 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4223 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4224 return (SD_FAILURE);
4226 return (SD_SUCCESS);
4231 * Function: sd_set_vers1_properties
4233 * Description: Set version 1 device properties based on a property list
4234 * retrieved from the driver configuration file or static
4235 * configuration table. Version 1 properties have the format:
4237 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4239 * where the prop0 value will be used to set prop0 if bit0
4240 * is set in the flags
4242 * Arguments: un - driver soft state (unit) structure
4243 * flags - integer mask indicating properties to be set
4244 * prop_list - integer list of property values
4247 static void
4248 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4250 ASSERT(un != NULL);
4253 * Set the flag to indicate cache is to be disabled. An attempt
4254 * to disable the cache via sd_cache_control() will be made
4255 * later during attach once the basic initialization is complete.
4257 if (flags & SD_CONF_BSET_NOCACHE) {
4258 un->un_f_opt_disable_cache = TRUE;
4259 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4260 "sd_set_vers1_properties: caching disabled flag set\n");
4263 /* CD-specific configuration parameters */
4264 if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4265 un->un_f_cfg_playmsf_bcd = TRUE;
4266 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4267 "sd_set_vers1_properties: playmsf_bcd set\n");
4269 if (flags & SD_CONF_BSET_READSUB_BCD) {
4270 un->un_f_cfg_readsub_bcd = TRUE;
4271 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4272 "sd_set_vers1_properties: readsub_bcd set\n");
4274 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4275 un->un_f_cfg_read_toc_trk_bcd = TRUE;
4276 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4277 "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4279 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4280 un->un_f_cfg_read_toc_addr_bcd = TRUE;
4281 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4282 "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4284 if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4285 un->un_f_cfg_no_read_header = TRUE;
4286 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4287 "sd_set_vers1_properties: no_read_header set\n");
4289 if (flags & SD_CONF_BSET_READ_CD_XD4) {
4290 un->un_f_cfg_read_cd_xd4 = TRUE;
4291 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4292 "sd_set_vers1_properties: read_cd_xd4 set\n");
4295 /* Support for devices which do not have valid/unique serial numbers */
4296 if (flags & SD_CONF_BSET_FAB_DEVID) {
4297 un->un_f_opt_fab_devid = TRUE;
4298 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4299 "sd_set_vers1_properties: fab_devid bit set\n");
4302 /* Support for user throttle configuration */
4303 if (flags & SD_CONF_BSET_THROTTLE) {
4304 ASSERT(prop_list != NULL);
4305 un->un_saved_throttle = un->un_throttle =
4306 prop_list->sdt_throttle;
4307 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4308 "sd_set_vers1_properties: throttle set to %d\n",
4309 prop_list->sdt_throttle);
4312 /* Set the per disk retry count according to the conf file or table. */
4313 if (flags & SD_CONF_BSET_NRR_COUNT) {
4314 ASSERT(prop_list != NULL);
4315 if (prop_list->sdt_not_rdy_retries) {
4316 un->un_notready_retry_count =
4317 prop_list->sdt_not_rdy_retries;
4318 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4319 "sd_set_vers1_properties: not ready retry count"
4320 " set to %d\n", un->un_notready_retry_count);
4324 /* The controller type is reported for generic disk driver ioctls */
4325 if (flags & SD_CONF_BSET_CTYPE) {
4326 ASSERT(prop_list != NULL);
4327 switch (prop_list->sdt_ctype) {
4328 case CTYPE_CDROM:
4329 un->un_ctype = prop_list->sdt_ctype;
4330 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4331 "sd_set_vers1_properties: ctype set to "
4332 "CTYPE_CDROM\n");
4333 break;
4334 case CTYPE_CCS:
4335 un->un_ctype = prop_list->sdt_ctype;
4336 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4337 "sd_set_vers1_properties: ctype set to "
4338 "CTYPE_CCS\n");
4339 break;
4340 case CTYPE_ROD: /* RW optical */
4341 un->un_ctype = prop_list->sdt_ctype;
4342 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4343 "sd_set_vers1_properties: ctype set to "
4344 "CTYPE_ROD\n");
4345 break;
4346 default:
4347 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4348 "sd_set_vers1_properties: Could not set "
4349 "invalid ctype value (%d)",
4350 prop_list->sdt_ctype);
4354 /* Purple failover timeout */
4355 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4356 ASSERT(prop_list != NULL);
4357 un->un_busy_retry_count =
4358 prop_list->sdt_busy_retries;
4359 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4360 "sd_set_vers1_properties: "
4361 "busy retry count set to %d\n",
4362 un->un_busy_retry_count);
4365 /* Purple reset retry count */
4366 if (flags & SD_CONF_BSET_RST_RETRIES) {
4367 ASSERT(prop_list != NULL);
4368 un->un_reset_retry_count =
4369 prop_list->sdt_reset_retries;
4370 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4371 "sd_set_vers1_properties: "
4372 "reset retry count set to %d\n",
4373 un->un_reset_retry_count);
4376 /* Purple reservation release timeout */
4377 if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4378 ASSERT(prop_list != NULL);
4379 un->un_reserve_release_time =
4380 prop_list->sdt_reserv_rel_time;
4381 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4382 "sd_set_vers1_properties: "
4383 "reservation release timeout set to %d\n",
4384 un->un_reserve_release_time);
4388 * Driver flag telling the driver to verify that no commands are pending
4389 * for a device before issuing a Test Unit Ready. This is a workaround
4390 * for a firmware bug in some Seagate eliteI drives.
4392 if (flags & SD_CONF_BSET_TUR_CHECK) {
4393 un->un_f_cfg_tur_check = TRUE;
4394 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4395 "sd_set_vers1_properties: tur queue check set\n");
4398 if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4399 un->un_min_throttle = prop_list->sdt_min_throttle;
4400 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4401 "sd_set_vers1_properties: min throttle set to %d\n",
4402 un->un_min_throttle);
4405 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4406 un->un_f_disksort_disabled =
4407 (prop_list->sdt_disk_sort_dis != 0) ?
4408 TRUE : FALSE;
4409 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4410 "sd_set_vers1_properties: disksort disabled "
4411 "flag set to %d\n",
4412 prop_list->sdt_disk_sort_dis);
4415 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4416 un->un_f_lun_reset_enabled =
4417 (prop_list->sdt_lun_reset_enable != 0) ?
4418 TRUE : FALSE;
4419 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4420 "sd_set_vers1_properties: lun reset enabled "
4421 "flag set to %d\n",
4422 prop_list->sdt_lun_reset_enable);
4425 if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4426 un->un_f_suppress_cache_flush =
4427 (prop_list->sdt_suppress_cache_flush != 0) ?
4428 TRUE : FALSE;
4429 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4430 "sd_set_vers1_properties: suppress_cache_flush "
4431 "flag set to %d\n",
4432 prop_list->sdt_suppress_cache_flush);
4435 if (flags & SD_CONF_BSET_PC_DISABLED) {
4436 un->un_f_power_condition_disabled =
4437 (prop_list->sdt_power_condition_dis != 0) ?
4438 TRUE : FALSE;
4439 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4440 "sd_set_vers1_properties: power_condition_disabled "
4441 "flag set to %d\n",
4442 prop_list->sdt_power_condition_dis);
4446 * Validate the throttle values.
4447 * If any of the numbers are invalid, set everything to defaults.
4449 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4450 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4451 (un->un_min_throttle > un->un_throttle)) {
4452 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4453 un->un_min_throttle = sd_min_throttle;
4458 * Function: sd_is_lsi()
4460 * Description: Check for lsi devices, step through the static device
4461 * table to match vid/pid.
4463 * Args: un - ptr to sd_lun
4465 * Notes: When creating new LSI property, need to add the new LSI property
4466 * to this function.
4468 static void
4469 sd_is_lsi(struct sd_lun *un)
4471 char *id = NULL;
4472 int table_index;
4473 int idlen;
4474 void *prop;
4476 ASSERT(un != NULL);
4477 for (table_index = 0; table_index < sd_disk_table_size;
4478 table_index++) {
4479 id = sd_disk_table[table_index].device_id;
4480 idlen = strlen(id);
4481 if (idlen == 0) {
4482 continue;
4485 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4486 prop = sd_disk_table[table_index].properties;
4487 if (prop == &lsi_properties ||
4488 prop == &lsi_oem_properties ||
4489 prop == &lsi_properties_scsi ||
4490 prop == &symbios_properties) {
4491 un->un_f_cfg_is_lsi = TRUE;
4493 break;
4499 * Function: sd_get_physical_geometry
4501 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4502 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4503 * target, and use this information to initialize the physical
4504 * geometry cache specified by pgeom_p.
4506 * MODE SENSE is an optional command, so failure in this case
4507 * does not necessarily denote an error. We want to use the
4508 * MODE SENSE commands to derive the physical geometry of the
4509 * device, but if either command fails, the logical geometry is
4510 * used as the fallback for disk label geometry in cmlb.
4512 * This requires that un->un_blockcount and un->un_tgt_blocksize
4513 * have already been initialized for the current target and
4514 * that the current values be passed as args so that we don't
4515 * end up ever trying to use -1 as a valid value. This could
4516 * happen if either value is reset while we're not holding
4517 * the mutex.
4519 * Arguments: un - driver soft state (unit) structure
4520 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4521 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4522 * to use the USCSI "direct" chain and bypass the normal
4523 * command waitq.
4525 * Context: Kernel thread only (can sleep).
4528 static int
4529 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4530 diskaddr_t capacity, int lbasize, int path_flag)
4532 struct mode_format *page3p;
4533 struct mode_geometry *page4p;
4534 struct mode_header *headerp;
4535 int sector_size;
4536 int nsect;
4537 int nhead;
4538 int ncyl;
4539 int intrlv;
4540 int spc;
4541 diskaddr_t modesense_capacity;
4542 int rpm;
4543 int bd_len;
4544 int mode_header_length;
4545 uchar_t *p3bufp;
4546 uchar_t *p4bufp;
4547 int cdbsize;
4548 int ret = EIO;
4549 sd_ssc_t *ssc;
4550 int status;
4552 ASSERT(un != NULL);
4554 if (lbasize == 0) {
4555 if (ISCD(un)) {
4556 lbasize = 2048;
4557 } else {
4558 lbasize = un->un_sys_blocksize;
4561 pgeom_p->g_secsize = (unsigned short)lbasize;
4564 * If the unit is a cd/dvd drive MODE SENSE page three
4565 * and MODE SENSE page four are reserved (see SBC spec
4566 * and MMC spec). To prevent soft errors just return
4567 * using the default LBA size.
4569 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4570 * implement support for mode pages 3 and 4 return here to prevent
4571 * illegal requests on SATA drives.
4573 * These pages are also reserved in SBC-2 and later. We assume SBC-2
4574 * or later for a direct-attached block device if the SCSI version is
4575 * at least SPC-3.
4578 if (ISCD(un) ||
4579 un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4580 (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4581 return (ret);
4583 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4586 * Retrieve MODE SENSE page 3 - Format Device Page
4588 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4589 ssc = sd_ssc_init(un);
4590 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4591 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4592 if (status != 0) {
4593 SD_ERROR(SD_LOG_COMMON, un,
4594 "sd_get_physical_geometry: mode sense page 3 failed\n");
4595 goto page3_exit;
4599 * Determine size of Block Descriptors in order to locate the mode
4600 * page data. ATAPI devices return 0, SCSI devices should return
4601 * MODE_BLK_DESC_LENGTH.
4603 headerp = (struct mode_header *)p3bufp;
4604 if (un->un_f_cfg_is_atapi == TRUE) {
4605 struct mode_header_grp2 *mhp =
4606 (struct mode_header_grp2 *)headerp;
4607 mode_header_length = MODE_HEADER_LENGTH_GRP2;
4608 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4609 } else {
4610 mode_header_length = MODE_HEADER_LENGTH;
4611 bd_len = ((struct mode_header *)headerp)->bdesc_length;
4614 if (bd_len > MODE_BLK_DESC_LENGTH) {
4615 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4616 "sd_get_physical_geometry: received unexpected bd_len "
4617 "of %d, page3\n", bd_len);
4618 status = EIO;
4619 goto page3_exit;
4622 page3p = (struct mode_format *)
4623 ((caddr_t)headerp + mode_header_length + bd_len);
4625 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4626 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4627 "sd_get_physical_geometry: mode sense pg3 code mismatch "
4628 "%d\n", page3p->mode_page.code);
4629 status = EIO;
4630 goto page3_exit;
4634 * Use this physical geometry data only if BOTH MODE SENSE commands
4635 * complete successfully; otherwise, revert to the logical geometry.
4636 * So, we need to save everything in temporary variables.
4638 sector_size = BE_16(page3p->data_bytes_sect);
4641 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
4643 if (sector_size == 0) {
4644 sector_size = un->un_sys_blocksize;
4645 } else {
4646 sector_size &= ~(un->un_sys_blocksize - 1);
4649 nsect = BE_16(page3p->sect_track);
4650 intrlv = BE_16(page3p->interleave);
4652 SD_INFO(SD_LOG_COMMON, un,
4653 "sd_get_physical_geometry: Format Parameters (page 3)\n");
4654 SD_INFO(SD_LOG_COMMON, un,
4655 " mode page: %d; nsect: %d; sector size: %d;\n",
4656 page3p->mode_page.code, nsect, sector_size);
4657 SD_INFO(SD_LOG_COMMON, un,
4658 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
4659 BE_16(page3p->track_skew),
4660 BE_16(page3p->cylinder_skew));
4662 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
4665 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
4667 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
4668 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
4669 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
4670 if (status != 0) {
4671 SD_ERROR(SD_LOG_COMMON, un,
4672 "sd_get_physical_geometry: mode sense page 4 failed\n");
4673 goto page4_exit;
4677 * Determine size of Block Descriptors in order to locate the mode
4678 * page data. ATAPI devices return 0, SCSI devices should return
4679 * MODE_BLK_DESC_LENGTH.
4681 headerp = (struct mode_header *)p4bufp;
4682 if (un->un_f_cfg_is_atapi == TRUE) {
4683 struct mode_header_grp2 *mhp =
4684 (struct mode_header_grp2 *)headerp;
4685 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4686 } else {
4687 bd_len = ((struct mode_header *)headerp)->bdesc_length;
4690 if (bd_len > MODE_BLK_DESC_LENGTH) {
4691 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4692 "sd_get_physical_geometry: received unexpected bd_len of "
4693 "%d, page4\n", bd_len);
4694 status = EIO;
4695 goto page4_exit;
4698 page4p = (struct mode_geometry *)
4699 ((caddr_t)headerp + mode_header_length + bd_len);
4701 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
4702 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4703 "sd_get_physical_geometry: mode sense pg4 code mismatch "
4704 "%d\n", page4p->mode_page.code);
4705 status = EIO;
4706 goto page4_exit;
4710 * Stash the data now, after we know that both commands completed.
4714 nhead = (int)page4p->heads; /* uchar, so no conversion needed */
4715 spc = nhead * nsect;
4716 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
4717 rpm = BE_16(page4p->rpm);
4719 modesense_capacity = spc * ncyl;
4721 SD_INFO(SD_LOG_COMMON, un,
4722 "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
4723 SD_INFO(SD_LOG_COMMON, un,
4724 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
4725 SD_INFO(SD_LOG_COMMON, un,
4726 " computed capacity(h*s*c): %d;\n", modesense_capacity);
4727 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n",
4728 (void *)pgeom_p, capacity);
4731 * Compensate if the drive's geometry is not rectangular, i.e.,
4732 * the product of C * H * S returned by MODE SENSE >= that returned
4733 * by read capacity. This is an idiosyncrasy of the original x86
4734 * disk subsystem.
4736 if (modesense_capacity >= capacity) {
4737 SD_INFO(SD_LOG_COMMON, un,
4738 "sd_get_physical_geometry: adjusting acyl; "
4739 "old: %d; new: %d\n", pgeom_p->g_acyl,
4740 (modesense_capacity - capacity + spc - 1) / spc);
4741 if (sector_size != 0) {
4742 /* 1243403: NEC D38x7 drives don't support sec size */
4743 pgeom_p->g_secsize = (unsigned short)sector_size;
4745 pgeom_p->g_nsect = (unsigned short)nsect;
4746 pgeom_p->g_nhead = (unsigned short)nhead;
4747 pgeom_p->g_capacity = capacity;
4748 pgeom_p->g_acyl =
4749 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
4750 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl;
4753 pgeom_p->g_rpm = (unsigned short)rpm;
4754 pgeom_p->g_intrlv = (unsigned short)intrlv;
4755 ret = 0;
4757 SD_INFO(SD_LOG_COMMON, un,
4758 "sd_get_physical_geometry: mode sense geometry:\n");
4759 SD_INFO(SD_LOG_COMMON, un,
4760 " nsect: %d; sector size: %d; interlv: %d\n",
4761 nsect, sector_size, intrlv);
4762 SD_INFO(SD_LOG_COMMON, un,
4763 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
4764 nhead, ncyl, rpm, modesense_capacity);
4765 SD_INFO(SD_LOG_COMMON, un,
4766 "sd_get_physical_geometry: (cached)\n");
4767 SD_INFO(SD_LOG_COMMON, un,
4768 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4769 pgeom_p->g_ncyl, pgeom_p->g_acyl,
4770 pgeom_p->g_nhead, pgeom_p->g_nsect);
4771 SD_INFO(SD_LOG_COMMON, un,
4772 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
4773 pgeom_p->g_secsize, pgeom_p->g_capacity,
4774 pgeom_p->g_intrlv, pgeom_p->g_rpm);
4775 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
4777 page4_exit:
4778 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
4780 page3_exit:
4781 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
4783 if (status != 0) {
4784 if (status == EIO) {
4786 * Some disks do not support mode sense(6), we
4787 * should ignore this kind of error(sense key is
4788 * 0x5 - illegal request).
4790 uint8_t *sensep;
4791 int senlen;
4793 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
4794 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
4795 ssc->ssc_uscsi_cmd->uscsi_rqresid);
4797 if (senlen > 0 &&
4798 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
4799 sd_ssc_assessment(ssc,
4800 SD_FMT_IGNORE_COMPROMISE);
4801 } else {
4802 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
4804 } else {
4805 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
4808 sd_ssc_fini(ssc);
4809 return (ret);
4813 * Function: sd_get_virtual_geometry
4815 * Description: Ask the controller to tell us about the target device.
4817 * Arguments: un - pointer to softstate
4818 * capacity - disk capacity in #blocks
4819 * lbasize - disk block size in bytes
4821 * Context: Kernel thread only
4824 static int
4825 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
4826 diskaddr_t capacity, int lbasize)
4828 uint_t geombuf;
4829 int spc;
4831 ASSERT(un != NULL);
4833 /* Set sector size, and total number of sectors */
4834 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1);
4835 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
4837 /* Let the HBA tell us its geometry */
4838 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
4840 /* A value of -1 indicates an undefined "geometry" property */
4841 if (geombuf == (-1)) {
4842 return (EINVAL);
4845 /* Initialize the logical geometry cache. */
4846 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff;
4847 lgeom_p->g_nsect = geombuf & 0xffff;
4848 lgeom_p->g_secsize = un->un_sys_blocksize;
4850 spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
4853 * Note: The driver originally converted the capacity value from
4854 * target blocks to system blocks. However, the capacity value passed
4855 * to this routine is already in terms of system blocks (this scaling
4856 * is done when the READ CAPACITY command is issued and processed).
4857 * This 'error' may have gone undetected because the usage of g_ncyl
4858 * (which is based upon g_capacity) is very limited within the driver
4860 lgeom_p->g_capacity = capacity;
4863 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
4864 * hba may return zero values if the device has been removed.
4866 if (spc == 0) {
4867 lgeom_p->g_ncyl = 0;
4868 } else {
4869 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
4871 lgeom_p->g_acyl = 0;
4873 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
4874 return (0);
4878 * Function: sd_update_block_info
4880 * Description: Calculate a byte count to sector count bitshift value
4881 * from sector size.
4883 * Arguments: un: unit struct.
4884 * lbasize: new target sector size
4885 * capacity: new target capacity, ie. block count
4887 * Context: Kernel thread context
4890 static void
4891 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
4893 if (lbasize != 0) {
4894 un->un_tgt_blocksize = lbasize;
4895 un->un_f_tgt_blocksize_is_valid = TRUE;
4896 if (!un->un_f_has_removable_media) {
4897 un->un_sys_blocksize = lbasize;
4901 if (capacity != 0) {
4902 un->un_blockcount = capacity;
4903 un->un_f_blockcount_is_valid = TRUE;
4906 * The capacity has changed so update the errstats.
4908 if (un->un_errstats != NULL) {
4909 struct sd_errstats *stp;
4911 capacity *= un->un_sys_blocksize;
4912 stp = (struct sd_errstats *)un->un_errstats->ks_data;
4913 if (stp->sd_capacity.value.ui64 < capacity)
4914 stp->sd_capacity.value.ui64 = capacity;
4920 * Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
4921 * vpd_pg, in which case all the block limits will be reset to the defaults.
4923 static void
4924 sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
4926 sd_blk_limits_t *lim = &un->un_blk_lim;
4927 unsigned pg_len;
4929 if (vpd_pg != NULL)
4930 pg_len = BE_IN16(&vpd_pg[2]);
4931 else
4932 pg_len = 0;
4934 /* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
4935 if (pg_len >= 0x10) {
4936 lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
4937 lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
4938 lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
4940 /* Zero means not reported, so use "unlimited" */
4941 if (lim->lim_max_xfer_len == 0)
4942 lim->lim_max_xfer_len = UINT32_MAX;
4943 if (lim->lim_opt_xfer_len == 0)
4944 lim->lim_opt_xfer_len = UINT32_MAX;
4945 } else {
4946 lim->lim_opt_xfer_len_gran = 0;
4947 lim->lim_max_xfer_len = UINT32_MAX;
4948 lim->lim_opt_xfer_len = UINT32_MAX;
4950 if (pg_len >= 0x3c) {
4951 lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
4953 * A zero in either of the following two fields indicates lack
4954 * of UNMAP support.
4956 lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
4957 lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
4958 lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
4959 if ((vpd_pg[32] >> 7) == 1) {
4960 lim->lim_unmap_gran_align =
4961 ((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
4962 (vpd_pg[34] << 8) | vpd_pg[35];
4963 } else {
4964 lim->lim_unmap_gran_align = 0;
4966 lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
4967 } else {
4968 lim->lim_max_pfetch_len = UINT32_MAX;
4969 lim->lim_max_unmap_lba_cnt = UINT32_MAX;
4970 lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
4971 lim->lim_opt_unmap_gran = 0;
4972 lim->lim_unmap_gran_align = 0;
4973 lim->lim_max_write_same_len = UINT64_MAX;
4978 * Collects VPD page B0 data if available (block limits). If the data is
4979 * not available or querying the device failed, we revert to the defaults.
4981 static void
4982 sd_setup_blk_limits(sd_ssc_t *ssc)
4984 struct sd_lun *un = ssc->ssc_un;
4985 uchar_t *inqB0 = NULL;
4986 size_t inqB0_resid = 0;
4987 int rval;
4989 if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
4990 inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
4991 rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
4992 0xB0, &inqB0_resid);
4993 if (rval != 0) {
4994 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
4995 kmem_free(inqB0, MAX_INQUIRY_SIZE);
4996 inqB0 = NULL;
4999 /* passing NULL inqB0 will reset to defaults */
5000 sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
5001 if (inqB0)
5002 kmem_free(inqB0, MAX_INQUIRY_SIZE);
5006 * Function: sd_register_devid
5008 * Description: This routine will obtain the device id information from the
5009 * target, obtain the serial number, and register the device
5010 * id with the ddi framework.
5012 * Arguments: devi - the system's dev_info_t for the device.
5013 * un - driver soft state (unit) structure
5014 * reservation_flag - indicates if a reservation conflict
5015 * occurred during attach
5017 * Context: Kernel Thread
5019 static void
5020 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5022 int rval = 0;
5023 uchar_t *inq80 = NULL;
5024 size_t inq80_len = MAX_INQUIRY_SIZE;
5025 size_t inq80_resid = 0;
5026 uchar_t *inq83 = NULL;
5027 size_t inq83_len = MAX_INQUIRY_SIZE;
5028 size_t inq83_resid = 0;
5029 int dlen, len;
5030 char *sn;
5031 struct sd_lun *un;
5033 ASSERT(ssc != NULL);
5034 un = ssc->ssc_un;
5035 ASSERT(un != NULL);
5036 ASSERT(mutex_owned(SD_MUTEX(un)));
5037 ASSERT((SD_DEVINFO(un)) == devi);
5041 * We check the availability of the World Wide Name (0x83) and Unit
5042 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5043 * un_vpd_page_mask from them, we decide which way to get the WWN. If
5044 * 0x83 is available, that is the best choice. Our next choice is
5045 * 0x80. If neither are available, we munge the devid from the device
5046 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5047 * to fabricate a devid for non-Sun qualified disks.
5049 if (sd_check_vpd_page_support(ssc) == 0) {
5050 /* collect page 80 data if available */
5051 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5053 mutex_exit(SD_MUTEX(un));
5054 inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5056 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5057 0x01, 0x80, &inq80_resid);
5059 if (rval != 0) {
5060 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5061 kmem_free(inq80, inq80_len);
5062 inq80 = NULL;
5063 inq80_len = 0;
5064 } else if (ddi_prop_exists(
5065 DDI_DEV_T_NONE, SD_DEVINFO(un),
5066 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5067 INQUIRY_SERIAL_NO) == 0) {
5069 * If we don't already have a serial number
5070 * property, do quick verify of data returned
5071 * and define property.
5073 dlen = inq80_len - inq80_resid;
5074 len = (size_t)inq80[3];
5075 if ((dlen >= 4) && ((len + 4) <= dlen)) {
5077 * Ensure sn termination, skip leading
5078 * blanks, and create property
5079 * 'inquiry-serial-no'.
5081 sn = (char *)&inq80[4];
5082 sn[len] = 0;
5083 while (*sn && (*sn == ' '))
5084 sn++;
5085 if (*sn) {
5086 (void) ddi_prop_update_string(
5087 DDI_DEV_T_NONE,
5088 SD_DEVINFO(un),
5089 INQUIRY_SERIAL_NO, sn);
5093 mutex_enter(SD_MUTEX(un));
5096 /* collect page 83 data if available */
5097 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5098 mutex_exit(SD_MUTEX(un));
5099 inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5101 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5102 0x01, 0x83, &inq83_resid);
5104 if (rval != 0) {
5105 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5106 kmem_free(inq83, inq83_len);
5107 inq83 = NULL;
5108 inq83_len = 0;
5110 mutex_enter(SD_MUTEX(un));
5115 * If transport has already registered a devid for this target
5116 * then that takes precedence over the driver's determination
5117 * of the devid.
5119 * NOTE: The reason this check is done here instead of at the beginning
5120 * of the function is to allow the code above to create the
5121 * 'inquiry-serial-no' property.
5123 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5124 ASSERT(un->un_devid);
5125 un->un_f_devid_transport_defined = TRUE;
5126 goto cleanup; /* use devid registered by the transport */
5130 * This is the case of antiquated Sun disk drives that have the
5131 * FAB_DEVID property set in the disk_table. These drives
5132 * manage the devid's by storing them in last 2 available sectors
5133 * on the drive and have them fabricated by the ddi layer by calling
5134 * ddi_devid_init and passing the DEVID_FAB flag.
5136 if (un->un_f_opt_fab_devid == TRUE) {
5138 * Depending on EINVAL isn't reliable, since a reserved disk
5139 * may result in invalid geometry, so check to make sure a
5140 * reservation conflict did not occur during attach.
5142 if ((sd_get_devid(ssc) == EINVAL) &&
5143 (reservation_flag != SD_TARGET_IS_RESERVED)) {
5145 * The devid is invalid AND there is no reservation
5146 * conflict. Fabricate a new devid.
5148 (void) sd_create_devid(ssc);
5151 /* Register the devid if it exists */
5152 if (un->un_devid != NULL) {
5153 (void) ddi_devid_register(SD_DEVINFO(un),
5154 un->un_devid);
5155 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5156 "sd_register_devid: Devid Fabricated\n");
5158 goto cleanup;
5161 /* encode best devid possible based on data available */
5162 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5163 (char *)ddi_driver_name(SD_DEVINFO(un)),
5164 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5165 inq80, inq80_len - inq80_resid, inq83, inq83_len -
5166 inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5168 /* devid successfully encoded, register devid */
5169 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5171 } else {
5173 * Unable to encode a devid based on data available.
5174 * This is not a Sun qualified disk. Older Sun disk
5175 * drives that have the SD_FAB_DEVID property
5176 * set in the disk_table and non Sun qualified
5177 * disks are treated in the same manner. These
5178 * drives manage the devid's by storing them in
5179 * last 2 available sectors on the drive and
5180 * have them fabricated by the ddi layer by
5181 * calling ddi_devid_init and passing the
5182 * DEVID_FAB flag.
5183 * Create a fabricate devid only if there's no
5184 * fabricate devid existed.
5186 if (sd_get_devid(ssc) == EINVAL) {
5187 (void) sd_create_devid(ssc);
5189 un->un_f_opt_fab_devid = TRUE;
5191 /* Register the devid if it exists */
5192 if (un->un_devid != NULL) {
5193 (void) ddi_devid_register(SD_DEVINFO(un),
5194 un->un_devid);
5195 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5196 "sd_register_devid: devid fabricated using "
5197 "ddi framework\n");
5201 cleanup:
5202 /* clean up resources */
5203 if (inq80 != NULL) {
5204 kmem_free(inq80, inq80_len);
5206 if (inq83 != NULL) {
5207 kmem_free(inq83, inq83_len);
5214 * Function: sd_get_devid
5216 * Description: This routine will return 0 if a valid device id has been
5217 * obtained from the target and stored in the soft state. If a
5218 * valid device id has not been previously read and stored, a
5219 * read attempt will be made.
5221 * Arguments: un - driver soft state (unit) structure
5223 * Return Code: 0 if we successfully get the device id
5225 * Context: Kernel Thread
5228 static int
5229 sd_get_devid(sd_ssc_t *ssc)
5231 struct dk_devid *dkdevid;
5232 ddi_devid_t tmpid;
5233 uint_t *ip;
5234 size_t sz;
5235 diskaddr_t blk;
5236 int status;
5237 int chksum;
5238 int i;
5239 size_t buffer_size;
5240 struct sd_lun *un;
5242 ASSERT(ssc != NULL);
5243 un = ssc->ssc_un;
5244 ASSERT(un != NULL);
5245 ASSERT(mutex_owned(SD_MUTEX(un)));
5247 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5248 un);
5250 if (un->un_devid != NULL) {
5251 return (0);
5254 mutex_exit(SD_MUTEX(un));
5255 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5256 (void *)SD_PATH_DIRECT) != 0) {
5257 mutex_enter(SD_MUTEX(un));
5258 return (EINVAL);
5262 * Read and verify device id, stored in the reserved cylinders at the
5263 * end of the disk. Backup label is on the odd sectors of the last
5264 * track of the last cylinder. Device id will be on track of the next
5265 * to last cylinder.
5267 mutex_enter(SD_MUTEX(un));
5268 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5269 mutex_exit(SD_MUTEX(un));
5270 dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5271 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5272 SD_PATH_DIRECT);
5274 if (status != 0) {
5275 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5276 goto error;
5279 /* Validate the revision */
5280 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5281 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5282 status = EINVAL;
5283 goto error;
5286 /* Calculate the checksum */
5287 chksum = 0;
5288 ip = (uint_t *)dkdevid;
5289 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5290 i++) {
5291 chksum ^= ip[i];
5294 /* Compare the checksums */
5295 if (DKD_GETCHKSUM(dkdevid) != chksum) {
5296 status = EINVAL;
5297 goto error;
5300 /* Validate the device id */
5301 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5302 status = EINVAL;
5303 goto error;
5307 * Store the device id in the driver soft state
5309 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5310 tmpid = kmem_alloc(sz, KM_SLEEP);
5312 mutex_enter(SD_MUTEX(un));
5314 un->un_devid = tmpid;
5315 bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5317 kmem_free(dkdevid, buffer_size);
5319 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5321 return (status);
5322 error:
5323 mutex_enter(SD_MUTEX(un));
5324 kmem_free(dkdevid, buffer_size);
5325 return (status);
5330 * Function: sd_create_devid
5332 * Description: This routine will fabricate the device id and write it
5333 * to the disk.
5335 * Arguments: un - driver soft state (unit) structure
5337 * Return Code: value of the fabricated device id
5339 * Context: Kernel Thread
5342 static ddi_devid_t
5343 sd_create_devid(sd_ssc_t *ssc)
5345 struct sd_lun *un;
5347 ASSERT(ssc != NULL);
5348 un = ssc->ssc_un;
5349 ASSERT(un != NULL);
5351 /* Fabricate the devid */
5352 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5353 == DDI_FAILURE) {
5354 return (NULL);
5357 /* Write the devid to disk */
5358 if (sd_write_deviceid(ssc) != 0) {
5359 ddi_devid_free(un->un_devid);
5360 un->un_devid = NULL;
5363 return (un->un_devid);
5368 * Function: sd_write_deviceid
5370 * Description: This routine will write the device id to the disk
5371 * reserved sector.
5373 * Arguments: un - driver soft state (unit) structure
5375 * Return Code: EINVAL
5376 * value returned by sd_send_scsi_cmd
5378 * Context: Kernel Thread
5381 static int
5382 sd_write_deviceid(sd_ssc_t *ssc)
5384 struct dk_devid *dkdevid;
5385 uchar_t *buf;
5386 diskaddr_t blk;
5387 uint_t *ip, chksum;
5388 int status;
5389 int i;
5390 struct sd_lun *un;
5392 ASSERT(ssc != NULL);
5393 un = ssc->ssc_un;
5394 ASSERT(un != NULL);
5395 ASSERT(mutex_owned(SD_MUTEX(un)));
5397 mutex_exit(SD_MUTEX(un));
5398 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5399 (void *)SD_PATH_DIRECT) != 0) {
5400 mutex_enter(SD_MUTEX(un));
5401 return (-1);
5405 /* Allocate the buffer */
5406 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5407 dkdevid = (struct dk_devid *)buf;
5409 /* Fill in the revision */
5410 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5411 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5413 /* Copy in the device id */
5414 mutex_enter(SD_MUTEX(un));
5415 bcopy(un->un_devid, &dkdevid->dkd_devid,
5416 ddi_devid_sizeof(un->un_devid));
5417 mutex_exit(SD_MUTEX(un));
5419 /* Calculate the checksum */
5420 chksum = 0;
5421 ip = (uint_t *)dkdevid;
5422 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5423 i++) {
5424 chksum ^= ip[i];
5427 /* Fill-in checksum */
5428 DKD_FORMCHKSUM(chksum, dkdevid);
5430 /* Write the reserved sector */
5431 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5432 SD_PATH_DIRECT);
5433 if (status != 0)
5434 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5436 kmem_free(buf, un->un_sys_blocksize);
5438 mutex_enter(SD_MUTEX(un));
5439 return (status);
5444 * Function: sd_check_vpd_page_support
5446 * Description: This routine sends an inquiry command with the EVPD bit set and
5447 * a page code of 0x00 to the device. It is used to determine which
5448 * vital product pages are available to find the devid. We are
5449 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1,
5450 * the device does not support that command.
5452 * Arguments: un - driver soft state (unit) structure
5454 * Return Code: 0 - success
5455 * 1 - check condition
5457 * Context: This routine can sleep.
5460 static int
5461 sd_check_vpd_page_support(sd_ssc_t *ssc)
5463 uchar_t *page_list = NULL;
5464 uchar_t page_length = 0xff; /* Use max possible length */
5465 uchar_t evpd = 0x01; /* Set the EVPD bit */
5466 uchar_t page_code = 0x00; /* Supported VPD Pages */
5467 int rval = 0;
5468 int counter;
5469 struct sd_lun *un;
5471 ASSERT(ssc != NULL);
5472 un = ssc->ssc_un;
5473 ASSERT(un != NULL);
5474 ASSERT(mutex_owned(SD_MUTEX(un)));
5476 mutex_exit(SD_MUTEX(un));
5479 * We'll set the page length to the maximum to save figuring it out
5480 * with an additional call.
5482 page_list = kmem_zalloc(page_length, KM_SLEEP);
5484 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5485 page_code, NULL);
5487 if (rval != 0)
5488 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5490 mutex_enter(SD_MUTEX(un));
5493 * Now we must validate that the device accepted the command, as some
5494 * drives do not support it. If the drive does support it, we will
5495 * return 0, and the supported pages will be in un_vpd_page_mask. If
5496 * not, we return -1.
5498 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5499 /* Loop to find one of the 2 pages we need */
5500 counter = 4; /* Supported pages start at byte 4, with 0x00 */
5503 * Pages are returned in ascending order, and 0x83 is what we
5504 * are hoping for.
5506 while ((page_list[counter] <= 0xB1) &&
5507 (counter <= (page_list[VPD_PAGE_LENGTH] +
5508 VPD_HEAD_OFFSET))) {
5510 * Add 3 because page_list[3] is the number of
5511 * pages minus 3
5514 switch (page_list[counter]) {
5515 case 0x00:
5516 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5517 break;
5518 case 0x80:
5519 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5520 break;
5521 case 0x81:
5522 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5523 break;
5524 case 0x82:
5525 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5526 break;
5527 case 0x83:
5528 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5529 break;
5530 case 0x86:
5531 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5532 break;
5533 case 0xB0:
5534 un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
5535 break;
5536 case 0xB1:
5537 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5538 break;
5540 counter++;
5543 } else {
5544 rval = -1;
5546 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5547 "sd_check_vpd_page_support: This drive does not implement "
5548 "VPD pages.\n");
5551 kmem_free(page_list, page_length);
5553 return (rval);
5558 * Function: sd_setup_pm
5560 * Description: Initialize Power Management on the device
5562 * Context: Kernel Thread
5565 static void
5566 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5568 uint_t log_page_size;
5569 uchar_t *log_page_data;
5570 int rval = 0;
5571 struct sd_lun *un;
5573 ASSERT(ssc != NULL);
5574 un = ssc->ssc_un;
5575 ASSERT(un != NULL);
5578 * Since we are called from attach, holding a mutex for
5579 * un is unnecessary. Because some of the routines called
5580 * from here require SD_MUTEX to not be held, assert this
5581 * right up front.
5583 ASSERT(!mutex_owned(SD_MUTEX(un)));
5585 * Since the sd device does not have the 'reg' property,
5586 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5587 * The following code is to tell cpr that this device
5588 * DOES need to be suspended and resumed.
5590 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5591 "pm-hardware-state", "needs-suspend-resume");
5594 * This complies with the new power management framework
5595 * for certain desktop machines. Create the pm_components
5596 * property as a string array property.
5597 * If un_f_pm_supported is TRUE, that means the disk
5598 * attached HBA has set the "pm-capable" property and
5599 * the value of this property is bigger than 0.
5601 if (un->un_f_pm_supported) {
5603 * not all devices have a motor, try it first.
5604 * some devices may return ILLEGAL REQUEST, some
5605 * will hang
5606 * The following START_STOP_UNIT is used to check if target
5607 * device has a motor.
5609 un->un_f_start_stop_supported = TRUE;
5611 if (un->un_f_power_condition_supported) {
5612 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5613 SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5614 SD_PATH_DIRECT);
5615 if (rval != 0) {
5616 un->un_f_power_condition_supported = FALSE;
5619 if (!un->un_f_power_condition_supported) {
5620 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5621 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5623 if (rval != 0) {
5624 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5625 un->un_f_start_stop_supported = FALSE;
5629 * create pm properties anyways otherwise the parent can't
5630 * go to sleep
5632 un->un_f_pm_is_enabled = TRUE;
5633 (void) sd_create_pm_components(devi, un);
5636 * If it claims that log sense is supported, check it out.
5638 if (un->un_f_log_sense_supported) {
5639 rval = sd_log_page_supported(ssc,
5640 START_STOP_CYCLE_PAGE);
5641 if (rval == 1) {
5642 /* Page found, use it. */
5643 un->un_start_stop_cycle_page =
5644 START_STOP_CYCLE_PAGE;
5645 } else {
5647 * Page not found or log sense is not
5648 * supported.
5649 * Notice we do not check the old style
5650 * START_STOP_CYCLE_VU_PAGE because this
5651 * code path does not apply to old disks.
5653 un->un_f_log_sense_supported = FALSE;
5654 un->un_f_pm_log_sense_smart = FALSE;
5658 return;
5662 * For the disk whose attached HBA has not set the "pm-capable"
5663 * property, check if it supports the power management.
5665 if (!un->un_f_log_sense_supported) {
5666 un->un_power_level = SD_SPINDLE_ON;
5667 un->un_f_pm_is_enabled = FALSE;
5668 return;
5671 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5673 #ifdef SDDEBUG
5674 if (sd_force_pm_supported) {
5675 /* Force a successful result */
5676 rval = 1;
5678 #endif
5681 * If the start-stop cycle counter log page is not supported
5682 * or if the pm-capable property is set to be false (0),
5683 * then we should not create the pm_components property.
5685 if (rval == -1) {
5687 * Error.
5688 * Reading log sense failed, most likely this is
5689 * an older drive that does not support log sense.
5690 * If this fails auto-pm is not supported.
5692 un->un_power_level = SD_SPINDLE_ON;
5693 un->un_f_pm_is_enabled = FALSE;
5695 } else if (rval == 0) {
5697 * Page not found.
5698 * The start stop cycle counter is implemented as page
5699 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
5700 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
5702 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
5704 * Page found, use this one.
5706 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
5707 un->un_f_pm_is_enabled = TRUE;
5708 } else {
5710 * Error or page not found.
5711 * auto-pm is not supported for this device.
5713 un->un_power_level = SD_SPINDLE_ON;
5714 un->un_f_pm_is_enabled = FALSE;
5716 } else {
5718 * Page found, use it.
5720 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
5721 un->un_f_pm_is_enabled = TRUE;
5725 if (un->un_f_pm_is_enabled == TRUE) {
5726 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
5727 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
5729 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
5730 log_page_size, un->un_start_stop_cycle_page,
5731 0x01, 0, SD_PATH_DIRECT);
5733 if (rval != 0) {
5734 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5737 #ifdef SDDEBUG
5738 if (sd_force_pm_supported) {
5739 /* Force a successful result */
5740 rval = 0;
5742 #endif
5745 * If the Log sense for Page( Start/stop cycle counter page)
5746 * succeeds, then power management is supported and we can
5747 * enable auto-pm.
5749 if (rval == 0) {
5750 (void) sd_create_pm_components(devi, un);
5751 } else {
5752 un->un_power_level = SD_SPINDLE_ON;
5753 un->un_f_pm_is_enabled = FALSE;
5756 kmem_free(log_page_data, log_page_size);
5762 * Function: sd_create_pm_components
5764 * Description: Initialize PM property.
5766 * Context: Kernel thread context
5769 static void
5770 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
5772 ASSERT(!mutex_owned(SD_MUTEX(un)));
5774 if (un->un_f_power_condition_supported) {
5775 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
5776 "pm-components", sd_pwr_pc.pm_comp, 5)
5777 != DDI_PROP_SUCCESS) {
5778 un->un_power_level = SD_SPINDLE_ACTIVE;
5779 un->un_f_pm_is_enabled = FALSE;
5780 return;
5782 } else {
5783 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
5784 "pm-components", sd_pwr_ss.pm_comp, 3)
5785 != DDI_PROP_SUCCESS) {
5786 un->un_power_level = SD_SPINDLE_ON;
5787 un->un_f_pm_is_enabled = FALSE;
5788 return;
5792 * When components are initially created they are idle,
5793 * power up any non-removables.
5794 * Note: the return value of pm_raise_power can't be used
5795 * for determining if PM should be enabled for this device.
5796 * Even if you check the return values and remove this
5797 * property created above, the PM framework will not honor the
5798 * change after the first call to pm_raise_power. Hence,
5799 * removal of that property does not help if pm_raise_power
5800 * fails. In the case of removable media, the start/stop
5801 * will fail if the media is not present.
5803 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
5804 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
5805 mutex_enter(SD_MUTEX(un));
5806 un->un_power_level = SD_PM_STATE_ACTIVE(un);
5807 mutex_enter(&un->un_pm_mutex);
5808 /* Set to on and not busy. */
5809 un->un_pm_count = 0;
5810 } else {
5811 mutex_enter(SD_MUTEX(un));
5812 un->un_power_level = SD_PM_STATE_STOPPED(un);
5813 mutex_enter(&un->un_pm_mutex);
5814 /* Set to off. */
5815 un->un_pm_count = -1;
5817 mutex_exit(&un->un_pm_mutex);
5818 mutex_exit(SD_MUTEX(un));
5823 * Function: sd_ddi_suspend
5825 * Description: Performs system power-down operations. This includes
5826 * setting the drive state to indicate its suspended so
5827 * that no new commands will be accepted. Also, wait for
5828 * all commands that are in transport or queued to a timer
5829 * for retry to complete. All timeout threads are cancelled.
5831 * Return Code: DDI_FAILURE or DDI_SUCCESS
5833 * Context: Kernel thread context
5836 static int
5837 sd_ddi_suspend(dev_info_t *devi)
5839 struct sd_lun *un;
5840 clock_t wait_cmds_complete;
5842 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
5843 if (un == NULL) {
5844 return (DDI_FAILURE);
5847 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
5849 mutex_enter(SD_MUTEX(un));
5851 /* Return success if the device is already suspended. */
5852 if (un->un_state == SD_STATE_SUSPENDED) {
5853 mutex_exit(SD_MUTEX(un));
5854 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5855 "device already suspended, exiting\n");
5856 return (DDI_SUCCESS);
5859 /* Return failure if the device is being used by HA */
5860 if (un->un_resvd_status &
5861 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
5862 mutex_exit(SD_MUTEX(un));
5863 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5864 "device in use by HA, exiting\n");
5865 return (DDI_FAILURE);
5869 * Return failure if the device is in a resource wait
5870 * or power changing state.
5872 if ((un->un_state == SD_STATE_RWAIT) ||
5873 (un->un_state == SD_STATE_PM_CHANGING)) {
5874 mutex_exit(SD_MUTEX(un));
5875 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5876 "device in resource wait state, exiting\n");
5877 return (DDI_FAILURE);
5881 un->un_save_state = un->un_last_state;
5882 New_state(un, SD_STATE_SUSPENDED);
5885 * Wait for all commands that are in transport or queued to a timer
5886 * for retry to complete.
5888 * While waiting, no new commands will be accepted or sent because of
5889 * the new state we set above.
5891 * Wait till current operation has completed. If we are in the resource
5892 * wait state (with an intr outstanding) then we need to wait till the
5893 * intr completes and starts the next cmd. We want to wait for
5894 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
5896 wait_cmds_complete = ddi_get_lbolt() +
5897 (sd_wait_cmds_complete * drv_usectohz(1000000));
5899 while (un->un_ncmds_in_transport != 0) {
5901 * Fail if commands do not finish in the specified time.
5903 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
5904 wait_cmds_complete) == -1) {
5906 * Undo the state changes made above. Everything
5907 * must go back to it's original value.
5909 Restore_state(un);
5910 un->un_last_state = un->un_save_state;
5911 /* Wake up any threads that might be waiting. */
5912 cv_broadcast(&un->un_suspend_cv);
5913 mutex_exit(SD_MUTEX(un));
5914 SD_ERROR(SD_LOG_IO_PM, un,
5915 "sd_ddi_suspend: failed due to outstanding cmds\n");
5916 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
5917 return (DDI_FAILURE);
5922 * Cancel SCSI watch thread and timeouts, if any are active
5925 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
5926 opaque_t temp_token = un->un_swr_token;
5927 mutex_exit(SD_MUTEX(un));
5928 scsi_watch_suspend(temp_token);
5929 mutex_enter(SD_MUTEX(un));
5932 if (un->un_reset_throttle_timeid != NULL) {
5933 timeout_id_t temp_id = un->un_reset_throttle_timeid;
5934 un->un_reset_throttle_timeid = NULL;
5935 mutex_exit(SD_MUTEX(un));
5936 (void) untimeout(temp_id);
5937 mutex_enter(SD_MUTEX(un));
5940 if (un->un_dcvb_timeid != NULL) {
5941 timeout_id_t temp_id = un->un_dcvb_timeid;
5942 un->un_dcvb_timeid = NULL;
5943 mutex_exit(SD_MUTEX(un));
5944 (void) untimeout(temp_id);
5945 mutex_enter(SD_MUTEX(un));
5948 mutex_enter(&un->un_pm_mutex);
5949 if (un->un_pm_timeid != NULL) {
5950 timeout_id_t temp_id = un->un_pm_timeid;
5951 un->un_pm_timeid = NULL;
5952 mutex_exit(&un->un_pm_mutex);
5953 mutex_exit(SD_MUTEX(un));
5954 (void) untimeout(temp_id);
5955 mutex_enter(SD_MUTEX(un));
5956 } else {
5957 mutex_exit(&un->un_pm_mutex);
5960 if (un->un_rmw_msg_timeid != NULL) {
5961 timeout_id_t temp_id = un->un_rmw_msg_timeid;
5962 un->un_rmw_msg_timeid = NULL;
5963 mutex_exit(SD_MUTEX(un));
5964 (void) untimeout(temp_id);
5965 mutex_enter(SD_MUTEX(un));
5968 if (un->un_retry_timeid != NULL) {
5969 timeout_id_t temp_id = un->un_retry_timeid;
5970 un->un_retry_timeid = NULL;
5971 mutex_exit(SD_MUTEX(un));
5972 (void) untimeout(temp_id);
5973 mutex_enter(SD_MUTEX(un));
5975 if (un->un_retry_bp != NULL) {
5976 un->un_retry_bp->av_forw = un->un_waitq_headp;
5977 un->un_waitq_headp = un->un_retry_bp;
5978 if (un->un_waitq_tailp == NULL) {
5979 un->un_waitq_tailp = un->un_retry_bp;
5981 un->un_retry_bp = NULL;
5982 un->un_retry_statp = NULL;
5986 if (un->un_direct_priority_timeid != NULL) {
5987 timeout_id_t temp_id = un->un_direct_priority_timeid;
5988 un->un_direct_priority_timeid = NULL;
5989 mutex_exit(SD_MUTEX(un));
5990 (void) untimeout(temp_id);
5991 mutex_enter(SD_MUTEX(un));
5994 if (un->un_f_is_fibre == TRUE) {
5996 * Remove callbacks for insert and remove events
5998 if (un->un_insert_event != NULL) {
5999 mutex_exit(SD_MUTEX(un));
6000 (void) ddi_remove_event_handler(un->un_insert_cb_id);
6001 mutex_enter(SD_MUTEX(un));
6002 un->un_insert_event = NULL;
6005 if (un->un_remove_event != NULL) {
6006 mutex_exit(SD_MUTEX(un));
6007 (void) ddi_remove_event_handler(un->un_remove_cb_id);
6008 mutex_enter(SD_MUTEX(un));
6009 un->un_remove_event = NULL;
6013 mutex_exit(SD_MUTEX(un));
6015 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6017 return (DDI_SUCCESS);
6022 * Function: sd_ddi_resume
6024 * Description: Performs system power-up operations..
6026 * Return Code: DDI_SUCCESS
6027 * DDI_FAILURE
6029 * Context: Kernel thread context
6032 static int
6033 sd_ddi_resume(dev_info_t *devi)
6035 struct sd_lun *un;
6037 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6038 if (un == NULL) {
6039 return (DDI_FAILURE);
6042 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6044 mutex_enter(SD_MUTEX(un));
6045 Restore_state(un);
6048 * Restore the state which was saved to give the
6049 * the right state in un_last_state
6051 un->un_last_state = un->un_save_state;
6053 * Note: throttle comes back at full.
6054 * Also note: this MUST be done before calling pm_raise_power
6055 * otherwise the system can get hung in biowait. The scenario where
6056 * this'll happen is under cpr suspend. Writing of the system
6057 * state goes through sddump, which writes 0 to un_throttle. If
6058 * writing the system state then fails, example if the partition is
6059 * too small, then cpr attempts a resume. If throttle isn't restored
6060 * from the saved value until after calling pm_raise_power then
6061 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6062 * in biowait.
6064 un->un_throttle = un->un_saved_throttle;
6067 * The chance of failure is very rare as the only command done in power
6068 * entry point is START command when you transition from 0->1 or
6069 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6070 * which suspend was done. Ignore the return value as the resume should
6071 * not be failed. In the case of removable media the media need not be
6072 * inserted and hence there is a chance that raise power will fail with
6073 * media not present.
6075 if (un->un_f_attach_spinup) {
6076 mutex_exit(SD_MUTEX(un));
6077 (void) pm_raise_power(SD_DEVINFO(un), 0,
6078 SD_PM_STATE_ACTIVE(un));
6079 mutex_enter(SD_MUTEX(un));
6083 * Don't broadcast to the suspend cv and therefore possibly
6084 * start I/O until after power has been restored.
6086 cv_broadcast(&un->un_suspend_cv);
6087 cv_broadcast(&un->un_state_cv);
6089 /* restart thread */
6090 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6091 scsi_watch_resume(un->un_swr_token);
6095 * Transport any pending commands to the target.
6097 * If this is a low-activity device commands in queue will have to wait
6098 * until new commands come in, which may take awhile. Also, we
6099 * specifically don't check un_ncmds_in_transport because we know that
6100 * there really are no commands in progress after the unit was
6101 * suspended and we could have reached the throttle level, been
6102 * suspended, and have no new commands coming in for awhile. Highly
6103 * unlikely, but so is the low-activity disk scenario.
6105 ddi_xbuf_dispatch(un->un_xbuf_attr);
6107 sd_start_cmds(un, NULL);
6108 mutex_exit(SD_MUTEX(un));
6110 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6112 return (DDI_SUCCESS);
6117 * Function: sd_pm_state_change
6119 * Description: Change the driver power state.
6120 * Someone else is required to actually change the driver
6121 * power level.
6123 * Arguments: un - driver soft state (unit) structure
6124 * level - the power level that is changed to
6125 * flag - to decide how to change the power state
6127 * Return Code: DDI_SUCCESS
6129 * Context: Kernel thread context
6131 static int
6132 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6134 ASSERT(un != NULL);
6135 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6137 ASSERT(!mutex_owned(SD_MUTEX(un)));
6138 mutex_enter(SD_MUTEX(un));
6140 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6141 un->un_power_level = level;
6142 ASSERT(!mutex_owned(&un->un_pm_mutex));
6143 mutex_enter(&un->un_pm_mutex);
6144 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6145 un->un_pm_count++;
6146 ASSERT(un->un_pm_count == 0);
6148 mutex_exit(&un->un_pm_mutex);
6149 } else {
6151 * Exit if power management is not enabled for this device,
6152 * or if the device is being used by HA.
6154 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6155 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6156 mutex_exit(SD_MUTEX(un));
6157 SD_TRACE(SD_LOG_POWER, un,
6158 "sd_pm_state_change: exiting\n");
6159 return (DDI_FAILURE);
6162 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6163 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6166 * See if the device is not busy, ie.:
6167 * - we have no commands in the driver for this device
6168 * - not waiting for resources
6170 if ((un->un_ncmds_in_driver == 0) &&
6171 (un->un_state != SD_STATE_RWAIT)) {
6173 * The device is not busy, so it is OK to go to low
6174 * power state. Indicate low power, but rely on someone
6175 * else to actually change it.
6177 mutex_enter(&un->un_pm_mutex);
6178 un->un_pm_count = -1;
6179 mutex_exit(&un->un_pm_mutex);
6180 un->un_power_level = level;
6184 mutex_exit(SD_MUTEX(un));
6186 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6188 return (DDI_SUCCESS);
6193 * Function: sd_pm_idletimeout_handler
6195 * Description: A timer routine that's active only while a device is busy.
6196 * The purpose is to extend slightly the pm framework's busy
6197 * view of the device to prevent busy/idle thrashing for
6198 * back-to-back commands. Do this by comparing the current time
6199 * to the time at which the last command completed and when the
6200 * difference is greater than sd_pm_idletime, call
6201 * pm_idle_component. In addition to indicating idle to the pm
6202 * framework, update the chain type to again use the internal pm
6203 * layers of the driver.
6205 * Arguments: arg - driver soft state (unit) structure
6207 * Context: Executes in a timeout(9F) thread context
6210 static void
6211 sd_pm_idletimeout_handler(void *arg)
6213 const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6214 struct sd_lun *un = arg;
6216 mutex_enter(&sd_detach_mutex);
6217 if (un->un_detach_count != 0) {
6218 /* Abort if the instance is detaching */
6219 mutex_exit(&sd_detach_mutex);
6220 return;
6222 mutex_exit(&sd_detach_mutex);
6225 * Grab both mutexes, in the proper order, since we're accessing
6226 * both PM and softstate variables.
6228 mutex_enter(SD_MUTEX(un));
6229 mutex_enter(&un->un_pm_mutex);
6230 if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6231 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6233 * Update the chain types.
6234 * This takes affect on the next new command received.
6236 if (un->un_f_non_devbsize_supported) {
6237 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6238 } else {
6239 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6241 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6243 SD_TRACE(SD_LOG_IO_PM, un,
6244 "sd_pm_idletimeout_handler: idling device\n");
6245 (void) pm_idle_component(SD_DEVINFO(un), 0);
6246 un->un_pm_idle_timeid = NULL;
6247 } else {
6248 un->un_pm_idle_timeid =
6249 timeout(sd_pm_idletimeout_handler, un,
6250 (drv_usectohz((clock_t)300000))); /* 300 ms. */
6252 mutex_exit(&un->un_pm_mutex);
6253 mutex_exit(SD_MUTEX(un));
6258 * Function: sd_pm_timeout_handler
6260 * Description: Callback to tell framework we are idle.
6262 * Context: timeout(9f) thread context.
6265 static void
6266 sd_pm_timeout_handler(void *arg)
6268 struct sd_lun *un = arg;
6270 (void) pm_idle_component(SD_DEVINFO(un), 0);
6271 mutex_enter(&un->un_pm_mutex);
6272 un->un_pm_timeid = NULL;
6273 mutex_exit(&un->un_pm_mutex);
6278 * Function: sdpower
6280 * Description: PM entry point.
6282 * Return Code: DDI_SUCCESS
6283 * DDI_FAILURE
6285 * Context: Kernel thread context
6288 static int
6289 sdpower(dev_info_t *devi, int component, int level)
6291 struct sd_lun *un;
6292 int instance;
6293 int rval = DDI_SUCCESS;
6294 uint_t i, log_page_size, maxcycles, ncycles;
6295 uchar_t *log_page_data;
6296 int log_sense_page;
6297 int medium_present;
6298 time_t intvlp;
6299 struct pm_trans_data sd_pm_tran_data;
6300 uchar_t save_state = SD_STATE_NORMAL;
6301 int sval;
6302 uchar_t state_before_pm;
6303 int got_semaphore_here;
6304 sd_ssc_t *ssc;
6305 int last_power_level = SD_SPINDLE_UNINIT;
6307 instance = ddi_get_instance(devi);
6309 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6310 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6311 return (DDI_FAILURE);
6314 ssc = sd_ssc_init(un);
6316 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6319 * Must synchronize power down with close.
6320 * Attempt to decrement/acquire the open/close semaphore,
6321 * but do NOT wait on it. If it's not greater than zero,
6322 * ie. it can't be decremented without waiting, then
6323 * someone else, either open or close, already has it
6324 * and the try returns 0. Use that knowledge here to determine
6325 * if it's OK to change the device power level.
6326 * Also, only increment it on exit if it was decremented, ie. gotten,
6327 * here.
6329 got_semaphore_here = sema_tryp(&un->un_semoclose);
6331 mutex_enter(SD_MUTEX(un));
6333 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6334 un->un_ncmds_in_driver);
6337 * If un_ncmds_in_driver is non-zero it indicates commands are
6338 * already being processed in the driver, or if the semaphore was
6339 * not gotten here it indicates an open or close is being processed.
6340 * At the same time somebody is requesting to go to a lower power
6341 * that can't perform I/O, which can't happen, therefore we need to
6342 * return failure.
6344 if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6345 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6346 mutex_exit(SD_MUTEX(un));
6348 if (got_semaphore_here != 0) {
6349 sema_v(&un->un_semoclose);
6351 SD_TRACE(SD_LOG_IO_PM, un,
6352 "sdpower: exit, device has queued cmds.\n");
6354 goto sdpower_failed;
6358 * if it is OFFLINE that means the disk is completely dead
6359 * in our case we have to put the disk in on or off by sending commands
6360 * Of course that will fail anyway so return back here.
6362 * Power changes to a device that's OFFLINE or SUSPENDED
6363 * are not allowed.
6365 if ((un->un_state == SD_STATE_OFFLINE) ||
6366 (un->un_state == SD_STATE_SUSPENDED)) {
6367 mutex_exit(SD_MUTEX(un));
6369 if (got_semaphore_here != 0) {
6370 sema_v(&un->un_semoclose);
6372 SD_TRACE(SD_LOG_IO_PM, un,
6373 "sdpower: exit, device is off-line.\n");
6375 goto sdpower_failed;
6379 * Change the device's state to indicate it's power level
6380 * is being changed. Do this to prevent a power off in the
6381 * middle of commands, which is especially bad on devices
6382 * that are really powered off instead of just spun down.
6384 state_before_pm = un->un_state;
6385 un->un_state = SD_STATE_PM_CHANGING;
6387 mutex_exit(SD_MUTEX(un));
6390 * If log sense command is not supported, bypass the
6391 * following checking, otherwise, check the log sense
6392 * information for this device.
6394 if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6395 un->un_f_log_sense_supported) {
6397 * Get the log sense information to understand whether the
6398 * the powercycle counts have gone beyond the threshhold.
6400 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6401 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6403 mutex_enter(SD_MUTEX(un));
6404 log_sense_page = un->un_start_stop_cycle_page;
6405 mutex_exit(SD_MUTEX(un));
6407 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6408 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6410 if (rval != 0) {
6411 if (rval == EIO)
6412 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6413 else
6414 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6417 #ifdef SDDEBUG
6418 if (sd_force_pm_supported) {
6419 /* Force a successful result */
6420 rval = 0;
6422 #endif
6423 if (rval != 0) {
6424 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6425 "Log Sense Failed\n");
6427 kmem_free(log_page_data, log_page_size);
6428 /* Cannot support power management on those drives */
6430 if (got_semaphore_here != 0) {
6431 sema_v(&un->un_semoclose);
6434 * On exit put the state back to it's original value
6435 * and broadcast to anyone waiting for the power
6436 * change completion.
6438 mutex_enter(SD_MUTEX(un));
6439 un->un_state = state_before_pm;
6440 cv_broadcast(&un->un_suspend_cv);
6441 mutex_exit(SD_MUTEX(un));
6442 SD_TRACE(SD_LOG_IO_PM, un,
6443 "sdpower: exit, Log Sense Failed.\n");
6445 goto sdpower_failed;
6449 * From the page data - Convert the essential information to
6450 * pm_trans_data
6452 maxcycles =
6453 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6454 (log_page_data[0x1E] << 8) | log_page_data[0x1F];
6456 ncycles =
6457 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6458 (log_page_data[0x26] << 8) | log_page_data[0x27];
6460 if (un->un_f_pm_log_sense_smart) {
6461 sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6462 sd_pm_tran_data.un.smart_count.consumed = ncycles;
6463 sd_pm_tran_data.un.smart_count.flag = 0;
6464 sd_pm_tran_data.format = DC_SMART_FORMAT;
6465 } else {
6466 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6467 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6468 for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6469 sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6470 log_page_data[8+i];
6472 sd_pm_tran_data.un.scsi_cycles.flag = 0;
6473 sd_pm_tran_data.format = DC_SCSI_FORMAT;
6476 kmem_free(log_page_data, log_page_size);
6479 * Call pm_trans_check routine to get the Ok from
6480 * the global policy
6482 rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6483 #ifdef SDDEBUG
6484 if (sd_force_pm_supported) {
6485 /* Force a successful result */
6486 rval = 1;
6488 #endif
6489 switch (rval) {
6490 case 0:
6492 * Not Ok to Power cycle or error in parameters passed
6493 * Would have given the advised time to consider power
6494 * cycle. Based on the new intvlp parameter we are
6495 * supposed to pretend we are busy so that pm framework
6496 * will never call our power entry point. Because of
6497 * that install a timeout handler and wait for the
6498 * recommended time to elapse so that power management
6499 * can be effective again.
6501 * To effect this behavior, call pm_busy_component to
6502 * indicate to the framework this device is busy.
6503 * By not adjusting un_pm_count the rest of PM in
6504 * the driver will function normally, and independent
6505 * of this but because the framework is told the device
6506 * is busy it won't attempt powering down until it gets
6507 * a matching idle. The timeout handler sends this.
6508 * Note: sd_pm_entry can't be called here to do this
6509 * because sdpower may have been called as a result
6510 * of a call to pm_raise_power from within sd_pm_entry.
6512 * If a timeout handler is already active then
6513 * don't install another.
6515 mutex_enter(&un->un_pm_mutex);
6516 if (un->un_pm_timeid == NULL) {
6517 un->un_pm_timeid =
6518 timeout(sd_pm_timeout_handler,
6519 un, intvlp * drv_usectohz(1000000));
6520 mutex_exit(&un->un_pm_mutex);
6521 (void) pm_busy_component(SD_DEVINFO(un), 0);
6522 } else {
6523 mutex_exit(&un->un_pm_mutex);
6525 if (got_semaphore_here != 0) {
6526 sema_v(&un->un_semoclose);
6529 * On exit put the state back to it's original value
6530 * and broadcast to anyone waiting for the power
6531 * change completion.
6533 mutex_enter(SD_MUTEX(un));
6534 un->un_state = state_before_pm;
6535 cv_broadcast(&un->un_suspend_cv);
6536 mutex_exit(SD_MUTEX(un));
6538 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6539 "trans check Failed, not ok to power cycle.\n");
6541 goto sdpower_failed;
6542 case -1:
6543 if (got_semaphore_here != 0) {
6544 sema_v(&un->un_semoclose);
6547 * On exit put the state back to it's original value
6548 * and broadcast to anyone waiting for the power
6549 * change completion.
6551 mutex_enter(SD_MUTEX(un));
6552 un->un_state = state_before_pm;
6553 cv_broadcast(&un->un_suspend_cv);
6554 mutex_exit(SD_MUTEX(un));
6555 SD_TRACE(SD_LOG_IO_PM, un,
6556 "sdpower: exit, trans check command Failed.\n");
6558 goto sdpower_failed;
6562 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6564 * Save the last state... if the STOP FAILS we need it
6565 * for restoring
6567 mutex_enter(SD_MUTEX(un));
6568 save_state = un->un_last_state;
6569 last_power_level = un->un_power_level;
6571 * There must not be any cmds. getting processed
6572 * in the driver when we get here. Power to the
6573 * device is potentially going off.
6575 ASSERT(un->un_ncmds_in_driver == 0);
6576 mutex_exit(SD_MUTEX(un));
6579 * For now PM suspend the device completely before spindle is
6580 * turned off
6582 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6583 == DDI_FAILURE) {
6584 if (got_semaphore_here != 0) {
6585 sema_v(&un->un_semoclose);
6588 * On exit put the state back to it's original value
6589 * and broadcast to anyone waiting for the power
6590 * change completion.
6592 mutex_enter(SD_MUTEX(un));
6593 un->un_state = state_before_pm;
6594 un->un_power_level = last_power_level;
6595 cv_broadcast(&un->un_suspend_cv);
6596 mutex_exit(SD_MUTEX(un));
6597 SD_TRACE(SD_LOG_IO_PM, un,
6598 "sdpower: exit, PM suspend Failed.\n");
6600 goto sdpower_failed;
6605 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6606 * close, or strategy. Dump no long uses this routine, it uses it's
6607 * own code so it can be done in polled mode.
6610 medium_present = TRUE;
6613 * When powering up, issue a TUR in case the device is at unit
6614 * attention. Don't do retries. Bypass the PM layer, otherwise
6615 * a deadlock on un_pm_busy_cv will occur.
6617 if (SD_PM_IS_IO_CAPABLE(un, level)) {
6618 sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6619 SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6620 if (sval != 0)
6621 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6624 if (un->un_f_power_condition_supported) {
6625 char *pm_condition_name[] = {"STOPPED", "STANDBY",
6626 "IDLE", "ACTIVE"};
6627 SD_TRACE(SD_LOG_IO_PM, un,
6628 "sdpower: sending \'%s\' power condition",
6629 pm_condition_name[level]);
6630 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6631 sd_pl2pc[level], SD_PATH_DIRECT);
6632 } else {
6633 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6634 ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6635 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6636 ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6637 SD_TARGET_STOP), SD_PATH_DIRECT);
6639 if (sval != 0) {
6640 if (sval == EIO)
6641 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6642 else
6643 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6646 /* Command failed, check for media present. */
6647 if ((sval == ENXIO) && un->un_f_has_removable_media) {
6648 medium_present = FALSE;
6652 * The conditions of interest here are:
6653 * if a spindle off with media present fails,
6654 * then restore the state and return an error.
6655 * else if a spindle on fails,
6656 * then return an error (there's no state to restore).
6657 * In all other cases we setup for the new state
6658 * and return success.
6660 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6661 if ((medium_present == TRUE) && (sval != 0)) {
6662 /* The stop command from above failed */
6663 rval = DDI_FAILURE;
6665 * The stop command failed, and we have media
6666 * present. Put the level back by calling the
6667 * sd_pm_resume() and set the state back to
6668 * it's previous value.
6670 (void) sd_pm_state_change(un, last_power_level,
6671 SD_PM_STATE_ROLLBACK);
6672 mutex_enter(SD_MUTEX(un));
6673 un->un_last_state = save_state;
6674 mutex_exit(SD_MUTEX(un));
6675 } else if (un->un_f_monitor_media_state) {
6677 * The stop command from above succeeded.
6678 * Terminate watch thread in case of removable media
6679 * devices going into low power state. This is as per
6680 * the requirements of pm framework, otherwise commands
6681 * will be generated for the device (through watch
6682 * thread), even when the device is in low power state.
6684 mutex_enter(SD_MUTEX(un));
6685 un->un_f_watcht_stopped = FALSE;
6686 if (un->un_swr_token != NULL) {
6687 opaque_t temp_token = un->un_swr_token;
6688 un->un_f_watcht_stopped = TRUE;
6689 un->un_swr_token = NULL;
6690 mutex_exit(SD_MUTEX(un));
6691 (void) scsi_watch_request_terminate(temp_token,
6692 SCSI_WATCH_TERMINATE_ALL_WAIT);
6693 } else {
6694 mutex_exit(SD_MUTEX(un));
6697 } else {
6699 * The level requested is I/O capable.
6700 * Legacy behavior: return success on a failed spinup
6701 * if there is no media in the drive.
6702 * Do this by looking at medium_present here.
6704 if ((sval != 0) && medium_present) {
6705 /* The start command from above failed */
6706 rval = DDI_FAILURE;
6707 } else {
6709 * The start command from above succeeded
6710 * PM resume the devices now that we have
6711 * started the disks
6713 (void) sd_pm_state_change(un, level,
6714 SD_PM_STATE_CHANGE);
6717 * Resume the watch thread since it was suspended
6718 * when the device went into low power mode.
6720 if (un->un_f_monitor_media_state) {
6721 mutex_enter(SD_MUTEX(un));
6722 if (un->un_f_watcht_stopped == TRUE) {
6723 opaque_t temp_token;
6725 un->un_f_watcht_stopped = FALSE;
6726 mutex_exit(SD_MUTEX(un));
6727 temp_token =
6728 sd_watch_request_submit(un);
6729 mutex_enter(SD_MUTEX(un));
6730 un->un_swr_token = temp_token;
6732 mutex_exit(SD_MUTEX(un));
6737 if (got_semaphore_here != 0) {
6738 sema_v(&un->un_semoclose);
6741 * On exit put the state back to it's original value
6742 * and broadcast to anyone waiting for the power
6743 * change completion.
6745 mutex_enter(SD_MUTEX(un));
6746 un->un_state = state_before_pm;
6747 cv_broadcast(&un->un_suspend_cv);
6748 mutex_exit(SD_MUTEX(un));
6750 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
6752 sd_ssc_fini(ssc);
6753 return (rval);
6755 sdpower_failed:
6757 sd_ssc_fini(ssc);
6758 return (DDI_FAILURE);
6764 * Function: sdattach
6766 * Description: Driver's attach(9e) entry point function.
6768 * Arguments: devi - opaque device info handle
6769 * cmd - attach type
6771 * Return Code: DDI_SUCCESS
6772 * DDI_FAILURE
6774 * Context: Kernel thread context
6777 static int
6778 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
6780 switch (cmd) {
6781 case DDI_ATTACH:
6782 return (sd_unit_attach(devi));
6783 case DDI_RESUME:
6784 return (sd_ddi_resume(devi));
6785 default:
6786 break;
6788 return (DDI_FAILURE);
6793 * Function: sddetach
6795 * Description: Driver's detach(9E) entry point function.
6797 * Arguments: devi - opaque device info handle
6798 * cmd - detach type
6800 * Return Code: DDI_SUCCESS
6801 * DDI_FAILURE
6803 * Context: Kernel thread context
6806 static int
6807 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
6809 switch (cmd) {
6810 case DDI_DETACH:
6811 return (sd_unit_detach(devi));
6812 case DDI_SUSPEND:
6813 return (sd_ddi_suspend(devi));
6814 default:
6815 break;
6817 return (DDI_FAILURE);
6822 * Function: sd_sync_with_callback
6824 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
6825 * state while the callback routine is active.
6827 * Arguments: un: softstate structure for the instance
6829 * Context: Kernel thread context
6832 static void
6833 sd_sync_with_callback(struct sd_lun *un)
6835 ASSERT(un != NULL);
6837 mutex_enter(SD_MUTEX(un));
6839 ASSERT(un->un_in_callback >= 0);
6841 while (un->un_in_callback > 0) {
6842 mutex_exit(SD_MUTEX(un));
6843 delay(2);
6844 mutex_enter(SD_MUTEX(un));
6847 mutex_exit(SD_MUTEX(un));
6851 * Function: sd_unit_attach
6853 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
6854 * the soft state structure for the device and performs
6855 * all necessary structure and device initializations.
6857 * Arguments: devi: the system's dev_info_t for the device.
6859 * Return Code: DDI_SUCCESS if attach is successful.
6860 * DDI_FAILURE if any part of the attach fails.
6862 * Context: Called at attach(9e) time for the DDI_ATTACH flag.
6863 * Kernel thread context only. Can sleep.
6866 static int
6867 sd_unit_attach(dev_info_t *devi)
6869 struct scsi_device *devp;
6870 struct sd_lun *un;
6871 char *variantp;
6872 char name_str[48];
6873 int reservation_flag = SD_TARGET_IS_UNRESERVED;
6874 int instance;
6875 int rval;
6876 int wc_enabled;
6877 int wc_changeable;
6878 int tgt;
6879 uint64_t capacity;
6880 uint_t lbasize = 0;
6881 dev_info_t *pdip = ddi_get_parent(devi);
6882 int offbyone = 0;
6883 int geom_label_valid = 0;
6884 sd_ssc_t *ssc;
6885 int status;
6886 struct sd_fm_internal *sfip = NULL;
6887 int max_xfer_size;
6890 * Retrieve the target driver's private data area. This was set
6891 * up by the HBA.
6893 devp = ddi_get_driver_private(devi);
6896 * Retrieve the target ID of the device.
6898 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
6899 SCSI_ADDR_PROP_TARGET, -1);
6902 * Since we have no idea what state things were left in by the last
6903 * user of the device, set up some 'default' settings, ie. turn 'em
6904 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
6905 * Do this before the scsi_probe, which sends an inquiry.
6906 * This is a fix for bug (4430280).
6907 * Of special importance is wide-xfer. The drive could have been left
6908 * in wide transfer mode by the last driver to communicate with it,
6909 * this includes us. If that's the case, and if the following is not
6910 * setup properly or we don't re-negotiate with the drive prior to
6911 * transferring data to/from the drive, it causes bus parity errors,
6912 * data overruns, and unexpected interrupts. This first occurred when
6913 * the fix for bug (4378686) was made.
6915 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
6916 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
6917 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
6920 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
6921 * on a target. Setting it per lun instance actually sets the
6922 * capability of this target, which affects those luns already
6923 * attached on the same target. So during attach, we can only disable
6924 * this capability only when no other lun has been attached on this
6925 * target. By doing this, we assume a target has the same tagged-qing
6926 * capability for every lun. The condition can be removed when HBA
6927 * is changed to support per lun based tagged-qing capability.
6929 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
6930 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
6934 * Use scsi_probe() to issue an INQUIRY command to the device.
6935 * This call will allocate and fill in the scsi_inquiry structure
6936 * and point the sd_inq member of the scsi_device structure to it.
6937 * If the attach succeeds, then this memory will not be de-allocated
6938 * (via scsi_unprobe()) until the instance is detached.
6940 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
6941 goto probe_failed;
6945 * Check the device type as specified in the inquiry data and
6946 * claim it if it is of a type that we support.
6948 switch (devp->sd_inq->inq_dtype) {
6949 case DTYPE_DIRECT:
6950 break;
6951 case DTYPE_RODIRECT:
6952 break;
6953 case DTYPE_OPTICAL:
6954 break;
6955 case DTYPE_NOTPRESENT:
6956 default:
6957 /* Unsupported device type; fail the attach. */
6958 goto probe_failed;
6962 * Allocate the soft state structure for this unit.
6964 * We rely upon this memory being set to all zeroes by
6965 * ddi_soft_state_zalloc(). We assume that any member of the
6966 * soft state structure that is not explicitly initialized by
6967 * this routine will have a value of zero.
6969 instance = ddi_get_instance(devp->sd_dev);
6970 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
6971 goto probe_failed;
6975 * Retrieve a pointer to the newly-allocated soft state.
6977 * This should NEVER fail if the ddi_soft_state_zalloc() call above
6978 * was successful, unless something has gone horribly wrong and the
6979 * ddi's soft state internals are corrupt (in which case it is
6980 * probably better to halt here than just fail the attach....)
6982 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
6983 panic("sd_unit_attach: NULL soft state on instance:0x%x",
6984 instance);
6985 /*NOTREACHED*/
6989 * Link the back ptr of the driver soft state to the scsi_device
6990 * struct for this lun.
6991 * Save a pointer to the softstate in the driver-private area of
6992 * the scsi_device struct.
6993 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
6994 * we first set un->un_sd below.
6996 un->un_sd = devp;
6997 devp->sd_private = (opaque_t)un;
7000 * The following must be after devp is stored in the soft state struct.
7002 #ifdef SDDEBUG
7003 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7004 "%s_unit_attach: un:0x%p instance:%d\n",
7005 ddi_driver_name(devi), un, instance);
7006 #endif
7009 * Set up the device type and node type (for the minor nodes).
7010 * By default we assume that the device can at least support the
7011 * Common Command Set. Call it a CD-ROM if it reports itself
7012 * as a RODIRECT device.
7014 switch (devp->sd_inq->inq_dtype) {
7015 case DTYPE_RODIRECT:
7016 un->un_node_type = DDI_NT_CD_CHAN;
7017 un->un_ctype = CTYPE_CDROM;
7018 break;
7019 case DTYPE_OPTICAL:
7020 un->un_node_type = DDI_NT_BLOCK_CHAN;
7021 un->un_ctype = CTYPE_ROD;
7022 break;
7023 default:
7024 un->un_node_type = DDI_NT_BLOCK_CHAN;
7025 un->un_ctype = CTYPE_CCS;
7026 break;
7030 * Try to read the interconnect type from the HBA.
7032 * Note: This driver is currently compiled as two binaries, a parallel
7033 * scsi version (sd) and a fibre channel version (ssd). All functional
7034 * differences are determined at compile time. In the future a single
7035 * binary will be provided and the interconnect type will be used to
7036 * differentiate between fibre and parallel scsi behaviors. At that time
7037 * it will be necessary for all fibre channel HBAs to support this
7038 * property.
7040 * set un_f_is_fiber to TRUE ( default fiber )
7042 un->un_f_is_fibre = TRUE;
7043 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7044 case INTERCONNECT_SSA:
7045 un->un_interconnect_type = SD_INTERCONNECT_SSA;
7046 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7047 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7048 break;
7049 case INTERCONNECT_PARALLEL:
7050 un->un_f_is_fibre = FALSE;
7051 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7052 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7053 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7054 break;
7055 case INTERCONNECT_SAS:
7056 un->un_f_is_fibre = FALSE;
7057 un->un_interconnect_type = SD_INTERCONNECT_SAS;
7058 un->un_node_type = DDI_NT_BLOCK_SAS;
7059 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7060 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7061 break;
7062 case INTERCONNECT_SATA:
7063 un->un_f_is_fibre = FALSE;
7064 un->un_interconnect_type = SD_INTERCONNECT_SATA;
7065 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7066 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7067 break;
7068 case INTERCONNECT_FIBRE:
7069 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7070 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7071 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7072 break;
7073 case INTERCONNECT_FABRIC:
7074 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7075 un->un_node_type = DDI_NT_BLOCK_FABRIC;
7076 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7077 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7078 break;
7079 default:
7080 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7082 * The HBA does not support the "interconnect-type" property
7083 * (or did not provide a recognized type).
7085 * Note: This will be obsoleted when a single fibre channel
7086 * and parallel scsi driver is delivered. In the meantime the
7087 * interconnect type will be set to the platform default.If that
7088 * type is not parallel SCSI, it means that we should be
7089 * assuming "ssd" semantics. However, here this also means that
7090 * the FC HBA is not supporting the "interconnect-type" property
7091 * like we expect it to, so log this occurrence.
7093 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7094 if (!SD_IS_PARALLEL_SCSI(un)) {
7095 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7096 "sd_unit_attach: un:0x%p Assuming "
7097 "INTERCONNECT_FIBRE\n", un);
7098 } else {
7099 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7100 "sd_unit_attach: un:0x%p Assuming "
7101 "INTERCONNECT_PARALLEL\n", un);
7102 un->un_f_is_fibre = FALSE;
7104 #else
7106 * Note: This source will be implemented when a single fibre
7107 * channel and parallel scsi driver is delivered. The default
7108 * will be to assume that if a device does not support the
7109 * "interconnect-type" property it is a parallel SCSI HBA and
7110 * we will set the interconnect type for parallel scsi.
7112 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7113 un->un_f_is_fibre = FALSE;
7114 #endif
7115 break;
7118 if (un->un_f_is_fibre == TRUE) {
7119 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7120 SCSI_VERSION_3) {
7121 switch (un->un_interconnect_type) {
7122 case SD_INTERCONNECT_FIBRE:
7123 case SD_INTERCONNECT_SSA:
7124 un->un_node_type = DDI_NT_BLOCK_WWN;
7125 break;
7126 default:
7127 break;
7133 * Initialize the Request Sense command for the target
7135 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7136 goto alloc_rqs_failed;
7140 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7141 * with separate binary for sd and ssd.
7143 * x86 has 1 binary, un_retry_count is set base on connection type.
7144 * The hardcoded values will go away when Sparc uses 1 binary
7145 * for sd and ssd. This hardcoded values need to match
7146 * SD_RETRY_COUNT in sddef.h
7147 * The value used is base on interconnect type.
7148 * fibre = 3, parallel = 5
7150 #if defined(__i386) || defined(__amd64)
7151 un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7152 #else
7153 un->un_retry_count = SD_RETRY_COUNT;
7154 #endif
7157 * Set the per disk retry count to the default number of retries
7158 * for disks and CDROMs. This value can be overridden by the
7159 * disk property list or an entry in sd.conf.
7161 un->un_notready_retry_count =
7162 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7163 : DISK_NOT_READY_RETRY_COUNT(un);
7166 * Set the busy retry count to the default value of un_retry_count.
7167 * This can be overridden by entries in sd.conf or the device
7168 * config table.
7170 un->un_busy_retry_count = un->un_retry_count;
7173 * Init the reset threshold for retries. This number determines
7174 * how many retries must be performed before a reset can be issued
7175 * (for certain error conditions). This can be overridden by entries
7176 * in sd.conf or the device config table.
7178 un->un_reset_retry_count = (un->un_retry_count / 2);
7181 * Set the victim_retry_count to the default un_retry_count
7183 un->un_victim_retry_count = (2 * un->un_retry_count);
7186 * Set the reservation release timeout to the default value of
7187 * 5 seconds. This can be overridden by entries in ssd.conf or the
7188 * device config table.
7190 un->un_reserve_release_time = 5;
7193 * Set up the default maximum transfer size. Note that this may
7194 * get updated later in the attach, when setting up default wide
7195 * operations for disks.
7197 #if defined(__i386) || defined(__amd64)
7198 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7199 un->un_partial_dma_supported = 1;
7200 #else
7201 un->un_max_xfer_size = (uint_t)maxphys;
7202 #endif
7205 * Get "allow bus device reset" property (defaults to "enabled" if
7206 * the property was not defined). This is to disable bus resets for
7207 * certain kinds of error recovery. Note: In the future when a run-time
7208 * fibre check is available the soft state flag should default to
7209 * enabled.
7211 if (un->un_f_is_fibre == TRUE) {
7212 un->un_f_allow_bus_device_reset = TRUE;
7213 } else {
7214 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7215 "allow-bus-device-reset", 1) != 0) {
7216 un->un_f_allow_bus_device_reset = TRUE;
7217 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7218 "sd_unit_attach: un:0x%p Bus device reset "
7219 "enabled\n", un);
7220 } else {
7221 un->un_f_allow_bus_device_reset = FALSE;
7222 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7223 "sd_unit_attach: un:0x%p Bus device reset "
7224 "disabled\n", un);
7229 * Check if this is an ATAPI device. ATAPI devices use Group 1
7230 * Read/Write commands and Group 2 Mode Sense/Select commands.
7232 * Note: The "obsolete" way of doing this is to check for the "atapi"
7233 * property. The new "variant" property with a value of "atapi" has been
7234 * introduced so that future 'variants' of standard SCSI behavior (like
7235 * atapi) could be specified by the underlying HBA drivers by supplying
7236 * a new value for the "variant" property, instead of having to define a
7237 * new property.
7239 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7240 un->un_f_cfg_is_atapi = TRUE;
7241 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7242 "sd_unit_attach: un:0x%p Atapi device\n", un);
7244 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7245 &variantp) == DDI_PROP_SUCCESS) {
7246 if (strcmp(variantp, "atapi") == 0) {
7247 un->un_f_cfg_is_atapi = TRUE;
7248 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7249 "sd_unit_attach: un:0x%p Atapi device\n", un);
7251 ddi_prop_free(variantp);
7254 un->un_cmd_timeout = SD_IO_TIME;
7256 un->un_busy_timeout = SD_BSY_TIMEOUT;
7258 /* Info on current states, statuses, etc. (Updated frequently) */
7259 un->un_state = SD_STATE_NORMAL;
7260 un->un_last_state = SD_STATE_NORMAL;
7262 /* Control & status info for command throttling */
7263 un->un_throttle = sd_max_throttle;
7264 un->un_saved_throttle = sd_max_throttle;
7265 un->un_min_throttle = sd_min_throttle;
7267 if (un->un_f_is_fibre == TRUE) {
7268 un->un_f_use_adaptive_throttle = TRUE;
7269 } else {
7270 un->un_f_use_adaptive_throttle = FALSE;
7273 /* Removable media support. */
7274 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7275 un->un_mediastate = DKIO_NONE;
7276 un->un_specified_mediastate = DKIO_NONE;
7278 /* CVs for suspend/resume (PM or DR) */
7279 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
7280 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7282 /* Power management support. */
7283 un->un_power_level = SD_SPINDLE_UNINIT;
7285 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
7286 un->un_f_wcc_inprog = 0;
7289 * The open/close semaphore is used to serialize threads executing
7290 * in the driver's open & close entry point routines for a given
7291 * instance.
7293 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7296 * The conf file entry and softstate variable is a forceful override,
7297 * meaning a non-zero value must be entered to change the default.
7299 un->un_f_disksort_disabled = FALSE;
7300 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7301 un->un_f_enable_rmw = FALSE;
7304 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7305 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7307 un->un_f_mmc_gesn_polling = TRUE;
7310 * physical sector size defaults to DEV_BSIZE currently. We can
7311 * override this value via the driver configuration file so we must
7312 * set it before calling sd_read_unit_properties().
7314 un->un_phy_blocksize = DEV_BSIZE;
7317 * Retrieve the properties from the static driver table or the driver
7318 * configuration file (.conf) for this unit and update the soft state
7319 * for the device as needed for the indicated properties.
7320 * Note: the property configuration needs to occur here as some of the
7321 * following routines may have dependencies on soft state flags set
7322 * as part of the driver property configuration.
7324 sd_read_unit_properties(un);
7325 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7326 "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7329 * Only if a device has "hotpluggable" property, it is
7330 * treated as hotpluggable device. Otherwise, it is
7331 * regarded as non-hotpluggable one.
7333 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7334 -1) != -1) {
7335 un->un_f_is_hotpluggable = TRUE;
7339 * set unit's attributes(flags) according to "hotpluggable" and
7340 * RMB bit in INQUIRY data.
7342 sd_set_unit_attributes(un, devi);
7345 * By default, we mark the capacity, lbasize, and geometry
7346 * as invalid. Only if we successfully read a valid capacity
7347 * will we update the un_blockcount and un_tgt_blocksize with the
7348 * valid values (the geometry will be validated later).
7350 un->un_f_blockcount_is_valid = FALSE;
7351 un->un_f_tgt_blocksize_is_valid = FALSE;
7354 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7355 * otherwise.
7357 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE;
7358 un->un_blockcount = 0;
7361 * Set up the per-instance info needed to determine the correct
7362 * CDBs and other info for issuing commands to the target.
7364 sd_init_cdb_limits(un);
7367 * Set up the IO chains to use, based upon the target type.
7369 if (un->un_f_non_devbsize_supported) {
7370 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7371 } else {
7372 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7374 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
7375 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7376 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7378 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7379 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit,
7380 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7381 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7384 if (ISCD(un)) {
7385 un->un_additional_codes = sd_additional_codes;
7386 } else {
7387 un->un_additional_codes = NULL;
7391 * Create the kstats here so they can be available for attach-time
7392 * routines that send commands to the unit (either polled or via
7393 * sd_send_scsi_cmd).
7395 * Note: This is a critical sequence that needs to be maintained:
7396 * 1) Instantiate the kstats here, before any routines using the
7397 * iopath (i.e. sd_send_scsi_cmd).
7398 * 2) Instantiate and initialize the partition stats
7399 * (sd_set_pstats).
7400 * 3) Initialize the error stats (sd_set_errstats), following
7401 * sd_validate_geometry(),sd_register_devid(),
7402 * and sd_cache_control().
7405 un->un_stats = kstat_create(sd_label, instance,
7406 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7407 if (un->un_stats != NULL) {
7408 un->un_stats->ks_lock = SD_MUTEX(un);
7409 kstat_install(un->un_stats);
7411 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7412 "sd_unit_attach: un:0x%p un_stats created\n", un);
7414 un->un_unmapstats_ks = kstat_create(sd_label, instance, "unmapstats",
7415 "misc", KSTAT_TYPE_NAMED, sizeof (*un->un_unmapstats) /
7416 sizeof (kstat_named_t), 0);
7417 if (un->un_unmapstats_ks) {
7418 un->un_unmapstats = un->un_unmapstats_ks->ks_data;
7420 kstat_named_init(&un->un_unmapstats->us_cmds,
7421 "commands", KSTAT_DATA_UINT64);
7422 kstat_named_init(&un->un_unmapstats->us_errs,
7423 "errors", KSTAT_DATA_UINT64);
7424 kstat_named_init(&un->un_unmapstats->us_extents,
7425 "extents", KSTAT_DATA_UINT64);
7426 kstat_named_init(&un->un_unmapstats->us_bytes,
7427 "bytes", KSTAT_DATA_UINT64);
7429 kstat_install(un->un_unmapstats_ks);
7430 } else {
7431 cmn_err(CE_NOTE, "!Cannot create unmap kstats for disk %d",
7432 instance);
7435 sd_create_errstats(un, instance);
7436 if (un->un_errstats == NULL) {
7437 goto create_errstats_failed;
7439 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7440 "sd_unit_attach: un:0x%p errstats created\n", un);
7443 * The following if/else code was relocated here from below as part
7444 * of the fix for bug (4430280). However with the default setup added
7445 * on entry to this routine, it's no longer absolutely necessary for
7446 * this to be before the call to sd_spin_up_unit.
7448 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7449 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7450 (devp->sd_inq->inq_ansi == 5)) &&
7451 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7454 * If tagged queueing is supported by the target
7455 * and by the host adapter then we will enable it
7457 un->un_tagflags = 0;
7458 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7459 (un->un_f_arq_enabled == TRUE)) {
7460 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7461 1, 1) == 1) {
7462 un->un_tagflags = FLAG_STAG;
7463 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7464 "sd_unit_attach: un:0x%p tag queueing "
7465 "enabled\n", un);
7466 } else if (scsi_ifgetcap(SD_ADDRESS(un),
7467 "untagged-qing", 0) == 1) {
7468 un->un_f_opt_queueing = TRUE;
7469 un->un_saved_throttle = un->un_throttle =
7470 min(un->un_throttle, 3);
7471 } else {
7472 un->un_f_opt_queueing = FALSE;
7473 un->un_saved_throttle = un->un_throttle = 1;
7475 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7476 == 1) && (un->un_f_arq_enabled == TRUE)) {
7477 /* The Host Adapter supports internal queueing. */
7478 un->un_f_opt_queueing = TRUE;
7479 un->un_saved_throttle = un->un_throttle =
7480 min(un->un_throttle, 3);
7481 } else {
7482 un->un_f_opt_queueing = FALSE;
7483 un->un_saved_throttle = un->un_throttle = 1;
7484 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7485 "sd_unit_attach: un:0x%p no tag queueing\n", un);
7489 * Enable large transfers for SATA/SAS drives
7491 if (SD_IS_SERIAL(un)) {
7492 un->un_max_xfer_size =
7493 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7494 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7495 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7496 "sd_unit_attach: un:0x%p max transfer "
7497 "size=0x%x\n", un, un->un_max_xfer_size);
7501 /* Setup or tear down default wide operations for disks */
7504 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7505 * and "ssd_max_xfer_size" to exist simultaneously on the same
7506 * system and be set to different values. In the future this
7507 * code may need to be updated when the ssd module is
7508 * obsoleted and removed from the system. (4299588)
7510 if (SD_IS_PARALLEL_SCSI(un) &&
7511 (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7512 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7513 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7514 1, 1) == 1) {
7515 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7516 "sd_unit_attach: un:0x%p Wide Transfer "
7517 "enabled\n", un);
7521 * If tagged queuing has also been enabled, then
7522 * enable large xfers
7524 if (un->un_saved_throttle == sd_max_throttle) {
7525 un->un_max_xfer_size =
7526 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7527 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7528 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7529 "sd_unit_attach: un:0x%p max transfer "
7530 "size=0x%x\n", un, un->un_max_xfer_size);
7532 } else {
7533 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7534 0, 1) == 1) {
7535 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7536 "sd_unit_attach: un:0x%p "
7537 "Wide Transfer disabled\n", un);
7540 } else {
7541 un->un_tagflags = FLAG_STAG;
7542 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7543 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7547 * If this target supports LUN reset, try to enable it.
7549 if (un->un_f_lun_reset_enabled) {
7550 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7551 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7552 "un:0x%p lun_reset capability set\n", un);
7553 } else {
7554 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7555 "un:0x%p lun-reset capability not set\n", un);
7560 * Adjust the maximum transfer size. This is to fix
7561 * the problem of partial DMA support on SPARC. Some
7562 * HBA driver, like aac, has very small dma_attr_maxxfer
7563 * size, which requires partial DMA support on SPARC.
7564 * In the future the SPARC pci nexus driver may solve
7565 * the problem instead of this fix.
7567 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7568 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7569 /* We need DMA partial even on sparc to ensure sddump() works */
7570 un->un_max_xfer_size = max_xfer_size;
7571 if (un->un_partial_dma_supported == 0)
7572 un->un_partial_dma_supported = 1;
7574 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7575 DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7576 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7577 un->un_max_xfer_size) == 1) {
7578 un->un_buf_breakup_supported = 1;
7579 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7580 "un:0x%p Buf breakup enabled\n", un);
7585 * Set PKT_DMA_PARTIAL flag.
7587 if (un->un_partial_dma_supported == 1) {
7588 un->un_pkt_flags = PKT_DMA_PARTIAL;
7589 } else {
7590 un->un_pkt_flags = 0;
7593 /* Initialize sd_ssc_t for internal uscsi commands */
7594 ssc = sd_ssc_init(un);
7595 scsi_fm_init(devp);
7598 * Allocate memory for SCSI FMA stuffs.
7600 un->un_fm_private =
7601 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7602 sfip = (struct sd_fm_internal *)un->un_fm_private;
7603 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7604 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7605 sfip->fm_ssc.ssc_un = un;
7607 if (ISCD(un) ||
7608 un->un_f_has_removable_media ||
7609 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7611 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7612 * Their log are unchanged.
7614 sfip->fm_log_level = SD_FM_LOG_NSUP;
7615 } else {
7617 * If enter here, it should be non-CDROM and FM-capable
7618 * device, and it will not keep the old scsi_log as before
7619 * in /var/log/messages. However, the property
7620 * "fm-scsi-log" will control whether the FM telemetry will
7621 * be logged in /var/log/messages.
7623 int fm_scsi_log;
7624 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7625 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7627 if (fm_scsi_log)
7628 sfip->fm_log_level = SD_FM_LOG_EREPORT;
7629 else
7630 sfip->fm_log_level = SD_FM_LOG_SILENT;
7634 * At this point in the attach, we have enough info in the
7635 * soft state to be able to issue commands to the target.
7637 * All command paths used below MUST issue their commands as
7638 * SD_PATH_DIRECT. This is important as intermediate layers
7639 * are not all initialized yet (such as PM).
7643 * Send a TEST UNIT READY command to the device. This should clear
7644 * any outstanding UNIT ATTENTION that may be present.
7646 * Note: Don't check for success, just track if there is a reservation,
7647 * this is a throw away command to clear any unit attentions.
7649 * Note: This MUST be the first command issued to the target during
7650 * attach to ensure power on UNIT ATTENTIONS are cleared.
7651 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7652 * with attempts at spinning up a device with no media.
7654 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7655 if (status != 0) {
7656 if (status == EACCES)
7657 reservation_flag = SD_TARGET_IS_RESERVED;
7658 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7662 * If the device is NOT a removable media device, attempt to spin
7663 * it up (using the START_STOP_UNIT command) and read its capacity
7664 * (using the READ CAPACITY command). Note, however, that either
7665 * of these could fail and in some cases we would continue with
7666 * the attach despite the failure (see below).
7668 if (un->un_f_descr_format_supported) {
7670 switch (sd_spin_up_unit(ssc)) {
7671 case 0:
7673 * Spin-up was successful; now try to read the
7674 * capacity. If successful then save the results
7675 * and mark the capacity & lbasize as valid.
7677 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7678 "sd_unit_attach: un:0x%p spin-up successful\n", un);
7680 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7681 &lbasize, SD_PATH_DIRECT);
7683 switch (status) {
7684 case 0: {
7685 if (capacity > DK_MAX_BLOCKS) {
7686 #ifdef _LP64
7687 if ((capacity + 1) >
7688 SD_GROUP1_MAX_ADDRESS) {
7690 * Enable descriptor format
7691 * sense data so that we can
7692 * get 64 bit sense data
7693 * fields.
7695 sd_enable_descr_sense(ssc);
7697 #else
7698 /* 32-bit kernels can't handle this */
7699 scsi_log(SD_DEVINFO(un),
7700 sd_label, CE_WARN,
7701 "disk has %llu blocks, which "
7702 "is too large for a 32-bit "
7703 "kernel", capacity);
7705 #if defined(__i386) || defined(__amd64)
7707 * 1TB disk was treated as (1T - 512)B
7708 * in the past, so that it might have
7709 * valid VTOC and solaris partitions,
7710 * we have to allow it to continue to
7711 * work.
7713 if (capacity -1 > DK_MAX_BLOCKS)
7714 #endif
7715 goto spinup_failed;
7716 #endif
7720 * Here it's not necessary to check the case:
7721 * the capacity of the device is bigger than
7722 * what the max hba cdb can support. Because
7723 * sd_send_scsi_READ_CAPACITY will retrieve
7724 * the capacity by sending USCSI command, which
7725 * is constrained by the max hba cdb. Actually,
7726 * sd_send_scsi_READ_CAPACITY will return
7727 * EINVAL when using bigger cdb than required
7728 * cdb length. Will handle this case in
7729 * "case EINVAL".
7733 * The following relies on
7734 * sd_send_scsi_READ_CAPACITY never
7735 * returning 0 for capacity and/or lbasize.
7737 sd_update_block_info(un, lbasize, capacity);
7739 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7740 "sd_unit_attach: un:0x%p capacity = %ld "
7741 "blocks; lbasize= %ld.\n", un,
7742 un->un_blockcount, un->un_tgt_blocksize);
7744 break;
7746 case EINVAL:
7748 * In the case where the max-cdb-length property
7749 * is smaller than the required CDB length for
7750 * a SCSI device, a target driver can fail to
7751 * attach to that device.
7753 scsi_log(SD_DEVINFO(un),
7754 sd_label, CE_WARN,
7755 "disk capacity is too large "
7756 "for current cdb length");
7757 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7759 goto spinup_failed;
7760 case EACCES:
7762 * Should never get here if the spin-up
7763 * succeeded, but code it in anyway.
7764 * From here, just continue with the attach...
7766 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7767 "sd_unit_attach: un:0x%p "
7768 "sd_send_scsi_READ_CAPACITY "
7769 "returned reservation conflict\n", un);
7770 reservation_flag = SD_TARGET_IS_RESERVED;
7771 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7772 break;
7773 default:
7775 * Likewise, should never get here if the
7776 * spin-up succeeded. Just continue with
7777 * the attach...
7779 if (status == EIO)
7780 sd_ssc_assessment(ssc,
7781 SD_FMT_STATUS_CHECK);
7782 else
7783 sd_ssc_assessment(ssc,
7784 SD_FMT_IGNORE);
7785 break;
7787 break;
7788 case EACCES:
7790 * Device is reserved by another host. In this case
7791 * we could not spin it up or read the capacity, but
7792 * we continue with the attach anyway.
7794 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7795 "sd_unit_attach: un:0x%p spin-up reservation "
7796 "conflict.\n", un);
7797 reservation_flag = SD_TARGET_IS_RESERVED;
7798 break;
7799 default:
7800 /* Fail the attach if the spin-up failed. */
7801 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7802 "sd_unit_attach: un:0x%p spin-up failed.", un);
7803 goto spinup_failed;
7809 * Check to see if this is a MMC drive
7811 if (ISCD(un)) {
7812 sd_set_mmc_caps(ssc);
7816 * Add a zero-length attribute to tell the world we support
7817 * kernel ioctls (for layered drivers)
7819 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7820 DDI_KERNEL_IOCTL, NULL, 0);
7823 * Add a boolean property to tell the world we support
7824 * the B_FAILFAST flag (for layered drivers)
7826 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7827 "ddi-failfast-supported", NULL, 0);
7830 * Initialize power management
7832 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
7833 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
7834 sd_setup_pm(ssc, devi);
7835 if (un->un_f_pm_is_enabled == FALSE) {
7837 * For performance, point to a jump table that does
7838 * not include pm.
7839 * The direct and priority chains don't change with PM.
7841 * Note: this is currently done based on individual device
7842 * capabilities. When an interface for determining system
7843 * power enabled state becomes available, or when additional
7844 * layers are added to the command chain, these values will
7845 * have to be re-evaluated for correctness.
7847 if (un->un_f_non_devbsize_supported) {
7848 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
7849 } else {
7850 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
7852 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
7856 * This property is set to 0 by HA software to avoid retries
7857 * on a reserved disk. (The preferred property name is
7858 * "retry-on-reservation-conflict") (1189689)
7860 * Note: The use of a global here can have unintended consequences. A
7861 * per instance variable is preferable to match the capabilities of
7862 * different underlying hba's (4402600)
7864 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
7865 DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
7866 sd_retry_on_reservation_conflict);
7867 if (sd_retry_on_reservation_conflict != 0) {
7868 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
7869 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
7870 sd_retry_on_reservation_conflict);
7873 /* Set up options for QFULL handling. */
7874 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7875 "qfull-retries", -1)) != -1) {
7876 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
7877 rval, 1);
7879 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7880 "qfull-retry-interval", -1)) != -1) {
7881 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
7882 rval, 1);
7886 * This just prints a message that announces the existence of the
7887 * device. The message is always printed in the system logfile, but
7888 * only appears on the console if the system is booted with the
7889 * -v (verbose) argument.
7891 ddi_report_dev(devi);
7893 un->un_mediastate = DKIO_NONE;
7896 * Check Block Device Characteristics VPD.
7898 sd_check_bdc_vpd(ssc);
7901 * Check whether the drive is in emulation mode.
7903 sd_check_emulation_mode(ssc);
7905 cmlb_alloc_handle(&un->un_cmlbhandle);
7907 #if defined(__i386) || defined(__amd64)
7909 * On x86, compensate for off-by-1 legacy error
7911 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
7912 (lbasize == un->un_sys_blocksize))
7913 offbyone = CMLB_OFF_BY_ONE;
7914 #endif
7916 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
7917 un->un_f_has_removable_media != 0, un->un_f_is_hotpluggable != 0,
7918 un->un_node_type, offbyone, un->un_cmlbhandle,
7919 (void *)SD_PATH_DIRECT) != 0) {
7920 goto cmlb_attach_failed;
7925 * Read and validate the device's geometry (ie, disk label)
7926 * A new unformatted drive will not have a valid geometry, but
7927 * the driver needs to successfully attach to this device so
7928 * the drive can be formatted via ioctls.
7930 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
7931 (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
7933 mutex_enter(SD_MUTEX(un));
7936 * Read and initialize the devid for the unit.
7938 if (un->un_f_devid_supported) {
7939 sd_register_devid(ssc, devi, reservation_flag);
7941 mutex_exit(SD_MUTEX(un));
7943 if (un->un_f_opt_disable_cache == TRUE) {
7945 * Disable both read cache and write cache. This is
7946 * the historic behavior of the keywords in the config file.
7948 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
7949 0) {
7950 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
7951 "sd_unit_attach: un:0x%p Could not disable "
7952 "caching", un);
7953 goto devid_failed;
7958 * Check the value of the WCE bit and if it's allowed to be changed,
7959 * set un_f_write_cache_enabled and un_f_cache_mode_changeable
7960 * accordingly.
7962 (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
7963 sd_get_write_cache_changeable(ssc, &wc_changeable);
7964 mutex_enter(SD_MUTEX(un));
7965 un->un_f_write_cache_enabled = (wc_enabled != 0);
7966 un->un_f_cache_mode_changeable = (wc_changeable != 0);
7967 mutex_exit(SD_MUTEX(un));
7969 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
7970 un->un_tgt_blocksize != DEV_BSIZE) ||
7971 un->un_f_enable_rmw) {
7972 if (!(un->un_wm_cache)) {
7973 (void) snprintf(name_str, sizeof (name_str),
7974 "%s%d_cache",
7975 ddi_driver_name(SD_DEVINFO(un)),
7976 ddi_get_instance(SD_DEVINFO(un)));
7977 un->un_wm_cache = kmem_cache_create(
7978 name_str, sizeof (struct sd_w_map),
7979 8, sd_wm_cache_constructor,
7980 sd_wm_cache_destructor, NULL,
7981 (void *)un, NULL, 0);
7982 if (!(un->un_wm_cache)) {
7983 goto wm_cache_failed;
7989 * Check the value of the NV_SUP bit and set
7990 * un_f_suppress_cache_flush accordingly.
7992 sd_get_nv_sup(ssc);
7995 * Find out what type of reservation this disk supports.
7997 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
7999 switch (status) {
8000 case 0:
8002 * SCSI-3 reservations are supported.
8004 un->un_reservation_type = SD_SCSI3_RESERVATION;
8005 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8006 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8007 break;
8008 case ENOTSUP:
8010 * The PERSISTENT RESERVE IN command would not be recognized by
8011 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8013 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8014 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8015 un->un_reservation_type = SD_SCSI2_RESERVATION;
8017 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8018 break;
8019 default:
8021 * default to SCSI-3 reservations
8023 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8024 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8025 un->un_reservation_type = SD_SCSI3_RESERVATION;
8027 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8028 break;
8032 * Set the pstat and error stat values here, so data obtained during the
8033 * previous attach-time routines is available.
8035 * Note: This is a critical sequence that needs to be maintained:
8036 * 1) Instantiate the kstats before any routines using the iopath
8037 * (i.e. sd_send_scsi_cmd).
8038 * 2) Initialize the error stats (sd_set_errstats) and partition
8039 * stats (sd_set_pstats)here, following
8040 * cmlb_validate_geometry(), sd_register_devid(), and
8041 * sd_cache_control().
8044 if (un->un_f_pkstats_enabled && geom_label_valid) {
8045 sd_set_pstats(un);
8046 SD_TRACE(SD_LOG_IO_PARTITION, un,
8047 "sd_unit_attach: un:0x%p pstats created and set\n", un);
8050 sd_set_errstats(un);
8051 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8052 "sd_unit_attach: un:0x%p errstats set\n", un);
8054 sd_setup_blk_limits(ssc);
8057 * After successfully attaching an instance, we record the information
8058 * of how many luns have been attached on the relative target and
8059 * controller for parallel SCSI. This information is used when sd tries
8060 * to set the tagged queuing capability in HBA.
8062 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8063 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8066 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8067 "sd_unit_attach: un:0x%p exit success\n", un);
8069 /* Uninitialize sd_ssc_t pointer */
8070 sd_ssc_fini(ssc);
8072 return (DDI_SUCCESS);
8075 * An error occurred during the attach; clean up & return failure.
8077 wm_cache_failed:
8078 devid_failed:
8079 ddi_remove_minor_node(devi, NULL);
8081 cmlb_attach_failed:
8083 * Cleanup from the scsi_ifsetcap() calls (437868)
8085 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8086 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8089 * Refer to the comments of setting tagged-qing in the beginning of
8090 * sd_unit_attach. We can only disable tagged queuing when there is
8091 * no lun attached on the target.
8093 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8094 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8097 if (un->un_f_is_fibre == FALSE) {
8098 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8101 spinup_failed:
8103 /* Uninitialize sd_ssc_t pointer */
8104 sd_ssc_fini(ssc);
8106 mutex_enter(SD_MUTEX(un));
8108 /* Deallocate SCSI FMA memory spaces */
8109 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8111 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8112 if (un->un_direct_priority_timeid != NULL) {
8113 timeout_id_t temp_id = un->un_direct_priority_timeid;
8114 un->un_direct_priority_timeid = NULL;
8115 mutex_exit(SD_MUTEX(un));
8116 (void) untimeout(temp_id);
8117 mutex_enter(SD_MUTEX(un));
8120 /* Cancel any pending start/stop timeouts */
8121 if (un->un_startstop_timeid != NULL) {
8122 timeout_id_t temp_id = un->un_startstop_timeid;
8123 un->un_startstop_timeid = NULL;
8124 mutex_exit(SD_MUTEX(un));
8125 (void) untimeout(temp_id);
8126 mutex_enter(SD_MUTEX(un));
8129 /* Cancel any pending reset-throttle timeouts */
8130 if (un->un_reset_throttle_timeid != NULL) {
8131 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8132 un->un_reset_throttle_timeid = NULL;
8133 mutex_exit(SD_MUTEX(un));
8134 (void) untimeout(temp_id);
8135 mutex_enter(SD_MUTEX(un));
8138 /* Cancel rmw warning message timeouts */
8139 if (un->un_rmw_msg_timeid != NULL) {
8140 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8141 un->un_rmw_msg_timeid = NULL;
8142 mutex_exit(SD_MUTEX(un));
8143 (void) untimeout(temp_id);
8144 mutex_enter(SD_MUTEX(un));
8147 /* Cancel any pending retry timeouts */
8148 if (un->un_retry_timeid != NULL) {
8149 timeout_id_t temp_id = un->un_retry_timeid;
8150 un->un_retry_timeid = NULL;
8151 mutex_exit(SD_MUTEX(un));
8152 (void) untimeout(temp_id);
8153 mutex_enter(SD_MUTEX(un));
8156 /* Cancel any pending delayed cv broadcast timeouts */
8157 if (un->un_dcvb_timeid != NULL) {
8158 timeout_id_t temp_id = un->un_dcvb_timeid;
8159 un->un_dcvb_timeid = NULL;
8160 mutex_exit(SD_MUTEX(un));
8161 (void) untimeout(temp_id);
8162 mutex_enter(SD_MUTEX(un));
8165 mutex_exit(SD_MUTEX(un));
8167 /* There should not be any in-progress I/O so ASSERT this check */
8168 ASSERT(un->un_ncmds_in_transport == 0);
8169 ASSERT(un->un_ncmds_in_driver == 0);
8171 /* Do not free the softstate if the callback routine is active */
8172 sd_sync_with_callback(un);
8175 * Partition stats apparently are not used with removables. These would
8176 * not have been created during attach, so no need to clean them up...
8178 if (un->un_errstats != NULL) {
8179 kstat_delete(un->un_errstats);
8180 un->un_errstats = NULL;
8183 create_errstats_failed:
8185 if (un->un_stats != NULL) {
8186 kstat_delete(un->un_stats);
8187 un->un_stats = NULL;
8190 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8191 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8193 ddi_prop_remove_all(devi);
8194 sema_destroy(&un->un_semoclose);
8195 cv_destroy(&un->un_state_cv);
8197 sd_free_rqs(un);
8199 alloc_rqs_failed:
8201 devp->sd_private = NULL;
8202 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
8205 * Note: the man pages are unclear as to whether or not doing a
8206 * ddi_soft_state_free(sd_state, instance) is the right way to
8207 * clean up after the ddi_soft_state_zalloc() if the subsequent
8208 * ddi_get_soft_state() fails. The implication seems to be
8209 * that the get_soft_state cannot fail if the zalloc succeeds.
8211 #ifndef XPV_HVM_DRIVER
8212 ddi_soft_state_free(sd_state, instance);
8213 #endif /* !XPV_HVM_DRIVER */
8215 probe_failed:
8216 scsi_unprobe(devp);
8218 return (DDI_FAILURE);
8223 * Function: sd_unit_detach
8225 * Description: Performs DDI_DETACH processing for sddetach().
8227 * Return Code: DDI_SUCCESS
8228 * DDI_FAILURE
8230 * Context: Kernel thread context
8233 static int
8234 sd_unit_detach(dev_info_t *devi)
8236 struct scsi_device *devp;
8237 struct sd_lun *un;
8238 int i;
8239 int tgt;
8240 dev_t dev;
8241 dev_info_t *pdip = ddi_get_parent(devi);
8242 int instance = ddi_get_instance(devi);
8244 mutex_enter(&sd_detach_mutex);
8247 * Fail the detach for any of the following:
8248 * - Unable to get the sd_lun struct for the instance
8249 * - A layered driver has an outstanding open on the instance
8250 * - Another thread is already detaching this instance
8251 * - Another thread is currently performing an open
8253 devp = ddi_get_driver_private(devi);
8254 if ((devp == NULL) ||
8255 ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8256 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8257 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8258 mutex_exit(&sd_detach_mutex);
8259 return (DDI_FAILURE);
8262 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8265 * Mark this instance as currently in a detach, to inhibit any
8266 * opens from a layered driver.
8268 un->un_detach_count++;
8269 mutex_exit(&sd_detach_mutex);
8271 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8272 SCSI_ADDR_PROP_TARGET, -1);
8274 dev = sd_make_device(SD_DEVINFO(un));
8276 _NOTE(COMPETING_THREADS_NOW);
8278 mutex_enter(SD_MUTEX(un));
8281 * Fail the detach if there are any outstanding layered
8282 * opens on this device.
8284 for (i = 0; i < NDKMAP; i++) {
8285 if (un->un_ocmap.lyropen[i] != 0) {
8286 goto err_notclosed;
8291 * Verify there are NO outstanding commands issued to this device.
8292 * ie, un_ncmds_in_transport == 0.
8293 * It's possible to have outstanding commands through the physio
8294 * code path, even though everything's closed.
8296 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8297 (un->un_direct_priority_timeid != NULL) ||
8298 (un->un_state == SD_STATE_RWAIT)) {
8299 mutex_exit(SD_MUTEX(un));
8300 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8301 "sd_dr_detach: Detach failure due to outstanding cmds\n");
8302 goto err_stillbusy;
8306 * If we have the device reserved, release the reservation.
8308 if ((un->un_resvd_status & SD_RESERVE) &&
8309 !(un->un_resvd_status & SD_LOST_RESERVE)) {
8310 mutex_exit(SD_MUTEX(un));
8312 * Note: sd_reserve_release sends a command to the device
8313 * via the sd_ioctlcmd() path, and can sleep.
8315 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8316 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8317 "sd_dr_detach: Cannot release reservation \n");
8319 } else {
8320 mutex_exit(SD_MUTEX(un));
8324 * Untimeout any reserve recover, throttle reset, restart unit
8325 * and delayed broadcast timeout threads. Protect the timeout pointer
8326 * from getting nulled by their callback functions.
8328 mutex_enter(SD_MUTEX(un));
8329 if (un->un_resvd_timeid != NULL) {
8330 timeout_id_t temp_id = un->un_resvd_timeid;
8331 un->un_resvd_timeid = NULL;
8332 mutex_exit(SD_MUTEX(un));
8333 (void) untimeout(temp_id);
8334 mutex_enter(SD_MUTEX(un));
8337 if (un->un_reset_throttle_timeid != NULL) {
8338 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8339 un->un_reset_throttle_timeid = NULL;
8340 mutex_exit(SD_MUTEX(un));
8341 (void) untimeout(temp_id);
8342 mutex_enter(SD_MUTEX(un));
8345 if (un->un_startstop_timeid != NULL) {
8346 timeout_id_t temp_id = un->un_startstop_timeid;
8347 un->un_startstop_timeid = NULL;
8348 mutex_exit(SD_MUTEX(un));
8349 (void) untimeout(temp_id);
8350 mutex_enter(SD_MUTEX(un));
8353 if (un->un_rmw_msg_timeid != NULL) {
8354 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8355 un->un_rmw_msg_timeid = NULL;
8356 mutex_exit(SD_MUTEX(un));
8357 (void) untimeout(temp_id);
8358 mutex_enter(SD_MUTEX(un));
8361 if (un->un_dcvb_timeid != NULL) {
8362 timeout_id_t temp_id = un->un_dcvb_timeid;
8363 un->un_dcvb_timeid = NULL;
8364 mutex_exit(SD_MUTEX(un));
8365 (void) untimeout(temp_id);
8366 } else {
8367 mutex_exit(SD_MUTEX(un));
8370 /* Remove any pending reservation reclaim requests for this device */
8371 sd_rmv_resv_reclaim_req(dev);
8373 mutex_enter(SD_MUTEX(un));
8375 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8376 if (un->un_direct_priority_timeid != NULL) {
8377 timeout_id_t temp_id = un->un_direct_priority_timeid;
8378 un->un_direct_priority_timeid = NULL;
8379 mutex_exit(SD_MUTEX(un));
8380 (void) untimeout(temp_id);
8381 mutex_enter(SD_MUTEX(un));
8384 /* Cancel any active multi-host disk watch thread requests */
8385 if (un->un_mhd_token != NULL) {
8386 mutex_exit(SD_MUTEX(un));
8387 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8388 if (scsi_watch_request_terminate(un->un_mhd_token,
8389 SCSI_WATCH_TERMINATE_NOWAIT)) {
8390 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8391 "sd_dr_detach: Cannot cancel mhd watch request\n");
8393 * Note: We are returning here after having removed
8394 * some driver timeouts above. This is consistent with
8395 * the legacy implementation but perhaps the watch
8396 * terminate call should be made with the wait flag set.
8398 goto err_stillbusy;
8400 mutex_enter(SD_MUTEX(un));
8401 un->un_mhd_token = NULL;
8404 if (un->un_swr_token != NULL) {
8405 mutex_exit(SD_MUTEX(un));
8406 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8407 if (scsi_watch_request_terminate(un->un_swr_token,
8408 SCSI_WATCH_TERMINATE_NOWAIT)) {
8409 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8410 "sd_dr_detach: Cannot cancel swr watch request\n");
8412 * Note: We are returning here after having removed
8413 * some driver timeouts above. This is consistent with
8414 * the legacy implementation but perhaps the watch
8415 * terminate call should be made with the wait flag set.
8417 goto err_stillbusy;
8419 mutex_enter(SD_MUTEX(un));
8420 un->un_swr_token = NULL;
8423 mutex_exit(SD_MUTEX(un));
8426 * Clear any scsi_reset_notifies. We clear the reset notifies
8427 * if we have not registered one.
8428 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8430 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8431 sd_mhd_reset_notify_cb, (caddr_t)un);
8434 * protect the timeout pointers from getting nulled by
8435 * their callback functions during the cancellation process.
8436 * In such a scenario untimeout can be invoked with a null value.
8438 _NOTE(NO_COMPETING_THREADS_NOW);
8440 mutex_enter(&un->un_pm_mutex);
8441 if (un->un_pm_idle_timeid != NULL) {
8442 timeout_id_t temp_id = un->un_pm_idle_timeid;
8443 un->un_pm_idle_timeid = NULL;
8444 mutex_exit(&un->un_pm_mutex);
8447 * Timeout is active; cancel it.
8448 * Note that it'll never be active on a device
8449 * that does not support PM therefore we don't
8450 * have to check before calling pm_idle_component.
8452 (void) untimeout(temp_id);
8453 (void) pm_idle_component(SD_DEVINFO(un), 0);
8454 mutex_enter(&un->un_pm_mutex);
8458 * Check whether there is already a timeout scheduled for power
8459 * management. If yes then don't lower the power here, that's.
8460 * the timeout handler's job.
8462 if (un->un_pm_timeid != NULL) {
8463 timeout_id_t temp_id = un->un_pm_timeid;
8464 un->un_pm_timeid = NULL;
8465 mutex_exit(&un->un_pm_mutex);
8467 * Timeout is active; cancel it.
8468 * Note that it'll never be active on a device
8469 * that does not support PM therefore we don't
8470 * have to check before calling pm_idle_component.
8472 (void) untimeout(temp_id);
8473 (void) pm_idle_component(SD_DEVINFO(un), 0);
8475 } else {
8476 mutex_exit(&un->un_pm_mutex);
8477 if ((un->un_f_pm_is_enabled == TRUE) &&
8478 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8479 != DDI_SUCCESS)) {
8480 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8481 "sd_dr_detach: Lower power request failed, ignoring.\n");
8483 * Fix for bug: 4297749, item # 13
8484 * The above test now includes a check to see if PM is
8485 * supported by this device before call
8486 * pm_lower_power().
8487 * Note, the following is not dead code. The call to
8488 * pm_lower_power above will generate a call back into
8489 * our sdpower routine which might result in a timeout
8490 * handler getting activated. Therefore the following
8491 * code is valid and necessary.
8493 mutex_enter(&un->un_pm_mutex);
8494 if (un->un_pm_timeid != NULL) {
8495 timeout_id_t temp_id = un->un_pm_timeid;
8496 un->un_pm_timeid = NULL;
8497 mutex_exit(&un->un_pm_mutex);
8498 (void) untimeout(temp_id);
8499 (void) pm_idle_component(SD_DEVINFO(un), 0);
8500 } else {
8501 mutex_exit(&un->un_pm_mutex);
8507 * Cleanup from the scsi_ifsetcap() calls (437868)
8508 * Relocated here from above to be after the call to
8509 * pm_lower_power, which was getting errors.
8511 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8512 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8515 * Currently, tagged queuing is supported per target based by HBA.
8516 * Setting this per lun instance actually sets the capability of this
8517 * target in HBA, which affects those luns already attached on the
8518 * same target. So during detach, we can only disable this capability
8519 * only when this is the only lun left on this target. By doing
8520 * this, we assume a target has the same tagged queuing capability
8521 * for every lun. The condition can be removed when HBA is changed to
8522 * support per lun based tagged queuing capability.
8524 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8525 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8528 if (un->un_f_is_fibre == FALSE) {
8529 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8533 * Remove any event callbacks, fibre only
8535 if (un->un_f_is_fibre == TRUE) {
8536 if ((un->un_insert_event != NULL) &&
8537 (ddi_remove_event_handler(un->un_insert_cb_id) !=
8538 DDI_SUCCESS)) {
8540 * Note: We are returning here after having done
8541 * substantial cleanup above. This is consistent
8542 * with the legacy implementation but this may not
8543 * be the right thing to do.
8545 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8546 "sd_dr_detach: Cannot cancel insert event\n");
8547 goto err_remove_event;
8549 un->un_insert_event = NULL;
8551 if ((un->un_remove_event != NULL) &&
8552 (ddi_remove_event_handler(un->un_remove_cb_id) !=
8553 DDI_SUCCESS)) {
8555 * Note: We are returning here after having done
8556 * substantial cleanup above. This is consistent
8557 * with the legacy implementation but this may not
8558 * be the right thing to do.
8560 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8561 "sd_dr_detach: Cannot cancel remove event\n");
8562 goto err_remove_event;
8564 un->un_remove_event = NULL;
8567 /* Do not free the softstate if the callback routine is active */
8568 sd_sync_with_callback(un);
8570 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8571 cmlb_free_handle(&un->un_cmlbhandle);
8574 * Hold the detach mutex here, to make sure that no other threads ever
8575 * can access a (partially) freed soft state structure.
8577 mutex_enter(&sd_detach_mutex);
8580 * Clean up the soft state struct.
8581 * Cleanup is done in reverse order of allocs/inits.
8582 * At this point there should be no competing threads anymore.
8585 scsi_fm_fini(devp);
8588 * Deallocate memory for SCSI FMA.
8590 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8593 * Unregister and free device id if it was not registered
8594 * by the transport.
8596 if (un->un_f_devid_transport_defined == FALSE)
8597 ddi_devid_unregister(devi);
8600 * free the devid structure if allocated before (by ddi_devid_init()
8601 * or ddi_devid_get()).
8603 if (un->un_devid) {
8604 ddi_devid_free(un->un_devid);
8605 un->un_devid = NULL;
8609 * Destroy wmap cache if it exists.
8611 if (un->un_wm_cache != NULL) {
8612 kmem_cache_destroy(un->un_wm_cache);
8613 un->un_wm_cache = NULL;
8617 * kstat cleanup is done in detach for all device types (4363169).
8618 * We do not want to fail detach if the device kstats are not deleted
8619 * since there is a confusion about the devo_refcnt for the device.
8620 * We just delete the kstats and let detach complete successfully.
8622 if (un->un_stats != NULL) {
8623 kstat_delete(un->un_stats);
8624 un->un_stats = NULL;
8626 if (un->un_unmapstats != NULL) {
8627 kstat_delete(un->un_unmapstats_ks);
8628 un->un_unmapstats_ks = NULL;
8629 un->un_unmapstats = NULL;
8631 if (un->un_errstats != NULL) {
8632 kstat_delete(un->un_errstats);
8633 un->un_errstats = NULL;
8636 /* Remove partition stats */
8637 if (un->un_f_pkstats_enabled) {
8638 for (i = 0; i < NSDMAP; i++) {
8639 if (un->un_pstats[i] != NULL) {
8640 kstat_delete(un->un_pstats[i]);
8641 un->un_pstats[i] = NULL;
8646 /* Remove xbuf registration */
8647 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8648 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8650 /* Remove driver properties */
8651 ddi_prop_remove_all(devi);
8653 mutex_destroy(&un->un_pm_mutex);
8654 cv_destroy(&un->un_pm_busy_cv);
8656 cv_destroy(&un->un_wcc_cv);
8658 /* Open/close semaphore */
8659 sema_destroy(&un->un_semoclose);
8661 /* Removable media condvar. */
8662 cv_destroy(&un->un_state_cv);
8664 /* Suspend/resume condvar. */
8665 cv_destroy(&un->un_suspend_cv);
8666 cv_destroy(&un->un_disk_busy_cv);
8668 sd_free_rqs(un);
8670 /* Free up soft state */
8671 devp->sd_private = NULL;
8673 bzero(un, sizeof (struct sd_lun));
8675 ddi_soft_state_free(sd_state, instance);
8677 mutex_exit(&sd_detach_mutex);
8679 /* This frees up the INQUIRY data associated with the device. */
8680 scsi_unprobe(devp);
8683 * After successfully detaching an instance, we update the information
8684 * of how many luns have been attached in the relative target and
8685 * controller for parallel SCSI. This information is used when sd tries
8686 * to set the tagged queuing capability in HBA.
8687 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8688 * check if the device is parallel SCSI. However, we don't need to
8689 * check here because we've already checked during attach. No device
8690 * that is not parallel SCSI is in the chain.
8692 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8693 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8696 return (DDI_SUCCESS);
8698 err_notclosed:
8699 mutex_exit(SD_MUTEX(un));
8701 err_stillbusy:
8702 _NOTE(NO_COMPETING_THREADS_NOW);
8704 err_remove_event:
8705 mutex_enter(&sd_detach_mutex);
8706 un->un_detach_count--;
8707 mutex_exit(&sd_detach_mutex);
8709 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
8710 return (DDI_FAILURE);
8715 * Function: sd_create_errstats
8717 * Description: This routine instantiates the device error stats.
8719 * Note: During attach the stats are instantiated first so they are
8720 * available for attach-time routines that utilize the driver
8721 * iopath to send commands to the device. The stats are initialized
8722 * separately so data obtained during some attach-time routines is
8723 * available. (4362483)
8725 * Arguments: un - driver soft state (unit) structure
8726 * instance - driver instance
8728 * Context: Kernel thread context
8731 static void
8732 sd_create_errstats(struct sd_lun *un, int instance)
8734 struct sd_errstats *stp;
8735 char kstatmodule_err[KSTAT_STRLEN];
8736 char kstatname[KSTAT_STRLEN];
8737 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
8739 ASSERT(un != NULL);
8741 if (un->un_errstats != NULL) {
8742 return;
8745 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
8746 "%serr", sd_label);
8747 (void) snprintf(kstatname, sizeof (kstatname),
8748 "%s%d,err", sd_label, instance);
8750 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
8751 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
8753 if (un->un_errstats == NULL) {
8754 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8755 "sd_create_errstats: Failed kstat_create\n");
8756 return;
8759 stp = (struct sd_errstats *)un->un_errstats->ks_data;
8760 kstat_named_init(&stp->sd_softerrs, "Soft Errors",
8761 KSTAT_DATA_UINT32);
8762 kstat_named_init(&stp->sd_harderrs, "Hard Errors",
8763 KSTAT_DATA_UINT32);
8764 kstat_named_init(&stp->sd_transerrs, "Transport Errors",
8765 KSTAT_DATA_UINT32);
8766 kstat_named_init(&stp->sd_vid, "Vendor",
8767 KSTAT_DATA_CHAR);
8768 kstat_named_init(&stp->sd_pid, "Product",
8769 KSTAT_DATA_CHAR);
8770 kstat_named_init(&stp->sd_revision, "Revision",
8771 KSTAT_DATA_CHAR);
8772 kstat_named_init(&stp->sd_serial, "Serial No",
8773 KSTAT_DATA_CHAR);
8774 kstat_named_init(&stp->sd_capacity, "Size",
8775 KSTAT_DATA_ULONGLONG);
8776 kstat_named_init(&stp->sd_rq_media_err, "Media Error",
8777 KSTAT_DATA_UINT32);
8778 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready",
8779 KSTAT_DATA_UINT32);
8780 kstat_named_init(&stp->sd_rq_nodev_err, "No Device",
8781 KSTAT_DATA_UINT32);
8782 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable",
8783 KSTAT_DATA_UINT32);
8784 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request",
8785 KSTAT_DATA_UINT32);
8786 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis",
8787 KSTAT_DATA_UINT32);
8789 un->un_errstats->ks_private = un;
8790 un->un_errstats->ks_update = nulldev;
8792 kstat_install(un->un_errstats);
8797 * Function: sd_set_errstats
8799 * Description: This routine sets the value of the vendor id, product id,
8800 * revision, serial number, and capacity device error stats.
8802 * Note: During attach the stats are instantiated first so they are
8803 * available for attach-time routines that utilize the driver
8804 * iopath to send commands to the device. The stats are initialized
8805 * separately so data obtained during some attach-time routines is
8806 * available. (4362483)
8808 * Arguments: un - driver soft state (unit) structure
8810 * Context: Kernel thread context
8813 static void
8814 sd_set_errstats(struct sd_lun *un)
8816 struct sd_errstats *stp;
8817 char *sn;
8819 ASSERT(un != NULL);
8820 ASSERT(un->un_errstats != NULL);
8821 stp = (struct sd_errstats *)un->un_errstats->ks_data;
8822 ASSERT(stp != NULL);
8823 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
8824 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
8825 (void) strncpy(stp->sd_revision.value.c,
8826 un->un_sd->sd_inq->inq_revision, 4);
8829 * All the errstats are persistent across detach/attach,
8830 * so reset all the errstats here in case of the hot
8831 * replacement of disk drives, except for not changed
8832 * Sun qualified drives.
8834 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
8835 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
8836 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
8837 stp->sd_softerrs.value.ui32 = 0;
8838 stp->sd_harderrs.value.ui32 = 0;
8839 stp->sd_transerrs.value.ui32 = 0;
8840 stp->sd_rq_media_err.value.ui32 = 0;
8841 stp->sd_rq_ntrdy_err.value.ui32 = 0;
8842 stp->sd_rq_nodev_err.value.ui32 = 0;
8843 stp->sd_rq_recov_err.value.ui32 = 0;
8844 stp->sd_rq_illrq_err.value.ui32 = 0;
8845 stp->sd_rq_pfa_err.value.ui32 = 0;
8849 * Set the "Serial No" kstat for Sun qualified drives (indicated by
8850 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
8851 * (4376302))
8853 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
8854 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
8855 sizeof (SD_INQUIRY(un)->inq_serial));
8856 } else {
8858 * Set the "Serial No" kstat for non-Sun qualified drives
8860 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
8861 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
8862 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
8863 (void) strlcpy(stp->sd_serial.value.c, sn,
8864 sizeof (stp->sd_serial.value.c));
8865 ddi_prop_free(sn);
8869 if (un->un_f_blockcount_is_valid != TRUE) {
8871 * Set capacity error stat to 0 for no media. This ensures
8872 * a valid capacity is displayed in response to 'iostat -E'
8873 * when no media is present in the device.
8875 stp->sd_capacity.value.ui64 = 0;
8876 } else {
8878 * Multiply un_blockcount by un->un_sys_blocksize to get
8879 * capacity.
8881 * Note: for non-512 blocksize devices "un_blockcount" has been
8882 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
8883 * (un_tgt_blocksize / un->un_sys_blocksize).
8885 stp->sd_capacity.value.ui64 = (uint64_t)
8886 ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
8892 * Function: sd_set_pstats
8894 * Description: This routine instantiates and initializes the partition
8895 * stats for each partition with more than zero blocks.
8896 * (4363169)
8898 * Arguments: un - driver soft state (unit) structure
8900 * Context: Kernel thread context
8903 static void
8904 sd_set_pstats(struct sd_lun *un)
8906 char kstatname[KSTAT_STRLEN];
8907 int instance;
8908 int i;
8909 diskaddr_t nblks = 0;
8910 char *partname = NULL;
8912 ASSERT(un != NULL);
8914 instance = ddi_get_instance(SD_DEVINFO(un));
8916 /* Note:x86: is this a VTOC8/VTOC16 difference? */
8917 for (i = 0; i < NSDMAP; i++) {
8919 if (cmlb_partinfo(un->un_cmlbhandle, i,
8920 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
8921 continue;
8922 mutex_enter(SD_MUTEX(un));
8924 if ((un->un_pstats[i] == NULL) &&
8925 (nblks != 0)) {
8927 (void) snprintf(kstatname, sizeof (kstatname),
8928 "%s%d,%s", sd_label, instance,
8929 partname);
8931 un->un_pstats[i] = kstat_create(sd_label,
8932 instance, kstatname, "partition", KSTAT_TYPE_IO,
8933 1, KSTAT_FLAG_PERSISTENT);
8934 if (un->un_pstats[i] != NULL) {
8935 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
8936 kstat_install(un->un_pstats[i]);
8939 mutex_exit(SD_MUTEX(un));
8945 * Values related to caching mode page depending on whether the unit is ATAPI.
8947 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
8948 CDB_GROUP1 : CDB_GROUP0)
8949 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
8950 MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
8952 * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
8953 * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
8955 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
8956 sizeof (struct mode_cache_scsi3))
8958 static int
8959 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
8960 int *bdlen)
8962 struct sd_lun *un = ssc->ssc_un;
8963 struct mode_caching *mode_caching_page;
8964 size_t buflen = SDC_BUFLEN(un);
8965 int hdrlen = SDC_HDRLEN(un);
8966 int rval;
8969 * Do a test unit ready, otherwise a mode sense may not work if this
8970 * is the first command sent to the device after boot.
8972 if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
8973 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8976 * Allocate memory for the retrieved mode page and its headers. Set
8977 * a pointer to the page itself.
8979 *header = kmem_zalloc(buflen, KM_SLEEP);
8981 /* Get the information from the device */
8982 rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
8983 page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
8984 if (rval != 0) {
8985 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
8986 __func__);
8987 goto mode_sense_failed;
8991 * Determine size of Block Descriptors in order to locate
8992 * the mode page data. ATAPI devices return 0, SCSI devices
8993 * should return MODE_BLK_DESC_LENGTH.
8995 if (un->un_f_cfg_is_atapi == TRUE) {
8996 struct mode_header_grp2 *mhp =
8997 (struct mode_header_grp2 *)(*header);
8998 *bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
8999 } else {
9000 *bdlen = ((struct mode_header *)(*header))->bdesc_length;
9003 if (*bdlen > MODE_BLK_DESC_LENGTH) {
9004 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9005 "%s: Mode Sense returned invalid block descriptor length\n",
9006 __func__);
9007 rval = EIO;
9008 goto mode_sense_failed;
9011 mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9012 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9013 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9014 "%s: Mode Sense caching page code mismatch %d\n",
9015 __func__, mode_caching_page->mode_page.code);
9016 rval = EIO;
9019 mode_sense_failed:
9020 if (rval != 0) {
9021 kmem_free(*header, buflen);
9022 *header = NULL;
9023 *bdlen = 0;
9025 return (rval);
9029 * Function: sd_cache_control()
9031 * Description: This routine is the driver entry point for setting
9032 * read and write caching by modifying the WCE (write cache
9033 * enable) and RCD (read cache disable) bits of mode
9034 * page 8 (MODEPAGE_CACHING).
9036 * Arguments: ssc - ssc contains pointer to driver soft state
9037 * (unit) structure for this target.
9038 * rcd_flag - flag for controlling the read cache
9039 * wce_flag - flag for controlling the write cache
9041 * Return Code: EIO
9042 * code returned by sd_send_scsi_MODE_SENSE and
9043 * sd_send_scsi_MODE_SELECT
9045 * Context: Kernel Thread
9048 static int
9049 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9051 struct sd_lun *un = ssc->ssc_un;
9052 struct mode_caching *mode_caching_page;
9053 uchar_t *header;
9054 size_t buflen = SDC_BUFLEN(un);
9055 int hdrlen = SDC_HDRLEN(un);
9056 int bdlen;
9057 int rval;
9059 rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9060 switch (rval) {
9061 case 0:
9062 /* Check the relevant bits on successful mode sense */
9063 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9064 bdlen);
9065 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9066 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9067 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9068 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9069 size_t sbuflen;
9070 uchar_t save_pg;
9073 * Construct select buffer length based on the
9074 * length of the sense data returned.
9076 sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9077 (int)mode_caching_page->mode_page.length;
9079 /* Set the caching bits as requested */
9080 if (rcd_flag == SD_CACHE_ENABLE)
9081 mode_caching_page->rcd = 0;
9082 else if (rcd_flag == SD_CACHE_DISABLE)
9083 mode_caching_page->rcd = 1;
9085 if (wce_flag == SD_CACHE_ENABLE)
9086 mode_caching_page->wce = 1;
9087 else if (wce_flag == SD_CACHE_DISABLE)
9088 mode_caching_page->wce = 0;
9091 * Save the page if the mode sense says the
9092 * drive supports it.
9094 save_pg = mode_caching_page->mode_page.ps ?
9095 SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9097 /* Clear reserved bits before mode select */
9098 mode_caching_page->mode_page.ps = 0;
9101 * Clear out mode header for mode select.
9102 * The rest of the retrieved page will be reused.
9104 bzero(header, hdrlen);
9106 if (un->un_f_cfg_is_atapi == TRUE) {
9107 struct mode_header_grp2 *mhp =
9108 (struct mode_header_grp2 *)header;
9109 mhp->bdesc_length_hi = bdlen >> 8;
9110 mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9111 } else {
9112 ((struct mode_header *)header)->bdesc_length =
9113 bdlen;
9116 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9118 /* Issue mode select to change the cache settings */
9119 rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9120 header, sbuflen, save_pg, SD_PATH_DIRECT);
9122 kmem_free(header, buflen);
9123 break;
9124 case EIO:
9125 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9126 break;
9127 default:
9128 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9129 break;
9132 return (rval);
9137 * Function: sd_get_write_cache_enabled()
9139 * Description: This routine is the driver entry point for determining if write
9140 * caching is enabled. It examines the WCE (write cache enable)
9141 * bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9142 * bits set to MODEPAGE_CURRENT.
9144 * Arguments: ssc - ssc contains pointer to driver soft state
9145 * (unit) structure for this target.
9146 * is_enabled - pointer to int where write cache enabled state
9147 * is returned (non-zero -> write cache enabled)
9149 * Return Code: EIO
9150 * code returned by sd_send_scsi_MODE_SENSE
9152 * Context: Kernel Thread
9154 * NOTE: If ioctl is added to disable write cache, this sequence should
9155 * be followed so that no locking is required for accesses to
9156 * un->un_f_write_cache_enabled:
9157 * do mode select to clear wce
9158 * do synchronize cache to flush cache
9159 * set un->un_f_write_cache_enabled = FALSE
9161 * Conversely, an ioctl to enable the write cache should be done
9162 * in this order:
9163 * set un->un_f_write_cache_enabled = TRUE
9164 * do mode select to set wce
9167 static int
9168 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9170 struct sd_lun *un = ssc->ssc_un;
9171 struct mode_caching *mode_caching_page;
9172 uchar_t *header;
9173 size_t buflen = SDC_BUFLEN(un);
9174 int hdrlen = SDC_HDRLEN(un);
9175 int bdlen;
9176 int rval;
9178 /* In case of error, flag as enabled */
9179 *is_enabled = TRUE;
9181 rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9182 switch (rval) {
9183 case 0:
9184 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9185 bdlen);
9186 *is_enabled = mode_caching_page->wce;
9187 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9188 kmem_free(header, buflen);
9189 break;
9190 case EIO: {
9192 * Some disks do not support Mode Sense(6), we
9193 * should ignore this kind of error (sense key is
9194 * 0x5 - illegal request).
9196 uint8_t *sensep;
9197 int senlen;
9199 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9200 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9201 ssc->ssc_uscsi_cmd->uscsi_rqresid);
9203 if (senlen > 0 &&
9204 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9205 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9206 } else {
9207 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9209 break;
9211 default:
9212 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9213 break;
9216 return (rval);
9220 * Function: sd_get_write_cache_changeable()
9222 * Description: This routine is the driver entry point for determining if write
9223 * caching is changeable. It examines the WCE (write cache enable)
9224 * bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9225 * bits set to MODEPAGE_CHANGEABLE.
9227 * Arguments: ssc - ssc contains pointer to driver soft state
9228 * (unit) structure for this target.
9229 * is_changeable - pointer to int where write cache changeable
9230 * state is returned (non-zero -> write cache
9231 * changeable)
9233 * Context: Kernel Thread
9236 static void
9237 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9239 struct sd_lun *un = ssc->ssc_un;
9240 struct mode_caching *mode_caching_page;
9241 uchar_t *header;
9242 size_t buflen = SDC_BUFLEN(un);
9243 int hdrlen = SDC_HDRLEN(un);
9244 int bdlen;
9245 int rval;
9247 /* In case of error, flag as enabled */
9248 *is_changeable = TRUE;
9250 rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9251 &bdlen);
9252 switch (rval) {
9253 case 0:
9254 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9255 bdlen);
9256 *is_changeable = mode_caching_page->wce;
9257 kmem_free(header, buflen);
9258 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9259 break;
9260 case EIO:
9261 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9262 break;
9263 default:
9264 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9265 break;
9270 * Function: sd_get_nv_sup()
9272 * Description: This routine is the driver entry point for
9273 * determining whether non-volatile cache is supported. This
9274 * determination process works as follows:
9276 * 1. sd first queries sd.conf on whether
9277 * suppress_cache_flush bit is set for this device.
9279 * 2. if not there, then queries the internal disk table.
9281 * 3. if either sd.conf or internal disk table specifies
9282 * cache flush be suppressed, we don't bother checking
9283 * NV_SUP bit.
9285 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9286 * the optional INQUIRY VPD page 0x86. If the device
9287 * supports VPD page 0x86, sd examines the NV_SUP
9288 * (non-volatile cache support) bit in the INQUIRY VPD page
9289 * 0x86:
9290 * o If NV_SUP bit is set, sd assumes the device has a
9291 * non-volatile cache and set the
9292 * un_f_sync_nv_supported to TRUE.
9293 * o Otherwise cache is not non-volatile,
9294 * un_f_sync_nv_supported is set to FALSE.
9296 * Arguments: un - driver soft state (unit) structure
9298 * Return Code:
9300 * Context: Kernel Thread
9303 static void
9304 sd_get_nv_sup(sd_ssc_t *ssc)
9306 int rval = 0;
9307 uchar_t *inq86 = NULL;
9308 size_t inq86_len = MAX_INQUIRY_SIZE;
9309 size_t inq86_resid = 0;
9310 struct dk_callback *dkc;
9311 struct sd_lun *un;
9313 ASSERT(ssc != NULL);
9314 un = ssc->ssc_un;
9315 ASSERT(un != NULL);
9317 mutex_enter(SD_MUTEX(un));
9320 * Be conservative on the device's support of
9321 * SYNC_NV bit: un_f_sync_nv_supported is
9322 * initialized to be false.
9324 un->un_f_sync_nv_supported = FALSE;
9327 * If either sd.conf or internal disk table
9328 * specifies cache flush be suppressed, then
9329 * we don't bother checking NV_SUP bit.
9331 if (un->un_f_suppress_cache_flush == TRUE) {
9332 mutex_exit(SD_MUTEX(un));
9333 return;
9336 if (sd_check_vpd_page_support(ssc) == 0 &&
9337 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9338 mutex_exit(SD_MUTEX(un));
9339 /* collect page 86 data if available */
9340 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9342 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9343 0x01, 0x86, &inq86_resid);
9345 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9346 SD_TRACE(SD_LOG_COMMON, un,
9347 "sd_get_nv_sup: \
9348 successfully get VPD page: %x \
9349 PAGE LENGTH: %x BYTE 6: %x\n",
9350 inq86[1], inq86[3], inq86[6]);
9352 mutex_enter(SD_MUTEX(un));
9354 * check the value of NV_SUP bit: only if the device
9355 * reports NV_SUP bit to be 1, the
9356 * un_f_sync_nv_supported bit will be set to true.
9358 if (inq86[6] & SD_VPD_NV_SUP) {
9359 un->un_f_sync_nv_supported = TRUE;
9361 mutex_exit(SD_MUTEX(un));
9362 } else if (rval != 0) {
9363 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9366 kmem_free(inq86, inq86_len);
9367 } else {
9368 mutex_exit(SD_MUTEX(un));
9372 * Send a SYNC CACHE command to check whether
9373 * SYNC_NV bit is supported. This command should have
9374 * un_f_sync_nv_supported set to correct value.
9376 mutex_enter(SD_MUTEX(un));
9377 if (un->un_f_sync_nv_supported) {
9378 mutex_exit(SD_MUTEX(un));
9379 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9380 dkc->dkc_flag = FLUSH_VOLATILE;
9381 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9384 * Send a TEST UNIT READY command to the device. This should
9385 * clear any outstanding UNIT ATTENTION that may be present.
9387 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9388 if (rval != 0)
9389 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9391 kmem_free(dkc, sizeof (struct dk_callback));
9392 } else {
9393 mutex_exit(SD_MUTEX(un));
9396 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9397 un_f_suppress_cache_flush is set to %d\n",
9398 un->un_f_suppress_cache_flush);
9402 * Function: sd_make_device
9404 * Description: Utility routine to return the Solaris device number from
9405 * the data in the device's dev_info structure.
9407 * Return Code: The Solaris device number
9409 * Context: Any
9412 static dev_t
9413 sd_make_device(dev_info_t *devi)
9415 return (makedevice(ddi_driver_major(devi),
9416 ddi_get_instance(devi) << SDUNIT_SHIFT));
9421 * Function: sd_pm_entry
9423 * Description: Called at the start of a new command to manage power
9424 * and busy status of a device. This includes determining whether
9425 * the current power state of the device is sufficient for
9426 * performing the command or whether it must be changed.
9427 * The PM framework is notified appropriately.
9428 * Only with a return status of DDI_SUCCESS will the
9429 * component be busy to the framework.
9431 * All callers of sd_pm_entry must check the return status
9432 * and only call sd_pm_exit it it was DDI_SUCCESS. A status
9433 * of DDI_FAILURE indicates the device failed to power up.
9434 * In this case un_pm_count has been adjusted so the result
9435 * on exit is still powered down, ie. count is less than 0.
9436 * Calling sd_pm_exit with this count value hits an ASSERT.
9438 * Return Code: DDI_SUCCESS or DDI_FAILURE
9440 * Context: Kernel thread context.
9443 static int
9444 sd_pm_entry(struct sd_lun *un)
9446 int return_status = DDI_SUCCESS;
9448 ASSERT(!mutex_owned(SD_MUTEX(un)));
9449 ASSERT(!mutex_owned(&un->un_pm_mutex));
9451 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9453 if (un->un_f_pm_is_enabled == FALSE) {
9454 SD_TRACE(SD_LOG_IO_PM, un,
9455 "sd_pm_entry: exiting, PM not enabled\n");
9456 return (return_status);
9460 * Just increment a counter if PM is enabled. On the transition from
9461 * 0 ==> 1, mark the device as busy. The iodone side will decrement
9462 * the count with each IO and mark the device as idle when the count
9463 * hits 0.
9465 * If the count is less than 0 the device is powered down. If a powered
9466 * down device is successfully powered up then the count must be
9467 * incremented to reflect the power up. Note that it'll get incremented
9468 * a second time to become busy.
9470 * Because the following has the potential to change the device state
9471 * and must release the un_pm_mutex to do so, only one thread can be
9472 * allowed through at a time.
9475 mutex_enter(&un->un_pm_mutex);
9476 while (un->un_pm_busy == TRUE) {
9477 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9479 un->un_pm_busy = TRUE;
9481 if (un->un_pm_count < 1) {
9483 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9486 * Indicate we are now busy so the framework won't attempt to
9487 * power down the device. This call will only fail if either
9488 * we passed a bad component number or the device has no
9489 * components. Neither of these should ever happen.
9491 mutex_exit(&un->un_pm_mutex);
9492 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9493 ASSERT(return_status == DDI_SUCCESS);
9495 mutex_enter(&un->un_pm_mutex);
9497 if (un->un_pm_count < 0) {
9498 mutex_exit(&un->un_pm_mutex);
9500 SD_TRACE(SD_LOG_IO_PM, un,
9501 "sd_pm_entry: power up component\n");
9504 * pm_raise_power will cause sdpower to be called
9505 * which brings the device power level to the
9506 * desired state, If successful, un_pm_count and
9507 * un_power_level will be updated appropriately.
9509 return_status = pm_raise_power(SD_DEVINFO(un), 0,
9510 SD_PM_STATE_ACTIVE(un));
9512 mutex_enter(&un->un_pm_mutex);
9514 if (return_status != DDI_SUCCESS) {
9516 * Power up failed.
9517 * Idle the device and adjust the count
9518 * so the result on exit is that we're
9519 * still powered down, ie. count is less than 0.
9521 SD_TRACE(SD_LOG_IO_PM, un,
9522 "sd_pm_entry: power up failed,"
9523 " idle the component\n");
9525 (void) pm_idle_component(SD_DEVINFO(un), 0);
9526 un->un_pm_count--;
9527 } else {
9529 * Device is powered up, verify the
9530 * count is non-negative.
9531 * This is debug only.
9533 ASSERT(un->un_pm_count == 0);
9537 if (return_status == DDI_SUCCESS) {
9539 * For performance, now that the device has been tagged
9540 * as busy, and it's known to be powered up, update the
9541 * chain types to use jump tables that do not include
9542 * pm. This significantly lowers the overhead and
9543 * therefore improves performance.
9546 mutex_exit(&un->un_pm_mutex);
9547 mutex_enter(SD_MUTEX(un));
9548 SD_TRACE(SD_LOG_IO_PM, un,
9549 "sd_pm_entry: changing uscsi_chain_type from %d\n",
9550 un->un_uscsi_chain_type);
9552 if (un->un_f_non_devbsize_supported) {
9553 un->un_buf_chain_type =
9554 SD_CHAIN_INFO_RMMEDIA_NO_PM;
9555 } else {
9556 un->un_buf_chain_type =
9557 SD_CHAIN_INFO_DISK_NO_PM;
9559 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9561 SD_TRACE(SD_LOG_IO_PM, un,
9562 " changed uscsi_chain_type to %d\n",
9563 un->un_uscsi_chain_type);
9564 mutex_exit(SD_MUTEX(un));
9565 mutex_enter(&un->un_pm_mutex);
9567 if (un->un_pm_idle_timeid == NULL) {
9568 /* 300 ms. */
9569 un->un_pm_idle_timeid =
9570 timeout(sd_pm_idletimeout_handler, un,
9571 (drv_usectohz((clock_t)300000)));
9573 * Include an extra call to busy which keeps the
9574 * device busy with-respect-to the PM layer
9575 * until the timer fires, at which time it'll
9576 * get the extra idle call.
9578 (void) pm_busy_component(SD_DEVINFO(un), 0);
9582 un->un_pm_busy = FALSE;
9583 /* Next... */
9584 cv_signal(&un->un_pm_busy_cv);
9586 un->un_pm_count++;
9588 SD_TRACE(SD_LOG_IO_PM, un,
9589 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9591 mutex_exit(&un->un_pm_mutex);
9593 return (return_status);
9598 * Function: sd_pm_exit
9600 * Description: Called at the completion of a command to manage busy
9601 * status for the device. If the device becomes idle the
9602 * PM framework is notified.
9604 * Context: Kernel thread context
9607 static void
9608 sd_pm_exit(struct sd_lun *un)
9610 ASSERT(!mutex_owned(SD_MUTEX(un)));
9611 ASSERT(!mutex_owned(&un->un_pm_mutex));
9613 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
9616 * After attach the following flag is only read, so don't
9617 * take the penalty of acquiring a mutex for it.
9619 if (un->un_f_pm_is_enabled == TRUE) {
9621 mutex_enter(&un->un_pm_mutex);
9622 un->un_pm_count--;
9624 SD_TRACE(SD_LOG_IO_PM, un,
9625 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
9627 ASSERT(un->un_pm_count >= 0);
9628 if (un->un_pm_count == 0) {
9629 mutex_exit(&un->un_pm_mutex);
9631 SD_TRACE(SD_LOG_IO_PM, un,
9632 "sd_pm_exit: idle component\n");
9634 (void) pm_idle_component(SD_DEVINFO(un), 0);
9636 } else {
9637 mutex_exit(&un->un_pm_mutex);
9641 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
9646 * Function: sdopen
9648 * Description: Driver's open(9e) entry point function.
9650 * Arguments: dev_i - pointer to device number
9651 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
9652 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
9653 * cred_p - user credential pointer
9655 * Return Code: EINVAL
9656 * ENXIO
9657 * EIO
9658 * EROFS
9659 * EBUSY
9661 * Context: Kernel thread context
9663 /* ARGSUSED */
9664 static int
9665 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
9667 struct sd_lun *un;
9668 int nodelay;
9669 int part;
9670 uint64_t partmask;
9671 int instance;
9672 dev_t dev;
9673 int rval = EIO;
9674 diskaddr_t nblks = 0;
9675 diskaddr_t label_cap;
9677 /* Validate the open type */
9678 if (otyp >= OTYPCNT) {
9679 return (EINVAL);
9682 dev = *dev_p;
9683 instance = SDUNIT(dev);
9684 mutex_enter(&sd_detach_mutex);
9687 * Fail the open if there is no softstate for the instance, or
9688 * if another thread somewhere is trying to detach the instance.
9690 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
9691 (un->un_detach_count != 0)) {
9692 mutex_exit(&sd_detach_mutex);
9694 * The probe cache only needs to be cleared when open (9e) fails
9695 * with ENXIO (4238046).
9698 * un-conditionally clearing probe cache is ok with
9699 * separate sd/ssd binaries
9700 * x86 platform can be an issue with both parallel
9701 * and fibre in 1 binary
9703 sd_scsi_clear_probe_cache();
9704 return (ENXIO);
9708 * The un_layer_count is to prevent another thread in specfs from
9709 * trying to detach the instance, which can happen when we are
9710 * called from a higher-layer driver instead of thru specfs.
9711 * This will not be needed when DDI provides a layered driver
9712 * interface that allows specfs to know that an instance is in
9713 * use by a layered driver & should not be detached.
9715 * Note: the semantics for layered driver opens are exactly one
9716 * close for every open.
9718 if (otyp == OTYP_LYR) {
9719 un->un_layer_count++;
9723 * Keep a count of the current # of opens in progress. This is because
9724 * some layered drivers try to call us as a regular open. This can
9725 * cause problems that we cannot prevent, however by keeping this count
9726 * we can at least keep our open and detach routines from racing against
9727 * each other under such conditions.
9729 un->un_opens_in_progress++;
9730 mutex_exit(&sd_detach_mutex);
9732 nodelay = (flag & (FNDELAY | FNONBLOCK));
9733 part = SDPART(dev);
9734 partmask = 1 << part;
9737 * We use a semaphore here in order to serialize
9738 * open and close requests on the device.
9740 sema_p(&un->un_semoclose);
9742 mutex_enter(SD_MUTEX(un));
9745 * All device accesses go thru sdstrategy() where we check
9746 * on suspend status but there could be a scsi_poll command,
9747 * which bypasses sdstrategy(), so we need to check pm
9748 * status.
9751 if (!nodelay) {
9752 while ((un->un_state == SD_STATE_SUSPENDED) ||
9753 (un->un_state == SD_STATE_PM_CHANGING)) {
9754 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
9757 mutex_exit(SD_MUTEX(un));
9758 if (sd_pm_entry(un) != DDI_SUCCESS) {
9759 rval = EIO;
9760 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9761 "sdopen: sd_pm_entry failed\n");
9762 goto open_failed_with_pm;
9764 mutex_enter(SD_MUTEX(un));
9767 /* check for previous exclusive open */
9768 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
9769 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
9770 "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
9771 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
9773 if (un->un_exclopen & (partmask)) {
9774 goto excl_open_fail;
9777 if (flag & FEXCL) {
9778 int i;
9779 if (un->un_ocmap.lyropen[part]) {
9780 goto excl_open_fail;
9782 for (i = 0; i < (OTYPCNT - 1); i++) {
9783 if (un->un_ocmap.regopen[i] & (partmask)) {
9784 goto excl_open_fail;
9790 * Check the write permission if this is a removable media device,
9791 * NDELAY has not been set, and writable permission is requested.
9793 * Note: If NDELAY was set and this is write-protected media the WRITE
9794 * attempt will fail with EIO as part of the I/O processing. This is a
9795 * more permissive implementation that allows the open to succeed and
9796 * WRITE attempts to fail when appropriate.
9798 if (un->un_f_chk_wp_open) {
9799 if ((flag & FWRITE) && (!nodelay)) {
9800 mutex_exit(SD_MUTEX(un));
9802 * Defer the check for write permission on writable
9803 * DVD drive till sdstrategy and will not fail open even
9804 * if FWRITE is set as the device can be writable
9805 * depending upon the media and the media can change
9806 * after the call to open().
9808 if (un->un_f_dvdram_writable_device == FALSE) {
9809 if (ISCD(un) || sr_check_wp(dev)) {
9810 rval = EROFS;
9811 mutex_enter(SD_MUTEX(un));
9812 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9813 "write to cd or write protected media\n");
9814 goto open_fail;
9817 mutex_enter(SD_MUTEX(un));
9822 * If opening in NDELAY/NONBLOCK mode, just return.
9823 * Check if disk is ready and has a valid geometry later.
9825 if (!nodelay) {
9826 sd_ssc_t *ssc;
9828 mutex_exit(SD_MUTEX(un));
9829 ssc = sd_ssc_init(un);
9830 rval = sd_ready_and_valid(ssc, part);
9831 sd_ssc_fini(ssc);
9832 mutex_enter(SD_MUTEX(un));
9834 * Fail if device is not ready or if the number of disk
9835 * blocks is zero or negative for non CD devices.
9838 nblks = 0;
9840 if (rval == SD_READY_VALID && (!ISCD(un))) {
9841 /* if cmlb_partinfo fails, nblks remains 0 */
9842 mutex_exit(SD_MUTEX(un));
9843 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
9844 NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
9845 mutex_enter(SD_MUTEX(un));
9848 if ((rval != SD_READY_VALID) ||
9849 (!ISCD(un) && nblks <= 0)) {
9850 rval = un->un_f_has_removable_media ? ENXIO : EIO;
9851 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9852 "device not ready or invalid disk block value\n");
9853 goto open_fail;
9855 #if defined(__i386) || defined(__amd64)
9856 } else {
9857 uchar_t *cp;
9859 * x86 requires special nodelay handling, so that p0 is
9860 * always defined and accessible.
9861 * Invalidate geometry only if device is not already open.
9863 cp = &un->un_ocmap.chkd[0];
9864 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
9865 if (*cp != (uchar_t)0) {
9866 break;
9868 cp++;
9870 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
9871 mutex_exit(SD_MUTEX(un));
9872 cmlb_invalidate(un->un_cmlbhandle,
9873 (void *)SD_PATH_DIRECT);
9874 mutex_enter(SD_MUTEX(un));
9877 #endif
9880 if (otyp == OTYP_LYR) {
9881 un->un_ocmap.lyropen[part]++;
9882 } else {
9883 un->un_ocmap.regopen[otyp] |= partmask;
9886 /* Set up open and exclusive open flags */
9887 if (flag & FEXCL) {
9888 un->un_exclopen |= (partmask);
9892 * If the lun is EFI labeled and lun capacity is greater than the
9893 * capacity contained in the label, log a sys-event to notify the
9894 * interested module.
9895 * To avoid an infinite loop of logging sys-event, we only log the
9896 * event when the lun is not opened in NDELAY mode. The event handler
9897 * should open the lun in NDELAY mode.
9899 if (!nodelay) {
9900 mutex_exit(SD_MUTEX(un));
9901 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
9902 (void*)SD_PATH_DIRECT) == 0) {
9903 mutex_enter(SD_MUTEX(un));
9904 if (un->un_f_blockcount_is_valid &&
9905 un->un_blockcount > label_cap &&
9906 un->un_f_expnevent == B_FALSE) {
9907 un->un_f_expnevent = B_TRUE;
9908 mutex_exit(SD_MUTEX(un));
9909 sd_log_lun_expansion_event(un,
9910 (nodelay ? KM_NOSLEEP : KM_SLEEP));
9911 mutex_enter(SD_MUTEX(un));
9913 } else {
9914 mutex_enter(SD_MUTEX(un));
9918 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9919 "open of part %d type %d\n", part, otyp);
9921 mutex_exit(SD_MUTEX(un));
9922 if (!nodelay) {
9923 sd_pm_exit(un);
9926 sema_v(&un->un_semoclose);
9928 mutex_enter(&sd_detach_mutex);
9929 un->un_opens_in_progress--;
9930 mutex_exit(&sd_detach_mutex);
9932 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
9933 return (DDI_SUCCESS);
9935 excl_open_fail:
9936 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
9937 rval = EBUSY;
9939 open_fail:
9940 mutex_exit(SD_MUTEX(un));
9943 * On a failed open we must exit the pm management.
9945 if (!nodelay) {
9946 sd_pm_exit(un);
9948 open_failed_with_pm:
9949 sema_v(&un->un_semoclose);
9951 mutex_enter(&sd_detach_mutex);
9952 un->un_opens_in_progress--;
9953 if (otyp == OTYP_LYR) {
9954 un->un_layer_count--;
9956 mutex_exit(&sd_detach_mutex);
9958 return (rval);
9963 * Function: sdclose
9965 * Description: Driver's close(9e) entry point function.
9967 * Arguments: dev - device number
9968 * flag - file status flag, informational only
9969 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
9970 * cred_p - user credential pointer
9972 * Return Code: ENXIO
9974 * Context: Kernel thread context
9976 /* ARGSUSED */
9977 static int
9978 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
9980 struct sd_lun *un;
9981 uchar_t *cp;
9982 int part;
9983 int nodelay;
9984 int rval = 0;
9986 /* Validate the open type */
9987 if (otyp >= OTYPCNT) {
9988 return (ENXIO);
9991 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
9992 return (ENXIO);
9995 part = SDPART(dev);
9996 nodelay = flag & (FNDELAY | FNONBLOCK);
9998 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
9999 "sdclose: close of part %d type %d\n", part, otyp);
10002 * We use a semaphore here in order to serialize
10003 * open and close requests on the device.
10005 sema_p(&un->un_semoclose);
10007 mutex_enter(SD_MUTEX(un));
10009 /* Don't proceed if power is being changed. */
10010 while (un->un_state == SD_STATE_PM_CHANGING) {
10011 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10014 if (un->un_exclopen & (1 << part)) {
10015 un->un_exclopen &= ~(1 << part);
10018 /* Update the open partition map */
10019 if (otyp == OTYP_LYR) {
10020 un->un_ocmap.lyropen[part] -= 1;
10021 } else {
10022 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10025 cp = &un->un_ocmap.chkd[0];
10026 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10027 if (*cp != 0) {
10028 break;
10030 cp++;
10033 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10034 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10037 * We avoid persistance upon the last close, and set
10038 * the throttle back to the maximum.
10040 un->un_throttle = un->un_saved_throttle;
10042 if (un->un_state == SD_STATE_OFFLINE) {
10043 if (un->un_f_is_fibre == FALSE) {
10044 scsi_log(SD_DEVINFO(un), sd_label,
10045 CE_WARN, "offline\n");
10047 mutex_exit(SD_MUTEX(un));
10048 cmlb_invalidate(un->un_cmlbhandle,
10049 (void *)SD_PATH_DIRECT);
10050 mutex_enter(SD_MUTEX(un));
10052 } else {
10054 * Flush any outstanding writes in NVRAM cache.
10055 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10056 * cmd, it may not work for non-Pluto devices.
10057 * SYNCHRONIZE CACHE is not required for removables,
10058 * except DVD-RAM drives.
10060 * Also note: because SYNCHRONIZE CACHE is currently
10061 * the only command issued here that requires the
10062 * drive be powered up, only do the power up before
10063 * sending the Sync Cache command. If additional
10064 * commands are added which require a powered up
10065 * drive, the following sequence may have to change.
10067 * And finally, note that parallel SCSI on SPARC
10068 * only issues a Sync Cache to DVD-RAM, a newly
10069 * supported device.
10071 #if defined(__i386) || defined(__amd64)
10072 if ((un->un_f_sync_cache_supported &&
10073 un->un_f_sync_cache_required) ||
10074 un->un_f_dvdram_writable_device == TRUE) {
10075 #else
10076 if (un->un_f_dvdram_writable_device == TRUE) {
10077 #endif
10078 mutex_exit(SD_MUTEX(un));
10079 if (sd_pm_entry(un) == DDI_SUCCESS) {
10080 rval =
10081 sd_send_scsi_SYNCHRONIZE_CACHE(un,
10082 NULL);
10083 /* ignore error if not supported */
10084 if (rval == ENOTSUP) {
10085 rval = 0;
10086 } else if (rval != 0) {
10087 rval = EIO;
10089 sd_pm_exit(un);
10090 } else {
10091 rval = EIO;
10093 mutex_enter(SD_MUTEX(un));
10097 * For devices which supports DOOR_LOCK, send an ALLOW
10098 * MEDIA REMOVAL command, but don't get upset if it
10099 * fails. We need to raise the power of the drive before
10100 * we can call sd_send_scsi_DOORLOCK()
10102 if (un->un_f_doorlock_supported) {
10103 mutex_exit(SD_MUTEX(un));
10104 if (sd_pm_entry(un) == DDI_SUCCESS) {
10105 sd_ssc_t *ssc;
10107 ssc = sd_ssc_init(un);
10108 rval = sd_send_scsi_DOORLOCK(ssc,
10109 SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10110 if (rval != 0)
10111 sd_ssc_assessment(ssc,
10112 SD_FMT_IGNORE);
10113 sd_ssc_fini(ssc);
10115 sd_pm_exit(un);
10116 if (ISCD(un) && (rval != 0) &&
10117 (nodelay != 0)) {
10118 rval = ENXIO;
10120 } else {
10121 rval = EIO;
10123 mutex_enter(SD_MUTEX(un));
10127 * If a device has removable media, invalidate all
10128 * parameters related to media, such as geometry,
10129 * blocksize, and blockcount.
10131 if (un->un_f_has_removable_media) {
10132 sr_ejected(un);
10136 * Destroy the cache (if it exists) which was
10137 * allocated for the write maps since this is
10138 * the last close for this media.
10140 if (un->un_wm_cache) {
10142 * Check if there are pending commands.
10143 * and if there are give a warning and
10144 * do not destroy the cache.
10146 if (un->un_ncmds_in_driver > 0) {
10147 scsi_log(SD_DEVINFO(un),
10148 sd_label, CE_WARN,
10149 "Unable to clean up memory "
10150 "because of pending I/O\n");
10151 } else {
10152 kmem_cache_destroy(
10153 un->un_wm_cache);
10154 un->un_wm_cache = NULL;
10160 mutex_exit(SD_MUTEX(un));
10161 sema_v(&un->un_semoclose);
10163 if (otyp == OTYP_LYR) {
10164 mutex_enter(&sd_detach_mutex);
10166 * The detach routine may run when the layer count
10167 * drops to zero.
10169 un->un_layer_count--;
10170 mutex_exit(&sd_detach_mutex);
10173 return (rval);
10178 * Function: sd_ready_and_valid
10180 * Description: Test if device is ready and has a valid geometry.
10182 * Arguments: ssc - sd_ssc_t will contain un
10183 * un - driver soft state (unit) structure
10185 * Return Code: SD_READY_VALID ready and valid label
10186 * SD_NOT_READY_VALID not ready, no label
10187 * SD_RESERVED_BY_OTHERS reservation conflict
10189 * Context: Never called at interrupt context.
10192 static int
10193 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10195 struct sd_errstats *stp;
10196 uint64_t capacity;
10197 uint_t lbasize;
10198 int rval = SD_READY_VALID;
10199 char name_str[48];
10200 boolean_t is_valid;
10201 struct sd_lun *un;
10202 int status;
10204 ASSERT(ssc != NULL);
10205 un = ssc->ssc_un;
10206 ASSERT(un != NULL);
10207 ASSERT(!mutex_owned(SD_MUTEX(un)));
10209 mutex_enter(SD_MUTEX(un));
10211 * If a device has removable media, we must check if media is
10212 * ready when checking if this device is ready and valid.
10214 if (un->un_f_has_removable_media) {
10215 mutex_exit(SD_MUTEX(un));
10216 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10218 if (status != 0) {
10219 rval = SD_NOT_READY_VALID;
10220 mutex_enter(SD_MUTEX(un));
10222 /* Ignore all failed status for removalbe media */
10223 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10225 goto done;
10228 is_valid = SD_IS_VALID_LABEL(un);
10229 mutex_enter(SD_MUTEX(un));
10230 if (!is_valid ||
10231 (un->un_f_blockcount_is_valid == FALSE) ||
10232 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10234 /* capacity has to be read every open. */
10235 mutex_exit(SD_MUTEX(un));
10236 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10237 &lbasize, SD_PATH_DIRECT);
10239 if (status != 0) {
10240 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10242 cmlb_invalidate(un->un_cmlbhandle,
10243 (void *)SD_PATH_DIRECT);
10244 mutex_enter(SD_MUTEX(un));
10245 rval = SD_NOT_READY_VALID;
10247 goto done;
10248 } else {
10249 mutex_enter(SD_MUTEX(un));
10250 sd_update_block_info(un, lbasize, capacity);
10255 * Check if the media in the device is writable or not.
10257 if (!is_valid && ISCD(un)) {
10258 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10261 } else {
10263 * Do a test unit ready to clear any unit attention from non-cd
10264 * devices.
10266 mutex_exit(SD_MUTEX(un));
10268 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10269 if (status != 0) {
10270 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10273 mutex_enter(SD_MUTEX(un));
10278 * If this is a non 512 block device, allocate space for
10279 * the wmap cache. This is being done here since every time
10280 * a media is changed this routine will be called and the
10281 * block size is a function of media rather than device.
10283 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10284 un->un_f_non_devbsize_supported) &&
10285 un->un_tgt_blocksize != DEV_BSIZE) ||
10286 un->un_f_enable_rmw) {
10287 if (!(un->un_wm_cache)) {
10288 (void) snprintf(name_str, sizeof (name_str),
10289 "%s%d_cache",
10290 ddi_driver_name(SD_DEVINFO(un)),
10291 ddi_get_instance(SD_DEVINFO(un)));
10292 un->un_wm_cache = kmem_cache_create(
10293 name_str, sizeof (struct sd_w_map),
10294 8, sd_wm_cache_constructor,
10295 sd_wm_cache_destructor, NULL,
10296 (void *)un, NULL, 0);
10297 if (!(un->un_wm_cache)) {
10298 rval = ENOMEM;
10299 goto done;
10304 if (un->un_state == SD_STATE_NORMAL) {
10306 * If the target is not yet ready here (defined by a TUR
10307 * failure), invalidate the geometry and print an 'offline'
10308 * message. This is a legacy message, as the state of the
10309 * target is not actually changed to SD_STATE_OFFLINE.
10311 * If the TUR fails for EACCES (Reservation Conflict),
10312 * SD_RESERVED_BY_OTHERS will be returned to indicate
10313 * reservation conflict. If the TUR fails for other
10314 * reasons, SD_NOT_READY_VALID will be returned.
10316 int err;
10318 mutex_exit(SD_MUTEX(un));
10319 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10320 mutex_enter(SD_MUTEX(un));
10322 if (err != 0) {
10323 mutex_exit(SD_MUTEX(un));
10324 cmlb_invalidate(un->un_cmlbhandle,
10325 (void *)SD_PATH_DIRECT);
10326 mutex_enter(SD_MUTEX(un));
10327 if (err == EACCES) {
10328 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10329 "reservation conflict\n");
10330 rval = SD_RESERVED_BY_OTHERS;
10331 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10332 } else {
10333 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10334 "drive offline\n");
10335 rval = SD_NOT_READY_VALID;
10336 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10338 goto done;
10342 if (un->un_f_format_in_progress == FALSE) {
10343 mutex_exit(SD_MUTEX(un));
10345 (void) cmlb_validate(un->un_cmlbhandle, 0,
10346 (void *)SD_PATH_DIRECT);
10347 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10348 NULL, (void *) SD_PATH_DIRECT) != 0) {
10349 rval = SD_NOT_READY_VALID;
10350 mutex_enter(SD_MUTEX(un));
10352 goto done;
10354 if (un->un_f_pkstats_enabled) {
10355 sd_set_pstats(un);
10356 SD_TRACE(SD_LOG_IO_PARTITION, un,
10357 "sd_ready_and_valid: un:0x%p pstats created and "
10358 "set\n", un);
10360 mutex_enter(SD_MUTEX(un));
10364 * If this device supports DOOR_LOCK command, try and send
10365 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10366 * if it fails. For a CD, however, it is an error
10368 if (un->un_f_doorlock_supported) {
10369 mutex_exit(SD_MUTEX(un));
10370 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10371 SD_PATH_DIRECT);
10373 if ((status != 0) && ISCD(un)) {
10374 rval = SD_NOT_READY_VALID;
10375 mutex_enter(SD_MUTEX(un));
10377 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10379 goto done;
10380 } else if (status != 0)
10381 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10382 mutex_enter(SD_MUTEX(un));
10385 /* The state has changed, inform the media watch routines */
10386 un->un_mediastate = DKIO_INSERTED;
10387 cv_broadcast(&un->un_state_cv);
10388 rval = SD_READY_VALID;
10390 done:
10393 * Initialize the capacity kstat value, if no media previously
10394 * (capacity kstat is 0) and a media has been inserted
10395 * (un_blockcount > 0).
10397 if (un->un_errstats != NULL) {
10398 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10399 if ((stp->sd_capacity.value.ui64 == 0) &&
10400 (un->un_f_blockcount_is_valid == TRUE)) {
10401 stp->sd_capacity.value.ui64 =
10402 (uint64_t)((uint64_t)un->un_blockcount *
10403 un->un_sys_blocksize);
10407 mutex_exit(SD_MUTEX(un));
10408 return (rval);
10413 * Function: sdmin
10415 * Description: Routine to limit the size of a data transfer. Used in
10416 * conjunction with physio(9F).
10418 * Arguments: bp - pointer to the indicated buf(9S) struct.
10420 * Context: Kernel thread context.
10423 static void
10424 sdmin(struct buf *bp)
10426 struct sd_lun *un;
10427 int instance;
10429 instance = SDUNIT(bp->b_edev);
10431 un = ddi_get_soft_state(sd_state, instance);
10432 ASSERT(un != NULL);
10435 * We depend on buf breakup to restrict
10436 * IO size if it is enabled.
10438 if (un->un_buf_breakup_supported) {
10439 return;
10442 if (bp->b_bcount > un->un_max_xfer_size) {
10443 bp->b_bcount = un->un_max_xfer_size;
10449 * Function: sdread
10451 * Description: Driver's read(9e) entry point function.
10453 * Arguments: dev - device number
10454 * uio - structure pointer describing where data is to be stored
10455 * in user's space
10456 * cred_p - user credential pointer
10458 * Return Code: ENXIO
10459 * EIO
10460 * EINVAL
10461 * value returned by physio
10463 * Context: Kernel thread context.
10465 /* ARGSUSED */
10466 static int
10467 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10469 struct sd_lun *un = NULL;
10470 int secmask;
10471 int err = 0;
10472 sd_ssc_t *ssc;
10474 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10475 return (ENXIO);
10478 ASSERT(!mutex_owned(SD_MUTEX(un)));
10481 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10482 mutex_enter(SD_MUTEX(un));
10484 * Because the call to sd_ready_and_valid will issue I/O we
10485 * must wait here if either the device is suspended or
10486 * if it's power level is changing.
10488 while ((un->un_state == SD_STATE_SUSPENDED) ||
10489 (un->un_state == SD_STATE_PM_CHANGING)) {
10490 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10492 un->un_ncmds_in_driver++;
10493 mutex_exit(SD_MUTEX(un));
10495 /* Initialize sd_ssc_t for internal uscsi commands */
10496 ssc = sd_ssc_init(un);
10497 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10498 err = EIO;
10499 } else {
10500 err = 0;
10502 sd_ssc_fini(ssc);
10504 mutex_enter(SD_MUTEX(un));
10505 un->un_ncmds_in_driver--;
10506 ASSERT(un->un_ncmds_in_driver >= 0);
10507 mutex_exit(SD_MUTEX(un));
10508 if (err != 0)
10509 return (err);
10513 * Read requests are restricted to multiples of the system block size.
10515 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10516 !un->un_f_enable_rmw)
10517 secmask = un->un_tgt_blocksize - 1;
10518 else
10519 secmask = DEV_BSIZE - 1;
10521 if (uio->uio_loffset & ((offset_t)(secmask))) {
10522 SD_ERROR(SD_LOG_READ_WRITE, un,
10523 "sdread: file offset not modulo %d\n",
10524 secmask + 1);
10525 err = EINVAL;
10526 } else if (uio->uio_iov->iov_len & (secmask)) {
10527 SD_ERROR(SD_LOG_READ_WRITE, un,
10528 "sdread: transfer length not modulo %d\n",
10529 secmask + 1);
10530 err = EINVAL;
10531 } else {
10532 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10535 return (err);
10540 * Function: sdwrite
10542 * Description: Driver's write(9e) entry point function.
10544 * Arguments: dev - device number
10545 * uio - structure pointer describing where data is stored in
10546 * user's space
10547 * cred_p - user credential pointer
10549 * Return Code: ENXIO
10550 * EIO
10551 * EINVAL
10552 * value returned by physio
10554 * Context: Kernel thread context.
10556 /* ARGSUSED */
10557 static int
10558 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10560 struct sd_lun *un = NULL;
10561 int secmask;
10562 int err = 0;
10563 sd_ssc_t *ssc;
10565 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10566 return (ENXIO);
10569 ASSERT(!mutex_owned(SD_MUTEX(un)));
10571 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10572 mutex_enter(SD_MUTEX(un));
10574 * Because the call to sd_ready_and_valid will issue I/O we
10575 * must wait here if either the device is suspended or
10576 * if it's power level is changing.
10578 while ((un->un_state == SD_STATE_SUSPENDED) ||
10579 (un->un_state == SD_STATE_PM_CHANGING)) {
10580 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10582 un->un_ncmds_in_driver++;
10583 mutex_exit(SD_MUTEX(un));
10585 /* Initialize sd_ssc_t for internal uscsi commands */
10586 ssc = sd_ssc_init(un);
10587 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10588 err = EIO;
10589 } else {
10590 err = 0;
10592 sd_ssc_fini(ssc);
10594 mutex_enter(SD_MUTEX(un));
10595 un->un_ncmds_in_driver--;
10596 ASSERT(un->un_ncmds_in_driver >= 0);
10597 mutex_exit(SD_MUTEX(un));
10598 if (err != 0)
10599 return (err);
10603 * Write requests are restricted to multiples of the system block size.
10605 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10606 !un->un_f_enable_rmw)
10607 secmask = un->un_tgt_blocksize - 1;
10608 else
10609 secmask = DEV_BSIZE - 1;
10611 if (uio->uio_loffset & ((offset_t)(secmask))) {
10612 SD_ERROR(SD_LOG_READ_WRITE, un,
10613 "sdwrite: file offset not modulo %d\n",
10614 secmask + 1);
10615 err = EINVAL;
10616 } else if (uio->uio_iov->iov_len & (secmask)) {
10617 SD_ERROR(SD_LOG_READ_WRITE, un,
10618 "sdwrite: transfer length not modulo %d\n",
10619 secmask + 1);
10620 err = EINVAL;
10621 } else {
10622 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10625 return (err);
10630 * Function: sdaread
10632 * Description: Driver's aread(9e) entry point function.
10634 * Arguments: dev - device number
10635 * aio - structure pointer describing where data is to be stored
10636 * cred_p - user credential pointer
10638 * Return Code: ENXIO
10639 * EIO
10640 * EINVAL
10641 * value returned by aphysio
10643 * Context: Kernel thread context.
10645 /* ARGSUSED */
10646 static int
10647 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10649 struct sd_lun *un = NULL;
10650 struct uio *uio = aio->aio_uio;
10651 int secmask;
10652 int err = 0;
10653 sd_ssc_t *ssc;
10655 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10656 return (ENXIO);
10659 ASSERT(!mutex_owned(SD_MUTEX(un)));
10661 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10662 mutex_enter(SD_MUTEX(un));
10664 * Because the call to sd_ready_and_valid will issue I/O we
10665 * must wait here if either the device is suspended or
10666 * if it's power level is changing.
10668 while ((un->un_state == SD_STATE_SUSPENDED) ||
10669 (un->un_state == SD_STATE_PM_CHANGING)) {
10670 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10672 un->un_ncmds_in_driver++;
10673 mutex_exit(SD_MUTEX(un));
10675 /* Initialize sd_ssc_t for internal uscsi commands */
10676 ssc = sd_ssc_init(un);
10677 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10678 err = EIO;
10679 } else {
10680 err = 0;
10682 sd_ssc_fini(ssc);
10684 mutex_enter(SD_MUTEX(un));
10685 un->un_ncmds_in_driver--;
10686 ASSERT(un->un_ncmds_in_driver >= 0);
10687 mutex_exit(SD_MUTEX(un));
10688 if (err != 0)
10689 return (err);
10693 * Read requests are restricted to multiples of the system block size.
10695 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10696 !un->un_f_enable_rmw)
10697 secmask = un->un_tgt_blocksize - 1;
10698 else
10699 secmask = DEV_BSIZE - 1;
10701 if (uio->uio_loffset & ((offset_t)(secmask))) {
10702 SD_ERROR(SD_LOG_READ_WRITE, un,
10703 "sdaread: file offset not modulo %d\n",
10704 secmask + 1);
10705 err = EINVAL;
10706 } else if (uio->uio_iov->iov_len & (secmask)) {
10707 SD_ERROR(SD_LOG_READ_WRITE, un,
10708 "sdaread: transfer length not modulo %d\n",
10709 secmask + 1);
10710 err = EINVAL;
10711 } else {
10712 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
10715 return (err);
10720 * Function: sdawrite
10722 * Description: Driver's awrite(9e) entry point function.
10724 * Arguments: dev - device number
10725 * aio - structure pointer describing where data is stored
10726 * cred_p - user credential pointer
10728 * Return Code: ENXIO
10729 * EIO
10730 * EINVAL
10731 * value returned by aphysio
10733 * Context: Kernel thread context.
10735 /* ARGSUSED */
10736 static int
10737 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10739 struct sd_lun *un = NULL;
10740 struct uio *uio = aio->aio_uio;
10741 int secmask;
10742 int err = 0;
10743 sd_ssc_t *ssc;
10745 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10746 return (ENXIO);
10749 ASSERT(!mutex_owned(SD_MUTEX(un)));
10751 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10752 mutex_enter(SD_MUTEX(un));
10754 * Because the call to sd_ready_and_valid will issue I/O we
10755 * must wait here if either the device is suspended or
10756 * if it's power level is changing.
10758 while ((un->un_state == SD_STATE_SUSPENDED) ||
10759 (un->un_state == SD_STATE_PM_CHANGING)) {
10760 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10762 un->un_ncmds_in_driver++;
10763 mutex_exit(SD_MUTEX(un));
10765 /* Initialize sd_ssc_t for internal uscsi commands */
10766 ssc = sd_ssc_init(un);
10767 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10768 err = EIO;
10769 } else {
10770 err = 0;
10772 sd_ssc_fini(ssc);
10774 mutex_enter(SD_MUTEX(un));
10775 un->un_ncmds_in_driver--;
10776 ASSERT(un->un_ncmds_in_driver >= 0);
10777 mutex_exit(SD_MUTEX(un));
10778 if (err != 0)
10779 return (err);
10783 * Write requests are restricted to multiples of the system block size.
10785 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10786 !un->un_f_enable_rmw)
10787 secmask = un->un_tgt_blocksize - 1;
10788 else
10789 secmask = DEV_BSIZE - 1;
10791 if (uio->uio_loffset & ((offset_t)(secmask))) {
10792 SD_ERROR(SD_LOG_READ_WRITE, un,
10793 "sdawrite: file offset not modulo %d\n",
10794 secmask + 1);
10795 err = EINVAL;
10796 } else if (uio->uio_iov->iov_len & (secmask)) {
10797 SD_ERROR(SD_LOG_READ_WRITE, un,
10798 "sdawrite: transfer length not modulo %d\n",
10799 secmask + 1);
10800 err = EINVAL;
10801 } else {
10802 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
10805 return (err);
10813 * Driver IO processing follows the following sequence:
10815 * sdioctl(9E) sdstrategy(9E) biodone(9F)
10816 * | | ^
10817 * v v |
10818 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+
10819 * | | | |
10820 * v | | |
10821 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone()
10822 * | | ^ ^
10823 * v v | |
10824 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | |
10825 * | | | |
10826 * +---+ | +------------+ +-------+
10827 * | | | |
10828 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
10829 * | v | |
10830 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() |
10831 * | | ^ |
10832 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
10833 * | v | |
10834 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() |
10835 * | | ^ |
10836 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
10837 * | v | |
10838 * | sd_checksum_iostart() sd_checksum_iodone() |
10839 * | | ^ |
10840 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+
10841 * | v | |
10842 * | sd_pm_iostart() sd_pm_iodone() |
10843 * | | ^ |
10844 * | | | |
10845 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+
10846 * | ^
10847 * v |
10848 * sd_core_iostart() |
10849 * | |
10850 * | +------>(*destroypkt)()
10851 * +-> sd_start_cmds() <-+ | |
10852 * | | | v
10853 * | | | scsi_destroy_pkt(9F)
10854 * | | |
10855 * +->(*initpkt)() +- sdintr()
10856 * | | | |
10857 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx()
10858 * | +-> scsi_setup_cdb(9F) |
10859 * | |
10860 * +--> scsi_transport(9F) |
10861 * | |
10862 * +----> SCSA ---->+
10865 * This code is based upon the following presumptions:
10867 * - iostart and iodone functions operate on buf(9S) structures. These
10868 * functions perform the necessary operations on the buf(9S) and pass
10869 * them along to the next function in the chain by using the macros
10870 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
10871 * (for iodone side functions).
10873 * - The iostart side functions may sleep. The iodone side functions
10874 * are called under interrupt context and may NOT sleep. Therefore
10875 * iodone side functions also may not call iostart side functions.
10876 * (NOTE: iostart side functions should NOT sleep for memory, as
10877 * this could result in deadlock.)
10879 * - An iostart side function may call its corresponding iodone side
10880 * function directly (if necessary).
10882 * - In the event of an error, an iostart side function can return a buf(9S)
10883 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
10884 * b_error in the usual way of course).
10886 * - The taskq mechanism may be used by the iodone side functions to dispatch
10887 * requests to the iostart side functions. The iostart side functions in
10888 * this case would be called under the context of a taskq thread, so it's
10889 * OK for them to block/sleep/spin in this case.
10891 * - iostart side functions may allocate "shadow" buf(9S) structs and
10892 * pass them along to the next function in the chain. The corresponding
10893 * iodone side functions must coalesce the "shadow" bufs and return
10894 * the "original" buf to the next higher layer.
10896 * - The b_private field of the buf(9S) struct holds a pointer to
10897 * an sd_xbuf struct, which contains information needed to
10898 * construct the scsi_pkt for the command.
10900 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
10901 * layer must acquire & release the SD_MUTEX(un) as needed.
10906 * Create taskq for all targets in the system. This is created at
10907 * _init(9E) and destroyed at _fini(9E).
10909 * Note: here we set the minalloc to a reasonably high number to ensure that
10910 * we will have an adequate supply of task entries available at interrupt time.
10911 * This is used in conjunction with the TASKQ_PREPOPULATE flag in
10912 * sd_create_taskq(). Since we do not want to sleep for allocations at
10913 * interrupt time, set maxalloc equal to minalloc. That way we will just fail
10914 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
10915 * requests any one instant in time.
10917 #define SD_TASKQ_NUMTHREADS 8
10918 #define SD_TASKQ_MINALLOC 256
10919 #define SD_TASKQ_MAXALLOC 256
10921 static taskq_t *sd_tq = NULL;
10922 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
10924 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
10925 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
10928 * The following task queue is being created for the write part of
10929 * read-modify-write of non-512 block size devices.
10930 * Limit the number of threads to 1 for now. This number has been chosen
10931 * considering the fact that it applies only to dvd ram drives/MO drives
10932 * currently. Performance for which is not main criteria at this stage.
10933 * Note: It needs to be explored if we can use a single taskq in future
10935 #define SD_WMR_TASKQ_NUMTHREADS 1
10936 static taskq_t *sd_wmr_tq = NULL;
10937 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
10940 * Function: sd_taskq_create
10942 * Description: Create taskq thread(s) and preallocate task entries
10944 * Return Code: Returns a pointer to the allocated taskq_t.
10946 * Context: Can sleep. Requires blockable context.
10948 * Notes: - The taskq() facility currently is NOT part of the DDI.
10949 * (definitely NOT recommeded for 3rd-party drivers!) :-)
10950 * - taskq_create() will block for memory, also it will panic
10951 * if it cannot create the requested number of threads.
10952 * - Currently taskq_create() creates threads that cannot be
10953 * swapped.
10954 * - We use TASKQ_PREPOPULATE to ensure we have an adequate
10955 * supply of taskq entries at interrupt time (ie, so that we
10956 * do not have to sleep for memory)
10959 static void
10960 sd_taskq_create(void)
10962 char taskq_name[TASKQ_NAMELEN];
10964 ASSERT(sd_tq == NULL);
10965 ASSERT(sd_wmr_tq == NULL);
10967 (void) snprintf(taskq_name, sizeof (taskq_name),
10968 "%s_drv_taskq", sd_label);
10969 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
10970 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
10971 TASKQ_PREPOPULATE));
10973 (void) snprintf(taskq_name, sizeof (taskq_name),
10974 "%s_rmw_taskq", sd_label);
10975 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
10976 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
10977 TASKQ_PREPOPULATE));
10982 * Function: sd_taskq_delete
10984 * Description: Complementary cleanup routine for sd_taskq_create().
10986 * Context: Kernel thread context.
10989 static void
10990 sd_taskq_delete(void)
10992 ASSERT(sd_tq != NULL);
10993 ASSERT(sd_wmr_tq != NULL);
10994 taskq_destroy(sd_tq);
10995 taskq_destroy(sd_wmr_tq);
10996 sd_tq = NULL;
10997 sd_wmr_tq = NULL;
11002 * Function: sdstrategy
11004 * Description: Driver's strategy (9E) entry point function.
11006 * Arguments: bp - pointer to buf(9S)
11008 * Return Code: Always returns zero
11010 * Context: Kernel thread context.
11013 static int
11014 sdstrategy(struct buf *bp)
11016 struct sd_lun *un;
11018 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11019 if (un == NULL) {
11020 bioerror(bp, EIO);
11021 bp->b_resid = bp->b_bcount;
11022 biodone(bp);
11023 return (0);
11026 /* As was done in the past, fail new cmds. if state is dumping. */
11027 if (un->un_state == SD_STATE_DUMPING) {
11028 bioerror(bp, ENXIO);
11029 bp->b_resid = bp->b_bcount;
11030 biodone(bp);
11031 return (0);
11034 ASSERT(!mutex_owned(SD_MUTEX(un)));
11037 * Commands may sneak in while we released the mutex in
11038 * DDI_SUSPEND, we should block new commands. However, old
11039 * commands that are still in the driver at this point should
11040 * still be allowed to drain.
11042 mutex_enter(SD_MUTEX(un));
11044 * Must wait here if either the device is suspended or
11045 * if it's power level is changing.
11047 while ((un->un_state == SD_STATE_SUSPENDED) ||
11048 (un->un_state == SD_STATE_PM_CHANGING)) {
11049 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11052 un->un_ncmds_in_driver++;
11055 * atapi: Since we are running the CD for now in PIO mode we need to
11056 * call bp_mapin here to avoid bp_mapin called interrupt context under
11057 * the HBA's init_pkt routine.
11059 if (un->un_f_cfg_is_atapi == TRUE) {
11060 mutex_exit(SD_MUTEX(un));
11061 bp_mapin(bp);
11062 mutex_enter(SD_MUTEX(un));
11064 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11065 un->un_ncmds_in_driver);
11067 if (bp->b_flags & B_WRITE)
11068 un->un_f_sync_cache_required = TRUE;
11070 mutex_exit(SD_MUTEX(un));
11073 * This will (eventually) allocate the sd_xbuf area and
11074 * call sd_xbuf_strategy(). We just want to return the
11075 * result of ddi_xbuf_qstrategy so that we have an opt-
11076 * imized tail call which saves us a stack frame.
11078 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11083 * Function: sd_xbuf_strategy
11085 * Description: Function for initiating IO operations via the
11086 * ddi_xbuf_qstrategy() mechanism.
11088 * Context: Kernel thread context.
11091 static void
11092 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11094 struct sd_lun *un = arg;
11096 ASSERT(bp != NULL);
11097 ASSERT(xp != NULL);
11098 ASSERT(un != NULL);
11099 ASSERT(!mutex_owned(SD_MUTEX(un)));
11102 * Initialize the fields in the xbuf and save a pointer to the
11103 * xbuf in bp->b_private.
11105 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11107 /* Send the buf down the iostart chain */
11108 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11113 * Function: sd_xbuf_init
11115 * Description: Prepare the given sd_xbuf struct for use.
11117 * Arguments: un - ptr to softstate
11118 * bp - ptr to associated buf(9S)
11119 * xp - ptr to associated sd_xbuf
11120 * chain_type - IO chain type to use:
11121 * SD_CHAIN_NULL
11122 * SD_CHAIN_BUFIO
11123 * SD_CHAIN_USCSI
11124 * SD_CHAIN_DIRECT
11125 * SD_CHAIN_DIRECT_PRIORITY
11126 * pktinfop - ptr to private data struct for scsi_pkt(9S)
11127 * initialization; may be NULL if none.
11129 * Context: Kernel thread context
11132 static void
11133 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11134 uchar_t chain_type, void *pktinfop)
11136 int index;
11138 ASSERT(un != NULL);
11139 ASSERT(bp != NULL);
11140 ASSERT(xp != NULL);
11142 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11143 bp, chain_type);
11145 xp->xb_un = un;
11146 xp->xb_pktp = NULL;
11147 xp->xb_pktinfo = pktinfop;
11148 xp->xb_private = bp->b_private;
11149 xp->xb_blkno = (daddr_t)bp->b_blkno;
11152 * Set up the iostart and iodone chain indexes in the xbuf, based
11153 * upon the specified chain type to use.
11155 switch (chain_type) {
11156 case SD_CHAIN_NULL:
11158 * Fall thru to just use the values for the buf type, even
11159 * tho for the NULL chain these values will never be used.
11161 /* FALLTHRU */
11162 case SD_CHAIN_BUFIO:
11163 index = un->un_buf_chain_type;
11164 if ((!un->un_f_has_removable_media) &&
11165 (un->un_tgt_blocksize != 0) &&
11166 (un->un_tgt_blocksize != DEV_BSIZE ||
11167 un->un_f_enable_rmw)) {
11168 int secmask = 0, blknomask = 0;
11169 if (un->un_f_enable_rmw) {
11170 blknomask =
11171 (un->un_phy_blocksize / DEV_BSIZE) - 1;
11172 secmask = un->un_phy_blocksize - 1;
11173 } else {
11174 blknomask =
11175 (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11176 secmask = un->un_tgt_blocksize - 1;
11179 if ((bp->b_lblkno & (blknomask)) ||
11180 (bp->b_bcount & (secmask))) {
11181 if ((un->un_f_rmw_type !=
11182 SD_RMW_TYPE_RETURN_ERROR) ||
11183 un->un_f_enable_rmw) {
11184 if (un->un_f_pm_is_enabled == FALSE)
11185 index =
11186 SD_CHAIN_INFO_MSS_DSK_NO_PM;
11187 else
11188 index =
11189 SD_CHAIN_INFO_MSS_DISK;
11193 break;
11194 case SD_CHAIN_USCSI:
11195 index = un->un_uscsi_chain_type;
11196 break;
11197 case SD_CHAIN_DIRECT:
11198 index = un->un_direct_chain_type;
11199 break;
11200 case SD_CHAIN_DIRECT_PRIORITY:
11201 index = un->un_priority_chain_type;
11202 break;
11203 default:
11204 /* We're really broken if we ever get here... */
11205 panic("sd_xbuf_init: illegal chain type!");
11206 /*NOTREACHED*/
11209 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11210 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11213 * It might be a bit easier to simply bzero the entire xbuf above,
11214 * but it turns out that since we init a fair number of members anyway,
11215 * we save a fair number cycles by doing explicit assignment of zero.
11217 xp->xb_pkt_flags = 0;
11218 xp->xb_dma_resid = 0;
11219 xp->xb_retry_count = 0;
11220 xp->xb_victim_retry_count = 0;
11221 xp->xb_ua_retry_count = 0;
11222 xp->xb_nr_retry_count = 0;
11223 xp->xb_sense_bp = NULL;
11224 xp->xb_sense_status = 0;
11225 xp->xb_sense_state = 0;
11226 xp->xb_sense_resid = 0;
11227 xp->xb_ena = 0;
11229 bp->b_private = xp;
11230 bp->b_flags &= ~(B_DONE | B_ERROR);
11231 bp->b_resid = 0;
11232 bp->av_forw = NULL;
11233 bp->av_back = NULL;
11234 bioerror(bp, 0);
11236 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11241 * Function: sd_uscsi_strategy
11243 * Description: Wrapper for calling into the USCSI chain via physio(9F)
11245 * Arguments: bp - buf struct ptr
11247 * Return Code: Always returns 0
11249 * Context: Kernel thread context
11252 static int
11253 sd_uscsi_strategy(struct buf *bp)
11255 struct sd_lun *un;
11256 struct sd_uscsi_info *uip;
11257 struct sd_xbuf *xp;
11258 uchar_t chain_type;
11259 uchar_t cmd;
11261 ASSERT(bp != NULL);
11263 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11264 if (un == NULL) {
11265 bioerror(bp, EIO);
11266 bp->b_resid = bp->b_bcount;
11267 biodone(bp);
11268 return (0);
11271 ASSERT(!mutex_owned(SD_MUTEX(un)));
11273 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11276 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11278 ASSERT(bp->b_private != NULL);
11279 uip = (struct sd_uscsi_info *)bp->b_private;
11280 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11282 mutex_enter(SD_MUTEX(un));
11284 * atapi: Since we are running the CD for now in PIO mode we need to
11285 * call bp_mapin here to avoid bp_mapin called interrupt context under
11286 * the HBA's init_pkt routine.
11288 if (un->un_f_cfg_is_atapi == TRUE) {
11289 mutex_exit(SD_MUTEX(un));
11290 bp_mapin(bp);
11291 mutex_enter(SD_MUTEX(un));
11293 un->un_ncmds_in_driver++;
11294 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11295 un->un_ncmds_in_driver);
11297 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11298 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11299 un->un_f_sync_cache_required = TRUE;
11301 mutex_exit(SD_MUTEX(un));
11303 switch (uip->ui_flags) {
11304 case SD_PATH_DIRECT:
11305 chain_type = SD_CHAIN_DIRECT;
11306 break;
11307 case SD_PATH_DIRECT_PRIORITY:
11308 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11309 break;
11310 default:
11311 chain_type = SD_CHAIN_USCSI;
11312 break;
11316 * We may allocate extra buf for external USCSI commands. If the
11317 * application asks for bigger than 20-byte sense data via USCSI,
11318 * SCSA layer will allocate 252 bytes sense buf for that command.
11320 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11321 SENSE_LENGTH) {
11322 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11323 MAX_SENSE_LENGTH, KM_SLEEP);
11324 } else {
11325 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11328 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11330 /* Use the index obtained within xbuf_init */
11331 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11333 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11335 return (0);
11339 * Function: sd_send_scsi_cmd
11341 * Description: Runs a USCSI command for user (when called thru sdioctl),
11342 * or for the driver
11344 * Arguments: dev - the dev_t for the device
11345 * incmd - ptr to a valid uscsi_cmd struct
11346 * flag - bit flag, indicating open settings, 32/64 bit type
11347 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11348 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11349 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11350 * to use the USCSI "direct" chain and bypass the normal
11351 * command waitq.
11353 * Return Code: 0 - successful completion of the given command
11354 * EIO - scsi_uscsi_handle_command() failed
11355 * ENXIO - soft state not found for specified dev
11356 * EINVAL
11357 * EFAULT - copyin/copyout error
11358 * return code of scsi_uscsi_handle_command():
11359 * EIO
11360 * ENXIO
11361 * EACCES
11363 * Context: Waits for command to complete. Can sleep.
11366 static int
11367 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11368 enum uio_seg dataspace, int path_flag)
11370 struct sd_lun *un;
11371 sd_ssc_t *ssc;
11372 int rval;
11374 un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11375 if (un == NULL) {
11376 return (ENXIO);
11380 * Using sd_ssc_send to handle uscsi cmd
11382 ssc = sd_ssc_init(un);
11383 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11384 sd_ssc_fini(ssc);
11386 return (rval);
11390 * Function: sd_ssc_init
11392 * Description: Uscsi end-user call this function to initialize necessary
11393 * fields, such as uscsi_cmd and sd_uscsi_info struct.
11395 * The return value of sd_send_scsi_cmd will be treated as a
11396 * fault in various conditions. Even it is not Zero, some
11397 * callers may ignore the return value. That is to say, we can
11398 * not make an accurate assessment in sdintr, since if a
11399 * command is failed in sdintr it does not mean the caller of
11400 * sd_send_scsi_cmd will treat it as a real failure.
11402 * To avoid printing too many error logs for a failed uscsi
11403 * packet that the caller may not treat it as a failure, the
11404 * sd will keep silent for handling all uscsi commands.
11406 * During detach->attach and attach-open, for some types of
11407 * problems, the driver should be providing information about
11408 * the problem encountered. Device use USCSI_SILENT, which
11409 * suppresses all driver information. The result is that no
11410 * information about the problem is available. Being
11411 * completely silent during this time is inappropriate. The
11412 * driver needs a more selective filter than USCSI_SILENT, so
11413 * that information related to faults is provided.
11415 * To make the accurate accessment, the caller of
11416 * sd_send_scsi_USCSI_CMD should take the ownership and
11417 * get necessary information to print error messages.
11419 * If we want to print necessary info of uscsi command, we need to
11420 * keep the uscsi_cmd and sd_uscsi_info till we can make the
11421 * assessment. We use sd_ssc_init to alloc necessary
11422 * structs for sending an uscsi command and we are also
11423 * responsible for free the memory by calling
11424 * sd_ssc_fini.
11426 * The calling secquences will look like:
11427 * sd_ssc_init->
11429 * ...
11431 * sd_send_scsi_USCSI_CMD->
11432 * sd_ssc_send-> - - - sdintr
11433 * ...
11435 * if we think the return value should be treated as a
11436 * failure, we make the accessment here and print out
11437 * necessary by retrieving uscsi_cmd and sd_uscsi_info'
11439 * ...
11441 * sd_ssc_fini
11444 * Arguments: un - pointer to driver soft state (unit) structure for this
11445 * target.
11447 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11448 * uscsi_cmd and sd_uscsi_info.
11449 * NULL - if can not alloc memory for sd_ssc_t struct
11451 * Context: Kernel Thread.
11453 static sd_ssc_t *
11454 sd_ssc_init(struct sd_lun *un)
11456 sd_ssc_t *ssc;
11457 struct uscsi_cmd *ucmdp;
11458 struct sd_uscsi_info *uip;
11460 ASSERT(un != NULL);
11461 ASSERT(!mutex_owned(SD_MUTEX(un)));
11464 * Allocate sd_ssc_t structure
11466 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11469 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11471 ucmdp = scsi_uscsi_alloc();
11474 * Allocate sd_uscsi_info structure
11476 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11478 ssc->ssc_uscsi_cmd = ucmdp;
11479 ssc->ssc_uscsi_info = uip;
11480 ssc->ssc_un = un;
11482 return (ssc);
11486 * Function: sd_ssc_fini
11488 * Description: To free sd_ssc_t and it's hanging off
11490 * Arguments: ssc - struct pointer of sd_ssc_t.
11492 static void
11493 sd_ssc_fini(sd_ssc_t *ssc)
11495 scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11497 if (ssc->ssc_uscsi_info != NULL) {
11498 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11499 ssc->ssc_uscsi_info = NULL;
11502 kmem_free(ssc, sizeof (sd_ssc_t));
11503 ssc = NULL;
11507 * Function: sd_ssc_send
11509 * Description: Runs a USCSI command for user when called through sdioctl,
11510 * or for the driver.
11512 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11513 * sd_uscsi_info in.
11514 * incmd - ptr to a valid uscsi_cmd struct
11515 * flag - bit flag, indicating open settings, 32/64 bit type
11516 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11517 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11518 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11519 * to use the USCSI "direct" chain and bypass the normal
11520 * command waitq.
11522 * Return Code: 0 - successful completion of the given command
11523 * EIO - scsi_uscsi_handle_command() failed
11524 * ENXIO - soft state not found for specified dev
11525 * ECANCELED - command cancelled due to low power
11526 * EINVAL
11527 * EFAULT - copyin/copyout error
11528 * return code of scsi_uscsi_handle_command():
11529 * EIO
11530 * ENXIO
11531 * EACCES
11533 * Context: Kernel Thread;
11534 * Waits for command to complete. Can sleep.
11536 static int
11537 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11538 enum uio_seg dataspace, int path_flag)
11540 struct sd_uscsi_info *uip;
11541 struct uscsi_cmd *uscmd;
11542 struct sd_lun *un;
11543 dev_t dev;
11545 int format = 0;
11546 int rval;
11548 ASSERT(ssc != NULL);
11549 un = ssc->ssc_un;
11550 ASSERT(un != NULL);
11551 uscmd = ssc->ssc_uscsi_cmd;
11552 ASSERT(uscmd != NULL);
11553 ASSERT(!mutex_owned(SD_MUTEX(un)));
11554 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11556 * If enter here, it indicates that the previous uscsi
11557 * command has not been processed by sd_ssc_assessment.
11558 * This is violating our rules of FMA telemetry processing.
11559 * We should print out this message and the last undisposed
11560 * uscsi command.
11562 if (uscmd->uscsi_cdb != NULL) {
11563 SD_INFO(SD_LOG_SDTEST, un,
11564 "sd_ssc_send is missing the alternative "
11565 "sd_ssc_assessment when running command 0x%x.\n",
11566 uscmd->uscsi_cdb[0]);
11569 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11570 * the initial status.
11572 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11576 * We need to make sure sd_ssc_send will have sd_ssc_assessment
11577 * followed to avoid missing FMA telemetries.
11579 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11582 * if USCSI_PMFAILFAST is set and un is in low power, fail the
11583 * command immediately.
11585 mutex_enter(SD_MUTEX(un));
11586 mutex_enter(&un->un_pm_mutex);
11587 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11588 SD_DEVICE_IS_IN_LOW_POWER(un)) {
11589 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11590 "un:0x%p is in low power\n", un);
11591 mutex_exit(&un->un_pm_mutex);
11592 mutex_exit(SD_MUTEX(un));
11593 return (ECANCELED);
11595 mutex_exit(&un->un_pm_mutex);
11596 mutex_exit(SD_MUTEX(un));
11598 #ifdef SDDEBUG
11599 switch (dataspace) {
11600 case UIO_USERSPACE:
11601 SD_TRACE(SD_LOG_IO, un,
11602 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
11603 break;
11604 case UIO_SYSSPACE:
11605 SD_TRACE(SD_LOG_IO, un,
11606 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
11607 break;
11608 default:
11609 SD_TRACE(SD_LOG_IO, un,
11610 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
11611 break;
11613 #endif
11615 rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
11616 SD_ADDRESS(un), &uscmd);
11617 if (rval != 0) {
11618 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
11619 "scsi_uscsi_alloc_and_copyin failed\n", un);
11620 return (rval);
11623 if ((uscmd->uscsi_cdb != NULL) &&
11624 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
11625 mutex_enter(SD_MUTEX(un));
11626 un->un_f_format_in_progress = TRUE;
11627 mutex_exit(SD_MUTEX(un));
11628 format = 1;
11632 * Allocate an sd_uscsi_info struct and fill it with the info
11633 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
11634 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
11635 * since we allocate the buf here in this function, we do not
11636 * need to preserve the prior contents of b_private.
11637 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11639 uip = ssc->ssc_uscsi_info;
11640 uip->ui_flags = path_flag;
11641 uip->ui_cmdp = uscmd;
11644 * Commands sent with priority are intended for error recovery
11645 * situations, and do not have retries performed.
11647 if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11648 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11650 uscmd->uscsi_flags &= ~USCSI_NOINTR;
11652 dev = SD_GET_DEV(un);
11653 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
11654 sd_uscsi_strategy, NULL, uip);
11657 * mark ssc_flags right after handle_cmd to make sure
11658 * the uscsi has been sent
11660 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
11662 #ifdef SDDEBUG
11663 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11664 "uscsi_status: 0x%02x uscsi_resid:0x%x\n",
11665 uscmd->uscsi_status, uscmd->uscsi_resid);
11666 if (uscmd->uscsi_bufaddr != NULL) {
11667 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11668 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
11669 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11670 if (dataspace == UIO_SYSSPACE) {
11671 SD_DUMP_MEMORY(un, SD_LOG_IO,
11672 "data", (uchar_t *)uscmd->uscsi_bufaddr,
11673 uscmd->uscsi_buflen, SD_LOG_HEX);
11676 #endif
11678 if (format == 1) {
11679 mutex_enter(SD_MUTEX(un));
11680 un->un_f_format_in_progress = FALSE;
11681 mutex_exit(SD_MUTEX(un));
11684 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
11686 return (rval);
11690 * Function: sd_ssc_print
11692 * Description: Print information available to the console.
11694 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11695 * sd_uscsi_info in.
11696 * sd_severity - log level.
11697 * Context: Kernel thread or interrupt context.
11699 static void
11700 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
11702 struct uscsi_cmd *ucmdp;
11703 struct scsi_device *devp;
11704 dev_info_t *devinfo;
11705 uchar_t *sensep;
11706 int senlen;
11707 union scsi_cdb *cdbp;
11708 uchar_t com;
11709 extern struct scsi_key_strings scsi_cmds[];
11711 ASSERT(ssc != NULL);
11712 ASSERT(ssc->ssc_un != NULL);
11714 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
11715 return;
11716 ucmdp = ssc->ssc_uscsi_cmd;
11717 devp = SD_SCSI_DEVP(ssc->ssc_un);
11718 devinfo = SD_DEVINFO(ssc->ssc_un);
11719 ASSERT(ucmdp != NULL);
11720 ASSERT(devp != NULL);
11721 VERIFY(devinfo != NULL);
11722 sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
11723 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
11724 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
11726 /* In certain case (like DOORLOCK), the cdb could be NULL. */
11727 if (cdbp == NULL)
11728 return;
11729 /* We don't print log if no sense data available. */
11730 if (senlen == 0)
11731 sensep = NULL;
11732 com = cdbp->scc_cmd;
11733 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
11734 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
11738 * Function: sd_ssc_assessment
11740 * Description: We use this function to make an assessment at the point
11741 * where SD driver may encounter a potential error.
11743 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11744 * sd_uscsi_info in.
11745 * tp_assess - a hint of strategy for ereport posting.
11746 * Possible values of tp_assess include:
11747 * SD_FMT_IGNORE - we don't post any ereport because we're
11748 * sure that it is ok to ignore the underlying problems.
11749 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
11750 * but it might be not correct to ignore the underlying hardware
11751 * error.
11752 * SD_FMT_STATUS_CHECK - we will post an ereport with the
11753 * payload driver-assessment of value "fail" or
11754 * "fatal"(depending on what information we have here). This
11755 * assessment value is usually set when SD driver think there
11756 * is a potential error occurred(Typically, when return value
11757 * of the SCSI command is EIO).
11758 * SD_FMT_STANDARD - we will post an ereport with the payload
11759 * driver-assessment of value "info". This assessment value is
11760 * set when the SCSI command returned successfully and with
11761 * sense data sent back.
11763 * Context: Kernel thread.
11765 static void
11766 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
11768 int senlen = 0;
11769 struct uscsi_cmd *ucmdp = NULL;
11770 struct sd_lun *un;
11772 ASSERT(ssc != NULL);
11773 un = ssc->ssc_un;
11774 ASSERT(un != NULL);
11775 ucmdp = ssc->ssc_uscsi_cmd;
11776 ASSERT(ucmdp != NULL);
11778 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11779 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
11780 } else {
11782 * If enter here, it indicates that we have a wrong
11783 * calling sequence of sd_ssc_send and sd_ssc_assessment,
11784 * both of which should be called in a pair in case of
11785 * loss of FMA telemetries.
11787 if (ucmdp->uscsi_cdb != NULL) {
11788 SD_INFO(SD_LOG_SDTEST, un,
11789 "sd_ssc_assessment is missing the "
11790 "alternative sd_ssc_send when running 0x%x, "
11791 "or there are superfluous sd_ssc_assessment for "
11792 "the same sd_ssc_send.\n",
11793 ucmdp->uscsi_cdb[0]);
11796 * Set the ssc_flags to the initial value to avoid passing
11797 * down dirty flags to the following sd_ssc_send function.
11799 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11800 return;
11804 * Only handle an issued command which is waiting for assessment.
11805 * A command which is not issued will not have
11806 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
11808 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
11809 sd_ssc_print(ssc, SCSI_ERR_INFO);
11810 return;
11811 } else {
11813 * For an issued command, we should clear this flag in
11814 * order to make the sd_ssc_t structure be used off
11815 * multiple uscsi commands.
11817 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
11821 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
11822 * commands here. And we should clear the ssc_flags before return.
11824 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
11825 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11826 return;
11829 switch (tp_assess) {
11830 case SD_FMT_IGNORE:
11831 case SD_FMT_IGNORE_COMPROMISE:
11832 break;
11833 case SD_FMT_STATUS_CHECK:
11835 * For a failed command(including the succeeded command
11836 * with invalid data sent back).
11838 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
11839 break;
11840 case SD_FMT_STANDARD:
11842 * Always for the succeeded commands probably with sense
11843 * data sent back.
11844 * Limitation:
11845 * We can only handle a succeeded command with sense
11846 * data sent back when auto-request-sense is enabled.
11848 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
11849 ssc->ssc_uscsi_cmd->uscsi_rqresid;
11850 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
11851 (un->un_f_arq_enabled == TRUE) &&
11852 senlen > 0 &&
11853 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
11854 sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
11856 break;
11857 default:
11859 * Should not have other type of assessment.
11861 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
11862 "sd_ssc_assessment got wrong "
11863 "sd_type_assessment %d.\n", tp_assess);
11864 break;
11867 * Clear up the ssc_flags before return.
11869 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11873 * Function: sd_ssc_post
11875 * Description: 1. read the driver property to get fm-scsi-log flag.
11876 * 2. print log if fm_log_capable is non-zero.
11877 * 3. call sd_ssc_ereport_post to post ereport if possible.
11879 * Context: May be called from kernel thread or interrupt context.
11881 static void
11882 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
11884 struct sd_lun *un;
11885 int sd_severity;
11887 ASSERT(ssc != NULL);
11888 un = ssc->ssc_un;
11889 ASSERT(un != NULL);
11892 * We may enter here from sd_ssc_assessment(for USCSI command) or
11893 * by directly called from sdintr context.
11894 * We don't handle a non-disk drive(CD-ROM, removable media).
11895 * Clear the ssc_flags before return in case we've set
11896 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
11897 * driver.
11899 if (ISCD(un) || un->un_f_has_removable_media) {
11900 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11901 return;
11904 switch (sd_assess) {
11905 case SD_FM_DRV_FATAL:
11906 sd_severity = SCSI_ERR_FATAL;
11907 break;
11908 case SD_FM_DRV_RECOVERY:
11909 sd_severity = SCSI_ERR_RECOVERED;
11910 break;
11911 case SD_FM_DRV_RETRY:
11912 sd_severity = SCSI_ERR_RETRYABLE;
11913 break;
11914 case SD_FM_DRV_NOTICE:
11915 sd_severity = SCSI_ERR_INFO;
11916 break;
11917 default:
11918 sd_severity = SCSI_ERR_UNKNOWN;
11920 /* print log */
11921 sd_ssc_print(ssc, sd_severity);
11923 /* always post ereport */
11924 sd_ssc_ereport_post(ssc, sd_assess);
11928 * Function: sd_ssc_set_info
11930 * Description: Mark ssc_flags and set ssc_info which would be the
11931 * payload of uderr ereport. This function will cause
11932 * sd_ssc_ereport_post to post uderr ereport only.
11933 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
11934 * the function will also call SD_ERROR or scsi_log for a
11935 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
11937 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11938 * sd_uscsi_info in.
11939 * ssc_flags - indicate the sub-category of a uderr.
11940 * comp - this argument is meaningful only when
11941 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
11942 * values include:
11943 * > 0, SD_ERROR is used with comp as the driver logging
11944 * component;
11945 * = 0, scsi-log is used to log error telemetries;
11946 * < 0, no log available for this telemetry.
11948 * Context: Kernel thread or interrupt context
11950 static void
11951 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
11953 va_list ap;
11955 ASSERT(ssc != NULL);
11956 ASSERT(ssc->ssc_un != NULL);
11958 ssc->ssc_flags |= ssc_flags;
11959 va_start(ap, fmt);
11960 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
11961 va_end(ap);
11964 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
11965 * with invalid data sent back. For non-uscsi command, the
11966 * following code will be bypassed.
11968 if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
11969 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
11971 * If the error belong to certain component and we
11972 * do not want it to show up on the console, we
11973 * will use SD_ERROR, otherwise scsi_log is
11974 * preferred.
11976 if (comp > 0) {
11977 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
11978 } else if (comp == 0) {
11979 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
11980 CE_WARN, ssc->ssc_info);
11987 * Function: sd_buf_iodone
11989 * Description: Frees the sd_xbuf & returns the buf to its originator.
11991 * Context: May be called from interrupt context.
11993 /* ARGSUSED */
11994 static void
11995 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
11997 struct sd_xbuf *xp;
11999 ASSERT(un != NULL);
12000 ASSERT(bp != NULL);
12001 ASSERT(!mutex_owned(SD_MUTEX(un)));
12003 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12005 xp = SD_GET_XBUF(bp);
12006 VERIFY(xp != NULL);
12008 /* xbuf is gone after this */
12009 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12010 mutex_enter(SD_MUTEX(un));
12013 * Grab time when the cmd completed.
12014 * This is used for determining if the system has been
12015 * idle long enough to make it idle to the PM framework.
12016 * This is for lowering the overhead, and therefore improving
12017 * performance per I/O operation.
12019 un->un_pm_idle_time = gethrtime();
12021 un->un_ncmds_in_driver--;
12022 ASSERT(un->un_ncmds_in_driver >= 0);
12023 SD_INFO(SD_LOG_IO, un,
12024 "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12025 un->un_ncmds_in_driver);
12027 mutex_exit(SD_MUTEX(un));
12030 biodone(bp); /* bp is gone after this */
12032 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12037 * Function: sd_uscsi_iodone
12039 * Description: Frees the sd_xbuf & returns the buf to its originator.
12041 * Context: May be called from interrupt context.
12043 /* ARGSUSED */
12044 static void
12045 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12047 struct sd_xbuf *xp;
12049 ASSERT(un != NULL);
12050 ASSERT(bp != NULL);
12052 xp = SD_GET_XBUF(bp);
12053 ASSERT(xp != NULL);
12054 ASSERT(!mutex_owned(SD_MUTEX(un)));
12056 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12058 bp->b_private = xp->xb_private;
12060 mutex_enter(SD_MUTEX(un));
12063 * Grab time when the cmd completed.
12064 * This is used for determining if the system has been
12065 * idle long enough to make it idle to the PM framework.
12066 * This is for lowering the overhead, and therefore improving
12067 * performance per I/O operation.
12069 un->un_pm_idle_time = gethrtime();
12071 un->un_ncmds_in_driver--;
12072 ASSERT(un->un_ncmds_in_driver >= 0);
12073 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12074 un->un_ncmds_in_driver);
12076 mutex_exit(SD_MUTEX(un));
12078 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12079 SENSE_LENGTH) {
12080 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12081 MAX_SENSE_LENGTH);
12082 } else {
12083 kmem_free(xp, sizeof (struct sd_xbuf));
12086 biodone(bp);
12088 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12093 * Function: sd_mapblockaddr_iostart
12095 * Description: Verify request lies within the partition limits for
12096 * the indicated minor device. Issue "overrun" buf if
12097 * request would exceed partition range. Converts
12098 * partition-relative block address to absolute.
12100 * Upon exit of this function:
12101 * 1.I/O is aligned
12102 * xp->xb_blkno represents the absolute sector address
12103 * 2.I/O is misaligned
12104 * xp->xb_blkno represents the absolute logical block address
12105 * based on DEV_BSIZE. The logical block address will be
12106 * converted to physical sector address in sd_mapblocksize_\
12107 * iostart.
12108 * 3.I/O is misaligned but is aligned in "overrun" buf
12109 * xp->xb_blkno represents the absolute logical block address
12110 * based on DEV_BSIZE. The logical block address will be
12111 * converted to physical sector address in sd_mapblocksize_\
12112 * iostart. But no RMW will be issued in this case.
12114 * Context: Can sleep
12116 * Issues: This follows what the old code did, in terms of accessing
12117 * some of the partition info in the unit struct without holding
12118 * the mutext. This is a general issue, if the partition info
12119 * can be altered while IO is in progress... as soon as we send
12120 * a buf, its partitioning can be invalid before it gets to the
12121 * device. Probably the right fix is to move partitioning out
12122 * of the driver entirely.
12125 static void
12126 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12128 diskaddr_t nblocks; /* #blocks in the given partition */
12129 daddr_t blocknum; /* Block number specified by the buf */
12130 size_t requested_nblocks;
12131 size_t available_nblocks;
12132 int partition;
12133 diskaddr_t partition_offset;
12134 struct sd_xbuf *xp;
12135 int secmask = 0, blknomask = 0;
12136 ushort_t is_aligned = TRUE;
12138 ASSERT(un != NULL);
12139 ASSERT(bp != NULL);
12140 ASSERT(!mutex_owned(SD_MUTEX(un)));
12142 SD_TRACE(SD_LOG_IO_PARTITION, un,
12143 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12145 xp = SD_GET_XBUF(bp);
12146 ASSERT(xp != NULL);
12149 * If the geometry is not indicated as valid, attempt to access
12150 * the unit & verify the geometry/label. This can be the case for
12151 * removable-media devices, of if the device was opened in
12152 * NDELAY/NONBLOCK mode.
12154 partition = SDPART(bp->b_edev);
12156 if (!SD_IS_VALID_LABEL(un)) {
12157 sd_ssc_t *ssc;
12159 * Initialize sd_ssc_t for internal uscsi commands
12160 * In case of potential porformance issue, we need
12161 * to alloc memory only if there is invalid label
12163 ssc = sd_ssc_init(un);
12165 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12167 * For removable devices it is possible to start an
12168 * I/O without a media by opening the device in nodelay
12169 * mode. Also for writable CDs there can be many
12170 * scenarios where there is no geometry yet but volume
12171 * manager is trying to issue a read() just because
12172 * it can see TOC on the CD. So do not print a message
12173 * for removables.
12175 if (!un->un_f_has_removable_media) {
12176 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12177 "i/o to invalid geometry\n");
12179 bioerror(bp, EIO);
12180 bp->b_resid = bp->b_bcount;
12181 SD_BEGIN_IODONE(index, un, bp);
12183 sd_ssc_fini(ssc);
12184 return;
12186 sd_ssc_fini(ssc);
12189 nblocks = 0;
12190 (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12191 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12193 if (un->un_f_enable_rmw) {
12194 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12195 secmask = un->un_phy_blocksize - 1;
12196 } else {
12197 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12198 secmask = un->un_tgt_blocksize - 1;
12201 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12202 is_aligned = FALSE;
12205 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12207 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12208 * Convert the logical block number to target's physical sector
12209 * number.
12211 if (is_aligned) {
12212 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12213 } else {
12215 * There is no RMW if we're just reading, so don't
12216 * warn or error out because of it.
12218 if (bp->b_flags & B_READ) {
12219 /*EMPTY*/
12220 } else if (!un->un_f_enable_rmw &&
12221 un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12222 bp->b_flags |= B_ERROR;
12223 goto error_exit;
12224 } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12225 mutex_enter(SD_MUTEX(un));
12226 if (!un->un_f_enable_rmw &&
12227 un->un_rmw_msg_timeid == NULL) {
12228 scsi_log(SD_DEVINFO(un), sd_label,
12229 CE_WARN, "I/O request is not "
12230 "aligned with %d disk sector size. "
12231 "It is handled through Read Modify "
12232 "Write but the performance is "
12233 "very low.\n",
12234 un->un_tgt_blocksize);
12235 un->un_rmw_msg_timeid =
12236 timeout(sd_rmw_msg_print_handler,
12237 un, SD_RMW_MSG_PRINT_TIMEOUT);
12238 } else {
12239 un->un_rmw_incre_count ++;
12241 mutex_exit(SD_MUTEX(un));
12244 nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12245 partition_offset = SD_TGT2SYSBLOCK(un,
12246 partition_offset);
12251 * blocknum is the starting block number of the request. At this
12252 * point it is still relative to the start of the minor device.
12254 blocknum = xp->xb_blkno;
12257 * Legacy: If the starting block number is one past the last block
12258 * in the partition, do not set B_ERROR in the buf.
12260 if (blocknum == nblocks) {
12261 goto error_exit;
12265 * Confirm that the first block of the request lies within the
12266 * partition limits. Also the requested number of bytes must be
12267 * a multiple of the system block size.
12269 if ((blocknum < 0) || (blocknum >= nblocks) ||
12270 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12271 bp->b_flags |= B_ERROR;
12272 goto error_exit;
12276 * If the requsted # blocks exceeds the available # blocks, that
12277 * is an overrun of the partition.
12279 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12280 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12281 } else {
12282 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12285 available_nblocks = (size_t)(nblocks - blocknum);
12286 ASSERT(nblocks >= blocknum);
12288 if (requested_nblocks > available_nblocks) {
12289 size_t resid;
12292 * Allocate an "overrun" buf to allow the request to proceed
12293 * for the amount of space available in the partition. The
12294 * amount not transferred will be added into the b_resid
12295 * when the operation is complete. The overrun buf
12296 * replaces the original buf here, and the original buf
12297 * is saved inside the overrun buf, for later use.
12299 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12300 resid = SD_TGTBLOCKS2BYTES(un,
12301 (offset_t)(requested_nblocks - available_nblocks));
12302 } else {
12303 resid = SD_SYSBLOCKS2BYTES(
12304 (offset_t)(requested_nblocks - available_nblocks));
12307 size_t count = bp->b_bcount - resid;
12309 * Note: count is an unsigned entity thus it'll NEVER
12310 * be less than 0 so ASSERT the original values are
12311 * correct.
12313 ASSERT(bp->b_bcount >= resid);
12315 bp = sd_bioclone_alloc(bp, count, blocknum,
12316 (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12317 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12318 ASSERT(xp != NULL);
12321 /* At this point there should be no residual for this buf. */
12322 ASSERT(bp->b_resid == 0);
12324 /* Convert the block number to an absolute address. */
12325 xp->xb_blkno += partition_offset;
12327 SD_NEXT_IOSTART(index, un, bp);
12329 SD_TRACE(SD_LOG_IO_PARTITION, un,
12330 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12332 return;
12334 error_exit:
12335 bp->b_resid = bp->b_bcount;
12336 SD_BEGIN_IODONE(index, un, bp);
12337 SD_TRACE(SD_LOG_IO_PARTITION, un,
12338 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12343 * Function: sd_mapblockaddr_iodone
12345 * Description: Completion-side processing for partition management.
12347 * Context: May be called under interrupt context
12350 static void
12351 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12353 /* int partition; */ /* Not used, see below. */
12354 ASSERT(un != NULL);
12355 ASSERT(bp != NULL);
12356 ASSERT(!mutex_owned(SD_MUTEX(un)));
12358 SD_TRACE(SD_LOG_IO_PARTITION, un,
12359 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12361 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12363 * We have an "overrun" buf to deal with...
12365 struct sd_xbuf *xp;
12366 struct buf *obp; /* ptr to the original buf */
12368 xp = SD_GET_XBUF(bp);
12369 ASSERT(xp != NULL);
12371 /* Retrieve the pointer to the original buf */
12372 obp = (struct buf *)xp->xb_private;
12373 ASSERT(obp != NULL);
12375 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12376 bioerror(obp, bp->b_error);
12378 sd_bioclone_free(bp);
12381 * Get back the original buf.
12382 * Note that since the restoration of xb_blkno below
12383 * was removed, the sd_xbuf is not needed.
12385 bp = obp;
12387 * xp = SD_GET_XBUF(bp);
12388 * ASSERT(xp != NULL);
12393 * Convert sd->xb_blkno back to a minor-device relative value.
12394 * Note: this has been commented out, as it is not needed in the
12395 * current implementation of the driver (ie, since this function
12396 * is at the top of the layering chains, so the info will be
12397 * discarded) and it is in the "hot" IO path.
12399 * partition = getminor(bp->b_edev) & SDPART_MASK;
12400 * xp->xb_blkno -= un->un_offset[partition];
12403 SD_NEXT_IODONE(index, un, bp);
12405 SD_TRACE(SD_LOG_IO_PARTITION, un,
12406 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12411 * Function: sd_mapblocksize_iostart
12413 * Description: Convert between system block size (un->un_sys_blocksize)
12414 * and target block size (un->un_tgt_blocksize).
12416 * Context: Can sleep to allocate resources.
12418 * Assumptions: A higher layer has already performed any partition validation,
12419 * and converted the xp->xb_blkno to an absolute value relative
12420 * to the start of the device.
12422 * It is also assumed that the higher layer has implemented
12423 * an "overrun" mechanism for the case where the request would
12424 * read/write beyond the end of a partition. In this case we
12425 * assume (and ASSERT) that bp->b_resid == 0.
12427 * Note: The implementation for this routine assumes the target
12428 * block size remains constant between allocation and transport.
12431 static void
12432 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12434 struct sd_mapblocksize_info *bsp;
12435 struct sd_xbuf *xp;
12436 offset_t first_byte;
12437 daddr_t start_block, end_block;
12438 daddr_t request_bytes;
12439 ushort_t is_aligned = FALSE;
12441 ASSERT(un != NULL);
12442 ASSERT(bp != NULL);
12443 ASSERT(!mutex_owned(SD_MUTEX(un)));
12444 ASSERT(bp->b_resid == 0);
12446 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12447 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12450 * For a non-writable CD, a write request is an error
12452 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12453 (un->un_f_mmc_writable_media == FALSE)) {
12454 bioerror(bp, EIO);
12455 bp->b_resid = bp->b_bcount;
12456 SD_BEGIN_IODONE(index, un, bp);
12457 return;
12461 * We do not need a shadow buf if the device is using
12462 * un->un_sys_blocksize as its block size or if bcount == 0.
12463 * In this case there is no layer-private data block allocated.
12465 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12466 (bp->b_bcount == 0)) {
12467 goto done;
12470 #if defined(__i386) || defined(__amd64)
12471 /* We do not support non-block-aligned transfers for ROD devices */
12472 ASSERT(!ISROD(un));
12473 #endif
12475 xp = SD_GET_XBUF(bp);
12476 ASSERT(xp != NULL);
12478 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12479 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12480 un->un_tgt_blocksize, DEV_BSIZE);
12481 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12482 "request start block:0x%x\n", xp->xb_blkno);
12483 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12484 "request len:0x%x\n", bp->b_bcount);
12487 * Allocate the layer-private data area for the mapblocksize layer.
12488 * Layers are allowed to use the xp_private member of the sd_xbuf
12489 * struct to store the pointer to their layer-private data block, but
12490 * each layer also has the responsibility of restoring the prior
12491 * contents of xb_private before returning the buf/xbuf to the
12492 * higher layer that sent it.
12494 * Here we save the prior contents of xp->xb_private into the
12495 * bsp->mbs_oprivate field of our layer-private data area. This value
12496 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12497 * the layer-private area and returning the buf/xbuf to the layer
12498 * that sent it.
12500 * Note that here we use kmem_zalloc for the allocation as there are
12501 * parts of the mapblocksize code that expect certain fields to be
12502 * zero unless explicitly set to a required value.
12504 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12505 bsp->mbs_oprivate = xp->xb_private;
12506 xp->xb_private = bsp;
12509 * This treats the data on the disk (target) as an array of bytes.
12510 * first_byte is the byte offset, from the beginning of the device,
12511 * to the location of the request. This is converted from a
12512 * un->un_sys_blocksize block address to a byte offset, and then back
12513 * to a block address based upon a un->un_tgt_blocksize block size.
12515 * xp->xb_blkno should be absolute upon entry into this function,
12516 * but, but it is based upon partitions that use the "system"
12517 * block size. It must be adjusted to reflect the block size of
12518 * the target.
12520 * Note that end_block is actually the block that follows the last
12521 * block of the request, but that's what is needed for the computation.
12523 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12524 if (un->un_f_enable_rmw) {
12525 start_block = xp->xb_blkno =
12526 (first_byte / un->un_phy_blocksize) *
12527 (un->un_phy_blocksize / DEV_BSIZE);
12528 end_block = ((first_byte + bp->b_bcount +
12529 un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12530 (un->un_phy_blocksize / DEV_BSIZE);
12531 } else {
12532 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12533 end_block = (first_byte + bp->b_bcount +
12534 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12537 /* request_bytes is rounded up to a multiple of the target block size */
12538 request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12541 * See if the starting address of the request and the request
12542 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12543 * then we do not need to allocate a shadow buf to handle the request.
12545 if (un->un_f_enable_rmw) {
12546 if (((first_byte % un->un_phy_blocksize) == 0) &&
12547 ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12548 is_aligned = TRUE;
12550 } else {
12551 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12552 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12553 is_aligned = TRUE;
12557 if ((bp->b_flags & B_READ) == 0) {
12559 * Lock the range for a write operation. An aligned request is
12560 * considered a simple write; otherwise the request must be a
12561 * read-modify-write.
12563 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12564 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12568 * Alloc a shadow buf if the request is not aligned. Also, this is
12569 * where the READ command is generated for a read-modify-write. (The
12570 * write phase is deferred until after the read completes.)
12572 if (is_aligned == FALSE) {
12574 struct sd_mapblocksize_info *shadow_bsp;
12575 struct sd_xbuf *shadow_xp;
12576 struct buf *shadow_bp;
12579 * Allocate the shadow buf and it associated xbuf. Note that
12580 * after this call the xb_blkno value in both the original
12581 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12582 * same: absolute relative to the start of the device, and
12583 * adjusted for the target block size. The b_blkno in the
12584 * shadow buf will also be set to this value. We should never
12585 * change b_blkno in the original bp however.
12587 * Note also that the shadow buf will always need to be a
12588 * READ command, regardless of whether the incoming command
12589 * is a READ or a WRITE.
12591 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12592 xp->xb_blkno,
12593 (int (*)(struct buf *)) sd_mapblocksize_iodone);
12595 shadow_xp = SD_GET_XBUF(shadow_bp);
12598 * Allocate the layer-private data for the shadow buf.
12599 * (No need to preserve xb_private in the shadow xbuf.)
12601 shadow_xp->xb_private = shadow_bsp =
12602 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12605 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12606 * to figure out where the start of the user data is (based upon
12607 * the system block size) in the data returned by the READ
12608 * command (which will be based upon the target blocksize). Note
12609 * that this is only really used if the request is unaligned.
12611 if (un->un_f_enable_rmw) {
12612 bsp->mbs_copy_offset = (ssize_t)(first_byte -
12613 ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
12614 ASSERT((bsp->mbs_copy_offset >= 0) &&
12615 (bsp->mbs_copy_offset < un->un_phy_blocksize));
12616 } else {
12617 bsp->mbs_copy_offset = (ssize_t)(first_byte -
12618 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12619 ASSERT((bsp->mbs_copy_offset >= 0) &&
12620 (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12623 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12625 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12627 /* Transfer the wmap (if any) to the shadow buf */
12628 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12629 bsp->mbs_wmp = NULL;
12632 * The shadow buf goes on from here in place of the
12633 * original buf.
12635 shadow_bsp->mbs_orig_bp = bp;
12636 bp = shadow_bp;
12639 SD_INFO(SD_LOG_IO_RMMEDIA, un,
12640 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12641 SD_INFO(SD_LOG_IO_RMMEDIA, un,
12642 "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12643 request_bytes);
12644 SD_INFO(SD_LOG_IO_RMMEDIA, un,
12645 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12647 done:
12648 SD_NEXT_IOSTART(index, un, bp);
12650 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12651 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12656 * Function: sd_mapblocksize_iodone
12658 * Description: Completion side processing for block-size mapping.
12660 * Context: May be called under interrupt context
12663 static void
12664 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12666 struct sd_mapblocksize_info *bsp;
12667 struct sd_xbuf *xp;
12668 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */
12669 struct buf *orig_bp; /* ptr to the original buf */
12670 offset_t shadow_end;
12671 offset_t request_end;
12672 offset_t shadow_start;
12673 ssize_t copy_offset;
12674 size_t copy_length;
12675 size_t shortfall;
12676 uint_t is_write; /* TRUE if this bp is a WRITE */
12677 uint_t has_wmap; /* TRUE is this bp has a wmap */
12679 ASSERT(un != NULL);
12680 ASSERT(bp != NULL);
12682 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12683 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12686 * There is no shadow buf or layer-private data if the target is
12687 * using un->un_sys_blocksize as its block size or if bcount == 0.
12689 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12690 (bp->b_bcount == 0)) {
12691 goto exit;
12694 xp = SD_GET_XBUF(bp);
12695 ASSERT(xp != NULL);
12697 /* Retrieve the pointer to the layer-private data area from the xbuf. */
12698 bsp = xp->xb_private;
12700 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12701 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12703 if (is_write) {
12705 * For a WRITE request we must free up the block range that
12706 * we have locked up. This holds regardless of whether this is
12707 * an aligned write request or a read-modify-write request.
12709 sd_range_unlock(un, bsp->mbs_wmp);
12710 bsp->mbs_wmp = NULL;
12713 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12715 * An aligned read or write command will have no shadow buf;
12716 * there is not much else to do with it.
12718 goto done;
12721 orig_bp = bsp->mbs_orig_bp;
12722 ASSERT(orig_bp != NULL);
12723 orig_xp = SD_GET_XBUF(orig_bp);
12724 VERIFY(orig_xp != NULL);
12725 ASSERT(!mutex_owned(SD_MUTEX(un)));
12727 if (!is_write && has_wmap) {
12729 * A READ with a wmap means this is the READ phase of a
12730 * read-modify-write. If an error occurred on the READ then
12731 * we do not proceed with the WRITE phase or copy any data.
12732 * Just release the write maps and return with an error.
12734 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12735 orig_bp->b_resid = orig_bp->b_bcount;
12736 bioerror(orig_bp, bp->b_error);
12737 sd_range_unlock(un, bsp->mbs_wmp);
12738 goto freebuf_done;
12743 * Here is where we set up to copy the data from the shadow buf
12744 * into the space associated with the original buf.
12746 * To deal with the conversion between block sizes, these
12747 * computations treat the data as an array of bytes, with the
12748 * first byte (byte 0) corresponding to the first byte in the
12749 * first block on the disk.
12753 * shadow_start and shadow_len indicate the location and size of
12754 * the data returned with the shadow IO request.
12756 if (un->un_f_enable_rmw) {
12757 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12758 } else {
12759 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12761 shadow_end = shadow_start + bp->b_bcount - bp->b_resid;
12764 * copy_offset gives the offset (in bytes) from the start of the first
12765 * block of the READ request to the beginning of the data. We retrieve
12766 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12767 * there by sd_mapblockize_iostart(). copy_length gives the amount of
12768 * data to be copied (in bytes).
12770 copy_offset = bsp->mbs_copy_offset;
12771 if (un->un_f_enable_rmw) {
12772 ASSERT((copy_offset >= 0) &&
12773 (copy_offset < un->un_phy_blocksize));
12774 } else {
12775 ASSERT((copy_offset >= 0) &&
12776 (copy_offset < un->un_tgt_blocksize));
12779 copy_length = orig_bp->b_bcount;
12780 request_end = shadow_start + copy_offset + orig_bp->b_bcount;
12783 * Set up the resid and error fields of orig_bp as appropriate.
12785 if (shadow_end >= request_end) {
12786 /* We got all the requested data; set resid to zero */
12787 orig_bp->b_resid = 0;
12788 } else {
12790 * We failed to get enough data to fully satisfy the original
12791 * request. Just copy back whatever data we got and set
12792 * up the residual and error code as required.
12794 * 'shortfall' is the amount by which the data received with the
12795 * shadow buf has "fallen short" of the requested amount.
12797 shortfall = (size_t)(request_end - shadow_end);
12799 if (shortfall > orig_bp->b_bcount) {
12801 * We did not get enough data to even partially
12802 * fulfill the original request. The residual is
12803 * equal to the amount requested.
12805 orig_bp->b_resid = orig_bp->b_bcount;
12806 } else {
12808 * We did not get all the data that we requested
12809 * from the device, but we will try to return what
12810 * portion we did get.
12812 orig_bp->b_resid = shortfall;
12814 ASSERT(copy_length >= orig_bp->b_resid);
12815 copy_length -= orig_bp->b_resid;
12818 /* Propagate the error code from the shadow buf to the original buf */
12819 bioerror(orig_bp, bp->b_error);
12821 if (is_write) {
12822 goto freebuf_done; /* No data copying for a WRITE */
12825 if (has_wmap) {
12827 * This is a READ command from the READ phase of a
12828 * read-modify-write request. We have to copy the data given
12829 * by the user OVER the data returned by the READ command,
12830 * then convert the command from a READ to a WRITE and send
12831 * it back to the target.
12833 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12834 copy_length);
12836 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */
12839 * Dispatch the WRITE command to the taskq thread, which
12840 * will in turn send the command to the target. When the
12841 * WRITE command completes, we (sd_mapblocksize_iodone())
12842 * will get called again as part of the iodone chain
12843 * processing for it. Note that we will still be dealing
12844 * with the shadow buf at that point.
12846 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12847 KM_NOSLEEP) != 0) {
12849 * Dispatch was successful so we are done. Return
12850 * without going any higher up the iodone chain. Do
12851 * not free up any layer-private data until after the
12852 * WRITE completes.
12854 return;
12858 * Dispatch of the WRITE command failed; set up the error
12859 * condition and send this IO back up the iodone chain.
12861 bioerror(orig_bp, EIO);
12862 orig_bp->b_resid = orig_bp->b_bcount;
12864 } else {
12866 * This is a regular READ request (ie, not a RMW). Copy the
12867 * data from the shadow buf into the original buf. The
12868 * copy_offset compensates for any "misalignment" between the
12869 * shadow buf (with its un->un_tgt_blocksize blocks) and the
12870 * original buf (with its un->un_sys_blocksize blocks).
12872 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
12873 copy_length);
12876 freebuf_done:
12879 * At this point we still have both the shadow buf AND the original
12880 * buf to deal with, as well as the layer-private data area in each.
12881 * Local variables are as follows:
12883 * bp -- points to shadow buf
12884 * xp -- points to xbuf of shadow buf
12885 * bsp -- points to layer-private data area of shadow buf
12886 * orig_bp -- points to original buf
12888 * First free the shadow buf and its associated xbuf, then free the
12889 * layer-private data area from the shadow buf. There is no need to
12890 * restore xb_private in the shadow xbuf.
12892 sd_shadow_buf_free(bp);
12893 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12896 * Now update the local variables to point to the original buf, xbuf,
12897 * and layer-private area.
12899 bp = orig_bp;
12900 xp = SD_GET_XBUF(bp);
12901 ASSERT(xp != NULL);
12902 ASSERT(xp == orig_xp);
12903 bsp = xp->xb_private;
12904 ASSERT(bsp != NULL);
12906 done:
12908 * Restore xb_private to whatever it was set to by the next higher
12909 * layer in the chain, then free the layer-private data area.
12911 xp->xb_private = bsp->mbs_oprivate;
12912 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12914 exit:
12915 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
12916 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
12918 SD_NEXT_IODONE(index, un, bp);
12923 * Function: sd_checksum_iostart
12925 * Description: A stub function for a layer that's currently not used.
12926 * For now just a placeholder.
12928 * Context: Kernel thread context
12931 static void
12932 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
12934 ASSERT(un != NULL);
12935 ASSERT(bp != NULL);
12936 ASSERT(!mutex_owned(SD_MUTEX(un)));
12937 SD_NEXT_IOSTART(index, un, bp);
12942 * Function: sd_checksum_iodone
12944 * Description: A stub function for a layer that's currently not used.
12945 * For now just a placeholder.
12947 * Context: May be called under interrupt context
12950 static void
12951 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
12953 ASSERT(un != NULL);
12954 ASSERT(bp != NULL);
12955 ASSERT(!mutex_owned(SD_MUTEX(un)));
12956 SD_NEXT_IODONE(index, un, bp);
12961 * Function: sd_checksum_uscsi_iostart
12963 * Description: A stub function for a layer that's currently not used.
12964 * For now just a placeholder.
12966 * Context: Kernel thread context
12969 static void
12970 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
12972 ASSERT(un != NULL);
12973 ASSERT(bp != NULL);
12974 ASSERT(!mutex_owned(SD_MUTEX(un)));
12975 SD_NEXT_IOSTART(index, un, bp);
12980 * Function: sd_checksum_uscsi_iodone
12982 * Description: A stub function for a layer that's currently not used.
12983 * For now just a placeholder.
12985 * Context: May be called under interrupt context
12988 static void
12989 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12991 ASSERT(un != NULL);
12992 ASSERT(bp != NULL);
12993 ASSERT(!mutex_owned(SD_MUTEX(un)));
12994 SD_NEXT_IODONE(index, un, bp);
12999 * Function: sd_pm_iostart
13001 * Description: iostart-side routine for Power mangement.
13003 * Context: Kernel thread context
13006 static void
13007 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13009 ASSERT(un != NULL);
13010 ASSERT(bp != NULL);
13011 ASSERT(!mutex_owned(SD_MUTEX(un)));
13012 ASSERT(!mutex_owned(&un->un_pm_mutex));
13014 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13016 if (sd_pm_entry(un) != DDI_SUCCESS) {
13018 * Set up to return the failed buf back up the 'iodone'
13019 * side of the calling chain.
13021 bioerror(bp, EIO);
13022 bp->b_resid = bp->b_bcount;
13024 SD_BEGIN_IODONE(index, un, bp);
13026 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13027 return;
13030 SD_NEXT_IOSTART(index, un, bp);
13032 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13037 * Function: sd_pm_iodone
13039 * Description: iodone-side routine for power mangement.
13041 * Context: may be called from interrupt context
13044 static void
13045 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13047 ASSERT(un != NULL);
13048 ASSERT(bp != NULL);
13049 ASSERT(!mutex_owned(&un->un_pm_mutex));
13051 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13054 * After attach the following flag is only read, so don't
13055 * take the penalty of acquiring a mutex for it.
13057 if (un->un_f_pm_is_enabled == TRUE) {
13058 sd_pm_exit(un);
13061 SD_NEXT_IODONE(index, un, bp);
13063 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13068 * Function: sd_core_iostart
13070 * Description: Primary driver function for enqueuing buf(9S) structs from
13071 * the system and initiating IO to the target device
13073 * Context: Kernel thread context. Can sleep.
13075 * Assumptions: - The given xp->xb_blkno is absolute
13076 * (ie, relative to the start of the device).
13077 * - The IO is to be done using the native blocksize of
13078 * the device, as specified in un->un_tgt_blocksize.
13080 /* ARGSUSED */
13081 static void
13082 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13084 struct sd_xbuf *xp;
13086 ASSERT(un != NULL);
13087 ASSERT(bp != NULL);
13088 ASSERT(!mutex_owned(SD_MUTEX(un)));
13089 ASSERT(bp->b_resid == 0);
13091 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13093 xp = SD_GET_XBUF(bp);
13094 ASSERT(xp != NULL);
13096 mutex_enter(SD_MUTEX(un));
13099 * If we are currently in the failfast state, fail any new IO
13100 * that has B_FAILFAST set, then return.
13102 if ((bp->b_flags & B_FAILFAST) &&
13103 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13104 mutex_exit(SD_MUTEX(un));
13105 bioerror(bp, EIO);
13106 bp->b_resid = bp->b_bcount;
13107 SD_BEGIN_IODONE(index, un, bp);
13108 return;
13111 if (SD_IS_DIRECT_PRIORITY(xp)) {
13113 * Priority command -- transport it immediately.
13115 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13116 * because all direct priority commands should be associated
13117 * with error recovery actions which we don't want to retry.
13119 sd_start_cmds(un, bp);
13120 } else {
13122 * Normal command -- add it to the wait queue, then start
13123 * transporting commands from the wait queue.
13125 sd_add_buf_to_waitq(un, bp);
13126 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13127 sd_start_cmds(un, NULL);
13130 mutex_exit(SD_MUTEX(un));
13132 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13137 * Function: sd_init_cdb_limits
13139 * Description: This is to handle scsi_pkt initialization differences
13140 * between the driver platforms.
13142 * Legacy behaviors:
13144 * If the block number or the sector count exceeds the
13145 * capabilities of a Group 0 command, shift over to a
13146 * Group 1 command. We don't blindly use Group 1
13147 * commands because a) some drives (CDC Wren IVs) get a
13148 * bit confused, and b) there is probably a fair amount
13149 * of speed difference for a target to receive and decode
13150 * a 10 byte command instead of a 6 byte command.
13152 * The xfer time difference of 6 vs 10 byte CDBs is
13153 * still significant so this code is still worthwhile.
13154 * 10 byte CDBs are very inefficient with the fas HBA driver
13155 * and older disks. Each CDB byte took 1 usec with some
13156 * popular disks.
13158 * Context: Must be called at attach time
13161 static void
13162 sd_init_cdb_limits(struct sd_lun *un)
13164 int hba_cdb_limit;
13167 * Use CDB_GROUP1 commands for most devices except for
13168 * parallel SCSI fixed drives in which case we get better
13169 * performance using CDB_GROUP0 commands (where applicable).
13171 un->un_mincdb = SD_CDB_GROUP1;
13172 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13173 !un->un_f_has_removable_media) {
13174 un->un_mincdb = SD_CDB_GROUP0;
13178 * Try to read the max-cdb-length supported by HBA.
13180 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13181 if (0 >= un->un_max_hba_cdb) {
13182 un->un_max_hba_cdb = CDB_GROUP4;
13183 hba_cdb_limit = SD_CDB_GROUP4;
13184 } else if (0 < un->un_max_hba_cdb &&
13185 un->un_max_hba_cdb < CDB_GROUP1) {
13186 hba_cdb_limit = SD_CDB_GROUP0;
13187 } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13188 un->un_max_hba_cdb < CDB_GROUP5) {
13189 hba_cdb_limit = SD_CDB_GROUP1;
13190 } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13191 un->un_max_hba_cdb < CDB_GROUP4) {
13192 hba_cdb_limit = SD_CDB_GROUP5;
13193 } else {
13194 hba_cdb_limit = SD_CDB_GROUP4;
13198 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
13199 * commands for fixed disks unless we are building for a 32 bit
13200 * kernel.
13202 #ifdef _LP64
13203 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13204 min(hba_cdb_limit, SD_CDB_GROUP4);
13205 #else
13206 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13207 min(hba_cdb_limit, SD_CDB_GROUP1);
13208 #endif
13210 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13211 ? sizeof (struct scsi_arq_status) : 1);
13212 if (!ISCD(un))
13213 un->un_cmd_timeout = (ushort_t)sd_io_time;
13214 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13219 * Function: sd_initpkt_for_buf
13221 * Description: Allocate and initialize for transport a scsi_pkt struct,
13222 * based upon the info specified in the given buf struct.
13224 * Assumes the xb_blkno in the request is absolute (ie,
13225 * relative to the start of the device (NOT partition!).
13226 * Also assumes that the request is using the native block
13227 * size of the device (as returned by the READ CAPACITY
13228 * command).
13230 * Return Code: SD_PKT_ALLOC_SUCCESS
13231 * SD_PKT_ALLOC_FAILURE
13232 * SD_PKT_ALLOC_FAILURE_NO_DMA
13233 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13235 * Context: Kernel thread and may be called from software interrupt context
13236 * as part of a sdrunout callback. This function may not block or
13237 * call routines that block
13240 static int
13241 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13243 struct sd_xbuf *xp;
13244 struct scsi_pkt *pktp = NULL;
13245 struct sd_lun *un;
13246 size_t blockcount;
13247 daddr_t startblock;
13248 int rval;
13249 int cmd_flags;
13251 ASSERT(bp != NULL);
13252 ASSERT(pktpp != NULL);
13253 xp = SD_GET_XBUF(bp);
13254 ASSERT(xp != NULL);
13255 un = SD_GET_UN(bp);
13256 ASSERT(un != NULL);
13257 ASSERT(mutex_owned(SD_MUTEX(un)));
13258 ASSERT(bp->b_resid == 0);
13260 SD_TRACE(SD_LOG_IO_CORE, un,
13261 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13263 mutex_exit(SD_MUTEX(un));
13265 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13266 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13268 * Already have a scsi_pkt -- just need DMA resources.
13269 * We must recompute the CDB in case the mapping returns
13270 * a nonzero pkt_resid.
13271 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13272 * that is being retried, the unmap/remap of the DMA resouces
13273 * will result in the entire transfer starting over again
13274 * from the very first block.
13276 ASSERT(xp->xb_pktp != NULL);
13277 pktp = xp->xb_pktp;
13278 } else {
13279 pktp = NULL;
13281 #endif /* __i386 || __amd64 */
13283 startblock = xp->xb_blkno; /* Absolute block num. */
13284 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13286 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13289 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13290 * call scsi_init_pkt, and build the CDB.
13292 rval = sd_setup_rw_pkt(un, &pktp, bp,
13293 cmd_flags, sdrunout, (caddr_t)un,
13294 startblock, blockcount);
13296 if (rval == 0) {
13298 * Success.
13300 * If partial DMA is being used and required for this transfer.
13301 * set it up here.
13303 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13304 (pktp->pkt_resid != 0)) {
13307 * Save the CDB length and pkt_resid for the
13308 * next xfer
13310 xp->xb_dma_resid = pktp->pkt_resid;
13312 /* rezero resid */
13313 pktp->pkt_resid = 0;
13315 } else {
13316 xp->xb_dma_resid = 0;
13319 pktp->pkt_flags = un->un_tagflags;
13320 pktp->pkt_time = un->un_cmd_timeout;
13321 pktp->pkt_comp = sdintr;
13323 pktp->pkt_private = bp;
13324 *pktpp = pktp;
13326 SD_TRACE(SD_LOG_IO_CORE, un,
13327 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13329 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13330 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13331 #endif
13333 mutex_enter(SD_MUTEX(un));
13334 return (SD_PKT_ALLOC_SUCCESS);
13339 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13340 * from sd_setup_rw_pkt.
13342 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13344 if (rval == SD_PKT_ALLOC_FAILURE) {
13345 *pktpp = NULL;
13347 * Set the driver state to RWAIT to indicate the driver
13348 * is waiting on resource allocations. The driver will not
13349 * suspend, pm_suspend, or detatch while the state is RWAIT.
13351 mutex_enter(SD_MUTEX(un));
13352 New_state(un, SD_STATE_RWAIT);
13354 SD_ERROR(SD_LOG_IO_CORE, un,
13355 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13357 if ((bp->b_flags & B_ERROR) != 0) {
13358 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13360 return (SD_PKT_ALLOC_FAILURE);
13361 } else {
13363 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13365 * This should never happen. Maybe someone messed with the
13366 * kernel's minphys?
13368 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13369 "Request rejected: too large for CDB: "
13370 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount);
13371 SD_ERROR(SD_LOG_IO_CORE, un,
13372 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13373 mutex_enter(SD_MUTEX(un));
13374 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13381 * Function: sd_destroypkt_for_buf
13383 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13385 * Context: Kernel thread or interrupt context
13388 static void
13389 sd_destroypkt_for_buf(struct buf *bp)
13391 ASSERT(bp != NULL);
13392 ASSERT(SD_GET_UN(bp) != NULL);
13394 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13395 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13397 ASSERT(SD_GET_PKTP(bp) != NULL);
13398 scsi_destroy_pkt(SD_GET_PKTP(bp));
13400 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13401 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13405 * Function: sd_setup_rw_pkt
13407 * Description: Determines appropriate CDB group for the requested LBA
13408 * and transfer length, calls scsi_init_pkt, and builds
13409 * the CDB. Do not use for partial DMA transfers except
13410 * for the initial transfer since the CDB size must
13411 * remain constant.
13413 * Context: Kernel thread and may be called from software interrupt
13414 * context as part of a sdrunout callback. This function may not
13415 * block or call routines that block
13420 sd_setup_rw_pkt(struct sd_lun *un,
13421 struct scsi_pkt **pktpp, struct buf *bp, int flags,
13422 int (*callback)(caddr_t), caddr_t callback_arg,
13423 diskaddr_t lba, uint32_t blockcount)
13425 struct scsi_pkt *return_pktp;
13426 union scsi_cdb *cdbp;
13427 struct sd_cdbinfo *cp = NULL;
13428 int i;
13431 * See which size CDB to use, based upon the request.
13433 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13436 * Check lba and block count against sd_cdbtab limits.
13437 * In the partial DMA case, we have to use the same size
13438 * CDB for all the transfers. Check lba + blockcount
13439 * against the max LBA so we know that segment of the
13440 * transfer can use the CDB we select.
13442 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13443 (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13446 * The command will fit into the CDB type
13447 * specified by sd_cdbtab[i].
13449 cp = sd_cdbtab + i;
13452 * Call scsi_init_pkt so we can fill in the
13453 * CDB.
13455 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13456 bp, cp->sc_grpcode, un->un_status_len, 0,
13457 flags, callback, callback_arg);
13459 if (return_pktp != NULL) {
13462 * Return new value of pkt
13464 *pktpp = return_pktp;
13467 * To be safe, zero the CDB insuring there is
13468 * no leftover data from a previous command.
13470 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13473 * Handle partial DMA mapping
13475 if (return_pktp->pkt_resid != 0) {
13478 * Not going to xfer as many blocks as
13479 * originally expected
13481 blockcount -=
13482 SD_BYTES2TGTBLOCKS(un,
13483 return_pktp->pkt_resid);
13486 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13489 * Set command byte based on the CDB
13490 * type we matched.
13492 cdbp->scc_cmd = cp->sc_grpmask |
13493 ((bp->b_flags & B_READ) ?
13494 SCMD_READ : SCMD_WRITE);
13496 SD_FILL_SCSI1_LUN(un, return_pktp);
13499 * Fill in LBA and length
13501 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13502 (cp->sc_grpcode == CDB_GROUP4) ||
13503 (cp->sc_grpcode == CDB_GROUP0) ||
13504 (cp->sc_grpcode == CDB_GROUP5));
13506 if (cp->sc_grpcode == CDB_GROUP1) {
13507 FORMG1ADDR(cdbp, lba);
13508 FORMG1COUNT(cdbp, blockcount);
13509 return (0);
13510 } else if (cp->sc_grpcode == CDB_GROUP4) {
13511 FORMG4LONGADDR(cdbp, lba);
13512 FORMG4COUNT(cdbp, blockcount);
13513 return (0);
13514 } else if (cp->sc_grpcode == CDB_GROUP0) {
13515 FORMG0ADDR(cdbp, lba);
13516 FORMG0COUNT(cdbp, blockcount);
13517 return (0);
13518 } else if (cp->sc_grpcode == CDB_GROUP5) {
13519 FORMG5ADDR(cdbp, lba);
13520 FORMG5COUNT(cdbp, blockcount);
13521 return (0);
13525 * It should be impossible to not match one
13526 * of the CDB types above, so we should never
13527 * reach this point. Set the CDB command byte
13528 * to test-unit-ready to avoid writing
13529 * to somewhere we don't intend.
13531 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13532 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13533 } else {
13535 * Couldn't get scsi_pkt
13537 return (SD_PKT_ALLOC_FAILURE);
13543 * None of the available CDB types were suitable. This really
13544 * should never happen: on a 64 bit system we support
13545 * READ16/WRITE16 which will hold an entire 64 bit disk address
13546 * and on a 32 bit system we will refuse to bind to a device
13547 * larger than 2TB so addresses will never be larger than 32 bits.
13549 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13553 * Function: sd_setup_next_rw_pkt
13555 * Description: Setup packet for partial DMA transfers, except for the
13556 * initial transfer. sd_setup_rw_pkt should be used for
13557 * the initial transfer.
13559 * Context: Kernel thread and may be called from interrupt context.
13563 sd_setup_next_rw_pkt(struct sd_lun *un,
13564 struct scsi_pkt *pktp, struct buf *bp,
13565 diskaddr_t lba, uint32_t blockcount)
13567 uchar_t com;
13568 union scsi_cdb *cdbp;
13569 uchar_t cdb_group_id;
13571 ASSERT(pktp != NULL);
13572 ASSERT(pktp->pkt_cdbp != NULL);
13574 cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13575 com = cdbp->scc_cmd;
13576 cdb_group_id = CDB_GROUPID(com);
13578 ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13579 (cdb_group_id == CDB_GROUPID_1) ||
13580 (cdb_group_id == CDB_GROUPID_4) ||
13581 (cdb_group_id == CDB_GROUPID_5));
13584 * Move pkt to the next portion of the xfer.
13585 * func is NULL_FUNC so we do not have to release
13586 * the disk mutex here.
13588 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13589 NULL_FUNC, NULL) == pktp) {
13590 /* Success. Handle partial DMA */
13591 if (pktp->pkt_resid != 0) {
13592 blockcount -=
13593 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13596 cdbp->scc_cmd = com;
13597 SD_FILL_SCSI1_LUN(un, pktp);
13598 if (cdb_group_id == CDB_GROUPID_1) {
13599 FORMG1ADDR(cdbp, lba);
13600 FORMG1COUNT(cdbp, blockcount);
13601 return (0);
13602 } else if (cdb_group_id == CDB_GROUPID_4) {
13603 FORMG4LONGADDR(cdbp, lba);
13604 FORMG4COUNT(cdbp, blockcount);
13605 return (0);
13606 } else if (cdb_group_id == CDB_GROUPID_0) {
13607 FORMG0ADDR(cdbp, lba);
13608 FORMG0COUNT(cdbp, blockcount);
13609 return (0);
13610 } else if (cdb_group_id == CDB_GROUPID_5) {
13611 FORMG5ADDR(cdbp, lba);
13612 FORMG5COUNT(cdbp, blockcount);
13613 return (0);
13616 /* Unreachable */
13617 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13621 * Error setting up next portion of cmd transfer.
13622 * Something is definitely very wrong and this
13623 * should not happen.
13625 return (SD_PKT_ALLOC_FAILURE);
13629 * Function: sd_initpkt_for_uscsi
13631 * Description: Allocate and initialize for transport a scsi_pkt struct,
13632 * based upon the info specified in the given uscsi_cmd struct.
13634 * Return Code: SD_PKT_ALLOC_SUCCESS
13635 * SD_PKT_ALLOC_FAILURE
13636 * SD_PKT_ALLOC_FAILURE_NO_DMA
13637 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13639 * Context: Kernel thread and may be called from software interrupt context
13640 * as part of a sdrunout callback. This function may not block or
13641 * call routines that block
13644 static int
13645 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13647 struct uscsi_cmd *uscmd;
13648 struct sd_xbuf *xp;
13649 struct scsi_pkt *pktp;
13650 struct sd_lun *un;
13651 uint32_t flags = 0;
13653 ASSERT(bp != NULL);
13654 ASSERT(pktpp != NULL);
13655 xp = SD_GET_XBUF(bp);
13656 ASSERT(xp != NULL);
13657 un = SD_GET_UN(bp);
13658 ASSERT(un != NULL);
13659 ASSERT(mutex_owned(SD_MUTEX(un)));
13661 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13662 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13663 ASSERT(uscmd != NULL);
13665 SD_TRACE(SD_LOG_IO_CORE, un,
13666 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13669 * Allocate the scsi_pkt for the command.
13670 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13671 * during scsi_init_pkt time and will continue to use the
13672 * same path as long as the same scsi_pkt is used without
13673 * intervening scsi_dma_free(). Since uscsi command does
13674 * not call scsi_dmafree() before retry failed command, it
13675 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13676 * set such that scsi_vhci can use other available path for
13677 * retry. Besides, ucsci command does not allow DMA breakup,
13678 * so there is no need to set PKT_DMA_PARTIAL flag.
13680 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
13681 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13682 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13683 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
13684 - sizeof (struct scsi_extended_sense)), 0,
13685 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
13686 sdrunout, (caddr_t)un);
13687 } else {
13688 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13689 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13690 sizeof (struct scsi_arq_status), 0,
13691 (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
13692 sdrunout, (caddr_t)un);
13695 if (pktp == NULL) {
13696 *pktpp = NULL;
13698 * Set the driver state to RWAIT to indicate the driver
13699 * is waiting on resource allocations. The driver will not
13700 * suspend, pm_suspend, or detatch while the state is RWAIT.
13702 New_state(un, SD_STATE_RWAIT);
13704 SD_ERROR(SD_LOG_IO_CORE, un,
13705 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13707 if ((bp->b_flags & B_ERROR) != 0) {
13708 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13710 return (SD_PKT_ALLOC_FAILURE);
13714 * We do not do DMA breakup for USCSI commands, so return failure
13715 * here if all the needed DMA resources were not allocated.
13717 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13718 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13719 scsi_destroy_pkt(pktp);
13720 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13721 "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13722 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13725 /* Init the cdb from the given uscsi struct */
13726 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13727 uscmd->uscsi_cdb[0], 0, 0, 0);
13729 SD_FILL_SCSI1_LUN(un, pktp);
13732 * Set up the optional USCSI flags. See the uscsi (7I) man page
13733 * for listing of the supported flags.
13736 if (uscmd->uscsi_flags & USCSI_SILENT) {
13737 flags |= FLAG_SILENT;
13740 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13741 flags |= FLAG_DIAGNOSE;
13744 if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13745 flags |= FLAG_ISOLATE;
13748 if (un->un_f_is_fibre == FALSE) {
13749 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13750 flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13755 * Set the pkt flags here so we save time later.
13756 * Note: These flags are NOT in the uscsi man page!!!
13758 if (uscmd->uscsi_flags & USCSI_HEAD) {
13759 flags |= FLAG_HEAD;
13762 if (uscmd->uscsi_flags & USCSI_NOINTR) {
13763 flags |= FLAG_NOINTR;
13767 * For tagged queueing, things get a bit complicated.
13768 * Check first for head of queue and last for ordered queue.
13769 * If neither head nor order, use the default driver tag flags.
13771 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13772 if (uscmd->uscsi_flags & USCSI_HTAG) {
13773 flags |= FLAG_HTAG;
13774 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
13775 flags |= FLAG_OTAG;
13776 } else {
13777 flags |= un->un_tagflags & FLAG_TAGMASK;
13781 if (uscmd->uscsi_flags & USCSI_NODISCON) {
13782 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13785 pktp->pkt_flags = flags;
13787 /* Transfer uscsi information to scsi_pkt */
13788 (void) scsi_uscsi_pktinit(uscmd, pktp);
13790 /* Copy the caller's CDB into the pkt... */
13791 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13793 if (uscmd->uscsi_timeout == 0) {
13794 pktp->pkt_time = un->un_uscsi_timeout;
13795 } else {
13796 pktp->pkt_time = uscmd->uscsi_timeout;
13799 /* need it later to identify USCSI request in sdintr */
13800 xp->xb_pkt_flags |= SD_XB_USCSICMD;
13802 xp->xb_sense_resid = uscmd->uscsi_rqresid;
13804 pktp->pkt_private = bp;
13805 pktp->pkt_comp = sdintr;
13806 *pktpp = pktp;
13808 SD_TRACE(SD_LOG_IO_CORE, un,
13809 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13811 return (SD_PKT_ALLOC_SUCCESS);
13816 * Function: sd_destroypkt_for_uscsi
13818 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13819 * IOs.. Also saves relevant info into the associated uscsi_cmd
13820 * struct.
13822 * Context: May be called under interrupt context
13825 static void
13826 sd_destroypkt_for_uscsi(struct buf *bp)
13828 struct uscsi_cmd *uscmd;
13829 struct sd_xbuf *xp;
13830 struct scsi_pkt *pktp;
13831 struct sd_lun *un;
13832 struct sd_uscsi_info *suip;
13834 ASSERT(bp != NULL);
13835 xp = SD_GET_XBUF(bp);
13836 ASSERT(xp != NULL);
13837 un = SD_GET_UN(bp);
13838 ASSERT(un != NULL);
13839 ASSERT(!mutex_owned(SD_MUTEX(un)));
13840 pktp = SD_GET_PKTP(bp);
13841 ASSERT(pktp != NULL);
13843 SD_TRACE(SD_LOG_IO_CORE, un,
13844 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13846 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13847 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13848 ASSERT(uscmd != NULL);
13850 /* Save the status and the residual into the uscsi_cmd struct */
13851 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
13852 uscmd->uscsi_resid = bp->b_resid;
13854 /* Transfer scsi_pkt information to uscsi */
13855 (void) scsi_uscsi_pktfini(pktp, uscmd);
13858 * If enabled, copy any saved sense data into the area specified
13859 * by the uscsi command.
13861 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
13862 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
13864 * Note: uscmd->uscsi_rqbuf should always point to a buffer
13865 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
13867 uscmd->uscsi_rqstatus = xp->xb_sense_status;
13868 uscmd->uscsi_rqresid = xp->xb_sense_resid;
13869 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
13870 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
13871 MAX_SENSE_LENGTH);
13872 } else {
13873 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
13874 SENSE_LENGTH);
13878 * The following assignments are for SCSI FMA.
13880 ASSERT(xp->xb_private != NULL);
13881 suip = (struct sd_uscsi_info *)xp->xb_private;
13882 suip->ui_pkt_reason = pktp->pkt_reason;
13883 suip->ui_pkt_state = pktp->pkt_state;
13884 suip->ui_pkt_statistics = pktp->pkt_statistics;
13885 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
13887 /* We are done with the scsi_pkt; free it now */
13888 ASSERT(SD_GET_PKTP(bp) != NULL);
13889 scsi_destroy_pkt(SD_GET_PKTP(bp));
13891 SD_TRACE(SD_LOG_IO_CORE, un,
13892 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
13897 * Function: sd_bioclone_alloc
13899 * Description: Allocate a buf(9S) and init it as per the given buf
13900 * and the various arguments. The associated sd_xbuf
13901 * struct is (nearly) duplicated. The struct buf *bp
13902 * argument is saved in new_xp->xb_private.
13904 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
13905 * datalen - size of data area for the shadow bp
13906 * blkno - starting LBA
13907 * func - function pointer for b_iodone in the shadow buf. (May
13908 * be NULL if none.)
13910 * Return Code: Pointer to allocates buf(9S) struct
13912 * Context: Can sleep.
13915 static struct buf *
13916 sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
13917 int (*func)(struct buf *))
13919 struct sd_lun *un;
13920 struct sd_xbuf *xp;
13921 struct sd_xbuf *new_xp;
13922 struct buf *new_bp;
13924 ASSERT(bp != NULL);
13925 xp = SD_GET_XBUF(bp);
13926 ASSERT(xp != NULL);
13927 un = SD_GET_UN(bp);
13928 ASSERT(un != NULL);
13929 ASSERT(!mutex_owned(SD_MUTEX(un)));
13931 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
13932 NULL, KM_SLEEP);
13934 new_bp->b_lblkno = blkno;
13937 * Allocate an xbuf for the shadow bp and copy the contents of the
13938 * original xbuf into it.
13940 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13941 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13944 * The given bp is automatically saved in the xb_private member
13945 * of the new xbuf. Callers are allowed to depend on this.
13947 new_xp->xb_private = bp;
13949 new_bp->b_private = new_xp;
13951 return (new_bp);
13955 * Function: sd_shadow_buf_alloc
13957 * Description: Allocate a buf(9S) and init it as per the given buf
13958 * and the various arguments. The associated sd_xbuf
13959 * struct is (nearly) duplicated. The struct buf *bp
13960 * argument is saved in new_xp->xb_private.
13962 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
13963 * datalen - size of data area for the shadow bp
13964 * bflags - B_READ or B_WRITE (pseudo flag)
13965 * blkno - starting LBA
13966 * func - function pointer for b_iodone in the shadow buf. (May
13967 * be NULL if none.)
13969 * Return Code: Pointer to allocates buf(9S) struct
13971 * Context: Can sleep.
13974 static struct buf *
13975 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
13976 daddr_t blkno, int (*func)(struct buf *))
13978 struct sd_lun *un;
13979 struct sd_xbuf *xp;
13980 struct sd_xbuf *new_xp;
13981 struct buf *new_bp;
13983 ASSERT(bp != NULL);
13984 xp = SD_GET_XBUF(bp);
13985 ASSERT(xp != NULL);
13986 un = SD_GET_UN(bp);
13987 VERIFY(un != NULL);
13988 ASSERT(!mutex_owned(SD_MUTEX(un)));
13990 if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
13991 bp_mapin(bp);
13994 bflags &= (B_READ | B_WRITE);
13995 #if defined(__i386) || defined(__amd64)
13996 new_bp = getrbuf(KM_SLEEP);
13997 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
13998 new_bp->b_bcount = datalen;
13999 new_bp->b_flags = bflags |
14000 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14001 #else
14002 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14003 datalen, bflags, SLEEP_FUNC, NULL);
14004 #endif
14005 new_bp->av_forw = NULL;
14006 new_bp->av_back = NULL;
14007 new_bp->b_dev = bp->b_dev;
14008 new_bp->b_blkno = blkno;
14009 new_bp->b_iodone = func;
14010 new_bp->b_edev = bp->b_edev;
14011 new_bp->b_resid = 0;
14013 /* We need to preserve the B_FAILFAST flag */
14014 if (bp->b_flags & B_FAILFAST) {
14015 new_bp->b_flags |= B_FAILFAST;
14019 * Allocate an xbuf for the shadow bp and copy the contents of the
14020 * original xbuf into it.
14022 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14023 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14025 /* Need later to copy data between the shadow buf & original buf! */
14026 new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14029 * The given bp is automatically saved in the xb_private member
14030 * of the new xbuf. Callers are allowed to depend on this.
14032 new_xp->xb_private = bp;
14034 new_bp->b_private = new_xp;
14036 return (new_bp);
14040 * Function: sd_bioclone_free
14042 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14043 * in the larger than partition operation.
14045 * Context: May be called under interrupt context
14048 static void
14049 sd_bioclone_free(struct buf *bp)
14051 struct sd_xbuf *xp;
14053 ASSERT(bp != NULL);
14054 xp = SD_GET_XBUF(bp);
14055 ASSERT(xp != NULL);
14058 * Call bp_mapout() before freeing the buf, in case a lower
14059 * layer or HBA had done a bp_mapin(). we must do this here
14060 * as we are the "originator" of the shadow buf.
14062 bp_mapout(bp);
14065 * Null out b_iodone before freeing the bp, to ensure that the driver
14066 * never gets confused by a stale value in this field. (Just a little
14067 * extra defensiveness here.)
14069 bp->b_iodone = NULL;
14071 freerbuf(bp);
14073 kmem_free(xp, sizeof (struct sd_xbuf));
14077 * Function: sd_shadow_buf_free
14079 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14081 * Context: May be called under interrupt context
14084 static void
14085 sd_shadow_buf_free(struct buf *bp)
14087 struct sd_xbuf *xp;
14089 ASSERT(bp != NULL);
14090 xp = SD_GET_XBUF(bp);
14091 ASSERT(xp != NULL);
14095 * Null out b_iodone before freeing the bp, to ensure that the driver
14096 * never gets confused by a stale value in this field. (Just a little
14097 * extra defensiveness here.)
14099 bp->b_iodone = NULL;
14101 #if defined(__i386) || defined(__amd64)
14102 kmem_free(bp->b_un.b_addr, bp->b_bcount);
14103 freerbuf(bp);
14104 #else
14105 scsi_free_consistent_buf(bp);
14106 #endif
14108 kmem_free(xp, sizeof (struct sd_xbuf));
14113 * Function: sd_print_transport_rejected_message
14115 * Description: This implements the ludicrously complex rules for printing
14116 * a "transport rejected" message. This is to address the
14117 * specific problem of having a flood of this error message
14118 * produced when a failover occurs.
14120 * Context: Any.
14123 static void
14124 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14125 int code)
14127 ASSERT(un != NULL);
14128 ASSERT(mutex_owned(SD_MUTEX(un)));
14129 ASSERT(xp != NULL);
14132 * Print the "transport rejected" message under the following
14133 * conditions:
14135 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14136 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14137 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14138 * printed the FIRST time a TRAN_FATAL_ERROR is returned from
14139 * scsi_transport(9F) (which indicates that the target might have
14140 * gone off-line). This uses the un->un_tran_fatal_count
14141 * count, which is incremented whenever a TRAN_FATAL_ERROR is
14142 * received, and reset to zero whenver a TRAN_ACCEPT is returned
14143 * from scsi_transport().
14145 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14146 * the preceeding cases in order for the message to be printed.
14148 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14149 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14150 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14151 (code != TRAN_FATAL_ERROR) ||
14152 (un->un_tran_fatal_count == 1)) {
14153 switch (code) {
14154 case TRAN_BADPKT:
14155 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14156 "transport rejected bad packet\n");
14157 break;
14158 case TRAN_FATAL_ERROR:
14159 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14160 "transport rejected fatal error\n");
14161 break;
14162 default:
14163 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14164 "transport rejected (%d)\n", code);
14165 break;
14173 * Function: sd_add_buf_to_waitq
14175 * Description: Add the given buf(9S) struct to the wait queue for the
14176 * instance. If sorting is enabled, then the buf is added
14177 * to the queue via an elevator sort algorithm (a la
14178 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key.
14179 * If sorting is not enabled, then the buf is just added
14180 * to the end of the wait queue.
14182 * Return Code: void
14184 * Context: Does not sleep/block, therefore technically can be called
14185 * from any context. However if sorting is enabled then the
14186 * execution time is indeterminate, and may take long if
14187 * the wait queue grows large.
14190 static void
14191 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14193 struct buf *ap;
14195 ASSERT(bp != NULL);
14196 ASSERT(un != NULL);
14197 ASSERT(mutex_owned(SD_MUTEX(un)));
14199 /* If the queue is empty, add the buf as the only entry & return. */
14200 if (un->un_waitq_headp == NULL) {
14201 ASSERT(un->un_waitq_tailp == NULL);
14202 un->un_waitq_headp = un->un_waitq_tailp = bp;
14203 bp->av_forw = NULL;
14204 return;
14207 ASSERT(un->un_waitq_tailp != NULL);
14210 * If sorting is disabled, just add the buf to the tail end of
14211 * the wait queue and return.
14213 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14214 un->un_waitq_tailp->av_forw = bp;
14215 un->un_waitq_tailp = bp;
14216 bp->av_forw = NULL;
14217 return;
14221 * Sort thru the list of requests currently on the wait queue
14222 * and add the new buf request at the appropriate position.
14224 * The un->un_waitq_headp is an activity chain pointer on which
14225 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14226 * first queue holds those requests which are positioned after
14227 * the current SD_GET_BLKNO() (in the first request); the second holds
14228 * requests which came in after their SD_GET_BLKNO() number was passed.
14229 * Thus we implement a one way scan, retracting after reaching
14230 * the end of the drive to the first request on the second
14231 * queue, at which time it becomes the first queue.
14232 * A one-way scan is natural because of the way UNIX read-ahead
14233 * blocks are allocated.
14235 * If we lie after the first request, then we must locate the
14236 * second request list and add ourselves to it.
14238 ap = un->un_waitq_headp;
14239 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14240 while (ap->av_forw != NULL) {
14242 * Look for an "inversion" in the (normally
14243 * ascending) block numbers. This indicates
14244 * the start of the second request list.
14246 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14248 * Search the second request list for the
14249 * first request at a larger block number.
14250 * We go before that; however if there is
14251 * no such request, we go at the end.
14253 do {
14254 if (SD_GET_BLKNO(bp) <
14255 SD_GET_BLKNO(ap->av_forw)) {
14256 goto insert;
14258 ap = ap->av_forw;
14259 } while (ap->av_forw != NULL);
14260 goto insert; /* after last */
14262 ap = ap->av_forw;
14266 * No inversions... we will go after the last, and
14267 * be the first request in the second request list.
14269 goto insert;
14273 * Request is at/after the current request...
14274 * sort in the first request list.
14276 while (ap->av_forw != NULL) {
14278 * We want to go after the current request (1) if
14279 * there is an inversion after it (i.e. it is the end
14280 * of the first request list), or (2) if the next
14281 * request is a larger block no. than our request.
14283 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14284 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14285 goto insert;
14287 ap = ap->av_forw;
14291 * Neither a second list nor a larger request, therefore
14292 * we go at the end of the first list (which is the same
14293 * as the end of the whole schebang).
14295 insert:
14296 bp->av_forw = ap->av_forw;
14297 ap->av_forw = bp;
14300 * If we inserted onto the tail end of the waitq, make sure the
14301 * tail pointer is updated.
14303 if (ap == un->un_waitq_tailp) {
14304 un->un_waitq_tailp = bp;
14310 * Function: sd_start_cmds
14312 * Description: Remove and transport cmds from the driver queues.
14314 * Arguments: un - pointer to the unit (soft state) struct for the target.
14316 * immed_bp - ptr to a buf to be transported immediately. Only
14317 * the immed_bp is transported; bufs on the waitq are not
14318 * processed and the un_retry_bp is not checked. If immed_bp is
14319 * NULL, then normal queue processing is performed.
14321 * Context: May be called from kernel thread context, interrupt context,
14322 * or runout callback context. This function may not block or
14323 * call routines that block.
14326 static void
14327 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14329 struct sd_xbuf *xp;
14330 struct buf *bp;
14331 void (*statp)(kstat_io_t *);
14332 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14333 void (*saved_statp)(kstat_io_t *);
14334 #endif
14335 int rval;
14336 struct sd_fm_internal *sfip = NULL;
14338 ASSERT(un != NULL);
14339 ASSERT(mutex_owned(SD_MUTEX(un)));
14340 ASSERT(un->un_ncmds_in_transport >= 0);
14341 ASSERT(un->un_throttle >= 0);
14343 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14345 do {
14346 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14347 saved_statp = NULL;
14348 #endif
14351 * If we are syncing or dumping, fail the command to
14352 * avoid recursively calling back into scsi_transport().
14353 * The dump I/O itself uses a separate code path so this
14354 * only prevents non-dump I/O from being sent while dumping.
14355 * File system sync takes place before dumping begins.
14356 * During panic, filesystem I/O is allowed provided
14357 * un_in_callback is <= 1. This is to prevent recursion
14358 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14359 * sd_start_cmds and so on. See panic.c for more information
14360 * about the states the system can be in during panic.
14362 if ((un->un_state == SD_STATE_DUMPING) ||
14363 (ddi_in_panic() && (un->un_in_callback > 1))) {
14364 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14365 "sd_start_cmds: panicking\n");
14366 goto exit;
14369 if ((bp = immed_bp) != NULL) {
14371 * We have a bp that must be transported immediately.
14372 * It's OK to transport the immed_bp here without doing
14373 * the throttle limit check because the immed_bp is
14374 * always used in a retry/recovery case. This means
14375 * that we know we are not at the throttle limit by
14376 * virtue of the fact that to get here we must have
14377 * already gotten a command back via sdintr(). This also
14378 * relies on (1) the command on un_retry_bp preventing
14379 * further commands from the waitq from being issued;
14380 * and (2) the code in sd_retry_command checking the
14381 * throttle limit before issuing a delayed or immediate
14382 * retry. This holds even if the throttle limit is
14383 * currently ratcheted down from its maximum value.
14385 statp = kstat_runq_enter;
14386 if (bp == un->un_retry_bp) {
14387 ASSERT((un->un_retry_statp == NULL) ||
14388 (un->un_retry_statp == kstat_waitq_enter) ||
14389 (un->un_retry_statp ==
14390 kstat_runq_back_to_waitq));
14392 * If the waitq kstat was incremented when
14393 * sd_set_retry_bp() queued this bp for a retry,
14394 * then we must set up statp so that the waitq
14395 * count will get decremented correctly below.
14396 * Also we must clear un->un_retry_statp to
14397 * ensure that we do not act on a stale value
14398 * in this field.
14400 if ((un->un_retry_statp == kstat_waitq_enter) ||
14401 (un->un_retry_statp ==
14402 kstat_runq_back_to_waitq)) {
14403 statp = kstat_waitq_to_runq;
14405 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14406 saved_statp = un->un_retry_statp;
14407 #endif
14408 un->un_retry_statp = NULL;
14410 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14411 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14412 "un_throttle:%d un_ncmds_in_transport:%d\n",
14413 un, un->un_retry_bp, un->un_throttle,
14414 un->un_ncmds_in_transport);
14415 } else {
14416 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14417 "processing priority bp:0x%p\n", bp);
14420 } else if ((bp = un->un_waitq_headp) != NULL) {
14422 * A command on the waitq is ready to go, but do not
14423 * send it if:
14425 * (1) the throttle limit has been reached, or
14426 * (2) a retry is pending, or
14427 * (3) a START_STOP_UNIT callback pending, or
14428 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14429 * command is pending.
14431 * For all of these conditions, IO processing will
14432 * restart after the condition is cleared.
14434 if (un->un_ncmds_in_transport >= un->un_throttle) {
14435 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14436 "sd_start_cmds: exiting, "
14437 "throttle limit reached!\n");
14438 goto exit;
14440 if (un->un_retry_bp != NULL) {
14441 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14442 "sd_start_cmds: exiting, retry pending!\n");
14443 goto exit;
14445 if (un->un_startstop_timeid != NULL) {
14446 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14447 "sd_start_cmds: exiting, "
14448 "START_STOP pending!\n");
14449 goto exit;
14451 if (un->un_direct_priority_timeid != NULL) {
14452 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14453 "sd_start_cmds: exiting, "
14454 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14455 goto exit;
14458 /* Dequeue the command */
14459 un->un_waitq_headp = bp->av_forw;
14460 if (un->un_waitq_headp == NULL) {
14461 un->un_waitq_tailp = NULL;
14463 bp->av_forw = NULL;
14464 statp = kstat_waitq_to_runq;
14465 SD_TRACE(SD_LOG_IO_CORE, un,
14466 "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14468 } else {
14469 /* No work to do so bail out now */
14470 SD_TRACE(SD_LOG_IO_CORE, un,
14471 "sd_start_cmds: no more work, exiting!\n");
14472 goto exit;
14476 * Reset the state to normal. This is the mechanism by which
14477 * the state transitions from either SD_STATE_RWAIT or
14478 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14479 * If state is SD_STATE_PM_CHANGING then this command is
14480 * part of the device power control and the state must
14481 * not be put back to normal. Doing so would would
14482 * allow new commands to proceed when they shouldn't,
14483 * the device may be going off.
14485 if ((un->un_state != SD_STATE_SUSPENDED) &&
14486 (un->un_state != SD_STATE_PM_CHANGING)) {
14487 New_state(un, SD_STATE_NORMAL);
14490 xp = SD_GET_XBUF(bp);
14491 ASSERT(xp != NULL);
14493 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14495 * Allocate the scsi_pkt if we need one, or attach DMA
14496 * resources if we have a scsi_pkt that needs them. The
14497 * latter should only occur for commands that are being
14498 * retried.
14500 if ((xp->xb_pktp == NULL) ||
14501 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14502 #else
14503 if (xp->xb_pktp == NULL) {
14504 #endif
14506 * There is no scsi_pkt allocated for this buf. Call
14507 * the initpkt function to allocate & init one.
14509 * The scsi_init_pkt runout callback functionality is
14510 * implemented as follows:
14512 * 1) The initpkt function always calls
14513 * scsi_init_pkt(9F) with sdrunout specified as the
14514 * callback routine.
14515 * 2) A successful packet allocation is initialized and
14516 * the I/O is transported.
14517 * 3) The I/O associated with an allocation resource
14518 * failure is left on its queue to be retried via
14519 * runout or the next I/O.
14520 * 4) The I/O associated with a DMA error is removed
14521 * from the queue and failed with EIO. Processing of
14522 * the transport queues is also halted to be
14523 * restarted via runout or the next I/O.
14524 * 5) The I/O associated with a CDB size or packet
14525 * size error is removed from the queue and failed
14526 * with EIO. Processing of the transport queues is
14527 * continued.
14529 * Note: there is no interface for canceling a runout
14530 * callback. To prevent the driver from detaching or
14531 * suspending while a runout is pending the driver
14532 * state is set to SD_STATE_RWAIT
14534 * Note: using the scsi_init_pkt callback facility can
14535 * result in an I/O request persisting at the head of
14536 * the list which cannot be satisfied even after
14537 * multiple retries. In the future the driver may
14538 * implement some kind of maximum runout count before
14539 * failing an I/O.
14541 * Note: the use of funcp below may seem superfluous,
14542 * but it helps warlock figure out the correct
14543 * initpkt function calls (see [s]sd.wlcmd).
14545 struct scsi_pkt *pktp;
14546 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14548 ASSERT(bp != un->un_rqs_bp);
14550 funcp = sd_initpkt_map[xp->xb_chain_iostart];
14551 switch ((*funcp)(bp, &pktp)) {
14552 case SD_PKT_ALLOC_SUCCESS:
14553 xp->xb_pktp = pktp;
14554 SD_TRACE(SD_LOG_IO_CORE, un,
14555 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14556 pktp);
14557 goto got_pkt;
14559 case SD_PKT_ALLOC_FAILURE:
14561 * Temporary (hopefully) resource depletion.
14562 * Since retries and RQS commands always have a
14563 * scsi_pkt allocated, these cases should never
14564 * get here. So the only cases this needs to
14565 * handle is a bp from the waitq (which we put
14566 * back onto the waitq for sdrunout), or a bp
14567 * sent as an immed_bp (which we just fail).
14569 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14570 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14572 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14574 if (bp == immed_bp) {
14576 * If SD_XB_DMA_FREED is clear, then
14577 * this is a failure to allocate a
14578 * scsi_pkt, and we must fail the
14579 * command.
14581 if ((xp->xb_pkt_flags &
14582 SD_XB_DMA_FREED) == 0) {
14583 break;
14587 * If this immediate command is NOT our
14588 * un_retry_bp, then we must fail it.
14590 if (bp != un->un_retry_bp) {
14591 break;
14595 * We get here if this cmd is our
14596 * un_retry_bp that was DMAFREED, but
14597 * scsi_init_pkt() failed to reallocate
14598 * DMA resources when we attempted to
14599 * retry it. This can happen when an
14600 * mpxio failover is in progress, but
14601 * we don't want to just fail the
14602 * command in this case.
14604 * Use timeout(9F) to restart it after
14605 * a 100ms delay. We don't want to
14606 * let sdrunout() restart it, because
14607 * sdrunout() is just supposed to start
14608 * commands that are sitting on the
14609 * wait queue. The un_retry_bp stays
14610 * set until the command completes, but
14611 * sdrunout can be called many times
14612 * before that happens. Since sdrunout
14613 * cannot tell if the un_retry_bp is
14614 * already in the transport, it could
14615 * end up calling scsi_transport() for
14616 * the un_retry_bp multiple times.
14618 * Also: don't schedule the callback
14619 * if some other callback is already
14620 * pending.
14622 if (un->un_retry_statp == NULL) {
14624 * restore the kstat pointer to
14625 * keep kstat counts coherent
14626 * when we do retry the command.
14628 un->un_retry_statp =
14629 saved_statp;
14632 if ((un->un_startstop_timeid == NULL) &&
14633 (un->un_retry_timeid == NULL) &&
14634 (un->un_direct_priority_timeid ==
14635 NULL)) {
14637 un->un_retry_timeid =
14638 timeout(
14639 sd_start_retry_command,
14640 un, SD_RESTART_TIMEOUT);
14642 goto exit;
14645 #else
14646 if (bp == immed_bp) {
14647 break; /* Just fail the command */
14649 #endif
14651 /* Add the buf back to the head of the waitq */
14652 bp->av_forw = un->un_waitq_headp;
14653 un->un_waitq_headp = bp;
14654 if (un->un_waitq_tailp == NULL) {
14655 un->un_waitq_tailp = bp;
14657 goto exit;
14659 case SD_PKT_ALLOC_FAILURE_NO_DMA:
14661 * HBA DMA resource failure. Fail the command
14662 * and continue processing of the queues.
14664 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14665 "sd_start_cmds: "
14666 "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14667 break;
14669 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14671 * Note:x86: Partial DMA mapping not supported
14672 * for USCSI commands, and all the needed DMA
14673 * resources were not allocated.
14675 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14676 "sd_start_cmds: "
14677 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14678 break;
14680 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14682 * Note:x86: Request cannot fit into CDB based
14683 * on lba and len.
14685 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14686 "sd_start_cmds: "
14687 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14688 break;
14690 default:
14691 /* Should NEVER get here! */
14692 panic("scsi_initpkt error");
14693 /*NOTREACHED*/
14697 * Fatal error in allocating a scsi_pkt for this buf.
14698 * Update kstats & return the buf with an error code.
14699 * We must use sd_return_failed_command_no_restart() to
14700 * avoid a recursive call back into sd_start_cmds().
14701 * However this also means that we must keep processing
14702 * the waitq here in order to avoid stalling.
14704 if (statp == kstat_waitq_to_runq) {
14705 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14707 sd_return_failed_command_no_restart(un, bp, EIO);
14708 if (bp == immed_bp) {
14709 /* immed_bp is gone by now, so clear this */
14710 immed_bp = NULL;
14712 continue;
14714 got_pkt:
14715 if (bp == immed_bp) {
14716 /* goto the head of the class.... */
14717 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14720 un->un_ncmds_in_transport++;
14721 SD_UPDATE_KSTATS(un, statp, bp);
14724 * Call scsi_transport() to send the command to the target.
14725 * According to SCSA architecture, we must drop the mutex here
14726 * before calling scsi_transport() in order to avoid deadlock.
14727 * Note that the scsi_pkt's completion routine can be executed
14728 * (from interrupt context) even before the call to
14729 * scsi_transport() returns.
14731 SD_TRACE(SD_LOG_IO_CORE, un,
14732 "sd_start_cmds: calling scsi_transport()\n");
14733 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14735 mutex_exit(SD_MUTEX(un));
14736 rval = scsi_transport(xp->xb_pktp);
14737 mutex_enter(SD_MUTEX(un));
14739 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14740 "sd_start_cmds: scsi_transport() returned %d\n", rval);
14742 switch (rval) {
14743 case TRAN_ACCEPT:
14744 /* Clear this with every pkt accepted by the HBA */
14745 un->un_tran_fatal_count = 0;
14746 break; /* Success; try the next cmd (if any) */
14748 case TRAN_BUSY:
14749 un->un_ncmds_in_transport--;
14750 ASSERT(un->un_ncmds_in_transport >= 0);
14753 * Don't retry request sense, the sense data
14754 * is lost when another request is sent.
14755 * Free up the rqs buf and retry
14756 * the original failed cmd. Update kstat.
14758 if (bp == un->un_rqs_bp) {
14759 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14760 bp = sd_mark_rqs_idle(un, xp);
14761 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14762 NULL, NULL, EIO, un->un_busy_timeout / 500,
14763 kstat_waitq_enter);
14764 goto exit;
14767 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14769 * Free the DMA resources for the scsi_pkt. This will
14770 * allow mpxio to select another path the next time
14771 * we call scsi_transport() with this scsi_pkt.
14772 * See sdintr() for the rationalization behind this.
14774 if ((un->un_f_is_fibre == TRUE) &&
14775 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14776 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14777 scsi_dmafree(xp->xb_pktp);
14778 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14780 #endif
14782 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14784 * Commands that are SD_PATH_DIRECT_PRIORITY
14785 * are for error recovery situations. These do
14786 * not use the normal command waitq, so if they
14787 * get a TRAN_BUSY we cannot put them back onto
14788 * the waitq for later retry. One possible
14789 * problem is that there could already be some
14790 * other command on un_retry_bp that is waiting
14791 * for this one to complete, so we would be
14792 * deadlocked if we put this command back onto
14793 * the waitq for later retry (since un_retry_bp
14794 * must complete before the driver gets back to
14795 * commands on the waitq).
14797 * To avoid deadlock we must schedule a callback
14798 * that will restart this command after a set
14799 * interval. This should keep retrying for as
14800 * long as the underlying transport keeps
14801 * returning TRAN_BUSY (just like for other
14802 * commands). Use the same timeout interval as
14803 * for the ordinary TRAN_BUSY retry.
14805 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14806 "sd_start_cmds: scsi_transport() returned "
14807 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14809 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14810 un->un_direct_priority_timeid =
14811 timeout(sd_start_direct_priority_command,
14812 bp, un->un_busy_timeout / 500);
14814 goto exit;
14818 * For TRAN_BUSY, we want to reduce the throttle value,
14819 * unless we are retrying a command.
14821 if (bp != un->un_retry_bp) {
14822 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14826 * Set up the bp to be tried again 10 ms later.
14827 * Note:x86: Is there a timeout value in the sd_lun
14828 * for this condition?
14830 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
14831 kstat_runq_back_to_waitq);
14832 goto exit;
14834 case TRAN_FATAL_ERROR:
14835 un->un_tran_fatal_count++;
14836 /* FALLTHRU */
14838 case TRAN_BADPKT:
14839 default:
14840 un->un_ncmds_in_transport--;
14841 ASSERT(un->un_ncmds_in_transport >= 0);
14844 * If this is our REQUEST SENSE command with a
14845 * transport error, we must get back the pointers
14846 * to the original buf, and mark the REQUEST
14847 * SENSE command as "available".
14849 if (bp == un->un_rqs_bp) {
14850 bp = sd_mark_rqs_idle(un, xp);
14851 xp = SD_GET_XBUF(bp);
14852 } else {
14854 * Legacy behavior: do not update transport
14855 * error count for request sense commands.
14857 SD_UPDATE_ERRSTATS(un, sd_transerrs);
14860 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14861 sd_print_transport_rejected_message(un, xp, rval);
14864 * This command will be terminated by SD driver due
14865 * to a fatal transport error. We should post
14866 * ereport.io.scsi.cmd.disk.tran with driver-assessment
14867 * of "fail" for any command to indicate this
14868 * situation.
14870 if (xp->xb_ena > 0) {
14871 ASSERT(un->un_fm_private != NULL);
14872 sfip = un->un_fm_private;
14873 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
14874 sd_ssc_extract_info(&sfip->fm_ssc, un,
14875 xp->xb_pktp, bp, xp);
14876 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
14880 * We must use sd_return_failed_command_no_restart() to
14881 * avoid a recursive call back into sd_start_cmds().
14882 * However this also means that we must keep processing
14883 * the waitq here in order to avoid stalling.
14885 sd_return_failed_command_no_restart(un, bp, EIO);
14888 * Notify any threads waiting in sd_ddi_suspend() that
14889 * a command completion has occurred.
14891 if (un->un_state == SD_STATE_SUSPENDED) {
14892 cv_broadcast(&un->un_disk_busy_cv);
14895 if (bp == immed_bp) {
14896 /* immed_bp is gone by now, so clear this */
14897 immed_bp = NULL;
14899 break;
14902 } while (immed_bp == NULL);
14904 exit:
14905 ASSERT(mutex_owned(SD_MUTEX(un)));
14906 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
14911 * Function: sd_return_command
14913 * Description: Returns a command to its originator (with or without an
14914 * error). Also starts commands waiting to be transported
14915 * to the target.
14917 * Context: May be called from interrupt, kernel, or timeout context
14920 static void
14921 sd_return_command(struct sd_lun *un, struct buf *bp)
14923 struct sd_xbuf *xp;
14924 struct scsi_pkt *pktp;
14925 struct sd_fm_internal *sfip;
14927 ASSERT(bp != NULL);
14928 ASSERT(un != NULL);
14929 ASSERT(mutex_owned(SD_MUTEX(un)));
14930 ASSERT(bp != un->un_rqs_bp);
14931 xp = SD_GET_XBUF(bp);
14932 ASSERT(xp != NULL);
14934 pktp = SD_GET_PKTP(bp);
14935 sfip = (struct sd_fm_internal *)un->un_fm_private;
14936 ASSERT(sfip != NULL);
14938 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
14941 * Note: check for the "sdrestart failed" case.
14943 if ((un->un_partial_dma_supported == 1) &&
14944 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
14945 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
14946 (xp->xb_pktp->pkt_resid == 0)) {
14948 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
14950 * Successfully set up next portion of cmd
14951 * transfer, try sending it
14953 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
14954 NULL, NULL, 0, (clock_t)0, NULL);
14955 sd_start_cmds(un, NULL);
14956 return; /* Note:x86: need a return here? */
14961 * If this is the failfast bp, clear it from un_failfast_bp. This
14962 * can happen if upon being re-tried the failfast bp either
14963 * succeeded or encountered another error (possibly even a different
14964 * error than the one that precipitated the failfast state, but in
14965 * that case it would have had to exhaust retries as well). Regardless,
14966 * this should not occur whenever the instance is in the active
14967 * failfast state.
14969 if (bp == un->un_failfast_bp) {
14970 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14971 un->un_failfast_bp = NULL;
14975 * Clear the failfast state upon successful completion of ANY cmd.
14977 if (bp->b_error == 0) {
14978 un->un_failfast_state = SD_FAILFAST_INACTIVE;
14980 * If this is a successful command, but used to be retried,
14981 * we will take it as a recovered command and post an
14982 * ereport with driver-assessment of "recovered".
14984 if (xp->xb_ena > 0) {
14985 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
14986 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
14988 } else {
14990 * If this is a failed non-USCSI command we will post an
14991 * ereport with driver-assessment set accordingly("fail" or
14992 * "fatal").
14994 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
14995 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
14996 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15001 * This is used if the command was retried one or more times. Show that
15002 * we are done with it, and allow processing of the waitq to resume.
15004 if (bp == un->un_retry_bp) {
15005 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15006 "sd_return_command: un:0x%p: "
15007 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15008 un->un_retry_bp = NULL;
15009 un->un_retry_statp = NULL;
15012 SD_UPDATE_RDWR_STATS(un, bp);
15013 SD_UPDATE_PARTITION_STATS(un, bp);
15015 switch (un->un_state) {
15016 case SD_STATE_SUSPENDED:
15018 * Notify any threads waiting in sd_ddi_suspend() that
15019 * a command completion has occurred.
15021 cv_broadcast(&un->un_disk_busy_cv);
15022 break;
15023 default:
15024 sd_start_cmds(un, NULL);
15025 break;
15028 /* Return this command up the iodone chain to its originator. */
15029 mutex_exit(SD_MUTEX(un));
15031 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15032 xp->xb_pktp = NULL;
15034 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15036 ASSERT(!mutex_owned(SD_MUTEX(un)));
15037 mutex_enter(SD_MUTEX(un));
15039 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15044 * Function: sd_return_failed_command
15046 * Description: Command completion when an error occurred.
15048 * Context: May be called from interrupt context
15051 static void
15052 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15054 ASSERT(bp != NULL);
15055 ASSERT(un != NULL);
15056 ASSERT(mutex_owned(SD_MUTEX(un)));
15058 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15059 "sd_return_failed_command: entry\n");
15062 * b_resid could already be nonzero due to a partial data
15063 * transfer, so do not change it here.
15065 SD_BIOERROR(bp, errcode);
15067 sd_return_command(un, bp);
15068 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15069 "sd_return_failed_command: exit\n");
15074 * Function: sd_return_failed_command_no_restart
15076 * Description: Same as sd_return_failed_command, but ensures that no
15077 * call back into sd_start_cmds will be issued.
15079 * Context: May be called from interrupt context
15082 static void
15083 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15084 int errcode)
15086 struct sd_xbuf *xp;
15088 ASSERT(bp != NULL);
15089 ASSERT(un != NULL);
15090 ASSERT(mutex_owned(SD_MUTEX(un)));
15091 xp = SD_GET_XBUF(bp);
15092 ASSERT(xp != NULL);
15093 ASSERT(errcode != 0);
15095 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15096 "sd_return_failed_command_no_restart: entry\n");
15099 * b_resid could already be nonzero due to a partial data
15100 * transfer, so do not change it here.
15102 SD_BIOERROR(bp, errcode);
15105 * If this is the failfast bp, clear it. This can happen if the
15106 * failfast bp encounterd a fatal error when we attempted to
15107 * re-try it (such as a scsi_transport(9F) failure). However
15108 * we should NOT be in an active failfast state if the failfast
15109 * bp is not NULL.
15111 if (bp == un->un_failfast_bp) {
15112 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15113 un->un_failfast_bp = NULL;
15116 if (bp == un->un_retry_bp) {
15118 * This command was retried one or more times. Show that we are
15119 * done with it, and allow processing of the waitq to resume.
15121 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15122 "sd_return_failed_command_no_restart: "
15123 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15124 un->un_retry_bp = NULL;
15125 un->un_retry_statp = NULL;
15128 SD_UPDATE_RDWR_STATS(un, bp);
15129 SD_UPDATE_PARTITION_STATS(un, bp);
15131 mutex_exit(SD_MUTEX(un));
15133 if (xp->xb_pktp != NULL) {
15134 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15135 xp->xb_pktp = NULL;
15138 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15140 mutex_enter(SD_MUTEX(un));
15142 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15143 "sd_return_failed_command_no_restart: exit\n");
15148 * Function: sd_retry_command
15150 * Description: queue up a command for retry, or (optionally) fail it
15151 * if retry counts are exhausted.
15153 * Arguments: un - Pointer to the sd_lun struct for the target.
15155 * bp - Pointer to the buf for the command to be retried.
15157 * retry_check_flag - Flag to see which (if any) of the retry
15158 * counts should be decremented/checked. If the indicated
15159 * retry count is exhausted, then the command will not be
15160 * retried; it will be failed instead. This should use a
15161 * value equal to one of the following:
15163 * SD_RETRIES_NOCHECK
15164 * SD_RESD_RETRIES_STANDARD
15165 * SD_RETRIES_VICTIM
15167 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15168 * if the check should be made to see of FLAG_ISOLATE is set
15169 * in the pkt. If FLAG_ISOLATE is set, then the command is
15170 * not retried, it is simply failed.
15172 * user_funcp - Ptr to function to call before dispatching the
15173 * command. May be NULL if no action needs to be performed.
15174 * (Primarily intended for printing messages.)
15176 * user_arg - Optional argument to be passed along to
15177 * the user_funcp call.
15179 * failure_code - errno return code to set in the bp if the
15180 * command is going to be failed.
15182 * retry_delay - Retry delay interval in (clock_t) units. May
15183 * be zero which indicates that the retry should be retried
15184 * immediately (ie, without an intervening delay).
15186 * statp - Ptr to kstat function to be updated if the command
15187 * is queued for a delayed retry. May be NULL if no kstat
15188 * update is desired.
15190 * Context: May be called from interrupt context.
15193 static void
15194 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15195 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
15196 void *user_arg, int failure_code, clock_t retry_delay,
15197 void (*statp)(kstat_io_t *))
15199 struct sd_xbuf *xp;
15200 struct scsi_pkt *pktp;
15201 struct sd_fm_internal *sfip;
15203 ASSERT(un != NULL);
15204 ASSERT(mutex_owned(SD_MUTEX(un)));
15205 ASSERT(bp != NULL);
15206 xp = SD_GET_XBUF(bp);
15207 ASSERT(xp != NULL);
15208 pktp = SD_GET_PKTP(bp);
15209 ASSERT(pktp != NULL);
15211 sfip = (struct sd_fm_internal *)un->un_fm_private;
15212 ASSERT(sfip != NULL);
15214 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15215 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15218 * If we are syncing or dumping, fail the command to avoid
15219 * recursively calling back into scsi_transport().
15221 if (ddi_in_panic()) {
15222 goto fail_command_no_log;
15226 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15227 * log an error and fail the command.
15229 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15230 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15231 "ERROR, retrying FLAG_DIAGNOSE command.\n");
15232 sd_dump_memory(un, SD_LOG_IO, "CDB",
15233 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15234 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15235 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15236 goto fail_command;
15240 * If we are suspended, then put the command onto head of the
15241 * wait queue since we don't want to start more commands, and
15242 * clear the un_retry_bp. Next time when we are resumed, will
15243 * handle the command in the wait queue.
15245 switch (un->un_state) {
15246 case SD_STATE_SUSPENDED:
15247 case SD_STATE_DUMPING:
15248 bp->av_forw = un->un_waitq_headp;
15249 un->un_waitq_headp = bp;
15250 if (un->un_waitq_tailp == NULL) {
15251 un->un_waitq_tailp = bp;
15253 if (bp == un->un_retry_bp) {
15254 un->un_retry_bp = NULL;
15255 un->un_retry_statp = NULL;
15257 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15258 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15259 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15260 return;
15261 default:
15262 break;
15266 * If the caller wants us to check FLAG_ISOLATE, then see if that
15267 * is set; if it is then we do not want to retry the command.
15268 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15270 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15271 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15272 goto fail_command;
15278 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15279 * command timeout or a selection timeout has occurred. This means
15280 * that we were unable to establish an kind of communication with
15281 * the target, and subsequent retries and/or commands are likely
15282 * to encounter similar results and take a long time to complete.
15284 * If this is a failfast error condition, we need to update the
15285 * failfast state, even if this bp does not have B_FAILFAST set.
15287 if (retry_check_flag & SD_RETRIES_FAILFAST) {
15288 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15289 ASSERT(un->un_failfast_bp == NULL);
15291 * If we are already in the active failfast state, and
15292 * another failfast error condition has been detected,
15293 * then fail this command if it has B_FAILFAST set.
15294 * If B_FAILFAST is clear, then maintain the legacy
15295 * behavior of retrying heroically, even tho this will
15296 * take a lot more time to fail the command.
15298 if (bp->b_flags & B_FAILFAST) {
15299 goto fail_command;
15301 } else {
15303 * We're not in the active failfast state, but we
15304 * have a failfast error condition, so we must begin
15305 * transition to the next state. We do this regardless
15306 * of whether or not this bp has B_FAILFAST set.
15308 if (un->un_failfast_bp == NULL) {
15310 * This is the first bp to meet a failfast
15311 * condition so save it on un_failfast_bp &
15312 * do normal retry processing. Do not enter
15313 * active failfast state yet. This marks
15314 * entry into the "failfast pending" state.
15316 un->un_failfast_bp = bp;
15318 } else if (un->un_failfast_bp == bp) {
15320 * This is the second time *this* bp has
15321 * encountered a failfast error condition,
15322 * so enter active failfast state & flush
15323 * queues as appropriate.
15325 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15326 un->un_failfast_bp = NULL;
15327 sd_failfast_flushq(un);
15330 * Fail this bp now if B_FAILFAST set;
15331 * otherwise continue with retries. (It would
15332 * be pretty ironic if this bp succeeded on a
15333 * subsequent retry after we just flushed all
15334 * the queues).
15336 if (bp->b_flags & B_FAILFAST) {
15337 goto fail_command;
15340 } else {
15342 * If neither of the preceeding conditionals
15343 * was true, it means that there is some
15344 * *other* bp that has met an inital failfast
15345 * condition and is currently either being
15346 * retried or is waiting to be retried. In
15347 * that case we should perform normal retry
15348 * processing on *this* bp, since there is a
15349 * chance that the current failfast condition
15350 * is transient and recoverable. If that does
15351 * not turn out to be the case, then retries
15352 * will be cleared when the wait queue is
15353 * flushed anyway.
15357 } else {
15359 * SD_RETRIES_FAILFAST is clear, which indicates that we
15360 * likely were able to at least establish some level of
15361 * communication with the target and subsequent commands
15362 * and/or retries are likely to get through to the target,
15363 * In this case we want to be aggressive about clearing
15364 * the failfast state. Note that this does not affect
15365 * the "failfast pending" condition.
15367 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15372 * Check the specified retry count to see if we can still do
15373 * any retries with this pkt before we should fail it.
15375 switch (retry_check_flag & SD_RETRIES_MASK) {
15376 case SD_RETRIES_VICTIM:
15378 * Check the victim retry count. If exhausted, then fall
15379 * thru & check against the standard retry count.
15381 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15382 /* Increment count & proceed with the retry */
15383 xp->xb_victim_retry_count++;
15384 break;
15386 /* Victim retries exhausted, fall back to std. retries... */
15387 /* FALLTHRU */
15389 case SD_RETRIES_STANDARD:
15390 if (xp->xb_retry_count >= un->un_retry_count) {
15391 /* Retries exhausted, fail the command */
15392 SD_TRACE(SD_LOG_IO_CORE, un,
15393 "sd_retry_command: retries exhausted!\n");
15395 * update b_resid for failed SCMD_READ & SCMD_WRITE
15396 * commands with nonzero pkt_resid.
15398 if ((pktp->pkt_reason == CMD_CMPLT) &&
15399 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15400 (pktp->pkt_resid != 0)) {
15401 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15402 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15403 SD_UPDATE_B_RESID(bp, pktp);
15406 goto fail_command;
15408 xp->xb_retry_count++;
15409 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15410 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15411 break;
15413 case SD_RETRIES_UA:
15414 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15415 /* Retries exhausted, fail the command */
15416 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15417 "Unit Attention retries exhausted. "
15418 "Check the target.\n");
15419 goto fail_command;
15421 xp->xb_ua_retry_count++;
15422 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15423 "sd_retry_command: retry count:%d\n",
15424 xp->xb_ua_retry_count);
15425 break;
15427 case SD_RETRIES_BUSY:
15428 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15429 /* Retries exhausted, fail the command */
15430 SD_TRACE(SD_LOG_IO_CORE, un,
15431 "sd_retry_command: retries exhausted!\n");
15432 goto fail_command;
15434 xp->xb_retry_count++;
15435 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15436 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15437 break;
15439 case SD_RETRIES_NOCHECK:
15440 default:
15441 /* No retry count to check. Just proceed with the retry */
15442 break;
15445 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15448 * If this is a non-USCSI command being retried
15449 * during execution last time, we should post an ereport with
15450 * driver-assessment of the value "retry".
15451 * For partial DMA, request sense and STATUS_QFULL, there are no
15452 * hardware errors, we bypass ereport posting.
15454 if (failure_code != 0) {
15455 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15456 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15457 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15462 * If we were given a zero timeout, we must attempt to retry the
15463 * command immediately (ie, without a delay).
15465 if (retry_delay == 0) {
15467 * Check some limiting conditions to see if we can actually
15468 * do the immediate retry. If we cannot, then we must
15469 * fall back to queueing up a delayed retry.
15471 if (un->un_ncmds_in_transport >= un->un_throttle) {
15473 * We are at the throttle limit for the target,
15474 * fall back to delayed retry.
15476 retry_delay = un->un_busy_timeout;
15477 statp = kstat_waitq_enter;
15478 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15479 "sd_retry_command: immed. retry hit "
15480 "throttle!\n");
15481 } else {
15483 * We're clear to proceed with the immediate retry.
15484 * First call the user-provided function (if any)
15486 if (user_funcp != NULL) {
15487 (*user_funcp)(un, bp, user_arg,
15488 SD_IMMEDIATE_RETRY_ISSUED);
15491 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15492 "sd_retry_command: issuing immediate retry\n");
15495 * Call sd_start_cmds() to transport the command to
15496 * the target.
15498 sd_start_cmds(un, bp);
15500 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15501 "sd_retry_command exit\n");
15502 return;
15507 * Set up to retry the command after a delay.
15508 * First call the user-provided function (if any)
15510 if (user_funcp != NULL) {
15511 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15514 sd_set_retry_bp(un, bp, retry_delay, statp);
15516 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15517 return;
15519 fail_command:
15521 if (user_funcp != NULL) {
15522 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15525 fail_command_no_log:
15527 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15528 "sd_retry_command: returning failed command\n");
15530 sd_return_failed_command(un, bp, failure_code);
15532 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15537 * Function: sd_set_retry_bp
15539 * Description: Set up the given bp for retry.
15541 * Arguments: un - ptr to associated softstate
15542 * bp - ptr to buf(9S) for the command
15543 * retry_delay - time interval before issuing retry (may be 0)
15544 * statp - optional pointer to kstat function
15546 * Context: May be called under interrupt context
15549 static void
15550 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15551 void (*statp)(kstat_io_t *))
15553 ASSERT(un != NULL);
15554 ASSERT(mutex_owned(SD_MUTEX(un)));
15555 ASSERT(bp != NULL);
15557 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15558 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15561 * Indicate that the command is being retried. This will not allow any
15562 * other commands on the wait queue to be transported to the target
15563 * until this command has been completed (success or failure). The
15564 * "retry command" is not transported to the target until the given
15565 * time delay expires, unless the user specified a 0 retry_delay.
15567 * Note: the timeout(9F) callback routine is what actually calls
15568 * sd_start_cmds() to transport the command, with the exception of a
15569 * zero retry_delay. The only current implementor of a zero retry delay
15570 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15572 if (un->un_retry_bp == NULL) {
15573 ASSERT(un->un_retry_statp == NULL);
15574 un->un_retry_bp = bp;
15577 * If the user has not specified a delay the command should
15578 * be queued and no timeout should be scheduled.
15580 if (retry_delay == 0) {
15582 * Save the kstat pointer that will be used in the
15583 * call to SD_UPDATE_KSTATS() below, so that
15584 * sd_start_cmds() can correctly decrement the waitq
15585 * count when it is time to transport this command.
15587 un->un_retry_statp = statp;
15588 goto done;
15592 if (un->un_retry_bp == bp) {
15594 * Save the kstat pointer that will be used in the call to
15595 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15596 * correctly decrement the waitq count when it is time to
15597 * transport this command.
15599 un->un_retry_statp = statp;
15602 * Schedule a timeout if:
15603 * 1) The user has specified a delay.
15604 * 2) There is not a START_STOP_UNIT callback pending.
15606 * If no delay has been specified, then it is up to the caller
15607 * to ensure that IO processing continues without stalling.
15608 * Effectively, this means that the caller will issue the
15609 * required call to sd_start_cmds(). The START_STOP_UNIT
15610 * callback does this after the START STOP UNIT command has
15611 * completed. In either of these cases we should not schedule
15612 * a timeout callback here. Also don't schedule the timeout if
15613 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15615 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15616 (un->un_direct_priority_timeid == NULL)) {
15617 un->un_retry_timeid =
15618 timeout(sd_start_retry_command, un, retry_delay);
15619 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15620 "sd_set_retry_bp: setting timeout: un: 0x%p"
15621 " bp:0x%p un_retry_timeid:0x%p\n",
15622 un, bp, un->un_retry_timeid);
15624 } else {
15626 * We only get in here if there is already another command
15627 * waiting to be retried. In this case, we just put the
15628 * given command onto the wait queue, so it can be transported
15629 * after the current retry command has completed.
15631 * Also we have to make sure that if the command at the head
15632 * of the wait queue is the un_failfast_bp, that we do not
15633 * put ahead of it any other commands that are to be retried.
15635 if ((un->un_failfast_bp != NULL) &&
15636 (un->un_failfast_bp == un->un_waitq_headp)) {
15638 * Enqueue this command AFTER the first command on
15639 * the wait queue (which is also un_failfast_bp).
15641 bp->av_forw = un->un_waitq_headp->av_forw;
15642 un->un_waitq_headp->av_forw = bp;
15643 if (un->un_waitq_headp == un->un_waitq_tailp) {
15644 un->un_waitq_tailp = bp;
15646 } else {
15647 /* Enqueue this command at the head of the waitq. */
15648 bp->av_forw = un->un_waitq_headp;
15649 un->un_waitq_headp = bp;
15650 if (un->un_waitq_tailp == NULL) {
15651 un->un_waitq_tailp = bp;
15655 if (statp == NULL) {
15656 statp = kstat_waitq_enter;
15658 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15659 "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15662 done:
15663 if (statp != NULL) {
15664 SD_UPDATE_KSTATS(un, statp, bp);
15667 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15668 "sd_set_retry_bp: exit un:0x%p\n", un);
15673 * Function: sd_start_retry_command
15675 * Description: Start the command that has been waiting on the target's
15676 * retry queue. Called from timeout(9F) context after the
15677 * retry delay interval has expired.
15679 * Arguments: arg - pointer to associated softstate for the device.
15681 * Context: timeout(9F) thread context. May not sleep.
15684 static void
15685 sd_start_retry_command(void *arg)
15687 struct sd_lun *un = arg;
15689 ASSERT(un != NULL);
15690 ASSERT(!mutex_owned(SD_MUTEX(un)));
15692 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15693 "sd_start_retry_command: entry\n");
15695 mutex_enter(SD_MUTEX(un));
15697 un->un_retry_timeid = NULL;
15699 if (un->un_retry_bp != NULL) {
15700 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15701 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15702 un, un->un_retry_bp);
15703 sd_start_cmds(un, un->un_retry_bp);
15706 mutex_exit(SD_MUTEX(un));
15708 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15709 "sd_start_retry_command: exit\n");
15713 * Function: sd_rmw_msg_print_handler
15715 * Description: If RMW mode is enabled and warning message is triggered
15716 * print I/O count during a fixed interval.
15718 * Arguments: arg - pointer to associated softstate for the device.
15720 * Context: timeout(9F) thread context. May not sleep.
15722 static void
15723 sd_rmw_msg_print_handler(void *arg)
15725 struct sd_lun *un = arg;
15727 ASSERT(un != NULL);
15728 ASSERT(!mutex_owned(SD_MUTEX(un)));
15730 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15731 "sd_rmw_msg_print_handler: entry\n");
15733 mutex_enter(SD_MUTEX(un));
15735 if (un->un_rmw_incre_count > 0) {
15736 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15737 "%"PRIu64" I/O requests are not aligned with %d disk "
15738 "sector size in %ld seconds. They are handled through "
15739 "Read Modify Write but the performance is very low!\n",
15740 un->un_rmw_incre_count, un->un_tgt_blocksize,
15741 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
15742 un->un_rmw_incre_count = 0;
15743 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
15744 un, SD_RMW_MSG_PRINT_TIMEOUT);
15745 } else {
15746 un->un_rmw_msg_timeid = NULL;
15749 mutex_exit(SD_MUTEX(un));
15751 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15752 "sd_rmw_msg_print_handler: exit\n");
15756 * Function: sd_start_direct_priority_command
15758 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15759 * received TRAN_BUSY when we called scsi_transport() to send it
15760 * to the underlying HBA. This function is called from timeout(9F)
15761 * context after the delay interval has expired.
15763 * Arguments: arg - pointer to associated buf(9S) to be restarted.
15765 * Context: timeout(9F) thread context. May not sleep.
15768 static void
15769 sd_start_direct_priority_command(void *arg)
15771 struct buf *priority_bp = arg;
15772 struct sd_lun *un;
15774 ASSERT(priority_bp != NULL);
15775 un = SD_GET_UN(priority_bp);
15776 ASSERT(un != NULL);
15777 ASSERT(!mutex_owned(SD_MUTEX(un)));
15779 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15780 "sd_start_direct_priority_command: entry\n");
15782 mutex_enter(SD_MUTEX(un));
15783 un->un_direct_priority_timeid = NULL;
15784 sd_start_cmds(un, priority_bp);
15785 mutex_exit(SD_MUTEX(un));
15787 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15788 "sd_start_direct_priority_command: exit\n");
15793 * Function: sd_send_request_sense_command
15795 * Description: Sends a REQUEST SENSE command to the target
15797 * Context: May be called from interrupt context.
15800 static void
15801 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15802 struct scsi_pkt *pktp)
15804 ASSERT(bp != NULL);
15805 ASSERT(un != NULL);
15806 ASSERT(mutex_owned(SD_MUTEX(un)));
15808 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15809 "entry: buf:0x%p\n", bp);
15812 * If we are syncing or dumping, then fail the command to avoid a
15813 * recursive callback into scsi_transport(). Also fail the command
15814 * if we are suspended (legacy behavior).
15816 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15817 (un->un_state == SD_STATE_DUMPING)) {
15818 sd_return_failed_command(un, bp, EIO);
15819 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15820 "sd_send_request_sense_command: syncing/dumping, exit\n");
15821 return;
15825 * Retry the failed command and don't issue the request sense if:
15826 * 1) the sense buf is busy
15827 * 2) we have 1 or more outstanding commands on the target
15828 * (the sense data will be cleared or invalidated any way)
15830 * Note: There could be an issue with not checking a retry limit here,
15831 * the problem is determining which retry limit to check.
15833 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
15834 /* Don't retry if the command is flagged as non-retryable */
15835 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
15836 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15837 NULL, NULL, 0, un->un_busy_timeout,
15838 kstat_waitq_enter);
15839 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15840 "sd_send_request_sense_command: "
15841 "at full throttle, retrying exit\n");
15842 } else {
15843 sd_return_failed_command(un, bp, EIO);
15844 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15845 "sd_send_request_sense_command: "
15846 "at full throttle, non-retryable exit\n");
15848 return;
15851 sd_mark_rqs_busy(un, bp);
15852 sd_start_cmds(un, un->un_rqs_bp);
15854 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15855 "sd_send_request_sense_command: exit\n");
15860 * Function: sd_mark_rqs_busy
15862 * Description: Indicate that the request sense bp for this instance is
15863 * in use.
15865 * Context: May be called under interrupt context
15868 static void
15869 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
15871 struct sd_xbuf *sense_xp;
15873 ASSERT(un != NULL);
15874 ASSERT(bp != NULL);
15875 ASSERT(mutex_owned(SD_MUTEX(un)));
15876 ASSERT(un->un_sense_isbusy == 0);
15878 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
15879 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
15881 sense_xp = SD_GET_XBUF(un->un_rqs_bp);
15882 ASSERT(sense_xp != NULL);
15884 SD_INFO(SD_LOG_IO, un,
15885 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
15887 ASSERT(sense_xp->xb_pktp != NULL);
15888 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
15889 == (FLAG_SENSING | FLAG_HEAD));
15891 un->un_sense_isbusy = 1;
15892 un->un_rqs_bp->b_resid = 0;
15893 sense_xp->xb_pktp->pkt_resid = 0;
15894 sense_xp->xb_pktp->pkt_reason = 0;
15896 /* So we can get back the bp at interrupt time! */
15897 sense_xp->xb_sense_bp = bp;
15899 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
15902 * Mark this buf as awaiting sense data. (This is already set in
15903 * the pkt_flags for the RQS packet.)
15905 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
15907 /* Request sense down same path */
15908 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
15909 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
15910 sense_xp->xb_pktp->pkt_path_instance =
15911 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
15913 sense_xp->xb_retry_count = 0;
15914 sense_xp->xb_victim_retry_count = 0;
15915 sense_xp->xb_ua_retry_count = 0;
15916 sense_xp->xb_nr_retry_count = 0;
15917 sense_xp->xb_dma_resid = 0;
15919 /* Clean up the fields for auto-request sense */
15920 sense_xp->xb_sense_status = 0;
15921 sense_xp->xb_sense_state = 0;
15922 sense_xp->xb_sense_resid = 0;
15923 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
15925 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
15930 * Function: sd_mark_rqs_idle
15932 * Description: SD_MUTEX must be held continuously through this routine
15933 * to prevent reuse of the rqs struct before the caller can
15934 * complete it's processing.
15936 * Return Code: Pointer to the RQS buf
15938 * Context: May be called under interrupt context
15941 static struct buf *
15942 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
15944 struct buf *bp;
15945 ASSERT(un != NULL);
15946 ASSERT(sense_xp != NULL);
15947 ASSERT(mutex_owned(SD_MUTEX(un)));
15948 ASSERT(un->un_sense_isbusy != 0);
15950 un->un_sense_isbusy = 0;
15951 bp = sense_xp->xb_sense_bp;
15952 sense_xp->xb_sense_bp = NULL;
15954 /* This pkt is no longer interested in getting sense data */
15955 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
15957 return (bp);
15963 * Function: sd_alloc_rqs
15965 * Description: Set up the unit to receive auto request sense data
15967 * Return Code: DDI_SUCCESS or DDI_FAILURE
15969 * Context: Called under attach(9E) context
15972 static int
15973 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
15975 struct sd_xbuf *xp;
15977 ASSERT(un != NULL);
15978 ASSERT(!mutex_owned(SD_MUTEX(un)));
15979 ASSERT(un->un_rqs_bp == NULL);
15980 ASSERT(un->un_rqs_pktp == NULL);
15983 * First allocate the required buf and scsi_pkt structs, then set up
15984 * the CDB in the scsi_pkt for a REQUEST SENSE command.
15986 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
15987 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
15988 if (un->un_rqs_bp == NULL) {
15989 return (DDI_FAILURE);
15992 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
15993 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
15995 if (un->un_rqs_pktp == NULL) {
15996 sd_free_rqs(un);
15997 return (DDI_FAILURE);
16000 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16001 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16002 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16004 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16006 /* Set up the other needed members in the ARQ scsi_pkt. */
16007 un->un_rqs_pktp->pkt_comp = sdintr;
16008 un->un_rqs_pktp->pkt_time = sd_io_time;
16009 un->un_rqs_pktp->pkt_flags |=
16010 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16013 * Allocate & init the sd_xbuf struct for the RQS command. Do not
16014 * provide any intpkt, destroypkt routines as we take care of
16015 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16017 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16018 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16019 xp->xb_pktp = un->un_rqs_pktp;
16020 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16021 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
16022 un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16025 * Save the pointer to the request sense private bp so it can
16026 * be retrieved in sdintr.
16028 un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16029 ASSERT(un->un_rqs_bp->b_private == xp);
16032 * See if the HBA supports auto-request sense for the specified
16033 * target/lun. If it does, then try to enable it (if not already
16034 * enabled).
16036 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16037 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16038 * return success. However, in both of these cases ARQ is always
16039 * enabled and scsi_ifgetcap will always return true. The best approach
16040 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16042 * The 3rd case is the HBA (adp) always return enabled on
16043 * scsi_ifgetgetcap even when it's not enable, the best approach
16044 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16045 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16048 if (un->un_f_is_fibre == TRUE) {
16049 un->un_f_arq_enabled = TRUE;
16050 } else {
16051 #if defined(__i386) || defined(__amd64)
16053 * Circumvent the Adaptec bug, remove this code when
16054 * the bug is fixed
16056 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16057 #endif
16058 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16059 case 0:
16060 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16061 "sd_alloc_rqs: HBA supports ARQ\n");
16063 * ARQ is supported by this HBA but currently is not
16064 * enabled. Attempt to enable it and if successful then
16065 * mark this instance as ARQ enabled.
16067 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16068 == 1) {
16069 /* Successfully enabled ARQ in the HBA */
16070 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16071 "sd_alloc_rqs: ARQ enabled\n");
16072 un->un_f_arq_enabled = TRUE;
16073 } else {
16074 /* Could not enable ARQ in the HBA */
16075 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16076 "sd_alloc_rqs: failed ARQ enable\n");
16077 un->un_f_arq_enabled = FALSE;
16079 break;
16080 case 1:
16082 * ARQ is supported by this HBA and is already enabled.
16083 * Just mark ARQ as enabled for this instance.
16085 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16086 "sd_alloc_rqs: ARQ already enabled\n");
16087 un->un_f_arq_enabled = TRUE;
16088 break;
16089 default:
16091 * ARQ is not supported by this HBA; disable it for this
16092 * instance.
16094 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16095 "sd_alloc_rqs: HBA does not support ARQ\n");
16096 un->un_f_arq_enabled = FALSE;
16097 break;
16101 return (DDI_SUCCESS);
16106 * Function: sd_free_rqs
16108 * Description: Cleanup for the pre-instance RQS command.
16110 * Context: Kernel thread context
16113 static void
16114 sd_free_rqs(struct sd_lun *un)
16116 ASSERT(un != NULL);
16118 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16121 * If consistent memory is bound to a scsi_pkt, the pkt
16122 * has to be destroyed *before* freeing the consistent memory.
16123 * Don't change the sequence of this operations.
16124 * scsi_destroy_pkt() might access memory, which isn't allowed,
16125 * after it was freed in scsi_free_consistent_buf().
16127 if (un->un_rqs_pktp != NULL) {
16128 scsi_destroy_pkt(un->un_rqs_pktp);
16129 un->un_rqs_pktp = NULL;
16132 if (un->un_rqs_bp != NULL) {
16133 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16134 if (xp != NULL) {
16135 kmem_free(xp, sizeof (struct sd_xbuf));
16137 scsi_free_consistent_buf(un->un_rqs_bp);
16138 un->un_rqs_bp = NULL;
16140 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16146 * Function: sd_reduce_throttle
16148 * Description: Reduces the maximum # of outstanding commands on a
16149 * target to the current number of outstanding commands.
16150 * Queues a tiemout(9F) callback to restore the limit
16151 * after a specified interval has elapsed.
16152 * Typically used when we get a TRAN_BUSY return code
16153 * back from scsi_transport().
16155 * Arguments: un - ptr to the sd_lun softstate struct
16156 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16158 * Context: May be called from interrupt context
16161 static void
16162 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16164 ASSERT(un != NULL);
16165 ASSERT(mutex_owned(SD_MUTEX(un)));
16166 ASSERT(un->un_ncmds_in_transport >= 0);
16168 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16169 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16170 un, un->un_throttle, un->un_ncmds_in_transport);
16172 if (un->un_throttle > 1) {
16173 if (un->un_f_use_adaptive_throttle == TRUE) {
16174 switch (throttle_type) {
16175 case SD_THROTTLE_TRAN_BUSY:
16176 if (un->un_busy_throttle == 0) {
16177 un->un_busy_throttle = un->un_throttle;
16179 break;
16180 case SD_THROTTLE_QFULL:
16181 un->un_busy_throttle = 0;
16182 break;
16183 default:
16184 ASSERT(FALSE);
16187 if (un->un_ncmds_in_transport > 0) {
16188 un->un_throttle = un->un_ncmds_in_transport;
16191 } else {
16192 if (un->un_ncmds_in_transport == 0) {
16193 un->un_throttle = 1;
16194 } else {
16195 un->un_throttle = un->un_ncmds_in_transport;
16200 /* Reschedule the timeout if none is currently active */
16201 if (un->un_reset_throttle_timeid == NULL) {
16202 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16203 un, SD_THROTTLE_RESET_INTERVAL);
16204 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16205 "sd_reduce_throttle: timeout scheduled!\n");
16208 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16209 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16215 * Function: sd_restore_throttle
16217 * Description: Callback function for timeout(9F). Resets the current
16218 * value of un->un_throttle to its default.
16220 * Arguments: arg - pointer to associated softstate for the device.
16222 * Context: May be called from interrupt context
16225 static void
16226 sd_restore_throttle(void *arg)
16228 struct sd_lun *un = arg;
16230 ASSERT(un != NULL);
16231 ASSERT(!mutex_owned(SD_MUTEX(un)));
16233 mutex_enter(SD_MUTEX(un));
16235 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16236 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16238 un->un_reset_throttle_timeid = NULL;
16240 if (un->un_f_use_adaptive_throttle == TRUE) {
16242 * If un_busy_throttle is nonzero, then it contains the
16243 * value that un_throttle was when we got a TRAN_BUSY back
16244 * from scsi_transport(). We want to revert back to this
16245 * value.
16247 * In the QFULL case, the throttle limit will incrementally
16248 * increase until it reaches max throttle.
16250 if (un->un_busy_throttle > 0) {
16251 un->un_throttle = un->un_busy_throttle;
16252 un->un_busy_throttle = 0;
16253 } else {
16255 * increase throttle by 10% open gate slowly, schedule
16256 * another restore if saved throttle has not been
16257 * reached
16259 short throttle;
16260 if (sd_qfull_throttle_enable) {
16261 throttle = un->un_throttle +
16262 max((un->un_throttle / 10), 1);
16263 un->un_throttle =
16264 (throttle < un->un_saved_throttle) ?
16265 throttle : un->un_saved_throttle;
16266 if (un->un_throttle < un->un_saved_throttle) {
16267 un->un_reset_throttle_timeid =
16268 timeout(sd_restore_throttle,
16270 SD_QFULL_THROTTLE_RESET_INTERVAL);
16276 * If un_throttle has fallen below the low-water mark, we
16277 * restore the maximum value here (and allow it to ratchet
16278 * down again if necessary).
16280 if (un->un_throttle < un->un_min_throttle) {
16281 un->un_throttle = un->un_saved_throttle;
16283 } else {
16284 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16285 "restoring limit from 0x%x to 0x%x\n",
16286 un->un_throttle, un->un_saved_throttle);
16287 un->un_throttle = un->un_saved_throttle;
16290 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16291 "sd_restore_throttle: calling sd_start_cmds!\n");
16293 sd_start_cmds(un, NULL);
16295 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16296 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16297 un, un->un_throttle);
16299 mutex_exit(SD_MUTEX(un));
16301 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16305 * Function: sdrunout
16307 * Description: Callback routine for scsi_init_pkt when a resource allocation
16308 * fails.
16310 * Arguments: arg - a pointer to the sd_lun unit struct for the particular
16311 * soft state instance.
16313 * Return Code: The scsi_init_pkt routine allows for the callback function to
16314 * return a 0 indicating the callback should be rescheduled or a 1
16315 * indicating not to reschedule. This routine always returns 1
16316 * because the driver always provides a callback function to
16317 * scsi_init_pkt. This results in a callback always being scheduled
16318 * (via the scsi_init_pkt callback implementation) if a resource
16319 * failure occurs.
16321 * Context: This callback function may not block or call routines that block
16323 * Note: Using the scsi_init_pkt callback facility can result in an I/O
16324 * request persisting at the head of the list which cannot be
16325 * satisfied even after multiple retries. In the future the driver
16326 * may implement some time of maximum runout count before failing
16327 * an I/O.
16330 static int
16331 sdrunout(caddr_t arg)
16333 struct sd_lun *un = (struct sd_lun *)arg;
16335 ASSERT(un != NULL);
16336 ASSERT(!mutex_owned(SD_MUTEX(un)));
16338 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16340 mutex_enter(SD_MUTEX(un));
16341 sd_start_cmds(un, NULL);
16342 mutex_exit(SD_MUTEX(un));
16344 * This callback routine always returns 1 (i.e. do not reschedule)
16345 * because we always specify sdrunout as the callback handler for
16346 * scsi_init_pkt inside the call to sd_start_cmds.
16348 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16349 return (1);
16354 * Function: sdintr
16356 * Description: Completion callback routine for scsi_pkt(9S) structs
16357 * sent to the HBA driver via scsi_transport(9F).
16359 * Context: Interrupt context
16362 static void
16363 sdintr(struct scsi_pkt *pktp)
16365 struct buf *bp;
16366 struct sd_xbuf *xp;
16367 struct sd_lun *un;
16368 size_t actual_len;
16369 sd_ssc_t *sscp;
16371 ASSERT(pktp != NULL);
16372 bp = (struct buf *)pktp->pkt_private;
16373 ASSERT(bp != NULL);
16374 xp = SD_GET_XBUF(bp);
16375 ASSERT(xp != NULL);
16376 ASSERT(xp->xb_pktp != NULL);
16377 un = SD_GET_UN(bp);
16378 ASSERT(un != NULL);
16379 ASSERT(!mutex_owned(SD_MUTEX(un)));
16381 #ifdef SD_FAULT_INJECTION
16383 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16384 /* SD FaultInjection */
16385 sd_faultinjection(pktp);
16387 #endif /* SD_FAULT_INJECTION */
16389 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16390 " xp:0x%p, un:0x%p\n", bp, xp, un);
16392 mutex_enter(SD_MUTEX(un));
16394 ASSERT(un->un_fm_private != NULL);
16395 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16396 ASSERT(sscp != NULL);
16398 /* Reduce the count of the #commands currently in transport */
16399 un->un_ncmds_in_transport--;
16400 ASSERT(un->un_ncmds_in_transport >= 0);
16402 /* Increment counter to indicate that the callback routine is active */
16403 un->un_in_callback++;
16405 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16407 #ifdef SDDEBUG
16408 if (bp == un->un_retry_bp) {
16409 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16410 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16411 un, un->un_retry_bp, un->un_ncmds_in_transport);
16413 #endif
16416 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16417 * state if needed.
16419 if (pktp->pkt_reason == CMD_DEV_GONE) {
16420 /* Prevent multiple console messages for the same failure. */
16421 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16422 un->un_last_pkt_reason = CMD_DEV_GONE;
16423 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16424 "Command failed to complete...Device is gone\n");
16426 if (un->un_mediastate != DKIO_DEV_GONE) {
16427 un->un_mediastate = DKIO_DEV_GONE;
16428 cv_broadcast(&un->un_state_cv);
16431 * If the command happens to be the REQUEST SENSE command,
16432 * free up the rqs buf and fail the original command.
16434 if (bp == un->un_rqs_bp) {
16435 bp = sd_mark_rqs_idle(un, xp);
16437 sd_return_failed_command(un, bp, EIO);
16438 goto exit;
16441 if (pktp->pkt_state & STATE_XARQ_DONE) {
16442 SD_TRACE(SD_LOG_COMMON, un,
16443 "sdintr: extra sense data received. pkt=%p\n", pktp);
16447 * First see if the pkt has auto-request sense data with it....
16448 * Look at the packet state first so we don't take a performance
16449 * hit looking at the arq enabled flag unless absolutely necessary.
16451 if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16452 (un->un_f_arq_enabled == TRUE)) {
16454 * The HBA did an auto request sense for this command so check
16455 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16456 * driver command that should not be retried.
16458 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16460 * Save the relevant sense info into the xp for the
16461 * original cmd.
16463 struct scsi_arq_status *asp;
16464 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16465 xp->xb_sense_status =
16466 *((uchar_t *)(&(asp->sts_rqpkt_status)));
16467 xp->xb_sense_state = asp->sts_rqpkt_state;
16468 xp->xb_sense_resid = asp->sts_rqpkt_resid;
16469 if (pktp->pkt_state & STATE_XARQ_DONE) {
16470 actual_len = MAX_SENSE_LENGTH -
16471 xp->xb_sense_resid;
16472 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16473 MAX_SENSE_LENGTH);
16474 } else {
16475 if (xp->xb_sense_resid > SENSE_LENGTH) {
16476 actual_len = MAX_SENSE_LENGTH -
16477 xp->xb_sense_resid;
16478 } else {
16479 actual_len = SENSE_LENGTH -
16480 xp->xb_sense_resid;
16482 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16483 if ((((struct uscsi_cmd *)
16484 (xp->xb_pktinfo))->uscsi_rqlen) >
16485 actual_len) {
16486 xp->xb_sense_resid =
16487 (((struct uscsi_cmd *)
16488 (xp->xb_pktinfo))->
16489 uscsi_rqlen) - actual_len;
16490 } else {
16491 xp->xb_sense_resid = 0;
16494 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16495 SENSE_LENGTH);
16498 /* fail the command */
16499 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16500 "sdintr: arq done and FLAG_DIAGNOSE set\n");
16501 sd_return_failed_command(un, bp, EIO);
16502 goto exit;
16505 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
16507 * We want to either retry or fail this command, so free
16508 * the DMA resources here. If we retry the command then
16509 * the DMA resources will be reallocated in sd_start_cmds().
16510 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16511 * causes the *entire* transfer to start over again from the
16512 * beginning of the request, even for PARTIAL chunks that
16513 * have already transferred successfully.
16515 if ((un->un_f_is_fibre == TRUE) &&
16516 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16517 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16518 scsi_dmafree(pktp);
16519 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16521 #endif
16523 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16524 "sdintr: arq done, sd_handle_auto_request_sense\n");
16526 sd_handle_auto_request_sense(un, bp, xp, pktp);
16527 goto exit;
16530 /* Next see if this is the REQUEST SENSE pkt for the instance */
16531 if (pktp->pkt_flags & FLAG_SENSING) {
16532 /* This pktp is from the unit's REQUEST_SENSE command */
16533 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16534 "sdintr: sd_handle_request_sense\n");
16535 sd_handle_request_sense(un, bp, xp, pktp);
16536 goto exit;
16540 * Check to see if the command successfully completed as requested;
16541 * this is the most common case (and also the hot performance path).
16543 * Requirements for successful completion are:
16544 * pkt_reason is CMD_CMPLT and packet status is status good.
16545 * In addition:
16546 * - A residual of zero indicates successful completion no matter what
16547 * the command is.
16548 * - If the residual is not zero and the command is not a read or
16549 * write, then it's still defined as successful completion. In other
16550 * words, if the command is a read or write the residual must be
16551 * zero for successful completion.
16552 * - If the residual is not zero and the command is a read or
16553 * write, and it's a USCSICMD, then it's still defined as
16554 * successful completion.
16556 if ((pktp->pkt_reason == CMD_CMPLT) &&
16557 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16560 * Since this command is returned with a good status, we
16561 * can reset the count for Sonoma failover.
16563 un->un_sonoma_failure_count = 0;
16566 * Return all USCSI commands on good status
16568 if (pktp->pkt_resid == 0) {
16569 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16570 "sdintr: returning command for resid == 0\n");
16571 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16572 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16573 SD_UPDATE_B_RESID(bp, pktp);
16574 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16575 "sdintr: returning command for resid != 0\n");
16576 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16577 SD_UPDATE_B_RESID(bp, pktp);
16578 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16579 "sdintr: returning uscsi command\n");
16580 } else {
16581 goto not_successful;
16583 sd_return_command(un, bp);
16586 * Decrement counter to indicate that the callback routine
16587 * is done.
16589 un->un_in_callback--;
16590 ASSERT(un->un_in_callback >= 0);
16591 mutex_exit(SD_MUTEX(un));
16593 return;
16596 not_successful:
16598 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
16600 * The following is based upon knowledge of the underlying transport
16601 * and its use of DMA resources. This code should be removed when
16602 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16603 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16604 * and sd_start_cmds().
16606 * Free any DMA resources associated with this command if there
16607 * is a chance it could be retried or enqueued for later retry.
16608 * If we keep the DMA binding then mpxio cannot reissue the
16609 * command on another path whenever a path failure occurs.
16611 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16612 * causes the *entire* transfer to start over again from the
16613 * beginning of the request, even for PARTIAL chunks that
16614 * have already transferred successfully.
16616 * This is only done for non-uscsi commands (and also skipped for the
16617 * driver's internal RQS command). Also just do this for Fibre Channel
16618 * devices as these are the only ones that support mpxio.
16620 if ((un->un_f_is_fibre == TRUE) &&
16621 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16622 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16623 scsi_dmafree(pktp);
16624 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16626 #endif
16629 * The command did not successfully complete as requested so check
16630 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16631 * driver command that should not be retried so just return. If
16632 * FLAG_DIAGNOSE is not set the error will be processed below.
16634 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16635 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16636 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16638 * Issue a request sense if a check condition caused the error
16639 * (we handle the auto request sense case above), otherwise
16640 * just fail the command.
16642 if ((pktp->pkt_reason == CMD_CMPLT) &&
16643 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16644 sd_send_request_sense_command(un, bp, pktp);
16645 } else {
16646 sd_return_failed_command(un, bp, EIO);
16648 goto exit;
16652 * The command did not successfully complete as requested so process
16653 * the error, retry, and/or attempt recovery.
16655 switch (pktp->pkt_reason) {
16656 case CMD_CMPLT:
16657 switch (SD_GET_PKT_STATUS(pktp)) {
16658 case STATUS_GOOD:
16660 * The command completed successfully with a non-zero
16661 * residual
16663 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16664 "sdintr: STATUS_GOOD \n");
16665 sd_pkt_status_good(un, bp, xp, pktp);
16666 break;
16668 case STATUS_CHECK:
16669 case STATUS_TERMINATED:
16670 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16671 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16672 sd_pkt_status_check_condition(un, bp, xp, pktp);
16673 break;
16675 case STATUS_BUSY:
16676 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16677 "sdintr: STATUS_BUSY\n");
16678 sd_pkt_status_busy(un, bp, xp, pktp);
16679 break;
16681 case STATUS_RESERVATION_CONFLICT:
16682 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16683 "sdintr: STATUS_RESERVATION_CONFLICT\n");
16684 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16685 break;
16687 case STATUS_QFULL:
16688 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16689 "sdintr: STATUS_QFULL\n");
16690 sd_pkt_status_qfull(un, bp, xp, pktp);
16691 break;
16693 case STATUS_MET:
16694 case STATUS_INTERMEDIATE:
16695 case STATUS_SCSI2:
16696 case STATUS_INTERMEDIATE_MET:
16697 case STATUS_ACA_ACTIVE:
16698 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16699 "Unexpected SCSI status received: 0x%x\n",
16700 SD_GET_PKT_STATUS(pktp));
16702 * Mark the ssc_flags when detected invalid status
16703 * code for non-USCSI command.
16705 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16706 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
16707 0, "stat-code");
16709 sd_return_failed_command(un, bp, EIO);
16710 break;
16712 default:
16713 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16714 "Invalid SCSI status received: 0x%x\n",
16715 SD_GET_PKT_STATUS(pktp));
16716 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16717 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
16718 0, "stat-code");
16720 sd_return_failed_command(un, bp, EIO);
16721 break;
16724 break;
16726 case CMD_INCOMPLETE:
16727 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16728 "sdintr: CMD_INCOMPLETE\n");
16729 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16730 break;
16731 case CMD_TRAN_ERR:
16732 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16733 "sdintr: CMD_TRAN_ERR\n");
16734 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16735 break;
16736 case CMD_RESET:
16737 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16738 "sdintr: CMD_RESET \n");
16739 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16740 break;
16741 case CMD_ABORTED:
16742 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16743 "sdintr: CMD_ABORTED \n");
16744 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16745 break;
16746 case CMD_TIMEOUT:
16747 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16748 "sdintr: CMD_TIMEOUT\n");
16749 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16750 break;
16751 case CMD_UNX_BUS_FREE:
16752 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16753 "sdintr: CMD_UNX_BUS_FREE \n");
16754 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16755 break;
16756 case CMD_TAG_REJECT:
16757 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16758 "sdintr: CMD_TAG_REJECT\n");
16759 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16760 break;
16761 default:
16762 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16763 "sdintr: default\n");
16765 * Mark the ssc_flags for detecting invliad pkt_reason.
16767 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16768 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
16769 0, "pkt-reason");
16771 sd_pkt_reason_default(un, bp, xp, pktp);
16772 break;
16775 exit:
16776 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16778 /* Decrement counter to indicate that the callback routine is done. */
16779 un->un_in_callback--;
16780 ASSERT(un->un_in_callback >= 0);
16783 * At this point, the pkt has been dispatched, ie, it is either
16784 * being re-tried or has been returned to its caller and should
16785 * not be referenced.
16788 mutex_exit(SD_MUTEX(un));
16793 * Function: sd_print_incomplete_msg
16795 * Description: Prints the error message for a CMD_INCOMPLETE error.
16797 * Arguments: un - ptr to associated softstate for the device.
16798 * bp - ptr to the buf(9S) for the command.
16799 * arg - message string ptr
16800 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16801 * or SD_NO_RETRY_ISSUED.
16803 * Context: May be called under interrupt context
16806 static void
16807 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16809 struct scsi_pkt *pktp;
16810 char *msgp;
16811 char *cmdp = arg;
16813 ASSERT(un != NULL);
16814 ASSERT(mutex_owned(SD_MUTEX(un)));
16815 ASSERT(bp != NULL);
16816 ASSERT(arg != NULL);
16817 pktp = SD_GET_PKTP(bp);
16818 ASSERT(pktp != NULL);
16820 switch (code) {
16821 case SD_DELAYED_RETRY_ISSUED:
16822 case SD_IMMEDIATE_RETRY_ISSUED:
16823 msgp = "retrying";
16824 break;
16825 case SD_NO_RETRY_ISSUED:
16826 default:
16827 msgp = "giving up";
16828 break;
16831 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16832 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16833 "incomplete %s- %s\n", cmdp, msgp);
16840 * Function: sd_pkt_status_good
16842 * Description: Processing for a STATUS_GOOD code in pkt_status.
16844 * Context: May be called under interrupt context
16847 static void
16848 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
16849 struct sd_xbuf *xp, struct scsi_pkt *pktp)
16851 char *cmdp;
16853 ASSERT(un != NULL);
16854 ASSERT(mutex_owned(SD_MUTEX(un)));
16855 ASSERT(bp != NULL);
16856 ASSERT(xp != NULL);
16857 ASSERT(pktp != NULL);
16858 ASSERT(pktp->pkt_reason == CMD_CMPLT);
16859 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
16860 ASSERT(pktp->pkt_resid != 0);
16862 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
16864 SD_UPDATE_ERRSTATS(un, sd_harderrs);
16865 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
16866 case SCMD_READ:
16867 cmdp = "read";
16868 break;
16869 case SCMD_WRITE:
16870 cmdp = "write";
16871 break;
16872 default:
16873 SD_UPDATE_B_RESID(bp, pktp);
16874 sd_return_command(un, bp);
16875 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16876 return;
16880 * See if we can retry the read/write, preferrably immediately.
16881 * If retries are exhaused, then sd_retry_command() will update
16882 * the b_resid count.
16884 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
16885 cmdp, EIO, (clock_t)0, NULL);
16887 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16895 * Function: sd_handle_request_sense
16897 * Description: Processing for non-auto Request Sense command.
16899 * Arguments: un - ptr to associated softstate
16900 * sense_bp - ptr to buf(9S) for the RQS command
16901 * sense_xp - ptr to the sd_xbuf for the RQS command
16902 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
16904 * Context: May be called under interrupt context
16907 static void
16908 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
16909 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
16911 struct buf *cmd_bp; /* buf for the original command */
16912 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */
16913 struct scsi_pkt *cmd_pktp; /* pkt for the original command */
16914 size_t actual_len; /* actual sense data length */
16916 ASSERT(un != NULL);
16917 ASSERT(mutex_owned(SD_MUTEX(un)));
16918 ASSERT(sense_bp != NULL);
16919 ASSERT(sense_xp != NULL);
16920 ASSERT(sense_pktp != NULL);
16923 * Note the sense_bp, sense_xp, and sense_pktp here are for the
16924 * RQS command and not the original command.
16926 ASSERT(sense_pktp == un->un_rqs_pktp);
16927 ASSERT(sense_bp == un->un_rqs_bp);
16928 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
16929 (FLAG_SENSING | FLAG_HEAD));
16930 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
16931 FLAG_SENSING) == FLAG_SENSING);
16933 /* These are the bp, xp, and pktp for the original command */
16934 cmd_bp = sense_xp->xb_sense_bp;
16935 cmd_xp = SD_GET_XBUF(cmd_bp);
16936 cmd_pktp = SD_GET_PKTP(cmd_bp);
16938 if (sense_pktp->pkt_reason != CMD_CMPLT) {
16940 * The REQUEST SENSE command failed. Release the REQUEST
16941 * SENSE command for re-use, get back the bp for the original
16942 * command, and attempt to re-try the original command if
16943 * FLAG_DIAGNOSE is not set in the original packet.
16945 SD_UPDATE_ERRSTATS(un, sd_harderrs);
16946 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16947 cmd_bp = sd_mark_rqs_idle(un, sense_xp);
16948 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
16949 NULL, NULL, EIO, (clock_t)0, NULL);
16950 return;
16955 * Save the relevant sense info into the xp for the original cmd.
16957 * Note: if the request sense failed the state info will be zero
16958 * as set in sd_mark_rqs_busy()
16960 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
16961 cmd_xp->xb_sense_state = sense_pktp->pkt_state;
16962 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
16963 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
16964 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
16965 SENSE_LENGTH)) {
16966 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
16967 MAX_SENSE_LENGTH);
16968 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
16969 } else {
16970 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
16971 SENSE_LENGTH);
16972 if (actual_len < SENSE_LENGTH) {
16973 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
16974 } else {
16975 cmd_xp->xb_sense_resid = 0;
16980 * Free up the RQS command....
16981 * NOTE:
16982 * Must do this BEFORE calling sd_validate_sense_data!
16983 * sd_validate_sense_data may return the original command in
16984 * which case the pkt will be freed and the flags can no
16985 * longer be touched.
16986 * SD_MUTEX is held through this process until the command
16987 * is dispatched based upon the sense data, so there are
16988 * no race conditions.
16990 (void) sd_mark_rqs_idle(un, sense_xp);
16993 * For a retryable command see if we have valid sense data, if so then
16994 * turn it over to sd_decode_sense() to figure out the right course of
16995 * action. Just fail a non-retryable command.
16997 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16998 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
16999 SD_SENSE_DATA_IS_VALID) {
17000 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17002 } else {
17003 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17004 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17005 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17006 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17007 sd_return_failed_command(un, cmd_bp, EIO);
17015 * Function: sd_handle_auto_request_sense
17017 * Description: Processing for auto-request sense information.
17019 * Arguments: un - ptr to associated softstate
17020 * bp - ptr to buf(9S) for the command
17021 * xp - ptr to the sd_xbuf for the command
17022 * pktp - ptr to the scsi_pkt(9S) for the command
17024 * Context: May be called under interrupt context
17027 static void
17028 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17029 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17031 struct scsi_arq_status *asp;
17032 size_t actual_len;
17034 ASSERT(un != NULL);
17035 ASSERT(mutex_owned(SD_MUTEX(un)));
17036 ASSERT(bp != NULL);
17037 ASSERT(xp != NULL);
17038 ASSERT(pktp != NULL);
17039 ASSERT(pktp != un->un_rqs_pktp);
17040 ASSERT(bp != un->un_rqs_bp);
17043 * For auto-request sense, we get a scsi_arq_status back from
17044 * the HBA, with the sense data in the sts_sensedata member.
17045 * The pkt_scbp of the packet points to this scsi_arq_status.
17047 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17049 if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17051 * The auto REQUEST SENSE failed; see if we can re-try
17052 * the original command.
17054 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17055 "auto request sense failed (reason=%s)\n",
17056 scsi_rname(asp->sts_rqpkt_reason));
17058 sd_reset_target(un, pktp);
17060 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17061 NULL, NULL, EIO, (clock_t)0, NULL);
17062 return;
17065 /* Save the relevant sense info into the xp for the original cmd. */
17066 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17067 xp->xb_sense_state = asp->sts_rqpkt_state;
17068 xp->xb_sense_resid = asp->sts_rqpkt_resid;
17069 if (xp->xb_sense_state & STATE_XARQ_DONE) {
17070 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17071 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17072 MAX_SENSE_LENGTH);
17073 } else {
17074 if (xp->xb_sense_resid > SENSE_LENGTH) {
17075 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17076 } else {
17077 actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17079 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17080 if ((((struct uscsi_cmd *)
17081 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17082 xp->xb_sense_resid = (((struct uscsi_cmd *)
17083 (xp->xb_pktinfo))->uscsi_rqlen) -
17084 actual_len;
17085 } else {
17086 xp->xb_sense_resid = 0;
17089 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17093 * See if we have valid sense data, if so then turn it over to
17094 * sd_decode_sense() to figure out the right course of action.
17096 if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17097 SD_SENSE_DATA_IS_VALID) {
17098 sd_decode_sense(un, bp, xp, pktp);
17104 * Function: sd_print_sense_failed_msg
17106 * Description: Print log message when RQS has failed.
17108 * Arguments: un - ptr to associated softstate
17109 * bp - ptr to buf(9S) for the command
17110 * arg - generic message string ptr
17111 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17112 * or SD_NO_RETRY_ISSUED
17114 * Context: May be called from interrupt context
17117 static void
17118 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17119 int code)
17121 char *msgp = arg;
17123 ASSERT(un != NULL);
17124 ASSERT(mutex_owned(SD_MUTEX(un)));
17125 ASSERT(bp != NULL);
17127 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17128 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17134 * Function: sd_validate_sense_data
17136 * Description: Check the given sense data for validity.
17137 * If the sense data is not valid, the command will
17138 * be either failed or retried!
17140 * Return Code: SD_SENSE_DATA_IS_INVALID
17141 * SD_SENSE_DATA_IS_VALID
17143 * Context: May be called from interrupt context
17146 static int
17147 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17148 size_t actual_len)
17150 struct scsi_extended_sense *esp;
17151 struct scsi_pkt *pktp;
17152 char *msgp = NULL;
17153 sd_ssc_t *sscp;
17155 ASSERT(un != NULL);
17156 ASSERT(mutex_owned(SD_MUTEX(un)));
17157 ASSERT(bp != NULL);
17158 ASSERT(bp != un->un_rqs_bp);
17159 ASSERT(xp != NULL);
17160 ASSERT(un->un_fm_private != NULL);
17162 pktp = SD_GET_PKTP(bp);
17163 ASSERT(pktp != NULL);
17165 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17166 ASSERT(sscp != NULL);
17169 * Check the status of the RQS command (auto or manual).
17171 switch (xp->xb_sense_status & STATUS_MASK) {
17172 case STATUS_GOOD:
17173 break;
17175 case STATUS_RESERVATION_CONFLICT:
17176 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17177 return (SD_SENSE_DATA_IS_INVALID);
17179 case STATUS_BUSY:
17180 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17181 "Busy Status on REQUEST SENSE\n");
17182 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17183 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17184 return (SD_SENSE_DATA_IS_INVALID);
17186 case STATUS_QFULL:
17187 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17188 "QFULL Status on REQUEST SENSE\n");
17189 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17190 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17191 return (SD_SENSE_DATA_IS_INVALID);
17193 case STATUS_CHECK:
17194 case STATUS_TERMINATED:
17195 msgp = "Check Condition on REQUEST SENSE\n";
17196 goto sense_failed;
17198 default:
17199 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17200 goto sense_failed;
17204 * See if we got the minimum required amount of sense data.
17205 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17206 * or less.
17208 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17209 (actual_len == 0)) {
17210 msgp = "Request Sense couldn't get sense data\n";
17211 goto sense_failed;
17214 if (actual_len < SUN_MIN_SENSE_LENGTH) {
17215 msgp = "Not enough sense information\n";
17216 /* Mark the ssc_flags for detecting invalid sense data */
17217 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17218 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17219 "sense-data");
17221 goto sense_failed;
17225 * We require the extended sense data
17227 esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17228 if (esp->es_class != CLASS_EXTENDED_SENSE) {
17229 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17230 static char tmp[8];
17231 static char buf[148];
17232 char *p = (char *)(xp->xb_sense_data);
17233 int i;
17235 mutex_enter(&sd_sense_mutex);
17236 (void) strcpy(buf, "undecodable sense information:");
17237 for (i = 0; i < actual_len; i++) {
17238 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17239 (void) strcpy(&buf[strlen(buf)], tmp);
17241 i = strlen(buf);
17242 (void) strcpy(&buf[i], "-(assumed fatal)\n");
17244 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17245 scsi_log(SD_DEVINFO(un), sd_label,
17246 CE_WARN, buf);
17248 mutex_exit(&sd_sense_mutex);
17251 /* Mark the ssc_flags for detecting invalid sense data */
17252 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17253 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17254 "sense-data");
17257 /* Note: Legacy behavior, fail the command with no retry */
17258 sd_return_failed_command(un, bp, EIO);
17259 return (SD_SENSE_DATA_IS_INVALID);
17263 * Check that es_code is valid (es_class concatenated with es_code
17264 * make up the "response code" field. es_class will always be 7, so
17265 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the
17266 * format.
17268 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17269 (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17270 (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17271 (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17272 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17273 /* Mark the ssc_flags for detecting invalid sense data */
17274 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17275 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17276 "sense-data");
17278 goto sense_failed;
17281 return (SD_SENSE_DATA_IS_VALID);
17283 sense_failed:
17285 * If the request sense failed (for whatever reason), attempt
17286 * to retry the original command.
17288 #if defined(__i386) || defined(__amd64)
17290 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17291 * sddef.h for Sparc platform, and x86 uses 1 binary
17292 * for both SCSI/FC.
17293 * The SD_RETRY_DELAY value need to be adjusted here
17294 * when SD_RETRY_DELAY change in sddef.h
17296 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17297 sd_print_sense_failed_msg, msgp, EIO,
17298 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17299 #else
17300 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17301 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17302 #endif
17304 return (SD_SENSE_DATA_IS_INVALID);
17308 * Function: sd_decode_sense
17310 * Description: Take recovery action(s) when SCSI Sense Data is received.
17312 * Context: Interrupt context.
17315 static void
17316 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17317 struct scsi_pkt *pktp)
17319 uint8_t sense_key;
17321 ASSERT(un != NULL);
17322 ASSERT(mutex_owned(SD_MUTEX(un)));
17323 ASSERT(bp != NULL);
17324 ASSERT(bp != un->un_rqs_bp);
17325 ASSERT(xp != NULL);
17326 ASSERT(pktp != NULL);
17328 sense_key = scsi_sense_key(xp->xb_sense_data);
17330 switch (sense_key) {
17331 case KEY_NO_SENSE:
17332 sd_sense_key_no_sense(un, bp, xp, pktp);
17333 break;
17334 case KEY_RECOVERABLE_ERROR:
17335 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17336 bp, xp, pktp);
17337 break;
17338 case KEY_NOT_READY:
17339 sd_sense_key_not_ready(un, xp->xb_sense_data,
17340 bp, xp, pktp);
17341 break;
17342 case KEY_MEDIUM_ERROR:
17343 case KEY_HARDWARE_ERROR:
17344 sd_sense_key_medium_or_hardware_error(un,
17345 xp->xb_sense_data, bp, xp, pktp);
17346 break;
17347 case KEY_ILLEGAL_REQUEST:
17348 sd_sense_key_illegal_request(un, bp, xp, pktp);
17349 break;
17350 case KEY_UNIT_ATTENTION:
17351 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17352 bp, xp, pktp);
17353 break;
17354 case KEY_WRITE_PROTECT:
17355 case KEY_VOLUME_OVERFLOW:
17356 case KEY_MISCOMPARE:
17357 sd_sense_key_fail_command(un, bp, xp, pktp);
17358 break;
17359 case KEY_BLANK_CHECK:
17360 sd_sense_key_blank_check(un, bp, xp, pktp);
17361 break;
17362 case KEY_ABORTED_COMMAND:
17363 sd_sense_key_aborted_command(un, bp, xp, pktp);
17364 break;
17365 case KEY_VENDOR_UNIQUE:
17366 case KEY_COPY_ABORTED:
17367 case KEY_EQUAL:
17368 case KEY_RESERVED:
17369 default:
17370 sd_sense_key_default(un, xp->xb_sense_data,
17371 bp, xp, pktp);
17372 break;
17378 * Function: sd_dump_memory
17380 * Description: Debug logging routine to print the contents of a user provided
17381 * buffer. The output of the buffer is broken up into 256 byte
17382 * segments due to a size constraint of the scsi_log.
17383 * implementation.
17385 * Arguments: un - ptr to softstate
17386 * comp - component mask
17387 * title - "title" string to preceed data when printed
17388 * data - ptr to data block to be printed
17389 * len - size of data block to be printed
17390 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17392 * Context: May be called from interrupt context
17395 #define SD_DUMP_MEMORY_BUF_SIZE 256
17397 static char *sd_dump_format_string[] = {
17398 " 0x%02x",
17399 " %c"
17402 static void
17403 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17404 int len, int fmt)
17406 int i, j;
17407 int avail_count;
17408 int start_offset;
17409 int end_offset;
17410 size_t entry_len;
17411 char *bufp;
17412 char *local_buf;
17413 char *format_string;
17415 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17418 * In the debug version of the driver, this function is called from a
17419 * number of places which are NOPs in the release driver.
17420 * The debug driver therefore has additional methods of filtering
17421 * debug output.
17423 #ifdef SDDEBUG
17425 * In the debug version of the driver we can reduce the amount of debug
17426 * messages by setting sd_error_level to something other than
17427 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17428 * sd_component_mask.
17430 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17431 (sd_error_level != SCSI_ERR_ALL)) {
17432 return;
17434 if (((sd_component_mask & comp) == 0) ||
17435 (sd_error_level != SCSI_ERR_ALL)) {
17436 return;
17438 #else
17439 if (sd_error_level != SCSI_ERR_ALL) {
17440 return;
17442 #endif
17444 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17445 bufp = local_buf;
17447 * Available length is the length of local_buf[], minus the
17448 * length of the title string, minus one for the ":", minus
17449 * one for the newline, minus one for the NULL terminator.
17450 * This gives the #bytes available for holding the printed
17451 * values from the given data buffer.
17453 if (fmt == SD_LOG_HEX) {
17454 format_string = sd_dump_format_string[0];
17455 } else /* SD_LOG_CHAR */ {
17456 format_string = sd_dump_format_string[1];
17459 * Available count is the number of elements from the given
17460 * data buffer that we can fit into the available length.
17461 * This is based upon the size of the format string used.
17462 * Make one entry and find it's size.
17464 (void) sprintf(bufp, format_string, data[0]);
17465 entry_len = strlen(bufp);
17466 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17468 j = 0;
17469 while (j < len) {
17470 bufp = local_buf;
17471 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17472 start_offset = j;
17474 end_offset = start_offset + avail_count;
17476 (void) sprintf(bufp, "%s:", title);
17477 bufp += strlen(bufp);
17478 for (i = start_offset; ((i < end_offset) && (j < len));
17479 i++, j++) {
17480 (void) sprintf(bufp, format_string, data[i]);
17481 bufp += entry_len;
17483 (void) sprintf(bufp, "\n");
17485 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17487 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17491 * Function: sd_print_sense_msg
17493 * Description: Log a message based upon the given sense data.
17495 * Arguments: un - ptr to associated softstate
17496 * bp - ptr to buf(9S) for the command
17497 * arg - ptr to associate sd_sense_info struct
17498 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17499 * or SD_NO_RETRY_ISSUED
17501 * Context: May be called from interrupt context
17504 static void
17505 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17507 struct sd_xbuf *xp;
17508 struct scsi_pkt *pktp;
17509 uint8_t *sensep;
17510 daddr_t request_blkno;
17511 diskaddr_t err_blkno;
17512 int severity;
17513 int pfa_flag;
17514 extern struct scsi_key_strings scsi_cmds[];
17516 ASSERT(un != NULL);
17517 ASSERT(mutex_owned(SD_MUTEX(un)));
17518 ASSERT(bp != NULL);
17519 xp = SD_GET_XBUF(bp);
17520 ASSERT(xp != NULL);
17521 pktp = SD_GET_PKTP(bp);
17522 ASSERT(pktp != NULL);
17523 ASSERT(arg != NULL);
17525 severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17526 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17528 if ((code == SD_DELAYED_RETRY_ISSUED) ||
17529 (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17530 severity = SCSI_ERR_RETRYABLE;
17533 /* Use absolute block number for the request block number */
17534 request_blkno = xp->xb_blkno;
17537 * Now try to get the error block number from the sense data
17539 sensep = xp->xb_sense_data;
17541 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17542 (uint64_t *)&err_blkno)) {
17544 * We retrieved the error block number from the information
17545 * portion of the sense data.
17547 * For USCSI commands we are better off using the error
17548 * block no. as the requested block no. (This is the best
17549 * we can estimate.)
17551 if ((SD_IS_BUFIO(xp) == FALSE) &&
17552 ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17553 request_blkno = err_blkno;
17555 } else {
17557 * Without the es_valid bit set (for fixed format) or an
17558 * information descriptor (for descriptor format) we cannot
17559 * be certain of the error blkno, so just use the
17560 * request_blkno.
17562 err_blkno = (diskaddr_t)request_blkno;
17566 * The following will log the buffer contents for the release driver
17567 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17568 * level is set to verbose.
17570 sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17571 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17572 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17573 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17575 if (pfa_flag == FALSE) {
17576 /* This is normally only set for USCSI */
17577 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17578 return;
17581 if ((SD_IS_BUFIO(xp) == TRUE) &&
17582 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17583 (severity < sd_error_level))) {
17584 return;
17588 * Check for Sonoma Failover and keep a count of how many failed I/O's
17590 if ((SD_IS_LSI(un)) &&
17591 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17592 (scsi_sense_asc(sensep) == 0x94) &&
17593 (scsi_sense_ascq(sensep) == 0x01)) {
17594 un->un_sonoma_failure_count++;
17595 if (un->un_sonoma_failure_count > 1) {
17596 return;
17600 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
17601 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
17602 (pktp->pkt_resid == 0))) {
17603 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17604 request_blkno, err_blkno, scsi_cmds,
17605 (struct scsi_extended_sense *)sensep,
17606 un->un_additional_codes, NULL);
17611 * Function: sd_sense_key_no_sense
17613 * Description: Recovery action when sense data was not received.
17615 * Context: May be called from interrupt context
17618 static void
17619 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17620 struct scsi_pkt *pktp)
17622 struct sd_sense_info si;
17624 ASSERT(un != NULL);
17625 ASSERT(mutex_owned(SD_MUTEX(un)));
17626 ASSERT(bp != NULL);
17627 ASSERT(xp != NULL);
17628 ASSERT(pktp != NULL);
17630 si.ssi_severity = SCSI_ERR_FATAL;
17631 si.ssi_pfa_flag = FALSE;
17633 SD_UPDATE_ERRSTATS(un, sd_softerrs);
17635 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17636 &si, EIO, (clock_t)0, NULL);
17641 * Function: sd_sense_key_recoverable_error
17643 * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17645 * Context: May be called from interrupt context
17648 static void
17649 sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
17650 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17652 struct sd_sense_info si;
17653 uint8_t asc = scsi_sense_asc(sense_datap);
17654 uint8_t ascq = scsi_sense_ascq(sense_datap);
17656 ASSERT(un != NULL);
17657 ASSERT(mutex_owned(SD_MUTEX(un)));
17658 ASSERT(bp != NULL);
17659 ASSERT(xp != NULL);
17660 ASSERT(pktp != NULL);
17663 * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
17665 if (asc == 0x00 && ascq == 0x1D) {
17666 sd_return_command(un, bp);
17667 return;
17671 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17673 if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17674 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17675 si.ssi_severity = SCSI_ERR_INFO;
17676 si.ssi_pfa_flag = TRUE;
17677 } else {
17678 SD_UPDATE_ERRSTATS(un, sd_softerrs);
17679 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17680 si.ssi_severity = SCSI_ERR_RECOVERED;
17681 si.ssi_pfa_flag = FALSE;
17684 if (pktp->pkt_resid == 0) {
17685 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17686 sd_return_command(un, bp);
17687 return;
17690 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17691 &si, EIO, (clock_t)0, NULL);
17698 * Function: sd_sense_key_not_ready
17700 * Description: Recovery actions for a SCSI "Not Ready" sense key.
17702 * Context: May be called from interrupt context
17705 static void
17706 sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
17707 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17709 struct sd_sense_info si;
17710 uint8_t asc = scsi_sense_asc(sense_datap);
17711 uint8_t ascq = scsi_sense_ascq(sense_datap);
17713 ASSERT(un != NULL);
17714 ASSERT(mutex_owned(SD_MUTEX(un)));
17715 ASSERT(bp != NULL);
17716 ASSERT(xp != NULL);
17717 ASSERT(pktp != NULL);
17719 si.ssi_severity = SCSI_ERR_FATAL;
17720 si.ssi_pfa_flag = FALSE;
17723 * Update error stats after first NOT READY error. Disks may have
17724 * been powered down and may need to be restarted. For CDROMs,
17725 * report NOT READY errors only if media is present.
17727 if ((ISCD(un) && (asc == 0x3A)) ||
17728 (xp->xb_nr_retry_count > 0)) {
17729 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17730 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17734 * Just fail if the "not ready" retry limit has been reached.
17736 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
17737 /* Special check for error message printing for removables. */
17738 if (un->un_f_has_removable_media && (asc == 0x04) &&
17739 (ascq >= 0x04)) {
17740 si.ssi_severity = SCSI_ERR_ALL;
17742 goto fail_command;
17746 * Check the ASC and ASCQ in the sense data as needed, to determine
17747 * what to do.
17749 switch (asc) {
17750 case 0x04: /* LOGICAL UNIT NOT READY */
17752 * disk drives that don't spin up result in a very long delay
17753 * in format without warning messages. We will log a message
17754 * if the error level is set to verbose.
17756 if (sd_error_level < SCSI_ERR_RETRYABLE) {
17757 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17758 "logical unit not ready, resetting disk\n");
17762 * There are different requirements for CDROMs and disks for
17763 * the number of retries. If a CD-ROM is giving this, it is
17764 * probably reading TOC and is in the process of getting
17765 * ready, so we should keep on trying for a long time to make
17766 * sure that all types of media are taken in account (for
17767 * some media the drive takes a long time to read TOC). For
17768 * disks we do not want to retry this too many times as this
17769 * can cause a long hang in format when the drive refuses to
17770 * spin up (a very common failure).
17772 switch (ascq) {
17773 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */
17775 * Disk drives frequently refuse to spin up which
17776 * results in a very long hang in format without
17777 * warning messages.
17779 * Note: This code preserves the legacy behavior of
17780 * comparing xb_nr_retry_count against zero for fibre
17781 * channel targets instead of comparing against the
17782 * un_reset_retry_count value. The reason for this
17783 * discrepancy has been so utterly lost beneath the
17784 * Sands of Time that even Indiana Jones could not
17785 * find it.
17787 if (un->un_f_is_fibre == TRUE) {
17788 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17789 (xp->xb_nr_retry_count > 0)) &&
17790 (un->un_startstop_timeid == NULL)) {
17791 scsi_log(SD_DEVINFO(un), sd_label,
17792 CE_WARN, "logical unit not ready, "
17793 "resetting disk\n");
17794 sd_reset_target(un, pktp);
17796 } else {
17797 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17798 (xp->xb_nr_retry_count >
17799 un->un_reset_retry_count)) &&
17800 (un->un_startstop_timeid == NULL)) {
17801 scsi_log(SD_DEVINFO(un), sd_label,
17802 CE_WARN, "logical unit not ready, "
17803 "resetting disk\n");
17804 sd_reset_target(un, pktp);
17807 break;
17809 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */
17811 * If the target is in the process of becoming
17812 * ready, just proceed with the retry. This can
17813 * happen with CD-ROMs that take a long time to
17814 * read TOC after a power cycle or reset.
17816 goto do_retry;
17818 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17819 break;
17821 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17823 * Retries cannot help here so just fail right away.
17825 goto fail_command;
17827 case 0x88:
17829 * Vendor-unique code for T3/T4: it indicates a
17830 * path problem in a mutipathed config, but as far as
17831 * the target driver is concerned it equates to a fatal
17832 * error, so we should just fail the command right away
17833 * (without printing anything to the console). If this
17834 * is not a T3/T4, fall thru to the default recovery
17835 * action.
17836 * T3/T4 is FC only, don't need to check is_fibre
17838 if (SD_IS_T3(un) || SD_IS_T4(un)) {
17839 sd_return_failed_command(un, bp, EIO);
17840 return;
17842 /* FALLTHRU */
17844 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */
17845 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */
17846 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */
17847 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */
17848 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */
17849 default: /* Possible future codes in SCSI spec? */
17851 * For removable-media devices, do not retry if
17852 * ASCQ > 2 as these result mostly from USCSI commands
17853 * on MMC devices issued to check status of an
17854 * operation initiated in immediate mode. Also for
17855 * ASCQ >= 4 do not print console messages as these
17856 * mainly represent a user-initiated operation
17857 * instead of a system failure.
17859 if (un->un_f_has_removable_media) {
17860 si.ssi_severity = SCSI_ERR_ALL;
17861 goto fail_command;
17863 break;
17867 * As part of our recovery attempt for the NOT READY
17868 * condition, we issue a START STOP UNIT command. However
17869 * we want to wait for a short delay before attempting this
17870 * as there may still be more commands coming back from the
17871 * target with the check condition. To do this we use
17872 * timeout(9F) to call sd_start_stop_unit_callback() after
17873 * the delay interval expires. (sd_start_stop_unit_callback()
17874 * dispatches sd_start_stop_unit_task(), which will issue
17875 * the actual START STOP UNIT command. The delay interval
17876 * is one-half of the delay that we will use to retry the
17877 * command that generated the NOT READY condition.
17879 * Note that we could just dispatch sd_start_stop_unit_task()
17880 * from here and allow it to sleep for the delay interval,
17881 * but then we would be tying up the taskq thread
17882 * uncesessarily for the duration of the delay.
17884 * Do not issue the START STOP UNIT if the current command
17885 * is already a START STOP UNIT.
17887 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17888 break;
17892 * Do not schedule the timeout if one is already pending.
17894 if (un->un_startstop_timeid != NULL) {
17895 SD_INFO(SD_LOG_ERROR, un,
17896 "sd_sense_key_not_ready: restart already issued to"
17897 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
17898 ddi_get_instance(SD_DEVINFO(un)));
17899 break;
17903 * Schedule the START STOP UNIT command, then queue the command
17904 * for a retry.
17906 * Note: A timeout is not scheduled for this retry because we
17907 * want the retry to be serial with the START_STOP_UNIT. The
17908 * retry will be started when the START_STOP_UNIT is completed
17909 * in sd_start_stop_unit_task.
17911 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
17912 un, un->un_busy_timeout / 2);
17913 xp->xb_nr_retry_count++;
17914 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
17915 return;
17917 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
17918 if (sd_error_level < SCSI_ERR_RETRYABLE) {
17919 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17920 "unit does not respond to selection\n");
17922 break;
17924 case 0x3A: /* MEDIUM NOT PRESENT */
17925 if (sd_error_level >= SCSI_ERR_FATAL) {
17926 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17927 "Caddy not inserted in drive\n");
17930 sr_ejected(un);
17931 un->un_mediastate = DKIO_EJECTED;
17932 /* The state has changed, inform the media watch routines */
17933 cv_broadcast(&un->un_state_cv);
17934 /* Just fail if no media is present in the drive. */
17935 goto fail_command;
17937 default:
17938 if (sd_error_level < SCSI_ERR_RETRYABLE) {
17939 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
17940 "Unit not Ready. Additional sense code 0x%x\n",
17941 asc);
17943 break;
17946 do_retry:
17949 * Retry the command, as some targets may report NOT READY for
17950 * several seconds after being reset.
17952 xp->xb_nr_retry_count++;
17953 si.ssi_severity = SCSI_ERR_RETRYABLE;
17954 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
17955 &si, EIO, un->un_busy_timeout, NULL);
17957 return;
17959 fail_command:
17960 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17961 sd_return_failed_command(un, bp, EIO);
17967 * Function: sd_sense_key_medium_or_hardware_error
17969 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
17970 * sense key.
17972 * Context: May be called from interrupt context
17975 static void
17976 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
17977 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17979 struct sd_sense_info si;
17980 uint8_t sense_key = scsi_sense_key(sense_datap);
17981 uint8_t asc = scsi_sense_asc(sense_datap);
17983 ASSERT(un != NULL);
17984 ASSERT(mutex_owned(SD_MUTEX(un)));
17985 ASSERT(bp != NULL);
17986 ASSERT(xp != NULL);
17987 ASSERT(pktp != NULL);
17989 si.ssi_severity = SCSI_ERR_FATAL;
17990 si.ssi_pfa_flag = FALSE;
17992 if (sense_key == KEY_MEDIUM_ERROR) {
17993 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
17996 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17998 if ((un->un_reset_retry_count != 0) &&
17999 (xp->xb_retry_count == un->un_reset_retry_count)) {
18000 mutex_exit(SD_MUTEX(un));
18001 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18002 if (un->un_f_allow_bus_device_reset == TRUE) {
18004 boolean_t try_resetting_target = B_TRUE;
18007 * We need to be able to handle specific ASC when we are
18008 * handling a KEY_HARDWARE_ERROR. In particular
18009 * taking the default action of resetting the target may
18010 * not be the appropriate way to attempt recovery.
18011 * Resetting a target because of a single LUN failure
18012 * victimizes all LUNs on that target.
18014 * This is true for the LSI arrays, if an LSI
18015 * array controller returns an ASC of 0x84 (LUN Dead) we
18016 * should trust it.
18019 if (sense_key == KEY_HARDWARE_ERROR) {
18020 switch (asc) {
18021 case 0x84:
18022 if (SD_IS_LSI(un)) {
18023 try_resetting_target = B_FALSE;
18025 break;
18026 default:
18027 break;
18031 if (try_resetting_target == B_TRUE) {
18032 int reset_retval = 0;
18033 if (un->un_f_lun_reset_enabled == TRUE) {
18034 SD_TRACE(SD_LOG_IO_CORE, un,
18035 "sd_sense_key_medium_or_hardware_"
18036 "error: issuing RESET_LUN\n");
18037 reset_retval =
18038 scsi_reset(SD_ADDRESS(un),
18039 RESET_LUN);
18041 if (reset_retval == 0) {
18042 SD_TRACE(SD_LOG_IO_CORE, un,
18043 "sd_sense_key_medium_or_hardware_"
18044 "error: issuing RESET_TARGET\n");
18045 (void) scsi_reset(SD_ADDRESS(un),
18046 RESET_TARGET);
18050 mutex_enter(SD_MUTEX(un));
18054 * This really ought to be a fatal error, but we will retry anyway
18055 * as some drives report this as a spurious error.
18057 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18058 &si, EIO, (clock_t)0, NULL);
18064 * Function: sd_sense_key_illegal_request
18066 * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18068 * Context: May be called from interrupt context
18071 static void
18072 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18073 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18075 struct sd_sense_info si;
18077 ASSERT(un != NULL);
18078 ASSERT(mutex_owned(SD_MUTEX(un)));
18079 ASSERT(bp != NULL);
18080 ASSERT(xp != NULL);
18081 ASSERT(pktp != NULL);
18083 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18085 si.ssi_severity = SCSI_ERR_INFO;
18086 si.ssi_pfa_flag = FALSE;
18088 /* Pointless to retry if the target thinks it's an illegal request */
18089 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18090 sd_return_failed_command(un, bp, EIO);
18097 * Function: sd_sense_key_unit_attention
18099 * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18101 * Context: May be called from interrupt context
18104 static void
18105 sd_sense_key_unit_attention(struct sd_lun *un, uint8_t *sense_datap,
18106 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18109 * For UNIT ATTENTION we allow retries for one minute. Devices
18110 * like Sonoma can return UNIT ATTENTION close to a minute
18111 * under certain conditions.
18113 int retry_check_flag = SD_RETRIES_UA;
18114 boolean_t kstat_updated = B_FALSE;
18115 struct sd_sense_info si;
18116 uint8_t asc = scsi_sense_asc(sense_datap);
18117 uint8_t ascq = scsi_sense_ascq(sense_datap);
18119 ASSERT(un != NULL);
18120 ASSERT(mutex_owned(SD_MUTEX(un)));
18121 ASSERT(bp != NULL);
18122 ASSERT(xp != NULL);
18123 ASSERT(pktp != NULL);
18125 si.ssi_severity = SCSI_ERR_INFO;
18126 si.ssi_pfa_flag = FALSE;
18129 switch (asc) {
18130 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18131 if (sd_report_pfa != 0) {
18132 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18133 si.ssi_pfa_flag = TRUE;
18134 retry_check_flag = SD_RETRIES_STANDARD;
18135 goto do_retry;
18138 break;
18140 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18141 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18142 un->un_resvd_status |=
18143 (SD_LOST_RESERVE | SD_WANT_RESERVE);
18145 #ifdef _LP64
18146 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18147 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18148 un, KM_NOSLEEP) == 0) {
18150 * If we can't dispatch the task we'll just
18151 * live without descriptor sense. We can
18152 * try again on the next "unit attention"
18154 SD_ERROR(SD_LOG_ERROR, un,
18155 "sd_sense_key_unit_attention: "
18156 "Could not dispatch "
18157 "sd_reenable_dsense_task\n");
18160 #endif /* _LP64 */
18161 /* FALLTHRU */
18163 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18164 if (!un->un_f_has_removable_media) {
18165 break;
18169 * When we get a unit attention from a removable-media device,
18170 * it may be in a state that will take a long time to recover
18171 * (e.g., from a reset). Since we are executing in interrupt
18172 * context here, we cannot wait around for the device to come
18173 * back. So hand this command off to sd_media_change_task()
18174 * for deferred processing under taskq thread context. (Note
18175 * that the command still may be failed if a problem is
18176 * encountered at a later time.)
18178 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18179 KM_NOSLEEP) == 0) {
18181 * Cannot dispatch the request so fail the command.
18183 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18184 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18185 si.ssi_severity = SCSI_ERR_FATAL;
18186 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18187 sd_return_failed_command(un, bp, EIO);
18191 * If failed to dispatch sd_media_change_task(), we already
18192 * updated kstat. If succeed to dispatch sd_media_change_task(),
18193 * we should update kstat later if it encounters an error. So,
18194 * we update kstat_updated flag here.
18196 kstat_updated = B_TRUE;
18199 * Either the command has been successfully dispatched to a
18200 * task Q for retrying, or the dispatch failed. In either case
18201 * do NOT retry again by calling sd_retry_command. This sets up
18202 * two retries of the same command and when one completes and
18203 * frees the resources the other will access freed memory,
18204 * a bad thing.
18206 return;
18208 default:
18209 break;
18213 * ASC ASCQ
18214 * 2A 09 Capacity data has changed
18215 * 2A 01 Mode parameters changed
18216 * 3F 0E Reported luns data has changed
18217 * Arrays that support logical unit expansion should report
18218 * capacity changes(2Ah/09). Mode parameters changed and
18219 * reported luns data has changed are the approximation.
18221 if (((asc == 0x2a) && (ascq == 0x09)) ||
18222 ((asc == 0x2a) && (ascq == 0x01)) ||
18223 ((asc == 0x3f) && (ascq == 0x0e))) {
18224 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18225 KM_NOSLEEP) == 0) {
18226 SD_ERROR(SD_LOG_ERROR, un,
18227 "sd_sense_key_unit_attention: "
18228 "Could not dispatch sd_target_change_task\n");
18233 * Update kstat if we haven't done that.
18235 if (!kstat_updated) {
18236 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18237 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18240 do_retry:
18241 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18242 EIO, SD_UA_RETRY_DELAY, NULL);
18248 * Function: sd_sense_key_fail_command
18250 * Description: Use to fail a command when we don't like the sense key that
18251 * was returned.
18253 * Context: May be called from interrupt context
18256 static void
18257 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18258 struct scsi_pkt *pktp)
18260 struct sd_sense_info si;
18262 ASSERT(un != NULL);
18263 ASSERT(mutex_owned(SD_MUTEX(un)));
18264 ASSERT(bp != NULL);
18265 ASSERT(xp != NULL);
18266 ASSERT(pktp != NULL);
18268 si.ssi_severity = SCSI_ERR_FATAL;
18269 si.ssi_pfa_flag = FALSE;
18271 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18272 sd_return_failed_command(un, bp, EIO);
18278 * Function: sd_sense_key_blank_check
18280 * Description: Recovery actions for a SCSI "Blank Check" sense key.
18281 * Has no monetary connotation.
18283 * Context: May be called from interrupt context
18286 static void
18287 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18288 struct scsi_pkt *pktp)
18290 struct sd_sense_info si;
18292 ASSERT(un != NULL);
18293 ASSERT(mutex_owned(SD_MUTEX(un)));
18294 ASSERT(bp != NULL);
18295 ASSERT(xp != NULL);
18296 ASSERT(pktp != NULL);
18299 * Blank check is not fatal for removable devices, therefore
18300 * it does not require a console message.
18302 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18303 SCSI_ERR_FATAL;
18304 si.ssi_pfa_flag = FALSE;
18306 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18307 sd_return_failed_command(un, bp, EIO);
18314 * Function: sd_sense_key_aborted_command
18316 * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18318 * Context: May be called from interrupt context
18321 static void
18322 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18323 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18325 struct sd_sense_info si;
18327 ASSERT(un != NULL);
18328 ASSERT(mutex_owned(SD_MUTEX(un)));
18329 ASSERT(bp != NULL);
18330 ASSERT(xp != NULL);
18331 ASSERT(pktp != NULL);
18333 si.ssi_severity = SCSI_ERR_FATAL;
18334 si.ssi_pfa_flag = FALSE;
18336 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18339 * This really ought to be a fatal error, but we will retry anyway
18340 * as some drives report this as a spurious error.
18342 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18343 &si, EIO, drv_usectohz(100000), NULL);
18349 * Function: sd_sense_key_default
18351 * Description: Default recovery action for several SCSI sense keys (basically
18352 * attempts a retry).
18354 * Context: May be called from interrupt context
18357 static void
18358 sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18359 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18361 struct sd_sense_info si;
18362 uint8_t sense_key = scsi_sense_key(sense_datap);
18364 ASSERT(un != NULL);
18365 ASSERT(mutex_owned(SD_MUTEX(un)));
18366 ASSERT(bp != NULL);
18367 ASSERT(xp != NULL);
18368 ASSERT(pktp != NULL);
18370 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18373 * Undecoded sense key. Attempt retries and hope that will fix
18374 * the problem. Otherwise, we're dead.
18376 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18377 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18378 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18381 si.ssi_severity = SCSI_ERR_FATAL;
18382 si.ssi_pfa_flag = FALSE;
18384 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18385 &si, EIO, (clock_t)0, NULL);
18391 * Function: sd_print_retry_msg
18393 * Description: Print a message indicating the retry action being taken.
18395 * Arguments: un - ptr to associated softstate
18396 * bp - ptr to buf(9S) for the command
18397 * arg - not used.
18398 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18399 * or SD_NO_RETRY_ISSUED
18401 * Context: May be called from interrupt context
18403 /* ARGSUSED */
18404 static void
18405 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18407 struct sd_xbuf *xp;
18408 struct scsi_pkt *pktp;
18409 char *reasonp;
18410 char *msgp;
18412 ASSERT(un != NULL);
18413 ASSERT(mutex_owned(SD_MUTEX(un)));
18414 ASSERT(bp != NULL);
18415 pktp = SD_GET_PKTP(bp);
18416 ASSERT(pktp != NULL);
18417 xp = SD_GET_XBUF(bp);
18418 ASSERT(xp != NULL);
18420 ASSERT(!mutex_owned(&un->un_pm_mutex));
18421 mutex_enter(&un->un_pm_mutex);
18422 if ((un->un_state == SD_STATE_SUSPENDED) ||
18423 (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18424 (pktp->pkt_flags & FLAG_SILENT)) {
18425 mutex_exit(&un->un_pm_mutex);
18426 goto update_pkt_reason;
18428 mutex_exit(&un->un_pm_mutex);
18431 * Suppress messages if they are all the same pkt_reason; with
18432 * TQ, many (up to 256) are returned with the same pkt_reason.
18433 * If we are in panic, then suppress the retry messages.
18435 switch (flag) {
18436 case SD_NO_RETRY_ISSUED:
18437 msgp = "giving up";
18438 break;
18439 case SD_IMMEDIATE_RETRY_ISSUED:
18440 case SD_DELAYED_RETRY_ISSUED:
18441 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18442 ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18443 (sd_error_level != SCSI_ERR_ALL))) {
18444 return;
18446 msgp = "retrying command";
18447 break;
18448 default:
18449 goto update_pkt_reason;
18452 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18453 scsi_rname(pktp->pkt_reason));
18455 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18456 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18457 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18460 update_pkt_reason:
18462 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18463 * This is to prevent multiple console messages for the same failure
18464 * condition. Note that un->un_last_pkt_reason is NOT restored if &
18465 * when the command is retried successfully because there still may be
18466 * more commands coming back with the same value of pktp->pkt_reason.
18468 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18469 un->un_last_pkt_reason = pktp->pkt_reason;
18475 * Function: sd_print_cmd_incomplete_msg
18477 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18479 * Arguments: un - ptr to associated softstate
18480 * bp - ptr to buf(9S) for the command
18481 * arg - passed to sd_print_retry_msg()
18482 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18483 * or SD_NO_RETRY_ISSUED
18485 * Context: May be called from interrupt context
18488 static void
18489 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18490 int code)
18492 dev_info_t *dip;
18494 ASSERT(un != NULL);
18495 ASSERT(mutex_owned(SD_MUTEX(un)));
18496 ASSERT(bp != NULL);
18498 switch (code) {
18499 case SD_NO_RETRY_ISSUED:
18500 /* Command was failed. Someone turned off this target? */
18501 if (un->un_state != SD_STATE_OFFLINE) {
18503 * Suppress message if we are detaching and
18504 * device has been disconnected
18505 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18506 * private interface and not part of the DDI
18508 dip = un->un_sd->sd_dev;
18509 if (!(DEVI_IS_DETACHING(dip) &&
18510 DEVI_IS_DEVICE_REMOVED(dip))) {
18511 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18512 "disk not responding to selection\n");
18514 New_state(un, SD_STATE_OFFLINE);
18516 break;
18518 case SD_DELAYED_RETRY_ISSUED:
18519 case SD_IMMEDIATE_RETRY_ISSUED:
18520 default:
18521 /* Command was successfully queued for retry */
18522 sd_print_retry_msg(un, bp, arg, code);
18523 break;
18529 * Function: sd_pkt_reason_cmd_incomplete
18531 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18533 * Context: May be called from interrupt context
18536 static void
18537 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18538 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18540 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18542 ASSERT(un != NULL);
18543 ASSERT(mutex_owned(SD_MUTEX(un)));
18544 ASSERT(bp != NULL);
18545 ASSERT(xp != NULL);
18546 ASSERT(pktp != NULL);
18548 /* Do not do a reset if selection did not complete */
18549 /* Note: Should this not just check the bit? */
18550 if (pktp->pkt_state != STATE_GOT_BUS) {
18551 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18552 sd_reset_target(un, pktp);
18556 * If the target was not successfully selected, then set
18557 * SD_RETRIES_FAILFAST to indicate that we lost communication
18558 * with the target, and further retries and/or commands are
18559 * likely to take a long time.
18561 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18562 flag |= SD_RETRIES_FAILFAST;
18565 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18567 sd_retry_command(un, bp, flag,
18568 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18574 * Function: sd_pkt_reason_cmd_tran_err
18576 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18578 * Context: May be called from interrupt context
18581 static void
18582 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18583 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18585 ASSERT(un != NULL);
18586 ASSERT(mutex_owned(SD_MUTEX(un)));
18587 ASSERT(bp != NULL);
18588 ASSERT(xp != NULL);
18589 ASSERT(pktp != NULL);
18592 * Do not reset if we got a parity error, or if
18593 * selection did not complete.
18595 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18596 /* Note: Should this not just check the bit for pkt_state? */
18597 if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18598 (pktp->pkt_state != STATE_GOT_BUS)) {
18599 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18600 sd_reset_target(un, pktp);
18603 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18605 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18606 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18612 * Function: sd_pkt_reason_cmd_reset
18614 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18616 * Context: May be called from interrupt context
18619 static void
18620 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18621 struct scsi_pkt *pktp)
18623 ASSERT(un != NULL);
18624 ASSERT(mutex_owned(SD_MUTEX(un)));
18625 ASSERT(bp != NULL);
18626 ASSERT(xp != NULL);
18627 ASSERT(pktp != NULL);
18629 /* The target may still be running the command, so try to reset. */
18630 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18631 sd_reset_target(un, pktp);
18633 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18636 * If pkt_reason is CMD_RESET chances are that this pkt got
18637 * reset because another target on this bus caused it. The target
18638 * that caused it should get CMD_TIMEOUT with pkt_statistics
18639 * of STAT_TIMEOUT/STAT_DEV_RESET.
18642 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18643 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18650 * Function: sd_pkt_reason_cmd_aborted
18652 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18654 * Context: May be called from interrupt context
18657 static void
18658 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18659 struct scsi_pkt *pktp)
18661 ASSERT(un != NULL);
18662 ASSERT(mutex_owned(SD_MUTEX(un)));
18663 ASSERT(bp != NULL);
18664 ASSERT(xp != NULL);
18665 ASSERT(pktp != NULL);
18667 /* The target may still be running the command, so try to reset. */
18668 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18669 sd_reset_target(un, pktp);
18671 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18674 * If pkt_reason is CMD_ABORTED chances are that this pkt got
18675 * aborted because another target on this bus caused it. The target
18676 * that caused it should get CMD_TIMEOUT with pkt_statistics
18677 * of STAT_TIMEOUT/STAT_DEV_RESET.
18680 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18681 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18687 * Function: sd_pkt_reason_cmd_timeout
18689 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18691 * Context: May be called from interrupt context
18694 static void
18695 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18696 struct scsi_pkt *pktp)
18698 ASSERT(un != NULL);
18699 ASSERT(mutex_owned(SD_MUTEX(un)));
18700 ASSERT(bp != NULL);
18701 ASSERT(xp != NULL);
18702 ASSERT(pktp != NULL);
18705 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18706 sd_reset_target(un, pktp);
18708 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18711 * A command timeout indicates that we could not establish
18712 * communication with the target, so set SD_RETRIES_FAILFAST
18713 * as further retries/commands are likely to take a long time.
18715 sd_retry_command(un, bp,
18716 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18717 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18723 * Function: sd_pkt_reason_cmd_unx_bus_free
18725 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18727 * Context: May be called from interrupt context
18730 static void
18731 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18732 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18734 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18736 ASSERT(un != NULL);
18737 ASSERT(mutex_owned(SD_MUTEX(un)));
18738 ASSERT(bp != NULL);
18739 ASSERT(xp != NULL);
18740 ASSERT(pktp != NULL);
18742 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18743 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18745 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18746 sd_print_retry_msg : NULL;
18748 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18749 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18754 * Function: sd_pkt_reason_cmd_tag_reject
18756 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18758 * Context: May be called from interrupt context
18761 static void
18762 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18763 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18765 ASSERT(un != NULL);
18766 ASSERT(mutex_owned(SD_MUTEX(un)));
18767 ASSERT(bp != NULL);
18768 ASSERT(xp != NULL);
18769 ASSERT(pktp != NULL);
18771 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18772 pktp->pkt_flags = 0;
18773 un->un_tagflags = 0;
18774 if (un->un_f_opt_queueing == TRUE) {
18775 un->un_throttle = min(un->un_throttle, 3);
18776 } else {
18777 un->un_throttle = 1;
18779 mutex_exit(SD_MUTEX(un));
18780 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18781 mutex_enter(SD_MUTEX(un));
18783 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18785 /* Legacy behavior not to check retry counts here. */
18786 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18787 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18792 * Function: sd_pkt_reason_default
18794 * Description: Default recovery actions for SCSA pkt_reason values that
18795 * do not have more explicit recovery actions.
18797 * Context: May be called from interrupt context
18800 static void
18801 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18802 struct scsi_pkt *pktp)
18804 ASSERT(un != NULL);
18805 ASSERT(mutex_owned(SD_MUTEX(un)));
18806 ASSERT(bp != NULL);
18807 ASSERT(xp != NULL);
18808 ASSERT(pktp != NULL);
18810 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18811 sd_reset_target(un, pktp);
18813 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18815 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18816 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18822 * Function: sd_pkt_status_check_condition
18824 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18826 * Context: May be called from interrupt context
18829 static void
18830 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18831 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18833 ASSERT(un != NULL);
18834 ASSERT(mutex_owned(SD_MUTEX(un)));
18835 ASSERT(bp != NULL);
18836 ASSERT(xp != NULL);
18837 ASSERT(pktp != NULL);
18839 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18840 "entry: buf:0x%p xp:0x%p\n", bp, xp);
18843 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18844 * command will be retried after the request sense). Otherwise, retry
18845 * the command. Note: we are issuing the request sense even though the
18846 * retry limit may have been reached for the failed command.
18848 if (un->un_f_arq_enabled == FALSE) {
18849 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18850 "no ARQ, sending request sense command\n");
18851 sd_send_request_sense_command(un, bp, pktp);
18852 } else {
18853 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18854 "ARQ,retrying request sense command\n");
18855 #if defined(__i386) || defined(__amd64)
18857 * The SD_RETRY_DELAY value need to be adjusted here
18858 * when SD_RETRY_DELAY change in sddef.h
18860 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18861 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18862 NULL);
18863 #else
18864 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18865 EIO, SD_RETRY_DELAY, NULL);
18866 #endif
18869 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18874 * Function: sd_pkt_status_busy
18876 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18878 * Context: May be called from interrupt context
18881 static void
18882 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18883 struct scsi_pkt *pktp)
18885 ASSERT(un != NULL);
18886 ASSERT(mutex_owned(SD_MUTEX(un)));
18887 ASSERT(bp != NULL);
18888 ASSERT(xp != NULL);
18889 ASSERT(pktp != NULL);
18891 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18892 "sd_pkt_status_busy: entry\n");
18894 /* If retries are exhausted, just fail the command. */
18895 if (xp->xb_retry_count >= un->un_busy_retry_count) {
18896 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18897 "device busy too long\n");
18898 sd_return_failed_command(un, bp, EIO);
18899 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18900 "sd_pkt_status_busy: exit\n");
18901 return;
18903 xp->xb_retry_count++;
18906 * Try to reset the target. However, we do not want to perform
18907 * more than one reset if the device continues to fail. The reset
18908 * will be performed when the retry count reaches the reset
18909 * threshold. This threshold should be set such that at least
18910 * one retry is issued before the reset is performed.
18912 if (xp->xb_retry_count ==
18913 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
18914 int rval = 0;
18915 mutex_exit(SD_MUTEX(un));
18916 if (un->un_f_allow_bus_device_reset == TRUE) {
18918 * First try to reset the LUN; if we cannot then
18919 * try to reset the target.
18921 if (un->un_f_lun_reset_enabled == TRUE) {
18922 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18923 "sd_pkt_status_busy: RESET_LUN\n");
18924 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18926 if (rval == 0) {
18927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18928 "sd_pkt_status_busy: RESET_TARGET\n");
18929 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18932 if (rval == 0) {
18934 * If the RESET_LUN and/or RESET_TARGET failed,
18935 * try RESET_ALL
18937 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18938 "sd_pkt_status_busy: RESET_ALL\n");
18939 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
18941 mutex_enter(SD_MUTEX(un));
18942 if (rval == 0) {
18944 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
18945 * At this point we give up & fail the command.
18947 sd_return_failed_command(un, bp, EIO);
18948 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18949 "sd_pkt_status_busy: exit (failed cmd)\n");
18950 return;
18955 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
18956 * we have already checked the retry counts above.
18958 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
18959 EIO, un->un_busy_timeout, NULL);
18961 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18962 "sd_pkt_status_busy: exit\n");
18967 * Function: sd_pkt_status_reservation_conflict
18969 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
18970 * command status.
18972 * Context: May be called from interrupt context
18975 static void
18976 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
18977 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18979 ASSERT(un != NULL);
18980 ASSERT(mutex_owned(SD_MUTEX(un)));
18981 ASSERT(bp != NULL);
18982 ASSERT(xp != NULL);
18983 ASSERT(pktp != NULL);
18986 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
18987 * conflict could be due to various reasons like incorrect keys, not
18988 * registered or not reserved etc. So, we return EACCES to the caller.
18990 if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
18991 int cmd = SD_GET_PKT_OPCODE(pktp);
18992 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
18993 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
18994 sd_return_failed_command(un, bp, EACCES);
18995 return;
18999 un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19001 if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19002 if (sd_failfast_enable != 0) {
19003 /* By definition, we must panic here.... */
19004 sd_panic_for_res_conflict(un);
19005 /*NOTREACHED*/
19007 SD_ERROR(SD_LOG_IO, un,
19008 "sd_handle_resv_conflict: Disk Reserved\n");
19009 sd_return_failed_command(un, bp, EACCES);
19010 return;
19014 * 1147670: retry only if sd_retry_on_reservation_conflict
19015 * property is set (default is 1). Retries will not succeed
19016 * on a disk reserved by another initiator. HA systems
19017 * may reset this via sd.conf to avoid these retries.
19019 * Note: The legacy return code for this failure is EIO, however EACCES
19020 * seems more appropriate for a reservation conflict.
19022 if (sd_retry_on_reservation_conflict == 0) {
19023 SD_ERROR(SD_LOG_IO, un,
19024 "sd_handle_resv_conflict: Device Reserved\n");
19025 sd_return_failed_command(un, bp, EIO);
19026 return;
19030 * Retry the command if we can.
19032 * Note: The legacy return code for this failure is EIO, however EACCES
19033 * seems more appropriate for a reservation conflict.
19035 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19036 (clock_t)2, NULL);
19042 * Function: sd_pkt_status_qfull
19044 * Description: Handle a QUEUE FULL condition from the target. This can
19045 * occur if the HBA does not handle the queue full condition.
19046 * (Basically this means third-party HBAs as Sun HBAs will
19047 * handle the queue full condition.) Note that if there are
19048 * some commands already in the transport, then the queue full
19049 * has occurred because the queue for this nexus is actually
19050 * full. If there are no commands in the transport, then the
19051 * queue full is resulting from some other initiator or lun
19052 * consuming all the resources at the target.
19054 * Context: May be called from interrupt context
19057 static void
19058 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19059 struct scsi_pkt *pktp)
19061 ASSERT(un != NULL);
19062 ASSERT(mutex_owned(SD_MUTEX(un)));
19063 ASSERT(bp != NULL);
19064 ASSERT(xp != NULL);
19065 ASSERT(pktp != NULL);
19067 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19068 "sd_pkt_status_qfull: entry\n");
19071 * Just lower the QFULL throttle and retry the command. Note that
19072 * we do not limit the number of retries here.
19074 sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19075 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19076 SD_RESTART_TIMEOUT, NULL);
19078 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19079 "sd_pkt_status_qfull: exit\n");
19084 * Function: sd_reset_target
19086 * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19087 * RESET_TARGET, or RESET_ALL.
19089 * Context: May be called under interrupt context.
19092 static void
19093 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19095 int rval = 0;
19097 ASSERT(un != NULL);
19098 ASSERT(mutex_owned(SD_MUTEX(un)));
19099 ASSERT(pktp != NULL);
19101 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19104 * No need to reset if the transport layer has already done so.
19106 if ((pktp->pkt_statistics &
19107 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19108 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19109 "sd_reset_target: no reset\n");
19110 return;
19113 mutex_exit(SD_MUTEX(un));
19115 if (un->un_f_allow_bus_device_reset == TRUE) {
19116 if (un->un_f_lun_reset_enabled == TRUE) {
19117 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19118 "sd_reset_target: RESET_LUN\n");
19119 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19121 if (rval == 0) {
19122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19123 "sd_reset_target: RESET_TARGET\n");
19124 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19128 if (rval == 0) {
19129 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19130 "sd_reset_target: RESET_ALL\n");
19131 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19134 mutex_enter(SD_MUTEX(un));
19136 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19140 * Function: sd_target_change_task
19142 * Description: Handle dynamic target change
19144 * Context: Executes in a taskq() thread context
19146 static void
19147 sd_target_change_task(void *arg)
19149 struct sd_lun *un = arg;
19150 uint64_t capacity;
19151 diskaddr_t label_cap;
19152 uint_t lbasize;
19153 sd_ssc_t *ssc;
19155 ASSERT(un != NULL);
19156 ASSERT(!mutex_owned(SD_MUTEX(un)));
19158 if ((un->un_f_blockcount_is_valid == FALSE) ||
19159 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19160 return;
19163 ssc = sd_ssc_init(un);
19165 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19166 &lbasize, SD_PATH_DIRECT) != 0) {
19167 SD_ERROR(SD_LOG_ERROR, un,
19168 "sd_target_change_task: fail to read capacity\n");
19169 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19170 goto task_exit;
19173 mutex_enter(SD_MUTEX(un));
19174 if (capacity <= un->un_blockcount) {
19175 mutex_exit(SD_MUTEX(un));
19176 goto task_exit;
19179 sd_update_block_info(un, lbasize, capacity);
19180 mutex_exit(SD_MUTEX(un));
19183 * If lun is EFI labeled and lun capacity is greater than the
19184 * capacity contained in the label, log a sys event.
19186 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19187 (void*)SD_PATH_DIRECT) == 0) {
19188 mutex_enter(SD_MUTEX(un));
19189 if (un->un_f_blockcount_is_valid &&
19190 un->un_blockcount > label_cap) {
19191 mutex_exit(SD_MUTEX(un));
19192 sd_log_lun_expansion_event(un, KM_SLEEP);
19193 } else {
19194 mutex_exit(SD_MUTEX(un));
19198 task_exit:
19199 sd_ssc_fini(ssc);
19204 * Function: sd_log_dev_status_event
19206 * Description: Log EC_dev_status sysevent
19208 * Context: Never called from interrupt context
19210 static void
19211 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19213 int err;
19214 char *path;
19215 nvlist_t *attr_list;
19217 /* Allocate and build sysevent attribute list */
19218 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19219 if (err != 0) {
19220 SD_ERROR(SD_LOG_ERROR, un,
19221 "sd_log_dev_status_event: fail to allocate space\n");
19222 return;
19225 path = kmem_alloc(MAXPATHLEN, km_flag);
19226 if (path == NULL) {
19227 nvlist_free(attr_list);
19228 SD_ERROR(SD_LOG_ERROR, un,
19229 "sd_log_dev_status_event: fail to allocate space\n");
19230 return;
19233 * Add path attribute to identify the lun.
19234 * We are using minor node 'a' as the sysevent attribute.
19236 (void) snprintf(path, MAXPATHLEN, "/devices");
19237 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19238 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19239 ":a");
19241 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19242 if (err != 0) {
19243 nvlist_free(attr_list);
19244 kmem_free(path, MAXPATHLEN);
19245 SD_ERROR(SD_LOG_ERROR, un,
19246 "sd_log_dev_status_event: fail to add attribute\n");
19247 return;
19250 /* Log dynamic lun expansion sysevent */
19251 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19252 esc, attr_list, NULL, km_flag);
19253 if (err != DDI_SUCCESS) {
19254 SD_ERROR(SD_LOG_ERROR, un,
19255 "sd_log_dev_status_event: fail to log sysevent\n");
19258 nvlist_free(attr_list);
19259 kmem_free(path, MAXPATHLEN);
19264 * Function: sd_log_lun_expansion_event
19266 * Description: Log lun expansion sys event
19268 * Context: Never called from interrupt context
19270 static void
19271 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19273 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19278 * Function: sd_log_eject_request_event
19280 * Description: Log eject request sysevent
19282 * Context: Never called from interrupt context
19284 static void
19285 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19287 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19292 * Function: sd_media_change_task
19294 * Description: Recovery action for CDROM to become available.
19296 * Context: Executes in a taskq() thread context
19299 static void
19300 sd_media_change_task(void *arg)
19302 struct scsi_pkt *pktp = arg;
19303 struct sd_lun *un;
19304 struct buf *bp;
19305 struct sd_xbuf *xp;
19306 int err = 0;
19307 int retry_count = 0;
19308 int retry_limit = SD_UNIT_ATTENTION_RETRY/10;
19309 struct sd_sense_info si;
19311 ASSERT(pktp != NULL);
19312 bp = (struct buf *)pktp->pkt_private;
19313 ASSERT(bp != NULL);
19314 xp = SD_GET_XBUF(bp);
19315 VERIFY(xp != NULL);
19316 un = SD_GET_UN(bp);
19317 ASSERT(un != NULL);
19318 ASSERT(!mutex_owned(SD_MUTEX(un)));
19319 ASSERT(un->un_f_monitor_media_state);
19321 si.ssi_severity = SCSI_ERR_INFO;
19322 si.ssi_pfa_flag = FALSE;
19325 * When a reset is issued on a CDROM, it takes a long time to
19326 * recover. First few attempts to read capacity and other things
19327 * related to handling unit attention fail (with a ASC 0x4 and
19328 * ASCQ 0x1). In that case we want to do enough retries and we want
19329 * to limit the retries in other cases of genuine failures like
19330 * no media in drive.
19332 while (retry_count++ < retry_limit) {
19333 if ((err = sd_handle_mchange(un)) == 0) {
19334 break;
19336 if (err == EAGAIN) {
19337 retry_limit = SD_UNIT_ATTENTION_RETRY;
19339 /* Sleep for 0.5 sec. & try again */
19340 delay(drv_usectohz(500000));
19344 * Dispatch (retry or fail) the original command here,
19345 * along with appropriate console messages....
19347 * Must grab the mutex before calling sd_retry_command,
19348 * sd_print_sense_msg and sd_return_failed_command.
19350 mutex_enter(SD_MUTEX(un));
19351 if (err != SD_CMD_SUCCESS) {
19352 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19353 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19354 si.ssi_severity = SCSI_ERR_FATAL;
19355 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19356 sd_return_failed_command(un, bp, EIO);
19357 } else {
19358 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19359 &si, EIO, (clock_t)0, NULL);
19361 mutex_exit(SD_MUTEX(un));
19367 * Function: sd_handle_mchange
19369 * Description: Perform geometry validation & other recovery when CDROM
19370 * has been removed from drive.
19372 * Return Code: 0 for success
19373 * errno-type return code of either sd_send_scsi_DOORLOCK() or
19374 * sd_send_scsi_READ_CAPACITY()
19376 * Context: Executes in a taskq() thread context
19379 static int
19380 sd_handle_mchange(struct sd_lun *un)
19382 uint64_t capacity;
19383 uint32_t lbasize;
19384 int rval;
19385 sd_ssc_t *ssc;
19387 ASSERT(!mutex_owned(SD_MUTEX(un)));
19388 ASSERT(un->un_f_monitor_media_state);
19390 ssc = sd_ssc_init(un);
19391 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19392 SD_PATH_DIRECT_PRIORITY);
19394 if (rval != 0)
19395 goto failed;
19397 mutex_enter(SD_MUTEX(un));
19398 sd_update_block_info(un, lbasize, capacity);
19400 if (un->un_errstats != NULL) {
19401 struct sd_errstats *stp =
19402 (struct sd_errstats *)un->un_errstats->ks_data;
19403 stp->sd_capacity.value.ui64 = (uint64_t)
19404 ((uint64_t)un->un_blockcount *
19405 (uint64_t)un->un_tgt_blocksize);
19409 * Check if the media in the device is writable or not
19411 if (ISCD(un)) {
19412 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19416 * Note: Maybe let the strategy/partitioning chain worry about getting
19417 * valid geometry.
19419 mutex_exit(SD_MUTEX(un));
19420 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19423 if (cmlb_validate(un->un_cmlbhandle, 0,
19424 (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19425 sd_ssc_fini(ssc);
19426 return (EIO);
19427 } else {
19428 if (un->un_f_pkstats_enabled) {
19429 sd_set_pstats(un);
19430 SD_TRACE(SD_LOG_IO_PARTITION, un,
19431 "sd_handle_mchange: un:0x%p pstats created and "
19432 "set\n", un);
19437 * Try to lock the door
19439 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19440 SD_PATH_DIRECT_PRIORITY);
19441 failed:
19442 if (rval != 0)
19443 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19444 sd_ssc_fini(ssc);
19445 return (rval);
19450 * Function: sd_send_scsi_DOORLOCK
19452 * Description: Issue the scsi DOOR LOCK command
19454 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
19455 * structure for this target.
19456 * flag - SD_REMOVAL_ALLOW
19457 * SD_REMOVAL_PREVENT
19458 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19459 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19460 * to use the USCSI "direct" chain and bypass the normal
19461 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19462 * command is issued as part of an error recovery action.
19464 * Return Code: 0 - Success
19465 * errno return code from sd_ssc_send()
19467 * Context: Can sleep.
19470 static int
19471 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19473 struct scsi_extended_sense sense_buf;
19474 union scsi_cdb cdb;
19475 struct uscsi_cmd ucmd_buf;
19476 int status;
19477 struct sd_lun *un;
19479 ASSERT(ssc != NULL);
19480 un = ssc->ssc_un;
19481 ASSERT(un != NULL);
19482 ASSERT(!mutex_owned(SD_MUTEX(un)));
19484 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19486 /* already determined doorlock is not supported, fake success */
19487 if (un->un_f_doorlock_supported == FALSE) {
19488 return (0);
19492 * If we are ejecting and see an SD_REMOVAL_PREVENT
19493 * ignore the command so we can complete the eject
19494 * operation.
19496 if (flag == SD_REMOVAL_PREVENT) {
19497 mutex_enter(SD_MUTEX(un));
19498 if (un->un_f_ejecting == TRUE) {
19499 mutex_exit(SD_MUTEX(un));
19500 return (EAGAIN);
19502 mutex_exit(SD_MUTEX(un));
19505 bzero(&cdb, sizeof (cdb));
19506 bzero(&ucmd_buf, sizeof (ucmd_buf));
19508 cdb.scc_cmd = SCMD_DOORLOCK;
19509 cdb.cdb_opaque[4] = (uchar_t)flag;
19511 ucmd_buf.uscsi_cdb = (char *)&cdb;
19512 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
19513 ucmd_buf.uscsi_bufaddr = NULL;
19514 ucmd_buf.uscsi_buflen = 0;
19515 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19516 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19517 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
19518 ucmd_buf.uscsi_timeout = 15;
19520 SD_TRACE(SD_LOG_IO, un,
19521 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19523 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19524 UIO_SYSSPACE, path_flag);
19526 if (status == 0)
19527 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19529 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19530 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19531 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19532 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19534 /* fake success and skip subsequent doorlock commands */
19535 un->un_f_doorlock_supported = FALSE;
19536 return (0);
19539 return (status);
19543 * Function: sd_send_scsi_READ_CAPACITY
19545 * Description: This routine uses the scsi READ CAPACITY command to determine
19546 * the device capacity in number of blocks and the device native
19547 * block size. If this function returns a failure, then the
19548 * values in *capp and *lbap are undefined. If the capacity
19549 * returned is 0xffffffff then the lun is too large for a
19550 * normal READ CAPACITY command and the results of a
19551 * READ CAPACITY 16 will be used instead.
19553 * Arguments: ssc - ssc contains ptr to soft state struct for the target
19554 * capp - ptr to unsigned 64-bit variable to receive the
19555 * capacity value from the command.
19556 * lbap - ptr to unsigned 32-bit varaible to receive the
19557 * block size value from the command
19558 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19559 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19560 * to use the USCSI "direct" chain and bypass the normal
19561 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19562 * command is issued as part of an error recovery action.
19564 * Return Code: 0 - Success
19565 * EIO - IO error
19566 * EACCES - Reservation conflict detected
19567 * EAGAIN - Device is becoming ready
19568 * errno return code from sd_ssc_send()
19570 * Context: Can sleep. Blocks until command completes.
19573 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity)
19575 static int
19576 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19577 int path_flag)
19579 struct scsi_extended_sense sense_buf;
19580 struct uscsi_cmd ucmd_buf;
19581 union scsi_cdb cdb;
19582 uint32_t *capacity_buf;
19583 uint64_t capacity;
19584 uint32_t lbasize;
19585 uint32_t pbsize;
19586 int status;
19587 struct sd_lun *un;
19589 ASSERT(ssc != NULL);
19591 un = ssc->ssc_un;
19592 ASSERT(un != NULL);
19593 ASSERT(!mutex_owned(SD_MUTEX(un)));
19594 ASSERT(capp != NULL);
19595 ASSERT(lbap != NULL);
19597 SD_TRACE(SD_LOG_IO, un,
19598 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19601 * First send a READ_CAPACITY command to the target.
19602 * (This command is mandatory under SCSI-2.)
19604 * Set up the CDB for the READ_CAPACITY command. The Partial
19605 * Medium Indicator bit is cleared. The address field must be
19606 * zero if the PMI bit is zero.
19608 bzero(&cdb, sizeof (cdb));
19609 bzero(&ucmd_buf, sizeof (ucmd_buf));
19611 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19613 cdb.scc_cmd = SCMD_READ_CAPACITY;
19615 ucmd_buf.uscsi_cdb = (char *)&cdb;
19616 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
19617 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
19618 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
19619 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19620 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19621 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19622 ucmd_buf.uscsi_timeout = 60;
19624 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19625 UIO_SYSSPACE, path_flag);
19627 switch (status) {
19628 case 0:
19629 /* Return failure if we did not get valid capacity data. */
19630 if (ucmd_buf.uscsi_resid != 0) {
19631 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19632 "sd_send_scsi_READ_CAPACITY received invalid "
19633 "capacity data");
19634 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19635 return (EIO);
19638 * Read capacity and block size from the READ CAPACITY 10 data.
19639 * This data may be adjusted later due to device specific
19640 * issues.
19642 * According to the SCSI spec, the READ CAPACITY 10
19643 * command returns the following:
19645 * bytes 0-3: Maximum logical block address available.
19646 * (MSB in byte:0 & LSB in byte:3)
19648 * bytes 4-7: Block length in bytes
19649 * (MSB in byte:4 & LSB in byte:7)
19652 capacity = BE_32(capacity_buf[0]);
19653 lbasize = BE_32(capacity_buf[1]);
19656 * Done with capacity_buf
19658 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19661 * if the reported capacity is set to all 0xf's, then
19662 * this disk is too large and requires SBC-2 commands.
19663 * Reissue the request using READ CAPACITY 16.
19665 if (capacity == 0xffffffff) {
19666 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19667 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
19668 &lbasize, &pbsize, path_flag);
19669 if (status != 0) {
19670 return (status);
19671 } else {
19672 goto rc16_done;
19675 break; /* Success! */
19676 case EIO:
19677 switch (ucmd_buf.uscsi_status) {
19678 case STATUS_RESERVATION_CONFLICT:
19679 status = EACCES;
19680 break;
19681 case STATUS_CHECK:
19683 * Check condition; look for ASC/ASCQ of 0x04/0x01
19684 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19686 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19687 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19688 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19689 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19690 return (EAGAIN);
19692 break;
19693 default:
19694 break;
19696 /* FALLTHRU */
19697 default:
19698 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19699 return (status);
19703 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19704 * (2352 and 0 are common) so for these devices always force the value
19705 * to 2048 as required by the ATAPI specs.
19707 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19708 lbasize = 2048;
19712 * Get the maximum LBA value from the READ CAPACITY data.
19713 * Here we assume that the Partial Medium Indicator (PMI) bit
19714 * was cleared when issuing the command. This means that the LBA
19715 * returned from the device is the LBA of the last logical block
19716 * on the logical unit. The actual logical block count will be
19717 * this value plus one.
19719 capacity += 1;
19722 * Currently, for removable media, the capacity is saved in terms
19723 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
19725 if (un->un_f_has_removable_media)
19726 capacity *= (lbasize / un->un_sys_blocksize);
19728 rc16_done:
19731 * Copy the values from the READ CAPACITY command into the space
19732 * provided by the caller.
19734 *capp = capacity;
19735 *lbap = lbasize;
19737 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19738 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize);
19741 * Both the lbasize and capacity from the device must be nonzero,
19742 * otherwise we assume that the values are not valid and return
19743 * failure to the caller. (4203735)
19745 if ((capacity == 0) || (lbasize == 0)) {
19746 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19747 "sd_send_scsi_READ_CAPACITY received invalid value "
19748 "capacity %llu lbasize %d", capacity, lbasize);
19749 return (EIO);
19751 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19752 return (0);
19756 * Function: sd_send_scsi_READ_CAPACITY_16
19758 * Description: This routine uses the scsi READ CAPACITY 16 command to
19759 * determine the device capacity in number of blocks and the
19760 * device native block size. If this function returns a failure,
19761 * then the values in *capp and *lbap are undefined.
19762 * This routine should be called by sd_send_scsi_READ_CAPACITY
19763 * which will apply any device specific adjustments to capacity
19764 * and lbasize. One exception is it is also called by
19765 * sd_get_media_info_ext. In that function, there is no need to
19766 * adjust the capacity and lbasize.
19768 * Arguments: ssc - ssc contains ptr to soft state struct for the target
19769 * capp - ptr to unsigned 64-bit variable to receive the
19770 * capacity value from the command.
19771 * lbap - ptr to unsigned 32-bit varaible to receive the
19772 * block size value from the command
19773 * psp - ptr to unsigned 32-bit variable to receive the
19774 * physical block size value from the command
19775 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19776 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19777 * to use the USCSI "direct" chain and bypass the normal
19778 * command waitq. SD_PATH_DIRECT_PRIORITY is used when
19779 * this command is issued as part of an error recovery
19780 * action.
19782 * Return Code: 0 - Success
19783 * EIO - IO error
19784 * EACCES - Reservation conflict detected
19785 * EAGAIN - Device is becoming ready
19786 * errno return code from sd_ssc_send()
19788 * Context: Can sleep. Blocks until command completes.
19791 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16)
19793 static int
19794 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19795 uint32_t *psp, int path_flag)
19797 struct scsi_extended_sense sense_buf;
19798 struct uscsi_cmd ucmd_buf;
19799 union scsi_cdb cdb;
19800 uint64_t *capacity16_buf;
19801 uint64_t capacity;
19802 uint32_t lbasize;
19803 uint32_t pbsize;
19804 uint32_t lbpb_exp;
19805 int status;
19806 struct sd_lun *un;
19808 ASSERT(ssc != NULL);
19810 un = ssc->ssc_un;
19811 ASSERT(un != NULL);
19812 ASSERT(!mutex_owned(SD_MUTEX(un)));
19813 ASSERT(capp != NULL);
19814 ASSERT(lbap != NULL);
19816 SD_TRACE(SD_LOG_IO, un,
19817 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19820 * First send a READ_CAPACITY_16 command to the target.
19822 * Set up the CDB for the READ_CAPACITY_16 command. The Partial
19823 * Medium Indicator bit is cleared. The address field must be
19824 * zero if the PMI bit is zero.
19826 bzero(&cdb, sizeof (cdb));
19827 bzero(&ucmd_buf, sizeof (ucmd_buf));
19829 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
19831 ucmd_buf.uscsi_cdb = (char *)&cdb;
19832 ucmd_buf.uscsi_cdblen = CDB_GROUP4;
19833 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
19834 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
19835 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19836 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19837 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19838 ucmd_buf.uscsi_timeout = 60;
19841 * Read Capacity (16) is a Service Action In command. One
19842 * command byte (0x9E) is overloaded for multiple operations,
19843 * with the second CDB byte specifying the desired operation
19845 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
19846 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
19849 * Fill in allocation length field
19851 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
19853 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19854 UIO_SYSSPACE, path_flag);
19856 switch (status) {
19857 case 0:
19858 /* Return failure if we did not get valid capacity data. */
19859 if (ucmd_buf.uscsi_resid > 20) {
19860 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19861 "sd_send_scsi_READ_CAPACITY_16 received invalid "
19862 "capacity data");
19863 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19864 return (EIO);
19868 * Read capacity and block size from the READ CAPACITY 16 data.
19869 * This data may be adjusted later due to device specific
19870 * issues.
19872 * According to the SCSI spec, the READ CAPACITY 16
19873 * command returns the following:
19875 * bytes 0-7: Maximum logical block address available.
19876 * (MSB in byte:0 & LSB in byte:7)
19878 * bytes 8-11: Block length in bytes
19879 * (MSB in byte:8 & LSB in byte:11)
19881 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
19883 * byte 14:
19884 * bit 7: Thin-Provisioning Enabled
19885 * bit 6: Thin-Provisioning Read Zeros
19887 capacity = BE_64(capacity16_buf[0]);
19888 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
19889 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
19891 un->un_thin_flags = 0;
19892 if (((uint8_t *)capacity16_buf)[14] & (1 << 7))
19893 un->un_thin_flags |= SD_THIN_PROV_ENABLED;
19894 if (((uint8_t *)capacity16_buf)[14] & (1 << 6))
19895 un->un_thin_flags |= SD_THIN_PROV_READ_ZEROS;
19897 pbsize = lbasize << lbpb_exp;
19900 * Done with capacity16_buf
19902 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19905 * if the reported capacity is set to all 0xf's, then
19906 * this disk is too large. This could only happen with
19907 * a device that supports LBAs larger than 64 bits which
19908 * are not defined by any current T10 standards.
19910 if (capacity == 0xffffffffffffffff) {
19911 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19912 "disk is too large");
19913 return (EIO);
19915 break; /* Success! */
19916 case EIO:
19917 switch (ucmd_buf.uscsi_status) {
19918 case STATUS_RESERVATION_CONFLICT:
19919 status = EACCES;
19920 break;
19921 case STATUS_CHECK:
19923 * Check condition; look for ASC/ASCQ of 0x04/0x01
19924 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19926 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19927 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19928 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19929 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19930 return (EAGAIN);
19932 break;
19933 default:
19934 break;
19936 /* FALLTHRU */
19937 default:
19938 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19939 return (status);
19943 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19944 * (2352 and 0 are common) so for these devices always force the value
19945 * to 2048 as required by the ATAPI specs.
19947 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19948 lbasize = 2048;
19952 * Get the maximum LBA value from the READ CAPACITY 16 data.
19953 * Here we assume that the Partial Medium Indicator (PMI) bit
19954 * was cleared when issuing the command. This means that the LBA
19955 * returned from the device is the LBA of the last logical block
19956 * on the logical unit. The actual logical block count will be
19957 * this value plus one.
19959 capacity += 1;
19962 * Currently, for removable media, the capacity is saved in terms
19963 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
19965 if (un->un_f_has_removable_media)
19966 capacity *= (lbasize / un->un_sys_blocksize);
19968 *capp = capacity;
19969 *lbap = lbasize;
19970 *psp = pbsize;
19972 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19973 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n",
19974 capacity, lbasize, pbsize);
19976 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
19977 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19978 "sd_send_scsi_READ_CAPACITY_16 received invalid value "
19979 "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
19980 return (EIO);
19983 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19984 return (0);
19989 * Function: sd_send_scsi_START_STOP_UNIT
19991 * Description: Issue a scsi START STOP UNIT command to the target.
19993 * Arguments: ssc - ssc contatins pointer to driver soft state (unit)
19994 * structure for this target.
19995 * pc_flag - SD_POWER_CONDITION
19996 * SD_START_STOP
19997 * flag - SD_TARGET_START
19998 * SD_TARGET_STOP
19999 * SD_TARGET_EJECT
20000 * SD_TARGET_CLOSE
20001 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20002 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20003 * to use the USCSI "direct" chain and bypass the normal
20004 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20005 * command is issued as part of an error recovery action.
20007 * Return Code: 0 - Success
20008 * EIO - IO error
20009 * EACCES - Reservation conflict detected
20010 * ENXIO - Not Ready, medium not present
20011 * errno return code from sd_ssc_send()
20013 * Context: Can sleep.
20016 static int
20017 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20018 int path_flag)
20020 struct scsi_extended_sense sense_buf;
20021 union scsi_cdb cdb;
20022 struct uscsi_cmd ucmd_buf;
20023 int status;
20024 struct sd_lun *un;
20026 ASSERT(ssc != NULL);
20027 un = ssc->ssc_un;
20028 ASSERT(un != NULL);
20029 ASSERT(!mutex_owned(SD_MUTEX(un)));
20031 SD_TRACE(SD_LOG_IO, un,
20032 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20034 if (un->un_f_check_start_stop &&
20035 (pc_flag == SD_START_STOP) &&
20036 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20037 (un->un_f_start_stop_supported != TRUE)) {
20038 return (0);
20042 * If we are performing an eject operation and
20043 * we receive any command other than SD_TARGET_EJECT
20044 * we should immediately return.
20046 if (flag != SD_TARGET_EJECT) {
20047 mutex_enter(SD_MUTEX(un));
20048 if (un->un_f_ejecting == TRUE) {
20049 mutex_exit(SD_MUTEX(un));
20050 return (EAGAIN);
20052 mutex_exit(SD_MUTEX(un));
20055 bzero(&cdb, sizeof (cdb));
20056 bzero(&ucmd_buf, sizeof (ucmd_buf));
20057 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20059 cdb.scc_cmd = SCMD_START_STOP;
20060 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20061 (uchar_t)(flag << 4) : (uchar_t)flag;
20063 ucmd_buf.uscsi_cdb = (char *)&cdb;
20064 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20065 ucmd_buf.uscsi_bufaddr = NULL;
20066 ucmd_buf.uscsi_buflen = 0;
20067 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20068 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20069 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20070 ucmd_buf.uscsi_timeout = 200;
20072 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20073 UIO_SYSSPACE, path_flag);
20075 switch (status) {
20076 case 0:
20077 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20078 break; /* Success! */
20079 case EIO:
20080 switch (ucmd_buf.uscsi_status) {
20081 case STATUS_RESERVATION_CONFLICT:
20082 status = EACCES;
20083 break;
20084 case STATUS_CHECK:
20085 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20086 switch (scsi_sense_key(
20087 (uint8_t *)&sense_buf)) {
20088 case KEY_ILLEGAL_REQUEST:
20089 status = ENOTSUP;
20090 break;
20091 case KEY_NOT_READY:
20092 if (scsi_sense_asc(
20093 (uint8_t *)&sense_buf)
20094 == 0x3A) {
20095 status = ENXIO;
20097 break;
20098 default:
20099 break;
20102 break;
20103 default:
20104 break;
20106 break;
20107 default:
20108 break;
20111 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20113 return (status);
20118 * Function: sd_start_stop_unit_callback
20120 * Description: timeout(9F) callback to begin recovery process for a
20121 * device that has spun down.
20123 * Arguments: arg - pointer to associated softstate struct.
20125 * Context: Executes in a timeout(9F) thread context
20128 static void
20129 sd_start_stop_unit_callback(void *arg)
20131 struct sd_lun *un = arg;
20132 ASSERT(un != NULL);
20133 ASSERT(!mutex_owned(SD_MUTEX(un)));
20135 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20137 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20142 * Function: sd_start_stop_unit_task
20144 * Description: Recovery procedure when a drive is spun down.
20146 * Arguments: arg - pointer to associated softstate struct.
20148 * Context: Executes in a taskq() thread context
20151 static void
20152 sd_start_stop_unit_task(void *arg)
20154 struct sd_lun *un = arg;
20155 sd_ssc_t *ssc;
20156 int power_level;
20157 int rval;
20159 ASSERT(un != NULL);
20160 ASSERT(!mutex_owned(SD_MUTEX(un)));
20162 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20165 * Some unformatted drives report not ready error, no need to
20166 * restart if format has been initiated.
20168 mutex_enter(SD_MUTEX(un));
20169 if (un->un_f_format_in_progress == TRUE) {
20170 mutex_exit(SD_MUTEX(un));
20171 return;
20173 mutex_exit(SD_MUTEX(un));
20175 ssc = sd_ssc_init(un);
20177 * When a START STOP command is issued from here, it is part of a
20178 * failure recovery operation and must be issued before any other
20179 * commands, including any pending retries. Thus it must be sent
20180 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20181 * succeeds or not, we will start I/O after the attempt.
20182 * If power condition is supported and the current power level
20183 * is capable of performing I/O, we should set the power condition
20184 * to that level. Otherwise, set the power condition to ACTIVE.
20186 if (un->un_f_power_condition_supported) {
20187 mutex_enter(SD_MUTEX(un));
20188 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20189 power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20190 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20191 mutex_exit(SD_MUTEX(un));
20192 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20193 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20194 } else {
20195 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20196 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20199 if (rval != 0)
20200 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20201 sd_ssc_fini(ssc);
20203 * The above call blocks until the START_STOP_UNIT command completes.
20204 * Now that it has completed, we must re-try the original IO that
20205 * received the NOT READY condition in the first place. There are
20206 * three possible conditions here:
20208 * (1) The original IO is on un_retry_bp.
20209 * (2) The original IO is on the regular wait queue, and un_retry_bp
20210 * is NULL.
20211 * (3) The original IO is on the regular wait queue, and un_retry_bp
20212 * points to some other, unrelated bp.
20214 * For each case, we must call sd_start_cmds() with un_retry_bp
20215 * as the argument. If un_retry_bp is NULL, this will initiate
20216 * processing of the regular wait queue. If un_retry_bp is not NULL,
20217 * then this will process the bp on un_retry_bp. That may or may not
20218 * be the original IO, but that does not matter: the important thing
20219 * is to keep the IO processing going at this point.
20221 * Note: This is a very specific error recovery sequence associated
20222 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20223 * serialize the I/O with completion of the spin-up.
20225 mutex_enter(SD_MUTEX(un));
20226 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20227 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20228 un, un->un_retry_bp);
20229 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */
20230 sd_start_cmds(un, un->un_retry_bp);
20231 mutex_exit(SD_MUTEX(un));
20233 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20238 * Function: sd_send_scsi_INQUIRY
20240 * Description: Issue the scsi INQUIRY command.
20242 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20243 * structure for this target.
20244 * bufaddr
20245 * buflen
20246 * evpd
20247 * page_code
20248 * page_length
20250 * Return Code: 0 - Success
20251 * errno return code from sd_ssc_send()
20253 * Context: Can sleep. Does not return until command is completed.
20256 static int
20257 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20258 uchar_t evpd, uchar_t page_code, size_t *residp)
20260 union scsi_cdb cdb;
20261 struct uscsi_cmd ucmd_buf;
20262 int status;
20263 struct sd_lun *un;
20265 ASSERT(ssc != NULL);
20266 un = ssc->ssc_un;
20267 ASSERT(un != NULL);
20268 ASSERT(!mutex_owned(SD_MUTEX(un)));
20269 ASSERT(bufaddr != NULL);
20271 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20273 bzero(&cdb, sizeof (cdb));
20274 bzero(&ucmd_buf, sizeof (ucmd_buf));
20275 bzero(bufaddr, buflen);
20277 cdb.scc_cmd = SCMD_INQUIRY;
20278 cdb.cdb_opaque[1] = evpd;
20279 cdb.cdb_opaque[2] = page_code;
20280 FORMG0COUNT(&cdb, buflen);
20282 ucmd_buf.uscsi_cdb = (char *)&cdb;
20283 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20284 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
20285 ucmd_buf.uscsi_buflen = buflen;
20286 ucmd_buf.uscsi_rqbuf = NULL;
20287 ucmd_buf.uscsi_rqlen = 0;
20288 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
20289 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */
20291 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20292 UIO_SYSSPACE, SD_PATH_DIRECT);
20295 * Only handle status == 0, the upper-level caller
20296 * will put different assessment based on the context.
20298 if (status == 0)
20299 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20301 if ((status == 0) && (residp != NULL)) {
20302 *residp = ucmd_buf.uscsi_resid;
20305 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20307 return (status);
20312 * Function: sd_send_scsi_TEST_UNIT_READY
20314 * Description: Issue the scsi TEST UNIT READY command.
20315 * This routine can be told to set the flag USCSI_DIAGNOSE to
20316 * prevent retrying failed commands. Use this when the intent
20317 * is either to check for device readiness, to clear a Unit
20318 * Attention, or to clear any outstanding sense data.
20319 * However under specific conditions the expected behavior
20320 * is for retries to bring a device ready, so use the flag
20321 * with caution.
20323 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20324 * structure for this target.
20325 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20326 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20327 * 0: dont check for media present, do retries on cmd.
20329 * Return Code: 0 - Success
20330 * EIO - IO error
20331 * EACCES - Reservation conflict detected
20332 * ENXIO - Not Ready, medium not present
20333 * errno return code from sd_ssc_send()
20335 * Context: Can sleep. Does not return until command is completed.
20338 static int
20339 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20341 struct scsi_extended_sense sense_buf;
20342 union scsi_cdb cdb;
20343 struct uscsi_cmd ucmd_buf;
20344 int status;
20345 struct sd_lun *un;
20347 ASSERT(ssc != NULL);
20348 un = ssc->ssc_un;
20349 ASSERT(un != NULL);
20350 ASSERT(!mutex_owned(SD_MUTEX(un)));
20352 SD_TRACE(SD_LOG_IO, un,
20353 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20356 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20357 * timeouts when they receive a TUR and the queue is not empty. Check
20358 * the configuration flag set during attach (indicating the drive has
20359 * this firmware bug) and un_ncmds_in_transport before issuing the
20360 * TUR. If there are
20361 * pending commands return success, this is a bit arbitrary but is ok
20362 * for non-removables (i.e. the eliteI disks) and non-clustering
20363 * configurations.
20365 if (un->un_f_cfg_tur_check == TRUE) {
20366 mutex_enter(SD_MUTEX(un));
20367 if (un->un_ncmds_in_transport != 0) {
20368 mutex_exit(SD_MUTEX(un));
20369 return (0);
20371 mutex_exit(SD_MUTEX(un));
20374 bzero(&cdb, sizeof (cdb));
20375 bzero(&ucmd_buf, sizeof (ucmd_buf));
20376 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20378 cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20380 ucmd_buf.uscsi_cdb = (char *)&cdb;
20381 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20382 ucmd_buf.uscsi_bufaddr = NULL;
20383 ucmd_buf.uscsi_buflen = 0;
20384 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20385 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20386 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20388 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20389 if ((flag & SD_DONT_RETRY_TUR) != 0) {
20390 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20392 ucmd_buf.uscsi_timeout = 60;
20394 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20395 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20396 SD_PATH_STANDARD));
20398 switch (status) {
20399 case 0:
20400 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20401 break; /* Success! */
20402 case EIO:
20403 switch (ucmd_buf.uscsi_status) {
20404 case STATUS_RESERVATION_CONFLICT:
20405 status = EACCES;
20406 break;
20407 case STATUS_CHECK:
20408 if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20409 break;
20411 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20412 (scsi_sense_key((uint8_t *)&sense_buf) ==
20413 KEY_NOT_READY) &&
20414 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20415 status = ENXIO;
20417 break;
20418 default:
20419 break;
20421 break;
20422 default:
20423 break;
20426 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20428 return (status);
20432 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20434 * Description: Issue the scsi PERSISTENT RESERVE IN command.
20436 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
20437 * structure for this target.
20439 * Return Code: 0 - Success
20440 * EACCES
20441 * ENOTSUP
20442 * errno return code from sd_ssc_send()
20444 * Context: Can sleep. Does not return until command is completed.
20447 static int
20448 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
20449 uint16_t data_len, uchar_t *data_bufp)
20451 struct scsi_extended_sense sense_buf;
20452 union scsi_cdb cdb;
20453 struct uscsi_cmd ucmd_buf;
20454 int status;
20455 int no_caller_buf = FALSE;
20456 struct sd_lun *un;
20458 ASSERT(ssc != NULL);
20459 un = ssc->ssc_un;
20460 ASSERT(un != NULL);
20461 ASSERT(!mutex_owned(SD_MUTEX(un)));
20462 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20464 SD_TRACE(SD_LOG_IO, un,
20465 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20467 bzero(&cdb, sizeof (cdb));
20468 bzero(&ucmd_buf, sizeof (ucmd_buf));
20469 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20470 if (data_bufp == NULL) {
20471 /* Allocate a default buf if the caller did not give one */
20472 ASSERT(data_len == 0);
20473 data_len = MHIOC_RESV_KEY_SIZE;
20474 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20475 no_caller_buf = TRUE;
20478 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20479 cdb.cdb_opaque[1] = usr_cmd;
20480 FORMG1COUNT(&cdb, data_len);
20482 ucmd_buf.uscsi_cdb = (char *)&cdb;
20483 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20484 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp;
20485 ucmd_buf.uscsi_buflen = data_len;
20486 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20487 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20488 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20489 ucmd_buf.uscsi_timeout = 60;
20491 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20492 UIO_SYSSPACE, SD_PATH_STANDARD);
20494 switch (status) {
20495 case 0:
20496 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20498 break; /* Success! */
20499 case EIO:
20500 switch (ucmd_buf.uscsi_status) {
20501 case STATUS_RESERVATION_CONFLICT:
20502 status = EACCES;
20503 break;
20504 case STATUS_CHECK:
20505 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20506 (scsi_sense_key((uint8_t *)&sense_buf) ==
20507 KEY_ILLEGAL_REQUEST)) {
20508 status = ENOTSUP;
20510 break;
20511 default:
20512 break;
20514 break;
20515 default:
20516 break;
20519 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20521 if (no_caller_buf == TRUE) {
20522 kmem_free(data_bufp, data_len);
20525 return (status);
20530 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20532 * Description: This routine is the driver entry point for handling CD-ROM
20533 * multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20534 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20535 * device.
20537 * Arguments: ssc - ssc contains un - pointer to soft state struct
20538 * for the target.
20539 * usr_cmd SCSI-3 reservation facility command (one of
20540 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20541 * SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20542 * usr_bufp - user provided pointer register, reserve descriptor or
20543 * preempt and abort structure (mhioc_register_t,
20544 * mhioc_resv_desc_t, mhioc_preemptandabort_t)
20546 * Return Code: 0 - Success
20547 * EACCES
20548 * ENOTSUP
20549 * errno return code from sd_ssc_send()
20551 * Context: Can sleep. Does not return until command is completed.
20554 static int
20555 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20556 uchar_t *usr_bufp)
20558 struct scsi_extended_sense sense_buf;
20559 union scsi_cdb cdb;
20560 struct uscsi_cmd ucmd_buf;
20561 int status;
20562 uchar_t data_len = sizeof (sd_prout_t);
20563 sd_prout_t *prp;
20564 struct sd_lun *un;
20566 ASSERT(ssc != NULL);
20567 un = ssc->ssc_un;
20568 ASSERT(un != NULL);
20569 ASSERT(!mutex_owned(SD_MUTEX(un)));
20570 ASSERT(data_len == 24); /* required by scsi spec */
20572 SD_TRACE(SD_LOG_IO, un,
20573 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20575 if (usr_bufp == NULL) {
20576 return (EINVAL);
20579 bzero(&cdb, sizeof (cdb));
20580 bzero(&ucmd_buf, sizeof (ucmd_buf));
20581 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20582 prp = kmem_zalloc(data_len, KM_SLEEP);
20584 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20585 cdb.cdb_opaque[1] = usr_cmd;
20586 FORMG1COUNT(&cdb, data_len);
20588 ucmd_buf.uscsi_cdb = (char *)&cdb;
20589 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20590 ucmd_buf.uscsi_bufaddr = (caddr_t)prp;
20591 ucmd_buf.uscsi_buflen = data_len;
20592 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20593 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20594 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20595 ucmd_buf.uscsi_timeout = 60;
20597 switch (usr_cmd) {
20598 case SD_SCSI3_REGISTER: {
20599 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20601 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20602 bcopy(ptr->newkey.key, prp->service_key,
20603 MHIOC_RESV_KEY_SIZE);
20604 prp->aptpl = ptr->aptpl;
20605 break;
20607 case SD_SCSI3_CLEAR: {
20608 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20610 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20611 break;
20613 case SD_SCSI3_RESERVE:
20614 case SD_SCSI3_RELEASE: {
20615 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20617 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20618 prp->scope_address = BE_32(ptr->scope_specific_addr);
20619 cdb.cdb_opaque[2] = ptr->type;
20620 break;
20622 case SD_SCSI3_PREEMPTANDABORT: {
20623 mhioc_preemptandabort_t *ptr =
20624 (mhioc_preemptandabort_t *)usr_bufp;
20626 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20627 bcopy(ptr->victim_key.key, prp->service_key,
20628 MHIOC_RESV_KEY_SIZE);
20629 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20630 cdb.cdb_opaque[2] = ptr->resvdesc.type;
20631 ucmd_buf.uscsi_flags |= USCSI_HEAD;
20632 break;
20634 case SD_SCSI3_REGISTERANDIGNOREKEY:
20636 mhioc_registerandignorekey_t *ptr;
20637 ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20638 bcopy(ptr->newkey.key,
20639 prp->service_key, MHIOC_RESV_KEY_SIZE);
20640 prp->aptpl = ptr->aptpl;
20641 break;
20643 default:
20644 ASSERT(FALSE);
20645 break;
20648 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20649 UIO_SYSSPACE, SD_PATH_STANDARD);
20651 switch (status) {
20652 case 0:
20653 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20654 break; /* Success! */
20655 case EIO:
20656 switch (ucmd_buf.uscsi_status) {
20657 case STATUS_RESERVATION_CONFLICT:
20658 status = EACCES;
20659 break;
20660 case STATUS_CHECK:
20661 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20662 (scsi_sense_key((uint8_t *)&sense_buf) ==
20663 KEY_ILLEGAL_REQUEST)) {
20664 status = ENOTSUP;
20666 break;
20667 default:
20668 break;
20670 break;
20671 default:
20672 break;
20675 kmem_free(prp, data_len);
20676 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20677 return (status);
20682 * Function: sd_send_scsi_SYNCHRONIZE_CACHE
20684 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20686 * Arguments: un - pointer to the target's soft state struct
20687 * dkc - pointer to the callback structure
20689 * Return Code: 0 - success
20690 * errno-type error code
20692 * Context: kernel thread context only.
20694 * _______________________________________________________________
20695 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE |
20696 * |FLUSH_VOLATILE| | operation |
20697 * |______________|______________|_________________________________|
20698 * | 0 | NULL | Synchronous flush on both |
20699 * | | | volatile and non-volatile cache |
20700 * |______________|______________|_________________________________|
20701 * | 1 | NULL | Synchronous flush on volatile |
20702 * | | | cache; disk drivers may suppress|
20703 * | | | flush if disk table indicates |
20704 * | | | non-volatile cache |
20705 * |______________|______________|_________________________________|
20706 * | 0 | !NULL | Asynchronous flush on both |
20707 * | | | volatile and non-volatile cache;|
20708 * |______________|______________|_________________________________|
20709 * | 1 | !NULL | Asynchronous flush on volatile |
20710 * | | | cache; disk drivers may suppress|
20711 * | | | flush if disk table indicates |
20712 * | | | non-volatile cache |
20713 * |______________|______________|_________________________________|
20717 static int
20718 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20720 struct sd_uscsi_info *uip;
20721 struct uscsi_cmd *uscmd;
20722 union scsi_cdb *cdb;
20723 struct buf *bp;
20724 int rval = 0;
20725 int is_async;
20727 SD_TRACE(SD_LOG_IO, un,
20728 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20730 ASSERT(un != NULL);
20731 ASSERT(!mutex_owned(SD_MUTEX(un)));
20733 if (dkc == NULL || dkc->dkc_callback == NULL) {
20734 is_async = FALSE;
20735 } else {
20736 is_async = TRUE;
20739 mutex_enter(SD_MUTEX(un));
20740 /* check whether cache flush should be suppressed */
20741 if (un->un_f_suppress_cache_flush == TRUE) {
20742 mutex_exit(SD_MUTEX(un));
20744 * suppress the cache flush if the device is told to do
20745 * so by sd.conf or disk table
20747 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
20748 skip the cache flush since suppress_cache_flush is %d!\n",
20749 un->un_f_suppress_cache_flush);
20751 if (is_async == TRUE) {
20752 /* invoke callback for asynchronous flush */
20753 (*dkc->dkc_callback)(dkc->dkc_cookie, 0);
20755 return (rval);
20757 mutex_exit(SD_MUTEX(un));
20760 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
20761 * set properly
20763 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20764 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20766 mutex_enter(SD_MUTEX(un));
20767 if (dkc != NULL && un->un_f_sync_nv_supported &&
20768 (dkc->dkc_flag & FLUSH_VOLATILE)) {
20770 * if the device supports SYNC_NV bit, turn on
20771 * the SYNC_NV bit to only flush volatile cache
20773 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
20775 mutex_exit(SD_MUTEX(un));
20778 * First get some memory for the uscsi_cmd struct and cdb
20779 * and initialize for SYNCHRONIZE_CACHE cmd.
20781 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20782 uscmd->uscsi_cdblen = CDB_GROUP1;
20783 uscmd->uscsi_cdb = (caddr_t)cdb;
20784 uscmd->uscsi_bufaddr = NULL;
20785 uscmd->uscsi_buflen = 0;
20786 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20787 uscmd->uscsi_rqlen = SENSE_LENGTH;
20788 uscmd->uscsi_rqresid = SENSE_LENGTH;
20789 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20790 uscmd->uscsi_timeout = sd_io_time;
20793 * Allocate an sd_uscsi_info struct and fill it with the info
20794 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
20795 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
20796 * since we allocate the buf here in this function, we do not
20797 * need to preserve the prior contents of b_private.
20798 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
20800 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
20801 uip->ui_flags = SD_PATH_DIRECT;
20802 uip->ui_cmdp = uscmd;
20804 bp = getrbuf(KM_SLEEP);
20805 bp->b_private = uip;
20808 * Setup buffer to carry uscsi request.
20810 bp->b_flags = B_BUSY;
20811 bp->b_bcount = 0;
20812 bp->b_blkno = 0;
20814 if (is_async == TRUE) {
20815 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
20816 uip->ui_dkc = *dkc;
20819 bp->b_edev = SD_GET_DEV(un);
20820 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */
20823 * Unset un_f_sync_cache_required flag
20825 mutex_enter(SD_MUTEX(un));
20826 un->un_f_sync_cache_required = FALSE;
20827 mutex_exit(SD_MUTEX(un));
20829 (void) sd_uscsi_strategy(bp);
20832 * If synchronous request, wait for completion
20833 * If async just return and let b_iodone callback
20834 * cleanup.
20835 * NOTE: On return, u_ncmds_in_driver will be decremented,
20836 * but it was also incremented in sd_uscsi_strategy(), so
20837 * we should be ok.
20839 if (is_async == FALSE) {
20840 (void) biowait(bp);
20841 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
20844 return (rval);
20848 static int
20849 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
20851 struct sd_uscsi_info *uip;
20852 struct uscsi_cmd *uscmd;
20853 uint8_t *sense_buf;
20854 struct sd_lun *un;
20855 int status;
20856 union scsi_cdb *cdb;
20858 uip = (struct sd_uscsi_info *)(bp->b_private);
20859 ASSERT(uip != NULL);
20861 uscmd = uip->ui_cmdp;
20862 ASSERT(uscmd != NULL);
20864 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
20865 ASSERT(sense_buf != NULL);
20867 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
20868 ASSERT(un != NULL);
20870 cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
20872 status = geterror(bp);
20873 switch (status) {
20874 case 0:
20875 break; /* Success! */
20876 case EIO:
20877 switch (uscmd->uscsi_status) {
20878 case STATUS_RESERVATION_CONFLICT:
20879 /* Ignore reservation conflict */
20880 status = 0;
20881 goto done;
20883 case STATUS_CHECK:
20884 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
20885 (scsi_sense_key(sense_buf) ==
20886 KEY_ILLEGAL_REQUEST)) {
20887 /* Ignore Illegal Request error */
20888 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
20889 mutex_enter(SD_MUTEX(un));
20890 un->un_f_sync_nv_supported = FALSE;
20891 mutex_exit(SD_MUTEX(un));
20892 status = 0;
20893 SD_TRACE(SD_LOG_IO, un,
20894 "un_f_sync_nv_supported \
20895 is set to false.\n");
20896 goto done;
20899 mutex_enter(SD_MUTEX(un));
20900 un->un_f_sync_cache_supported = FALSE;
20901 mutex_exit(SD_MUTEX(un));
20902 SD_TRACE(SD_LOG_IO, un,
20903 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
20904 un_f_sync_cache_supported set to false \
20905 with asc = %x, ascq = %x\n",
20906 scsi_sense_asc(sense_buf),
20907 scsi_sense_ascq(sense_buf));
20908 status = ENOTSUP;
20909 goto done;
20911 break;
20912 default:
20913 break;
20915 /* FALLTHRU */
20916 default:
20918 * Turn on the un_f_sync_cache_required flag
20919 * since the SYNC CACHE command failed
20921 mutex_enter(SD_MUTEX(un));
20922 un->un_f_sync_cache_required = TRUE;
20923 mutex_exit(SD_MUTEX(un));
20926 * Don't log an error message if this device
20927 * has removable media.
20929 if (!un->un_f_has_removable_media) {
20930 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
20931 "SYNCHRONIZE CACHE command failed (%d)\n", status);
20933 break;
20936 done:
20937 if (uip->ui_dkc.dkc_callback != NULL) {
20938 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
20941 ASSERT((bp->b_flags & B_REMAPPED) == 0);
20942 freerbuf(bp);
20943 kmem_free(uip, sizeof (struct sd_uscsi_info));
20944 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
20945 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
20946 kmem_free(uscmd, sizeof (struct uscsi_cmd));
20948 return (status);
20952 * Issues a single SCSI UNMAP command with a prepared UNMAP parameter list.
20953 * Returns zero on success, or the non-zero command error code on failure.
20955 static int
20956 sd_send_scsi_UNMAP_issue_one(sd_ssc_t *ssc, unmap_param_hdr_t *uph,
20957 uint64_t num_descr, uint64_t bytes)
20959 struct sd_lun *un = ssc->ssc_un;
20960 struct scsi_extended_sense sense_buf;
20961 union scsi_cdb cdb;
20962 struct uscsi_cmd ucmd_buf;
20963 int status;
20964 const uint64_t param_size = sizeof (unmap_param_hdr_t) +
20965 num_descr * sizeof (unmap_blk_descr_t);
20967 ASSERT3U(param_size - 2, <=, UINT16_MAX);
20968 uph->uph_data_len = BE_16(param_size - 2);
20969 uph->uph_descr_data_len = BE_16(param_size - 8);
20971 bzero(&cdb, sizeof (cdb));
20972 bzero(&ucmd_buf, sizeof (ucmd_buf));
20973 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20975 cdb.scc_cmd = SCMD_UNMAP;
20976 FORMG1COUNT(&cdb, param_size);
20978 ucmd_buf.uscsi_cdb = (char *)&cdb;
20979 ucmd_buf.uscsi_cdblen = (uchar_t)CDB_GROUP1;
20980 ucmd_buf.uscsi_bufaddr = (caddr_t)uph;
20981 ucmd_buf.uscsi_buflen = param_size;
20982 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20983 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20984 ucmd_buf.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE | USCSI_SILENT;
20985 ucmd_buf.uscsi_timeout = un->un_cmd_timeout;
20987 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, UIO_SYSSPACE,
20988 SD_PATH_STANDARD);
20990 switch (status) {
20991 case 0:
20992 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20994 if (un->un_unmapstats) {
20995 atomic_inc_64(&un->un_unmapstats->us_cmds.value.ui64);
20996 atomic_add_64(&un->un_unmapstats->us_extents.value.ui64,
20997 num_descr);
20998 atomic_add_64(&un->un_unmapstats->us_bytes.value.ui64,
20999 bytes);
21001 break; /* Success! */
21002 case EIO:
21003 if (un->un_unmapstats)
21004 atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21005 switch (ucmd_buf.uscsi_status) {
21006 case STATUS_RESERVATION_CONFLICT:
21007 status = EACCES;
21008 break;
21009 default:
21010 break;
21012 break;
21013 default:
21014 if (un->un_unmapstats)
21015 atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21016 break;
21019 return (status);
21023 * Returns a pointer to the i'th block descriptor inside an UNMAP param list.
21025 static inline unmap_blk_descr_t *
21026 UNMAP_blk_descr_i(void *buf, size_t i)
21028 return ((unmap_blk_descr_t *)((uintptr_t)buf +
21029 sizeof (unmap_param_hdr_t) + (i * sizeof (unmap_blk_descr_t))));
21033 * Takes the list of extents from sd_send_scsi_UNMAP, chops it up, prepares
21034 * UNMAP block descriptors and issues individual SCSI UNMAP commands. While
21035 * doing so we consult the block limits to determine at most how many
21036 * extents and LBAs we can UNMAP in one command.
21037 * If a command fails for whatever, reason, extent list processing is aborted
21038 * and the failed command's status is returned. Otherwise returns 0 on
21039 * success.
21041 static int
21042 sd_send_scsi_UNMAP_issue(dev_t dev, sd_ssc_t *ssc, const dkioc_free_list_t *dfl)
21044 struct sd_lun *un = ssc->ssc_un;
21045 unmap_param_hdr_t *uph;
21046 sd_blk_limits_t *lim = &un->un_blk_lim;
21047 int rval = 0;
21048 int partition;
21049 /* partition offset & length in system blocks */
21050 diskaddr_t part_off_sysblks = 0, part_len_sysblks = 0;
21051 uint64_t part_off, part_len;
21052 uint64_t descr_cnt_lim, byte_cnt_lim;
21053 uint64_t descr_issued = 0, bytes_issued = 0;
21055 uph = kmem_zalloc(SD_UNMAP_PARAM_LIST_MAXSZ, KM_SLEEP);
21057 partition = SDPART(dev);
21058 rval = cmlb_partinfo(un->un_cmlbhandle, partition, &part_len_sysblks,
21059 &part_off_sysblks, NULL, NULL, (void *)SD_PATH_DIRECT);
21060 if (rval != 0)
21061 goto out;
21062 part_off = SD_SYSBLOCKS2BYTES(part_off_sysblks);
21063 part_len = SD_SYSBLOCKS2BYTES(part_len_sysblks);
21065 ASSERT(un->un_blk_lim.lim_max_unmap_lba_cnt != 0);
21066 ASSERT(un->un_blk_lim.lim_max_unmap_descr_cnt != 0);
21067 /* Spec says 0xffffffff are special values, so compute maximums. */
21068 byte_cnt_lim = lim->lim_max_unmap_lba_cnt < UINT32_MAX ?
21069 (uint64_t)lim->lim_max_unmap_lba_cnt * un->un_tgt_blocksize :
21070 UINT64_MAX;
21071 descr_cnt_lim = MIN(lim->lim_max_unmap_descr_cnt, SD_UNMAP_MAX_DESCR);
21073 if (dfl->dfl_offset >= part_len) {
21074 rval = SET_ERROR(EINVAL);
21075 goto out;
21078 for (size_t i = 0; i < dfl->dfl_num_exts; i++) {
21079 const dkioc_free_list_ext_t *ext = &dfl->dfl_exts[i];
21080 uint64_t ext_start = ext->dfle_start;
21081 uint64_t ext_length = ext->dfle_length;
21083 while (ext_length > 0) {
21084 unmap_blk_descr_t *ubd;
21085 /* Respect device limit on LBA count per command */
21086 uint64_t len = MIN(MIN(ext_length, byte_cnt_lim -
21087 bytes_issued), SD_TGTBLOCKS2BYTES(un, UINT32_MAX));
21089 /* check partition limits */
21090 if (ext_start >= part_len ||
21091 ext_start + len < ext_start ||
21092 dfl->dfl_offset + ext_start + len <
21093 dfl->dfl_offset ||
21094 dfl->dfl_offset + ext_start + len > part_len) {
21095 rval = SET_ERROR(EINVAL);
21096 goto out;
21099 ASSERT3U(descr_issued, <, descr_cnt_lim);
21100 ASSERT3U(bytes_issued, <, byte_cnt_lim);
21101 ubd = UNMAP_blk_descr_i(uph, descr_issued);
21103 /* adjust in-partition addresses to be device-global */
21104 ubd->ubd_lba = BE_64(SD_BYTES2TGTBLOCKS(un,
21105 dfl->dfl_offset + ext_start + part_off));
21106 ubd->ubd_lba_cnt = BE_32(SD_BYTES2TGTBLOCKS(un, len));
21108 descr_issued++;
21109 bytes_issued += len;
21111 /* Issue command when device limits reached */
21112 if (descr_issued == descr_cnt_lim ||
21113 bytes_issued == byte_cnt_lim) {
21114 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph,
21115 descr_issued, bytes_issued);
21116 if (rval != 0)
21117 goto out;
21118 descr_issued = 0;
21119 bytes_issued = 0;
21122 ext_start += len;
21123 ext_length -= len;
21127 if (descr_issued > 0) {
21128 /* issue last command */
21129 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph, descr_issued,
21130 bytes_issued);
21133 out:
21134 kmem_free(uph, SD_UNMAP_PARAM_LIST_MAXSZ);
21135 return (rval);
21139 * Issues one or several UNMAP commands based on a list of extents to be
21140 * unmapped. The internal multi-command processing is hidden, as the exact
21141 * number of commands and extents per command is limited by both SCSI
21142 * command syntax and device limits (as expressed in the SCSI Block Limits
21143 * VPD page and un_blk_lim in struct sd_lun).
21144 * Returns zero on success, or the error code of the first failed SCSI UNMAP
21145 * command.
21147 static int
21148 sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl, int flag)
21150 struct sd_lun *un = ssc->ssc_un;
21151 int rval = 0;
21153 ASSERT(!mutex_owned(SD_MUTEX(un)));
21154 ASSERT(dfl != NULL);
21156 /* Per spec, any of these conditions signals lack of UNMAP support. */
21157 if (!(un->un_thin_flags & SD_THIN_PROV_ENABLED) ||
21158 un->un_blk_lim.lim_max_unmap_descr_cnt == 0 ||
21159 un->un_blk_lim.lim_max_unmap_lba_cnt == 0) {
21160 return (SET_ERROR(ENOTSUP));
21163 /* For userspace calls we must copy in. */
21164 if (!(flag & FKIOCTL)) {
21165 int err = dfl_copyin(dfl, &dfl, flag, KM_SLEEP);
21166 if (err != 0)
21167 return (err);
21168 } else if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
21169 ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
21170 return (SET_ERROR(EINVAL));
21173 rval = sd_send_scsi_UNMAP_issue(dev, ssc, dfl);
21175 if (!(flag & FKIOCTL)) {
21176 dfl_free(dfl);
21177 dfl = NULL;
21180 return (rval);
21184 * Function: sd_send_scsi_GET_CONFIGURATION
21186 * Description: Issues the get configuration command to the device.
21187 * Called from sd_check_for_writable_cd & sd_get_media_info
21188 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21189 * Arguments: ssc
21190 * ucmdbuf
21191 * rqbuf
21192 * rqbuflen
21193 * bufaddr
21194 * buflen
21195 * path_flag
21197 * Return Code: 0 - Success
21198 * errno return code from sd_ssc_send()
21200 * Context: Can sleep. Does not return until command is completed.
21204 static int
21205 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21206 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21207 int path_flag)
21209 char cdb[CDB_GROUP1];
21210 int status;
21211 struct sd_lun *un;
21213 ASSERT(ssc != NULL);
21214 un = ssc->ssc_un;
21215 ASSERT(un != NULL);
21216 ASSERT(!mutex_owned(SD_MUTEX(un)));
21217 ASSERT(bufaddr != NULL);
21218 ASSERT(ucmdbuf != NULL);
21219 ASSERT(rqbuf != NULL);
21221 SD_TRACE(SD_LOG_IO, un,
21222 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21224 bzero(cdb, sizeof (cdb));
21225 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21226 bzero(rqbuf, rqbuflen);
21227 bzero(bufaddr, buflen);
21230 * Set up cdb field for the get configuration command.
21232 cdb[0] = SCMD_GET_CONFIGURATION;
21233 cdb[1] = 0x02; /* Requested Type */
21234 cdb[8] = SD_PROFILE_HEADER_LEN;
21235 ucmdbuf->uscsi_cdb = cdb;
21236 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21237 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21238 ucmdbuf->uscsi_buflen = buflen;
21239 ucmdbuf->uscsi_timeout = sd_io_time;
21240 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21241 ucmdbuf->uscsi_rqlen = rqbuflen;
21242 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21244 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21245 UIO_SYSSPACE, path_flag);
21247 switch (status) {
21248 case 0:
21249 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21250 break; /* Success! */
21251 case EIO:
21252 switch (ucmdbuf->uscsi_status) {
21253 case STATUS_RESERVATION_CONFLICT:
21254 status = EACCES;
21255 break;
21256 default:
21257 break;
21259 break;
21260 default:
21261 break;
21264 if (status == 0) {
21265 SD_DUMP_MEMORY(un, SD_LOG_IO,
21266 "sd_send_scsi_GET_CONFIGURATION: data",
21267 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21270 SD_TRACE(SD_LOG_IO, un,
21271 "sd_send_scsi_GET_CONFIGURATION: exit\n");
21273 return (status);
21277 * Function: sd_send_scsi_feature_GET_CONFIGURATION
21279 * Description: Issues the get configuration command to the device to
21280 * retrieve a specific feature. Called from
21281 * sd_check_for_writable_cd & sd_set_mmc_caps.
21282 * Arguments: ssc
21283 * ucmdbuf
21284 * rqbuf
21285 * rqbuflen
21286 * bufaddr
21287 * buflen
21288 * feature
21290 * Return Code: 0 - Success
21291 * errno return code from sd_ssc_send()
21293 * Context: Can sleep. Does not return until command is completed.
21296 static int
21297 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21298 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21299 char feature, int path_flag)
21301 char cdb[CDB_GROUP1];
21302 int status;
21303 struct sd_lun *un;
21305 ASSERT(ssc != NULL);
21306 un = ssc->ssc_un;
21307 ASSERT(un != NULL);
21308 ASSERT(!mutex_owned(SD_MUTEX(un)));
21309 ASSERT(bufaddr != NULL);
21310 ASSERT(ucmdbuf != NULL);
21311 ASSERT(rqbuf != NULL);
21313 SD_TRACE(SD_LOG_IO, un,
21314 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21316 bzero(cdb, sizeof (cdb));
21317 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21318 bzero(rqbuf, rqbuflen);
21319 bzero(bufaddr, buflen);
21322 * Set up cdb field for the get configuration command.
21324 cdb[0] = SCMD_GET_CONFIGURATION;
21325 cdb[1] = 0x02; /* Requested Type */
21326 cdb[3] = feature;
21327 cdb[8] = buflen;
21328 ucmdbuf->uscsi_cdb = cdb;
21329 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21330 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21331 ucmdbuf->uscsi_buflen = buflen;
21332 ucmdbuf->uscsi_timeout = sd_io_time;
21333 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21334 ucmdbuf->uscsi_rqlen = rqbuflen;
21335 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21337 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21338 UIO_SYSSPACE, path_flag);
21340 switch (status) {
21341 case 0:
21343 break; /* Success! */
21344 case EIO:
21345 switch (ucmdbuf->uscsi_status) {
21346 case STATUS_RESERVATION_CONFLICT:
21347 status = EACCES;
21348 break;
21349 default:
21350 break;
21352 break;
21353 default:
21354 break;
21357 if (status == 0) {
21358 SD_DUMP_MEMORY(un, SD_LOG_IO,
21359 "sd_send_scsi_feature_GET_CONFIGURATION: data",
21360 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21363 SD_TRACE(SD_LOG_IO, un,
21364 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21366 return (status);
21371 * Function: sd_send_scsi_MODE_SENSE
21373 * Description: Utility function for issuing a scsi MODE SENSE command.
21374 * Note: This routine uses a consistent implementation for Group0,
21375 * Group1, and Group2 commands across all platforms. ATAPI devices
21376 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21378 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21379 * structure for this target.
21380 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21381 * CDB_GROUP[1|2] (10 byte).
21382 * bufaddr - buffer for page data retrieved from the target.
21383 * buflen - size of page to be retrieved.
21384 * page_code - page code of data to be retrieved from the target.
21385 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21386 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21387 * to use the USCSI "direct" chain and bypass the normal
21388 * command waitq.
21390 * Return Code: 0 - Success
21391 * errno return code from sd_ssc_send()
21393 * Context: Can sleep. Does not return until command is completed.
21396 static int
21397 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21398 size_t buflen, uchar_t page_code, int path_flag)
21400 struct scsi_extended_sense sense_buf;
21401 union scsi_cdb cdb;
21402 struct uscsi_cmd ucmd_buf;
21403 int status;
21404 int headlen;
21405 struct sd_lun *un;
21407 ASSERT(ssc != NULL);
21408 un = ssc->ssc_un;
21409 ASSERT(un != NULL);
21410 ASSERT(!mutex_owned(SD_MUTEX(un)));
21411 ASSERT(bufaddr != NULL);
21412 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21413 (cdbsize == CDB_GROUP2));
21415 SD_TRACE(SD_LOG_IO, un,
21416 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21418 bzero(&cdb, sizeof (cdb));
21419 bzero(&ucmd_buf, sizeof (ucmd_buf));
21420 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21421 bzero(bufaddr, buflen);
21423 if (cdbsize == CDB_GROUP0) {
21424 cdb.scc_cmd = SCMD_MODE_SENSE;
21425 cdb.cdb_opaque[2] = page_code;
21426 FORMG0COUNT(&cdb, buflen);
21427 headlen = MODE_HEADER_LENGTH;
21428 } else {
21429 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21430 cdb.cdb_opaque[2] = page_code;
21431 FORMG1COUNT(&cdb, buflen);
21432 headlen = MODE_HEADER_LENGTH_GRP2;
21435 ASSERT(headlen <= buflen);
21436 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21438 ucmd_buf.uscsi_cdb = (char *)&cdb;
21439 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21440 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21441 ucmd_buf.uscsi_buflen = buflen;
21442 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21443 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21444 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21445 ucmd_buf.uscsi_timeout = 60;
21447 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21448 UIO_SYSSPACE, path_flag);
21450 switch (status) {
21451 case 0:
21453 * sr_check_wp() uses 0x3f page code and check the header of
21454 * mode page to determine if target device is write-protected.
21455 * But some USB devices return 0 bytes for 0x3f page code. For
21456 * this case, make sure that mode page header is returned at
21457 * least.
21459 if (buflen - ucmd_buf.uscsi_resid < headlen) {
21460 status = EIO;
21461 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21462 "mode page header is not returned");
21464 break; /* Success! */
21465 case EIO:
21466 switch (ucmd_buf.uscsi_status) {
21467 case STATUS_RESERVATION_CONFLICT:
21468 status = EACCES;
21469 break;
21470 default:
21471 break;
21473 break;
21474 default:
21475 break;
21478 if (status == 0) {
21479 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21480 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21482 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21484 return (status);
21489 * Function: sd_send_scsi_MODE_SELECT
21491 * Description: Utility function for issuing a scsi MODE SELECT command.
21492 * Note: This routine uses a consistent implementation for Group0,
21493 * Group1, and Group2 commands across all platforms. ATAPI devices
21494 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21496 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21497 * structure for this target.
21498 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21499 * CDB_GROUP[1|2] (10 byte).
21500 * bufaddr - buffer for page data retrieved from the target.
21501 * buflen - size of page to be retrieved.
21502 * save_page - boolean to determin if SP bit should be set.
21503 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21504 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21505 * to use the USCSI "direct" chain and bypass the normal
21506 * command waitq.
21508 * Return Code: 0 - Success
21509 * errno return code from sd_ssc_send()
21511 * Context: Can sleep. Does not return until command is completed.
21514 static int
21515 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21516 size_t buflen, uchar_t save_page, int path_flag)
21518 struct scsi_extended_sense sense_buf;
21519 union scsi_cdb cdb;
21520 struct uscsi_cmd ucmd_buf;
21521 int status;
21522 struct sd_lun *un;
21524 ASSERT(ssc != NULL);
21525 un = ssc->ssc_un;
21526 ASSERT(un != NULL);
21527 ASSERT(!mutex_owned(SD_MUTEX(un)));
21528 ASSERT(bufaddr != NULL);
21529 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21530 (cdbsize == CDB_GROUP2));
21532 SD_TRACE(SD_LOG_IO, un,
21533 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21535 bzero(&cdb, sizeof (cdb));
21536 bzero(&ucmd_buf, sizeof (ucmd_buf));
21537 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21539 /* Set the PF bit for many third party drives */
21540 cdb.cdb_opaque[1] = 0x10;
21542 /* Set the savepage(SP) bit if given */
21543 if (save_page == SD_SAVE_PAGE) {
21544 cdb.cdb_opaque[1] |= 0x01;
21547 if (cdbsize == CDB_GROUP0) {
21548 cdb.scc_cmd = SCMD_MODE_SELECT;
21549 FORMG0COUNT(&cdb, buflen);
21550 } else {
21551 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21552 FORMG1COUNT(&cdb, buflen);
21555 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21557 ucmd_buf.uscsi_cdb = (char *)&cdb;
21558 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21559 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21560 ucmd_buf.uscsi_buflen = buflen;
21561 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21562 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21563 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21564 ucmd_buf.uscsi_timeout = 60;
21566 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21567 UIO_SYSSPACE, path_flag);
21569 switch (status) {
21570 case 0:
21571 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21572 break; /* Success! */
21573 case EIO:
21574 switch (ucmd_buf.uscsi_status) {
21575 case STATUS_RESERVATION_CONFLICT:
21576 status = EACCES;
21577 break;
21578 default:
21579 break;
21581 break;
21582 default:
21583 break;
21586 if (status == 0) {
21587 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21588 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21590 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21592 return (status);
21597 * Function: sd_send_scsi_RDWR
21599 * Description: Issue a scsi READ or WRITE command with the given parameters.
21601 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21602 * structure for this target.
21603 * cmd: SCMD_READ or SCMD_WRITE
21604 * bufaddr: Address of caller's buffer to receive the RDWR data
21605 * buflen: Length of caller's buffer receive the RDWR data.
21606 * start_block: Block number for the start of the RDWR operation.
21607 * (Assumes target-native block size.)
21608 * residp: Pointer to variable to receive the redisual of the
21609 * RDWR operation (may be NULL of no residual requested).
21610 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21611 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21612 * to use the USCSI "direct" chain and bypass the normal
21613 * command waitq.
21615 * Return Code: 0 - Success
21616 * errno return code from sd_ssc_send()
21618 * Context: Can sleep. Does not return until command is completed.
21621 static int
21622 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21623 size_t buflen, daddr_t start_block, int path_flag)
21625 struct scsi_extended_sense sense_buf;
21626 union scsi_cdb cdb;
21627 struct uscsi_cmd ucmd_buf;
21628 uint32_t block_count;
21629 int status;
21630 int cdbsize;
21631 uchar_t flag;
21632 struct sd_lun *un;
21634 ASSERT(ssc != NULL);
21635 un = ssc->ssc_un;
21636 ASSERT(un != NULL);
21637 ASSERT(!mutex_owned(SD_MUTEX(un)));
21638 ASSERT(bufaddr != NULL);
21639 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21641 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21643 if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21644 return (EINVAL);
21647 mutex_enter(SD_MUTEX(un));
21648 block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21649 mutex_exit(SD_MUTEX(un));
21651 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21653 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21654 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21655 bufaddr, buflen, start_block, block_count);
21657 bzero(&cdb, sizeof (cdb));
21658 bzero(&ucmd_buf, sizeof (ucmd_buf));
21659 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21661 /* Compute CDB size to use */
21662 if (start_block > 0xffffffff)
21663 cdbsize = CDB_GROUP4;
21664 else if ((start_block & 0xFFE00000) ||
21665 (un->un_f_cfg_is_atapi == TRUE))
21666 cdbsize = CDB_GROUP1;
21667 else
21668 cdbsize = CDB_GROUP0;
21670 switch (cdbsize) {
21671 case CDB_GROUP0: /* 6-byte CDBs */
21672 cdb.scc_cmd = cmd;
21673 FORMG0ADDR(&cdb, start_block);
21674 FORMG0COUNT(&cdb, block_count);
21675 break;
21676 case CDB_GROUP1: /* 10-byte CDBs */
21677 cdb.scc_cmd = cmd | SCMD_GROUP1;
21678 FORMG1ADDR(&cdb, start_block);
21679 FORMG1COUNT(&cdb, block_count);
21680 break;
21681 case CDB_GROUP4: /* 16-byte CDBs */
21682 cdb.scc_cmd = cmd | SCMD_GROUP4;
21683 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21684 FORMG4COUNT(&cdb, block_count);
21685 break;
21686 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */
21687 default:
21688 /* All others reserved */
21689 return (EINVAL);
21692 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21693 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21695 ucmd_buf.uscsi_cdb = (char *)&cdb;
21696 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21697 ucmd_buf.uscsi_bufaddr = bufaddr;
21698 ucmd_buf.uscsi_buflen = buflen;
21699 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21700 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21701 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT;
21702 ucmd_buf.uscsi_timeout = 60;
21703 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21704 UIO_SYSSPACE, path_flag);
21706 switch (status) {
21707 case 0:
21708 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21709 break; /* Success! */
21710 case EIO:
21711 switch (ucmd_buf.uscsi_status) {
21712 case STATUS_RESERVATION_CONFLICT:
21713 status = EACCES;
21714 break;
21715 default:
21716 break;
21718 break;
21719 default:
21720 break;
21723 if (status == 0) {
21724 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21725 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21728 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21730 return (status);
21735 * Function: sd_send_scsi_LOG_SENSE
21737 * Description: Issue a scsi LOG_SENSE command with the given parameters.
21739 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21740 * structure for this target.
21742 * Return Code: 0 - Success
21743 * errno return code from sd_ssc_send()
21745 * Context: Can sleep. Does not return until command is completed.
21748 static int
21749 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21750 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
21752 struct scsi_extended_sense sense_buf;
21753 union scsi_cdb cdb;
21754 struct uscsi_cmd ucmd_buf;
21755 int status;
21756 struct sd_lun *un;
21758 ASSERT(ssc != NULL);
21759 un = ssc->ssc_un;
21760 ASSERT(un != NULL);
21761 ASSERT(!mutex_owned(SD_MUTEX(un)));
21763 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21765 bzero(&cdb, sizeof (cdb));
21766 bzero(&ucmd_buf, sizeof (ucmd_buf));
21767 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21769 cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21770 cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21771 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21772 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF);
21773 FORMG1COUNT(&cdb, buflen);
21775 ucmd_buf.uscsi_cdb = (char *)&cdb;
21776 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21777 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21778 ucmd_buf.uscsi_buflen = buflen;
21779 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21780 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21781 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21782 ucmd_buf.uscsi_timeout = 60;
21784 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21785 UIO_SYSSPACE, path_flag);
21787 switch (status) {
21788 case 0:
21789 break;
21790 case EIO:
21791 switch (ucmd_buf.uscsi_status) {
21792 case STATUS_RESERVATION_CONFLICT:
21793 status = EACCES;
21794 break;
21795 case STATUS_CHECK:
21796 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21797 (scsi_sense_key((uint8_t *)&sense_buf) ==
21798 KEY_ILLEGAL_REQUEST) &&
21799 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21801 * ASC 0x24: INVALID FIELD IN CDB
21803 switch (page_code) {
21804 case START_STOP_CYCLE_PAGE:
21806 * The start stop cycle counter is
21807 * implemented as page 0x31 in earlier
21808 * generation disks. In new generation
21809 * disks the start stop cycle counter is
21810 * implemented as page 0xE. To properly
21811 * handle this case if an attempt for
21812 * log page 0xE is made and fails we
21813 * will try again using page 0x31.
21815 * Network storage BU committed to
21816 * maintain the page 0x31 for this
21817 * purpose and will not have any other
21818 * page implemented with page code 0x31
21819 * until all disks transition to the
21820 * standard page.
21822 mutex_enter(SD_MUTEX(un));
21823 un->un_start_stop_cycle_page =
21824 START_STOP_CYCLE_VU_PAGE;
21825 cdb.cdb_opaque[2] =
21826 (char)(page_control << 6) |
21827 un->un_start_stop_cycle_page;
21828 mutex_exit(SD_MUTEX(un));
21829 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
21830 status = sd_ssc_send(
21831 ssc, &ucmd_buf, FKIOCTL,
21832 UIO_SYSSPACE, path_flag);
21834 break;
21835 case TEMPERATURE_PAGE:
21836 status = ENOTTY;
21837 break;
21838 default:
21839 break;
21842 break;
21843 default:
21844 break;
21846 break;
21847 default:
21848 break;
21851 if (status == 0) {
21852 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21853 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
21854 (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21857 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
21859 return (status);
21864 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
21866 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
21868 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
21869 * structure for this target.
21870 * bufaddr
21871 * buflen
21872 * class_req
21874 * Return Code: 0 - Success
21875 * errno return code from sd_ssc_send()
21877 * Context: Can sleep. Does not return until command is completed.
21880 static int
21881 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
21882 size_t buflen, uchar_t class_req)
21884 union scsi_cdb cdb;
21885 struct uscsi_cmd ucmd_buf;
21886 int status;
21887 struct sd_lun *un;
21889 ASSERT(ssc != NULL);
21890 un = ssc->ssc_un;
21891 ASSERT(un != NULL);
21892 ASSERT(!mutex_owned(SD_MUTEX(un)));
21893 ASSERT(bufaddr != NULL);
21895 SD_TRACE(SD_LOG_IO, un,
21896 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
21898 bzero(&cdb, sizeof (cdb));
21899 bzero(&ucmd_buf, sizeof (ucmd_buf));
21900 bzero(bufaddr, buflen);
21902 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
21903 cdb.cdb_opaque[1] = 1; /* polled */
21904 cdb.cdb_opaque[4] = class_req;
21905 FORMG1COUNT(&cdb, buflen);
21907 ucmd_buf.uscsi_cdb = (char *)&cdb;
21908 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21909 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21910 ucmd_buf.uscsi_buflen = buflen;
21911 ucmd_buf.uscsi_rqbuf = NULL;
21912 ucmd_buf.uscsi_rqlen = 0;
21913 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
21914 ucmd_buf.uscsi_timeout = 60;
21916 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21917 UIO_SYSSPACE, SD_PATH_DIRECT);
21920 * Only handle status == 0, the upper-level caller
21921 * will put different assessment based on the context.
21923 if (status == 0) {
21924 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21926 if (ucmd_buf.uscsi_resid != 0) {
21927 status = EIO;
21931 SD_TRACE(SD_LOG_IO, un,
21932 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
21934 return (status);
21938 static boolean_t
21939 sd_gesn_media_data_valid(uchar_t *data)
21941 uint16_t len;
21943 len = (data[1] << 8) | data[0];
21944 return ((len >= 6) &&
21945 ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
21946 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
21947 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
21952 * Function: sdioctl
21954 * Description: Driver's ioctl(9e) entry point function.
21956 * Arguments: dev - device number
21957 * cmd - ioctl operation to be performed
21958 * arg - user argument, contains data to be set or reference
21959 * parameter for get
21960 * flag - bit flag, indicating open settings, 32/64 bit type
21961 * cred_p - user credential pointer
21962 * rval_p - calling process return value (OPT)
21964 * Return Code: EINVAL
21965 * ENOTTY
21966 * ENXIO
21967 * EIO
21968 * EFAULT
21969 * ENOTSUP
21970 * EPERM
21972 * Context: Called from the device switch at normal priority.
21975 static int
21976 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
21978 struct sd_lun *un = NULL;
21979 int err = 0;
21980 int i = 0;
21981 cred_t *cr;
21982 int tmprval = EINVAL;
21983 boolean_t is_valid;
21984 sd_ssc_t *ssc;
21987 * All device accesses go thru sdstrategy where we check on suspend
21988 * status
21990 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21991 return (ENXIO);
21994 ASSERT(!mutex_owned(SD_MUTEX(un)));
21996 /* Initialize sd_ssc_t for internal uscsi commands */
21997 ssc = sd_ssc_init(un);
21999 is_valid = SD_IS_VALID_LABEL(un);
22002 * Moved this wait from sd_uscsi_strategy to here for
22003 * reasons of deadlock prevention. Internal driver commands,
22004 * specifically those to change a devices power level, result
22005 * in a call to sd_uscsi_strategy.
22007 mutex_enter(SD_MUTEX(un));
22008 while ((un->un_state == SD_STATE_SUSPENDED) ||
22009 (un->un_state == SD_STATE_PM_CHANGING)) {
22010 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22013 * Twiddling the counter here protects commands from now
22014 * through to the top of sd_uscsi_strategy. Without the
22015 * counter inc. a power down, for example, could get in
22016 * after the above check for state is made and before
22017 * execution gets to the top of sd_uscsi_strategy.
22018 * That would cause problems.
22020 un->un_ncmds_in_driver++;
22022 if (!is_valid &&
22023 (flag & (FNDELAY | FNONBLOCK))) {
22024 switch (cmd) {
22025 case DKIOCGGEOM: /* SD_PATH_DIRECT */
22026 case DKIOCGVTOC:
22027 case DKIOCGEXTVTOC:
22028 case DKIOCGAPART:
22029 case DKIOCPARTINFO:
22030 case DKIOCEXTPARTINFO:
22031 case DKIOCSGEOM:
22032 case DKIOCSAPART:
22033 case DKIOCGETEFI:
22034 case DKIOCPARTITION:
22035 case DKIOCSVTOC:
22036 case DKIOCSEXTVTOC:
22037 case DKIOCSETEFI:
22038 case DKIOCGMBOOT:
22039 case DKIOCSMBOOT:
22040 case DKIOCG_PHYGEOM:
22041 case DKIOCG_VIRTGEOM:
22042 #if defined(__i386) || defined(__amd64)
22043 case DKIOCSETEXTPART:
22044 #endif
22045 /* let cmlb handle it */
22046 goto skip_ready_valid;
22048 case CDROMPAUSE:
22049 case CDROMRESUME:
22050 case CDROMPLAYMSF:
22051 case CDROMPLAYTRKIND:
22052 case CDROMREADTOCHDR:
22053 case CDROMREADTOCENTRY:
22054 case CDROMSTOP:
22055 case CDROMSTART:
22056 case CDROMVOLCTRL:
22057 case CDROMSUBCHNL:
22058 case CDROMREADMODE2:
22059 case CDROMREADMODE1:
22060 case CDROMREADOFFSET:
22061 case CDROMSBLKMODE:
22062 case CDROMGBLKMODE:
22063 case CDROMGDRVSPEED:
22064 case CDROMSDRVSPEED:
22065 case CDROMCDDA:
22066 case CDROMCDXA:
22067 case CDROMSUBCODE:
22068 if (!ISCD(un)) {
22069 un->un_ncmds_in_driver--;
22070 ASSERT(un->un_ncmds_in_driver >= 0);
22071 mutex_exit(SD_MUTEX(un));
22072 err = ENOTTY;
22073 goto done_without_assess;
22075 break;
22076 case FDEJECT:
22077 case DKIOCEJECT:
22078 case CDROMEJECT:
22079 if (!un->un_f_eject_media_supported) {
22080 un->un_ncmds_in_driver--;
22081 ASSERT(un->un_ncmds_in_driver >= 0);
22082 mutex_exit(SD_MUTEX(un));
22083 err = ENOTTY;
22084 goto done_without_assess;
22086 break;
22087 case DKIOCFLUSHWRITECACHE:
22088 mutex_exit(SD_MUTEX(un));
22089 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22090 if (err != 0) {
22091 mutex_enter(SD_MUTEX(un));
22092 un->un_ncmds_in_driver--;
22093 ASSERT(un->un_ncmds_in_driver >= 0);
22094 mutex_exit(SD_MUTEX(un));
22095 err = EIO;
22096 goto done_quick_assess;
22098 mutex_enter(SD_MUTEX(un));
22099 /* FALLTHROUGH */
22100 case DKIOCREMOVABLE:
22101 case DKIOCHOTPLUGGABLE:
22102 case DKIOCINFO:
22103 case DKIOCGMEDIAINFO:
22104 case DKIOCGMEDIAINFOEXT:
22105 case DKIOCSOLIDSTATE:
22106 case MHIOCENFAILFAST:
22107 case MHIOCSTATUS:
22108 case MHIOCTKOWN:
22109 case MHIOCRELEASE:
22110 case MHIOCGRP_INKEYS:
22111 case MHIOCGRP_INRESV:
22112 case MHIOCGRP_REGISTER:
22113 case MHIOCGRP_CLEAR:
22114 case MHIOCGRP_RESERVE:
22115 case MHIOCGRP_PREEMPTANDABORT:
22116 case MHIOCGRP_REGISTERANDIGNOREKEY:
22117 case CDROMCLOSETRAY:
22118 case USCSICMD:
22119 goto skip_ready_valid;
22120 default:
22121 break;
22124 mutex_exit(SD_MUTEX(un));
22125 err = sd_ready_and_valid(ssc, SDPART(dev));
22126 mutex_enter(SD_MUTEX(un));
22128 if (err != SD_READY_VALID) {
22129 switch (cmd) {
22130 case DKIOCSTATE:
22131 case CDROMGDRVSPEED:
22132 case CDROMSDRVSPEED:
22133 case FDEJECT: /* for eject command */
22134 case DKIOCEJECT:
22135 case CDROMEJECT:
22136 case DKIOCREMOVABLE:
22137 case DKIOCHOTPLUGGABLE:
22138 break;
22139 default:
22140 if (un->un_f_has_removable_media) {
22141 err = ENXIO;
22142 } else {
22143 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22144 if (err == SD_RESERVED_BY_OTHERS) {
22145 err = EACCES;
22146 } else {
22147 err = EIO;
22150 un->un_ncmds_in_driver--;
22151 ASSERT(un->un_ncmds_in_driver >= 0);
22152 mutex_exit(SD_MUTEX(un));
22154 goto done_without_assess;
22159 skip_ready_valid:
22160 mutex_exit(SD_MUTEX(un));
22162 switch (cmd) {
22163 case DKIOCINFO:
22164 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22165 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22166 break;
22168 case DKIOCGMEDIAINFO:
22169 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22170 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22171 break;
22173 case DKIOCGMEDIAINFOEXT:
22174 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22175 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22176 break;
22178 case DKIOCGGEOM:
22179 case DKIOCGVTOC:
22180 case DKIOCGEXTVTOC:
22181 case DKIOCGAPART:
22182 case DKIOCPARTINFO:
22183 case DKIOCEXTPARTINFO:
22184 case DKIOCSGEOM:
22185 case DKIOCSAPART:
22186 case DKIOCGETEFI:
22187 case DKIOCPARTITION:
22188 case DKIOCSVTOC:
22189 case DKIOCSEXTVTOC:
22190 case DKIOCSETEFI:
22191 case DKIOCGMBOOT:
22192 case DKIOCSMBOOT:
22193 case DKIOCG_PHYGEOM:
22194 case DKIOCG_VIRTGEOM:
22195 #if defined(__i386) || defined(__amd64)
22196 case DKIOCSETEXTPART:
22197 #endif
22198 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22200 /* TUR should spin up */
22202 if (un->un_f_has_removable_media)
22203 err = sd_send_scsi_TEST_UNIT_READY(ssc,
22204 SD_CHECK_FOR_MEDIA);
22206 else
22207 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22209 if (err != 0)
22210 goto done_with_assess;
22212 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22213 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22215 if ((err == 0) &&
22216 ((cmd == DKIOCSETEFI) ||
22217 ((un->un_f_pkstats_enabled) &&
22218 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22219 cmd == DKIOCSEXTVTOC)))) {
22221 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22222 (void *)SD_PATH_DIRECT);
22223 if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22224 sd_set_pstats(un);
22225 SD_TRACE(SD_LOG_IO_PARTITION, un,
22226 "sd_ioctl: un:0x%p pstats created and "
22227 "set\n", un);
22231 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22232 ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22234 mutex_enter(SD_MUTEX(un));
22235 if (un->un_f_devid_supported &&
22236 (un->un_f_opt_fab_devid == TRUE)) {
22237 if (un->un_devid == NULL) {
22238 sd_register_devid(ssc, SD_DEVINFO(un),
22239 SD_TARGET_IS_UNRESERVED);
22240 } else {
22242 * The device id for this disk
22243 * has been fabricated. The
22244 * device id must be preserved
22245 * by writing it back out to
22246 * disk.
22248 if (sd_write_deviceid(ssc) != 0) {
22249 ddi_devid_free(un->un_devid);
22250 un->un_devid = NULL;
22254 mutex_exit(SD_MUTEX(un));
22257 break;
22259 case DKIOCLOCK:
22260 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22261 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22262 SD_PATH_STANDARD);
22263 goto done_with_assess;
22265 case DKIOCUNLOCK:
22266 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22267 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22268 SD_PATH_STANDARD);
22269 goto done_with_assess;
22271 case DKIOCSTATE: {
22272 enum dkio_state state;
22273 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22275 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22276 err = EFAULT;
22277 } else {
22278 err = sd_check_media(dev, state);
22279 if (err == 0) {
22280 if (ddi_copyout(&un->un_mediastate, (void *)arg,
22281 sizeof (int), flag) != 0)
22282 err = EFAULT;
22285 break;
22288 case DKIOCREMOVABLE:
22289 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22290 i = un->un_f_has_removable_media ? 1 : 0;
22291 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22292 err = EFAULT;
22293 } else {
22294 err = 0;
22296 break;
22298 case DKIOCSOLIDSTATE:
22299 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22300 i = un->un_f_is_solid_state ? 1 : 0;
22301 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22302 err = EFAULT;
22303 } else {
22304 err = 0;
22306 break;
22308 case DKIOCHOTPLUGGABLE:
22309 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22310 i = un->un_f_is_hotpluggable ? 1 : 0;
22311 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22312 err = EFAULT;
22313 } else {
22314 err = 0;
22316 break;
22318 case DKIOCREADONLY:
22319 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22320 i = 0;
22321 if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22322 (sr_check_wp(dev) != 0)) {
22323 i = 1;
22325 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22326 err = EFAULT;
22327 } else {
22328 err = 0;
22330 break;
22332 case DKIOCGTEMPERATURE:
22333 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22334 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22335 break;
22337 case MHIOCENFAILFAST:
22338 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22339 if ((err = drv_priv(cred_p)) == 0) {
22340 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22342 break;
22344 case MHIOCTKOWN:
22345 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22346 if ((err = drv_priv(cred_p)) == 0) {
22347 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22349 break;
22351 case MHIOCRELEASE:
22352 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22353 if ((err = drv_priv(cred_p)) == 0) {
22354 err = sd_mhdioc_release(dev);
22356 break;
22358 case MHIOCSTATUS:
22359 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22360 if ((err = drv_priv(cred_p)) == 0) {
22361 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22362 case 0:
22363 err = 0;
22364 break;
22365 case EACCES:
22366 *rval_p = 1;
22367 err = 0;
22368 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22369 break;
22370 default:
22371 err = EIO;
22372 goto done_with_assess;
22375 break;
22377 case MHIOCQRESERVE:
22378 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22379 if ((err = drv_priv(cred_p)) == 0) {
22380 err = sd_reserve_release(dev, SD_RESERVE);
22382 break;
22384 case MHIOCREREGISTERDEVID:
22385 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22386 if (drv_priv(cred_p) == EPERM) {
22387 err = EPERM;
22388 } else if (!un->un_f_devid_supported) {
22389 err = ENOTTY;
22390 } else {
22391 err = sd_mhdioc_register_devid(dev);
22393 break;
22395 case MHIOCGRP_INKEYS:
22396 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22397 if (((err = drv_priv(cred_p)) != EPERM) &&
22398 arg != (intptr_t)NULL) {
22399 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22400 err = ENOTSUP;
22401 } else {
22402 err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22403 flag);
22406 break;
22408 case MHIOCGRP_INRESV:
22409 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22410 if (((err = drv_priv(cred_p)) != EPERM) &&
22411 arg != (intptr_t)NULL) {
22412 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22413 err = ENOTSUP;
22414 } else {
22415 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22418 break;
22420 case MHIOCGRP_REGISTER:
22421 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22422 if ((err = drv_priv(cred_p)) != EPERM) {
22423 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22424 err = ENOTSUP;
22425 } else if (arg != (intptr_t)NULL) {
22426 mhioc_register_t reg;
22427 if (ddi_copyin((void *)arg, &reg,
22428 sizeof (mhioc_register_t), flag) != 0) {
22429 err = EFAULT;
22430 } else {
22431 err =
22432 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22433 ssc, SD_SCSI3_REGISTER,
22434 (uchar_t *)&reg);
22435 if (err != 0)
22436 goto done_with_assess;
22440 break;
22442 case MHIOCGRP_CLEAR:
22443 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22444 if ((err = drv_priv(cred_p)) != EPERM) {
22445 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22446 err = ENOTSUP;
22447 } else if (arg != (intptr_t)NULL) {
22448 mhioc_register_t reg;
22449 if (ddi_copyin((void *)arg, &reg,
22450 sizeof (mhioc_register_t), flag) != 0) {
22451 err = EFAULT;
22452 } else {
22453 err =
22454 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22455 ssc, SD_SCSI3_CLEAR,
22456 (uchar_t *)&reg);
22457 if (err != 0)
22458 goto done_with_assess;
22462 break;
22464 case MHIOCGRP_RESERVE:
22465 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22466 if ((err = drv_priv(cred_p)) != EPERM) {
22467 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22468 err = ENOTSUP;
22469 } else if (arg != (intptr_t)NULL) {
22470 mhioc_resv_desc_t resv_desc;
22471 if (ddi_copyin((void *)arg, &resv_desc,
22472 sizeof (mhioc_resv_desc_t), flag) != 0) {
22473 err = EFAULT;
22474 } else {
22475 err =
22476 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22477 ssc, SD_SCSI3_RESERVE,
22478 (uchar_t *)&resv_desc);
22479 if (err != 0)
22480 goto done_with_assess;
22484 break;
22486 case MHIOCGRP_PREEMPTANDABORT:
22487 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22488 if ((err = drv_priv(cred_p)) != EPERM) {
22489 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22490 err = ENOTSUP;
22491 } else if (arg != (intptr_t)NULL) {
22492 mhioc_preemptandabort_t preempt_abort;
22493 if (ddi_copyin((void *)arg, &preempt_abort,
22494 sizeof (mhioc_preemptandabort_t),
22495 flag) != 0) {
22496 err = EFAULT;
22497 } else {
22498 err =
22499 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22500 ssc, SD_SCSI3_PREEMPTANDABORT,
22501 (uchar_t *)&preempt_abort);
22502 if (err != 0)
22503 goto done_with_assess;
22507 break;
22509 case MHIOCGRP_REGISTERANDIGNOREKEY:
22510 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22511 if ((err = drv_priv(cred_p)) != EPERM) {
22512 if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22513 err = ENOTSUP;
22514 } else if (arg != (intptr_t)NULL) {
22515 mhioc_registerandignorekey_t r_and_i;
22516 if (ddi_copyin((void *)arg, (void *)&r_and_i,
22517 sizeof (mhioc_registerandignorekey_t),
22518 flag) != 0) {
22519 err = EFAULT;
22520 } else {
22521 err =
22522 sd_send_scsi_PERSISTENT_RESERVE_OUT(
22523 ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22524 (uchar_t *)&r_and_i);
22525 if (err != 0)
22526 goto done_with_assess;
22530 break;
22532 case USCSICMD:
22533 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22534 cr = ddi_get_cred();
22535 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22536 err = EPERM;
22537 } else {
22538 enum uio_seg uioseg;
22540 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22541 UIO_USERSPACE;
22542 if (un->un_f_format_in_progress == TRUE) {
22543 err = EAGAIN;
22544 break;
22547 err = sd_ssc_send(ssc,
22548 (struct uscsi_cmd *)arg,
22549 flag, uioseg, SD_PATH_STANDARD);
22550 if (err != 0)
22551 goto done_with_assess;
22552 else
22553 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22555 break;
22557 case CDROMPAUSE:
22558 case CDROMRESUME:
22559 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22560 if (!ISCD(un)) {
22561 err = ENOTTY;
22562 } else {
22563 err = sr_pause_resume(dev, cmd);
22565 break;
22567 case CDROMPLAYMSF:
22568 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22569 if (!ISCD(un)) {
22570 err = ENOTTY;
22571 } else {
22572 err = sr_play_msf(dev, (caddr_t)arg, flag);
22574 break;
22576 case CDROMPLAYTRKIND:
22577 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22578 #if defined(__i386) || defined(__amd64)
22580 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22582 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22583 #else
22584 if (!ISCD(un)) {
22585 #endif
22586 err = ENOTTY;
22587 } else {
22588 err = sr_play_trkind(dev, (caddr_t)arg, flag);
22590 break;
22592 case CDROMREADTOCHDR:
22593 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22594 if (!ISCD(un)) {
22595 err = ENOTTY;
22596 } else {
22597 err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22599 break;
22601 case CDROMREADTOCENTRY:
22602 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22603 if (!ISCD(un)) {
22604 err = ENOTTY;
22605 } else {
22606 err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22608 break;
22610 case CDROMSTOP:
22611 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22612 if (!ISCD(un)) {
22613 err = ENOTTY;
22614 } else {
22615 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22616 SD_TARGET_STOP, SD_PATH_STANDARD);
22617 goto done_with_assess;
22619 break;
22621 case CDROMSTART:
22622 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22623 if (!ISCD(un)) {
22624 err = ENOTTY;
22625 } else {
22626 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22627 SD_TARGET_START, SD_PATH_STANDARD);
22628 goto done_with_assess;
22630 break;
22632 case CDROMCLOSETRAY:
22633 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22634 if (!ISCD(un)) {
22635 err = ENOTTY;
22636 } else {
22637 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22638 SD_TARGET_CLOSE, SD_PATH_STANDARD);
22639 goto done_with_assess;
22641 break;
22643 case FDEJECT: /* for eject command */
22644 case DKIOCEJECT:
22645 case CDROMEJECT:
22646 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22647 if (!un->un_f_eject_media_supported) {
22648 err = ENOTTY;
22649 } else {
22650 err = sr_eject(dev);
22652 break;
22654 case CDROMVOLCTRL:
22655 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22656 if (!ISCD(un)) {
22657 err = ENOTTY;
22658 } else {
22659 err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22661 break;
22663 case CDROMSUBCHNL:
22664 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22665 if (!ISCD(un)) {
22666 err = ENOTTY;
22667 } else {
22668 err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22670 break;
22672 case CDROMREADMODE2:
22673 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22674 if (!ISCD(un)) {
22675 err = ENOTTY;
22676 } else if (un->un_f_cfg_is_atapi == TRUE) {
22678 * If the drive supports READ CD, use that instead of
22679 * switching the LBA size via a MODE SELECT
22680 * Block Descriptor
22682 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22683 } else {
22684 err = sr_read_mode2(dev, (caddr_t)arg, flag);
22686 break;
22688 case CDROMREADMODE1:
22689 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22690 if (!ISCD(un)) {
22691 err = ENOTTY;
22692 } else {
22693 err = sr_read_mode1(dev, (caddr_t)arg, flag);
22695 break;
22697 case CDROMREADOFFSET:
22698 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22699 if (!ISCD(un)) {
22700 err = ENOTTY;
22701 } else {
22702 err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22703 flag);
22705 break;
22707 case CDROMSBLKMODE:
22708 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22710 * There is no means of changing block size in case of atapi
22711 * drives, thus return ENOTTY if drive type is atapi
22713 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22714 err = ENOTTY;
22715 } else if (un->un_f_mmc_cap == TRUE) {
22718 * MMC Devices do not support changing the
22719 * logical block size
22721 * Note: EINVAL is being returned instead of ENOTTY to
22722 * maintain consistancy with the original mmc
22723 * driver update.
22725 err = EINVAL;
22726 } else {
22727 mutex_enter(SD_MUTEX(un));
22728 if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22729 (un->un_ncmds_in_transport > 0)) {
22730 mutex_exit(SD_MUTEX(un));
22731 err = EINVAL;
22732 } else {
22733 mutex_exit(SD_MUTEX(un));
22734 err = sr_change_blkmode(dev, cmd, arg, flag);
22737 break;
22739 case CDROMGBLKMODE:
22740 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22741 if (!ISCD(un)) {
22742 err = ENOTTY;
22743 } else if ((un->un_f_cfg_is_atapi != FALSE) &&
22744 (un->un_f_blockcount_is_valid != FALSE)) {
22746 * Drive is an ATAPI drive so return target block
22747 * size for ATAPI drives since we cannot change the
22748 * blocksize on ATAPI drives. Used primarily to detect
22749 * if an ATAPI cdrom is present.
22751 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22752 sizeof (int), flag) != 0) {
22753 err = EFAULT;
22754 } else {
22755 err = 0;
22758 } else {
22760 * Drive supports changing block sizes via a Mode
22761 * Select.
22763 err = sr_change_blkmode(dev, cmd, arg, flag);
22765 break;
22767 case CDROMGDRVSPEED:
22768 case CDROMSDRVSPEED:
22769 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22770 if (!ISCD(un)) {
22771 err = ENOTTY;
22772 } else if (un->un_f_mmc_cap == TRUE) {
22774 * Note: In the future the driver implementation
22775 * for getting and
22776 * setting cd speed should entail:
22777 * 1) If non-mmc try the Toshiba mode page
22778 * (sr_change_speed)
22779 * 2) If mmc but no support for Real Time Streaming try
22780 * the SET CD SPEED (0xBB) command
22781 * (sr_atapi_change_speed)
22782 * 3) If mmc and support for Real Time Streaming
22783 * try the GET PERFORMANCE and SET STREAMING
22784 * commands (not yet implemented, 4380808)
22787 * As per recent MMC spec, CD-ROM speed is variable
22788 * and changes with LBA. Since there is no such
22789 * things as drive speed now, fail this ioctl.
22791 * Note: EINVAL is returned for consistancy of original
22792 * implementation which included support for getting
22793 * the drive speed of mmc devices but not setting
22794 * the drive speed. Thus EINVAL would be returned
22795 * if a set request was made for an mmc device.
22796 * We no longer support get or set speed for
22797 * mmc but need to remain consistent with regard
22798 * to the error code returned.
22800 err = EINVAL;
22801 } else if (un->un_f_cfg_is_atapi == TRUE) {
22802 err = sr_atapi_change_speed(dev, cmd, arg, flag);
22803 } else {
22804 err = sr_change_speed(dev, cmd, arg, flag);
22806 break;
22808 case CDROMCDDA:
22809 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22810 if (!ISCD(un)) {
22811 err = ENOTTY;
22812 } else {
22813 err = sr_read_cdda(dev, (void *)arg, flag);
22815 break;
22817 case CDROMCDXA:
22818 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22819 if (!ISCD(un)) {
22820 err = ENOTTY;
22821 } else {
22822 err = sr_read_cdxa(dev, (caddr_t)arg, flag);
22824 break;
22826 case CDROMSUBCODE:
22827 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
22828 if (!ISCD(un)) {
22829 err = ENOTTY;
22830 } else {
22831 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
22833 break;
22836 #ifdef SDDEBUG
22837 /* RESET/ABORTS testing ioctls */
22838 case DKIOCRESET: {
22839 int reset_level;
22841 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
22842 err = EFAULT;
22843 } else {
22844 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
22845 "reset_level = 0x%lx\n", reset_level);
22846 if (scsi_reset(SD_ADDRESS(un), reset_level)) {
22847 err = 0;
22848 } else {
22849 err = EIO;
22852 break;
22855 case DKIOCABORT:
22856 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
22857 if (scsi_abort(SD_ADDRESS(un), NULL)) {
22858 err = 0;
22859 } else {
22860 err = EIO;
22862 break;
22863 #endif
22865 #ifdef SD_FAULT_INJECTION
22866 /* SDIOC FaultInjection testing ioctls */
22867 case SDIOCSTART:
22868 case SDIOCSTOP:
22869 case SDIOCINSERTPKT:
22870 case SDIOCINSERTXB:
22871 case SDIOCINSERTUN:
22872 case SDIOCINSERTARQ:
22873 case SDIOCPUSH:
22874 case SDIOCRETRIEVE:
22875 case SDIOCRUN:
22876 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
22877 "SDIOC detected cmd:0x%X:\n", cmd);
22878 /* call error generator */
22879 sd_faultinjection_ioctl(cmd, arg, un);
22880 err = 0;
22881 break;
22883 #endif /* SD_FAULT_INJECTION */
22885 case DKIOCFLUSHWRITECACHE:
22887 struct dk_callback *dkc = (struct dk_callback *)arg;
22889 mutex_enter(SD_MUTEX(un));
22890 if (!un->un_f_sync_cache_supported ||
22891 !un->un_f_write_cache_enabled) {
22892 err = un->un_f_sync_cache_supported ?
22893 0 : ENOTSUP;
22894 mutex_exit(SD_MUTEX(un));
22895 if ((flag & FKIOCTL) && dkc != NULL &&
22896 dkc->dkc_callback != NULL) {
22897 (*dkc->dkc_callback)(dkc->dkc_cookie,
22898 err);
22900 * Did callback and reported error.
22901 * Since we did a callback, ioctl
22902 * should return 0.
22904 err = 0;
22906 break;
22908 mutex_exit(SD_MUTEX(un));
22910 if ((flag & FKIOCTL) && dkc != NULL &&
22911 dkc->dkc_callback != NULL) {
22912 /* async SYNC CACHE request */
22913 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
22914 } else {
22915 /* synchronous SYNC CACHE request */
22916 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22919 break;
22921 case DKIOCFREE:
22923 dkioc_free_list_t *dfl = (dkioc_free_list_t *)arg;
22925 /* bad ioctls shouldn't panic */
22926 if (dfl == NULL) {
22927 /* check kernel callers strictly in debug */
22928 ASSERT0(flag & FKIOCTL);
22929 err = SET_ERROR(EINVAL);
22930 break;
22932 /* synchronous UNMAP request */
22933 err = sd_send_scsi_UNMAP(dev, ssc, dfl, flag);
22935 break;
22937 case DKIOCGETWCE: {
22939 int wce;
22941 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
22942 break;
22945 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
22946 err = EFAULT;
22948 break;
22951 case DKIOCSETWCE: {
22953 int wce, sync_supported;
22954 int cur_wce = 0;
22956 if (!un->un_f_cache_mode_changeable) {
22957 err = EINVAL;
22958 break;
22961 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
22962 err = EFAULT;
22963 break;
22967 * Synchronize multiple threads trying to enable
22968 * or disable the cache via the un_f_wcc_cv
22969 * condition variable.
22971 mutex_enter(SD_MUTEX(un));
22974 * Don't allow the cache to be enabled if the
22975 * config file has it disabled.
22977 if (un->un_f_opt_disable_cache && wce) {
22978 mutex_exit(SD_MUTEX(un));
22979 err = EINVAL;
22980 break;
22984 * Wait for write cache change in progress
22985 * bit to be clear before proceeding.
22987 while (un->un_f_wcc_inprog)
22988 cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
22990 un->un_f_wcc_inprog = 1;
22992 mutex_exit(SD_MUTEX(un));
22995 * Get the current write cache state
22997 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
22998 mutex_enter(SD_MUTEX(un));
22999 un->un_f_wcc_inprog = 0;
23000 cv_broadcast(&un->un_wcc_cv);
23001 mutex_exit(SD_MUTEX(un));
23002 break;
23005 mutex_enter(SD_MUTEX(un));
23006 un->un_f_write_cache_enabled = (cur_wce != 0);
23008 if (un->un_f_write_cache_enabled && wce == 0) {
23010 * Disable the write cache. Don't clear
23011 * un_f_write_cache_enabled until after
23012 * the mode select and flush are complete.
23014 sync_supported = un->un_f_sync_cache_supported;
23017 * If cache flush is suppressed, we assume that the
23018 * controller firmware will take care of managing the
23019 * write cache for us: no need to explicitly
23020 * disable it.
23022 if (!un->un_f_suppress_cache_flush) {
23023 mutex_exit(SD_MUTEX(un));
23024 if ((err = sd_cache_control(ssc,
23025 SD_CACHE_NOCHANGE,
23026 SD_CACHE_DISABLE)) == 0 &&
23027 sync_supported) {
23028 err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23029 NULL);
23031 } else {
23032 mutex_exit(SD_MUTEX(un));
23035 mutex_enter(SD_MUTEX(un));
23036 if (err == 0) {
23037 un->un_f_write_cache_enabled = 0;
23040 } else if (!un->un_f_write_cache_enabled && wce != 0) {
23042 * Set un_f_write_cache_enabled first, so there is
23043 * no window where the cache is enabled, but the
23044 * bit says it isn't.
23046 un->un_f_write_cache_enabled = 1;
23049 * If cache flush is suppressed, we assume that the
23050 * controller firmware will take care of managing the
23051 * write cache for us: no need to explicitly
23052 * enable it.
23054 if (!un->un_f_suppress_cache_flush) {
23055 mutex_exit(SD_MUTEX(un));
23056 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23057 SD_CACHE_ENABLE);
23058 } else {
23059 mutex_exit(SD_MUTEX(un));
23062 mutex_enter(SD_MUTEX(un));
23064 if (err) {
23065 un->un_f_write_cache_enabled = 0;
23069 un->un_f_wcc_inprog = 0;
23070 cv_broadcast(&un->un_wcc_cv);
23071 mutex_exit(SD_MUTEX(un));
23072 break;
23075 default:
23076 err = ENOTTY;
23077 break;
23079 mutex_enter(SD_MUTEX(un));
23080 un->un_ncmds_in_driver--;
23081 ASSERT(un->un_ncmds_in_driver >= 0);
23082 mutex_exit(SD_MUTEX(un));
23085 done_without_assess:
23086 sd_ssc_fini(ssc);
23088 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23089 return (err);
23091 done_with_assess:
23092 mutex_enter(SD_MUTEX(un));
23093 un->un_ncmds_in_driver--;
23094 ASSERT(un->un_ncmds_in_driver >= 0);
23095 mutex_exit(SD_MUTEX(un));
23097 done_quick_assess:
23098 if (err != 0)
23099 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23100 /* Uninitialize sd_ssc_t pointer */
23101 sd_ssc_fini(ssc);
23103 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23104 return (err);
23109 * Function: sd_dkio_ctrl_info
23111 * Description: This routine is the driver entry point for handling controller
23112 * information ioctl requests (DKIOCINFO).
23114 * Arguments: dev - the device number
23115 * arg - pointer to user provided dk_cinfo structure
23116 * specifying the controller type and attributes.
23117 * flag - this argument is a pass through to ddi_copyxxx()
23118 * directly from the mode argument of ioctl().
23120 * Return Code: 0
23121 * EFAULT
23122 * ENXIO
23125 static int
23126 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23128 struct sd_lun *un = NULL;
23129 struct dk_cinfo *info;
23130 dev_info_t *pdip;
23131 int lun, tgt;
23133 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23134 return (ENXIO);
23137 info = (struct dk_cinfo *)
23138 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23140 switch (un->un_ctype) {
23141 case CTYPE_CDROM:
23142 info->dki_ctype = DKC_CDROM;
23143 break;
23144 default:
23145 info->dki_ctype = DKC_SCSI_CCS;
23146 break;
23148 pdip = ddi_get_parent(SD_DEVINFO(un));
23149 info->dki_cnum = ddi_get_instance(pdip);
23150 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23151 (void) strcpy(info->dki_cname, ddi_get_name(pdip));
23152 } else {
23153 (void) strncpy(info->dki_cname, ddi_node_name(pdip),
23154 DK_DEVLEN - 1);
23157 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23158 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23159 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23160 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23162 /* Unit Information */
23163 info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23164 info->dki_slave = ((tgt << 3) | lun);
23165 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23166 DK_DEVLEN - 1);
23167 info->dki_flags = DKI_FMTVOL;
23168 info->dki_partition = SDPART(dev);
23170 /* Max Transfer size of this device in blocks */
23171 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23172 info->dki_addr = 0;
23173 info->dki_space = 0;
23174 info->dki_prio = 0;
23175 info->dki_vec = 0;
23177 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23178 kmem_free(info, sizeof (struct dk_cinfo));
23179 return (EFAULT);
23180 } else {
23181 kmem_free(info, sizeof (struct dk_cinfo));
23182 return (0);
23187 * Function: sd_get_media_info_com
23189 * Description: This routine returns the information required to populate
23190 * the fields for the dk_minfo/dk_minfo_ext structures.
23192 * Arguments: dev - the device number
23193 * dki_media_type - media_type
23194 * dki_lbsize - logical block size
23195 * dki_capacity - capacity in blocks
23196 * dki_pbsize - physical block size (if requested)
23198 * Return Code: 0
23199 * EACCESS
23200 * EFAULT
23201 * ENXIO
23202 * EIO
23204 static int
23205 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23206 diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23208 struct sd_lun *un = NULL;
23209 struct uscsi_cmd com;
23210 struct scsi_inquiry *sinq;
23211 u_longlong_t media_capacity;
23212 uint64_t capacity;
23213 uint_t lbasize;
23214 uint_t pbsize;
23215 uchar_t *out_data;
23216 uchar_t *rqbuf;
23217 int rval = 0;
23218 int rtn;
23219 sd_ssc_t *ssc;
23221 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23222 (un->un_state == SD_STATE_OFFLINE)) {
23223 return (ENXIO);
23226 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23228 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23229 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23230 ssc = sd_ssc_init(un);
23232 /* Issue a TUR to determine if the drive is ready with media present */
23233 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23234 if (rval == ENXIO) {
23235 goto done;
23236 } else if (rval != 0) {
23237 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23240 /* Now get configuration data */
23241 if (ISCD(un)) {
23242 *dki_media_type = DK_CDROM;
23244 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23245 if (un->un_f_mmc_cap == TRUE) {
23246 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23247 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23248 SD_PATH_STANDARD);
23250 if (rtn) {
23252 * We ignore all failures for CD and need to
23253 * put the assessment before processing code
23254 * to avoid missing assessment for FMA.
23256 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23258 * Failed for other than an illegal request
23259 * or command not supported
23261 if ((com.uscsi_status == STATUS_CHECK) &&
23262 (com.uscsi_rqstatus == STATUS_GOOD)) {
23263 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23264 (rqbuf[12] != 0x20)) {
23265 rval = EIO;
23266 goto no_assessment;
23269 } else {
23271 * The GET CONFIGURATION command succeeded
23272 * so set the media type according to the
23273 * returned data
23275 *dki_media_type = out_data[6];
23276 *dki_media_type <<= 8;
23277 *dki_media_type |= out_data[7];
23280 } else {
23282 * The profile list is not available, so we attempt to identify
23283 * the media type based on the inquiry data
23285 sinq = un->un_sd->sd_inq;
23286 if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23287 (sinq->inq_dtype == DTYPE_OPTICAL)) {
23288 /* This is a direct access device or optical disk */
23289 *dki_media_type = DK_FIXED_DISK;
23291 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23292 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23293 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23294 *dki_media_type = DK_ZIP;
23295 } else if (
23296 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23297 *dki_media_type = DK_JAZ;
23300 } else {
23302 * Not a CD, direct access or optical disk so return
23303 * unknown media
23305 *dki_media_type = DK_UNKNOWN;
23310 * Now read the capacity so we can provide the lbasize,
23311 * pbsize and capacity.
23313 if (dki_pbsize && un->un_f_descr_format_supported) {
23314 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23315 &pbsize, SD_PATH_DIRECT);
23318 * Override the physical blocksize if the instance already
23319 * has a larger value.
23321 pbsize = MAX(pbsize, un->un_phy_blocksize);
23324 if (dki_pbsize == NULL || rval != 0 ||
23325 !un->un_f_descr_format_supported) {
23326 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23327 SD_PATH_DIRECT);
23329 switch (rval) {
23330 case 0:
23331 if (un->un_f_enable_rmw &&
23332 un->un_phy_blocksize != 0) {
23333 pbsize = un->un_phy_blocksize;
23334 } else {
23335 pbsize = lbasize;
23337 media_capacity = capacity;
23340 * sd_send_scsi_READ_CAPACITY() reports capacity in
23341 * un->un_sys_blocksize chunks. So we need to convert
23342 * it into cap.lbsize chunks.
23344 if (un->un_f_has_removable_media) {
23345 media_capacity *= un->un_sys_blocksize;
23346 media_capacity /= lbasize;
23348 break;
23349 case EACCES:
23350 rval = EACCES;
23351 goto done;
23352 default:
23353 rval = EIO;
23354 goto done;
23356 } else {
23357 if (un->un_f_enable_rmw &&
23358 !ISP2(pbsize % DEV_BSIZE)) {
23359 pbsize = SSD_SECSIZE;
23360 } else if (!ISP2(lbasize % DEV_BSIZE) ||
23361 !ISP2(pbsize % DEV_BSIZE)) {
23362 pbsize = lbasize = DEV_BSIZE;
23364 media_capacity = capacity;
23368 * If lun is expanded dynamically, update the un structure.
23370 mutex_enter(SD_MUTEX(un));
23371 if ((un->un_f_blockcount_is_valid == TRUE) &&
23372 (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23373 (capacity > un->un_blockcount)) {
23374 un->un_f_expnevent = B_FALSE;
23375 sd_update_block_info(un, lbasize, capacity);
23377 mutex_exit(SD_MUTEX(un));
23379 *dki_lbsize = lbasize;
23380 *dki_capacity = media_capacity;
23381 if (dki_pbsize)
23382 *dki_pbsize = pbsize;
23384 done:
23385 if (rval != 0) {
23386 if (rval == EIO)
23387 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23388 else
23389 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23391 no_assessment:
23392 sd_ssc_fini(ssc);
23393 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23394 kmem_free(rqbuf, SENSE_LENGTH);
23395 return (rval);
23399 * Function: sd_get_media_info
23401 * Description: This routine is the driver entry point for handling ioctl
23402 * requests for the media type or command set profile used by the
23403 * drive to operate on the media (DKIOCGMEDIAINFO).
23405 * Arguments: dev - the device number
23406 * arg - pointer to user provided dk_minfo structure
23407 * specifying the media type, logical block size and
23408 * drive capacity.
23409 * flag - this argument is a pass through to ddi_copyxxx()
23410 * directly from the mode argument of ioctl().
23412 * Return Code: returns the value from sd_get_media_info_com
23414 static int
23415 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23417 struct dk_minfo mi;
23418 int rval;
23420 rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23421 &mi.dki_lbsize, &mi.dki_capacity, NULL);
23423 if (rval)
23424 return (rval);
23425 if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23426 rval = EFAULT;
23427 return (rval);
23431 * Function: sd_get_media_info_ext
23433 * Description: This routine is the driver entry point for handling ioctl
23434 * requests for the media type or command set profile used by the
23435 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23436 * difference this ioctl and DKIOCGMEDIAINFO is the return value
23437 * of this ioctl contains both logical block size and physical
23438 * block size.
23441 * Arguments: dev - the device number
23442 * arg - pointer to user provided dk_minfo_ext structure
23443 * specifying the media type, logical block size,
23444 * physical block size and disk capacity.
23445 * flag - this argument is a pass through to ddi_copyxxx()
23446 * directly from the mode argument of ioctl().
23448 * Return Code: returns the value from sd_get_media_info_com
23450 static int
23451 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23453 struct dk_minfo_ext mie;
23454 int rval = 0;
23456 rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23457 &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23459 if (rval)
23460 return (rval);
23461 if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23462 rval = EFAULT;
23463 return (rval);
23468 * Function: sd_watch_request_submit
23470 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23471 * depending on which is supported by device.
23473 static opaque_t
23474 sd_watch_request_submit(struct sd_lun *un)
23476 dev_t dev;
23478 /* All submissions are unified to use same device number */
23479 dev = sd_make_device(SD_DEVINFO(un));
23481 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23482 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23483 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23484 (caddr_t)dev));
23485 } else {
23486 return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23487 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23488 (caddr_t)dev));
23494 * Function: sd_check_media
23496 * Description: This utility routine implements the functionality for the
23497 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23498 * driver state changes from that specified by the user
23499 * (inserted or ejected). For example, if the user specifies
23500 * DKIO_EJECTED and the current media state is inserted this
23501 * routine will immediately return DKIO_INSERTED. However, if the
23502 * current media state is not inserted the user thread will be
23503 * blocked until the drive state changes. If DKIO_NONE is specified
23504 * the user thread will block until a drive state change occurs.
23506 * Arguments: dev - the device number
23507 * state - user pointer to a dkio_state, updated with the current
23508 * drive state at return.
23510 * Return Code: ENXIO
23511 * EIO
23512 * EAGAIN
23513 * EINTR
23516 static int
23517 sd_check_media(dev_t dev, enum dkio_state state)
23519 struct sd_lun *un = NULL;
23520 enum dkio_state prev_state;
23521 opaque_t token = NULL;
23522 int rval = 0;
23523 sd_ssc_t *ssc;
23525 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23526 return (ENXIO);
23529 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23531 ssc = sd_ssc_init(un);
23533 mutex_enter(SD_MUTEX(un));
23535 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23536 "state=%x, mediastate=%x\n", state, un->un_mediastate);
23538 prev_state = un->un_mediastate;
23540 /* is there anything to do? */
23541 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23543 * submit the request to the scsi_watch service;
23544 * scsi_media_watch_cb() does the real work
23546 mutex_exit(SD_MUTEX(un));
23549 * This change handles the case where a scsi watch request is
23550 * added to a device that is powered down. To accomplish this
23551 * we power up the device before adding the scsi watch request,
23552 * since the scsi watch sends a TUR directly to the device
23553 * which the device cannot handle if it is powered down.
23555 if (sd_pm_entry(un) != DDI_SUCCESS) {
23556 mutex_enter(SD_MUTEX(un));
23557 goto done;
23560 token = sd_watch_request_submit(un);
23562 sd_pm_exit(un);
23564 mutex_enter(SD_MUTEX(un));
23565 if (token == NULL) {
23566 rval = EAGAIN;
23567 goto done;
23571 * This is a special case IOCTL that doesn't return
23572 * until the media state changes. Routine sdpower
23573 * knows about and handles this so don't count it
23574 * as an active cmd in the driver, which would
23575 * keep the device busy to the pm framework.
23576 * If the count isn't decremented the device can't
23577 * be powered down.
23579 un->un_ncmds_in_driver--;
23580 ASSERT(un->un_ncmds_in_driver >= 0);
23583 * if a prior request had been made, this will be the same
23584 * token, as scsi_watch was designed that way.
23586 un->un_swr_token = token;
23587 un->un_specified_mediastate = state;
23590 * now wait for media change
23591 * we will not be signalled unless mediastate == state but it is
23592 * still better to test for this condition, since there is a
23593 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23595 SD_TRACE(SD_LOG_COMMON, un,
23596 "sd_check_media: waiting for media state change\n");
23597 while (un->un_mediastate == state) {
23598 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23599 SD_TRACE(SD_LOG_COMMON, un,
23600 "sd_check_media: waiting for media state "
23601 "was interrupted\n");
23602 un->un_ncmds_in_driver++;
23603 rval = EINTR;
23604 goto done;
23606 SD_TRACE(SD_LOG_COMMON, un,
23607 "sd_check_media: received signal, state=%x\n",
23608 un->un_mediastate);
23611 * Inc the counter to indicate the device once again
23612 * has an active outstanding cmd.
23614 un->un_ncmds_in_driver++;
23617 /* invalidate geometry */
23618 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23619 sr_ejected(un);
23622 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23623 uint64_t capacity;
23624 uint_t lbasize;
23626 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23627 mutex_exit(SD_MUTEX(un));
23629 * Since the following routines use SD_PATH_DIRECT, we must
23630 * call PM directly before the upcoming disk accesses. This
23631 * may cause the disk to be power/spin up.
23634 if (sd_pm_entry(un) == DDI_SUCCESS) {
23635 rval = sd_send_scsi_READ_CAPACITY(ssc,
23636 &capacity, &lbasize, SD_PATH_DIRECT);
23637 if (rval != 0) {
23638 sd_pm_exit(un);
23639 if (rval == EIO)
23640 sd_ssc_assessment(ssc,
23641 SD_FMT_STATUS_CHECK);
23642 else
23643 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23644 mutex_enter(SD_MUTEX(un));
23645 goto done;
23647 } else {
23648 rval = EIO;
23649 mutex_enter(SD_MUTEX(un));
23650 goto done;
23652 mutex_enter(SD_MUTEX(un));
23654 sd_update_block_info(un, lbasize, capacity);
23657 * Check if the media in the device is writable or not
23659 if (ISCD(un)) {
23660 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23663 mutex_exit(SD_MUTEX(un));
23664 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23665 if ((cmlb_validate(un->un_cmlbhandle, 0,
23666 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23667 sd_set_pstats(un);
23668 SD_TRACE(SD_LOG_IO_PARTITION, un,
23669 "sd_check_media: un:0x%p pstats created and "
23670 "set\n", un);
23673 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23674 SD_PATH_DIRECT);
23676 sd_pm_exit(un);
23678 if (rval != 0) {
23679 if (rval == EIO)
23680 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23681 else
23682 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23685 mutex_enter(SD_MUTEX(un));
23687 done:
23688 sd_ssc_fini(ssc);
23689 un->un_f_watcht_stopped = FALSE;
23690 if (token != NULL && un->un_swr_token != NULL) {
23692 * Use of this local token and the mutex ensures that we avoid
23693 * some race conditions associated with terminating the
23694 * scsi watch.
23696 token = un->un_swr_token;
23697 mutex_exit(SD_MUTEX(un));
23698 (void) scsi_watch_request_terminate(token,
23699 SCSI_WATCH_TERMINATE_WAIT);
23700 if (scsi_watch_get_ref_count(token) == 0) {
23701 mutex_enter(SD_MUTEX(un));
23702 un->un_swr_token = (opaque_t)NULL;
23703 } else {
23704 mutex_enter(SD_MUTEX(un));
23709 * Update the capacity kstat value, if no media previously
23710 * (capacity kstat is 0) and a media has been inserted
23711 * (un_f_blockcount_is_valid == TRUE)
23713 if (un->un_errstats) {
23714 struct sd_errstats *stp = NULL;
23716 stp = (struct sd_errstats *)un->un_errstats->ks_data;
23717 if ((stp->sd_capacity.value.ui64 == 0) &&
23718 (un->un_f_blockcount_is_valid == TRUE)) {
23719 stp->sd_capacity.value.ui64 =
23720 (uint64_t)((uint64_t)un->un_blockcount *
23721 un->un_sys_blocksize);
23724 mutex_exit(SD_MUTEX(un));
23725 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23726 return (rval);
23731 * Function: sd_delayed_cv_broadcast
23733 * Description: Delayed cv_broadcast to allow for target to recover from media
23734 * insertion.
23736 * Arguments: arg - driver soft state (unit) structure
23739 static void
23740 sd_delayed_cv_broadcast(void *arg)
23742 struct sd_lun *un = arg;
23744 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23746 mutex_enter(SD_MUTEX(un));
23747 un->un_dcvb_timeid = NULL;
23748 cv_broadcast(&un->un_state_cv);
23749 mutex_exit(SD_MUTEX(un));
23754 * Function: sd_media_watch_cb
23756 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23757 * routine processes the TUR sense data and updates the driver
23758 * state if a transition has occurred. The user thread
23759 * (sd_check_media) is then signalled.
23761 * Arguments: arg - the device 'dev_t' is used for context to discriminate
23762 * among multiple watches that share this callback function
23763 * resultp - scsi watch facility result packet containing scsi
23764 * packet, status byte and sense data
23766 * Return Code: 0 for success, -1 for failure
23769 static int
23770 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23772 struct sd_lun *un;
23773 struct scsi_status *statusp = resultp->statusp;
23774 uint8_t *sensep = (uint8_t *)resultp->sensep;
23775 enum dkio_state state = DKIO_NONE;
23776 dev_t dev = (dev_t)arg;
23777 uchar_t actual_sense_length;
23778 uint8_t skey, asc, ascq;
23780 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23781 return (-1);
23783 actual_sense_length = resultp->actual_sense_length;
23785 mutex_enter(SD_MUTEX(un));
23786 SD_TRACE(SD_LOG_COMMON, un,
23787 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23788 *((char *)statusp), (void *)sensep, actual_sense_length);
23790 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23791 un->un_mediastate = DKIO_DEV_GONE;
23792 cv_broadcast(&un->un_state_cv);
23793 mutex_exit(SD_MUTEX(un));
23795 return (0);
23798 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23799 if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23800 if ((resultp->mmc_data[5] &
23801 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23802 state = DKIO_INSERTED;
23803 } else {
23804 state = DKIO_EJECTED;
23806 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23807 SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23808 sd_log_eject_request_event(un, KM_NOSLEEP);
23811 } else if (sensep != NULL) {
23813 * If there was a check condition then sensep points to valid
23814 * sense data. If status was not a check condition but a
23815 * reservation or busy status then the new state is DKIO_NONE.
23817 skey = scsi_sense_key(sensep);
23818 asc = scsi_sense_asc(sensep);
23819 ascq = scsi_sense_ascq(sensep);
23821 SD_INFO(SD_LOG_COMMON, un,
23822 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23823 skey, asc, ascq);
23824 /* This routine only uses up to 13 bytes of sense data. */
23825 if (actual_sense_length >= 13) {
23826 if (skey == KEY_UNIT_ATTENTION) {
23827 if (asc == 0x28) {
23828 state = DKIO_INSERTED;
23830 } else if (skey == KEY_NOT_READY) {
23832 * Sense data of 02/06/00 means that the
23833 * drive could not read the media (No
23834 * reference position found). In this case
23835 * to prevent a hang on the DKIOCSTATE IOCTL
23836 * we set the media state to DKIO_INSERTED.
23838 if (asc == 0x06 && ascq == 0x00)
23839 state = DKIO_INSERTED;
23842 * if 02/04/02 means that the host
23843 * should send start command. Explicitly
23844 * leave the media state as is
23845 * (inserted) as the media is inserted
23846 * and host has stopped device for PM
23847 * reasons. Upon next true read/write
23848 * to this media will bring the
23849 * device to the right state good for
23850 * media access.
23852 if (asc == 0x3a) {
23853 state = DKIO_EJECTED;
23854 } else {
23856 * If the drive is busy with an
23857 * operation or long write, keep the
23858 * media in an inserted state.
23861 if ((asc == 0x04) &&
23862 ((ascq == 0x02) ||
23863 (ascq == 0x07) ||
23864 (ascq == 0x08))) {
23865 state = DKIO_INSERTED;
23868 } else if (skey == KEY_NO_SENSE) {
23869 if ((asc == 0x00) && (ascq == 0x00)) {
23871 * Sense Data 00/00/00 does not provide
23872 * any information about the state of
23873 * the media. Ignore it.
23875 mutex_exit(SD_MUTEX(un));
23876 return (0);
23880 } else if ((*((char *)statusp) == STATUS_GOOD) &&
23881 (resultp->pkt->pkt_reason == CMD_CMPLT)) {
23882 state = DKIO_INSERTED;
23885 SD_TRACE(SD_LOG_COMMON, un,
23886 "sd_media_watch_cb: state=%x, specified=%x\n",
23887 state, un->un_specified_mediastate);
23890 * now signal the waiting thread if this is *not* the specified state;
23891 * delay the signal if the state is DKIO_INSERTED to allow the target
23892 * to recover
23894 if (state != un->un_specified_mediastate) {
23895 un->un_mediastate = state;
23896 if (state == DKIO_INSERTED) {
23898 * delay the signal to give the drive a chance
23899 * to do what it apparently needs to do
23901 SD_TRACE(SD_LOG_COMMON, un,
23902 "sd_media_watch_cb: delayed cv_broadcast\n");
23903 if (un->un_dcvb_timeid == NULL) {
23904 un->un_dcvb_timeid =
23905 timeout(sd_delayed_cv_broadcast, un,
23906 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
23908 } else {
23909 SD_TRACE(SD_LOG_COMMON, un,
23910 "sd_media_watch_cb: immediate cv_broadcast\n");
23911 cv_broadcast(&un->un_state_cv);
23914 mutex_exit(SD_MUTEX(un));
23915 return (0);
23920 * Function: sd_dkio_get_temp
23922 * Description: This routine is the driver entry point for handling ioctl
23923 * requests to get the disk temperature.
23925 * Arguments: dev - the device number
23926 * arg - pointer to user provided dk_temperature structure.
23927 * flag - this argument is a pass through to ddi_copyxxx()
23928 * directly from the mode argument of ioctl().
23930 * Return Code: 0
23931 * EFAULT
23932 * ENXIO
23933 * EAGAIN
23936 static int
23937 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
23939 struct sd_lun *un = NULL;
23940 struct dk_temperature *dktemp = NULL;
23941 uchar_t *temperature_page;
23942 int rval = 0;
23943 int path_flag = SD_PATH_STANDARD;
23944 sd_ssc_t *ssc;
23946 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23947 return (ENXIO);
23950 ssc = sd_ssc_init(un);
23951 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
23953 /* copyin the disk temp argument to get the user flags */
23954 if (ddi_copyin((void *)arg, dktemp,
23955 sizeof (struct dk_temperature), flag) != 0) {
23956 rval = EFAULT;
23957 goto done;
23960 /* Initialize the temperature to invalid. */
23961 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
23962 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
23965 * Note: Investigate removing the "bypass pm" semantic.
23966 * Can we just bypass PM always?
23968 if (dktemp->dkt_flags & DKT_BYPASS_PM) {
23969 path_flag = SD_PATH_DIRECT;
23970 ASSERT(!mutex_owned(&un->un_pm_mutex));
23971 mutex_enter(&un->un_pm_mutex);
23972 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
23974 * If DKT_BYPASS_PM is set, and the drive happens to be
23975 * in low power mode, we can not wake it up, Need to
23976 * return EAGAIN.
23978 mutex_exit(&un->un_pm_mutex);
23979 rval = EAGAIN;
23980 goto done;
23981 } else {
23983 * Indicate to PM the device is busy. This is required
23984 * to avoid a race - i.e. the ioctl is issuing a
23985 * command and the pm framework brings down the device
23986 * to low power mode (possible power cut-off on some
23987 * platforms).
23989 mutex_exit(&un->un_pm_mutex);
23990 if (sd_pm_entry(un) != DDI_SUCCESS) {
23991 rval = EAGAIN;
23992 goto done;
23997 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
23999 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24000 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24001 if (rval != 0)
24002 goto done2;
24005 * For the current temperature verify that the parameter length is 0x02
24006 * and the parameter code is 0x00
24008 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24009 (temperature_page[5] == 0x00)) {
24010 if (temperature_page[9] == 0xFF) {
24011 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24012 } else {
24013 dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24018 * For the reference temperature verify that the parameter
24019 * length is 0x02 and the parameter code is 0x01
24021 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24022 (temperature_page[11] == 0x01)) {
24023 if (temperature_page[15] == 0xFF) {
24024 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24025 } else {
24026 dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24030 /* Do the copyout regardless of the temperature commands status. */
24031 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24032 flag) != 0) {
24033 rval = EFAULT;
24034 goto done1;
24037 done2:
24038 if (rval != 0) {
24039 if (rval == EIO)
24040 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24041 else
24042 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24044 done1:
24045 if (path_flag == SD_PATH_DIRECT) {
24046 sd_pm_exit(un);
24049 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24050 done:
24051 sd_ssc_fini(ssc);
24052 if (dktemp != NULL) {
24053 kmem_free(dktemp, sizeof (struct dk_temperature));
24056 return (rval);
24061 * Function: sd_log_page_supported
24063 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24064 * supported log pages.
24066 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
24067 * structure for this target.
24068 * log_page -
24070 * Return Code: -1 - on error (log sense is optional and may not be supported).
24071 * 0 - log page not found.
24072 * 1 - log page found.
24075 static int
24076 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24078 uchar_t *log_page_data;
24079 int i;
24080 int match = 0;
24081 int log_size;
24082 int status = 0;
24083 struct sd_lun *un;
24085 ASSERT(ssc != NULL);
24086 un = ssc->ssc_un;
24087 ASSERT(un != NULL);
24089 log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24091 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24092 SD_PATH_DIRECT);
24094 if (status != 0) {
24095 if (status == EIO) {
24097 * Some disks do not support log sense, we
24098 * should ignore this kind of error(sense key is
24099 * 0x5 - illegal request).
24101 uint8_t *sensep;
24102 int senlen;
24104 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24105 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24106 ssc->ssc_uscsi_cmd->uscsi_rqresid);
24108 if (senlen > 0 &&
24109 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24110 sd_ssc_assessment(ssc,
24111 SD_FMT_IGNORE_COMPROMISE);
24112 } else {
24113 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24115 } else {
24116 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24119 SD_ERROR(SD_LOG_COMMON, un,
24120 "sd_log_page_supported: failed log page retrieval\n");
24121 kmem_free(log_page_data, 0xFF);
24122 return (-1);
24125 log_size = log_page_data[3];
24128 * The list of supported log pages start from the fourth byte. Check
24129 * until we run out of log pages or a match is found.
24131 for (i = 4; (i < (log_size + 4)) && !match; i++) {
24132 if (log_page_data[i] == log_page) {
24133 match++;
24136 kmem_free(log_page_data, 0xFF);
24137 return (match);
24142 * Function: sd_mhdioc_failfast
24144 * Description: This routine is the driver entry point for handling ioctl
24145 * requests to enable/disable the multihost failfast option.
24146 * (MHIOCENFAILFAST)
24148 * Arguments: dev - the device number
24149 * arg - user specified probing interval.
24150 * flag - this argument is a pass through to ddi_copyxxx()
24151 * directly from the mode argument of ioctl().
24153 * Return Code: 0
24154 * EFAULT
24155 * ENXIO
24158 static int
24159 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24161 struct sd_lun *un = NULL;
24162 int mh_time;
24163 int rval = 0;
24165 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24166 return (ENXIO);
24169 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24170 return (EFAULT);
24172 if (mh_time) {
24173 mutex_enter(SD_MUTEX(un));
24174 un->un_resvd_status |= SD_FAILFAST;
24175 mutex_exit(SD_MUTEX(un));
24177 * If mh_time is INT_MAX, then this ioctl is being used for
24178 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24180 if (mh_time != INT_MAX) {
24181 rval = sd_check_mhd(dev, mh_time);
24183 } else {
24184 (void) sd_check_mhd(dev, 0);
24185 mutex_enter(SD_MUTEX(un));
24186 un->un_resvd_status &= ~SD_FAILFAST;
24187 mutex_exit(SD_MUTEX(un));
24189 return (rval);
24194 * Function: sd_mhdioc_takeown
24196 * Description: This routine is the driver entry point for handling ioctl
24197 * requests to forcefully acquire exclusive access rights to the
24198 * multihost disk (MHIOCTKOWN).
24200 * Arguments: dev - the device number
24201 * arg - user provided structure specifying the delay
24202 * parameters in milliseconds
24203 * flag - this argument is a pass through to ddi_copyxxx()
24204 * directly from the mode argument of ioctl().
24206 * Return Code: 0
24207 * EFAULT
24208 * ENXIO
24211 static int
24212 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24214 struct sd_lun *un = NULL;
24215 struct mhioctkown *tkown = NULL;
24216 int rval = 0;
24218 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24219 return (ENXIO);
24222 if (arg != NULL) {
24223 tkown = (struct mhioctkown *)
24224 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24225 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24226 if (rval != 0) {
24227 rval = EFAULT;
24228 goto error;
24232 rval = sd_take_ownership(dev, tkown);
24233 mutex_enter(SD_MUTEX(un));
24234 if (rval == 0) {
24235 un->un_resvd_status |= SD_RESERVE;
24236 if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24237 sd_reinstate_resv_delay =
24238 tkown->reinstate_resv_delay * 1000;
24239 } else {
24240 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24243 * Give the scsi_watch routine interval set by
24244 * the MHIOCENFAILFAST ioctl precedence here.
24246 if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24247 mutex_exit(SD_MUTEX(un));
24248 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24249 SD_TRACE(SD_LOG_IOCTL_MHD, un,
24250 "sd_mhdioc_takeown : %d\n",
24251 sd_reinstate_resv_delay);
24252 } else {
24253 mutex_exit(SD_MUTEX(un));
24255 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24256 sd_mhd_reset_notify_cb, (caddr_t)un);
24257 } else {
24258 un->un_resvd_status &= ~SD_RESERVE;
24259 mutex_exit(SD_MUTEX(un));
24262 error:
24263 if (tkown != NULL) {
24264 kmem_free(tkown, sizeof (struct mhioctkown));
24266 return (rval);
24271 * Function: sd_mhdioc_release
24273 * Description: This routine is the driver entry point for handling ioctl
24274 * requests to release exclusive access rights to the multihost
24275 * disk (MHIOCRELEASE).
24277 * Arguments: dev - the device number
24279 * Return Code: 0
24280 * ENXIO
24283 static int
24284 sd_mhdioc_release(dev_t dev)
24286 struct sd_lun *un = NULL;
24287 timeout_id_t resvd_timeid_save;
24288 int resvd_status_save;
24289 int rval = 0;
24291 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24292 return (ENXIO);
24295 mutex_enter(SD_MUTEX(un));
24296 resvd_status_save = un->un_resvd_status;
24297 un->un_resvd_status &=
24298 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24299 if (un->un_resvd_timeid) {
24300 resvd_timeid_save = un->un_resvd_timeid;
24301 un->un_resvd_timeid = NULL;
24302 mutex_exit(SD_MUTEX(un));
24303 (void) untimeout(resvd_timeid_save);
24304 } else {
24305 mutex_exit(SD_MUTEX(un));
24309 * destroy any pending timeout thread that may be attempting to
24310 * reinstate reservation on this device.
24312 sd_rmv_resv_reclaim_req(dev);
24314 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24315 mutex_enter(SD_MUTEX(un));
24316 if ((un->un_mhd_token) &&
24317 ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24318 mutex_exit(SD_MUTEX(un));
24319 (void) sd_check_mhd(dev, 0);
24320 } else {
24321 mutex_exit(SD_MUTEX(un));
24323 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24324 sd_mhd_reset_notify_cb, (caddr_t)un);
24325 } else {
24327 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24329 mutex_enter(SD_MUTEX(un));
24330 un->un_resvd_status = resvd_status_save;
24331 mutex_exit(SD_MUTEX(un));
24333 return (rval);
24338 * Function: sd_mhdioc_register_devid
24340 * Description: This routine is the driver entry point for handling ioctl
24341 * requests to register the device id (MHIOCREREGISTERDEVID).
24343 * Note: The implementation for this ioctl has been updated to
24344 * be consistent with the original PSARC case (1999/357)
24345 * (4375899, 4241671, 4220005)
24347 * Arguments: dev - the device number
24349 * Return Code: 0
24350 * ENXIO
24353 static int
24354 sd_mhdioc_register_devid(dev_t dev)
24356 struct sd_lun *un = NULL;
24357 int rval = 0;
24358 sd_ssc_t *ssc;
24360 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24361 return (ENXIO);
24364 ASSERT(!mutex_owned(SD_MUTEX(un)));
24366 mutex_enter(SD_MUTEX(un));
24368 /* If a devid already exists, de-register it */
24369 if (un->un_devid != NULL) {
24370 ddi_devid_unregister(SD_DEVINFO(un));
24372 * After unregister devid, needs to free devid memory
24374 ddi_devid_free(un->un_devid);
24375 un->un_devid = NULL;
24378 /* Check for reservation conflict */
24379 mutex_exit(SD_MUTEX(un));
24380 ssc = sd_ssc_init(un);
24381 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24382 mutex_enter(SD_MUTEX(un));
24384 switch (rval) {
24385 case 0:
24386 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24387 break;
24388 case EACCES:
24389 break;
24390 default:
24391 rval = EIO;
24394 mutex_exit(SD_MUTEX(un));
24395 if (rval != 0) {
24396 if (rval == EIO)
24397 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24398 else
24399 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24401 sd_ssc_fini(ssc);
24402 return (rval);
24407 * Function: sd_mhdioc_inkeys
24409 * Description: This routine is the driver entry point for handling ioctl
24410 * requests to issue the SCSI-3 Persistent In Read Keys command
24411 * to the device (MHIOCGRP_INKEYS).
24413 * Arguments: dev - the device number
24414 * arg - user provided in_keys structure
24415 * flag - this argument is a pass through to ddi_copyxxx()
24416 * directly from the mode argument of ioctl().
24418 * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24419 * ENXIO
24420 * EFAULT
24423 static int
24424 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24426 struct sd_lun *un;
24427 mhioc_inkeys_t inkeys;
24428 int rval = 0;
24430 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24431 return (ENXIO);
24434 #ifdef _MULTI_DATAMODEL
24435 switch (ddi_model_convert_from(flag & FMODELS)) {
24436 case DDI_MODEL_ILP32: {
24437 struct mhioc_inkeys32 inkeys32;
24439 if (ddi_copyin(arg, &inkeys32,
24440 sizeof (struct mhioc_inkeys32), flag) != 0) {
24441 return (EFAULT);
24443 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24444 if ((rval = sd_persistent_reservation_in_read_keys(un,
24445 &inkeys, flag)) != 0) {
24446 return (rval);
24448 inkeys32.generation = inkeys.generation;
24449 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24450 flag) != 0) {
24451 return (EFAULT);
24453 break;
24455 case DDI_MODEL_NONE:
24456 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24457 flag) != 0) {
24458 return (EFAULT);
24460 if ((rval = sd_persistent_reservation_in_read_keys(un,
24461 &inkeys, flag)) != 0) {
24462 return (rval);
24464 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24465 flag) != 0) {
24466 return (EFAULT);
24468 break;
24471 #else /* ! _MULTI_DATAMODEL */
24473 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24474 return (EFAULT);
24476 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24477 if (rval != 0) {
24478 return (rval);
24480 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24481 return (EFAULT);
24484 #endif /* _MULTI_DATAMODEL */
24486 return (rval);
24491 * Function: sd_mhdioc_inresv
24493 * Description: This routine is the driver entry point for handling ioctl
24494 * requests to issue the SCSI-3 Persistent In Read Reservations
24495 * command to the device (MHIOCGRP_INKEYS).
24497 * Arguments: dev - the device number
24498 * arg - user provided in_resv structure
24499 * flag - this argument is a pass through to ddi_copyxxx()
24500 * directly from the mode argument of ioctl().
24502 * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24503 * ENXIO
24504 * EFAULT
24507 static int
24508 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24510 struct sd_lun *un;
24511 mhioc_inresvs_t inresvs;
24512 int rval = 0;
24514 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24515 return (ENXIO);
24518 #ifdef _MULTI_DATAMODEL
24520 switch (ddi_model_convert_from(flag & FMODELS)) {
24521 case DDI_MODEL_ILP32: {
24522 struct mhioc_inresvs32 inresvs32;
24524 if (ddi_copyin(arg, &inresvs32,
24525 sizeof (struct mhioc_inresvs32), flag) != 0) {
24526 return (EFAULT);
24528 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24529 if ((rval = sd_persistent_reservation_in_read_resv(un,
24530 &inresvs, flag)) != 0) {
24531 return (rval);
24533 inresvs32.generation = inresvs.generation;
24534 if (ddi_copyout(&inresvs32, arg,
24535 sizeof (struct mhioc_inresvs32), flag) != 0) {
24536 return (EFAULT);
24538 break;
24540 case DDI_MODEL_NONE:
24541 if (ddi_copyin(arg, &inresvs,
24542 sizeof (mhioc_inresvs_t), flag) != 0) {
24543 return (EFAULT);
24545 if ((rval = sd_persistent_reservation_in_read_resv(un,
24546 &inresvs, flag)) != 0) {
24547 return (rval);
24549 if (ddi_copyout(&inresvs, arg,
24550 sizeof (mhioc_inresvs_t), flag) != 0) {
24551 return (EFAULT);
24553 break;
24556 #else /* ! _MULTI_DATAMODEL */
24558 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24559 return (EFAULT);
24561 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24562 if (rval != 0) {
24563 return (rval);
24565 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24566 return (EFAULT);
24569 #endif /* ! _MULTI_DATAMODEL */
24571 return (rval);
24576 * The following routines support the clustering functionality described below
24577 * and implement lost reservation reclaim functionality.
24579 * Clustering
24580 * ----------
24581 * The clustering code uses two different, independent forms of SCSI
24582 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24583 * Persistent Group Reservations. For any particular disk, it will use either
24584 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24586 * SCSI-2
24587 * The cluster software takes ownership of a multi-hosted disk by issuing the
24588 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24589 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a
24590 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24591 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the
24592 * driver. The meaning of failfast is that if the driver (on this host) ever
24593 * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24594 * it should immediately panic the host. The motivation for this ioctl is that
24595 * if this host does encounter reservation conflict, the underlying cause is
24596 * that some other host of the cluster has decided that this host is no longer
24597 * in the cluster and has seized control of the disks for itself. Since this
24598 * host is no longer in the cluster, it ought to panic itself. The
24599 * MHIOCENFAILFAST ioctl does two things:
24600 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24601 * error to panic the host
24602 * (b) it sets up a periodic timer to test whether this host still has
24603 * "access" (in that no other host has reserved the device): if the
24604 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24605 * purpose of that periodic timer is to handle scenarios where the host is
24606 * otherwise temporarily quiescent, temporarily doing no real i/o.
24607 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24608 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for
24609 * the device itself.
24611 * SCSI-3 PGR
24612 * A direct semantic implementation of the SCSI-3 Persistent Reservation
24613 * facility is supported through the shared multihost disk ioctls
24614 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24615 * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24617 * Reservation Reclaim:
24618 * --------------------
24619 * To support the lost reservation reclaim operations this driver creates a
24620 * single thread to handle reinstating reservations on all devices that have
24621 * lost reservations sd_resv_reclaim_requests are logged for all devices that
24622 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24623 * and the reservation reclaim thread loops through the requests to regain the
24624 * lost reservations.
24628 * Function: sd_check_mhd()
24630 * Description: This function sets up and submits a scsi watch request or
24631 * terminates an existing watch request. This routine is used in
24632 * support of reservation reclaim.
24634 * Arguments: dev - the device 'dev_t' is used for context to discriminate
24635 * among multiple watches that share the callback function
24636 * interval - the number of microseconds specifying the watch
24637 * interval for issuing TEST UNIT READY commands. If
24638 * set to 0 the watch should be terminated. If the
24639 * interval is set to 0 and if the device is required
24640 * to hold reservation while disabling failfast, the
24641 * watch is restarted with an interval of
24642 * reinstate_resv_delay.
24644 * Return Code: 0 - Successful submit/terminate of scsi watch request
24645 * ENXIO - Indicates an invalid device was specified
24646 * EAGAIN - Unable to submit the scsi watch request
24649 static int
24650 sd_check_mhd(dev_t dev, int interval)
24652 struct sd_lun *un;
24653 opaque_t token;
24655 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24656 return (ENXIO);
24659 /* is this a watch termination request? */
24660 if (interval == 0) {
24661 mutex_enter(SD_MUTEX(un));
24662 /* if there is an existing watch task then terminate it */
24663 if (un->un_mhd_token) {
24664 token = un->un_mhd_token;
24665 un->un_mhd_token = NULL;
24666 mutex_exit(SD_MUTEX(un));
24667 (void) scsi_watch_request_terminate(token,
24668 SCSI_WATCH_TERMINATE_ALL_WAIT);
24669 mutex_enter(SD_MUTEX(un));
24670 } else {
24671 mutex_exit(SD_MUTEX(un));
24673 * Note: If we return here we don't check for the
24674 * failfast case. This is the original legacy
24675 * implementation but perhaps we should be checking
24676 * the failfast case.
24678 return (0);
24681 * If the device is required to hold reservation while
24682 * disabling failfast, we need to restart the scsi_watch
24683 * routine with an interval of reinstate_resv_delay.
24685 if (un->un_resvd_status & SD_RESERVE) {
24686 interval = sd_reinstate_resv_delay/1000;
24687 } else {
24688 /* no failfast so bail */
24689 mutex_exit(SD_MUTEX(un));
24690 return (0);
24692 mutex_exit(SD_MUTEX(un));
24696 * adjust minimum time interval to 1 second,
24697 * and convert from msecs to usecs
24699 if (interval > 0 && interval < 1000) {
24700 interval = 1000;
24702 interval *= 1000;
24705 * submit the request to the scsi_watch service
24707 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24708 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24709 if (token == NULL) {
24710 return (EAGAIN);
24714 * save token for termination later on
24716 mutex_enter(SD_MUTEX(un));
24717 un->un_mhd_token = token;
24718 mutex_exit(SD_MUTEX(un));
24719 return (0);
24724 * Function: sd_mhd_watch_cb()
24726 * Description: This function is the call back function used by the scsi watch
24727 * facility. The scsi watch facility sends the "Test Unit Ready"
24728 * and processes the status. If applicable (i.e. a "Unit Attention"
24729 * status and automatic "Request Sense" not used) the scsi watch
24730 * facility will send a "Request Sense" and retrieve the sense data
24731 * to be passed to this callback function. In either case the
24732 * automatic "Request Sense" or the facility submitting one, this
24733 * callback is passed the status and sense data.
24735 * Arguments: arg - the device 'dev_t' is used for context to discriminate
24736 * among multiple watches that share this callback function
24737 * resultp - scsi watch facility result packet containing scsi
24738 * packet, status byte and sense data
24740 * Return Code: 0 - continue the watch task
24741 * non-zero - terminate the watch task
24744 static int
24745 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24747 struct sd_lun *un;
24748 struct scsi_status *statusp;
24749 uint8_t *sensep;
24750 struct scsi_pkt *pkt;
24751 uchar_t actual_sense_length;
24752 dev_t dev = (dev_t)arg;
24754 ASSERT(resultp != NULL);
24755 statusp = resultp->statusp;
24756 sensep = (uint8_t *)resultp->sensep;
24757 pkt = resultp->pkt;
24758 actual_sense_length = resultp->actual_sense_length;
24760 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24761 return (ENXIO);
24764 SD_TRACE(SD_LOG_IOCTL_MHD, un,
24765 "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24766 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24768 /* Begin processing of the status and/or sense data */
24769 if (pkt->pkt_reason != CMD_CMPLT) {
24770 /* Handle the incomplete packet */
24771 sd_mhd_watch_incomplete(un, pkt);
24772 return (0);
24773 } else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24774 if (*((unsigned char *)statusp)
24775 == STATUS_RESERVATION_CONFLICT) {
24777 * Handle a reservation conflict by panicking if
24778 * configured for failfast or by logging the conflict
24779 * and updating the reservation status
24781 mutex_enter(SD_MUTEX(un));
24782 if ((un->un_resvd_status & SD_FAILFAST) &&
24783 (sd_failfast_enable)) {
24784 sd_panic_for_res_conflict(un);
24785 /*NOTREACHED*/
24787 SD_INFO(SD_LOG_IOCTL_MHD, un,
24788 "sd_mhd_watch_cb: Reservation Conflict\n");
24789 un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24790 mutex_exit(SD_MUTEX(un));
24794 if (sensep != NULL) {
24795 if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24796 mutex_enter(SD_MUTEX(un));
24797 if ((scsi_sense_asc(sensep) ==
24798 SD_SCSI_RESET_SENSE_CODE) &&
24799 (un->un_resvd_status & SD_RESERVE)) {
24801 * The additional sense code indicates a power
24802 * on or bus device reset has occurred; update
24803 * the reservation status.
24805 un->un_resvd_status |=
24806 (SD_LOST_RESERVE | SD_WANT_RESERVE);
24807 SD_INFO(SD_LOG_IOCTL_MHD, un,
24808 "sd_mhd_watch_cb: Lost Reservation\n");
24810 } else {
24811 return (0);
24813 } else {
24814 mutex_enter(SD_MUTEX(un));
24817 if ((un->un_resvd_status & SD_RESERVE) &&
24818 (un->un_resvd_status & SD_LOST_RESERVE)) {
24819 if (un->un_resvd_status & SD_WANT_RESERVE) {
24821 * A reset occurred in between the last probe and this
24822 * one so if a timeout is pending cancel it.
24824 if (un->un_resvd_timeid) {
24825 timeout_id_t temp_id = un->un_resvd_timeid;
24826 un->un_resvd_timeid = NULL;
24827 mutex_exit(SD_MUTEX(un));
24828 (void) untimeout(temp_id);
24829 mutex_enter(SD_MUTEX(un));
24831 un->un_resvd_status &= ~SD_WANT_RESERVE;
24833 if (un->un_resvd_timeid == 0) {
24834 /* Schedule a timeout to handle the lost reservation */
24835 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
24836 (void *)dev,
24837 drv_usectohz(sd_reinstate_resv_delay));
24840 mutex_exit(SD_MUTEX(un));
24841 return (0);
24846 * Function: sd_mhd_watch_incomplete()
24848 * Description: This function is used to find out why a scsi pkt sent by the
24849 * scsi watch facility was not completed. Under some scenarios this
24850 * routine will return. Otherwise it will send a bus reset to see
24851 * if the drive is still online.
24853 * Arguments: un - driver soft state (unit) structure
24854 * pkt - incomplete scsi pkt
24857 static void
24858 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
24860 int be_chatty;
24861 int perr;
24863 ASSERT(pkt != NULL);
24864 ASSERT(un != NULL);
24865 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT));
24866 perr = (pkt->pkt_statistics & STAT_PERR);
24868 mutex_enter(SD_MUTEX(un));
24869 if (un->un_state == SD_STATE_DUMPING) {
24870 mutex_exit(SD_MUTEX(un));
24871 return;
24874 switch (pkt->pkt_reason) {
24875 case CMD_UNX_BUS_FREE:
24877 * If we had a parity error that caused the target to drop BSY*,
24878 * don't be chatty about it.
24880 if (perr && be_chatty) {
24881 be_chatty = 0;
24883 break;
24884 case CMD_TAG_REJECT:
24886 * The SCSI-2 spec states that a tag reject will be sent by the
24887 * target if tagged queuing is not supported. A tag reject may
24888 * also be sent during certain initialization periods or to
24889 * control internal resources. For the latter case the target
24890 * may also return Queue Full.
24892 * If this driver receives a tag reject from a target that is
24893 * going through an init period or controlling internal
24894 * resources tagged queuing will be disabled. This is a less
24895 * than optimal behavior but the driver is unable to determine
24896 * the target state and assumes tagged queueing is not supported
24898 pkt->pkt_flags = 0;
24899 un->un_tagflags = 0;
24901 if (un->un_f_opt_queueing == TRUE) {
24902 un->un_throttle = min(un->un_throttle, 3);
24903 } else {
24904 un->un_throttle = 1;
24906 mutex_exit(SD_MUTEX(un));
24907 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
24908 mutex_enter(SD_MUTEX(un));
24909 break;
24910 case CMD_INCOMPLETE:
24912 * The transport stopped with an abnormal state, fallthrough and
24913 * reset the target and/or bus unless selection did not complete
24914 * (indicated by STATE_GOT_BUS) in which case we don't want to
24915 * go through a target/bus reset
24917 if (pkt->pkt_state == STATE_GOT_BUS) {
24918 break;
24920 /*FALLTHROUGH*/
24922 case CMD_TIMEOUT:
24923 default:
24925 * The lun may still be running the command, so a lun reset
24926 * should be attempted. If the lun reset fails or cannot be
24927 * issued, than try a target reset. Lastly try a bus reset.
24929 if ((pkt->pkt_statistics &
24930 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
24931 int reset_retval = 0;
24932 mutex_exit(SD_MUTEX(un));
24933 if (un->un_f_allow_bus_device_reset == TRUE) {
24934 if (un->un_f_lun_reset_enabled == TRUE) {
24935 reset_retval =
24936 scsi_reset(SD_ADDRESS(un),
24937 RESET_LUN);
24939 if (reset_retval == 0) {
24940 reset_retval =
24941 scsi_reset(SD_ADDRESS(un),
24942 RESET_TARGET);
24945 if (reset_retval == 0) {
24946 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
24948 mutex_enter(SD_MUTEX(un));
24950 break;
24953 /* A device/bus reset has occurred; update the reservation status. */
24954 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
24955 (STAT_BUS_RESET | STAT_DEV_RESET))) {
24956 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
24957 un->un_resvd_status |=
24958 (SD_LOST_RESERVE | SD_WANT_RESERVE);
24959 SD_INFO(SD_LOG_IOCTL_MHD, un,
24960 "sd_mhd_watch_incomplete: Lost Reservation\n");
24965 * The disk has been turned off; Update the device state.
24967 * Note: Should we be offlining the disk here?
24969 if (pkt->pkt_state == STATE_GOT_BUS) {
24970 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
24971 "Disk not responding to selection\n");
24972 if (un->un_state != SD_STATE_OFFLINE) {
24973 New_state(un, SD_STATE_OFFLINE);
24975 } else if (be_chatty) {
24977 * suppress messages if they are all the same pkt reason;
24978 * with TQ, many (up to 256) are returned with the same
24979 * pkt_reason
24981 if (pkt->pkt_reason != un->un_last_pkt_reason) {
24982 SD_ERROR(SD_LOG_IOCTL_MHD, un,
24983 "sd_mhd_watch_incomplete: "
24984 "SCSI transport failed: reason '%s'\n",
24985 scsi_rname(pkt->pkt_reason));
24988 un->un_last_pkt_reason = pkt->pkt_reason;
24989 mutex_exit(SD_MUTEX(un));
24994 * Function: sd_sname()
24996 * Description: This is a simple little routine to return a string containing
24997 * a printable description of command status byte for use in
24998 * logging.
25000 * Arguments: status - pointer to a status byte
25002 * Return Code: char * - string containing status description.
25005 static char *
25006 sd_sname(uchar_t status)
25008 switch (status & STATUS_MASK) {
25009 case STATUS_GOOD:
25010 return ("good status");
25011 case STATUS_CHECK:
25012 return ("check condition");
25013 case STATUS_MET:
25014 return ("condition met");
25015 case STATUS_BUSY:
25016 return ("busy");
25017 case STATUS_INTERMEDIATE:
25018 return ("intermediate");
25019 case STATUS_INTERMEDIATE_MET:
25020 return ("intermediate - condition met");
25021 case STATUS_RESERVATION_CONFLICT:
25022 return ("reservation_conflict");
25023 case STATUS_TERMINATED:
25024 return ("command terminated");
25025 case STATUS_QFULL:
25026 return ("queue full");
25027 default:
25028 return ("<unknown status>");
25034 * Function: sd_mhd_resvd_recover()
25036 * Description: This function adds a reservation entry to the
25037 * sd_resv_reclaim_request list and signals the reservation
25038 * reclaim thread that there is work pending. If the reservation
25039 * reclaim thread has not been previously created this function
25040 * will kick it off.
25042 * Arguments: arg - the device 'dev_t' is used for context to discriminate
25043 * among multiple watches that share this callback function
25045 * Context: This routine is called by timeout() and is run in interrupt
25046 * context. It must not sleep or call other functions which may
25047 * sleep.
25050 static void
25051 sd_mhd_resvd_recover(void *arg)
25053 dev_t dev = (dev_t)arg;
25054 struct sd_lun *un;
25055 struct sd_thr_request *sd_treq = NULL;
25056 struct sd_thr_request *sd_cur = NULL;
25057 struct sd_thr_request *sd_prev = NULL;
25058 int already_there = 0;
25060 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25061 return;
25064 mutex_enter(SD_MUTEX(un));
25065 un->un_resvd_timeid = NULL;
25066 if (un->un_resvd_status & SD_WANT_RESERVE) {
25068 * There was a reset so don't issue the reserve, allow the
25069 * sd_mhd_watch_cb callback function to notice this and
25070 * reschedule the timeout for reservation.
25072 mutex_exit(SD_MUTEX(un));
25073 return;
25075 mutex_exit(SD_MUTEX(un));
25078 * Add this device to the sd_resv_reclaim_request list and the
25079 * sd_resv_reclaim_thread should take care of the rest.
25081 * Note: We can't sleep in this context so if the memory allocation
25082 * fails allow the sd_mhd_watch_cb callback function to notice this and
25083 * reschedule the timeout for reservation. (4378460)
25085 sd_treq = (struct sd_thr_request *)
25086 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25087 if (sd_treq == NULL) {
25088 return;
25091 sd_treq->sd_thr_req_next = NULL;
25092 sd_treq->dev = dev;
25093 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25094 if (sd_tr.srq_thr_req_head == NULL) {
25095 sd_tr.srq_thr_req_head = sd_treq;
25096 } else {
25097 sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25098 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25099 if (sd_cur->dev == dev) {
25101 * already in Queue so don't log
25102 * another request for the device
25104 already_there = 1;
25105 break;
25107 sd_prev = sd_cur;
25109 if (!already_there) {
25110 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25111 "logging request for %lx\n", dev);
25112 sd_prev->sd_thr_req_next = sd_treq;
25113 } else {
25114 kmem_free(sd_treq, sizeof (struct sd_thr_request));
25119 * Create a kernel thread to do the reservation reclaim and free up this
25120 * thread. We cannot block this thread while we go away to do the
25121 * reservation reclaim
25123 if (sd_tr.srq_resv_reclaim_thread == NULL)
25124 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25125 sd_resv_reclaim_thread, NULL,
25126 0, &p0, TS_RUN, v.v_maxsyspri - 2);
25128 /* Tell the reservation reclaim thread that it has work to do */
25129 cv_signal(&sd_tr.srq_resv_reclaim_cv);
25130 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25134 * Function: sd_resv_reclaim_thread()
25136 * Description: This function implements the reservation reclaim operations
25138 * Arguments: arg - the device 'dev_t' is used for context to discriminate
25139 * among multiple watches that share this callback function
25142 static void
25143 sd_resv_reclaim_thread()
25145 struct sd_lun *un;
25146 struct sd_thr_request *sd_mhreq;
25148 /* Wait for work */
25149 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25150 if (sd_tr.srq_thr_req_head == NULL) {
25151 cv_wait(&sd_tr.srq_resv_reclaim_cv,
25152 &sd_tr.srq_resv_reclaim_mutex);
25155 /* Loop while we have work */
25156 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25157 un = ddi_get_soft_state(sd_state,
25158 SDUNIT(sd_tr.srq_thr_cur_req->dev));
25159 if (un == NULL) {
25161 * softstate structure is NULL so just
25162 * dequeue the request and continue
25164 sd_tr.srq_thr_req_head =
25165 sd_tr.srq_thr_cur_req->sd_thr_req_next;
25166 kmem_free(sd_tr.srq_thr_cur_req,
25167 sizeof (struct sd_thr_request));
25168 continue;
25171 /* dequeue the request */
25172 sd_mhreq = sd_tr.srq_thr_cur_req;
25173 sd_tr.srq_thr_req_head =
25174 sd_tr.srq_thr_cur_req->sd_thr_req_next;
25175 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25178 * Reclaim reservation only if SD_RESERVE is still set. There
25179 * may have been a call to MHIOCRELEASE before we got here.
25181 mutex_enter(SD_MUTEX(un));
25182 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25184 * Note: The SD_LOST_RESERVE flag is cleared before
25185 * reclaiming the reservation. If this is done after the
25186 * call to sd_reserve_release a reservation loss in the
25187 * window between pkt completion of reserve cmd and
25188 * mutex_enter below may not be recognized
25190 un->un_resvd_status &= ~SD_LOST_RESERVE;
25191 mutex_exit(SD_MUTEX(un));
25193 if (sd_reserve_release(sd_mhreq->dev,
25194 SD_RESERVE) == 0) {
25195 mutex_enter(SD_MUTEX(un));
25196 un->un_resvd_status |= SD_RESERVE;
25197 mutex_exit(SD_MUTEX(un));
25198 SD_INFO(SD_LOG_IOCTL_MHD, un,
25199 "sd_resv_reclaim_thread: "
25200 "Reservation Recovered\n");
25201 } else {
25202 mutex_enter(SD_MUTEX(un));
25203 un->un_resvd_status |= SD_LOST_RESERVE;
25204 mutex_exit(SD_MUTEX(un));
25205 SD_INFO(SD_LOG_IOCTL_MHD, un,
25206 "sd_resv_reclaim_thread: Failed "
25207 "Reservation Recovery\n");
25209 } else {
25210 mutex_exit(SD_MUTEX(un));
25212 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25213 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25214 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25215 sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25217 * wakeup the destroy thread if anyone is waiting on
25218 * us to complete.
25220 cv_signal(&sd_tr.srq_inprocess_cv);
25221 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25222 "sd_resv_reclaim_thread: cv_signalling current request \n");
25226 * cleanup the sd_tr structure now that this thread will not exist
25228 ASSERT(sd_tr.srq_thr_req_head == NULL);
25229 ASSERT(sd_tr.srq_thr_cur_req == NULL);
25230 sd_tr.srq_resv_reclaim_thread = NULL;
25231 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25232 thread_exit();
25237 * Function: sd_rmv_resv_reclaim_req()
25239 * Description: This function removes any pending reservation reclaim requests
25240 * for the specified device.
25242 * Arguments: dev - the device 'dev_t'
25245 static void
25246 sd_rmv_resv_reclaim_req(dev_t dev)
25248 struct sd_thr_request *sd_mhreq;
25249 struct sd_thr_request *sd_prev;
25251 /* Remove a reservation reclaim request from the list */
25252 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25253 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25255 * We are attempting to reinstate reservation for
25256 * this device. We wait for sd_reserve_release()
25257 * to return before we return.
25259 cv_wait(&sd_tr.srq_inprocess_cv,
25260 &sd_tr.srq_resv_reclaim_mutex);
25261 } else {
25262 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25263 if (sd_mhreq && sd_mhreq->dev == dev) {
25264 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25265 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25266 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25267 return;
25269 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25270 if (sd_mhreq && sd_mhreq->dev == dev) {
25271 break;
25273 sd_prev = sd_mhreq;
25275 if (sd_mhreq != NULL) {
25276 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25277 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25280 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25285 * Function: sd_mhd_reset_notify_cb()
25287 * Description: This is a call back function for scsi_reset_notify. This
25288 * function updates the softstate reserved status and logs the
25289 * reset. The driver scsi watch facility callback function
25290 * (sd_mhd_watch_cb) and reservation reclaim thread functionality
25291 * will reclaim the reservation.
25293 * Arguments: arg - driver soft state (unit) structure
25296 static void
25297 sd_mhd_reset_notify_cb(caddr_t arg)
25299 struct sd_lun *un = (struct sd_lun *)arg;
25301 mutex_enter(SD_MUTEX(un));
25302 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25303 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25304 SD_INFO(SD_LOG_IOCTL_MHD, un,
25305 "sd_mhd_reset_notify_cb: Lost Reservation\n");
25307 mutex_exit(SD_MUTEX(un));
25312 * Function: sd_take_ownership()
25314 * Description: This routine implements an algorithm to achieve a stable
25315 * reservation on disks which don't implement priority reserve,
25316 * and makes sure that other host lose re-reservation attempts.
25317 * This algorithm contains of a loop that keeps issuing the RESERVE
25318 * for some period of time (min_ownership_delay, default 6 seconds)
25319 * During that loop, it looks to see if there has been a bus device
25320 * reset or bus reset (both of which cause an existing reservation
25321 * to be lost). If the reservation is lost issue RESERVE until a
25322 * period of min_ownership_delay with no resets has gone by, or
25323 * until max_ownership_delay has expired. This loop ensures that
25324 * the host really did manage to reserve the device, in spite of
25325 * resets. The looping for min_ownership_delay (default six
25326 * seconds) is important to early generation clustering products,
25327 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25328 * MHIOCENFAILFAST periodic timer of two seconds. By having
25329 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25330 * MHIOCENFAILFAST poll every two seconds, the idea is that by the
25331 * time the MHIOCTKOWN ioctl returns, the other host (if any) will
25332 * have already noticed, via the MHIOCENFAILFAST polling, that it
25333 * no longer "owns" the disk and will have panicked itself. Thus,
25334 * the host issuing the MHIOCTKOWN is assured (with timing
25335 * dependencies) that by the time it actually starts to use the
25336 * disk for real work, the old owner is no longer accessing it.
25338 * min_ownership_delay is the minimum amount of time for which the
25339 * disk must be reserved continuously devoid of resets before the
25340 * MHIOCTKOWN ioctl will return success.
25342 * max_ownership_delay indicates the amount of time by which the
25343 * take ownership should succeed or timeout with an error.
25345 * Arguments: dev - the device 'dev_t'
25346 * *p - struct containing timing info.
25348 * Return Code: 0 for success or error code
25351 static int
25352 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25354 struct sd_lun *un;
25355 int rval;
25356 int err;
25357 int reservation_count = 0;
25358 int min_ownership_delay = 6000000; /* in usec */
25359 int max_ownership_delay = 30000000; /* in usec */
25360 clock_t start_time; /* starting time of this algorithm */
25361 clock_t end_time; /* time limit for giving up */
25362 clock_t ownership_time; /* time limit for stable ownership */
25363 clock_t current_time;
25364 clock_t previous_current_time;
25366 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25367 return (ENXIO);
25371 * Attempt a device reservation. A priority reservation is requested.
25373 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25374 != SD_SUCCESS) {
25375 SD_ERROR(SD_LOG_IOCTL_MHD, un,
25376 "sd_take_ownership: return(1)=%d\n", rval);
25377 return (rval);
25380 /* Update the softstate reserved status to indicate the reservation */
25381 mutex_enter(SD_MUTEX(un));
25382 un->un_resvd_status |= SD_RESERVE;
25383 un->un_resvd_status &=
25384 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25385 mutex_exit(SD_MUTEX(un));
25387 if (p != NULL) {
25388 if (p->min_ownership_delay != 0) {
25389 min_ownership_delay = p->min_ownership_delay * 1000;
25391 if (p->max_ownership_delay != 0) {
25392 max_ownership_delay = p->max_ownership_delay * 1000;
25395 SD_INFO(SD_LOG_IOCTL_MHD, un,
25396 "sd_take_ownership: min, max delays: %d, %d\n",
25397 min_ownership_delay, max_ownership_delay);
25399 start_time = ddi_get_lbolt();
25400 current_time = start_time;
25401 ownership_time = current_time + drv_usectohz(min_ownership_delay);
25402 end_time = start_time + drv_usectohz(max_ownership_delay);
25404 while (current_time - end_time < 0) {
25405 delay(drv_usectohz(500000));
25407 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25408 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25409 mutex_enter(SD_MUTEX(un));
25410 rval = (un->un_resvd_status &
25411 SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25412 mutex_exit(SD_MUTEX(un));
25413 break;
25416 previous_current_time = current_time;
25417 current_time = ddi_get_lbolt();
25418 mutex_enter(SD_MUTEX(un));
25419 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25420 ownership_time = ddi_get_lbolt() +
25421 drv_usectohz(min_ownership_delay);
25422 reservation_count = 0;
25423 } else {
25424 reservation_count++;
25426 un->un_resvd_status |= SD_RESERVE;
25427 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25428 mutex_exit(SD_MUTEX(un));
25430 SD_INFO(SD_LOG_IOCTL_MHD, un,
25431 "sd_take_ownership: ticks for loop iteration=%ld, "
25432 "reservation=%s\n", (current_time - previous_current_time),
25433 reservation_count ? "ok" : "reclaimed");
25435 if (current_time - ownership_time >= 0 &&
25436 reservation_count >= 4) {
25437 rval = 0; /* Achieved a stable ownership */
25438 break;
25440 if (current_time - end_time >= 0) {
25441 rval = EACCES; /* No ownership in max possible time */
25442 break;
25445 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25446 "sd_take_ownership: return(2)=%d\n", rval);
25447 return (rval);
25452 * Function: sd_reserve_release()
25454 * Description: This function builds and sends scsi RESERVE, RELEASE, and
25455 * PRIORITY RESERVE commands based on a user specified command type
25457 * Arguments: dev - the device 'dev_t'
25458 * cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25459 * SD_RESERVE, SD_RELEASE
25461 * Return Code: 0 or Error Code
25464 static int
25465 sd_reserve_release(dev_t dev, int cmd)
25467 struct uscsi_cmd *com = NULL;
25468 struct sd_lun *un = NULL;
25469 char cdb[CDB_GROUP0];
25470 int rval;
25472 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25473 (cmd == SD_PRIORITY_RESERVE));
25475 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25476 return (ENXIO);
25479 /* instantiate and initialize the command and cdb */
25480 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25481 bzero(cdb, CDB_GROUP0);
25482 com->uscsi_flags = USCSI_SILENT;
25483 com->uscsi_timeout = un->un_reserve_release_time;
25484 com->uscsi_cdblen = CDB_GROUP0;
25485 com->uscsi_cdb = cdb;
25486 if (cmd == SD_RELEASE) {
25487 cdb[0] = SCMD_RELEASE;
25488 } else {
25489 cdb[0] = SCMD_RESERVE;
25492 /* Send the command. */
25493 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25494 SD_PATH_STANDARD);
25497 * "break" a reservation that is held by another host, by issuing a
25498 * reset if priority reserve is desired, and we could not get the
25499 * device.
25501 if ((cmd == SD_PRIORITY_RESERVE) &&
25502 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25504 * First try to reset the LUN. If we cannot, then try a target
25505 * reset, followed by a bus reset if the target reset fails.
25507 int reset_retval = 0;
25508 if (un->un_f_lun_reset_enabled == TRUE) {
25509 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25511 if (reset_retval == 0) {
25512 /* The LUN reset either failed or was not issued */
25513 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25515 if ((reset_retval == 0) &&
25516 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25517 rval = EIO;
25518 kmem_free(com, sizeof (*com));
25519 return (rval);
25522 bzero(com, sizeof (struct uscsi_cmd));
25523 com->uscsi_flags = USCSI_SILENT;
25524 com->uscsi_cdb = cdb;
25525 com->uscsi_cdblen = CDB_GROUP0;
25526 com->uscsi_timeout = 5;
25529 * Reissue the last reserve command, this time without request
25530 * sense. Assume that it is just a regular reserve command.
25532 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25533 SD_PATH_STANDARD);
25536 /* Return an error if still getting a reservation conflict. */
25537 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25538 rval = EACCES;
25541 kmem_free(com, sizeof (*com));
25542 return (rval);
25546 #define SD_NDUMP_RETRIES 12
25548 * System Crash Dump routine
25551 static int
25552 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25554 int instance;
25555 int partition;
25556 int i;
25557 int err;
25558 struct sd_lun *un;
25559 struct scsi_pkt *wr_pktp;
25560 struct buf *wr_bp;
25561 struct buf wr_buf;
25562 daddr_t tgt_byte_offset; /* rmw - byte offset for target */
25563 daddr_t tgt_blkno; /* rmw - blkno for target */
25564 size_t tgt_byte_count; /* rmw - # of bytes to xfer */
25565 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */
25566 size_t io_start_offset;
25567 int doing_rmw = FALSE;
25568 int rval;
25569 ssize_t dma_resid;
25570 daddr_t oblkno;
25571 diskaddr_t nblks = 0;
25572 diskaddr_t start_block;
25574 instance = SDUNIT(dev);
25575 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25576 !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25577 return (ENXIO);
25580 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25582 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25584 partition = SDPART(dev);
25585 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25587 if (!(NOT_DEVBSIZE(un))) {
25588 int secmask = 0;
25589 int blknomask = 0;
25591 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25592 secmask = un->un_tgt_blocksize - 1;
25594 if (blkno & blknomask) {
25595 SD_TRACE(SD_LOG_DUMP, un,
25596 "sddump: dump start block not modulo %d\n",
25597 un->un_tgt_blocksize);
25598 return (EINVAL);
25601 if ((nblk * DEV_BSIZE) & secmask) {
25602 SD_TRACE(SD_LOG_DUMP, un,
25603 "sddump: dump length not modulo %d\n",
25604 un->un_tgt_blocksize);
25605 return (EINVAL);
25610 /* Validate blocks to dump at against partition size. */
25612 (void) cmlb_partinfo(un->un_cmlbhandle, partition,
25613 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25615 if (NOT_DEVBSIZE(un)) {
25616 if ((blkno + nblk) > nblks) {
25617 SD_TRACE(SD_LOG_DUMP, un,
25618 "sddump: dump range larger than partition: "
25619 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25620 blkno, nblk, nblks);
25621 return (EINVAL);
25623 } else {
25624 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25625 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25626 SD_TRACE(SD_LOG_DUMP, un,
25627 "sddump: dump range larger than partition: "
25628 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25629 blkno, nblk, nblks);
25630 return (EINVAL);
25634 mutex_enter(&un->un_pm_mutex);
25635 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25636 struct scsi_pkt *start_pktp;
25638 mutex_exit(&un->un_pm_mutex);
25641 * use pm framework to power on HBA 1st
25643 (void) pm_raise_power(SD_DEVINFO(un), 0,
25644 SD_PM_STATE_ACTIVE(un));
25647 * Dump no long uses sdpower to power on a device, it's
25648 * in-line here so it can be done in polled mode.
25651 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25653 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25654 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25656 if (start_pktp == NULL) {
25657 /* We were not given a SCSI packet, fail. */
25658 return (EIO);
25660 bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25661 start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25662 start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25663 start_pktp->pkt_flags = FLAG_NOINTR;
25665 mutex_enter(SD_MUTEX(un));
25666 SD_FILL_SCSI1_LUN(un, start_pktp);
25667 mutex_exit(SD_MUTEX(un));
25669 * Scsi_poll returns 0 (success) if the command completes and
25670 * the status block is STATUS_GOOD.
25672 if (sd_scsi_poll(un, start_pktp) != 0) {
25673 scsi_destroy_pkt(start_pktp);
25674 return (EIO);
25676 scsi_destroy_pkt(start_pktp);
25677 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25678 SD_PM_STATE_CHANGE);
25679 } else {
25680 mutex_exit(&un->un_pm_mutex);
25683 mutex_enter(SD_MUTEX(un));
25684 un->un_throttle = 0;
25687 * The first time through, reset the specific target device.
25688 * However, when cpr calls sddump we know that sd is in a
25689 * a good state so no bus reset is required.
25690 * Clear sense data via Request Sense cmd.
25691 * In sddump we don't care about allow_bus_device_reset anymore
25694 if ((un->un_state != SD_STATE_SUSPENDED) &&
25695 (un->un_state != SD_STATE_DUMPING)) {
25697 New_state(un, SD_STATE_DUMPING);
25699 if (un->un_f_is_fibre == FALSE) {
25700 mutex_exit(SD_MUTEX(un));
25702 * Attempt a bus reset for parallel scsi.
25704 * Note: A bus reset is required because on some host
25705 * systems (i.e. E420R) a bus device reset is
25706 * insufficient to reset the state of the target.
25708 * Note: Don't issue the reset for fibre-channel,
25709 * because this tends to hang the bus (loop) for
25710 * too long while everyone is logging out and in
25711 * and the deadman timer for dumping will fire
25712 * before the dump is complete.
25714 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25715 mutex_enter(SD_MUTEX(un));
25716 Restore_state(un);
25717 mutex_exit(SD_MUTEX(un));
25718 return (EIO);
25721 /* Delay to give the device some recovery time. */
25722 drv_usecwait(10000);
25724 if (sd_send_polled_RQS(un) == SD_FAILURE) {
25725 SD_INFO(SD_LOG_DUMP, un,
25726 "sddump: sd_send_polled_RQS failed\n");
25728 mutex_enter(SD_MUTEX(un));
25733 * Convert the partition-relative block number to a
25734 * disk physical block number.
25736 if (NOT_DEVBSIZE(un)) {
25737 blkno += start_block;
25738 } else {
25739 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25740 blkno += start_block;
25743 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25747 * Check if the device has a non-512 block size.
25749 wr_bp = NULL;
25750 if (NOT_DEVBSIZE(un)) {
25751 tgt_byte_offset = blkno * un->un_sys_blocksize;
25752 tgt_byte_count = nblk * un->un_sys_blocksize;
25753 if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25754 (tgt_byte_count % un->un_tgt_blocksize)) {
25755 doing_rmw = TRUE;
25757 * Calculate the block number and number of block
25758 * in terms of the media block size.
25760 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25761 tgt_nblk =
25762 ((tgt_byte_offset + tgt_byte_count +
25763 (un->un_tgt_blocksize - 1)) /
25764 un->un_tgt_blocksize) - tgt_blkno;
25767 * Invoke the routine which is going to do read part
25768 * of read-modify-write.
25769 * Note that this routine returns a pointer to
25770 * a valid bp in wr_bp.
25772 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25773 &wr_bp);
25774 if (err) {
25775 mutex_exit(SD_MUTEX(un));
25776 return (err);
25779 * Offset is being calculated as -
25780 * (original block # * system block size) -
25781 * (new block # * target block size)
25783 io_start_offset =
25784 ((uint64_t)(blkno * un->un_sys_blocksize)) -
25785 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25787 ASSERT(io_start_offset < un->un_tgt_blocksize);
25789 * Do the modify portion of read modify write.
25791 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25792 (size_t)nblk * un->un_sys_blocksize);
25793 } else {
25794 doing_rmw = FALSE;
25795 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25796 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25799 /* Convert blkno and nblk to target blocks */
25800 blkno = tgt_blkno;
25801 nblk = tgt_nblk;
25802 } else {
25803 wr_bp = &wr_buf;
25804 bzero(wr_bp, sizeof (struct buf));
25805 wr_bp->b_flags = B_BUSY;
25806 wr_bp->b_un.b_addr = addr;
25807 wr_bp->b_bcount = nblk << DEV_BSHIFT;
25808 wr_bp->b_resid = 0;
25811 mutex_exit(SD_MUTEX(un));
25814 * Obtain a SCSI packet for the write command.
25815 * It should be safe to call the allocator here without
25816 * worrying about being locked for DVMA mapping because
25817 * the address we're passed is already a DVMA mapping
25819 * We are also not going to worry about semaphore ownership
25820 * in the dump buffer. Dumping is single threaded at present.
25823 wr_pktp = NULL;
25825 dma_resid = wr_bp->b_bcount;
25826 oblkno = blkno;
25828 if (!(NOT_DEVBSIZE(un))) {
25829 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
25832 while (dma_resid != 0) {
25834 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25835 wr_bp->b_flags &= ~B_ERROR;
25837 if (un->un_partial_dma_supported == 1) {
25838 blkno = oblkno +
25839 ((wr_bp->b_bcount - dma_resid) /
25840 un->un_tgt_blocksize);
25841 nblk = dma_resid / un->un_tgt_blocksize;
25843 if (wr_pktp) {
25845 * Partial DMA transfers after initial transfer
25847 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
25848 blkno, nblk);
25849 } else {
25850 /* Initial transfer */
25851 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25852 un->un_pkt_flags, NULL_FUNC, NULL,
25853 blkno, nblk);
25855 } else {
25856 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25857 0, NULL_FUNC, NULL, blkno, nblk);
25860 if (rval == 0) {
25861 /* We were given a SCSI packet, continue. */
25862 break;
25865 if (i == 0) {
25866 if (wr_bp->b_flags & B_ERROR) {
25867 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25868 "no resources for dumping; "
25869 "error code: 0x%x, retrying",
25870 geterror(wr_bp));
25871 } else {
25872 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25873 "no resources for dumping; retrying");
25875 } else if (i != (SD_NDUMP_RETRIES - 1)) {
25876 if (wr_bp->b_flags & B_ERROR) {
25877 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25878 "no resources for dumping; error code: "
25879 "0x%x, retrying\n", geterror(wr_bp));
25881 } else {
25882 if (wr_bp->b_flags & B_ERROR) {
25883 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25884 "no resources for dumping; "
25885 "error code: 0x%x, retries failed, "
25886 "giving up.\n", geterror(wr_bp));
25887 } else {
25888 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25889 "no resources for dumping; "
25890 "retries failed, giving up.\n");
25892 mutex_enter(SD_MUTEX(un));
25893 Restore_state(un);
25894 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
25895 mutex_exit(SD_MUTEX(un));
25896 scsi_free_consistent_buf(wr_bp);
25897 } else {
25898 mutex_exit(SD_MUTEX(un));
25900 return (EIO);
25902 drv_usecwait(10000);
25905 if (un->un_partial_dma_supported == 1) {
25907 * save the resid from PARTIAL_DMA
25909 dma_resid = wr_pktp->pkt_resid;
25910 if (dma_resid != 0)
25911 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
25912 wr_pktp->pkt_resid = 0;
25913 } else {
25914 dma_resid = 0;
25917 /* SunBug 1222170 */
25918 wr_pktp->pkt_flags = FLAG_NOINTR;
25920 err = EIO;
25921 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25924 * Scsi_poll returns 0 (success) if the command completes and
25925 * the status block is STATUS_GOOD. We should only check
25926 * errors if this condition is not true. Even then we should
25927 * send our own request sense packet only if we have a check
25928 * condition and auto request sense has not been performed by
25929 * the hba.
25931 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
25933 if ((sd_scsi_poll(un, wr_pktp) == 0) &&
25934 (wr_pktp->pkt_resid == 0)) {
25935 err = SD_SUCCESS;
25936 break;
25940 * Check CMD_DEV_GONE 1st, give up if device is gone.
25942 if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
25943 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25944 "Error while dumping state...Device is gone\n");
25945 break;
25948 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
25949 SD_INFO(SD_LOG_DUMP, un,
25950 "sddump: write failed with CHECK, try # %d\n", i);
25951 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
25952 (void) sd_send_polled_RQS(un);
25955 continue;
25958 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
25959 int reset_retval = 0;
25961 SD_INFO(SD_LOG_DUMP, un,
25962 "sddump: write failed with BUSY, try # %d\n", i);
25964 if (un->un_f_lun_reset_enabled == TRUE) {
25965 reset_retval = scsi_reset(SD_ADDRESS(un),
25966 RESET_LUN);
25968 if (reset_retval == 0) {
25969 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25971 (void) sd_send_polled_RQS(un);
25973 } else {
25974 SD_INFO(SD_LOG_DUMP, un,
25975 "sddump: write failed with 0x%x, try # %d\n",
25976 SD_GET_PKT_STATUS(wr_pktp), i);
25977 mutex_enter(SD_MUTEX(un));
25978 sd_reset_target(un, wr_pktp);
25979 mutex_exit(SD_MUTEX(un));
25983 * If we are not getting anywhere with lun/target resets,
25984 * let's reset the bus.
25986 if (i == SD_NDUMP_RETRIES/2) {
25987 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25988 (void) sd_send_polled_RQS(un);
25993 scsi_destroy_pkt(wr_pktp);
25994 mutex_enter(SD_MUTEX(un));
25995 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
25996 mutex_exit(SD_MUTEX(un));
25997 scsi_free_consistent_buf(wr_bp);
25998 } else {
25999 mutex_exit(SD_MUTEX(un));
26001 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26002 return (err);
26006 * Function: sd_scsi_poll()
26008 * Description: This is a wrapper for the scsi_poll call.
26010 * Arguments: sd_lun - The unit structure
26011 * scsi_pkt - The scsi packet being sent to the device.
26013 * Return Code: 0 - Command completed successfully with good status
26014 * -1 - Command failed. This could indicate a check condition
26015 * or other status value requiring recovery action.
26017 * NOTE: This code is only called off sddump().
26020 static int
26021 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26023 int status;
26025 ASSERT(un != NULL);
26026 ASSERT(!mutex_owned(SD_MUTEX(un)));
26027 ASSERT(pktp != NULL);
26029 status = SD_SUCCESS;
26031 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26032 pktp->pkt_flags |= un->un_tagflags;
26033 pktp->pkt_flags &= ~FLAG_NODISCON;
26036 status = sd_ddi_scsi_poll(pktp);
26038 * Scsi_poll returns 0 (success) if the command completes and the
26039 * status block is STATUS_GOOD. We should only check errors if this
26040 * condition is not true. Even then we should send our own request
26041 * sense packet only if we have a check condition and auto
26042 * request sense has not been performed by the hba.
26043 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26045 if ((status != SD_SUCCESS) &&
26046 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26047 (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26048 (pktp->pkt_reason != CMD_DEV_GONE))
26049 (void) sd_send_polled_RQS(un);
26051 return (status);
26055 * Function: sd_send_polled_RQS()
26057 * Description: This sends the request sense command to a device.
26059 * Arguments: sd_lun - The unit structure
26061 * Return Code: 0 - Command completed successfully with good status
26062 * -1 - Command failed.
26066 static int
26067 sd_send_polled_RQS(struct sd_lun *un)
26069 int ret_val;
26070 struct scsi_pkt *rqs_pktp;
26071 struct buf *rqs_bp;
26073 ASSERT(un != NULL);
26074 ASSERT(!mutex_owned(SD_MUTEX(un)));
26076 ret_val = SD_SUCCESS;
26078 rqs_pktp = un->un_rqs_pktp;
26079 rqs_bp = un->un_rqs_bp;
26081 mutex_enter(SD_MUTEX(un));
26083 if (un->un_sense_isbusy) {
26084 ret_val = SD_FAILURE;
26085 mutex_exit(SD_MUTEX(un));
26086 return (ret_val);
26090 * If the request sense buffer (and packet) is not in use,
26091 * let's set the un_sense_isbusy and send our packet
26093 un->un_sense_isbusy = 1;
26094 rqs_pktp->pkt_resid = 0;
26095 rqs_pktp->pkt_reason = 0;
26096 rqs_pktp->pkt_flags |= FLAG_NOINTR;
26097 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26099 mutex_exit(SD_MUTEX(un));
26101 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26102 " 0x%p\n", rqs_bp->b_un.b_addr);
26105 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26106 * axle - it has a call into us!
26108 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26109 SD_INFO(SD_LOG_COMMON, un,
26110 "sd_send_polled_RQS: RQS failed\n");
26113 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26114 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26116 mutex_enter(SD_MUTEX(un));
26117 un->un_sense_isbusy = 0;
26118 mutex_exit(SD_MUTEX(un));
26120 return (ret_val);
26124 * Defines needed for localized version of the scsi_poll routine.
26126 #define CSEC 10000 /* usecs */
26127 #define SEC_TO_CSEC (1000000/CSEC)
26130 * Function: sd_ddi_scsi_poll()
26132 * Description: Localized version of the scsi_poll routine. The purpose is to
26133 * send a scsi_pkt to a device as a polled command. This version
26134 * is to ensure more robust handling of transport errors.
26135 * Specifically this routine cures not ready, coming ready
26136 * transition for power up and reset of sonoma's. This can take
26137 * up to 45 seconds for power-on and 20 seconds for reset of a
26138 * sonoma lun.
26140 * Arguments: scsi_pkt - The scsi_pkt being sent to a device
26142 * Return Code: 0 - Command completed successfully with good status
26143 * -1 - Command failed.
26145 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26146 * be fixed (removing this code), we need to determine how to handle the
26147 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26149 * NOTE: This code is only called off sddump().
26151 static int
26152 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26154 int rval = -1;
26155 int savef;
26156 long savet;
26157 void (*savec)();
26158 int timeout;
26159 int busy_count;
26160 int poll_delay;
26161 int rc;
26162 uint8_t *sensep;
26163 struct scsi_arq_status *arqstat;
26164 extern int do_polled_io;
26166 ASSERT(pkt->pkt_scbp);
26169 * save old flags..
26171 savef = pkt->pkt_flags;
26172 savec = pkt->pkt_comp;
26173 savet = pkt->pkt_time;
26175 pkt->pkt_flags |= FLAG_NOINTR;
26178 * XXX there is nothing in the SCSA spec that states that we should not
26179 * do a callback for polled cmds; however, removing this will break sd
26180 * and probably other target drivers
26182 pkt->pkt_comp = NULL;
26185 * we don't like a polled command without timeout.
26186 * 60 seconds seems long enough.
26188 if (pkt->pkt_time == 0)
26189 pkt->pkt_time = SCSI_POLL_TIMEOUT;
26192 * Send polled cmd.
26194 * We do some error recovery for various errors. Tran_busy,
26195 * queue full, and non-dispatched commands are retried every 10 msec.
26196 * as they are typically transient failures. Busy status and Not
26197 * Ready are retried every second as this status takes a while to
26198 * change.
26200 timeout = pkt->pkt_time * SEC_TO_CSEC;
26202 for (busy_count = 0; busy_count < timeout; busy_count++) {
26204 * Initialize pkt status variables.
26206 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26208 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26209 if (rc != TRAN_BUSY) {
26210 /* Transport failed - give up. */
26211 break;
26212 } else {
26213 /* Transport busy - try again. */
26214 poll_delay = 1 * CSEC; /* 10 msec. */
26216 } else {
26218 * Transport accepted - check pkt status.
26220 rc = (*pkt->pkt_scbp) & STATUS_MASK;
26221 if ((pkt->pkt_reason == CMD_CMPLT) &&
26222 (rc == STATUS_CHECK) &&
26223 (pkt->pkt_state & STATE_ARQ_DONE)) {
26224 arqstat =
26225 (struct scsi_arq_status *)(pkt->pkt_scbp);
26226 sensep = (uint8_t *)&arqstat->sts_sensedata;
26227 } else {
26228 sensep = NULL;
26231 if ((pkt->pkt_reason == CMD_CMPLT) &&
26232 (rc == STATUS_GOOD)) {
26233 /* No error - we're done */
26234 rval = 0;
26235 break;
26237 } else if (pkt->pkt_reason == CMD_DEV_GONE) {
26238 /* Lost connection - give up */
26239 break;
26241 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26242 (pkt->pkt_state == 0)) {
26243 /* Pkt not dispatched - try again. */
26244 poll_delay = 1 * CSEC; /* 10 msec. */
26246 } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26247 (rc == STATUS_QFULL)) {
26248 /* Queue full - try again. */
26249 poll_delay = 1 * CSEC; /* 10 msec. */
26251 } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26252 (rc == STATUS_BUSY)) {
26253 /* Busy - try again. */
26254 poll_delay = 100 * CSEC; /* 1 sec. */
26255 busy_count += (SEC_TO_CSEC - 1);
26257 } else if ((sensep != NULL) &&
26258 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26260 * Unit Attention - try again.
26261 * Pretend it took 1 sec.
26262 * NOTE: 'continue' avoids poll_delay
26264 busy_count += (SEC_TO_CSEC - 1);
26265 continue;
26267 } else if ((sensep != NULL) &&
26268 (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26269 (scsi_sense_asc(sensep) == 0x04) &&
26270 (scsi_sense_ascq(sensep) == 0x01)) {
26272 * Not ready -> ready - try again.
26273 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26274 * ...same as STATUS_BUSY
26276 poll_delay = 100 * CSEC; /* 1 sec. */
26277 busy_count += (SEC_TO_CSEC - 1);
26279 } else {
26280 /* BAD status - give up. */
26281 break;
26285 if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26286 !do_polled_io) {
26287 delay(drv_usectohz(poll_delay));
26288 } else {
26289 /* we busy wait during cpr_dump or interrupt threads */
26290 drv_usecwait(poll_delay);
26294 pkt->pkt_flags = savef;
26295 pkt->pkt_comp = savec;
26296 pkt->pkt_time = savet;
26298 /* return on error */
26299 if (rval)
26300 return (rval);
26303 * This is not a performance critical code path.
26305 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26306 * issues associated with looking at DMA memory prior to
26307 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26309 scsi_sync_pkt(pkt);
26310 return (0);
26316 * Function: sd_persistent_reservation_in_read_keys
26318 * Description: This routine is the driver entry point for handling CD-ROM
26319 * multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26320 * by sending the SCSI-3 PRIN commands to the device.
26321 * Processes the read keys command response by copying the
26322 * reservation key information into the user provided buffer.
26323 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26325 * Arguments: un - Pointer to soft state struct for the target.
26326 * usrp - user provided pointer to multihost Persistent In Read
26327 * Keys structure (mhioc_inkeys_t)
26328 * flag - this argument is a pass through to ddi_copyxxx()
26329 * directly from the mode argument of ioctl().
26331 * Return Code: 0 - Success
26332 * EACCES
26333 * ENOTSUP
26334 * errno return code from sd_send_scsi_cmd()
26336 * Context: Can sleep. Does not return until command is completed.
26339 static int
26340 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26341 mhioc_inkeys_t *usrp, int flag)
26343 #ifdef _MULTI_DATAMODEL
26344 struct mhioc_key_list32 li32;
26345 #endif
26346 sd_prin_readkeys_t *in;
26347 mhioc_inkeys_t *ptr;
26348 mhioc_key_list_t li;
26349 uchar_t *data_bufp = NULL;
26350 int data_len = 0;
26351 int rval = 0;
26352 size_t copysz = 0;
26353 sd_ssc_t *ssc;
26355 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26356 return (EINVAL);
26358 bzero(&li, sizeof (mhioc_key_list_t));
26360 ssc = sd_ssc_init(un);
26363 * Get the listsize from user
26365 #ifdef _MULTI_DATAMODEL
26366 switch (ddi_model_convert_from(flag & FMODELS)) {
26367 case DDI_MODEL_ILP32:
26368 copysz = sizeof (struct mhioc_key_list32);
26369 if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26370 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26371 "sd_persistent_reservation_in_read_keys: "
26372 "failed ddi_copyin: mhioc_key_list32_t\n");
26373 rval = EFAULT;
26374 goto done;
26376 li.listsize = li32.listsize;
26377 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26378 break;
26380 case DDI_MODEL_NONE:
26381 copysz = sizeof (mhioc_key_list_t);
26382 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26383 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26384 "sd_persistent_reservation_in_read_keys: "
26385 "failed ddi_copyin: mhioc_key_list_t\n");
26386 rval = EFAULT;
26387 goto done;
26389 break;
26392 #else /* ! _MULTI_DATAMODEL */
26393 copysz = sizeof (mhioc_key_list_t);
26394 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26395 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26396 "sd_persistent_reservation_in_read_keys: "
26397 "failed ddi_copyin: mhioc_key_list_t\n");
26398 rval = EFAULT;
26399 goto done;
26401 #endif
26403 data_len = li.listsize * MHIOC_RESV_KEY_SIZE;
26404 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26405 data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26407 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26408 data_len, data_bufp);
26409 if (rval != 0) {
26410 if (rval == EIO)
26411 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26412 else
26413 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26414 goto done;
26416 in = (sd_prin_readkeys_t *)data_bufp;
26417 ptr->generation = BE_32(in->generation);
26418 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26421 * Return the min(listsize, listlen) keys
26423 #ifdef _MULTI_DATAMODEL
26425 switch (ddi_model_convert_from(flag & FMODELS)) {
26426 case DDI_MODEL_ILP32:
26427 li32.listlen = li.listlen;
26428 if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26429 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26430 "sd_persistent_reservation_in_read_keys: "
26431 "failed ddi_copyout: mhioc_key_list32_t\n");
26432 rval = EFAULT;
26433 goto done;
26435 break;
26437 case DDI_MODEL_NONE:
26438 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26439 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26440 "sd_persistent_reservation_in_read_keys: "
26441 "failed ddi_copyout: mhioc_key_list_t\n");
26442 rval = EFAULT;
26443 goto done;
26445 break;
26448 #else /* ! _MULTI_DATAMODEL */
26450 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26451 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26452 "sd_persistent_reservation_in_read_keys: "
26453 "failed ddi_copyout: mhioc_key_list_t\n");
26454 rval = EFAULT;
26455 goto done;
26458 #endif /* _MULTI_DATAMODEL */
26460 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26461 li.listsize * MHIOC_RESV_KEY_SIZE);
26462 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26463 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26464 "sd_persistent_reservation_in_read_keys: "
26465 "failed ddi_copyout: keylist\n");
26466 rval = EFAULT;
26468 done:
26469 sd_ssc_fini(ssc);
26470 kmem_free(data_bufp, data_len);
26471 return (rval);
26476 * Function: sd_persistent_reservation_in_read_resv
26478 * Description: This routine is the driver entry point for handling CD-ROM
26479 * multi-host persistent reservation requests (MHIOCGRP_INRESV)
26480 * by sending the SCSI-3 PRIN commands to the device.
26481 * Process the read persistent reservations command response by
26482 * copying the reservation information into the user provided
26483 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26485 * Arguments: un - Pointer to soft state struct for the target.
26486 * usrp - user provided pointer to multihost Persistent In Read
26487 * Keys structure (mhioc_inkeys_t)
26488 * flag - this argument is a pass through to ddi_copyxxx()
26489 * directly from the mode argument of ioctl().
26491 * Return Code: 0 - Success
26492 * EACCES
26493 * ENOTSUP
26494 * errno return code from sd_send_scsi_cmd()
26496 * Context: Can sleep. Does not return until command is completed.
26499 static int
26500 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26501 mhioc_inresvs_t *usrp, int flag)
26503 #ifdef _MULTI_DATAMODEL
26504 struct mhioc_resv_desc_list32 resvlist32;
26505 #endif
26506 sd_prin_readresv_t *in;
26507 mhioc_inresvs_t *ptr;
26508 sd_readresv_desc_t *readresv_ptr;
26509 mhioc_resv_desc_list_t resvlist;
26510 mhioc_resv_desc_t resvdesc;
26511 uchar_t *data_bufp = NULL;
26512 int data_len;
26513 int rval = 0;
26514 int i;
26515 size_t copysz = 0;
26516 mhioc_resv_desc_t *bufp;
26517 sd_ssc_t *ssc;
26519 if ((ptr = usrp) == NULL) {
26520 return (EINVAL);
26523 ssc = sd_ssc_init(un);
26526 * Get the listsize from user
26528 #ifdef _MULTI_DATAMODEL
26529 switch (ddi_model_convert_from(flag & FMODELS)) {
26530 case DDI_MODEL_ILP32:
26531 copysz = sizeof (struct mhioc_resv_desc_list32);
26532 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26533 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26534 "sd_persistent_reservation_in_read_resv: "
26535 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26536 rval = EFAULT;
26537 goto done;
26539 resvlist.listsize = resvlist32.listsize;
26540 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26541 break;
26543 case DDI_MODEL_NONE:
26544 copysz = sizeof (mhioc_resv_desc_list_t);
26545 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26546 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26547 "sd_persistent_reservation_in_read_resv: "
26548 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26549 rval = EFAULT;
26550 goto done;
26552 break;
26554 #else /* ! _MULTI_DATAMODEL */
26555 copysz = sizeof (mhioc_resv_desc_list_t);
26556 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26557 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26558 "sd_persistent_reservation_in_read_resv: "
26559 "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26560 rval = EFAULT;
26561 goto done;
26563 #endif /* ! _MULTI_DATAMODEL */
26565 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26566 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26567 data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26569 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26570 data_len, data_bufp);
26571 if (rval != 0) {
26572 if (rval == EIO)
26573 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26574 else
26575 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26576 goto done;
26578 in = (sd_prin_readresv_t *)data_bufp;
26579 ptr->generation = BE_32(in->generation);
26580 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26583 * Return the min(listsize, listlen( keys
26585 #ifdef _MULTI_DATAMODEL
26587 switch (ddi_model_convert_from(flag & FMODELS)) {
26588 case DDI_MODEL_ILP32:
26589 resvlist32.listlen = resvlist.listlen;
26590 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26591 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26592 "sd_persistent_reservation_in_read_resv: "
26593 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26594 rval = EFAULT;
26595 goto done;
26597 break;
26599 case DDI_MODEL_NONE:
26600 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26601 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26602 "sd_persistent_reservation_in_read_resv: "
26603 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26604 rval = EFAULT;
26605 goto done;
26607 break;
26610 #else /* ! _MULTI_DATAMODEL */
26612 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26613 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26614 "sd_persistent_reservation_in_read_resv: "
26615 "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26616 rval = EFAULT;
26617 goto done;
26620 #endif /* ! _MULTI_DATAMODEL */
26622 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26623 bufp = resvlist.list;
26624 copysz = sizeof (mhioc_resv_desc_t);
26625 for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26626 i++, readresv_ptr++, bufp++) {
26628 bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26629 MHIOC_RESV_KEY_SIZE);
26630 resvdesc.type = readresv_ptr->type;
26631 resvdesc.scope = readresv_ptr->scope;
26632 resvdesc.scope_specific_addr =
26633 BE_32(readresv_ptr->scope_specific_addr);
26635 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26636 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26637 "sd_persistent_reservation_in_read_resv: "
26638 "failed ddi_copyout: resvlist\n");
26639 rval = EFAULT;
26640 goto done;
26643 done:
26644 sd_ssc_fini(ssc);
26645 /* only if data_bufp is allocated, we need to free it */
26646 if (data_bufp) {
26647 kmem_free(data_bufp, data_len);
26649 return (rval);
26654 * Function: sr_change_blkmode()
26656 * Description: This routine is the driver entry point for handling CD-ROM
26657 * block mode ioctl requests. Support for returning and changing
26658 * the current block size in use by the device is implemented. The
26659 * LBA size is changed via a MODE SELECT Block Descriptor.
26661 * This routine issues a mode sense with an allocation length of
26662 * 12 bytes for the mode page header and a single block descriptor.
26664 * Arguments: dev - the device 'dev_t'
26665 * cmd - the request type; one of CDROMGBLKMODE (get) or
26666 * CDROMSBLKMODE (set)
26667 * data - current block size or requested block size
26668 * flag - this argument is a pass through to ddi_copyxxx() directly
26669 * from the mode argument of ioctl().
26671 * Return Code: the code returned by sd_send_scsi_cmd()
26672 * EINVAL if invalid arguments are provided
26673 * EFAULT if ddi_copyxxx() fails
26674 * ENXIO if fail ddi_get_soft_state
26675 * EIO if invalid mode sense block descriptor length
26679 static int
26680 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26682 struct sd_lun *un = NULL;
26683 struct mode_header *sense_mhp, *select_mhp;
26684 struct block_descriptor *sense_desc, *select_desc;
26685 int current_bsize;
26686 int rval = EINVAL;
26687 uchar_t *sense = NULL;
26688 uchar_t *select = NULL;
26689 sd_ssc_t *ssc;
26691 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26693 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26694 return (ENXIO);
26698 * The block length is changed via the Mode Select block descriptor, the
26699 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26700 * required as part of this routine. Therefore the mode sense allocation
26701 * length is specified to be the length of a mode page header and a
26702 * block descriptor.
26704 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26706 ssc = sd_ssc_init(un);
26707 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26708 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26709 sd_ssc_fini(ssc);
26710 if (rval != 0) {
26711 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26712 "sr_change_blkmode: Mode Sense Failed\n");
26713 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26714 return (rval);
26717 /* Check the block descriptor len to handle only 1 block descriptor */
26718 sense_mhp = (struct mode_header *)sense;
26719 if ((sense_mhp->bdesc_length == 0) ||
26720 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26721 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26722 "sr_change_blkmode: Mode Sense returned invalid block"
26723 " descriptor length\n");
26724 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26725 return (EIO);
26727 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26728 current_bsize = ((sense_desc->blksize_hi << 16) |
26729 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26731 /* Process command */
26732 switch (cmd) {
26733 case CDROMGBLKMODE:
26734 /* Return the block size obtained during the mode sense */
26735 if (ddi_copyout(&current_bsize, (void *)data,
26736 sizeof (int), flag) != 0)
26737 rval = EFAULT;
26738 break;
26739 case CDROMSBLKMODE:
26740 /* Validate the requested block size */
26741 switch (data) {
26742 case CDROM_BLK_512:
26743 case CDROM_BLK_1024:
26744 case CDROM_BLK_2048:
26745 case CDROM_BLK_2056:
26746 case CDROM_BLK_2336:
26747 case CDROM_BLK_2340:
26748 case CDROM_BLK_2352:
26749 case CDROM_BLK_2368:
26750 case CDROM_BLK_2448:
26751 case CDROM_BLK_2646:
26752 case CDROM_BLK_2647:
26753 break;
26754 default:
26755 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26756 "sr_change_blkmode: "
26757 "Block Size '%ld' Not Supported\n", data);
26758 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26759 return (EINVAL);
26763 * The current block size matches the requested block size so
26764 * there is no need to send the mode select to change the size
26766 if (current_bsize == data) {
26767 break;
26770 /* Build the select data for the requested block size */
26771 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26772 select_mhp = (struct mode_header *)select;
26773 select_desc =
26774 (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26776 * The LBA size is changed via the block descriptor, so the
26777 * descriptor is built according to the user data
26779 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26780 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16);
26781 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26782 select_desc->blksize_lo = (char)((data) & 0x000000ff);
26784 /* Send the mode select for the requested block size */
26785 ssc = sd_ssc_init(un);
26786 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26787 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26788 SD_PATH_STANDARD);
26789 sd_ssc_fini(ssc);
26790 if (rval != 0) {
26791 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26792 "sr_change_blkmode: Mode Select Failed\n");
26794 * The mode select failed for the requested block size,
26795 * so reset the data for the original block size and
26796 * send it to the target. The error is indicated by the
26797 * return value for the failed mode select.
26799 select_desc->blksize_hi = sense_desc->blksize_hi;
26800 select_desc->blksize_mid = sense_desc->blksize_mid;
26801 select_desc->blksize_lo = sense_desc->blksize_lo;
26802 ssc = sd_ssc_init(un);
26803 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26804 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26805 SD_PATH_STANDARD);
26806 sd_ssc_fini(ssc);
26807 } else {
26808 ASSERT(!mutex_owned(SD_MUTEX(un)));
26809 mutex_enter(SD_MUTEX(un));
26810 sd_update_block_info(un, (uint32_t)data, 0);
26811 mutex_exit(SD_MUTEX(un));
26813 break;
26814 default:
26815 /* should not reach here, but check anyway */
26816 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26817 "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26818 rval = EINVAL;
26819 break;
26822 if (select) {
26823 kmem_free(select, BUFLEN_CHG_BLK_MODE);
26825 if (sense) {
26826 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26828 return (rval);
26833 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
26834 * implement driver support for getting and setting the CD speed. The command
26835 * set used will be based on the device type. If the device has not been
26836 * identified as MMC the Toshiba vendor specific mode page will be used. If
26837 * the device is MMC but does not support the Real Time Streaming feature
26838 * the SET CD SPEED command will be used to set speed and mode page 0x2A will
26839 * be used to read the speed.
26843 * Function: sr_change_speed()
26845 * Description: This routine is the driver entry point for handling CD-ROM
26846 * drive speed ioctl requests for devices supporting the Toshiba
26847 * vendor specific drive speed mode page. Support for returning
26848 * and changing the current drive speed in use by the device is
26849 * implemented.
26851 * Arguments: dev - the device 'dev_t'
26852 * cmd - the request type; one of CDROMGDRVSPEED (get) or
26853 * CDROMSDRVSPEED (set)
26854 * data - current drive speed or requested drive speed
26855 * flag - this argument is a pass through to ddi_copyxxx() directly
26856 * from the mode argument of ioctl().
26858 * Return Code: the code returned by sd_send_scsi_cmd()
26859 * EINVAL if invalid arguments are provided
26860 * EFAULT if ddi_copyxxx() fails
26861 * ENXIO if fail ddi_get_soft_state
26862 * EIO if invalid mode sense block descriptor length
26865 static int
26866 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
26868 struct sd_lun *un = NULL;
26869 struct mode_header *sense_mhp, *select_mhp;
26870 struct mode_speed *sense_page, *select_page;
26871 int current_speed;
26872 int rval = EINVAL;
26873 int bd_len;
26874 uchar_t *sense = NULL;
26875 uchar_t *select = NULL;
26876 sd_ssc_t *ssc;
26878 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
26879 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26880 return (ENXIO);
26884 * Note: The drive speed is being modified here according to a Toshiba
26885 * vendor specific mode page (0x31).
26887 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26889 ssc = sd_ssc_init(un);
26890 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26891 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
26892 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_speed: Mode Sense Failed\n");
26897 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26898 return (rval);
26900 sense_mhp = (struct mode_header *)sense;
26902 /* Check the block descriptor len to handle only 1 block descriptor */
26903 bd_len = sense_mhp->bdesc_length;
26904 if (bd_len > MODE_BLK_DESC_LENGTH) {
26905 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26906 "sr_change_speed: Mode Sense returned invalid block "
26907 "descriptor length\n");
26908 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26909 return (EIO);
26912 sense_page = (struct mode_speed *)
26913 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
26914 current_speed = sense_page->speed;
26916 /* Process command */
26917 switch (cmd) {
26918 case CDROMGDRVSPEED:
26919 /* Return the drive speed obtained during the mode sense */
26920 if (current_speed == 0x2) {
26921 current_speed = CDROM_TWELVE_SPEED;
26923 if (ddi_copyout(&current_speed, (void *)data,
26924 sizeof (int), flag) != 0) {
26925 rval = EFAULT;
26927 break;
26928 case CDROMSDRVSPEED:
26929 /* Validate the requested drive speed */
26930 switch ((uchar_t)data) {
26931 case CDROM_TWELVE_SPEED:
26932 data = 0x2;
26933 /*FALLTHROUGH*/
26934 case CDROM_NORMAL_SPEED:
26935 case CDROM_DOUBLE_SPEED:
26936 case CDROM_QUAD_SPEED:
26937 case CDROM_MAXIMUM_SPEED:
26938 break;
26939 default:
26940 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26941 "sr_change_speed: "
26942 "Drive Speed '%d' Not Supported\n", (uchar_t)data);
26943 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26944 return (EINVAL);
26948 * The current drive speed matches the requested drive speed so
26949 * there is no need to send the mode select to change the speed
26951 if (current_speed == data) {
26952 break;
26955 /* Build the select data for the requested drive speed */
26956 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26957 select_mhp = (struct mode_header *)select;
26958 select_mhp->bdesc_length = 0;
26959 select_page =
26960 (struct mode_speed *)(select + MODE_HEADER_LENGTH);
26961 select_page =
26962 (struct mode_speed *)(select + MODE_HEADER_LENGTH);
26963 select_page->mode_page.code = CDROM_MODE_SPEED;
26964 select_page->mode_page.length = 2;
26965 select_page->speed = (uchar_t)data;
26967 /* Send the mode select for the requested block size */
26968 ssc = sd_ssc_init(un);
26969 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
26970 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
26971 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
26972 sd_ssc_fini(ssc);
26973 if (rval != 0) {
26975 * The mode select failed for the requested drive speed,
26976 * so reset the data for the original drive speed and
26977 * send it to the target. The error is indicated by the
26978 * return value for the failed mode select.
26980 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26981 "sr_drive_speed: Mode Select Failed\n");
26982 select_page->speed = sense_page->speed;
26983 ssc = sd_ssc_init(un);
26984 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
26985 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
26986 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
26987 sd_ssc_fini(ssc);
26989 break;
26990 default:
26991 /* should not reach here, but check anyway */
26992 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26993 "sr_change_speed: Command '%x' Not Supported\n", cmd);
26994 rval = EINVAL;
26995 break;
26998 if (select) {
26999 kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27001 if (sense) {
27002 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27005 return (rval);
27010 * Function: sr_atapi_change_speed()
27012 * Description: This routine is the driver entry point for handling CD-ROM
27013 * drive speed ioctl requests for MMC devices that do not support
27014 * the Real Time Streaming feature (0x107).
27016 * Note: This routine will use the SET SPEED command which may not
27017 * be supported by all devices.
27019 * Arguments: dev- the device 'dev_t'
27020 * cmd- the request type; one of CDROMGDRVSPEED (get) or
27021 * CDROMSDRVSPEED (set)
27022 * data- current drive speed or requested drive speed
27023 * flag- this argument is a pass through to ddi_copyxxx() directly
27024 * from the mode argument of ioctl().
27026 * Return Code: the code returned by sd_send_scsi_cmd()
27027 * EINVAL if invalid arguments are provided
27028 * EFAULT if ddi_copyxxx() fails
27029 * ENXIO if fail ddi_get_soft_state
27030 * EIO if invalid mode sense block descriptor length
27033 static int
27034 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27036 struct sd_lun *un;
27037 struct uscsi_cmd *com = NULL;
27038 struct mode_header_grp2 *sense_mhp;
27039 uchar_t *sense_page;
27040 uchar_t *sense = NULL;
27041 char cdb[CDB_GROUP5];
27042 int bd_len;
27043 int current_speed = 0;
27044 int max_speed = 0;
27045 int rval;
27046 sd_ssc_t *ssc;
27048 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27050 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27051 return (ENXIO);
27054 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27056 ssc = sd_ssc_init(un);
27057 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27058 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27059 SD_PATH_STANDARD);
27060 sd_ssc_fini(ssc);
27061 if (rval != 0) {
27062 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27063 "sr_atapi_change_speed: Mode Sense Failed\n");
27064 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27065 return (rval);
27068 /* Check the block descriptor len to handle only 1 block descriptor */
27069 sense_mhp = (struct mode_header_grp2 *)sense;
27070 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27071 if (bd_len > MODE_BLK_DESC_LENGTH) {
27072 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27073 "sr_atapi_change_speed: Mode Sense returned invalid "
27074 "block descriptor length\n");
27075 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27076 return (EIO);
27079 /* Calculate the current and maximum drive speeds */
27080 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27081 current_speed = (sense_page[14] << 8) | sense_page[15];
27082 max_speed = (sense_page[8] << 8) | sense_page[9];
27084 /* Process the command */
27085 switch (cmd) {
27086 case CDROMGDRVSPEED:
27087 current_speed /= SD_SPEED_1X;
27088 if (ddi_copyout(&current_speed, (void *)data,
27089 sizeof (int), flag) != 0)
27090 rval = EFAULT;
27091 break;
27092 case CDROMSDRVSPEED:
27093 /* Convert the speed code to KB/sec */
27094 switch ((uchar_t)data) {
27095 case CDROM_NORMAL_SPEED:
27096 current_speed = SD_SPEED_1X;
27097 break;
27098 case CDROM_DOUBLE_SPEED:
27099 current_speed = 2 * SD_SPEED_1X;
27100 break;
27101 case CDROM_QUAD_SPEED:
27102 current_speed = 4 * SD_SPEED_1X;
27103 break;
27104 case CDROM_TWELVE_SPEED:
27105 current_speed = 12 * SD_SPEED_1X;
27106 break;
27107 case CDROM_MAXIMUM_SPEED:
27108 current_speed = 0xffff;
27109 break;
27110 default:
27111 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27112 "sr_atapi_change_speed: invalid drive speed %d\n",
27113 (uchar_t)data);
27114 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27115 return (EINVAL);
27118 /* Check the request against the drive's max speed. */
27119 if (current_speed != 0xffff) {
27120 if (current_speed > max_speed) {
27121 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27122 return (EINVAL);
27127 * Build and send the SET SPEED command
27129 * Note: The SET SPEED (0xBB) command used in this routine is
27130 * obsolete per the SCSI MMC spec but still supported in the
27131 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27132 * therefore the command is still implemented in this routine.
27134 bzero(cdb, sizeof (cdb));
27135 cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27136 cdb[2] = (uchar_t)(current_speed >> 8);
27137 cdb[3] = (uchar_t)current_speed;
27138 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27139 com->uscsi_cdb = (caddr_t)cdb;
27140 com->uscsi_cdblen = CDB_GROUP5;
27141 com->uscsi_bufaddr = NULL;
27142 com->uscsi_buflen = 0;
27143 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27144 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27145 break;
27146 default:
27147 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27148 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27149 rval = EINVAL;
27152 if (sense) {
27153 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27155 if (com) {
27156 kmem_free(com, sizeof (*com));
27158 return (rval);
27163 * Function: sr_pause_resume()
27165 * Description: This routine is the driver entry point for handling CD-ROM
27166 * pause/resume ioctl requests. This only affects the audio play
27167 * operation.
27169 * Arguments: dev - the device 'dev_t'
27170 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27171 * for setting the resume bit of the cdb.
27173 * Return Code: the code returned by sd_send_scsi_cmd()
27174 * EINVAL if invalid mode specified
27178 static int
27179 sr_pause_resume(dev_t dev, int cmd)
27181 struct sd_lun *un;
27182 struct uscsi_cmd *com;
27183 char cdb[CDB_GROUP1];
27184 int rval;
27186 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27187 return (ENXIO);
27190 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27191 bzero(cdb, CDB_GROUP1);
27192 cdb[0] = SCMD_PAUSE_RESUME;
27193 switch (cmd) {
27194 case CDROMRESUME:
27195 cdb[8] = 1;
27196 break;
27197 case CDROMPAUSE:
27198 cdb[8] = 0;
27199 break;
27200 default:
27201 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27202 " Command '%x' Not Supported\n", cmd);
27203 rval = EINVAL;
27204 goto done;
27207 com->uscsi_cdb = cdb;
27208 com->uscsi_cdblen = CDB_GROUP1;
27209 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27211 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27212 SD_PATH_STANDARD);
27214 done:
27215 kmem_free(com, sizeof (*com));
27216 return (rval);
27221 * Function: sr_play_msf()
27223 * Description: This routine is the driver entry point for handling CD-ROM
27224 * ioctl requests to output the audio signals at the specified
27225 * starting address and continue the audio play until the specified
27226 * ending address (CDROMPLAYMSF) The address is in Minute Second
27227 * Frame (MSF) format.
27229 * Arguments: dev - the device 'dev_t'
27230 * data - pointer to user provided audio msf structure,
27231 * specifying start/end addresses.
27232 * flag - this argument is a pass through to ddi_copyxxx()
27233 * directly from the mode argument of ioctl().
27235 * Return Code: the code returned by sd_send_scsi_cmd()
27236 * EFAULT if ddi_copyxxx() fails
27237 * ENXIO if fail ddi_get_soft_state
27238 * EINVAL if data pointer is NULL
27241 static int
27242 sr_play_msf(dev_t dev, caddr_t data, int flag)
27244 struct sd_lun *un;
27245 struct uscsi_cmd *com;
27246 struct cdrom_msf msf_struct;
27247 struct cdrom_msf *msf = &msf_struct;
27248 char cdb[CDB_GROUP1];
27249 int rval;
27251 if (data == NULL) {
27252 return (EINVAL);
27255 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27256 return (ENXIO);
27259 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27260 return (EFAULT);
27263 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27264 bzero(cdb, CDB_GROUP1);
27265 cdb[0] = SCMD_PLAYAUDIO_MSF;
27266 if (un->un_f_cfg_playmsf_bcd == TRUE) {
27267 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27268 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27269 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27270 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27271 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27272 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27273 } else {
27274 cdb[3] = msf->cdmsf_min0;
27275 cdb[4] = msf->cdmsf_sec0;
27276 cdb[5] = msf->cdmsf_frame0;
27277 cdb[6] = msf->cdmsf_min1;
27278 cdb[7] = msf->cdmsf_sec1;
27279 cdb[8] = msf->cdmsf_frame1;
27281 com->uscsi_cdb = cdb;
27282 com->uscsi_cdblen = CDB_GROUP1;
27283 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27284 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27285 SD_PATH_STANDARD);
27286 kmem_free(com, sizeof (*com));
27287 return (rval);
27292 * Function: sr_play_trkind()
27294 * Description: This routine is the driver entry point for handling CD-ROM
27295 * ioctl requests to output the audio signals at the specified
27296 * starting address and continue the audio play until the specified
27297 * ending address (CDROMPLAYTRKIND). The address is in Track Index
27298 * format.
27300 * Arguments: dev - the device 'dev_t'
27301 * data - pointer to user provided audio track/index structure,
27302 * specifying start/end addresses.
27303 * flag - this argument is a pass through to ddi_copyxxx()
27304 * directly from the mode argument of ioctl().
27306 * Return Code: the code returned by sd_send_scsi_cmd()
27307 * EFAULT if ddi_copyxxx() fails
27308 * ENXIO if fail ddi_get_soft_state
27309 * EINVAL if data pointer is NULL
27312 static int
27313 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27315 struct cdrom_ti ti_struct;
27316 struct cdrom_ti *ti = &ti_struct;
27317 struct uscsi_cmd *com = NULL;
27318 char cdb[CDB_GROUP1];
27319 int rval;
27321 if (data == NULL) {
27322 return (EINVAL);
27325 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27326 return (EFAULT);
27329 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27330 bzero(cdb, CDB_GROUP1);
27331 cdb[0] = SCMD_PLAYAUDIO_TI;
27332 cdb[4] = ti->cdti_trk0;
27333 cdb[5] = ti->cdti_ind0;
27334 cdb[7] = ti->cdti_trk1;
27335 cdb[8] = ti->cdti_ind1;
27336 com->uscsi_cdb = cdb;
27337 com->uscsi_cdblen = CDB_GROUP1;
27338 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT;
27339 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27340 SD_PATH_STANDARD);
27341 kmem_free(com, sizeof (*com));
27342 return (rval);
27347 * Function: sr_read_all_subcodes()
27349 * Description: This routine is the driver entry point for handling CD-ROM
27350 * ioctl requests to return raw subcode data while the target is
27351 * playing audio (CDROMSUBCODE).
27353 * Arguments: dev - the device 'dev_t'
27354 * data - pointer to user provided cdrom subcode structure,
27355 * specifying the transfer length and address.
27356 * flag - this argument is a pass through to ddi_copyxxx()
27357 * directly from the mode argument of ioctl().
27359 * Return Code: the code returned by sd_send_scsi_cmd()
27360 * EFAULT if ddi_copyxxx() fails
27361 * ENXIO if fail ddi_get_soft_state
27362 * EINVAL if data pointer is NULL
27365 static int
27366 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27368 struct sd_lun *un = NULL;
27369 struct uscsi_cmd *com = NULL;
27370 struct cdrom_subcode *subcode = NULL;
27371 int rval;
27372 size_t buflen;
27373 char cdb[CDB_GROUP5];
27375 #ifdef _MULTI_DATAMODEL
27376 /* To support ILP32 applications in an LP64 world */
27377 struct cdrom_subcode32 cdrom_subcode32;
27378 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32;
27379 #endif
27380 if (data == NULL) {
27381 return (EINVAL);
27384 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27385 return (ENXIO);
27388 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27390 #ifdef _MULTI_DATAMODEL
27391 switch (ddi_model_convert_from(flag & FMODELS)) {
27392 case DDI_MODEL_ILP32:
27393 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27394 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27395 "sr_read_all_subcodes: ddi_copyin Failed\n");
27396 kmem_free(subcode, sizeof (struct cdrom_subcode));
27397 return (EFAULT);
27399 /* Convert the ILP32 uscsi data from the application to LP64 */
27400 cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27401 break;
27402 case DDI_MODEL_NONE:
27403 if (ddi_copyin(data, subcode,
27404 sizeof (struct cdrom_subcode), flag)) {
27405 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27406 "sr_read_all_subcodes: ddi_copyin Failed\n");
27407 kmem_free(subcode, sizeof (struct cdrom_subcode));
27408 return (EFAULT);
27410 break;
27412 #else /* ! _MULTI_DATAMODEL */
27413 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27414 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27415 "sr_read_all_subcodes: ddi_copyin Failed\n");
27416 kmem_free(subcode, sizeof (struct cdrom_subcode));
27417 return (EFAULT);
27419 #endif /* _MULTI_DATAMODEL */
27422 * Since MMC-2 expects max 3 bytes for length, check if the
27423 * length input is greater than 3 bytes
27425 if ((subcode->cdsc_length & 0xFF000000) != 0) {
27426 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27427 "sr_read_all_subcodes: "
27428 "cdrom transfer length too large: %d (limit %d)\n",
27429 subcode->cdsc_length, 0xFFFFFF);
27430 kmem_free(subcode, sizeof (struct cdrom_subcode));
27431 return (EINVAL);
27434 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27435 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27436 bzero(cdb, CDB_GROUP5);
27438 if (un->un_f_mmc_cap == TRUE) {
27439 cdb[0] = (char)SCMD_READ_CD;
27440 cdb[2] = (char)0xff;
27441 cdb[3] = (char)0xff;
27442 cdb[4] = (char)0xff;
27443 cdb[5] = (char)0xff;
27444 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27445 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27446 cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27447 cdb[10] = 1;
27448 } else {
27450 * Note: A vendor specific command (0xDF) is being used here to
27451 * request a read of all subcodes.
27453 cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27454 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27455 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27456 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27457 cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27459 com->uscsi_cdb = cdb;
27460 com->uscsi_cdblen = CDB_GROUP5;
27461 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27462 com->uscsi_buflen = buflen;
27463 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27464 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27465 SD_PATH_STANDARD);
27466 kmem_free(subcode, sizeof (struct cdrom_subcode));
27467 kmem_free(com, sizeof (*com));
27468 return (rval);
27473 * Function: sr_read_subchannel()
27475 * Description: This routine is the driver entry point for handling CD-ROM
27476 * ioctl requests to return the Q sub-channel data of the CD
27477 * current position block. (CDROMSUBCHNL) The data includes the
27478 * track number, index number, absolute CD-ROM address (LBA or MSF
27479 * format per the user) , track relative CD-ROM address (LBA or MSF
27480 * format per the user), control data and audio status.
27482 * Arguments: dev - the device 'dev_t'
27483 * data - pointer to user provided cdrom sub-channel structure
27484 * flag - this argument is a pass through to ddi_copyxxx()
27485 * directly from the mode argument of ioctl().
27487 * Return Code: the code returned by sd_send_scsi_cmd()
27488 * EFAULT if ddi_copyxxx() fails
27489 * ENXIO if fail ddi_get_soft_state
27490 * EINVAL if data pointer is NULL
27493 static int
27494 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27496 struct sd_lun *un;
27497 struct uscsi_cmd *com;
27498 struct cdrom_subchnl subchanel;
27499 struct cdrom_subchnl *subchnl = &subchanel;
27500 char cdb[CDB_GROUP1];
27501 caddr_t buffer;
27502 int rval;
27504 if (data == NULL) {
27505 return (EINVAL);
27508 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27509 (un->un_state == SD_STATE_OFFLINE)) {
27510 return (ENXIO);
27513 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27514 return (EFAULT);
27517 buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27518 bzero(cdb, CDB_GROUP1);
27519 cdb[0] = SCMD_READ_SUBCHANNEL;
27520 /* Set the MSF bit based on the user requested address format */
27521 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27523 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27524 * returned
27526 cdb[2] = 0x40;
27528 * Set byte 3 to specify the return data format. A value of 0x01
27529 * indicates that the CD-ROM current position should be returned.
27531 cdb[3] = 0x01;
27532 cdb[8] = 0x10;
27533 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27534 com->uscsi_cdb = cdb;
27535 com->uscsi_cdblen = CDB_GROUP1;
27536 com->uscsi_bufaddr = buffer;
27537 com->uscsi_buflen = 16;
27538 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27539 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27540 SD_PATH_STANDARD);
27541 if (rval != 0) {
27542 kmem_free(buffer, 16);
27543 kmem_free(com, sizeof (*com));
27544 return (rval);
27547 /* Process the returned Q sub-channel data */
27548 subchnl->cdsc_audiostatus = buffer[1];
27549 subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4;
27550 subchnl->cdsc_ctrl = (buffer[5] & 0x0F);
27551 subchnl->cdsc_trk = buffer[6];
27552 subchnl->cdsc_ind = buffer[7];
27553 if (subchnl->cdsc_format & CDROM_LBA) {
27554 subchnl->cdsc_absaddr.lba =
27555 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27556 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27557 subchnl->cdsc_reladdr.lba =
27558 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27559 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27560 } else if (un->un_f_cfg_readsub_bcd == TRUE) {
27561 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27562 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27563 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]);
27564 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27565 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27566 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]);
27567 } else {
27568 subchnl->cdsc_absaddr.msf.minute = buffer[9];
27569 subchnl->cdsc_absaddr.msf.second = buffer[10];
27570 subchnl->cdsc_absaddr.msf.frame = buffer[11];
27571 subchnl->cdsc_reladdr.msf.minute = buffer[13];
27572 subchnl->cdsc_reladdr.msf.second = buffer[14];
27573 subchnl->cdsc_reladdr.msf.frame = buffer[15];
27575 kmem_free(buffer, 16);
27576 kmem_free(com, sizeof (*com));
27577 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27578 != 0) {
27579 return (EFAULT);
27581 return (rval);
27586 * Function: sr_read_tocentry()
27588 * Description: This routine is the driver entry point for handling CD-ROM
27589 * ioctl requests to read from the Table of Contents (TOC)
27590 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27591 * fields, the starting address (LBA or MSF format per the user)
27592 * and the data mode if the user specified track is a data track.
27594 * Note: The READ HEADER (0x44) command used in this routine is
27595 * obsolete per the SCSI MMC spec but still supported in the
27596 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27597 * therefore the command is still implemented in this routine.
27599 * Arguments: dev - the device 'dev_t'
27600 * data - pointer to user provided toc entry structure,
27601 * specifying the track # and the address format
27602 * (LBA or MSF).
27603 * flag - this argument is a pass through to ddi_copyxxx()
27604 * directly from the mode argument of ioctl().
27606 * Return Code: the code returned by sd_send_scsi_cmd()
27607 * EFAULT if ddi_copyxxx() fails
27608 * ENXIO if fail ddi_get_soft_state
27609 * EINVAL if data pointer is NULL
27612 static int
27613 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27615 struct sd_lun *un = NULL;
27616 struct uscsi_cmd *com;
27617 struct cdrom_tocentry toc_entry;
27618 struct cdrom_tocentry *entry = &toc_entry;
27619 caddr_t buffer;
27620 int rval;
27621 char cdb[CDB_GROUP1];
27623 if (data == NULL) {
27624 return (EINVAL);
27627 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27628 (un->un_state == SD_STATE_OFFLINE)) {
27629 return (ENXIO);
27632 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27633 return (EFAULT);
27636 /* Validate the requested track and address format */
27637 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27638 return (EINVAL);
27641 if (entry->cdte_track == 0) {
27642 return (EINVAL);
27645 buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27646 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27647 bzero(cdb, CDB_GROUP1);
27649 cdb[0] = SCMD_READ_TOC;
27650 /* Set the MSF bit based on the user requested address format */
27651 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27652 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27653 cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27654 } else {
27655 cdb[6] = entry->cdte_track;
27659 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27660 * (4 byte TOC response header + 8 byte track descriptor)
27662 cdb[8] = 12;
27663 com->uscsi_cdb = cdb;
27664 com->uscsi_cdblen = CDB_GROUP1;
27665 com->uscsi_bufaddr = buffer;
27666 com->uscsi_buflen = 0x0C;
27667 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27668 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27669 SD_PATH_STANDARD);
27670 if (rval != 0) {
27671 kmem_free(buffer, 12);
27672 kmem_free(com, sizeof (*com));
27673 return (rval);
27676 /* Process the toc entry */
27677 entry->cdte_adr = (buffer[5] & 0xF0) >> 4;
27678 entry->cdte_ctrl = (buffer[5] & 0x0F);
27679 if (entry->cdte_format & CDROM_LBA) {
27680 entry->cdte_addr.lba =
27681 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27682 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27683 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27684 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]);
27685 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]);
27686 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]);
27688 * Send a READ TOC command using the LBA address format to get
27689 * the LBA for the track requested so it can be used in the
27690 * READ HEADER request
27692 * Note: The MSF bit of the READ HEADER command specifies the
27693 * output format. The block address specified in that command
27694 * must be in LBA format.
27696 cdb[1] = 0;
27697 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27698 SD_PATH_STANDARD);
27699 if (rval != 0) {
27700 kmem_free(buffer, 12);
27701 kmem_free(com, sizeof (*com));
27702 return (rval);
27704 } else {
27705 entry->cdte_addr.msf.minute = buffer[9];
27706 entry->cdte_addr.msf.second = buffer[10];
27707 entry->cdte_addr.msf.frame = buffer[11];
27709 * Send a READ TOC command using the LBA address format to get
27710 * the LBA for the track requested so it can be used in the
27711 * READ HEADER request
27713 * Note: The MSF bit of the READ HEADER command specifies the
27714 * output format. The block address specified in that command
27715 * must be in LBA format.
27717 cdb[1] = 0;
27718 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27719 SD_PATH_STANDARD);
27720 if (rval != 0) {
27721 kmem_free(buffer, 12);
27722 kmem_free(com, sizeof (*com));
27723 return (rval);
27728 * Build and send the READ HEADER command to determine the data mode of
27729 * the user specified track.
27731 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27732 (entry->cdte_track != CDROM_LEADOUT)) {
27733 bzero(cdb, CDB_GROUP1);
27734 cdb[0] = SCMD_READ_HEADER;
27735 cdb[2] = buffer[8];
27736 cdb[3] = buffer[9];
27737 cdb[4] = buffer[10];
27738 cdb[5] = buffer[11];
27739 cdb[8] = 0x08;
27740 com->uscsi_buflen = 0x08;
27741 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27742 SD_PATH_STANDARD);
27743 if (rval == 0) {
27744 entry->cdte_datamode = buffer[0];
27745 } else {
27747 * READ HEADER command failed, since this is
27748 * obsoleted in one spec, its better to return
27749 * -1 for an invlid track so that we can still
27750 * receive the rest of the TOC data.
27752 entry->cdte_datamode = (uchar_t)-1;
27754 } else {
27755 entry->cdte_datamode = (uchar_t)-1;
27758 kmem_free(buffer, 12);
27759 kmem_free(com, sizeof (*com));
27760 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27761 return (EFAULT);
27763 return (rval);
27768 * Function: sr_read_tochdr()
27770 * Description: This routine is the driver entry point for handling CD-ROM
27771 * ioctl requests to read the Table of Contents (TOC) header
27772 * (CDROMREADTOHDR). The TOC header consists of the disk starting
27773 * and ending track numbers
27775 * Arguments: dev - the device 'dev_t'
27776 * data - pointer to user provided toc header structure,
27777 * specifying the starting and ending track numbers.
27778 * flag - this argument is a pass through to ddi_copyxxx()
27779 * directly from the mode argument of ioctl().
27781 * Return Code: the code returned by sd_send_scsi_cmd()
27782 * EFAULT if ddi_copyxxx() fails
27783 * ENXIO if fail ddi_get_soft_state
27784 * EINVAL if data pointer is NULL
27787 static int
27788 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27790 struct sd_lun *un;
27791 struct uscsi_cmd *com;
27792 struct cdrom_tochdr toc_header;
27793 struct cdrom_tochdr *hdr = &toc_header;
27794 char cdb[CDB_GROUP1];
27795 int rval;
27796 caddr_t buffer;
27798 if (data == NULL) {
27799 return (EINVAL);
27802 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27803 (un->un_state == SD_STATE_OFFLINE)) {
27804 return (ENXIO);
27807 buffer = kmem_zalloc(4, KM_SLEEP);
27808 bzero(cdb, CDB_GROUP1);
27809 cdb[0] = SCMD_READ_TOC;
27811 * Specifying a track number of 0x00 in the READ TOC command indicates
27812 * that the TOC header should be returned
27814 cdb[6] = 0x00;
27816 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27817 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27819 cdb[8] = 0x04;
27820 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27821 com->uscsi_cdb = cdb;
27822 com->uscsi_cdblen = CDB_GROUP1;
27823 com->uscsi_bufaddr = buffer;
27824 com->uscsi_buflen = 0x04;
27825 com->uscsi_timeout = 300;
27826 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27828 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27829 SD_PATH_STANDARD);
27830 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27831 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
27832 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
27833 } else {
27834 hdr->cdth_trk0 = buffer[2];
27835 hdr->cdth_trk1 = buffer[3];
27837 kmem_free(buffer, 4);
27838 kmem_free(com, sizeof (*com));
27839 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
27840 return (EFAULT);
27842 return (rval);
27847 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
27848 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
27849 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
27850 * digital audio and extended architecture digital audio. These modes are
27851 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
27852 * MMC specs.
27854 * In addition to support for the various data formats these routines also
27855 * include support for devices that implement only the direct access READ
27856 * commands (0x08, 0x28), devices that implement the READ_CD commands
27857 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
27858 * READ CDXA commands (0xD8, 0xDB)
27862 * Function: sr_read_mode1()
27864 * Description: This routine is the driver entry point for handling CD-ROM
27865 * ioctl read mode1 requests (CDROMREADMODE1).
27867 * Arguments: dev - the device 'dev_t'
27868 * data - pointer to user provided cd read structure specifying
27869 * the lba buffer address and length.
27870 * flag - this argument is a pass through to ddi_copyxxx()
27871 * directly from the mode argument of ioctl().
27873 * Return Code: the code returned by sd_send_scsi_cmd()
27874 * EFAULT if ddi_copyxxx() fails
27875 * ENXIO if fail ddi_get_soft_state
27876 * EINVAL if data pointer is NULL
27879 static int
27880 sr_read_mode1(dev_t dev, caddr_t data, int flag)
27882 struct sd_lun *un;
27883 struct cdrom_read mode1_struct;
27884 struct cdrom_read *mode1 = &mode1_struct;
27885 int rval;
27886 sd_ssc_t *ssc;
27888 #ifdef _MULTI_DATAMODEL
27889 /* To support ILP32 applications in an LP64 world */
27890 struct cdrom_read32 cdrom_read32;
27891 struct cdrom_read32 *cdrd32 = &cdrom_read32;
27892 #endif /* _MULTI_DATAMODEL */
27894 if (data == NULL) {
27895 return (EINVAL);
27898 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27899 (un->un_state == SD_STATE_OFFLINE)) {
27900 return (ENXIO);
27903 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27904 "sd_read_mode1: entry: un:0x%p\n", un);
27906 #ifdef _MULTI_DATAMODEL
27907 switch (ddi_model_convert_from(flag & FMODELS)) {
27908 case DDI_MODEL_ILP32:
27909 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27910 return (EFAULT);
27912 /* Convert the ILP32 uscsi data from the application to LP64 */
27913 cdrom_read32tocdrom_read(cdrd32, mode1);
27914 break;
27915 case DDI_MODEL_NONE:
27916 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27917 return (EFAULT);
27920 #else /* ! _MULTI_DATAMODEL */
27921 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27922 return (EFAULT);
27924 #endif /* _MULTI_DATAMODEL */
27926 ssc = sd_ssc_init(un);
27927 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
27928 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
27929 sd_ssc_fini(ssc);
27931 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27932 "sd_read_mode1: exit: un:0x%p\n", un);
27934 return (rval);
27939 * Function: sr_read_cd_mode2()
27941 * Description: This routine is the driver entry point for handling CD-ROM
27942 * ioctl read mode2 requests (CDROMREADMODE2) for devices that
27943 * support the READ CD (0xBE) command or the 1st generation
27944 * READ CD (0xD4) command.
27946 * Arguments: dev - the device 'dev_t'
27947 * data - pointer to user provided cd read structure specifying
27948 * the lba buffer address and length.
27949 * flag - this argument is a pass through to ddi_copyxxx()
27950 * directly from the mode argument of ioctl().
27952 * Return Code: the code returned by sd_send_scsi_cmd()
27953 * EFAULT if ddi_copyxxx() fails
27954 * ENXIO if fail ddi_get_soft_state
27955 * EINVAL if data pointer is NULL
27958 static int
27959 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
27961 struct sd_lun *un;
27962 struct uscsi_cmd *com;
27963 struct cdrom_read mode2_struct;
27964 struct cdrom_read *mode2 = &mode2_struct;
27965 uchar_t cdb[CDB_GROUP5];
27966 int nblocks;
27967 int rval;
27968 #ifdef _MULTI_DATAMODEL
27969 /* To support ILP32 applications in an LP64 world */
27970 struct cdrom_read32 cdrom_read32;
27971 struct cdrom_read32 *cdrd32 = &cdrom_read32;
27972 #endif /* _MULTI_DATAMODEL */
27974 if (data == NULL) {
27975 return (EINVAL);
27978 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27979 (un->un_state == SD_STATE_OFFLINE)) {
27980 return (ENXIO);
27983 #ifdef _MULTI_DATAMODEL
27984 switch (ddi_model_convert_from(flag & FMODELS)) {
27985 case DDI_MODEL_ILP32:
27986 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27987 return (EFAULT);
27989 /* Convert the ILP32 uscsi data from the application to LP64 */
27990 cdrom_read32tocdrom_read(cdrd32, mode2);
27991 break;
27992 case DDI_MODEL_NONE:
27993 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
27994 return (EFAULT);
27996 break;
27999 #else /* ! _MULTI_DATAMODEL */
28000 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28001 return (EFAULT);
28003 #endif /* _MULTI_DATAMODEL */
28005 bzero(cdb, sizeof (cdb));
28006 if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28007 /* Read command supported by 1st generation atapi drives */
28008 cdb[0] = SCMD_READ_CDD4;
28009 } else {
28010 /* Universal CD Access Command */
28011 cdb[0] = SCMD_READ_CD;
28015 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28017 cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28019 /* set the start address */
28020 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28021 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28022 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28023 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28025 /* set the transfer length */
28026 nblocks = mode2->cdread_buflen / 2336;
28027 cdb[6] = (uchar_t)(nblocks >> 16);
28028 cdb[7] = (uchar_t)(nblocks >> 8);
28029 cdb[8] = (uchar_t)nblocks;
28031 /* set the filter bits */
28032 cdb[9] = CDROM_READ_CD_USERDATA;
28034 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28035 com->uscsi_cdb = (caddr_t)cdb;
28036 com->uscsi_cdblen = sizeof (cdb);
28037 com->uscsi_bufaddr = mode2->cdread_bufaddr;
28038 com->uscsi_buflen = mode2->cdread_buflen;
28039 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28041 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28042 SD_PATH_STANDARD);
28043 kmem_free(com, sizeof (*com));
28044 return (rval);
28049 * Function: sr_read_mode2()
28051 * Description: This routine is the driver entry point for handling CD-ROM
28052 * ioctl read mode2 requests (CDROMREADMODE2) for devices that
28053 * do not support the READ CD (0xBE) command.
28055 * Arguments: dev - the device 'dev_t'
28056 * data - pointer to user provided cd read structure specifying
28057 * the lba buffer address and length.
28058 * flag - this argument is a pass through to ddi_copyxxx()
28059 * directly from the mode argument of ioctl().
28061 * Return Code: the code returned by sd_send_scsi_cmd()
28062 * EFAULT if ddi_copyxxx() fails
28063 * ENXIO if fail ddi_get_soft_state
28064 * EINVAL if data pointer is NULL
28065 * EIO if fail to reset block size
28066 * EAGAIN if commands are in progress in the driver
28069 static int
28070 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28072 struct sd_lun *un;
28073 struct cdrom_read mode2_struct;
28074 struct cdrom_read *mode2 = &mode2_struct;
28075 int rval;
28076 uint32_t restore_blksize;
28077 struct uscsi_cmd *com;
28078 uchar_t cdb[CDB_GROUP0];
28079 int nblocks;
28081 #ifdef _MULTI_DATAMODEL
28082 /* To support ILP32 applications in an LP64 world */
28083 struct cdrom_read32 cdrom_read32;
28084 struct cdrom_read32 *cdrd32 = &cdrom_read32;
28085 #endif /* _MULTI_DATAMODEL */
28087 if (data == NULL) {
28088 return (EINVAL);
28091 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28092 (un->un_state == SD_STATE_OFFLINE)) {
28093 return (ENXIO);
28097 * Because this routine will update the device and driver block size
28098 * being used we want to make sure there are no commands in progress.
28099 * If commands are in progress the user will have to try again.
28101 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28102 * in sdioctl to protect commands from sdioctl through to the top of
28103 * sd_uscsi_strategy. See sdioctl for details.
28105 mutex_enter(SD_MUTEX(un));
28106 if (un->un_ncmds_in_driver != 1) {
28107 mutex_exit(SD_MUTEX(un));
28108 return (EAGAIN);
28110 mutex_exit(SD_MUTEX(un));
28112 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28113 "sd_read_mode2: entry: un:0x%p\n", un);
28115 #ifdef _MULTI_DATAMODEL
28116 switch (ddi_model_convert_from(flag & FMODELS)) {
28117 case DDI_MODEL_ILP32:
28118 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28119 return (EFAULT);
28121 /* Convert the ILP32 uscsi data from the application to LP64 */
28122 cdrom_read32tocdrom_read(cdrd32, mode2);
28123 break;
28124 case DDI_MODEL_NONE:
28125 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28126 return (EFAULT);
28128 break;
28130 #else /* ! _MULTI_DATAMODEL */
28131 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28132 return (EFAULT);
28134 #endif /* _MULTI_DATAMODEL */
28136 /* Store the current target block size for restoration later */
28137 restore_blksize = un->un_tgt_blocksize;
28139 /* Change the device and soft state target block size to 2336 */
28140 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28141 rval = EIO;
28142 goto done;
28146 bzero(cdb, sizeof (cdb));
28148 /* set READ operation */
28149 cdb[0] = SCMD_READ;
28151 /* adjust lba for 2kbyte blocks from 512 byte blocks */
28152 mode2->cdread_lba >>= 2;
28154 /* set the start address */
28155 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28156 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28157 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28159 /* set the transfer length */
28160 nblocks = mode2->cdread_buflen / 2336;
28161 cdb[4] = (uchar_t)nblocks & 0xFF;
28163 /* build command */
28164 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28165 com->uscsi_cdb = (caddr_t)cdb;
28166 com->uscsi_cdblen = sizeof (cdb);
28167 com->uscsi_bufaddr = mode2->cdread_bufaddr;
28168 com->uscsi_buflen = mode2->cdread_buflen;
28169 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28172 * Issue SCSI command with user space address for read buffer.
28174 * This sends the command through main channel in the driver.
28176 * Since this is accessed via an IOCTL call, we go through the
28177 * standard path, so that if the device was powered down, then
28178 * it would be 'awakened' to handle the command.
28180 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28181 SD_PATH_STANDARD);
28183 kmem_free(com, sizeof (*com));
28185 /* Restore the device and soft state target block size */
28186 if (sr_sector_mode(dev, restore_blksize) != 0) {
28187 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28188 "can't do switch back to mode 1\n");
28190 * If sd_send_scsi_READ succeeded we still need to report
28191 * an error because we failed to reset the block size
28193 if (rval == 0) {
28194 rval = EIO;
28198 done:
28199 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28200 "sd_read_mode2: exit: un:0x%p\n", un);
28202 return (rval);
28207 * Function: sr_sector_mode()
28209 * Description: This utility function is used by sr_read_mode2 to set the target
28210 * block size based on the user specified size. This is a legacy
28211 * implementation based upon a vendor specific mode page
28213 * Arguments: dev - the device 'dev_t'
28214 * data - flag indicating if block size is being set to 2336 or
28215 * 512.
28217 * Return Code: the code returned by sd_send_scsi_cmd()
28218 * EFAULT if ddi_copyxxx() fails
28219 * ENXIO if fail ddi_get_soft_state
28220 * EINVAL if data pointer is NULL
28223 static int
28224 sr_sector_mode(dev_t dev, uint32_t blksize)
28226 struct sd_lun *un;
28227 uchar_t *sense;
28228 uchar_t *select;
28229 int rval;
28230 sd_ssc_t *ssc;
28232 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28233 (un->un_state == SD_STATE_OFFLINE)) {
28234 return (ENXIO);
28237 sense = kmem_zalloc(20, KM_SLEEP);
28239 /* Note: This is a vendor specific mode page (0x81) */
28240 ssc = sd_ssc_init(un);
28241 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28242 SD_PATH_STANDARD);
28243 sd_ssc_fini(ssc);
28244 if (rval != 0) {
28245 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28246 "sr_sector_mode: Mode Sense failed\n");
28247 kmem_free(sense, 20);
28248 return (rval);
28250 select = kmem_zalloc(20, KM_SLEEP);
28251 select[3] = 0x08;
28252 select[10] = ((blksize >> 8) & 0xff);
28253 select[11] = (blksize & 0xff);
28254 select[12] = 0x01;
28255 select[13] = 0x06;
28256 select[14] = sense[14];
28257 select[15] = sense[15];
28258 if (blksize == SD_MODE2_BLKSIZE) {
28259 select[14] |= 0x01;
28262 ssc = sd_ssc_init(un);
28263 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28264 SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28265 sd_ssc_fini(ssc);
28266 if (rval != 0) {
28267 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28268 "sr_sector_mode: Mode Select failed\n");
28269 } else {
28271 * Only update the softstate block size if we successfully
28272 * changed the device block mode.
28274 mutex_enter(SD_MUTEX(un));
28275 sd_update_block_info(un, blksize, 0);
28276 mutex_exit(SD_MUTEX(un));
28278 kmem_free(sense, 20);
28279 kmem_free(select, 20);
28280 return (rval);
28285 * Function: sr_read_cdda()
28287 * Description: This routine is the driver entry point for handling CD-ROM
28288 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28289 * the target supports CDDA these requests are handled via a vendor
28290 * specific command (0xD8) If the target does not support CDDA
28291 * these requests are handled via the READ CD command (0xBE).
28293 * Arguments: dev - the device 'dev_t'
28294 * data - pointer to user provided CD-DA structure specifying
28295 * the track starting address, transfer length, and
28296 * subcode options.
28297 * flag - this argument is a pass through to ddi_copyxxx()
28298 * directly from the mode argument of ioctl().
28300 * Return Code: the code returned by sd_send_scsi_cmd()
28301 * EFAULT if ddi_copyxxx() fails
28302 * ENXIO if fail ddi_get_soft_state
28303 * EINVAL if invalid arguments are provided
28304 * ENOTTY
28307 static int
28308 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28310 struct sd_lun *un;
28311 struct uscsi_cmd *com;
28312 struct cdrom_cdda *cdda;
28313 int rval;
28314 size_t buflen;
28315 char cdb[CDB_GROUP5];
28317 #ifdef _MULTI_DATAMODEL
28318 /* To support ILP32 applications in an LP64 world */
28319 struct cdrom_cdda32 cdrom_cdda32;
28320 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32;
28321 #endif /* _MULTI_DATAMODEL */
28323 if (data == NULL) {
28324 return (EINVAL);
28327 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28328 return (ENXIO);
28331 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28333 #ifdef _MULTI_DATAMODEL
28334 switch (ddi_model_convert_from(flag & FMODELS)) {
28335 case DDI_MODEL_ILP32:
28336 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28337 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28338 "sr_read_cdda: ddi_copyin Failed\n");
28339 kmem_free(cdda, sizeof (struct cdrom_cdda));
28340 return (EFAULT);
28342 /* Convert the ILP32 uscsi data from the application to LP64 */
28343 cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28344 break;
28345 case DDI_MODEL_NONE:
28346 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28347 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28348 "sr_read_cdda: ddi_copyin Failed\n");
28349 kmem_free(cdda, sizeof (struct cdrom_cdda));
28350 return (EFAULT);
28352 break;
28354 #else /* ! _MULTI_DATAMODEL */
28355 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28356 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28357 "sr_read_cdda: ddi_copyin Failed\n");
28358 kmem_free(cdda, sizeof (struct cdrom_cdda));
28359 return (EFAULT);
28361 #endif /* _MULTI_DATAMODEL */
28364 * Since MMC-2 expects max 3 bytes for length, check if the
28365 * length input is greater than 3 bytes
28367 if ((cdda->cdda_length & 0xFF000000) != 0) {
28368 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28369 "cdrom transfer length too large: %d (limit %d)\n",
28370 cdda->cdda_length, 0xFFFFFF);
28371 kmem_free(cdda, sizeof (struct cdrom_cdda));
28372 return (EINVAL);
28375 switch (cdda->cdda_subcode) {
28376 case CDROM_DA_NO_SUBCODE:
28377 buflen = CDROM_BLK_2352 * cdda->cdda_length;
28378 break;
28379 case CDROM_DA_SUBQ:
28380 buflen = CDROM_BLK_2368 * cdda->cdda_length;
28381 break;
28382 case CDROM_DA_ALL_SUBCODE:
28383 buflen = CDROM_BLK_2448 * cdda->cdda_length;
28384 break;
28385 case CDROM_DA_SUBCODE_ONLY:
28386 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28387 break;
28388 default:
28389 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28390 "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28391 cdda->cdda_subcode);
28392 kmem_free(cdda, sizeof (struct cdrom_cdda));
28393 return (EINVAL);
28396 /* Build and send the command */
28397 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28398 bzero(cdb, CDB_GROUP5);
28400 if (un->un_f_cfg_cdda == TRUE) {
28401 cdb[0] = (char)SCMD_READ_CD;
28402 cdb[1] = 0x04;
28403 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28404 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28405 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28406 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28407 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28408 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28409 cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28410 cdb[9] = 0x10;
28411 switch (cdda->cdda_subcode) {
28412 case CDROM_DA_NO_SUBCODE :
28413 cdb[10] = 0x0;
28414 break;
28415 case CDROM_DA_SUBQ :
28416 cdb[10] = 0x2;
28417 break;
28418 case CDROM_DA_ALL_SUBCODE :
28419 cdb[10] = 0x1;
28420 break;
28421 case CDROM_DA_SUBCODE_ONLY :
28422 /* FALLTHROUGH */
28423 default :
28424 kmem_free(cdda, sizeof (struct cdrom_cdda));
28425 kmem_free(com, sizeof (*com));
28426 return (ENOTTY);
28428 } else {
28429 cdb[0] = (char)SCMD_READ_CDDA;
28430 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28431 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28432 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28433 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28434 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28435 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28436 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28437 cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28438 cdb[10] = cdda->cdda_subcode;
28441 com->uscsi_cdb = cdb;
28442 com->uscsi_cdblen = CDB_GROUP5;
28443 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28444 com->uscsi_buflen = buflen;
28445 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28447 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28448 SD_PATH_STANDARD);
28450 kmem_free(cdda, sizeof (struct cdrom_cdda));
28451 kmem_free(com, sizeof (*com));
28452 return (rval);
28457 * Function: sr_read_cdxa()
28459 * Description: This routine is the driver entry point for handling CD-ROM
28460 * ioctl requests to return CD-XA (Extended Architecture) data.
28461 * (CDROMCDXA).
28463 * Arguments: dev - the device 'dev_t'
28464 * data - pointer to user provided CD-XA structure specifying
28465 * the data starting address, transfer length, and format
28466 * flag - this argument is a pass through to ddi_copyxxx()
28467 * directly from the mode argument of ioctl().
28469 * Return Code: the code returned by sd_send_scsi_cmd()
28470 * EFAULT if ddi_copyxxx() fails
28471 * ENXIO if fail ddi_get_soft_state
28472 * EINVAL if data pointer is NULL
28475 static int
28476 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28478 struct sd_lun *un;
28479 struct uscsi_cmd *com;
28480 struct cdrom_cdxa *cdxa;
28481 int rval;
28482 size_t buflen;
28483 char cdb[CDB_GROUP5];
28484 uchar_t read_flags;
28486 #ifdef _MULTI_DATAMODEL
28487 /* To support ILP32 applications in an LP64 world */
28488 struct cdrom_cdxa32 cdrom_cdxa32;
28489 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32;
28490 #endif /* _MULTI_DATAMODEL */
28492 if (data == NULL) {
28493 return (EINVAL);
28496 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28497 return (ENXIO);
28500 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28502 #ifdef _MULTI_DATAMODEL
28503 switch (ddi_model_convert_from(flag & FMODELS)) {
28504 case DDI_MODEL_ILP32:
28505 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28506 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28507 return (EFAULT);
28510 * Convert the ILP32 uscsi data from the
28511 * application to LP64 for internal use.
28513 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28514 break;
28515 case DDI_MODEL_NONE:
28516 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28517 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28518 return (EFAULT);
28520 break;
28522 #else /* ! _MULTI_DATAMODEL */
28523 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28524 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28525 return (EFAULT);
28527 #endif /* _MULTI_DATAMODEL */
28530 * Since MMC-2 expects max 3 bytes for length, check if the
28531 * length input is greater than 3 bytes
28533 if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28534 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28535 "cdrom transfer length too large: %d (limit %d)\n",
28536 cdxa->cdxa_length, 0xFFFFFF);
28537 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28538 return (EINVAL);
28541 switch (cdxa->cdxa_format) {
28542 case CDROM_XA_DATA:
28543 buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28544 read_flags = 0x10;
28545 break;
28546 case CDROM_XA_SECTOR_DATA:
28547 buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28548 read_flags = 0xf8;
28549 break;
28550 case CDROM_XA_DATA_W_ERROR:
28551 buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28552 read_flags = 0xfc;
28553 break;
28554 default:
28555 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28556 "sr_read_cdxa: Format '0x%x' Not Supported\n",
28557 cdxa->cdxa_format);
28558 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28559 return (EINVAL);
28562 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28563 bzero(cdb, CDB_GROUP5);
28564 if (un->un_f_mmc_cap == TRUE) {
28565 cdb[0] = (char)SCMD_READ_CD;
28566 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28567 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28568 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28569 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28570 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28571 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28572 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28573 cdb[9] = (char)read_flags;
28574 } else {
28576 * Note: A vendor specific command (0xDB) is being used her to
28577 * request a read of all subcodes.
28579 cdb[0] = (char)SCMD_READ_CDXA;
28580 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28581 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28582 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28583 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28584 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28585 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28586 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28587 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28588 cdb[10] = cdxa->cdxa_format;
28590 com->uscsi_cdb = cdb;
28591 com->uscsi_cdblen = CDB_GROUP5;
28592 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28593 com->uscsi_buflen = buflen;
28594 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28595 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28596 SD_PATH_STANDARD);
28597 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28598 kmem_free(com, sizeof (*com));
28599 return (rval);
28604 * Function: sr_eject()
28606 * Description: This routine is the driver entry point for handling CD-ROM
28607 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28609 * Arguments: dev - the device 'dev_t'
28611 * Return Code: the code returned by sd_send_scsi_cmd()
28614 static int
28615 sr_eject(dev_t dev)
28617 struct sd_lun *un;
28618 int rval;
28619 sd_ssc_t *ssc;
28621 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28622 (un->un_state == SD_STATE_OFFLINE)) {
28623 return (ENXIO);
28627 * To prevent race conditions with the eject
28628 * command, keep track of an eject command as
28629 * it progresses. If we are already handling
28630 * an eject command in the driver for the given
28631 * unit and another request to eject is received
28632 * immediately return EAGAIN so we don't lose
28633 * the command if the current eject command fails.
28635 mutex_enter(SD_MUTEX(un));
28636 if (un->un_f_ejecting == TRUE) {
28637 mutex_exit(SD_MUTEX(un));
28638 return (EAGAIN);
28640 un->un_f_ejecting = TRUE;
28641 mutex_exit(SD_MUTEX(un));
28643 ssc = sd_ssc_init(un);
28644 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28645 SD_PATH_STANDARD);
28646 sd_ssc_fini(ssc);
28648 if (rval != 0) {
28649 mutex_enter(SD_MUTEX(un));
28650 un->un_f_ejecting = FALSE;
28651 mutex_exit(SD_MUTEX(un));
28652 return (rval);
28655 ssc = sd_ssc_init(un);
28656 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28657 SD_TARGET_EJECT, SD_PATH_STANDARD);
28658 sd_ssc_fini(ssc);
28660 if (rval == 0) {
28661 mutex_enter(SD_MUTEX(un));
28662 sr_ejected(un);
28663 un->un_mediastate = DKIO_EJECTED;
28664 un->un_f_ejecting = FALSE;
28665 cv_broadcast(&un->un_state_cv);
28666 mutex_exit(SD_MUTEX(un));
28667 } else {
28668 mutex_enter(SD_MUTEX(un));
28669 un->un_f_ejecting = FALSE;
28670 mutex_exit(SD_MUTEX(un));
28672 return (rval);
28677 * Function: sr_ejected()
28679 * Description: This routine updates the soft state structure to invalidate the
28680 * geometry information after the media has been ejected or a
28681 * media eject has been detected.
28683 * Arguments: un - driver soft state (unit) structure
28686 static void
28687 sr_ejected(struct sd_lun *un)
28689 struct sd_errstats *stp;
28691 ASSERT(un != NULL);
28692 ASSERT(mutex_owned(SD_MUTEX(un)));
28694 un->un_f_blockcount_is_valid = FALSE;
28695 un->un_f_tgt_blocksize_is_valid = FALSE;
28696 mutex_exit(SD_MUTEX(un));
28697 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28698 mutex_enter(SD_MUTEX(un));
28700 if (un->un_errstats != NULL) {
28701 stp = (struct sd_errstats *)un->un_errstats->ks_data;
28702 stp->sd_capacity.value.ui64 = 0;
28708 * Function: sr_check_wp()
28710 * Description: This routine checks the write protection of a removable
28711 * media disk and hotpluggable devices via the write protect bit of
28712 * the Mode Page Header device specific field. Some devices choke
28713 * on unsupported mode page. In order to workaround this issue,
28714 * this routine has been implemented to use 0x3f mode page(request
28715 * for all pages) for all device types.
28717 * Arguments: dev - the device 'dev_t'
28719 * Return Code: int indicating if the device is write protected (1) or not (0)
28721 * Context: Kernel thread.
28725 static int
28726 sr_check_wp(dev_t dev)
28728 struct sd_lun *un;
28729 uchar_t device_specific;
28730 uchar_t *sense;
28731 int hdrlen;
28732 int rval = FALSE;
28733 int status;
28734 sd_ssc_t *ssc;
28737 * Note: The return codes for this routine should be reworked to
28738 * properly handle the case of a NULL softstate.
28740 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28741 return (FALSE);
28744 if (un->un_f_cfg_is_atapi == TRUE) {
28746 * The mode page contents are not required; set the allocation
28747 * length for the mode page header only
28749 hdrlen = MODE_HEADER_LENGTH_GRP2;
28750 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28751 ssc = sd_ssc_init(un);
28752 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28753 MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28754 sd_ssc_fini(ssc);
28755 if (status != 0)
28756 goto err_exit;
28757 device_specific =
28758 ((struct mode_header_grp2 *)sense)->device_specific;
28759 } else {
28760 hdrlen = MODE_HEADER_LENGTH;
28761 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28762 ssc = sd_ssc_init(un);
28763 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28764 MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28765 sd_ssc_fini(ssc);
28766 if (status != 0)
28767 goto err_exit;
28768 device_specific =
28769 ((struct mode_header *)sense)->device_specific;
28774 * Write protect mode sense failed; not all disks
28775 * understand this query. Return FALSE assuming that
28776 * these devices are not writable.
28778 if (device_specific & WRITE_PROTECT) {
28779 rval = TRUE;
28782 err_exit:
28783 kmem_free(sense, hdrlen);
28784 return (rval);
28788 * Function: sr_volume_ctrl()
28790 * Description: This routine is the driver entry point for handling CD-ROM
28791 * audio output volume ioctl requests. (CDROMVOLCTRL)
28793 * Arguments: dev - the device 'dev_t'
28794 * data - pointer to user audio volume control structure
28795 * flag - this argument is a pass through to ddi_copyxxx()
28796 * directly from the mode argument of ioctl().
28798 * Return Code: the code returned by sd_send_scsi_cmd()
28799 * EFAULT if ddi_copyxxx() fails
28800 * ENXIO if fail ddi_get_soft_state
28801 * EINVAL if data pointer is NULL
28805 static int
28806 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28808 struct sd_lun *un;
28809 struct cdrom_volctrl volume;
28810 struct cdrom_volctrl *vol = &volume;
28811 uchar_t *sense_page;
28812 uchar_t *select_page;
28813 uchar_t *sense;
28814 uchar_t *select;
28815 int sense_buflen;
28816 int select_buflen;
28817 int rval;
28818 sd_ssc_t *ssc;
28820 if (data == NULL) {
28821 return (EINVAL);
28824 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28825 (un->un_state == SD_STATE_OFFLINE)) {
28826 return (ENXIO);
28829 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28830 return (EFAULT);
28833 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28834 struct mode_header_grp2 *sense_mhp;
28835 struct mode_header_grp2 *select_mhp;
28836 int bd_len;
28838 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
28839 select_buflen = MODE_HEADER_LENGTH_GRP2 +
28840 MODEPAGE_AUDIO_CTRL_LEN;
28841 sense = kmem_zalloc(sense_buflen, KM_SLEEP);
28842 select = kmem_zalloc(select_buflen, KM_SLEEP);
28843 ssc = sd_ssc_init(un);
28844 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
28845 sense_buflen, MODEPAGE_AUDIO_CTRL,
28846 SD_PATH_STANDARD);
28847 sd_ssc_fini(ssc);
28849 if (rval != 0) {
28850 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28851 "sr_volume_ctrl: Mode Sense Failed\n");
28852 kmem_free(sense, sense_buflen);
28853 kmem_free(select, select_buflen);
28854 return (rval);
28856 sense_mhp = (struct mode_header_grp2 *)sense;
28857 select_mhp = (struct mode_header_grp2 *)select;
28858 bd_len = (sense_mhp->bdesc_length_hi << 8) |
28859 sense_mhp->bdesc_length_lo;
28860 if (bd_len > MODE_BLK_DESC_LENGTH) {
28861 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28862 "sr_volume_ctrl: Mode Sense returned invalid "
28863 "block descriptor length\n");
28864 kmem_free(sense, sense_buflen);
28865 kmem_free(select, select_buflen);
28866 return (EIO);
28868 sense_page = (uchar_t *)
28869 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
28870 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
28871 select_mhp->length_msb = 0;
28872 select_mhp->length_lsb = 0;
28873 select_mhp->bdesc_length_hi = 0;
28874 select_mhp->bdesc_length_lo = 0;
28875 } else {
28876 struct mode_header *sense_mhp, *select_mhp;
28878 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28879 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28880 sense = kmem_zalloc(sense_buflen, KM_SLEEP);
28881 select = kmem_zalloc(select_buflen, KM_SLEEP);
28882 ssc = sd_ssc_init(un);
28883 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
28884 sense_buflen, MODEPAGE_AUDIO_CTRL,
28885 SD_PATH_STANDARD);
28886 sd_ssc_fini(ssc);
28888 if (rval != 0) {
28889 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28890 "sr_volume_ctrl: Mode Sense Failed\n");
28891 kmem_free(sense, sense_buflen);
28892 kmem_free(select, select_buflen);
28893 return (rval);
28895 sense_mhp = (struct mode_header *)sense;
28896 select_mhp = (struct mode_header *)select;
28897 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
28898 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28899 "sr_volume_ctrl: Mode Sense returned invalid "
28900 "block descriptor length\n");
28901 kmem_free(sense, sense_buflen);
28902 kmem_free(select, select_buflen);
28903 return (EIO);
28905 sense_page = (uchar_t *)
28906 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
28907 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
28908 select_mhp->length = 0;
28909 select_mhp->bdesc_length = 0;
28912 * Note: An audio control data structure could be created and overlayed
28913 * on the following in place of the array indexing method implemented.
28916 /* Build the select data for the user volume data */
28917 select_page[0] = MODEPAGE_AUDIO_CTRL;
28918 select_page[1] = 0xE;
28919 /* Set the immediate bit */
28920 select_page[2] = 0x04;
28921 /* Zero out reserved fields */
28922 select_page[3] = 0x00;
28923 select_page[4] = 0x00;
28924 /* Return sense data for fields not to be modified */
28925 select_page[5] = sense_page[5];
28926 select_page[6] = sense_page[6];
28927 select_page[7] = sense_page[7];
28928 /* Set the user specified volume levels for channel 0 and 1 */
28929 select_page[8] = 0x01;
28930 select_page[9] = vol->channel0;
28931 select_page[10] = 0x02;
28932 select_page[11] = vol->channel1;
28933 /* Channel 2 and 3 are currently unsupported so return the sense data */
28934 select_page[12] = sense_page[12];
28935 select_page[13] = sense_page[13];
28936 select_page[14] = sense_page[14];
28937 select_page[15] = sense_page[15];
28939 ssc = sd_ssc_init(un);
28940 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28941 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
28942 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28943 } else {
28944 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
28945 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28947 sd_ssc_fini(ssc);
28949 kmem_free(sense, sense_buflen);
28950 kmem_free(select, select_buflen);
28951 return (rval);
28956 * Function: sr_read_sony_session_offset()
28958 * Description: This routine is the driver entry point for handling CD-ROM
28959 * ioctl requests for session offset information. (CDROMREADOFFSET)
28960 * The address of the first track in the last session of a
28961 * multi-session CD-ROM is returned
28963 * Note: This routine uses a vendor specific key value in the
28964 * command control field without implementing any vendor check here
28965 * or in the ioctl routine.
28967 * Arguments: dev - the device 'dev_t'
28968 * data - pointer to an int to hold the requested address
28969 * flag - this argument is a pass through to ddi_copyxxx()
28970 * directly from the mode argument of ioctl().
28972 * Return Code: the code returned by sd_send_scsi_cmd()
28973 * EFAULT if ddi_copyxxx() fails
28974 * ENXIO if fail ddi_get_soft_state
28975 * EINVAL if data pointer is NULL
28978 static int
28979 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
28981 struct sd_lun *un;
28982 struct uscsi_cmd *com;
28983 caddr_t buffer;
28984 char cdb[CDB_GROUP1];
28985 int session_offset = 0;
28986 int rval;
28988 if (data == NULL) {
28989 return (EINVAL);
28992 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28993 (un->un_state == SD_STATE_OFFLINE)) {
28994 return (ENXIO);
28997 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
28998 bzero(cdb, CDB_GROUP1);
28999 cdb[0] = SCMD_READ_TOC;
29001 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29002 * (4 byte TOC response header + 8 byte response data)
29004 cdb[8] = SONY_SESSION_OFFSET_LEN;
29005 /* Byte 9 is the control byte. A vendor specific value is used */
29006 cdb[9] = SONY_SESSION_OFFSET_KEY;
29007 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29008 com->uscsi_cdb = cdb;
29009 com->uscsi_cdblen = CDB_GROUP1;
29010 com->uscsi_bufaddr = buffer;
29011 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29012 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29014 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29015 SD_PATH_STANDARD);
29016 if (rval != 0) {
29017 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29018 kmem_free(com, sizeof (*com));
29019 return (rval);
29021 if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29022 session_offset =
29023 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29024 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29026 * Offset returned offset in current lbasize block's. Convert to
29027 * 2k block's to return to the user
29029 if (un->un_tgt_blocksize == CDROM_BLK_512) {
29030 session_offset >>= 2;
29031 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29032 session_offset >>= 1;
29036 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29037 rval = EFAULT;
29040 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29041 kmem_free(com, sizeof (*com));
29042 return (rval);
29047 * Function: sd_wm_cache_constructor()
29049 * Description: Cache Constructor for the wmap cache for the read/modify/write
29050 * devices.
29052 * Arguments: wm - A pointer to the sd_w_map to be initialized.
29053 * un - sd_lun structure for the device.
29054 * flag - the km flags passed to constructor
29056 * Return Code: 0 on success.
29057 * -1 on failure.
29060 /*ARGSUSED*/
29061 static int
29062 sd_wm_cache_constructor(void *wm, void *un, int flags)
29064 bzero(wm, sizeof (struct sd_w_map));
29065 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29066 return (0);
29071 * Function: sd_wm_cache_destructor()
29073 * Description: Cache destructor for the wmap cache for the read/modify/write
29074 * devices.
29076 * Arguments: wm - A pointer to the sd_w_map to be initialized.
29077 * un - sd_lun structure for the device.
29079 /*ARGSUSED*/
29080 static void
29081 sd_wm_cache_destructor(void *wm, void *un)
29083 cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29088 * Function: sd_range_lock()
29090 * Description: Lock the range of blocks specified as parameter to ensure
29091 * that read, modify write is atomic and no other i/o writes
29092 * to the same location. The range is specified in terms
29093 * of start and end blocks. Block numbers are the actual
29094 * media block numbers and not system.
29096 * Arguments: un - sd_lun structure for the device.
29097 * startb - The starting block number
29098 * endb - The end block number
29099 * typ - type of i/o - simple/read_modify_write
29101 * Return Code: wm - pointer to the wmap structure.
29103 * Context: This routine can sleep.
29106 static struct sd_w_map *
29107 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29109 struct sd_w_map *wmp = NULL;
29110 struct sd_w_map *sl_wmp = NULL;
29111 struct sd_w_map *tmp_wmp;
29112 wm_state state = SD_WM_CHK_LIST;
29115 ASSERT(un != NULL);
29116 ASSERT(!mutex_owned(SD_MUTEX(un)));
29118 mutex_enter(SD_MUTEX(un));
29120 while (state != SD_WM_DONE) {
29122 switch (state) {
29123 case SD_WM_CHK_LIST:
29125 * This is the starting state. Check the wmap list
29126 * to see if the range is currently available.
29128 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29130 * If this is a simple write and no rmw
29131 * i/o is pending then try to lock the
29132 * range as the range should be available.
29134 state = SD_WM_LOCK_RANGE;
29135 } else {
29136 tmp_wmp = sd_get_range(un, startb, endb);
29137 if (tmp_wmp != NULL) {
29138 if ((wmp != NULL) && ONLIST(un, wmp)) {
29140 * Should not keep onlist wmps
29141 * while waiting this macro
29142 * will also do wmp = NULL;
29144 FREE_ONLIST_WMAP(un, wmp);
29147 * sl_wmp is the wmap on which wait
29148 * is done, since the tmp_wmp points
29149 * to the inuse wmap, set sl_wmp to
29150 * tmp_wmp and change the state to sleep
29152 sl_wmp = tmp_wmp;
29153 state = SD_WM_WAIT_MAP;
29154 } else {
29155 state = SD_WM_LOCK_RANGE;
29159 break;
29161 case SD_WM_LOCK_RANGE:
29162 ASSERT(un->un_wm_cache);
29164 * The range need to be locked, try to get a wmap.
29165 * First attempt it with NO_SLEEP, want to avoid a sleep
29166 * if possible as we will have to release the sd mutex
29167 * if we have to sleep.
29169 if (wmp == NULL)
29170 wmp = kmem_cache_alloc(un->un_wm_cache,
29171 KM_NOSLEEP);
29172 if (wmp == NULL) {
29173 mutex_exit(SD_MUTEX(un));
29174 _NOTE(DATA_READABLE_WITHOUT_LOCK
29175 (sd_lun::un_wm_cache))
29176 wmp = kmem_cache_alloc(un->un_wm_cache,
29177 KM_SLEEP);
29178 mutex_enter(SD_MUTEX(un));
29180 * we released the mutex so recheck and go to
29181 * check list state.
29183 state = SD_WM_CHK_LIST;
29184 } else {
29186 * We exit out of state machine since we
29187 * have the wmap. Do the housekeeping first.
29188 * place the wmap on the wmap list if it is not
29189 * on it already and then set the state to done.
29191 wmp->wm_start = startb;
29192 wmp->wm_end = endb;
29193 wmp->wm_flags = typ | SD_WM_BUSY;
29194 if (typ & SD_WTYPE_RMW) {
29195 un->un_rmw_count++;
29198 * If not already on the list then link
29200 if (!ONLIST(un, wmp)) {
29201 wmp->wm_next = un->un_wm;
29202 wmp->wm_prev = NULL;
29203 if (wmp->wm_next)
29204 wmp->wm_next->wm_prev = wmp;
29205 un->un_wm = wmp;
29207 state = SD_WM_DONE;
29209 break;
29211 case SD_WM_WAIT_MAP:
29212 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29214 * Wait is done on sl_wmp, which is set in the
29215 * check_list state.
29217 sl_wmp->wm_wanted_count++;
29218 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29219 sl_wmp->wm_wanted_count--;
29221 * We can reuse the memory from the completed sl_wmp
29222 * lock range for our new lock, but only if noone is
29223 * waiting for it.
29225 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29226 if (sl_wmp->wm_wanted_count == 0) {
29227 if (wmp != NULL) {
29228 CHK_N_FREEWMP(un, wmp);
29230 wmp = sl_wmp;
29232 sl_wmp = NULL;
29234 * After waking up, need to recheck for availability of
29235 * range.
29237 state = SD_WM_CHK_LIST;
29238 break;
29240 default:
29241 panic("sd_range_lock: "
29242 "Unknown state %d in sd_range_lock", state);
29243 /*NOTREACHED*/
29244 } /* switch(state) */
29246 } /* while(state != SD_WM_DONE) */
29248 mutex_exit(SD_MUTEX(un));
29250 ASSERT(wmp != NULL);
29252 return (wmp);
29257 * Function: sd_get_range()
29259 * Description: Find if there any overlapping I/O to this one
29260 * Returns the write-map of 1st such I/O, NULL otherwise.
29262 * Arguments: un - sd_lun structure for the device.
29263 * startb - The starting block number
29264 * endb - The end block number
29266 * Return Code: wm - pointer to the wmap structure.
29269 static struct sd_w_map *
29270 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29272 struct sd_w_map *wmp;
29274 ASSERT(un != NULL);
29276 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29277 if (!(wmp->wm_flags & SD_WM_BUSY)) {
29278 continue;
29280 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29281 break;
29283 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29284 break;
29288 return (wmp);
29293 * Function: sd_free_inlist_wmap()
29295 * Description: Unlink and free a write map struct.
29297 * Arguments: un - sd_lun structure for the device.
29298 * wmp - sd_w_map which needs to be unlinked.
29301 static void
29302 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29304 ASSERT(un != NULL);
29306 if (un->un_wm == wmp) {
29307 un->un_wm = wmp->wm_next;
29308 } else {
29309 wmp->wm_prev->wm_next = wmp->wm_next;
29312 if (wmp->wm_next) {
29313 wmp->wm_next->wm_prev = wmp->wm_prev;
29316 wmp->wm_next = wmp->wm_prev = NULL;
29318 kmem_cache_free(un->un_wm_cache, wmp);
29323 * Function: sd_range_unlock()
29325 * Description: Unlock the range locked by wm.
29326 * Free write map if nobody else is waiting on it.
29328 * Arguments: un - sd_lun structure for the device.
29329 * wmp - sd_w_map which needs to be unlinked.
29332 static void
29333 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29335 ASSERT(un != NULL);
29336 ASSERT(wm != NULL);
29337 ASSERT(!mutex_owned(SD_MUTEX(un)));
29339 mutex_enter(SD_MUTEX(un));
29341 if (wm->wm_flags & SD_WTYPE_RMW) {
29342 un->un_rmw_count--;
29345 if (wm->wm_wanted_count) {
29346 wm->wm_flags = 0;
29348 * Broadcast that the wmap is available now.
29350 cv_broadcast(&wm->wm_avail);
29351 } else {
29353 * If no one is waiting on the map, it should be free'ed.
29355 sd_free_inlist_wmap(un, wm);
29358 mutex_exit(SD_MUTEX(un));
29363 * Function: sd_read_modify_write_task
29365 * Description: Called from a taskq thread to initiate the write phase of
29366 * a read-modify-write request. This is used for targets where
29367 * un->un_sys_blocksize != un->un_tgt_blocksize.
29369 * Arguments: arg - a pointer to the buf(9S) struct for the write command.
29371 * Context: Called under taskq thread context.
29374 static void
29375 sd_read_modify_write_task(void *arg)
29377 struct sd_mapblocksize_info *bsp;
29378 struct buf *bp;
29379 struct sd_xbuf *xp;
29380 struct sd_lun *un;
29382 bp = arg; /* The bp is given in arg */
29383 ASSERT(bp != NULL);
29385 /* Get the pointer to the layer-private data struct */
29386 xp = SD_GET_XBUF(bp);
29387 ASSERT(xp != NULL);
29388 bsp = xp->xb_private;
29389 ASSERT(bsp != NULL);
29391 un = SD_GET_UN(bp);
29392 ASSERT(un != NULL);
29393 ASSERT(!mutex_owned(SD_MUTEX(un)));
29395 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29396 "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29399 * This is the write phase of a read-modify-write request, called
29400 * under the context of a taskq thread in response to the completion
29401 * of the read portion of the rmw request completing under interrupt
29402 * context. The write request must be sent from here down the iostart
29403 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29404 * we use the layer index saved in the layer-private data area.
29406 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29408 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29409 "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29414 * Function: sddump_do_read_of_rmw()
29416 * Description: This routine will be called from sddump, If sddump is called
29417 * with an I/O which not aligned on device blocksize boundary
29418 * then the write has to be converted to read-modify-write.
29419 * Do the read part here in order to keep sddump simple.
29420 * Note - That the sd_mutex is held across the call to this
29421 * routine.
29423 * Arguments: un - sd_lun
29424 * blkno - block number in terms of media block size.
29425 * nblk - number of blocks.
29426 * bpp - pointer to pointer to the buf structure. On return
29427 * from this function, *bpp points to the valid buffer
29428 * to which the write has to be done.
29430 * Return Code: 0 for success or errno-type return code
29433 static int
29434 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29435 struct buf **bpp)
29437 int err;
29438 int i;
29439 int rval;
29440 struct buf *bp;
29441 struct scsi_pkt *pkt = NULL;
29442 uint32_t target_blocksize;
29444 ASSERT(un != NULL);
29445 ASSERT(mutex_owned(SD_MUTEX(un)));
29447 target_blocksize = un->un_tgt_blocksize;
29449 mutex_exit(SD_MUTEX(un));
29451 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
29452 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29453 if (bp == NULL) {
29454 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29455 "no resources for dumping; giving up");
29456 err = ENOMEM;
29457 goto done;
29460 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29461 blkno, nblk);
29462 if (rval != 0) {
29463 scsi_free_consistent_buf(bp);
29464 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29465 "no resources for dumping; giving up");
29466 err = ENOMEM;
29467 goto done;
29470 pkt->pkt_flags |= FLAG_NOINTR;
29472 err = EIO;
29473 for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29476 * Scsi_poll returns 0 (success) if the command completes and
29477 * the status block is STATUS_GOOD. We should only check
29478 * errors if this condition is not true. Even then we should
29479 * send our own request sense packet only if we have a check
29480 * condition and auto request sense has not been performed by
29481 * the hba.
29483 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29485 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29486 err = 0;
29487 break;
29491 * Check CMD_DEV_GONE 1st, give up if device is gone,
29492 * no need to read RQS data.
29494 if (pkt->pkt_reason == CMD_DEV_GONE) {
29495 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29496 "Error while dumping state with rmw..."
29497 "Device is gone\n");
29498 break;
29501 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29502 SD_INFO(SD_LOG_DUMP, un,
29503 "sddump: read failed with CHECK, try # %d\n", i);
29504 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29505 (void) sd_send_polled_RQS(un);
29508 continue;
29511 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29512 int reset_retval = 0;
29514 SD_INFO(SD_LOG_DUMP, un,
29515 "sddump: read failed with BUSY, try # %d\n", i);
29517 if (un->un_f_lun_reset_enabled == TRUE) {
29518 reset_retval = scsi_reset(SD_ADDRESS(un),
29519 RESET_LUN);
29521 if (reset_retval == 0) {
29522 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29524 (void) sd_send_polled_RQS(un);
29526 } else {
29527 SD_INFO(SD_LOG_DUMP, un,
29528 "sddump: read failed with 0x%x, try # %d\n",
29529 SD_GET_PKT_STATUS(pkt), i);
29530 mutex_enter(SD_MUTEX(un));
29531 sd_reset_target(un, pkt);
29532 mutex_exit(SD_MUTEX(un));
29536 * If we are not getting anywhere with lun/target resets,
29537 * let's reset the bus.
29539 if (i > SD_NDUMP_RETRIES/2) {
29540 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29541 (void) sd_send_polled_RQS(un);
29545 scsi_destroy_pkt(pkt);
29547 if (err != 0) {
29548 scsi_free_consistent_buf(bp);
29549 *bpp = NULL;
29550 } else {
29551 *bpp = bp;
29554 done:
29555 mutex_enter(SD_MUTEX(un));
29556 return (err);
29561 * Function: sd_failfast_flushq
29563 * Description: Take all bp's on the wait queue that have B_FAILFAST set
29564 * in b_flags and move them onto the failfast queue, then kick
29565 * off a thread to return all bp's on the failfast queue to
29566 * their owners with an error set.
29568 * Arguments: un - pointer to the soft state struct for the instance.
29570 * Context: may execute in interrupt context.
29573 static void
29574 sd_failfast_flushq(struct sd_lun *un)
29576 struct buf *bp;
29577 struct buf *next_waitq_bp;
29578 struct buf *prev_waitq_bp = NULL;
29580 ASSERT(un != NULL);
29581 ASSERT(mutex_owned(SD_MUTEX(un)));
29582 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29583 ASSERT(un->un_failfast_bp == NULL);
29585 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29586 "sd_failfast_flushq: entry: un:0x%p\n", un);
29589 * Check if we should flush all bufs when entering failfast state, or
29590 * just those with B_FAILFAST set.
29592 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29594 * Move *all* bp's on the wait queue to the failfast flush
29595 * queue, including those that do NOT have B_FAILFAST set.
29597 if (un->un_failfast_headp == NULL) {
29598 ASSERT(un->un_failfast_tailp == NULL);
29599 un->un_failfast_headp = un->un_waitq_headp;
29600 } else {
29601 ASSERT(un->un_failfast_tailp != NULL);
29602 un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29605 un->un_failfast_tailp = un->un_waitq_tailp;
29607 /* update kstat for each bp moved out of the waitq */
29608 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29609 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29612 /* empty the waitq */
29613 un->un_waitq_headp = un->un_waitq_tailp = NULL;
29615 } else {
29617 * Go thru the wait queue, pick off all entries with
29618 * B_FAILFAST set, and move these onto the failfast queue.
29620 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29622 * Save the pointer to the next bp on the wait queue,
29623 * so we get to it on the next iteration of this loop.
29625 next_waitq_bp = bp->av_forw;
29628 * If this bp from the wait queue does NOT have
29629 * B_FAILFAST set, just move on to the next element
29630 * in the wait queue. Note, this is the only place
29631 * where it is correct to set prev_waitq_bp.
29633 if ((bp->b_flags & B_FAILFAST) == 0) {
29634 prev_waitq_bp = bp;
29635 continue;
29639 * Remove the bp from the wait queue.
29641 if (bp == un->un_waitq_headp) {
29642 /* The bp is the first element of the waitq. */
29643 un->un_waitq_headp = next_waitq_bp;
29644 if (un->un_waitq_headp == NULL) {
29645 /* The wait queue is now empty */
29646 un->un_waitq_tailp = NULL;
29648 } else {
29650 * The bp is either somewhere in the middle
29651 * or at the end of the wait queue.
29653 ASSERT(un->un_waitq_headp != NULL);
29654 ASSERT(prev_waitq_bp != NULL);
29655 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29656 == 0);
29657 if (bp == un->un_waitq_tailp) {
29658 /* bp is the last entry on the waitq. */
29659 ASSERT(next_waitq_bp == NULL);
29660 un->un_waitq_tailp = prev_waitq_bp;
29662 prev_waitq_bp->av_forw = next_waitq_bp;
29664 bp->av_forw = NULL;
29667 * update kstat since the bp is moved out of
29668 * the waitq
29670 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29673 * Now put the bp onto the failfast queue.
29675 if (un->un_failfast_headp == NULL) {
29676 /* failfast queue is currently empty */
29677 ASSERT(un->un_failfast_tailp == NULL);
29678 un->un_failfast_headp =
29679 un->un_failfast_tailp = bp;
29680 } else {
29681 /* Add the bp to the end of the failfast q */
29682 ASSERT(un->un_failfast_tailp != NULL);
29683 ASSERT(un->un_failfast_tailp->b_flags &
29684 B_FAILFAST);
29685 un->un_failfast_tailp->av_forw = bp;
29686 un->un_failfast_tailp = bp;
29692 * Now return all bp's on the failfast queue to their owners.
29694 while ((bp = un->un_failfast_headp) != NULL) {
29696 un->un_failfast_headp = bp->av_forw;
29697 if (un->un_failfast_headp == NULL) {
29698 un->un_failfast_tailp = NULL;
29702 * We want to return the bp with a failure error code, but
29703 * we do not want a call to sd_start_cmds() to occur here,
29704 * so use sd_return_failed_command_no_restart() instead of
29705 * sd_return_failed_command().
29707 sd_return_failed_command_no_restart(un, bp, EIO);
29710 /* Flush the xbuf queues if required. */
29711 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29712 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29715 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29716 "sd_failfast_flushq: exit: un:0x%p\n", un);
29721 * Function: sd_failfast_flushq_callback
29723 * Description: Return TRUE if the given bp meets the criteria for failfast
29724 * flushing. Used with ddi_xbuf_flushq(9F).
29726 * Arguments: bp - ptr to buf struct to be examined.
29728 * Context: Any
29731 static int
29732 sd_failfast_flushq_callback(struct buf *bp)
29735 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29736 * state is entered; OR (2) the given bp has B_FAILFAST set.
29738 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29739 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29745 * Function: sd_setup_next_xfer
29747 * Description: Prepare next I/O operation using DMA_PARTIAL
29751 static int
29752 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29753 struct scsi_pkt *pkt, struct sd_xbuf *xp)
29755 ssize_t num_blks_not_xfered;
29756 daddr_t strt_blk_num;
29757 ssize_t bytes_not_xfered;
29758 int rval;
29760 ASSERT(pkt->pkt_resid == 0);
29763 * Calculate next block number and amount to be transferred.
29765 * How much data NOT transfered to the HBA yet.
29767 bytes_not_xfered = xp->xb_dma_resid;
29770 * figure how many blocks NOT transfered to the HBA yet.
29772 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29775 * set starting block number to the end of what WAS transfered.
29777 strt_blk_num = xp->xb_blkno +
29778 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29781 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt
29782 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29783 * the disk mutex here.
29785 rval = sd_setup_next_rw_pkt(un, pkt, bp,
29786 strt_blk_num, num_blks_not_xfered);
29788 if (rval == 0) {
29791 * Success.
29793 * Adjust things if there are still more blocks to be
29794 * transfered.
29796 xp->xb_dma_resid = pkt->pkt_resid;
29797 pkt->pkt_resid = 0;
29799 return (1);
29803 * There's really only one possible return value from
29804 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29805 * returns NULL.
29807 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29809 bp->b_resid = bp->b_bcount;
29810 bp->b_flags |= B_ERROR;
29812 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29813 "Error setting up next portion of DMA transfer\n");
29815 return (0);
29819 * Function: sd_panic_for_res_conflict
29821 * Description: Call panic with a string formatted with "Reservation Conflict"
29822 * and a human readable identifier indicating the SD instance
29823 * that experienced the reservation conflict.
29825 * Arguments: un - pointer to the soft state struct for the instance.
29827 * Context: may execute in interrupt context.
29830 #define SD_RESV_CONFLICT_FMT_LEN 40
29831 void
29832 sd_panic_for_res_conflict(struct sd_lun *un)
29834 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
29835 char path_str[MAXPATHLEN];
29837 (void) snprintf(panic_str, sizeof (panic_str),
29838 "Reservation Conflict\nDisk: %s",
29839 ddi_pathname(SD_DEVINFO(un), path_str));
29841 panic(panic_str);
29845 * Note: The following sd_faultinjection_ioctl( ) routines implement
29846 * driver support for handling fault injection for error analysis
29847 * causing faults in multiple layers of the driver.
29851 #ifdef SD_FAULT_INJECTION
29852 static uint_t sd_fault_injection_on = 0;
29855 * Function: sd_faultinjection_ioctl()
29857 * Description: This routine is the driver entry point for handling
29858 * faultinjection ioctls to inject errors into the
29859 * layer model
29861 * Arguments: cmd - the ioctl cmd received
29862 * arg - the arguments from user and returns
29865 static void
29866 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un)
29868 uint_t i = 0;
29869 uint_t rval;
29871 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
29873 mutex_enter(SD_MUTEX(un));
29875 switch (cmd) {
29876 case SDIOCRUN:
29877 /* Allow pushed faults to be injected */
29878 SD_INFO(SD_LOG_SDTEST, un,
29879 "sd_faultinjection_ioctl: Injecting Fault Run\n");
29881 sd_fault_injection_on = 1;
29883 SD_INFO(SD_LOG_IOERR, un,
29884 "sd_faultinjection_ioctl: run finished\n");
29885 break;
29887 case SDIOCSTART:
29888 /* Start Injection Session */
29889 SD_INFO(SD_LOG_SDTEST, un,
29890 "sd_faultinjection_ioctl: Injecting Fault Start\n");
29892 sd_fault_injection_on = 0;
29893 un->sd_injection_mask = 0xFFFFFFFF;
29894 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29895 un->sd_fi_fifo_pkt[i] = NULL;
29896 un->sd_fi_fifo_xb[i] = NULL;
29897 un->sd_fi_fifo_un[i] = NULL;
29898 un->sd_fi_fifo_arq[i] = NULL;
29900 un->sd_fi_fifo_start = 0;
29901 un->sd_fi_fifo_end = 0;
29903 mutex_enter(&(un->un_fi_mutex));
29904 un->sd_fi_log[0] = '\0';
29905 un->sd_fi_buf_len = 0;
29906 mutex_exit(&(un->un_fi_mutex));
29908 SD_INFO(SD_LOG_IOERR, un,
29909 "sd_faultinjection_ioctl: start finished\n");
29910 break;
29912 case SDIOCSTOP:
29913 /* Stop Injection Session */
29914 SD_INFO(SD_LOG_SDTEST, un,
29915 "sd_faultinjection_ioctl: Injecting Fault Stop\n");
29916 sd_fault_injection_on = 0;
29917 un->sd_injection_mask = 0x0;
29919 /* Empty stray or unuseds structs from fifo */
29920 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29921 if (un->sd_fi_fifo_pkt[i] != NULL) {
29922 kmem_free(un->sd_fi_fifo_pkt[i],
29923 sizeof (struct sd_fi_pkt));
29925 if (un->sd_fi_fifo_xb[i] != NULL) {
29926 kmem_free(un->sd_fi_fifo_xb[i],
29927 sizeof (struct sd_fi_xb));
29929 if (un->sd_fi_fifo_un[i] != NULL) {
29930 kmem_free(un->sd_fi_fifo_un[i],
29931 sizeof (struct sd_fi_un));
29933 if (un->sd_fi_fifo_arq[i] != NULL) {
29934 kmem_free(un->sd_fi_fifo_arq[i],
29935 sizeof (struct sd_fi_arq));
29937 un->sd_fi_fifo_pkt[i] = NULL;
29938 un->sd_fi_fifo_un[i] = NULL;
29939 un->sd_fi_fifo_xb[i] = NULL;
29940 un->sd_fi_fifo_arq[i] = NULL;
29942 un->sd_fi_fifo_start = 0;
29943 un->sd_fi_fifo_end = 0;
29945 SD_INFO(SD_LOG_IOERR, un,
29946 "sd_faultinjection_ioctl: stop finished\n");
29947 break;
29949 case SDIOCINSERTPKT:
29950 /* Store a packet struct to be pushed onto fifo */
29951 SD_INFO(SD_LOG_SDTEST, un,
29952 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
29954 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29956 sd_fault_injection_on = 0;
29958 /* No more that SD_FI_MAX_ERROR allowed in Queue */
29959 if (un->sd_fi_fifo_pkt[i] != NULL) {
29960 kmem_free(un->sd_fi_fifo_pkt[i],
29961 sizeof (struct sd_fi_pkt));
29963 if (arg != (intptr_t)NULL) {
29964 un->sd_fi_fifo_pkt[i] =
29965 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
29966 if (un->sd_fi_fifo_pkt[i] == NULL) {
29967 /* Alloc failed don't store anything */
29968 break;
29970 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
29971 sizeof (struct sd_fi_pkt), 0);
29972 if (rval == -1) {
29973 kmem_free(un->sd_fi_fifo_pkt[i],
29974 sizeof (struct sd_fi_pkt));
29975 un->sd_fi_fifo_pkt[i] = NULL;
29977 } else {
29978 SD_INFO(SD_LOG_IOERR, un,
29979 "sd_faultinjection_ioctl: pkt null\n");
29981 break;
29983 case SDIOCINSERTXB:
29984 /* Store a xb struct to be pushed onto fifo */
29985 SD_INFO(SD_LOG_SDTEST, un,
29986 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
29988 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29990 sd_fault_injection_on = 0;
29992 if (un->sd_fi_fifo_xb[i] != NULL) {
29993 kmem_free(un->sd_fi_fifo_xb[i],
29994 sizeof (struct sd_fi_xb));
29995 un->sd_fi_fifo_xb[i] = NULL;
29997 if (arg != (intptr_t)NULL) {
29998 un->sd_fi_fifo_xb[i] =
29999 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30000 if (un->sd_fi_fifo_xb[i] == NULL) {
30001 /* Alloc failed don't store anything */
30002 break;
30004 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30005 sizeof (struct sd_fi_xb), 0);
30007 if (rval == -1) {
30008 kmem_free(un->sd_fi_fifo_xb[i],
30009 sizeof (struct sd_fi_xb));
30010 un->sd_fi_fifo_xb[i] = NULL;
30012 } else {
30013 SD_INFO(SD_LOG_IOERR, un,
30014 "sd_faultinjection_ioctl: xb null\n");
30016 break;
30018 case SDIOCINSERTUN:
30019 /* Store a un struct to be pushed onto fifo */
30020 SD_INFO(SD_LOG_SDTEST, un,
30021 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30023 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30025 sd_fault_injection_on = 0;
30027 if (un->sd_fi_fifo_un[i] != NULL) {
30028 kmem_free(un->sd_fi_fifo_un[i],
30029 sizeof (struct sd_fi_un));
30030 un->sd_fi_fifo_un[i] = NULL;
30032 if (arg != (intptr_t)NULL) {
30033 un->sd_fi_fifo_un[i] =
30034 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30035 if (un->sd_fi_fifo_un[i] == NULL) {
30036 /* Alloc failed don't store anything */
30037 break;
30039 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30040 sizeof (struct sd_fi_un), 0);
30041 if (rval == -1) {
30042 kmem_free(un->sd_fi_fifo_un[i],
30043 sizeof (struct sd_fi_un));
30044 un->sd_fi_fifo_un[i] = NULL;
30047 } else {
30048 SD_INFO(SD_LOG_IOERR, un,
30049 "sd_faultinjection_ioctl: un null\n");
30052 break;
30054 case SDIOCINSERTARQ:
30055 /* Store a arq struct to be pushed onto fifo */
30056 SD_INFO(SD_LOG_SDTEST, un,
30057 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30058 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30060 sd_fault_injection_on = 0;
30062 if (un->sd_fi_fifo_arq[i] != NULL) {
30063 kmem_free(un->sd_fi_fifo_arq[i],
30064 sizeof (struct sd_fi_arq));
30065 un->sd_fi_fifo_arq[i] = NULL;
30067 if (arg != (intptr_t)NULL) {
30068 un->sd_fi_fifo_arq[i] =
30069 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30070 if (un->sd_fi_fifo_arq[i] == NULL) {
30071 /* Alloc failed don't store anything */
30072 break;
30074 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30075 sizeof (struct sd_fi_arq), 0);
30076 if (rval == -1) {
30077 kmem_free(un->sd_fi_fifo_arq[i],
30078 sizeof (struct sd_fi_arq));
30079 un->sd_fi_fifo_arq[i] = NULL;
30082 } else {
30083 SD_INFO(SD_LOG_IOERR, un,
30084 "sd_faultinjection_ioctl: arq null\n");
30087 break;
30089 case SDIOCPUSH:
30090 /* Push stored xb, pkt, un, and arq onto fifo */
30091 sd_fault_injection_on = 0;
30093 if (arg != (intptr_t)NULL) {
30094 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30095 if (rval != -1 &&
30096 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30097 un->sd_fi_fifo_end += i;
30099 } else {
30100 SD_INFO(SD_LOG_IOERR, un,
30101 "sd_faultinjection_ioctl: push arg null\n");
30102 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30103 un->sd_fi_fifo_end++;
30106 SD_INFO(SD_LOG_IOERR, un,
30107 "sd_faultinjection_ioctl: push to end=%d\n",
30108 un->sd_fi_fifo_end);
30109 break;
30111 case SDIOCRETRIEVE:
30112 /* Return buffer of log from Injection session */
30113 SD_INFO(SD_LOG_SDTEST, un,
30114 "sd_faultinjection_ioctl: Injecting Fault Retreive");
30116 sd_fault_injection_on = 0;
30118 mutex_enter(&(un->un_fi_mutex));
30119 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30120 un->sd_fi_buf_len+1, 0);
30121 mutex_exit(&(un->un_fi_mutex));
30123 if (rval == -1) {
30125 * arg is possibly invalid setting
30126 * it to NULL for return
30128 arg = (intptr_t)NULL;
30130 break;
30133 mutex_exit(SD_MUTEX(un));
30134 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30139 * Function: sd_injection_log()
30141 * Description: This routine adds buff to the already existing injection log
30142 * for retrieval via faultinjection_ioctl for use in fault
30143 * detection and recovery
30145 * Arguments: buf - the string to add to the log
30148 static void
30149 sd_injection_log(char *buf, struct sd_lun *un)
30151 uint_t len;
30153 ASSERT(un != NULL);
30154 ASSERT(buf != NULL);
30156 mutex_enter(&(un->un_fi_mutex));
30158 len = min(strlen(buf), 255);
30159 /* Add logged value to Injection log to be returned later */
30160 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30161 uint_t offset = strlen((char *)un->sd_fi_log);
30162 char *destp = (char *)un->sd_fi_log + offset;
30163 int i;
30164 for (i = 0; i < len; i++) {
30165 *destp++ = *buf++;
30167 un->sd_fi_buf_len += len;
30168 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30171 mutex_exit(&(un->un_fi_mutex));
30176 * Function: sd_faultinjection()
30178 * Description: This routine takes the pkt and changes its
30179 * content based on error injection scenerio.
30181 * Arguments: pktp - packet to be changed
30184 static void
30185 sd_faultinjection(struct scsi_pkt *pktp)
30187 uint_t i;
30188 struct sd_fi_pkt *fi_pkt;
30189 struct sd_fi_xb *fi_xb;
30190 struct sd_fi_un *fi_un;
30191 struct sd_fi_arq *fi_arq;
30192 struct buf *bp;
30193 struct sd_xbuf *xb;
30194 struct sd_lun *un;
30196 ASSERT(pktp != NULL);
30198 /* pull bp xb and un from pktp */
30199 bp = (struct buf *)pktp->pkt_private;
30200 xb = SD_GET_XBUF(bp);
30201 un = SD_GET_UN(bp);
30203 ASSERT(un != NULL);
30205 mutex_enter(SD_MUTEX(un));
30207 SD_TRACE(SD_LOG_SDTEST, un,
30208 "sd_faultinjection: entry Injection from sdintr\n");
30210 /* if injection is off return */
30211 if (sd_fault_injection_on == 0 ||
30212 un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30213 mutex_exit(SD_MUTEX(un));
30214 return;
30217 SD_INFO(SD_LOG_SDTEST, un,
30218 "sd_faultinjection: is working for copying\n");
30220 /* take next set off fifo */
30221 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30223 fi_pkt = un->sd_fi_fifo_pkt[i];
30224 fi_xb = un->sd_fi_fifo_xb[i];
30225 fi_un = un->sd_fi_fifo_un[i];
30226 fi_arq = un->sd_fi_fifo_arq[i];
30229 /* set variables accordingly */
30230 /* set pkt if it was on fifo */
30231 if (fi_pkt != NULL) {
30232 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30233 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30234 if (fi_pkt->pkt_cdbp != 0xff)
30235 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30236 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30237 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30238 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30241 /* set xb if it was on fifo */
30242 if (fi_xb != NULL) {
30243 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30244 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30245 if (fi_xb->xb_retry_count != 0)
30246 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30247 SD_CONDSET(xb, xb, xb_victim_retry_count,
30248 "xb_victim_retry_count");
30249 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30250 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30251 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30253 /* copy in block data from sense */
30255 * if (fi_xb->xb_sense_data[0] != -1) {
30256 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30257 * SENSE_LENGTH);
30260 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30262 /* copy in extended sense codes */
30263 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30264 xb, es_code, "es_code");
30265 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30266 xb, es_key, "es_key");
30267 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30268 xb, es_add_code, "es_add_code");
30269 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30270 xb, es_qual_code, "es_qual_code");
30271 struct scsi_extended_sense *esp;
30272 esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30273 esp->es_class = CLASS_EXTENDED_SENSE;
30276 /* set un if it was on fifo */
30277 if (fi_un != NULL) {
30278 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30279 SD_CONDSET(un, un, un_ctype, "un_ctype");
30280 SD_CONDSET(un, un, un_reset_retry_count,
30281 "un_reset_retry_count");
30282 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30283 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30284 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30285 SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30286 "un_f_allow_bus_device_reset");
30287 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30291 /* copy in auto request sense if it was on fifo */
30292 if (fi_arq != NULL) {
30293 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30296 /* free structs */
30297 if (un->sd_fi_fifo_pkt[i] != NULL) {
30298 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30300 if (un->sd_fi_fifo_xb[i] != NULL) {
30301 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30303 if (un->sd_fi_fifo_un[i] != NULL) {
30304 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30306 if (un->sd_fi_fifo_arq[i] != NULL) {
30307 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30311 * kmem_free does not gurantee to set to NULL
30312 * since we uses these to determine if we set
30313 * values or not lets confirm they are always
30314 * NULL after free
30316 un->sd_fi_fifo_pkt[i] = NULL;
30317 un->sd_fi_fifo_un[i] = NULL;
30318 un->sd_fi_fifo_xb[i] = NULL;
30319 un->sd_fi_fifo_arq[i] = NULL;
30321 un->sd_fi_fifo_start++;
30323 mutex_exit(SD_MUTEX(un));
30325 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30328 #endif /* SD_FAULT_INJECTION */
30331 * This routine is invoked in sd_unit_attach(). Before calling it, the
30332 * properties in conf file should be processed already, and "hotpluggable"
30333 * property was processed also.
30335 * The sd driver distinguishes 3 different type of devices: removable media,
30336 * non-removable media, and hotpluggable. Below the differences are defined:
30338 * 1. Device ID
30340 * The device ID of a device is used to identify this device. Refer to
30341 * ddi_devid_register(9F).
30343 * For a non-removable media disk device which can provide 0x80 or 0x83
30344 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30345 * device ID is created to identify this device. For other non-removable
30346 * media devices, a default device ID is created only if this device has
30347 * at least 2 alter cylinders. Otherwise, this device has no devid.
30349 * -------------------------------------------------------
30350 * removable media hotpluggable | Can Have Device ID
30351 * -------------------------------------------------------
30352 * false false | Yes
30353 * false true | Yes
30354 * true x | No
30355 * ------------------------------------------------------
30358 * 2. SCSI group 4 commands
30360 * In SCSI specs, only some commands in group 4 command set can use
30361 * 8-byte addresses that can be used to access >2TB storage spaces.
30362 * Other commands have no such capability. Without supporting group4,
30363 * it is impossible to make full use of storage spaces of a disk with
30364 * capacity larger than 2TB.
30366 * -----------------------------------------------
30367 * removable media hotpluggable LP64 | Group
30368 * -----------------------------------------------
30369 * false false false | 1
30370 * false false true | 4
30371 * false true false | 1
30372 * false true true | 4
30373 * true x x | 5
30374 * -----------------------------------------------
30377 * 3. Check for VTOC Label
30379 * If a direct-access disk has no EFI label, sd will check if it has a
30380 * valid VTOC label. Now, sd also does that check for removable media
30381 * and hotpluggable devices.
30383 * --------------------------------------------------------------
30384 * Direct-Access removable media hotpluggable | Check Label
30385 * -------------------------------------------------------------
30386 * false false false | No
30387 * false false true | No
30388 * false true false | Yes
30389 * false true true | Yes
30390 * true x x | Yes
30391 * --------------------------------------------------------------
30394 * 4. Building default VTOC label
30396 * As section 3 says, sd checks if some kinds of devices have VTOC label.
30397 * If those devices have no valid VTOC label, sd(7d) will attempt to
30398 * create default VTOC for them. Currently sd creates default VTOC label
30399 * for all devices on x86 platform (VTOC_16), but only for removable
30400 * media devices on SPARC (VTOC_8).
30402 * -----------------------------------------------------------
30403 * removable media hotpluggable platform | Default Label
30404 * -----------------------------------------------------------
30405 * false false sparc | No
30406 * false true x86 | Yes
30407 * false true sparc | Yes
30408 * true x x | Yes
30409 * ----------------------------------------------------------
30412 * 5. Supported blocksizes of target devices
30414 * Sd supports non-512-byte blocksize for removable media devices only.
30415 * For other devices, only 512-byte blocksize is supported. This may be
30416 * changed in near future because some RAID devices require non-512-byte
30417 * blocksize
30419 * -----------------------------------------------------------
30420 * removable media hotpluggable | non-512-byte blocksize
30421 * -----------------------------------------------------------
30422 * false false | No
30423 * false true | No
30424 * true x | Yes
30425 * -----------------------------------------------------------
30428 * 6. Automatic mount & unmount
30430 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30431 * if a device is removable media device. It return 1 for removable media
30432 * devices, and 0 for others.
30434 * The automatic mounting subsystem should distinguish between the types
30435 * of devices and apply automounting policies to each.
30438 * 7. fdisk partition management
30440 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30441 * just supports fdisk partitions on x86 platform. On sparc platform, sd
30442 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30443 * fdisk partitions on both x86 and SPARC platform.
30445 * -----------------------------------------------------------
30446 * platform removable media USB/1394 | fdisk supported
30447 * -----------------------------------------------------------
30448 * x86 X X | true
30449 * ------------------------------------------------------------
30450 * sparc X X | false
30451 * ------------------------------------------------------------
30454 * 8. MBOOT/MBR
30456 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30457 * read/write mboot for removable media devices on sparc platform.
30459 * -----------------------------------------------------------
30460 * platform removable media USB/1394 | mboot supported
30461 * -----------------------------------------------------------
30462 * x86 X X | true
30463 * ------------------------------------------------------------
30464 * sparc false false | false
30465 * sparc false true | true
30466 * sparc true false | true
30467 * sparc true true | true
30468 * ------------------------------------------------------------
30471 * 9. error handling during opening device
30473 * If failed to open a disk device, an errno is returned. For some kinds
30474 * of errors, different errno is returned depending on if this device is
30475 * a removable media device. This brings USB/1394 hard disks in line with
30476 * expected hard disk behavior. It is not expected that this breaks any
30477 * application.
30479 * ------------------------------------------------------
30480 * removable media hotpluggable | errno
30481 * ------------------------------------------------------
30482 * false false | EIO
30483 * false true | EIO
30484 * true x | ENXIO
30485 * ------------------------------------------------------
30488 * 11. ioctls: DKIOCEJECT, CDROMEJECT
30490 * These IOCTLs are applicable only to removable media devices.
30492 * -----------------------------------------------------------
30493 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT
30494 * -----------------------------------------------------------
30495 * false false | No
30496 * false true | No
30497 * true x | Yes
30498 * -----------------------------------------------------------
30501 * 12. Kstats for partitions
30503 * sd creates partition kstat for non-removable media devices. USB and
30504 * Firewire hard disks now have partition kstats
30506 * ------------------------------------------------------
30507 * removable media hotpluggable | kstat
30508 * ------------------------------------------------------
30509 * false false | Yes
30510 * false true | Yes
30511 * true x | No
30512 * ------------------------------------------------------
30515 * 13. Removable media & hotpluggable properties
30517 * Sd driver creates a "removable-media" property for removable media
30518 * devices. Parent nexus drivers create a "hotpluggable" property if
30519 * it supports hotplugging.
30521 * ---------------------------------------------------------------------
30522 * removable media hotpluggable | "removable-media" " hotpluggable"
30523 * ---------------------------------------------------------------------
30524 * false false | No No
30525 * false true | No Yes
30526 * true false | Yes No
30527 * true true | Yes Yes
30528 * ---------------------------------------------------------------------
30531 * 14. Power Management
30533 * sd only power manages removable media devices or devices that support
30534 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250)
30536 * A parent nexus that supports hotplugging can also set "pm-capable"
30537 * if the disk can be power managed.
30539 * ------------------------------------------------------------
30540 * removable media hotpluggable pm-capable | power manage
30541 * ------------------------------------------------------------
30542 * false false false | No
30543 * false false true | Yes
30544 * false true false | No
30545 * false true true | Yes
30546 * true x x | Yes
30547 * ------------------------------------------------------------
30549 * USB and firewire hard disks can now be power managed independently
30550 * of the framebuffer
30553 * 15. Support for USB disks with capacity larger than 1TB
30555 * Currently, sd doesn't permit a fixed disk device with capacity
30556 * larger than 1TB to be used in a 32-bit operating system environment.
30557 * However, sd doesn't do that for removable media devices. Instead, it
30558 * assumes that removable media devices cannot have a capacity larger
30559 * than 1TB. Therefore, using those devices on 32-bit system is partially
30560 * supported, which can cause some unexpected results.
30562 * ---------------------------------------------------------------------
30563 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env
30564 * ---------------------------------------------------------------------
30565 * false false | true | no
30566 * false true | true | no
30567 * true false | true | Yes
30568 * true true | true | Yes
30569 * ---------------------------------------------------------------------
30572 * 16. Check write-protection at open time
30574 * When a removable media device is being opened for writing without NDELAY
30575 * flag, sd will check if this device is writable. If attempting to open
30576 * without NDELAY flag a write-protected device, this operation will abort.
30578 * ------------------------------------------------------------
30579 * removable media USB/1394 | WP Check
30580 * ------------------------------------------------------------
30581 * false false | No
30582 * false true | No
30583 * true false | Yes
30584 * true true | Yes
30585 * ------------------------------------------------------------
30588 * 17. syslog when corrupted VTOC is encountered
30590 * Currently, if an invalid VTOC is encountered, sd only print syslog
30591 * for fixed SCSI disks.
30592 * ------------------------------------------------------------
30593 * removable media USB/1394 | print syslog
30594 * ------------------------------------------------------------
30595 * false false | Yes
30596 * false true | No
30597 * true false | No
30598 * true true | No
30599 * ------------------------------------------------------------
30601 static void
30602 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30604 int pm_cap;
30606 ASSERT(un->un_sd);
30607 ASSERT(un->un_sd->sd_inq);
30610 * Enable SYNC CACHE support for all devices.
30612 un->un_f_sync_cache_supported = TRUE;
30615 * Set the sync cache required flag to false.
30616 * This would ensure that there is no SYNC CACHE
30617 * sent when there are no writes
30619 un->un_f_sync_cache_required = FALSE;
30621 if (un->un_sd->sd_inq->inq_rmb) {
30623 * The media of this device is removable. And for this kind
30624 * of devices, it is possible to change medium after opening
30625 * devices. Thus we should support this operation.
30627 un->un_f_has_removable_media = TRUE;
30630 * support non-512-byte blocksize of removable media devices
30632 un->un_f_non_devbsize_supported = TRUE;
30635 * Assume that all removable media devices support DOOR_LOCK
30637 un->un_f_doorlock_supported = TRUE;
30640 * For a removable media device, it is possible to be opened
30641 * with NDELAY flag when there is no media in drive, in this
30642 * case we don't care if device is writable. But if without
30643 * NDELAY flag, we need to check if media is write-protected.
30645 un->un_f_chk_wp_open = TRUE;
30648 * need to start a SCSI watch thread to monitor media state,
30649 * when media is being inserted or ejected, notify syseventd.
30651 un->un_f_monitor_media_state = TRUE;
30654 * Some devices don't support START_STOP_UNIT command.
30655 * Therefore, we'd better check if a device supports it
30656 * before sending it.
30658 un->un_f_check_start_stop = TRUE;
30661 * support eject media ioctl:
30662 * FDEJECT, DKIOCEJECT, CDROMEJECT
30664 un->un_f_eject_media_supported = TRUE;
30667 * Because many removable-media devices don't support
30668 * LOG_SENSE, we couldn't use this command to check if
30669 * a removable media device support power-management.
30670 * We assume that they support power-management via
30671 * START_STOP_UNIT command and can be spun up and down
30672 * without limitations.
30674 un->un_f_pm_supported = TRUE;
30677 * Need to create a zero length (Boolean) property
30678 * removable-media for the removable media devices.
30679 * Note that the return value of the property is not being
30680 * checked, since if unable to create the property
30681 * then do not want the attach to fail altogether. Consistent
30682 * with other property creation in attach.
30684 (void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30685 DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30687 } else {
30689 * create device ID for device
30691 un->un_f_devid_supported = TRUE;
30694 * Spin up non-removable-media devices once it is attached
30696 un->un_f_attach_spinup = TRUE;
30699 * According to SCSI specification, Sense data has two kinds of
30700 * format: fixed format, and descriptor format. At present, we
30701 * don't support descriptor format sense data for removable
30702 * media.
30704 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30705 un->un_f_descr_format_supported = TRUE;
30709 * kstats are created only for non-removable media devices.
30711 * Set this in sd.conf to 0 in order to disable kstats. The
30712 * default is 1, so they are enabled by default.
30714 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30715 SD_DEVINFO(un), DDI_PROP_DONTPASS,
30716 "enable-partition-kstats", 1));
30719 * Check if HBA has set the "pm-capable" property.
30720 * If "pm-capable" exists and is non-zero then we can
30721 * power manage the device without checking the start/stop
30722 * cycle count log sense page.
30724 * If "pm-capable" exists and is set to be false (0),
30725 * then we should not power manage the device.
30727 * If "pm-capable" doesn't exist then pm_cap will
30728 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case,
30729 * sd will check the start/stop cycle count log sense page
30730 * and power manage the device if the cycle count limit has
30731 * not been exceeded.
30733 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30734 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30735 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30736 un->un_f_log_sense_supported = TRUE;
30737 if (!un->un_f_power_condition_disabled &&
30738 SD_INQUIRY(un)->inq_ansi == 6) {
30739 un->un_f_power_condition_supported = TRUE;
30741 } else {
30743 * pm-capable property exists.
30745 * Convert "TRUE" values for pm_cap to
30746 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30747 * later. "TRUE" values are any values defined in
30748 * inquiry.h.
30750 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30751 un->un_f_log_sense_supported = FALSE;
30752 } else {
30753 /* SD_PM_CAPABLE_IS_TRUE case */
30754 un->un_f_pm_supported = TRUE;
30755 if (!un->un_f_power_condition_disabled &&
30756 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30757 un->un_f_power_condition_supported =
30758 TRUE;
30760 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30761 un->un_f_log_sense_supported = TRUE;
30762 un->un_f_pm_log_sense_smart =
30763 SD_PM_CAP_SMART_LOG(pm_cap);
30767 SD_INFO(SD_LOG_ATTACH_DETACH, un,
30768 "sd_unit_attach: un:0x%p pm-capable "
30769 "property set to %d.\n", un, un->un_f_pm_supported);
30773 if (un->un_f_is_hotpluggable) {
30776 * Have to watch hotpluggable devices as well, since
30777 * that's the only way for userland applications to
30778 * detect hot removal while device is busy/mounted.
30780 un->un_f_monitor_media_state = TRUE;
30782 un->un_f_check_start_stop = TRUE;
30788 * sd_tg_rdwr:
30789 * Provides rdwr access for cmlb via sd_tgops. The start_block is
30790 * in sys block size, req_length in bytes.
30793 static int
30794 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30795 diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30797 struct sd_lun *un;
30798 int path_flag = (int)(uintptr_t)tg_cookie;
30799 char *dkl = NULL;
30800 diskaddr_t real_addr = start_block;
30801 diskaddr_t first_byte, end_block;
30803 size_t buffer_size = reqlength;
30804 int rval = 0;
30805 diskaddr_t cap;
30806 uint32_t lbasize;
30807 sd_ssc_t *ssc;
30809 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30810 if (un == NULL)
30811 return (ENXIO);
30813 if (cmd != TG_READ && cmd != TG_WRITE)
30814 return (EINVAL);
30816 ssc = sd_ssc_init(un);
30817 mutex_enter(SD_MUTEX(un));
30818 if (un->un_f_tgt_blocksize_is_valid == FALSE) {
30819 mutex_exit(SD_MUTEX(un));
30820 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30821 &lbasize, path_flag);
30822 if (rval != 0)
30823 goto done1;
30824 mutex_enter(SD_MUTEX(un));
30825 sd_update_block_info(un, lbasize, cap);
30826 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
30827 mutex_exit(SD_MUTEX(un));
30828 rval = EIO;
30829 goto done;
30833 if (NOT_DEVBSIZE(un)) {
30835 * sys_blocksize != tgt_blocksize, need to re-adjust
30836 * blkno and save the index to beginning of dk_label
30838 first_byte = SD_SYSBLOCKS2BYTES(start_block);
30839 real_addr = first_byte / un->un_tgt_blocksize;
30841 end_block = (first_byte + reqlength +
30842 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
30844 /* round up buffer size to multiple of target block size */
30845 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
30847 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
30848 "label_addr: 0x%x allocation size: 0x%x\n",
30849 real_addr, buffer_size);
30851 if (((first_byte % un->un_tgt_blocksize) != 0) ||
30852 (reqlength % un->un_tgt_blocksize) != 0)
30853 /* the request is not aligned */
30854 dkl = kmem_zalloc(buffer_size, KM_SLEEP);
30858 * The MMC standard allows READ CAPACITY to be
30859 * inaccurate by a bounded amount (in the interest of
30860 * response latency). As a result, failed READs are
30861 * commonplace (due to the reading of metadata and not
30862 * data). Depending on the per-Vendor/drive Sense data,
30863 * the failed READ can cause many (unnecessary) retries.
30866 if (ISCD(un) && (cmd == TG_READ) &&
30867 (un->un_f_blockcount_is_valid == TRUE) &&
30868 ((start_block == (un->un_blockcount - 1))||
30869 (start_block == (un->un_blockcount - 2)))) {
30870 path_flag = SD_PATH_DIRECT_PRIORITY;
30873 mutex_exit(SD_MUTEX(un));
30874 if (cmd == TG_READ) {
30875 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
30876 buffer_size, real_addr, path_flag);
30877 if (dkl != NULL)
30878 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
30879 real_addr), bufaddr, reqlength);
30880 } else {
30881 if (dkl) {
30882 rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
30883 real_addr, path_flag);
30884 if (rval) {
30885 goto done1;
30887 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
30888 real_addr), reqlength);
30890 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
30891 buffer_size, real_addr, path_flag);
30894 done1:
30895 if (dkl != NULL)
30896 kmem_free(dkl, buffer_size);
30898 if (rval != 0) {
30899 if (rval == EIO)
30900 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
30901 else
30902 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
30904 done:
30905 sd_ssc_fini(ssc);
30906 return (rval);
30910 static int
30911 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
30914 struct sd_lun *un;
30915 diskaddr_t cap;
30916 uint32_t lbasize;
30917 int path_flag = (int)(uintptr_t)tg_cookie;
30918 int ret = 0;
30920 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30921 if (un == NULL)
30922 return (ENXIO);
30924 switch (cmd) {
30925 case TG_GETPHYGEOM:
30926 case TG_GETVIRTGEOM:
30927 case TG_GETCAPACITY:
30928 case TG_GETBLOCKSIZE:
30929 mutex_enter(SD_MUTEX(un));
30931 if ((un->un_f_blockcount_is_valid == TRUE) &&
30932 (un->un_f_tgt_blocksize_is_valid == TRUE)) {
30933 cap = un->un_blockcount;
30934 lbasize = un->un_tgt_blocksize;
30935 mutex_exit(SD_MUTEX(un));
30936 } else {
30937 sd_ssc_t *ssc;
30938 mutex_exit(SD_MUTEX(un));
30939 ssc = sd_ssc_init(un);
30940 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30941 &lbasize, path_flag);
30942 if (ret != 0) {
30943 if (ret == EIO)
30944 sd_ssc_assessment(ssc,
30945 SD_FMT_STATUS_CHECK);
30946 else
30947 sd_ssc_assessment(ssc,
30948 SD_FMT_IGNORE);
30949 sd_ssc_fini(ssc);
30950 return (ret);
30952 sd_ssc_fini(ssc);
30953 mutex_enter(SD_MUTEX(un));
30954 sd_update_block_info(un, lbasize, cap);
30955 if ((un->un_f_blockcount_is_valid == FALSE) ||
30956 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
30957 mutex_exit(SD_MUTEX(un));
30958 return (EIO);
30960 mutex_exit(SD_MUTEX(un));
30963 if (cmd == TG_GETCAPACITY) {
30964 *(diskaddr_t *)arg = cap;
30965 return (0);
30968 if (cmd == TG_GETBLOCKSIZE) {
30969 *(uint32_t *)arg = lbasize;
30970 return (0);
30973 if (cmd == TG_GETPHYGEOM)
30974 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
30975 cap, lbasize, path_flag);
30976 else
30977 /* TG_GETVIRTGEOM */
30978 ret = sd_get_virtual_geometry(un,
30979 (cmlb_geom_t *)arg, cap, lbasize);
30981 return (ret);
30983 case TG_GETATTR:
30984 mutex_enter(SD_MUTEX(un));
30985 ((tg_attribute_t *)arg)->media_is_writable =
30986 un->un_f_mmc_writable_media;
30987 ((tg_attribute_t *)arg)->media_is_solid_state =
30988 un->un_f_is_solid_state;
30989 ((tg_attribute_t *)arg)->media_is_rotational =
30990 un->un_f_is_rotational;
30991 mutex_exit(SD_MUTEX(un));
30992 return (0);
30993 default:
30994 return (ENOTTY);
31000 * Function: sd_ssc_ereport_post
31002 * Description: Will be called when SD driver need to post an ereport.
31004 * Context: Kernel thread or interrupt context.
31007 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31009 static void
31010 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31012 int uscsi_path_instance = 0;
31013 uchar_t uscsi_pkt_reason;
31014 uint32_t uscsi_pkt_state;
31015 uint32_t uscsi_pkt_statistics;
31016 uint64_t uscsi_ena;
31017 uchar_t op_code;
31018 uint8_t *sensep;
31019 union scsi_cdb *cdbp;
31020 uint_t cdblen = 0;
31021 uint_t senlen = 0;
31022 struct sd_lun *un;
31023 dev_info_t *dip;
31024 char *devid;
31025 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31026 SSC_FLAGS_INVALID_STATUS |
31027 SSC_FLAGS_INVALID_SENSE |
31028 SSC_FLAGS_INVALID_DATA;
31029 char assessment[16];
31031 ASSERT(ssc != NULL);
31032 ASSERT(ssc->ssc_uscsi_cmd != NULL);
31033 ASSERT(ssc->ssc_uscsi_info != NULL);
31035 un = ssc->ssc_un;
31036 ASSERT(un != NULL);
31038 dip = un->un_sd->sd_dev;
31041 * Get the devid:
31042 * devid will only be passed to non-transport error reports.
31044 devid = DEVI(dip)->devi_devid_str;
31047 * If we are syncing or dumping, the command will not be executed
31048 * so we bypass this situation.
31050 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31051 (un->un_state == SD_STATE_DUMPING))
31052 return;
31054 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31055 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31056 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31057 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31058 uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31060 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31061 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31063 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31064 if (cdbp == NULL) {
31065 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31066 "sd_ssc_ereport_post meet empty cdb\n");
31067 return;
31070 op_code = cdbp->scc_cmd;
31072 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31073 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31074 ssc->ssc_uscsi_cmd->uscsi_rqresid);
31076 if (senlen > 0)
31077 ASSERT(sensep != NULL);
31080 * Initialize drv_assess to corresponding values.
31081 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31082 * on the sense-key returned back.
31084 switch (drv_assess) {
31085 case SD_FM_DRV_RECOVERY:
31086 (void) sprintf(assessment, "%s", "recovered");
31087 break;
31088 case SD_FM_DRV_RETRY:
31089 (void) sprintf(assessment, "%s", "retry");
31090 break;
31091 case SD_FM_DRV_NOTICE:
31092 (void) sprintf(assessment, "%s", "info");
31093 break;
31094 case SD_FM_DRV_FATAL:
31095 default:
31096 (void) sprintf(assessment, "%s", "unknown");
31099 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31100 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31101 * driver-assessment will always be "recovered" here.
31103 if (drv_assess == SD_FM_DRV_RECOVERY) {
31104 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31105 "cmd.disk.recovered", uscsi_ena, devid, NULL,
31106 DDI_NOSLEEP, NULL,
31107 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31108 DEVID_IF_KNOWN(devid),
31109 "driver-assessment", DATA_TYPE_STRING, assessment,
31110 "op-code", DATA_TYPE_UINT8, op_code,
31111 "cdb", DATA_TYPE_UINT8_ARRAY,
31112 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31113 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31114 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31115 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31116 NULL);
31117 return;
31121 * If there is un-expected/un-decodable data, we should post
31122 * ereport.io.scsi.cmd.disk.dev.uderr.
31123 * driver-assessment will be set based on parameter drv_assess.
31124 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31125 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31126 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31127 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31129 if (ssc->ssc_flags & ssc_invalid_flags) {
31130 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31131 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31132 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31133 NULL, DDI_NOSLEEP, NULL,
31134 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31135 DEVID_IF_KNOWN(devid),
31136 "driver-assessment", DATA_TYPE_STRING,
31137 drv_assess == SD_FM_DRV_FATAL ?
31138 "fail" : assessment,
31139 "op-code", DATA_TYPE_UINT8, op_code,
31140 "cdb", DATA_TYPE_UINT8_ARRAY,
31141 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31142 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31143 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31144 "pkt-stats", DATA_TYPE_UINT32,
31145 uscsi_pkt_statistics,
31146 "stat-code", DATA_TYPE_UINT8,
31147 ssc->ssc_uscsi_cmd->uscsi_status,
31148 "un-decode-info", DATA_TYPE_STRING,
31149 ssc->ssc_info,
31150 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31151 senlen, sensep,
31152 NULL);
31153 } else {
31155 * For other type of invalid data, the
31156 * un-decode-value field would be empty because the
31157 * un-decodable content could be seen from upper
31158 * level payload or inside un-decode-info.
31160 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31161 NULL,
31162 "cmd.disk.dev.uderr", uscsi_ena, devid,
31163 NULL, DDI_NOSLEEP, NULL,
31164 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31165 DEVID_IF_KNOWN(devid),
31166 "driver-assessment", DATA_TYPE_STRING,
31167 drv_assess == SD_FM_DRV_FATAL ?
31168 "fail" : assessment,
31169 "op-code", DATA_TYPE_UINT8, op_code,
31170 "cdb", DATA_TYPE_UINT8_ARRAY,
31171 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31172 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31173 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31174 "pkt-stats", DATA_TYPE_UINT32,
31175 uscsi_pkt_statistics,
31176 "stat-code", DATA_TYPE_UINT8,
31177 ssc->ssc_uscsi_cmd->uscsi_status,
31178 "un-decode-info", DATA_TYPE_STRING,
31179 ssc->ssc_info,
31180 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31181 0, NULL,
31182 NULL);
31184 ssc->ssc_flags &= ~ssc_invalid_flags;
31185 return;
31188 if (uscsi_pkt_reason != CMD_CMPLT ||
31189 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31191 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31192 * set inside sd_start_cmds due to errors(bad packet or
31193 * fatal transport error), we should take it as a
31194 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31195 * driver-assessment will be set based on drv_assess.
31196 * We will set devid to NULL because it is a transport
31197 * error.
31199 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31200 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31202 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31203 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31204 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31205 DEVID_IF_KNOWN(devid),
31206 "driver-assessment", DATA_TYPE_STRING,
31207 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31208 "op-code", DATA_TYPE_UINT8, op_code,
31209 "cdb", DATA_TYPE_UINT8_ARRAY,
31210 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31211 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31212 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31213 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31214 NULL);
31215 } else {
31217 * If we got here, we have a completed command, and we need
31218 * to further investigate the sense data to see what kind
31219 * of ereport we should post.
31220 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31221 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31222 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31223 * KEY_MEDIUM_ERROR.
31224 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31225 * driver-assessment will be set based on the parameter
31226 * drv_assess.
31228 if (senlen > 0) {
31230 * Here we have sense data available.
31232 uint8_t sense_key = scsi_sense_key(sensep);
31233 uint8_t sense_asc = scsi_sense_asc(sensep);
31234 uint8_t sense_ascq = scsi_sense_ascq(sensep);
31236 if (sense_key == KEY_RECOVERABLE_ERROR &&
31237 sense_asc == 0x00 && sense_ascq == 0x1d)
31238 return;
31240 if (sense_key == KEY_MEDIUM_ERROR) {
31242 * driver-assessment should be "fatal" if
31243 * drv_assess is SD_FM_DRV_FATAL.
31245 scsi_fm_ereport_post(un->un_sd,
31246 uscsi_path_instance, NULL,
31247 "cmd.disk.dev.rqs.merr",
31248 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31249 FM_VERSION, DATA_TYPE_UINT8,
31250 FM_EREPORT_VERS0,
31251 DEVID_IF_KNOWN(devid),
31252 "driver-assessment",
31253 DATA_TYPE_STRING,
31254 drv_assess == SD_FM_DRV_FATAL ?
31255 "fatal" : assessment,
31256 "op-code",
31257 DATA_TYPE_UINT8, op_code,
31258 "cdb",
31259 DATA_TYPE_UINT8_ARRAY, cdblen,
31260 ssc->ssc_uscsi_cmd->uscsi_cdb,
31261 "pkt-reason",
31262 DATA_TYPE_UINT8, uscsi_pkt_reason,
31263 "pkt-state",
31264 DATA_TYPE_UINT8, uscsi_pkt_state,
31265 "pkt-stats",
31266 DATA_TYPE_UINT32,
31267 uscsi_pkt_statistics,
31268 "stat-code",
31269 DATA_TYPE_UINT8,
31270 ssc->ssc_uscsi_cmd->uscsi_status,
31271 "key",
31272 DATA_TYPE_UINT8,
31273 scsi_sense_key(sensep),
31274 "asc",
31275 DATA_TYPE_UINT8,
31276 scsi_sense_asc(sensep),
31277 "ascq",
31278 DATA_TYPE_UINT8,
31279 scsi_sense_ascq(sensep),
31280 "sense-data",
31281 DATA_TYPE_UINT8_ARRAY,
31282 senlen, sensep,
31283 "lba",
31284 DATA_TYPE_UINT64,
31285 ssc->ssc_uscsi_info->ui_lba,
31286 NULL);
31287 } else {
31289 * if sense-key == 0x4(hardware
31290 * error), driver-assessment should
31291 * be "fatal" if drv_assess is
31292 * SD_FM_DRV_FATAL.
31294 scsi_fm_ereport_post(un->un_sd,
31295 uscsi_path_instance, NULL,
31296 "cmd.disk.dev.rqs.derr",
31297 uscsi_ena, devid,
31298 NULL, DDI_NOSLEEP, NULL,
31299 FM_VERSION,
31300 DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31301 DEVID_IF_KNOWN(devid),
31302 "driver-assessment",
31303 DATA_TYPE_STRING,
31304 drv_assess == SD_FM_DRV_FATAL ?
31305 (sense_key == 0x4 ?
31306 "fatal" : "fail") : assessment,
31307 "op-code",
31308 DATA_TYPE_UINT8, op_code,
31309 "cdb",
31310 DATA_TYPE_UINT8_ARRAY, cdblen,
31311 ssc->ssc_uscsi_cmd->uscsi_cdb,
31312 "pkt-reason",
31313 DATA_TYPE_UINT8, uscsi_pkt_reason,
31314 "pkt-state",
31315 DATA_TYPE_UINT8, uscsi_pkt_state,
31316 "pkt-stats",
31317 DATA_TYPE_UINT32,
31318 uscsi_pkt_statistics,
31319 "stat-code",
31320 DATA_TYPE_UINT8,
31321 ssc->ssc_uscsi_cmd->uscsi_status,
31322 "key",
31323 DATA_TYPE_UINT8,
31324 scsi_sense_key(sensep),
31325 "asc",
31326 DATA_TYPE_UINT8,
31327 scsi_sense_asc(sensep),
31328 "ascq",
31329 DATA_TYPE_UINT8,
31330 scsi_sense_ascq(sensep),
31331 "sense-data",
31332 DATA_TYPE_UINT8_ARRAY,
31333 senlen, sensep,
31334 NULL);
31336 } else {
31338 * For stat_code == STATUS_GOOD, this is not a
31339 * hardware error.
31341 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31342 return;
31345 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31346 * stat-code but with sense data unavailable.
31347 * driver-assessment will be set based on parameter
31348 * drv_assess.
31350 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31351 NULL,
31352 "cmd.disk.dev.serr", uscsi_ena,
31353 devid, NULL, DDI_NOSLEEP, NULL,
31354 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31355 DEVID_IF_KNOWN(devid),
31356 "driver-assessment", DATA_TYPE_STRING,
31357 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31358 "op-code", DATA_TYPE_UINT8, op_code,
31359 "cdb",
31360 DATA_TYPE_UINT8_ARRAY,
31361 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31362 "pkt-reason",
31363 DATA_TYPE_UINT8, uscsi_pkt_reason,
31364 "pkt-state",
31365 DATA_TYPE_UINT8, uscsi_pkt_state,
31366 "pkt-stats",
31367 DATA_TYPE_UINT32, uscsi_pkt_statistics,
31368 "stat-code",
31369 DATA_TYPE_UINT8,
31370 ssc->ssc_uscsi_cmd->uscsi_status,
31371 NULL);
31377 * Function: sd_ssc_extract_info
31379 * Description: Extract information available to help generate ereport.
31381 * Context: Kernel thread or interrupt context.
31383 static void
31384 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31385 struct buf *bp, struct sd_xbuf *xp)
31387 size_t senlen = 0;
31388 union scsi_cdb *cdbp;
31389 int path_instance;
31391 * Need scsi_cdb_size array to determine the cdb length.
31393 extern uchar_t scsi_cdb_size[];
31395 ASSERT(un != NULL);
31396 ASSERT(pktp != NULL);
31397 ASSERT(bp != NULL);
31398 ASSERT(xp != NULL);
31399 ASSERT(ssc != NULL);
31400 ASSERT(mutex_owned(SD_MUTEX(un)));
31403 * Transfer the cdb buffer pointer here.
31405 cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31407 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31408 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31411 * Transfer the sense data buffer pointer if sense data is available,
31412 * calculate the sense data length first.
31414 if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31415 (xp->xb_sense_state & STATE_ARQ_DONE)) {
31417 * For arq case, we will enter here.
31419 if (xp->xb_sense_state & STATE_XARQ_DONE) {
31420 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31421 } else {
31422 senlen = SENSE_LENGTH;
31424 } else {
31426 * For non-arq case, we will enter this branch.
31428 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31429 (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31430 senlen = SENSE_LENGTH - xp->xb_sense_resid;
31435 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31436 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31437 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31439 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31442 * Only transfer path_instance when scsi_pkt was properly allocated.
31444 path_instance = pktp->pkt_path_instance;
31445 if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31446 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31447 else
31448 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31451 * Copy in the other fields we may need when posting ereport.
31453 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31454 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31455 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31456 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31459 * For partially read/write command, we will not create ena
31460 * in case of a successful command be reconized as recovered.
31462 if ((pktp->pkt_reason == CMD_CMPLT) &&
31463 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31464 (senlen == 0)) {
31465 return;
31469 * To associate ereports of a single command execution flow, we
31470 * need a shared ena for a specific command.
31472 if (xp->xb_ena == 0)
31473 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31474 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31479 * Function: sd_check_bdc_vpd
31481 * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31482 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31483 * RATE.
31485 * Set the following based on RPM value:
31486 * = 0 device is not solid state, non-rotational
31487 * = 1 device is solid state, non-rotational
31488 * > 1 device is not solid state, rotational
31490 * Context: Kernel thread or interrupt context.
31493 static void
31494 sd_check_bdc_vpd(sd_ssc_t *ssc)
31496 int rval = 0;
31497 uchar_t *inqb1 = NULL;
31498 size_t inqb1_len = MAX_INQUIRY_SIZE;
31499 size_t inqb1_resid = 0;
31500 struct sd_lun *un;
31502 ASSERT(ssc != NULL);
31503 un = ssc->ssc_un;
31504 ASSERT(un != NULL);
31505 ASSERT(!mutex_owned(SD_MUTEX(un)));
31507 mutex_enter(SD_MUTEX(un));
31508 un->un_f_is_rotational = TRUE;
31509 un->un_f_is_solid_state = FALSE;
31511 if (ISCD(un)) {
31512 mutex_exit(SD_MUTEX(un));
31513 return;
31516 if (sd_check_vpd_page_support(ssc) == 0 &&
31517 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31518 mutex_exit(SD_MUTEX(un));
31519 /* collect page b1 data */
31520 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31522 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31523 0x01, 0xB1, &inqb1_resid);
31525 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31526 SD_TRACE(SD_LOG_COMMON, un,
31527 "sd_check_bdc_vpd: \
31528 successfully get VPD page: %x \
31529 PAGE LENGTH: %x BYTE 4: %x \
31530 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31531 inqb1[5]);
31533 mutex_enter(SD_MUTEX(un));
31535 * Check the MEDIUM ROTATION RATE.
31537 if (inqb1[4] == 0) {
31538 if (inqb1[5] == 0) {
31539 un->un_f_is_rotational = FALSE;
31540 } else if (inqb1[5] == 1) {
31541 un->un_f_is_rotational = FALSE;
31542 un->un_f_is_solid_state = TRUE;
31544 * Solid state drives don't need
31545 * disksort.
31547 un->un_f_disksort_disabled = TRUE;
31550 mutex_exit(SD_MUTEX(un));
31551 } else if (rval != 0) {
31552 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31555 kmem_free(inqb1, inqb1_len);
31556 } else {
31557 mutex_exit(SD_MUTEX(un));
31562 * Function: sd_check_emulation_mode
31564 * Description: Check whether the SSD is at emulation mode
31565 * by issuing READ_CAPACITY_16 to see whether
31566 * we can get physical block size of the drive.
31568 * Context: Kernel thread or interrupt context.
31571 static void
31572 sd_check_emulation_mode(sd_ssc_t *ssc)
31574 int rval = 0;
31575 uint64_t capacity;
31576 uint_t lbasize;
31577 uint_t pbsize;
31578 int i;
31579 int devid_len;
31580 struct sd_lun *un;
31582 ASSERT(ssc != NULL);
31583 un = ssc->ssc_un;
31584 ASSERT(un != NULL);
31585 ASSERT(!mutex_owned(SD_MUTEX(un)));
31587 mutex_enter(SD_MUTEX(un));
31588 if (ISCD(un)) {
31589 mutex_exit(SD_MUTEX(un));
31590 return;
31593 if (un->un_f_descr_format_supported) {
31594 mutex_exit(SD_MUTEX(un));
31595 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31596 &pbsize, SD_PATH_DIRECT);
31597 mutex_enter(SD_MUTEX(un));
31599 if (rval != 0) {
31600 un->un_phy_blocksize = DEV_BSIZE;
31601 } else {
31602 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31603 un->un_phy_blocksize = DEV_BSIZE;
31604 } else if (pbsize > un->un_phy_blocksize) {
31606 * Don't reset the physical blocksize
31607 * unless we've detected a larger value.
31609 un->un_phy_blocksize = pbsize;
31614 for (i = 0; i < sd_flash_dev_table_size; i++) {
31615 devid_len = (int)strlen(sd_flash_dev_table[i]);
31616 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31617 == SD_SUCCESS) {
31618 un->un_phy_blocksize = SSD_SECSIZE;
31619 if (un->un_f_is_solid_state &&
31620 un->un_phy_blocksize != un->un_tgt_blocksize)
31621 un->un_f_enable_rmw = TRUE;
31625 mutex_exit(SD_MUTEX(un));