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