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_frontend_internal.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
87 && (lun->flags & CTL_LUN_SENSE_DESC))
88 sense_format = SSD_TYPE_DESC;
90 sense_format = SSD_TYPE_FIXED;
93 scsi_set_sense_data_va(sense_data, sense_format, current_error,
94 sense_key, asc, ascq, ap);
98 ctl_set_sense_data(struct scsi_sense_data *sense_data, void *lunptr,
99 scsi_sense_data_type sense_format, int current_error,
100 int sense_key, int asc, int ascq, ...)
105 ctl_set_sense_data_va(sense_data, lunptr, sense_format, current_error,
106 sense_key, asc, ascq, ap);
111 ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
112 int asc, int ascq, ...)
118 * The LUN can't go away until all of the commands have been
119 * completed. Therefore we can safely access the LUN structure and
120 * flags without the lock.
122 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
125 ctl_set_sense_data_va(&ctsio->sense_data,
135 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
136 ctsio->sense_len = SSD_FULL_SIZE;
137 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
141 * Transform fixed sense data into descriptor sense data.
143 * For simplicity's sake, we assume that both sense structures are
144 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
147 ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
148 struct scsi_sense_data_desc *sense_dest)
150 struct scsi_sense_stream stream_sense;
154 bzero(sense_dest, sizeof(*sense_dest));
156 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
161 bzero(&stream_sense, sizeof(stream_sense));
164 * Check to see whether any of the tape-specific bits are set. If
165 * so, we'll need a stream sense descriptor.
167 if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
168 stream_bits = sense_src->flags & ~SSD_KEY;
173 * Utilize our sense setting routine to do the transform. If a
174 * value is set in the fixed sense data, set it in the descriptor
175 * data. Otherwise, skip it.
177 ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
179 /*sense_format*/ SSD_TYPE_DESC,
181 /*sense_key*/ sense_src->flags & SSD_KEY,
182 /*asc*/ sense_src->add_sense_code,
183 /*ascq*/ sense_src->add_sense_code_qual,
185 /* Information Bytes */
186 (scsi_4btoul(sense_src->info) != 0) ?
187 SSD_ELEM_INFO : SSD_ELEM_SKIP,
188 sizeof(sense_src->info),
191 /* Command specific bytes */
192 (scsi_4btoul(sense_src->cmd_spec_info) != 0) ?
193 SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
194 sizeof(sense_src->cmd_spec_info),
195 sense_src->cmd_spec_info,
198 (sense_src->fru != 0) ?
199 SSD_ELEM_FRU : SSD_ELEM_SKIP,
200 sizeof(sense_src->fru),
203 /* Sense Key Specific */
204 (sense_src->sense_key_spec[0] & SSD_SCS_VALID) ?
205 SSD_ELEM_SKS : SSD_ELEM_SKIP,
206 sizeof(sense_src->sense_key_spec),
207 sense_src->sense_key_spec,
211 SSD_ELEM_STREAM : SSD_ELEM_SKIP,
219 * Transform descriptor format sense data into fixed sense data.
221 * Some data may be lost in translation, because there are descriptors
222 * thant can't be represented as fixed sense data.
224 * For simplicity's sake, we assume that both sense structures are
225 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
228 ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
229 struct scsi_sense_data_fixed *sense_dest)
232 uint8_t *info_ptr = NULL, *cmd_ptr = NULL, *fru_ptr = NULL;
233 uint8_t *sks_ptr = NULL, *stream_ptr = NULL;
234 int info_size = 0, cmd_size = 0, fru_size = 0;
235 int sks_size = 0, stream_size = 0;
238 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
243 for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
244 struct scsi_sense_desc_header *header;
246 header = (struct scsi_sense_desc_header *)
247 &sense_src->sense_desc[pos];
250 * See if this record goes past the end of the sense data.
251 * It shouldn't, but check just in case.
253 if ((pos + header->length + sizeof(*header)) >
254 sense_src->extra_len)
257 switch (sense_src->sense_desc[pos]) {
258 case SSD_DESC_INFO: {
259 struct scsi_sense_info *info;
261 info = (struct scsi_sense_info *)header;
263 info_ptr = info->info;
264 info_size = sizeof(info->info);
266 pos += info->length +
267 sizeof(struct scsi_sense_desc_header);
270 case SSD_DESC_COMMAND: {
271 struct scsi_sense_command *cmd;
273 cmd = (struct scsi_sense_command *)header;
274 cmd_ptr = cmd->command_info;
275 cmd_size = sizeof(cmd->command_info);
278 sizeof(struct scsi_sense_desc_header);
282 struct scsi_sense_fru *fru;
284 fru = (struct scsi_sense_fru *)header;
286 fru_size = sizeof(fru->fru);
288 sizeof(struct scsi_sense_desc_header);
292 struct scsi_sense_sks *sks;
294 sks = (struct scsi_sense_sks *)header;
295 sks_ptr = sks->sense_key_spec;
296 sks_size = sizeof(sks->sense_key_spec);
299 sizeof(struct scsi_sense_desc_header);
302 case SSD_DESC_STREAM: {
303 struct scsi_sense_stream *stream_sense;
305 stream_sense = (struct scsi_sense_stream *)header;
306 stream_ptr = &stream_sense->byte3;
307 stream_size = sizeof(stream_sense->byte3);
308 pos = stream_sense->length +
309 sizeof(struct scsi_sense_desc_header);
314 * We don't recognize this particular sense
315 * descriptor type, so just skip it.
317 pos += sizeof(*header) + header->length;
322 ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
324 /*sense_format*/ SSD_TYPE_FIXED,
326 /*sense_key*/ sense_src->sense_key & SSD_KEY,
327 /*asc*/ sense_src->add_sense_code,
328 /*ascq*/ sense_src->add_sense_code_qual,
330 /* Information Bytes */
331 (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
335 /* Command specific bytes */
336 (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
341 (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
345 /* Sense Key Specific */
346 (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
351 (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
359 ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
363 /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
370 ctl_build_ua(ctl_ua_type ua_type, struct scsi_sense_data *sense,
371 scsi_sense_data_type sense_format)
373 ctl_ua_type ua_to_build;
376 if (ua_type == CTL_UA_NONE)
379 ua_to_build = CTL_UA_NONE;
381 for (i = 0; i < (sizeof(ua_type) * 8); i++) {
382 if (ua_type & (1 << i)) {
383 ua_to_build = 1 << i;
388 switch (ua_to_build) {
390 /* 29h/01h POWER ON OCCURRED */
394 case CTL_UA_BUS_RESET:
395 /* 29h/02h SCSI BUS RESET OCCURRED */
399 case CTL_UA_TARG_RESET:
400 /* 29h/03h BUS DEVICE RESET FUNCTION OCCURRED*/
404 case CTL_UA_LUN_RESET:
405 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
407 * Since we don't have a specific ASC/ASCQ pair for a LUN
408 * reset, just return the generic reset code.
413 case CTL_UA_LUN_CHANGE:
414 /* 3Fh/0Eh REPORTED LUNS DATA HAS CHANGED */
418 case CTL_UA_MODE_CHANGE:
419 /* 2Ah/01h MODE PARAMETERS CHANGED */
423 case CTL_UA_LOG_CHANGE:
424 /* 2Ah/02h LOG PARAMETERS CHANGED */
429 /* 29h/06h TRANSCEIVER MODE CHANGED TO LVD */
434 /* 29h/05h TRANSCEIVER MODE CHANGED TO SINGLE-ENDED */
438 case CTL_UA_RES_PREEMPT:
439 /* 2Ah/03h RESERVATIONS PREEMPTED */
443 case CTL_UA_RES_RELEASE:
444 /* 2Ah/04h RESERVATIONS RELEASED */
448 case CTL_UA_REG_PREEMPT:
449 /* 2Ah/05h REGISTRATIONS PREEMPTED */
453 case CTL_UA_ASYM_ACC_CHANGE:
454 /* 2Ah/06n ASYMMETRIC ACCESS STATE CHANGED */
458 case CTL_UA_CAPACITY_CHANGED:
459 /* 2Ah/09n CAPACITY DATA HAS CHANGED */
464 ua_to_build = CTL_UA_NONE;
465 return (ua_to_build);
466 break; /* NOTREACHED */
469 ctl_set_sense_data(sense,
473 /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
478 return (ua_to_build);
482 ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
484 /* OVERLAPPED COMMANDS ATTEMPTED */
487 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
494 ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
496 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
499 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
506 * Tell the user that there was a problem with the command or data he sent.
509 ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
510 int field, int bit_valid, int bit)
516 /* "Invalid field in CDB" */
519 /* "Invalid field in parameter list" */
524 sks[0] = SSD_SCS_VALID;
526 sks[0] |= SSD_FIELDPTR_CMD;
527 scsi_ulto2b(field, &sks[1]);
530 sks[0] |= SSD_BITPTR_VALID | bit;
535 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
538 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
539 /*size*/ sizeof(sks),
545 ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
547 struct scsi_sense_data *sense;
550 sense = &ctsio->sense_data;
552 sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
553 scsi_ulto2b(0, &sks[1]);
555 /* "Invalid command operation code" */
558 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
561 /*type*/ SSD_ELEM_SKS,
562 /*size*/ sizeof(sks),
568 ctl_set_param_len_error(struct ctl_scsiio *ctsio)
570 /* "Parameter list length error" */
573 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
580 ctl_set_already_locked(struct ctl_scsiio *ctsio)
582 /* Vendor unique "Somebody already is locked" */
585 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
592 ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
594 /* "Logical unit not supported" */
597 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
604 ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
605 uint16_t retry_count)
610 sks[0] = SSD_SCS_VALID;
611 sks[1] = (retry_count >> 8) & 0xff;
612 sks[2] = retry_count & 0xff;
615 /* "Internal target failure" */
618 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
621 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
622 /*size*/ sizeof(sks),
628 ctl_set_medium_error(struct ctl_scsiio *ctsio)
630 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
631 /* "Unrecovered read error" */
634 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
639 /* "Write error - auto reallocation failed" */
642 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
650 ctl_set_aborted(struct ctl_scsiio *ctsio)
654 /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
661 ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio)
663 /* "Logical block address out of range" */
666 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
673 ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
675 /* "Logical unit not ready, initializing cmd. required" */
678 /*sense_key*/ SSD_KEY_NOT_READY,
685 ctl_set_lun_not_ready(struct ctl_scsiio *ctsio)
687 /* "Logical unit not ready, manual intervention required" */
690 /*sense_key*/ SSD_KEY_NOT_READY,
697 ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
699 /* "Invalid release of persistent reservation" */
702 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
709 ctl_set_lun_standby(struct ctl_scsiio *ctsio)
711 /* "Logical unit not ready, target port in standby state" */
714 /*sense_key*/ SSD_KEY_NOT_READY,
721 ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
723 /* "Medium format corrupted" */
726 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
733 ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
735 /* "Medium magazine not accessible" */
738 /*sense_key*/ SSD_KEY_NOT_READY,
745 ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
747 /* "Data phase error" */
750 /*sense_key*/ SSD_KEY_NOT_READY,
757 ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
759 struct scsi_sense_data *sense;
761 sense = &ctsio->sense_data;
762 memset(sense, 0, sizeof(*sense));
763 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
764 ctsio->sense_len = 0;
765 ctsio->io_hdr.status = CTL_SCSI_ERROR;
769 ctl_set_queue_full(struct ctl_scsiio *ctsio)
771 struct scsi_sense_data *sense;
773 sense = &ctsio->sense_data;
774 memset(sense, 0, sizeof(*sense));
775 ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
776 ctsio->sense_len = 0;
777 ctsio->io_hdr.status = CTL_SCSI_ERROR;
781 ctl_set_busy(struct ctl_scsiio *ctsio)
783 struct scsi_sense_data *sense;
785 sense = &ctsio->sense_data;
786 memset(sense, 0, sizeof(*sense));
787 ctsio->scsi_status = SCSI_STATUS_BUSY;
788 ctsio->sense_len = 0;
789 ctsio->io_hdr.status = CTL_SCSI_ERROR;
793 ctl_set_success(struct ctl_scsiio *ctsio)
795 struct scsi_sense_data *sense;
797 sense = &ctsio->sense_data;
798 memset(sense, 0, sizeof(*sense));
799 ctsio->scsi_status = SCSI_STATUS_OK;
800 ctsio->sense_len = 0;
801 ctsio->io_hdr.status = CTL_SUCCESS;