2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2011 Spectra Logic Corporation
6 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
37 * CAM Target Layer error reporting routines.
39 * Author: Ken Merry <ken@FreeBSD.org>
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, u_int *sense_len,
68 void *lunptr, 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 * SPC-3 and up require some UAs to be returned as fixed.
83 if (asc == 0x29 || (asc == 0x2A && ascq == 0x01))
84 sense_format = SSD_TYPE_FIXED;
87 * If the format isn't specified, we only return descriptor
88 * sense if the LUN exists and descriptor sense is turned
91 if ((lun != NULL) && (lun->MODE_CTRL.rlec & SCP_DSENSE))
92 sense_format = SSD_TYPE_DESC;
94 sense_format = SSD_TYPE_FIXED;
98 * Determine maximum sense data length to return.
100 if (*sense_len == 0) {
101 if ((lun != NULL) && (lun->MODE_CTRLE.max_sense != 0))
102 *sense_len = lun->MODE_CTRLE.max_sense;
104 *sense_len = SSD_FULL_SIZE;
107 scsi_set_sense_data_va(sense_data, sense_len, sense_format,
108 current_error, sense_key, asc, ascq, ap);
112 ctl_set_sense_data(struct scsi_sense_data *sense_data, u_int *sense_len,
113 void *lunptr, scsi_sense_data_type sense_format, int current_error,
114 int sense_key, int asc, int ascq, ...)
119 ctl_set_sense_data_va(sense_data, sense_len, lunptr, sense_format,
120 current_error, sense_key, asc, ascq, ap);
125 ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
126 int asc, int ascq, ...)
133 * The LUN can't go away until all of the commands have been
134 * completed. Therefore we can safely access the LUN structure and
135 * flags without the lock.
137 lun = CTL_LUN(ctsio);
141 ctl_set_sense_data_va(&ctsio->sense_data, &sense_len,
151 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
152 ctsio->sense_len = sense_len;
153 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
157 * Transform fixed sense data into descriptor sense data.
159 * For simplicity's sake, we assume that both sense structures are
160 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
163 ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
164 struct scsi_sense_data_desc *sense_dest)
166 struct scsi_sense_stream stream_sense;
171 bzero(sense_dest, sizeof(*sense_dest));
173 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
178 bzero(&stream_sense, sizeof(stream_sense));
181 * Check to see whether any of the tape-specific bits are set. If
182 * so, we'll need a stream sense descriptor.
184 if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
185 stream_bits = sense_src->flags & ~SSD_KEY;
190 * Utilize our sense setting routine to do the transform. If a
191 * value is set in the fixed sense data, set it in the descriptor
192 * data. Otherwise, skip it.
194 sense_len = SSD_FULL_SIZE;
195 ctl_set_sense_data((struct scsi_sense_data *)sense_dest, &sense_len,
197 /*sense_format*/ SSD_TYPE_DESC,
199 /*sense_key*/ sense_src->flags & SSD_KEY,
200 /*asc*/ sense_src->add_sense_code,
201 /*ascq*/ sense_src->add_sense_code_qual,
203 /* Information Bytes */
204 (sense_src->error_code & SSD_ERRCODE_VALID) ?
205 SSD_ELEM_INFO : SSD_ELEM_SKIP,
206 sizeof(sense_src->info),
209 /* Command specific bytes */
210 (scsi_4btoul(sense_src->cmd_spec_info) != 0) ?
211 SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
212 sizeof(sense_src->cmd_spec_info),
213 sense_src->cmd_spec_info,
216 (sense_src->fru != 0) ?
217 SSD_ELEM_FRU : SSD_ELEM_SKIP,
218 sizeof(sense_src->fru),
221 /* Sense Key Specific */
222 (sense_src->sense_key_spec[0] & SSD_SCS_VALID) ?
223 SSD_ELEM_SKS : SSD_ELEM_SKIP,
224 sizeof(sense_src->sense_key_spec),
225 sense_src->sense_key_spec,
229 SSD_ELEM_STREAM : SSD_ELEM_SKIP,
237 * Transform descriptor format sense data into fixed sense data.
239 * Some data may be lost in translation, because there are descriptors
240 * thant can't be represented as fixed sense data.
242 * For simplicity's sake, we assume that both sense structures are
243 * SSD_FULL_SIZE. Otherwise, the logic gets more complicated.
246 ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
247 struct scsi_sense_data_fixed *sense_dest)
250 uint8_t *info_ptr = NULL, *cmd_ptr = NULL, *fru_ptr = NULL;
251 uint8_t *sks_ptr = NULL, *stream_ptr = NULL;
252 int info_size = 0, cmd_size = 0, fru_size = 0;
253 int sks_size = 0, stream_size = 0;
257 if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
262 for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
263 struct scsi_sense_desc_header *header;
265 header = (struct scsi_sense_desc_header *)
266 &sense_src->sense_desc[pos];
269 * See if this record goes past the end of the sense data.
270 * It shouldn't, but check just in case.
272 if ((pos + header->length + sizeof(*header)) >
273 sense_src->extra_len)
276 switch (sense_src->sense_desc[pos]) {
277 case SSD_DESC_INFO: {
278 struct scsi_sense_info *info;
280 info = (struct scsi_sense_info *)header;
282 info_ptr = info->info;
283 info_size = sizeof(info->info);
285 pos += info->length +
286 sizeof(struct scsi_sense_desc_header);
289 case SSD_DESC_COMMAND: {
290 struct scsi_sense_command *cmd;
292 cmd = (struct scsi_sense_command *)header;
293 cmd_ptr = cmd->command_info;
294 cmd_size = sizeof(cmd->command_info);
297 sizeof(struct scsi_sense_desc_header);
301 struct scsi_sense_fru *fru;
303 fru = (struct scsi_sense_fru *)header;
305 fru_size = sizeof(fru->fru);
307 sizeof(struct scsi_sense_desc_header);
311 struct scsi_sense_sks *sks;
313 sks = (struct scsi_sense_sks *)header;
314 sks_ptr = sks->sense_key_spec;
315 sks_size = sizeof(sks->sense_key_spec);
318 sizeof(struct scsi_sense_desc_header);
321 case SSD_DESC_STREAM: {
322 struct scsi_sense_stream *stream_sense;
324 stream_sense = (struct scsi_sense_stream *)header;
325 stream_ptr = &stream_sense->byte3;
326 stream_size = sizeof(stream_sense->byte3);
327 pos = stream_sense->length +
328 sizeof(struct scsi_sense_desc_header);
333 * We don't recognize this particular sense
334 * descriptor type, so just skip it.
336 pos += sizeof(*header) + header->length;
341 sense_len = SSD_FULL_SIZE;
342 ctl_set_sense_data((struct scsi_sense_data *)sense_dest, &sense_len,
344 /*sense_format*/ SSD_TYPE_FIXED,
346 /*sense_key*/ sense_src->sense_key & SSD_KEY,
347 /*asc*/ sense_src->add_sense_code,
348 /*ascq*/ sense_src->add_sense_code_qual,
350 /* Information Bytes */
351 (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
355 /* Command specific bytes */
356 (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
361 (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
365 /* Sense Key Specific */
366 (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
371 (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
379 ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
383 /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
390 ctl_ua_to_ascq(struct ctl_lun *lun, ctl_ua_type ua_to_build, int *asc,
391 int *ascq, ctl_ua_type *ua_to_clear, uint8_t **info)
394 switch (ua_to_build) {
396 /* 29h/01h POWER ON OCCURRED */
401 case CTL_UA_BUS_RESET:
402 /* 29h/02h SCSI BUS RESET OCCURRED */
407 case CTL_UA_TARG_RESET:
408 /* 29h/03h BUS DEVICE RESET FUNCTION OCCURRED*/
413 case CTL_UA_I_T_NEXUS_LOSS:
414 /* 29h/07h I_T NEXUS LOSS OCCURRED */
419 case CTL_UA_LUN_RESET:
420 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
422 * Since we don't have a specific ASC/ASCQ pair for a LUN
423 * reset, just return the generic reset code.
428 case CTL_UA_LUN_CHANGE:
429 /* 3Fh/0Eh REPORTED LUNS DATA HAS CHANGED */
433 case CTL_UA_MODE_CHANGE:
434 /* 2Ah/01h MODE PARAMETERS CHANGED */
438 case CTL_UA_LOG_CHANGE:
439 /* 2Ah/02h LOG PARAMETERS CHANGED */
443 case CTL_UA_INQ_CHANGE:
444 /* 3Fh/03h INQUIRY DATA HAS CHANGED */
448 case CTL_UA_RES_PREEMPT:
449 /* 2Ah/03h RESERVATIONS PREEMPTED */
453 case CTL_UA_RES_RELEASE:
454 /* 2Ah/04h RESERVATIONS RELEASED */
458 case CTL_UA_REG_PREEMPT:
459 /* 2Ah/05h REGISTRATIONS PREEMPTED */
463 case CTL_UA_ASYM_ACC_CHANGE:
464 /* 2Ah/06h ASYMMETRIC ACCESS STATE CHANGED */
468 case CTL_UA_CAPACITY_CHANGE:
469 /* 2Ah/09h CAPACITY DATA HAS CHANGED */
473 case CTL_UA_THIN_PROV_THRES:
474 /* 38h/07h THIN PROVISIONING SOFT THRESHOLD REACHED */
477 *info = lun->ua_tpt_info;
479 case CTL_UA_MEDIUM_CHANGE:
480 /* 28h/00h NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
485 /* Informational exception */
487 *ascq = lun->ie_ascq;
490 panic("%s: Unknown UA %x", __func__, ua_to_build);
495 ctl_build_qae(struct ctl_lun *lun, uint32_t initidx, uint8_t *resp)
498 ctl_ua_type ua_to_build, ua_to_clear;
503 mtx_assert(&lun->lun_lock, MA_OWNED);
504 p = initidx / CTL_MAX_INIT_PER_PORT;
505 i = initidx % CTL_MAX_INIT_PER_PORT;
506 if (lun->pending_ua[p] == NULL)
509 ua = lun->pending_ua[p][i];
510 if (ua == CTL_UA_NONE)
511 return (CTL_UA_NONE);
513 ua_to_build = (1 << (ffs(ua) - 1));
514 ua_to_clear = ua_to_build;
516 ctl_ua_to_ascq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
518 resp[0] = SSD_KEY_UNIT_ATTENTION;
519 if (ua_to_build == ua)
525 return (ua_to_build);
529 ctl_build_ua(struct ctl_lun *lun, uint32_t initidx,
530 struct scsi_sense_data *sense, u_int *sense_len,
531 scsi_sense_data_type sense_format)
534 ctl_ua_type ua_to_build, ua_to_clear;
539 mtx_assert(&lun->lun_lock, MA_OWNED);
540 mtx_assert(&lun->ctl_softc->ctl_lock, MA_NOTOWNED);
541 p = initidx / CTL_MAX_INIT_PER_PORT;
542 if ((ua = lun->pending_ua[p]) == NULL) {
543 mtx_unlock(&lun->lun_lock);
544 ua = malloc(sizeof(ctl_ua_type) * CTL_MAX_INIT_PER_PORT,
546 mtx_lock(&lun->lun_lock);
547 if (lun->pending_ua[p] == NULL) {
548 lun->pending_ua[p] = ua;
549 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++)
550 ua[i] = CTL_UA_POWERON;
553 ua = lun->pending_ua[p];
556 i = initidx % CTL_MAX_INIT_PER_PORT;
557 if (ua[i] == CTL_UA_NONE)
558 return (CTL_UA_NONE);
560 ua_to_build = (1 << (ffs(ua[i]) - 1));
561 ua_to_clear = ua_to_build;
563 ctl_ua_to_ascq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
565 ctl_set_sense_data(sense, sense_len, lun, sense_format, 1,
566 /*sense_key*/ SSD_KEY_UNIT_ATTENTION, asc, ascq,
567 ((info != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP), 8, info,
570 /* We're reporting this UA, so clear it */
571 ua[i] &= ~ua_to_clear;
573 if (ua_to_build == CTL_UA_LUN_CHANGE) {
574 mtx_unlock(&lun->lun_lock);
575 mtx_lock(&lun->ctl_softc->ctl_lock);
576 ctl_clr_ua_allluns(lun->ctl_softc, initidx, ua_to_build);
577 mtx_unlock(&lun->ctl_softc->ctl_lock);
578 mtx_lock(&lun->lun_lock);
579 } else if (ua_to_build == CTL_UA_THIN_PROV_THRES &&
580 (lun->MODE_LBP.main.flags & SLBPP_SITUA) != 0) {
581 ctl_clr_ua_all(lun, -1, ua_to_build);
584 return (ua_to_build);
588 ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
590 /* OVERLAPPED COMMANDS ATTEMPTED */
593 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
600 ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
602 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
605 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
612 * Tell the user that there was a problem with the command or data he sent.
615 ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
616 int field, int bit_valid, int bit)
622 /* "Invalid field in CDB" */
625 /* "Invalid field in parameter list" */
630 sks[0] = SSD_SCS_VALID;
632 sks[0] |= SSD_FIELDPTR_CMD;
633 scsi_ulto2b(field, &sks[1]);
636 sks[0] |= SSD_BITPTR_VALID | bit;
641 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
644 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
645 /*size*/ sizeof(sks),
650 ctl_set_invalid_field_ciu(struct ctl_scsiio *ctsio)
653 /* "Invalid field in command information unit" */
656 /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
663 ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
667 sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
668 scsi_ulto2b(0, &sks[1]);
670 /* "Invalid command operation code" */
673 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
676 /*type*/ SSD_ELEM_SKS,
677 /*size*/ sizeof(sks),
683 ctl_set_param_len_error(struct ctl_scsiio *ctsio)
685 /* "Parameter list length error" */
688 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
695 ctl_set_already_locked(struct ctl_scsiio *ctsio)
697 /* Vendor unique "Somebody already is locked" */
700 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
707 ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
709 /* "Logical unit not supported" */
712 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
719 ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
720 uint16_t retry_count)
725 sks[0] = SSD_SCS_VALID;
726 sks[1] = (retry_count >> 8) & 0xff;
727 sks[2] = retry_count & 0xff;
730 /* "Internal target failure" */
733 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
736 /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
737 /*size*/ sizeof(sks),
743 ctl_set_medium_error(struct ctl_scsiio *ctsio, int read)
746 /* "Unrecovered read error" */
749 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
754 /* "Write error - auto reallocation failed" */
757 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
765 ctl_set_aborted(struct ctl_scsiio *ctsio)
769 /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
776 ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio, uint64_t lba)
780 scsi_u64to8b(lba, info);
782 /* "Logical block address out of range" */
785 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
788 /*type*/ (lba != 0) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
789 /*size*/ sizeof(info), /*data*/ &info,
794 ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
796 /* "Logical unit not ready, initializing cmd. required" */
799 /*sense_key*/ SSD_KEY_NOT_READY,
806 ctl_set_lun_int_reqd(struct ctl_scsiio *ctsio)
808 /* "Logical unit not ready, manual intervention required" */
811 /*sense_key*/ SSD_KEY_NOT_READY,
818 ctl_set_lun_ejected(struct ctl_scsiio *ctsio)
820 /* "Medium not present - tray open" */
823 /*sense_key*/ SSD_KEY_NOT_READY,
830 ctl_set_lun_no_media(struct ctl_scsiio *ctsio)
832 /* "Medium not present - tray closed" */
835 /*sense_key*/ SSD_KEY_NOT_READY,
842 ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
844 /* "Invalid release of persistent reservation" */
847 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
854 ctl_set_lun_transit(struct ctl_scsiio *ctsio)
856 /* "Logical unit not ready, asymmetric access state transition" */
859 /*sense_key*/ SSD_KEY_NOT_READY,
866 ctl_set_lun_standby(struct ctl_scsiio *ctsio)
868 /* "Logical unit not ready, target port in standby state" */
871 /*sense_key*/ SSD_KEY_NOT_READY,
878 ctl_set_lun_unavail(struct ctl_scsiio *ctsio)
880 /* "Logical unit not ready, target port in unavailable state" */
883 /*sense_key*/ SSD_KEY_NOT_READY,
890 ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
892 /* "Medium format corrupted" */
895 /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
902 ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
904 /* "Medium magazine not accessible" */
907 /*sense_key*/ SSD_KEY_NOT_READY,
914 ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
916 /* "Data phase error" */
919 /*sense_key*/ SSD_KEY_NOT_READY,
926 ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
929 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
930 ctsio->sense_len = 0;
931 ctsio->io_hdr.status = CTL_SCSI_ERROR;
935 ctl_set_queue_full(struct ctl_scsiio *ctsio)
938 ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
939 ctsio->sense_len = 0;
940 ctsio->io_hdr.status = CTL_SCSI_ERROR;
944 ctl_set_busy(struct ctl_scsiio *ctsio)
947 ctsio->scsi_status = SCSI_STATUS_BUSY;
948 ctsio->sense_len = 0;
949 ctsio->io_hdr.status = CTL_SCSI_ERROR;
953 ctl_set_task_aborted(struct ctl_scsiio *ctsio)
956 ctsio->scsi_status = SCSI_STATUS_TASK_ABORTED;
957 ctsio->sense_len = 0;
958 ctsio->io_hdr.status = CTL_CMD_ABORTED;
962 ctl_set_hw_write_protected(struct ctl_scsiio *ctsio)
964 /* "Hardware write protected" */
967 /*sense_key*/ SSD_KEY_DATA_PROTECT,
974 ctl_set_space_alloc_fail(struct ctl_scsiio *ctsio)
976 /* "Space allocation failed write protect" */
979 /*sense_key*/ SSD_KEY_DATA_PROTECT,
986 ctl_set_success(struct ctl_scsiio *ctsio)
989 ctsio->scsi_status = SCSI_STATUS_OK;
990 ctsio->sense_len = 0;
991 ctsio->io_hdr.status = CTL_SUCCESS;
995 ctl_nvme_set_error(struct ctl_nvmeio *ctnio, uint8_t sc_type,
1000 memset(&ctnio->cpl, 0, sizeof(ctnio->cpl));
1001 status = NVMEF(NVME_STATUS_SCT, sc_type) |
1002 NVMEF(NVME_STATUS_SC, sc_status);
1003 ctnio->cpl.status = htole16(status);
1004 ctnio->io_hdr.status = CTL_NVME_ERROR;
1008 ctl_nvme_set_generic_error(struct ctl_nvmeio *ctnio, uint8_t sc_status)
1010 ctl_nvme_set_error(ctnio, NVME_SCT_GENERIC, sc_status);
1014 ctl_nvme_set_invalid_opcode(struct ctl_nvmeio *ctnio)
1016 ctl_nvme_set_generic_error(ctnio, NVME_SC_INVALID_OPCODE);
1020 ctl_nvme_set_invalid_field(struct ctl_nvmeio *ctnio)
1022 ctl_nvme_set_generic_error(ctnio, NVME_SC_INVALID_FIELD);
1026 ctl_nvme_set_data_transfer_error(struct ctl_nvmeio *ctnio)
1028 ctl_nvme_set_generic_error(ctnio, NVME_SC_DATA_TRANSFER_ERROR);
1032 ctl_nvme_set_internal_error(struct ctl_nvmeio *ctnio)
1034 ctl_nvme_set_generic_error(ctnio, NVME_SC_INTERNAL_DEVICE_ERROR);
1038 ctl_nvme_set_invalid_namespace(struct ctl_nvmeio *ctnio)
1040 ctl_nvme_set_generic_error(ctnio, NVME_SC_INVALID_NAMESPACE_OR_FORMAT);
1044 ctl_nvme_set_command_aborted(struct ctl_nvmeio *ctnio)
1046 ctl_nvme_set_generic_error(ctnio, NVME_SC_COMMAND_ABORTED_BY_HOST);
1050 ctl_nvme_set_failed_fused_command(struct ctl_nvmeio *ctnio)
1052 ctl_nvme_set_generic_error(ctnio, NVME_SC_ABORTED_FAILED_FUSED);
1056 ctl_nvme_set_missing_fused_command(struct ctl_nvmeio *ctnio)
1058 ctl_nvme_set_generic_error(ctnio, NVME_SC_ABORTED_MISSING_FUSED);
1062 ctl_nvme_set_namespace_is_write_protected(struct ctl_nvmeio *ctnio)
1064 ctl_nvme_set_generic_error(ctnio, NVME_SC_NAMESPACE_IS_WRITE_PROTECTED);
1068 ctl_nvme_set_lba_out_of_range(struct ctl_nvmeio *ctnio)
1070 ctl_nvme_set_generic_error(ctnio, NVME_SC_LBA_OUT_OF_RANGE);
1074 ctl_nvme_set_namespace_not_ready(struct ctl_nvmeio *ctnio)
1076 ctl_nvme_set_generic_error(ctnio, NVME_SC_NAMESPACE_NOT_READY);
1080 ctl_nvme_set_write_fault(struct ctl_nvmeio *ctnio)
1082 ctl_nvme_set_error(ctnio, NVME_SCT_MEDIA_ERROR,
1083 NVME_SC_WRITE_FAULTS);
1087 ctl_nvme_set_unrecoverable_read_error(struct ctl_nvmeio *ctnio)
1089 ctl_nvme_set_error(ctnio, NVME_SCT_MEDIA_ERROR,
1090 NVME_SC_UNRECOVERED_READ_ERROR);
1094 ctl_nvme_set_compare_failure(struct ctl_nvmeio *ctnio)
1096 ctl_nvme_set_error(ctnio, NVME_SCT_MEDIA_ERROR,
1097 NVME_SC_COMPARE_FAILURE);
1101 ctl_nvme_set_space_alloc_fail(struct ctl_nvmeio *ctnio)
1103 ctl_nvme_set_error(ctnio, NVME_SCT_MEDIA_ERROR,
1104 NVME_SC_DEALLOCATED_OR_UNWRITTEN);
1108 ctl_nvme_set_success(struct ctl_nvmeio *ctnio)
1110 memset(&ctnio->cpl, 0, sizeof(ctnio->cpl));
1111 ctnio->io_hdr.status = CTL_SUCCESS;
1115 ctl_io_set_invalid_opcode(union ctl_io *io)
1117 switch (io->io_hdr.io_type) {
1119 ctl_set_invalid_opcode(&io->scsiio);
1122 ctl_nvme_set_invalid_opcode(&io->nvmeio);
1125 __assert_unreachable();
1130 ctl_io_set_hw_write_protected(union ctl_io *io)
1132 switch (io->io_hdr.io_type) {
1134 ctl_set_hw_write_protected(&io->scsiio);
1137 ctl_nvme_set_namespace_is_write_protected(&io->nvmeio);
1140 __assert_unreachable();
1145 ctl_io_set_busy(union ctl_io *io)
1147 switch (io->io_hdr.io_type) {
1149 ctl_set_busy(&io->scsiio);
1152 ctl_nvme_set_namespace_not_ready(&io->nvmeio);
1155 __assert_unreachable();
1160 ctl_io_set_compare_failure(union ctl_io *io, uint64_t offset)
1164 switch (io->io_hdr.io_type) {
1166 scsi_u64to8b(offset, info);
1167 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
1168 /*sense_key*/ SSD_KEY_MISCOMPARE,
1169 /*asc*/ 0x1D, /*ascq*/ 0x00,
1170 /*type*/ SSD_ELEM_INFO,
1171 /*size*/ sizeof(info), /*data*/ &info,
1172 /*type*/ SSD_ELEM_NONE);
1175 ctl_nvme_set_compare_failure(&io->nvmeio);
1178 __assert_unreachable();
1183 ctl_io_set_space_alloc_fail(union ctl_io *io)
1185 switch (io->io_hdr.io_type) {
1187 ctl_set_space_alloc_fail(&io->scsiio);
1190 ctl_nvme_set_space_alloc_fail(&io->nvmeio);
1193 __assert_unreachable();
1198 ctl_io_set_success(union ctl_io *io)
1200 switch (io->io_hdr.io_type) {
1202 ctl_set_success(&io->scsiio);
1205 case CTL_IO_NVME_ADMIN:
1206 ctl_nvme_set_success(&io->nvmeio);
1209 __assert_unreachable();