2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2011 Spectra Logic Corporation
4 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
32 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
35 * CAM Target Layer error reporting routines.
37 * Author: Ken Merry <ken@FreeBSD.org>
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/types.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/stddef.h>
52 #include <sys/ctype.h>
53 #include <sys/sysctl.h>
54 #include <machine/stdarg.h>
56 #include <cam/scsi/scsi_all.h>
57 #include <cam/scsi/scsi_da.h>
58 #include <cam/ctl/ctl_io.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_frontend.h>
61 #include <cam/ctl/ctl_backend.h>
62 #include <cam/ctl/ctl_ioctl.h>
63 #include <cam/ctl/ctl_error.h>
64 #include <cam/ctl/ctl_ha.h>
65 #include <cam/ctl/ctl_private.h>
68 ctl_set_sense_data_va(struct scsi_sense_data *sense_data, void *lunptr,
69 scsi_sense_data_type sense_format, int current_error,
70 int sense_key, int asc, int ascq, va_list ap)
74 lun = (struct ctl_lun *)lunptr;
77 * Determine whether to return fixed or descriptor format sense
80 if (sense_format == SSD_TYPE_NONE) {
82 * If the format isn't specified, we only return descriptor
83 * sense if the LUN exists and descriptor sense is turned
86 if ((lun != NULL) && (lun->MODE_CTRL.rlec & SCP_DSENSE))
87 sense_format = SSD_TYPE_DESC;
89 sense_format = SSD_TYPE_FIXED;
92 scsi_set_sense_data_va(sense_data, sense_format, current_error,
93 sense_key, asc, ascq, ap);
97 ctl_set_sense_data(struct scsi_sense_data *sense_data, void *lunptr,
98 scsi_sense_data_type sense_format, int current_error,
99 int sense_key, int asc, int ascq, ...)
104 ctl_set_sense_data_va(sense_data, lunptr, sense_format, current_error,
105 sense_key, asc, ascq, ap);
110 ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
111 int asc, int ascq, ...)
117 * The LUN can't go away until all of the commands have been
118 * completed. Therefore we can safely access the LUN structure and
119 * flags without the lock.
121 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
124 ctl_set_sense_data_va(&ctsio->sense_data,
134 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
135 ctsio->sense_len = SSD_FULL_SIZE;
136 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
140 * Transform fixed sense data into descriptor sense data.
142 * For simplicity's sake, we assume that both sense structures are
143 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
146 ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
147 struct scsi_sense_data_desc *sense_dest)
149 struct scsi_sense_stream stream_sense;
153 bzero(sense_dest, sizeof(*sense_dest));
155 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
160 bzero(&stream_sense, sizeof(stream_sense));
163 * Check to see whether any of the tape-specific bits are set. If
164 * so, we'll need a stream sense descriptor.
166 if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
167 stream_bits = sense_src->flags & ~SSD_KEY;
172 * Utilize our sense setting routine to do the transform. If a
173 * value is set in the fixed sense data, set it in the descriptor
174 * data. Otherwise, skip it.
176 ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
178 /*sense_format*/ SSD_TYPE_DESC,
180 /*sense_key*/ sense_src->flags & SSD_KEY,
181 /*asc*/ sense_src->add_sense_code,
182 /*ascq*/ sense_src->add_sense_code_qual,
184 /* Information Bytes */
185 (scsi_4btoul(sense_src->info) != 0) ?
186 SSD_ELEM_INFO : SSD_ELEM_SKIP,
187 sizeof(sense_src->info),
190 /* Command specific bytes */
191 (scsi_4btoul(sense_src->cmd_spec_info) != 0) ?
192 SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
193 sizeof(sense_src->cmd_spec_info),
194 sense_src->cmd_spec_info,
197 (sense_src->fru != 0) ?
198 SSD_ELEM_FRU : SSD_ELEM_SKIP,
199 sizeof(sense_src->fru),
202 /* Sense Key Specific */
203 (sense_src->sense_key_spec[0] & SSD_SCS_VALID) ?
204 SSD_ELEM_SKS : SSD_ELEM_SKIP,
205 sizeof(sense_src->sense_key_spec),
206 sense_src->sense_key_spec,
210 SSD_ELEM_STREAM : SSD_ELEM_SKIP,
218 * Transform descriptor format sense data into fixed sense data.
220 * Some data may be lost in translation, because there are descriptors
221 * thant can't be represented as fixed sense data.
223 * For simplicity's sake, we assume that both sense structures are
224 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
227 ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
228 struct scsi_sense_data_fixed *sense_dest)
231 uint8_t *info_ptr = NULL, *cmd_ptr = NULL, *fru_ptr = NULL;
232 uint8_t *sks_ptr = NULL, *stream_ptr = NULL;
233 int info_size = 0, cmd_size = 0, fru_size = 0;
234 int sks_size = 0, stream_size = 0;
237 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
242 for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
243 struct scsi_sense_desc_header *header;
245 header = (struct scsi_sense_desc_header *)
246 &sense_src->sense_desc[pos];
249 * See if this record goes past the end of the sense data.
250 * It shouldn't, but check just in case.
252 if ((pos + header->length + sizeof(*header)) >
253 sense_src->extra_len)
256 switch (sense_src->sense_desc[pos]) {
257 case SSD_DESC_INFO: {
258 struct scsi_sense_info *info;
260 info = (struct scsi_sense_info *)header;
262 info_ptr = info->info;
263 info_size = sizeof(info->info);
265 pos += info->length +
266 sizeof(struct scsi_sense_desc_header);
269 case SSD_DESC_COMMAND: {
270 struct scsi_sense_command *cmd;
272 cmd = (struct scsi_sense_command *)header;
273 cmd_ptr = cmd->command_info;
274 cmd_size = sizeof(cmd->command_info);
277 sizeof(struct scsi_sense_desc_header);
281 struct scsi_sense_fru *fru;
283 fru = (struct scsi_sense_fru *)header;
285 fru_size = sizeof(fru->fru);
287 sizeof(struct scsi_sense_desc_header);
291 struct scsi_sense_sks *sks;
293 sks = (struct scsi_sense_sks *)header;
294 sks_ptr = sks->sense_key_spec;
295 sks_size = sizeof(sks->sense_key_spec);
298 sizeof(struct scsi_sense_desc_header);
301 case SSD_DESC_STREAM: {
302 struct scsi_sense_stream *stream_sense;
304 stream_sense = (struct scsi_sense_stream *)header;
305 stream_ptr = &stream_sense->byte3;
306 stream_size = sizeof(stream_sense->byte3);
307 pos = stream_sense->length +
308 sizeof(struct scsi_sense_desc_header);
313 * We don't recognize this particular sense
314 * descriptor type, so just skip it.
316 pos += sizeof(*header) + header->length;
321 ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
323 /*sense_format*/ SSD_TYPE_FIXED,
325 /*sense_key*/ sense_src->sense_key & SSD_KEY,
326 /*asc*/ sense_src->add_sense_code,
327 /*ascq*/ sense_src->add_sense_code_qual,
329 /* Information Bytes */
330 (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
334 /* Command specific bytes */
335 (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
340 (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
344 /* Sense Key Specific */
345 (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
350 (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
358 ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
362 /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
369 ctl_ua_to_acsq(struct ctl_lun *lun, ctl_ua_type ua_to_build, int *asc,
370 int *ascq, ctl_ua_type *ua_to_clear, uint8_t **info)
373 switch (ua_to_build) {
375 /* 29h/01h POWER ON OCCURRED */
380 case CTL_UA_BUS_RESET:
381 /* 29h/02h SCSI BUS RESET OCCURRED */
386 case CTL_UA_TARG_RESET:
387 /* 29h/03h BUS DEVICE RESET FUNCTION OCCURRED*/
392 case CTL_UA_I_T_NEXUS_LOSS:
393 /* 29h/07h I_T NEXUS LOSS OCCURRED */
398 case CTL_UA_LUN_RESET:
399 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
401 * Since we don't have a specific ASC/ASCQ pair for a LUN
402 * reset, just return the generic reset code.
407 case CTL_UA_LUN_CHANGE:
408 /* 3Fh/0Eh REPORTED LUNS DATA HAS CHANGED */
412 case CTL_UA_MODE_CHANGE:
413 /* 2Ah/01h MODE PARAMETERS CHANGED */
417 case CTL_UA_LOG_CHANGE:
418 /* 2Ah/02h LOG PARAMETERS CHANGED */
422 case CTL_UA_INQ_CHANGE:
423 /* 3Fh/03h INQUIRY DATA HAS CHANGED */
427 case CTL_UA_RES_PREEMPT:
428 /* 2Ah/03h RESERVATIONS PREEMPTED */
432 case CTL_UA_RES_RELEASE:
433 /* 2Ah/04h RESERVATIONS RELEASED */
437 case CTL_UA_REG_PREEMPT:
438 /* 2Ah/05h REGISTRATIONS PREEMPTED */
442 case CTL_UA_ASYM_ACC_CHANGE:
443 /* 2Ah/06h ASYMMETRIC ACCESS STATE CHANGED */
447 case CTL_UA_CAPACITY_CHANGE:
448 /* 2Ah/09h CAPACITY DATA HAS CHANGED */
452 case CTL_UA_THIN_PROV_THRES:
453 /* 38h/07h THIN PROVISIONING SOFT THRESHOLD REACHED */
456 *info = lun->ua_tpt_info;
458 case CTL_UA_MEDIUM_CHANGE:
459 /* 28h/00h NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
464 /* Informational exception */
466 *ascq = lun->ie_ascq;
469 panic("%s: Unknown UA %x", __func__, ua_to_build);
474 ctl_build_qae(struct ctl_lun *lun, uint32_t initidx, uint8_t *resp)
477 ctl_ua_type ua_to_build, ua_to_clear;
482 mtx_assert(&lun->lun_lock, MA_OWNED);
483 p = initidx / CTL_MAX_INIT_PER_PORT;
484 i = initidx % CTL_MAX_INIT_PER_PORT;
485 if (lun->pending_ua[p] == NULL)
488 ua = lun->pending_ua[p][i];
489 if (ua == CTL_UA_NONE)
490 return (CTL_UA_NONE);
492 ua_to_build = (1 << (ffs(ua) - 1));
493 ua_to_clear = ua_to_build;
495 ctl_ua_to_acsq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
497 resp[0] = SSD_KEY_UNIT_ATTENTION;
498 if (ua_to_build == ua)
508 ctl_build_ua(struct ctl_lun *lun, uint32_t initidx,
509 struct scsi_sense_data *sense, scsi_sense_data_type sense_format)
512 ctl_ua_type ua_to_build, ua_to_clear;
517 mtx_assert(&lun->lun_lock, MA_OWNED);
518 p = initidx / CTL_MAX_INIT_PER_PORT;
519 if ((ua = lun->pending_ua[p]) == NULL) {
520 mtx_unlock(&lun->lun_lock);
521 ua = malloc(sizeof(ctl_ua_type) * CTL_MAX_INIT_PER_PORT,
523 mtx_lock(&lun->lun_lock);
524 if (lun->pending_ua[p] == NULL) {
525 lun->pending_ua[p] = ua;
526 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++)
527 ua[i] = CTL_UA_POWERON;
530 ua = lun->pending_ua[p];
533 i = initidx % CTL_MAX_INIT_PER_PORT;
534 if (ua[i] == CTL_UA_NONE)
535 return (CTL_UA_NONE);
537 ua_to_build = (1 << (ffs(ua[i]) - 1));
538 ua_to_clear = ua_to_build;
540 ctl_ua_to_acsq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
542 ctl_set_sense_data(sense, lun, sense_format, /*current_error*/ 1,
543 /*sense_key*/ SSD_KEY_UNIT_ATTENTION, asc, ascq,
544 ((info != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP), 8, info,
547 /* We're reporting this UA, so clear it */
548 ua[i] &= ~ua_to_clear;
550 return (ua_to_build);
554 ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
556 /* OVERLAPPED COMMANDS ATTEMPTED */
559 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
566 ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
568 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
571 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
578 * Tell the user that there was a problem with the command or data he sent.
581 ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
582 int field, int bit_valid, int bit)
588 /* "Invalid field in CDB" */
591 /* "Invalid field in parameter list" */
596 sks[0] = SSD_SCS_VALID;
598 sks[0] |= SSD_FIELDPTR_CMD;
599 scsi_ulto2b(field, &sks[1]);
602 sks[0] |= SSD_BITPTR_VALID | bit;
607 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
610 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
611 /*size*/ sizeof(sks),
617 ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
621 sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
622 scsi_ulto2b(0, &sks[1]);
624 /* "Invalid command operation code" */
627 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
630 /*type*/ SSD_ELEM_SKS,
631 /*size*/ sizeof(sks),
637 ctl_set_param_len_error(struct ctl_scsiio *ctsio)
639 /* "Parameter list length error" */
642 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
649 ctl_set_already_locked(struct ctl_scsiio *ctsio)
651 /* Vendor unique "Somebody already is locked" */
654 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
661 ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
663 /* "Logical unit not supported" */
666 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
673 ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
674 uint16_t retry_count)
679 sks[0] = SSD_SCS_VALID;
680 sks[1] = (retry_count >> 8) & 0xff;
681 sks[2] = retry_count & 0xff;
684 /* "Internal target failure" */
687 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
690 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
691 /*size*/ sizeof(sks),
697 ctl_set_medium_error(struct ctl_scsiio *ctsio, int read)
700 /* "Unrecovered read error" */
703 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
708 /* "Write error - auto reallocation failed" */
711 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
719 ctl_set_aborted(struct ctl_scsiio *ctsio)
723 /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
730 ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio)
732 /* "Logical block address out of range" */
735 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
742 ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
744 /* "Logical unit not ready, initializing cmd. required" */
747 /*sense_key*/ SSD_KEY_NOT_READY,
754 ctl_set_lun_int_reqd(struct ctl_scsiio *ctsio)
756 /* "Logical unit not ready, manual intervention required" */
759 /*sense_key*/ SSD_KEY_NOT_READY,
766 ctl_set_lun_ejected(struct ctl_scsiio *ctsio)
768 /* "Medium not present - tray open" */
771 /*sense_key*/ SSD_KEY_NOT_READY,
778 ctl_set_lun_no_media(struct ctl_scsiio *ctsio)
780 /* "Medium not present - tray closed" */
783 /*sense_key*/ SSD_KEY_NOT_READY,
790 ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
792 /* "Invalid release of persistent reservation" */
795 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
802 ctl_set_lun_transit(struct ctl_scsiio *ctsio)
804 /* "Logical unit not ready, asymmetric access state transition" */
807 /*sense_key*/ SSD_KEY_NOT_READY,
814 ctl_set_lun_standby(struct ctl_scsiio *ctsio)
816 /* "Logical unit not ready, target port in standby state" */
819 /*sense_key*/ SSD_KEY_NOT_READY,
826 ctl_set_lun_unavail(struct ctl_scsiio *ctsio)
828 /* "Logical unit not ready, target port in unavailable state" */
831 /*sense_key*/ SSD_KEY_NOT_READY,
838 ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
840 /* "Medium format corrupted" */
843 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
850 ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
852 /* "Medium magazine not accessible" */
855 /*sense_key*/ SSD_KEY_NOT_READY,
862 ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
864 /* "Data phase error" */
867 /*sense_key*/ SSD_KEY_NOT_READY,
874 ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
876 struct scsi_sense_data *sense;
878 sense = &ctsio->sense_data;
879 memset(sense, 0, sizeof(*sense));
880 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
881 ctsio->sense_len = 0;
882 ctsio->io_hdr.status = CTL_SCSI_ERROR;
886 ctl_set_queue_full(struct ctl_scsiio *ctsio)
888 struct scsi_sense_data *sense;
890 sense = &ctsio->sense_data;
891 memset(sense, 0, sizeof(*sense));
892 ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
893 ctsio->sense_len = 0;
894 ctsio->io_hdr.status = CTL_SCSI_ERROR;
898 ctl_set_busy(struct ctl_scsiio *ctsio)
900 struct scsi_sense_data *sense;
902 sense = &ctsio->sense_data;
903 memset(sense, 0, sizeof(*sense));
904 ctsio->scsi_status = SCSI_STATUS_BUSY;
905 ctsio->sense_len = 0;
906 ctsio->io_hdr.status = CTL_SCSI_ERROR;
910 ctl_set_task_aborted(struct ctl_scsiio *ctsio)
912 struct scsi_sense_data *sense;
914 sense = &ctsio->sense_data;
915 memset(sense, 0, sizeof(*sense));
916 ctsio->scsi_status = SCSI_STATUS_TASK_ABORTED;
917 ctsio->sense_len = 0;
918 ctsio->io_hdr.status = CTL_CMD_ABORTED;
922 ctl_set_hw_write_protected(struct ctl_scsiio *ctsio)
924 /* "Hardware write protected" */
927 /*sense_key*/ SSD_KEY_DATA_PROTECT,
934 ctl_set_space_alloc_fail(struct ctl_scsiio *ctsio)
936 /* "Space allocation failed write protect" */
939 /*sense_key*/ SSD_KEY_DATA_PROTECT,
946 ctl_set_success(struct ctl_scsiio *ctsio)
948 struct scsi_sense_data *sense;
950 sense = &ctsio->sense_data;
951 memset(sense, 0, sizeof(*sense));
952 ctsio->scsi_status = SCSI_STATUS_OK;
953 ctsio->sense_len = 0;
954 ctsio->io_hdr.status = CTL_SUCCESS;