]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_error.c
MFC r272040:
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_error.c
1 /*-
2  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3  * Copyright (c) 2011 Spectra Logic Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
17  *
18  * NO WARRANTY
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.
30  *
31  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
32  */
33 /*
34  * CAM Target Layer error reporting routines.
35  *
36  * Author: Ken Merry <ken@FreeBSD.org>
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
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>
47 #include <sys/lock.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>
54
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>
66
67 void
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) 
71 {
72         struct ctl_lun *lun;
73
74         lun = (struct ctl_lun *)lunptr;
75
76         /*
77          * Determine whether to return fixed or descriptor format sense
78          * data.
79          */
80         if (sense_format == SSD_TYPE_NONE) {
81                 /*
82                  * If the format isn't specified, we only return descriptor
83                  * sense if the LUN exists and descriptor sense is turned
84                  * on for that LUN.
85                  */
86                 if ((lun != NULL)
87                  && (lun->flags & CTL_LUN_SENSE_DESC))
88                         sense_format = SSD_TYPE_DESC;
89                 else
90                         sense_format = SSD_TYPE_FIXED;
91         }
92
93         scsi_set_sense_data_va(sense_data, sense_format, current_error,
94                                sense_key, asc, ascq, ap);
95 }
96
97 void
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, ...) 
101 {
102         va_list ap;
103
104         va_start(ap, ascq);
105         ctl_set_sense_data_va(sense_data, lunptr, sense_format, current_error,
106                               sense_key, asc, ascq, ap);
107         va_end(ap);
108 }
109
110 void
111 ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
112               int asc, int ascq, ...)
113 {
114         va_list ap;
115         struct ctl_lun *lun;
116
117         /*
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.
121          */
122         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
123
124         va_start(ap, ascq);
125         ctl_set_sense_data_va(&ctsio->sense_data,
126                               lun,
127                               SSD_TYPE_NONE,
128                               current_error,
129                               sense_key,
130                               asc,
131                               ascq,
132                               ap);
133         va_end(ap);
134
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;
138 }
139
140 /*
141  * Transform fixed sense data into descriptor sense data.
142  * 
143  * For simplicity's sake, we assume that both sense structures are
144  * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
145  */
146 void
147 ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
148                   struct scsi_sense_data_desc *sense_dest)
149 {
150         struct scsi_sense_stream stream_sense;
151         int current_error;
152         uint8_t stream_bits;
153
154         bzero(sense_dest, sizeof(*sense_dest));
155
156         if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
157                 current_error = 0;
158         else
159                 current_error = 1;
160
161         bzero(&stream_sense, sizeof(stream_sense));
162
163         /*
164          * Check to see whether any of the tape-specific bits are set.  If
165          * so, we'll need a stream sense descriptor.
166          */
167         if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
168                 stream_bits = sense_src->flags & ~SSD_KEY;
169         else
170                 stream_bits = 0;
171
172         /*
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.
176          */
177         ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
178                            /*lun*/ NULL,
179                            /*sense_format*/ SSD_TYPE_DESC,
180                            current_error,
181                            /*sense_key*/ sense_src->flags & SSD_KEY,
182                            /*asc*/ sense_src->add_sense_code,
183                            /*ascq*/ sense_src->add_sense_code_qual,
184
185                            /* Information Bytes */ 
186                            (scsi_4btoul(sense_src->info) != 0) ?
187                            SSD_ELEM_INFO : SSD_ELEM_SKIP,
188                            sizeof(sense_src->info),
189                            sense_src->info,
190
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,
196
197                            /* FRU */
198                            (sense_src->fru != 0) ?
199                            SSD_ELEM_FRU : SSD_ELEM_SKIP,
200                            sizeof(sense_src->fru),
201                            &sense_src->fru,
202
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,
208
209                            /* Tape bits */
210                            (stream_bits != 0) ?
211                            SSD_ELEM_STREAM : SSD_ELEM_SKIP,
212                            sizeof(stream_bits),
213                            &stream_bits,
214
215                            SSD_ELEM_NONE);
216 }
217
218 /*
219  * Transform descriptor format sense data into fixed sense data.
220  *
221  * Some data may be lost in translation, because there are descriptors
222  * thant can't be represented as fixed sense data.
223  *
224  * For simplicity's sake, we assume that both sense structures are
225  * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
226  */
227 void
228 ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
229                    struct scsi_sense_data_fixed *sense_dest)
230 {
231         int current_error;
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;
236         int pos;
237
238         if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
239                 current_error = 1;
240         else
241                 current_error = 0;
242
243         for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
244                 struct scsi_sense_desc_header *header;
245
246                 header = (struct scsi_sense_desc_header *)
247                     &sense_src->sense_desc[pos];
248
249                 /*
250                  * See if this record goes past the end of the sense data.
251                  * It shouldn't, but check just in case.
252                  */
253                 if ((pos + header->length + sizeof(*header)) >
254                      sense_src->extra_len)
255                         break;
256
257                 switch (sense_src->sense_desc[pos]) {
258                 case SSD_DESC_INFO: {
259                         struct scsi_sense_info *info;
260
261                         info = (struct scsi_sense_info *)header;
262
263                         info_ptr = info->info;
264                         info_size = sizeof(info->info);
265
266                         pos += info->length +
267                             sizeof(struct scsi_sense_desc_header);
268                         break;
269                 }
270                 case SSD_DESC_COMMAND: {
271                         struct scsi_sense_command *cmd;
272
273                         cmd = (struct scsi_sense_command *)header;
274                         cmd_ptr = cmd->command_info;
275                         cmd_size = sizeof(cmd->command_info);
276
277                         pos += cmd->length + 
278                             sizeof(struct scsi_sense_desc_header);
279                         break;
280                 }
281                 case SSD_DESC_FRU: {
282                         struct scsi_sense_fru *fru;
283
284                         fru = (struct scsi_sense_fru *)header;
285                         fru_ptr = &fru->fru;
286                         fru_size = sizeof(fru->fru);
287                         pos += fru->length +
288                             sizeof(struct scsi_sense_desc_header);
289                         break;
290                 }
291                 case SSD_DESC_SKS: {
292                         struct scsi_sense_sks *sks;
293
294                         sks = (struct scsi_sense_sks *)header;
295                         sks_ptr = sks->sense_key_spec;
296                         sks_size = sizeof(sks->sense_key_spec);
297
298                         pos = sks->length +
299                             sizeof(struct scsi_sense_desc_header);
300                         break;
301                 }
302                 case SSD_DESC_STREAM: {
303                         struct scsi_sense_stream *stream_sense;
304
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);
310                         break;
311                 }
312                 default:
313                         /*
314                          * We don't recognize this particular sense
315                          * descriptor type, so just skip it.
316                          */
317                         pos += sizeof(*header) + header->length;
318                         break;
319                 }
320         }
321
322         ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
323                            /*lun*/ NULL,
324                            /*sense_format*/ SSD_TYPE_FIXED,
325                            current_error,
326                            /*sense_key*/ sense_src->sense_key & SSD_KEY,
327                            /*asc*/ sense_src->add_sense_code,
328                            /*ascq*/ sense_src->add_sense_code_qual,
329
330                            /* Information Bytes */ 
331                            (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
332                            info_size,
333                            info_ptr,
334
335                            /* Command specific bytes */
336                            (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
337                            cmd_size,
338                            cmd_ptr,
339
340                            /* FRU */
341                            (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
342                            fru_size,
343                            fru_ptr,
344
345                            /* Sense Key Specific */
346                            (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
347                            sks_size,
348                            sks_ptr,
349
350                            /* Tape bits */
351                            (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
352                            stream_size,
353                            stream_ptr,
354
355                            SSD_ELEM_NONE);
356 }
357
358 void
359 ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
360 {
361         ctl_set_sense(ctsio,
362                       /*current_error*/ 1,
363                       /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
364                       asc,
365                       ascq,
366                       SSD_ELEM_NONE);
367 }
368
369 ctl_ua_type
370 ctl_build_ua(ctl_ua_type *ua_type, struct scsi_sense_data *sense,
371              scsi_sense_data_type sense_format)
372 {
373         ctl_ua_type ua_to_build, ua_to_clear;
374         int asc, ascq;
375
376         if (*ua_type == CTL_UA_NONE)
377                 return (CTL_UA_NONE);
378
379         ua_to_build = (1 << (ffs(*ua_type) - 1));
380         ua_to_clear = ua_to_build;
381
382         switch (ua_to_build) {
383         case CTL_UA_POWERON:
384                 /* 29h/01h  POWER ON OCCURRED */
385                 asc = 0x29;
386                 ascq = 0x01;
387                 ua_to_clear = ~0;
388                 break;
389         case CTL_UA_BUS_RESET:
390                 /* 29h/02h  SCSI BUS RESET OCCURRED */
391                 asc = 0x29;
392                 ascq = 0x02;
393                 ua_to_clear = ~0;
394                 break;
395         case CTL_UA_TARG_RESET:
396                 /* 29h/03h  BUS DEVICE RESET FUNCTION OCCURRED*/
397                 asc = 0x29;
398                 ascq = 0x03;
399                 ua_to_clear = ~0;
400                 break;
401         case CTL_UA_I_T_NEXUS_LOSS:
402                 /* 29h/07h  I_T NEXUS LOSS OCCURRED */
403                 asc = 0x29;
404                 ascq = 0x07;
405                 ua_to_clear = ~0;
406                 break;
407         case CTL_UA_LUN_RESET:
408                 /* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
409                 /*
410                  * Since we don't have a specific ASC/ASCQ pair for a LUN
411                  * reset, just return the generic reset code.
412                  */
413                 asc = 0x29;
414                 ascq = 0x00;
415                 break;
416         case CTL_UA_LUN_CHANGE:
417                 /* 3Fh/0Eh  REPORTED LUNS DATA HAS CHANGED */
418                 asc = 0x3F;
419                 ascq = 0x0E;
420                 break;
421         case CTL_UA_MODE_CHANGE:
422                 /* 2Ah/01h  MODE PARAMETERS CHANGED */
423                 asc = 0x2A;
424                 ascq = 0x01;
425                 break;
426         case CTL_UA_LOG_CHANGE:
427                 /* 2Ah/02h  LOG PARAMETERS CHANGED */
428                 asc = 0x2A;
429                 ascq = 0x02;
430                 break;
431         case CTL_UA_LVD:
432                 /* 29h/06h  TRANSCEIVER MODE CHANGED TO LVD */
433                 asc = 0x29;
434                 ascq = 0x06;
435                 break;
436         case CTL_UA_SE:
437                 /* 29h/05h  TRANSCEIVER MODE CHANGED TO SINGLE-ENDED */
438                 asc = 0x29;
439                 ascq = 0x05;
440                 break;
441         case CTL_UA_RES_PREEMPT:
442                 /* 2Ah/03h  RESERVATIONS PREEMPTED */
443                 asc = 0x2A;
444                 ascq = 0x03;
445                 break;
446         case CTL_UA_RES_RELEASE:
447                 /* 2Ah/04h  RESERVATIONS RELEASED */
448                 asc = 0x2A;
449                 ascq = 0x04;
450                 break;
451         case CTL_UA_REG_PREEMPT:
452                 /* 2Ah/05h  REGISTRATIONS PREEMPTED */
453                 asc = 0x2A;
454                 ascq = 0x05;
455                 break;
456         case CTL_UA_ASYM_ACC_CHANGE:
457                 /* 2Ah/06n  ASYMMETRIC ACCESS STATE CHANGED */
458                 asc = 0x2A;
459                 ascq = 0x06;
460                 break;
461         case CTL_UA_CAPACITY_CHANGED:
462                 /* 2Ah/09n  CAPACITY DATA HAS CHANGED */
463                 asc = 0x2A;
464                 ascq = 0x09;
465                 break;
466         default:
467                 panic("ctl_build_ua: Unknown UA %x", ua_to_build);
468         }
469
470         ctl_set_sense_data(sense,
471                            /*lun*/ NULL,
472                            sense_format,
473                            /*current_error*/ 1,
474                            /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
475                            asc,
476                            ascq,
477                            SSD_ELEM_NONE);
478
479         /* We're reporting this UA, so clear it */
480         *ua_type &= ~ua_to_clear;
481
482         return (ua_to_build);
483 }
484
485 void
486 ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
487 {
488         /* OVERLAPPED COMMANDS ATTEMPTED */
489         ctl_set_sense(ctsio,
490                       /*current_error*/ 1,
491                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
492                       /*asc*/ 0x4E,
493                       /*ascq*/ 0x00,
494                       SSD_ELEM_NONE);
495 }
496
497 void
498 ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
499 {
500         /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
501         ctl_set_sense(ctsio,
502                       /*current_error*/ 1,
503                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
504                       /*asc*/ 0x4D,
505                       /*ascq*/ tag,
506                       SSD_ELEM_NONE);
507 }
508
509 /*
510  * Tell the user that there was a problem with the command or data he sent.
511  */
512 void
513 ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
514                       int field, int bit_valid, int bit)
515 {
516         uint8_t sks[3];
517         int asc;
518
519         if (command != 0) {
520                 /* "Invalid field in CDB" */
521                 asc = 0x24;
522         } else {
523                 /* "Invalid field in parameter list" */
524                 asc = 0x26;
525         }
526
527         if (sks_valid) {
528                 sks[0] = SSD_SCS_VALID;
529                 if (command)
530                         sks[0] |= SSD_FIELDPTR_CMD;
531                 scsi_ulto2b(field, &sks[1]);
532
533                 if (bit_valid)
534                         sks[0] |= SSD_BITPTR_VALID | bit;
535         }
536
537         ctl_set_sense(ctsio,
538                       /*current_error*/ 1,
539                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
540                       asc,
541                       /*ascq*/ 0x00,
542                       /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
543                       /*size*/ sizeof(sks),
544                       /*data*/ sks,
545                       SSD_ELEM_NONE);
546 }
547
548 void
549 ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
550 {
551         struct scsi_sense_data *sense;
552         uint8_t sks[3];
553
554         sense = &ctsio->sense_data;
555
556         sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
557         scsi_ulto2b(0, &sks[1]);
558
559         /* "Invalid command operation code" */
560         ctl_set_sense(ctsio,
561                       /*current_error*/ 1,
562                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
563                       /*asc*/ 0x20,
564                       /*ascq*/ 0x00,
565                       /*type*/ SSD_ELEM_SKS,
566                       /*size*/ sizeof(sks),
567                       /*data*/ sks,
568                       SSD_ELEM_NONE);
569 }
570
571 void
572 ctl_set_param_len_error(struct ctl_scsiio *ctsio)
573 {
574         /* "Parameter list length error" */
575         ctl_set_sense(ctsio,
576                       /*current_error*/ 1,
577                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
578                       /*asc*/ 0x1a,
579                       /*ascq*/ 0x00,
580                       SSD_ELEM_NONE);
581 }
582
583 void
584 ctl_set_already_locked(struct ctl_scsiio *ctsio)
585 {
586         /* Vendor unique "Somebody already is locked" */
587         ctl_set_sense(ctsio,
588                       /*current_error*/ 1,
589                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
590                       /*asc*/ 0x81,
591                       /*ascq*/ 0x00,
592                       SSD_ELEM_NONE);
593 }
594
595 void
596 ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
597 {
598         /* "Logical unit not supported" */
599         ctl_set_sense(ctsio,
600                       /*current_error*/ 1,
601                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
602                       /*asc*/ 0x25,
603                       /*ascq*/ 0x00,
604                       SSD_ELEM_NONE);
605 }
606
607 void
608 ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
609                          uint16_t retry_count)
610 {
611         uint8_t sks[3];
612
613         if (sks_valid) {
614                 sks[0] = SSD_SCS_VALID;
615                 sks[1] = (retry_count >> 8) & 0xff;
616                 sks[2] = retry_count & 0xff;
617         }
618
619         /* "Internal target failure" */
620         ctl_set_sense(ctsio,
621                       /*current_error*/ 1,
622                       /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
623                       /*asc*/ 0x44,
624                       /*ascq*/ 0x00,
625                       /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
626                       /*size*/ sizeof(sks),
627                       /*data*/ sks,
628                       SSD_ELEM_NONE);
629 }
630
631 void
632 ctl_set_medium_error(struct ctl_scsiio *ctsio)
633 {
634         if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
635                 /* "Unrecovered read error" */
636                 ctl_set_sense(ctsio,
637                               /*current_error*/ 1,
638                               /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
639                               /*asc*/ 0x11,
640                               /*ascq*/ 0x00,
641                               SSD_ELEM_NONE);
642         } else {
643                 /* "Write error - auto reallocation failed" */
644                 ctl_set_sense(ctsio,
645                               /*current_error*/ 1,
646                               /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
647                               /*asc*/ 0x0C,
648                               /*ascq*/ 0x02,
649                               SSD_ELEM_NONE);
650         }
651 }
652
653 void
654 ctl_set_aborted(struct ctl_scsiio *ctsio)
655 {
656         ctl_set_sense(ctsio,
657                       /*current_error*/ 1,
658                       /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
659                       /*asc*/ 0x45,
660                       /*ascq*/ 0x00,
661                       SSD_ELEM_NONE);
662 }
663
664 void
665 ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio)
666 {
667         /* "Logical block address out of range" */
668         ctl_set_sense(ctsio,
669                       /*current_error*/ 1,
670                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
671                       /*asc*/ 0x21,
672                       /*ascq*/ 0x00,
673                       SSD_ELEM_NONE);
674 }
675
676 void
677 ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
678 {
679         /* "Logical unit not ready, initializing cmd. required" */
680         ctl_set_sense(ctsio,
681                       /*current_error*/ 1,
682                       /*sense_key*/ SSD_KEY_NOT_READY,
683                       /*asc*/ 0x04,
684                       /*ascq*/ 0x02,
685                       SSD_ELEM_NONE);
686 }
687
688 void
689 ctl_set_lun_not_ready(struct ctl_scsiio *ctsio)
690 {
691         /* "Logical unit not ready, manual intervention required" */
692         ctl_set_sense(ctsio,
693                       /*current_error*/ 1,
694                       /*sense_key*/ SSD_KEY_NOT_READY,
695                       /*asc*/ 0x04,
696                       /*ascq*/ 0x05,
697                       SSD_ELEM_NONE);
698 }
699
700 void
701 ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
702 {
703         /* "Invalid release of persistent reservation" */
704         ctl_set_sense(ctsio,
705                       /*current_error*/ 1,
706                       /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
707                       /*asc*/ 0x26,
708                       /*ascq*/ 0x04,
709                       SSD_ELEM_NONE);
710 }
711
712 void
713 ctl_set_lun_standby(struct ctl_scsiio *ctsio)
714 {
715         /* "Logical unit not ready, target port in standby state" */
716         ctl_set_sense(ctsio,
717                       /*current_error*/ 1,
718                       /*sense_key*/ SSD_KEY_NOT_READY,
719                       /*asc*/ 0x04,
720                       /*ascq*/ 0x0b,
721                       SSD_ELEM_NONE);
722 }
723
724 void
725 ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
726 {
727         /* "Medium format corrupted" */
728         ctl_set_sense(ctsio,
729                       /*current_error*/ 1,
730                       /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
731                       /*asc*/ 0x31,
732                       /*ascq*/ 0x00,
733                       SSD_ELEM_NONE);
734 }
735
736 void
737 ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
738 {
739         /* "Medium magazine not accessible" */
740         ctl_set_sense(ctsio,
741                       /*current_error*/ 1,
742                       /*sense_key*/ SSD_KEY_NOT_READY,
743                       /*asc*/ 0x3b,
744                       /*ascq*/ 0x11,
745                       SSD_ELEM_NONE);
746 }
747
748 void
749 ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
750 {
751         /* "Data phase error" */
752         ctl_set_sense(ctsio,
753                       /*current_error*/ 1,
754                       /*sense_key*/ SSD_KEY_NOT_READY,
755                       /*asc*/ 0x4b,
756                       /*ascq*/ 0x00,
757                       SSD_ELEM_NONE);
758 }
759
760 void
761 ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
762 {
763         struct scsi_sense_data *sense;
764
765         sense = &ctsio->sense_data;
766         memset(sense, 0, sizeof(*sense));
767         ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
768         ctsio->sense_len = 0;
769         ctsio->io_hdr.status = CTL_SCSI_ERROR;
770 }
771
772 void
773 ctl_set_queue_full(struct ctl_scsiio *ctsio)
774 {
775         struct scsi_sense_data *sense;
776
777         sense = &ctsio->sense_data;
778         memset(sense, 0, sizeof(*sense));
779         ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
780         ctsio->sense_len = 0;
781         ctsio->io_hdr.status = CTL_SCSI_ERROR;
782 }
783
784 void
785 ctl_set_busy(struct ctl_scsiio *ctsio)
786 {
787         struct scsi_sense_data *sense;
788
789         sense = &ctsio->sense_data;
790         memset(sense, 0, sizeof(*sense));
791         ctsio->scsi_status = SCSI_STATUS_BUSY;
792         ctsio->sense_len = 0;
793         ctsio->io_hdr.status = CTL_SCSI_ERROR;
794 }
795
796 void
797 ctl_set_task_aborted(struct ctl_scsiio *ctsio)
798 {
799         struct scsi_sense_data *sense;
800
801         sense = &ctsio->sense_data;
802         memset(sense, 0, sizeof(*sense));
803         ctsio->scsi_status = SCSI_STATUS_TASK_ABORTED;
804         ctsio->sense_len = 0;
805         ctsio->io_hdr.status = CTL_CMD_ABORTED;
806 }
807
808 void
809 ctl_set_success(struct ctl_scsiio *ctsio)
810 {
811         struct scsi_sense_data *sense;
812
813         sense = &ctsio->sense_data;
814         memset(sense, 0, sizeof(*sense));
815         ctsio->scsi_status = SCSI_STATUS_OK;
816         ctsio->sense_len = 0;
817         ctsio->io_hdr.status = CTL_SUCCESS;
818 }