2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2011 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
34 * CAM Target Layer error reporting routines.
36 * Author: Ken Merry <ken@FreeBSD.org>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/types.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/stddef.h>
51 #include <sys/ctype.h>
52 #include <sys/sysctl.h>
53 #include <machine/stdarg.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_da.h>
57 #include <cam/ctl/ctl_io.h>
58 #include <cam/ctl/ctl.h>
59 #include <cam/ctl/ctl_frontend.h>
60 #include <cam/ctl/ctl_backend.h>
61 #include <cam/ctl/ctl_ioctl.h>
62 #include <cam/ctl/ctl_error.h>
63 #include <cam/ctl/ctl_ha.h>
64 #include <cam/ctl/ctl_private.h>
67 ctl_set_sense_data_va(struct scsi_sense_data *sense_data, void *lunptr,
68 scsi_sense_data_type sense_format, int current_error,
69 int sense_key, int asc, int ascq, va_list ap)
73 lun = (struct ctl_lun *)lunptr;
76 * Determine whether to return fixed or descriptor format sense
79 if (sense_format == SSD_TYPE_NONE) {
81 * If the format isn't specified, we only return descriptor
82 * sense if the LUN exists and descriptor sense is turned
86 && (lun->flags & CTL_LUN_SENSE_DESC))
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_build_ua(struct ctl_lun *lun, uint32_t initidx,
370 struct scsi_sense_data *sense, scsi_sense_data_type sense_format)
373 ctl_ua_type ua_to_build, ua_to_clear;
377 mtx_assert(&lun->lun_lock, MA_OWNED);
378 p = initidx / CTL_MAX_INIT_PER_PORT;
379 if ((ua = lun->pending_ua[p]) == NULL) {
380 mtx_unlock(&lun->lun_lock);
381 ua = malloc(sizeof(ctl_ua_type) * CTL_MAX_INIT_PER_PORT,
383 mtx_lock(&lun->lun_lock);
384 if (lun->pending_ua[p] == NULL) {
385 lun->pending_ua[p] = ua;
386 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++)
387 ua[i] = CTL_UA_POWERON;
390 ua = lun->pending_ua[p];
393 i = initidx % CTL_MAX_INIT_PER_PORT;
394 if (ua[i] == CTL_UA_NONE)
395 return (CTL_UA_NONE);
397 ua_to_build = (1 << (ffs(ua[i]) - 1));
398 ua_to_clear = ua_to_build;
400 switch (ua_to_build) {
402 /* 29h/01h POWER ON OCCURRED */
407 case CTL_UA_BUS_RESET:
408 /* 29h/02h SCSI BUS RESET OCCURRED */
413 case CTL_UA_TARG_RESET:
414 /* 29h/03h BUS DEVICE RESET FUNCTION OCCURRED*/
419 case CTL_UA_I_T_NEXUS_LOSS:
420 /* 29h/07h I_T NEXUS LOSS OCCURRED */
425 case CTL_UA_LUN_RESET:
426 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
428 * Since we don't have a specific ASC/ASCQ pair for a LUN
429 * reset, just return the generic reset code.
434 case CTL_UA_LUN_CHANGE:
435 /* 3Fh/0Eh REPORTED LUNS DATA HAS CHANGED */
439 case CTL_UA_MODE_CHANGE:
440 /* 2Ah/01h MODE PARAMETERS CHANGED */
444 case CTL_UA_LOG_CHANGE:
445 /* 2Ah/02h LOG PARAMETERS CHANGED */
449 case CTL_UA_RES_PREEMPT:
450 /* 2Ah/03h RESERVATIONS PREEMPTED */
454 case CTL_UA_RES_RELEASE:
455 /* 2Ah/04h RESERVATIONS RELEASED */
459 case CTL_UA_REG_PREEMPT:
460 /* 2Ah/05h REGISTRATIONS PREEMPTED */
464 case CTL_UA_ASYM_ACC_CHANGE:
465 /* 2Ah/06n ASYMMETRIC ACCESS STATE CHANGED */
469 case CTL_UA_CAPACITY_CHANGED:
470 /* 2Ah/09n CAPACITY DATA HAS CHANGED */
474 case CTL_UA_THIN_PROV_THRES:
475 /* 38h/07n THIN PROVISIONING SOFT THRESHOLD REACHED */
480 panic("ctl_build_ua: Unknown UA %x", ua_to_build);
483 ctl_set_sense_data(sense,
487 /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
492 /* We're reporting this UA, so clear it */
493 ua[i] &= ~ua_to_clear;
495 return (ua_to_build);
499 ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
501 /* OVERLAPPED COMMANDS ATTEMPTED */
504 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
511 ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
513 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
516 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
523 * Tell the user that there was a problem with the command or data he sent.
526 ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
527 int field, int bit_valid, int bit)
533 /* "Invalid field in CDB" */
536 /* "Invalid field in parameter list" */
541 sks[0] = SSD_SCS_VALID;
543 sks[0] |= SSD_FIELDPTR_CMD;
544 scsi_ulto2b(field, &sks[1]);
547 sks[0] |= SSD_BITPTR_VALID | bit;
552 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
555 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
556 /*size*/ sizeof(sks),
562 ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
564 struct scsi_sense_data *sense;
567 sense = &ctsio->sense_data;
569 sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
570 scsi_ulto2b(0, &sks[1]);
572 /* "Invalid command operation code" */
575 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
578 /*type*/ SSD_ELEM_SKS,
579 /*size*/ sizeof(sks),
585 ctl_set_param_len_error(struct ctl_scsiio *ctsio)
587 /* "Parameter list length error" */
590 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
597 ctl_set_already_locked(struct ctl_scsiio *ctsio)
599 /* Vendor unique "Somebody already is locked" */
602 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
609 ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
611 /* "Logical unit not supported" */
614 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
621 ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
622 uint16_t retry_count)
627 sks[0] = SSD_SCS_VALID;
628 sks[1] = (retry_count >> 8) & 0xff;
629 sks[2] = retry_count & 0xff;
632 /* "Internal target failure" */
635 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
638 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
639 /*size*/ sizeof(sks),
645 ctl_set_medium_error(struct ctl_scsiio *ctsio)
647 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
648 /* "Unrecovered read error" */
651 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
656 /* "Write error - auto reallocation failed" */
659 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
667 ctl_set_aborted(struct ctl_scsiio *ctsio)
671 /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
678 ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio)
680 /* "Logical block address out of range" */
683 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
690 ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
692 /* "Logical unit not ready, initializing cmd. required" */
695 /*sense_key*/ SSD_KEY_NOT_READY,
702 ctl_set_lun_not_ready(struct ctl_scsiio *ctsio)
704 /* "Logical unit not ready, manual intervention required" */
707 /*sense_key*/ SSD_KEY_NOT_READY,
714 ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
716 /* "Invalid release of persistent reservation" */
719 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
726 ctl_set_lun_transit(struct ctl_scsiio *ctsio)
728 /* "Logical unit not ready, asymmetric access state transition" */
731 /*sense_key*/ SSD_KEY_NOT_READY,
738 ctl_set_lun_standby(struct ctl_scsiio *ctsio)
740 /* "Logical unit not ready, target port in standby state" */
743 /*sense_key*/ SSD_KEY_NOT_READY,
750 ctl_set_lun_unavail(struct ctl_scsiio *ctsio)
752 /* "Logical unit not ready, target port in unavailable state" */
755 /*sense_key*/ SSD_KEY_NOT_READY,
762 ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
764 /* "Medium format corrupted" */
767 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
774 ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
776 /* "Medium magazine not accessible" */
779 /*sense_key*/ SSD_KEY_NOT_READY,
786 ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
788 /* "Data phase error" */
791 /*sense_key*/ SSD_KEY_NOT_READY,
798 ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
800 struct scsi_sense_data *sense;
802 sense = &ctsio->sense_data;
803 memset(sense, 0, sizeof(*sense));
804 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
805 ctsio->sense_len = 0;
806 ctsio->io_hdr.status = CTL_SCSI_ERROR;
810 ctl_set_queue_full(struct ctl_scsiio *ctsio)
812 struct scsi_sense_data *sense;
814 sense = &ctsio->sense_data;
815 memset(sense, 0, sizeof(*sense));
816 ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
817 ctsio->sense_len = 0;
818 ctsio->io_hdr.status = CTL_SCSI_ERROR;
822 ctl_set_busy(struct ctl_scsiio *ctsio)
824 struct scsi_sense_data *sense;
826 sense = &ctsio->sense_data;
827 memset(sense, 0, sizeof(*sense));
828 ctsio->scsi_status = SCSI_STATUS_BUSY;
829 ctsio->sense_len = 0;
830 ctsio->io_hdr.status = CTL_SCSI_ERROR;
834 ctl_set_task_aborted(struct ctl_scsiio *ctsio)
836 struct scsi_sense_data *sense;
838 sense = &ctsio->sense_data;
839 memset(sense, 0, sizeof(*sense));
840 ctsio->scsi_status = SCSI_STATUS_TASK_ABORTED;
841 ctsio->sense_len = 0;
842 ctsio->io_hdr.status = CTL_CMD_ABORTED;
846 ctl_set_space_alloc_fail(struct ctl_scsiio *ctsio)
848 /* "Space allocation failed write protect" */
851 /*sense_key*/ SSD_KEY_DATA_PROTECT,
858 ctl_set_success(struct ctl_scsiio *ctsio)
860 struct scsi_sense_data *sense;
862 sense = &ctsio->sense_data;
863 memset(sense, 0, sizeof(*sense));
864 ctsio->scsi_status = SCSI_STATUS_OK;
865 ctsio->sense_len = 0;
866 ctsio->io_hdr.status = CTL_SUCCESS;