ctl_error.c revision 314380
1/*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2011 Spectra Logic Corporation
4 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions, and the following disclaimer,
12 *    without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 *    substantially similar to the "NO WARRANTY" disclaimer below
15 *    ("Disclaimer") and any redistribution must be conditioned upon
16 *    including a substantially similar Disclaimer requirement for further
17 *    binary redistribution.
18 *
19 * NO WARRANTY
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
31 *
32 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
33 */
34/*
35 * CAM Target Layer error reporting routines.
36 *
37 * Author: Ken Merry <ken@FreeBSD.org>
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_error.c 314380 2017-02-28 06:32:01Z mav $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/types.h>
47#include <sys/malloc.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/condvar.h>
51#include <sys/stddef.h>
52#include <sys/ctype.h>
53#include <sys/sysctl.h>
54#include <machine/stdarg.h>
55
56#include <cam/scsi/scsi_all.h>
57#include <cam/scsi/scsi_da.h>
58#include <cam/ctl/ctl_io.h>
59#include <cam/ctl/ctl.h>
60#include <cam/ctl/ctl_frontend.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
67void
68ctl_set_sense_data_va(struct scsi_sense_data *sense_data, u_int *sense_len,
69    void *lunptr, 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) && (lun->MODE_CTRL.rlec & SCP_DSENSE))
87			sense_format = SSD_TYPE_DESC;
88		else
89			sense_format = SSD_TYPE_FIXED;
90	}
91
92	/*
93	 * Determine maximum sense data length to return.
94	 */
95	if (*sense_len == 0) {
96		if ((lun != NULL) && (lun->MODE_CTRLE.max_sense != 0))
97			*sense_len = lun->MODE_CTRLE.max_sense;
98		else
99			*sense_len = SSD_FULL_SIZE;
100	}
101
102	scsi_set_sense_data_va(sense_data, sense_len, sense_format,
103	    current_error, sense_key, asc, ascq, ap);
104}
105
106void
107ctl_set_sense_data(struct scsi_sense_data *sense_data, u_int *sense_len,
108    void *lunptr, scsi_sense_data_type sense_format, int current_error,
109    int sense_key, int asc, int ascq, ...)
110{
111	va_list ap;
112
113	va_start(ap, ascq);
114	ctl_set_sense_data_va(sense_data, sense_len, lunptr, sense_format,
115	    current_error, sense_key, asc, ascq, ap);
116	va_end(ap);
117}
118
119void
120ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
121	      int asc, int ascq, ...)
122{
123	va_list ap;
124	struct ctl_lun *lun;
125	u_int sense_len;
126
127	/*
128	 * The LUN can't go away until all of the commands have been
129	 * completed.  Therefore we can safely access the LUN structure and
130	 * flags without the lock.
131	 */
132	lun = CTL_LUN(ctsio);
133
134	va_start(ap, ascq);
135	sense_len = 0;
136	ctl_set_sense_data_va(&ctsio->sense_data, &sense_len,
137			      lun,
138			      SSD_TYPE_NONE,
139			      current_error,
140			      sense_key,
141			      asc,
142			      ascq,
143			      ap);
144	va_end(ap);
145
146	ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
147	ctsio->sense_len = sense_len;
148	ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
149}
150
151/*
152 * Transform fixed sense data into descriptor sense data.
153 *
154 * For simplicity's sake, we assume that both sense structures are
155 * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
156 */
157void
158ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
159		  struct scsi_sense_data_desc *sense_dest)
160{
161	struct scsi_sense_stream stream_sense;
162	int current_error;
163	u_int sense_len;
164	uint8_t stream_bits;
165
166	bzero(sense_dest, sizeof(*sense_dest));
167
168	if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
169		current_error = 0;
170	else
171		current_error = 1;
172
173	bzero(&stream_sense, sizeof(stream_sense));
174
175	/*
176	 * Check to see whether any of the tape-specific bits are set.  If
177	 * so, we'll need a stream sense descriptor.
178	 */
179	if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
180		stream_bits = sense_src->flags & ~SSD_KEY;
181	else
182		stream_bits = 0;
183
184	/*
185	 * Utilize our sense setting routine to do the transform.  If a
186	 * value is set in the fixed sense data, set it in the descriptor
187	 * data.  Otherwise, skip it.
188	 */
189	sense_len = SSD_FULL_SIZE;
190	ctl_set_sense_data((struct scsi_sense_data *)sense_dest, &sense_len,
191			   /*lun*/ NULL,
192			   /*sense_format*/ SSD_TYPE_DESC,
193			   current_error,
194			   /*sense_key*/ sense_src->flags & SSD_KEY,
195			   /*asc*/ sense_src->add_sense_code,
196			   /*ascq*/ sense_src->add_sense_code_qual,
197
198			   /* Information Bytes */
199			   (sense_src->error_code & SSD_ERRCODE_VALID) ?
200			   SSD_ELEM_INFO : SSD_ELEM_SKIP,
201			   sizeof(sense_src->info),
202			   sense_src->info,
203
204			   /* Command specific bytes */
205			   (scsi_4btoul(sense_src->cmd_spec_info) != 0) ?
206			   SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
207			   sizeof(sense_src->cmd_spec_info),
208			   sense_src->cmd_spec_info,
209
210			   /* FRU */
211			   (sense_src->fru != 0) ?
212			   SSD_ELEM_FRU : SSD_ELEM_SKIP,
213			   sizeof(sense_src->fru),
214			   &sense_src->fru,
215
216			   /* Sense Key Specific */
217			   (sense_src->sense_key_spec[0] & SSD_SCS_VALID) ?
218			   SSD_ELEM_SKS : SSD_ELEM_SKIP,
219			   sizeof(sense_src->sense_key_spec),
220			   sense_src->sense_key_spec,
221
222			   /* Tape bits */
223			   (stream_bits != 0) ?
224			   SSD_ELEM_STREAM : SSD_ELEM_SKIP,
225			   sizeof(stream_bits),
226			   &stream_bits,
227
228			   SSD_ELEM_NONE);
229}
230
231/*
232 * Transform descriptor format sense data into fixed sense data.
233 *
234 * Some data may be lost in translation, because there are descriptors
235 * thant can't be represented as fixed sense data.
236 *
237 * For simplicity's sake, we assume that both sense structures are
238 * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
239 */
240void
241ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
242		   struct scsi_sense_data_fixed *sense_dest)
243{
244	int current_error;
245	uint8_t *info_ptr = NULL, *cmd_ptr = NULL, *fru_ptr = NULL;
246	uint8_t *sks_ptr = NULL, *stream_ptr = NULL;
247	int info_size = 0, cmd_size = 0, fru_size = 0;
248	int sks_size = 0, stream_size = 0;
249	int pos;
250	u_int sense_len;
251
252	if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
253		current_error = 1;
254	else
255		current_error = 0;
256
257	for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
258		struct scsi_sense_desc_header *header;
259
260		header = (struct scsi_sense_desc_header *)
261		    &sense_src->sense_desc[pos];
262
263		/*
264		 * See if this record goes past the end of the sense data.
265		 * It shouldn't, but check just in case.
266		 */
267		if ((pos + header->length + sizeof(*header)) >
268		     sense_src->extra_len)
269			break;
270
271		switch (sense_src->sense_desc[pos]) {
272		case SSD_DESC_INFO: {
273			struct scsi_sense_info *info;
274
275			info = (struct scsi_sense_info *)header;
276
277			info_ptr = info->info;
278			info_size = sizeof(info->info);
279
280			pos += info->length +
281			    sizeof(struct scsi_sense_desc_header);
282			break;
283		}
284		case SSD_DESC_COMMAND: {
285			struct scsi_sense_command *cmd;
286
287			cmd = (struct scsi_sense_command *)header;
288			cmd_ptr = cmd->command_info;
289			cmd_size = sizeof(cmd->command_info);
290
291			pos += cmd->length +
292			    sizeof(struct scsi_sense_desc_header);
293			break;
294		}
295		case SSD_DESC_FRU: {
296			struct scsi_sense_fru *fru;
297
298			fru = (struct scsi_sense_fru *)header;
299			fru_ptr = &fru->fru;
300			fru_size = sizeof(fru->fru);
301			pos += fru->length +
302			    sizeof(struct scsi_sense_desc_header);
303			break;
304		}
305		case SSD_DESC_SKS: {
306			struct scsi_sense_sks *sks;
307
308			sks = (struct scsi_sense_sks *)header;
309			sks_ptr = sks->sense_key_spec;
310			sks_size = sizeof(sks->sense_key_spec);
311
312			pos = sks->length +
313			    sizeof(struct scsi_sense_desc_header);
314			break;
315		}
316		case SSD_DESC_STREAM: {
317			struct scsi_sense_stream *stream_sense;
318
319			stream_sense = (struct scsi_sense_stream *)header;
320			stream_ptr = &stream_sense->byte3;
321			stream_size = sizeof(stream_sense->byte3);
322			pos = stream_sense->length +
323			    sizeof(struct scsi_sense_desc_header);
324			break;
325		}
326		default:
327			/*
328			 * We don't recognize this particular sense
329			 * descriptor type, so just skip it.
330			 */
331			pos += sizeof(*header) + header->length;
332			break;
333		}
334	}
335
336	sense_len = SSD_FULL_SIZE;
337	ctl_set_sense_data((struct scsi_sense_data *)sense_dest, &sense_len,
338			   /*lun*/ NULL,
339			   /*sense_format*/ SSD_TYPE_FIXED,
340			   current_error,
341			   /*sense_key*/ sense_src->sense_key & SSD_KEY,
342			   /*asc*/ sense_src->add_sense_code,
343			   /*ascq*/ sense_src->add_sense_code_qual,
344
345			   /* Information Bytes */
346			   (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
347			   info_size,
348			   info_ptr,
349
350			   /* Command specific bytes */
351			   (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
352			   cmd_size,
353			   cmd_ptr,
354
355			   /* FRU */
356			   (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
357			   fru_size,
358			   fru_ptr,
359
360			   /* Sense Key Specific */
361			   (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
362			   sks_size,
363			   sks_ptr,
364
365			   /* Tape bits */
366			   (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
367			   stream_size,
368			   stream_ptr,
369
370			   SSD_ELEM_NONE);
371}
372
373void
374ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
375{
376	ctl_set_sense(ctsio,
377		      /*current_error*/ 1,
378		      /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
379		      asc,
380		      ascq,
381		      SSD_ELEM_NONE);
382}
383
384static void
385ctl_ua_to_ascq(struct ctl_lun *lun, ctl_ua_type ua_to_build, int *asc,
386    int *ascq, ctl_ua_type *ua_to_clear, uint8_t **info)
387{
388
389	switch (ua_to_build) {
390	case CTL_UA_POWERON:
391		/* 29h/01h  POWER ON OCCURRED */
392		*asc = 0x29;
393		*ascq = 0x01;
394		*ua_to_clear = ~0;
395		break;
396	case CTL_UA_BUS_RESET:
397		/* 29h/02h  SCSI BUS RESET OCCURRED */
398		*asc = 0x29;
399		*ascq = 0x02;
400		*ua_to_clear = ~0;
401		break;
402	case CTL_UA_TARG_RESET:
403		/* 29h/03h  BUS DEVICE RESET FUNCTION OCCURRED*/
404		*asc = 0x29;
405		*ascq = 0x03;
406		*ua_to_clear = ~0;
407		break;
408	case CTL_UA_I_T_NEXUS_LOSS:
409		/* 29h/07h  I_T NEXUS LOSS OCCURRED */
410		*asc = 0x29;
411		*ascq = 0x07;
412		*ua_to_clear = ~0;
413		break;
414	case CTL_UA_LUN_RESET:
415		/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
416		/*
417		 * Since we don't have a specific ASC/ASCQ pair for a LUN
418		 * reset, just return the generic reset code.
419		 */
420		*asc = 0x29;
421		*ascq = 0x00;
422		break;
423	case CTL_UA_LUN_CHANGE:
424		/* 3Fh/0Eh  REPORTED LUNS DATA HAS CHANGED */
425		*asc = 0x3F;
426		*ascq = 0x0E;
427		break;
428	case CTL_UA_MODE_CHANGE:
429		/* 2Ah/01h  MODE PARAMETERS CHANGED */
430		*asc = 0x2A;
431		*ascq = 0x01;
432		break;
433	case CTL_UA_LOG_CHANGE:
434		/* 2Ah/02h  LOG PARAMETERS CHANGED */
435		*asc = 0x2A;
436		*ascq = 0x02;
437		break;
438	case CTL_UA_INQ_CHANGE:
439		/* 3Fh/03h  INQUIRY DATA HAS CHANGED */
440		*asc = 0x3F;
441		*ascq = 0x03;
442		break;
443	case CTL_UA_RES_PREEMPT:
444		/* 2Ah/03h  RESERVATIONS PREEMPTED */
445		*asc = 0x2A;
446		*ascq = 0x03;
447		break;
448	case CTL_UA_RES_RELEASE:
449		/* 2Ah/04h  RESERVATIONS RELEASED */
450		*asc = 0x2A;
451		*ascq = 0x04;
452		break;
453	case CTL_UA_REG_PREEMPT:
454		/* 2Ah/05h  REGISTRATIONS PREEMPTED */
455		*asc = 0x2A;
456		*ascq = 0x05;
457		break;
458	case CTL_UA_ASYM_ACC_CHANGE:
459		/* 2Ah/06h  ASYMMETRIC ACCESS STATE CHANGED */
460		*asc = 0x2A;
461		*ascq = 0x06;
462		break;
463	case CTL_UA_CAPACITY_CHANGE:
464		/* 2Ah/09h  CAPACITY DATA HAS CHANGED */
465		*asc = 0x2A;
466		*ascq = 0x09;
467		break;
468	case CTL_UA_THIN_PROV_THRES:
469		/* 38h/07h  THIN PROVISIONING SOFT THRESHOLD REACHED */
470		*asc = 0x38;
471		*ascq = 0x07;
472		*info = lun->ua_tpt_info;
473		break;
474	case CTL_UA_MEDIUM_CHANGE:
475		/* 28h/00h  NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
476		*asc = 0x28;
477		*ascq = 0x00;
478		break;
479	case CTL_UA_IE:
480		/* Informational exception */
481		*asc = lun->ie_asc;
482		*ascq = lun->ie_ascq;
483		break;
484	default:
485		panic("%s: Unknown UA %x", __func__, ua_to_build);
486	}
487}
488
489ctl_ua_type
490ctl_build_qae(struct ctl_lun *lun, uint32_t initidx, uint8_t *resp)
491{
492	ctl_ua_type ua;
493	ctl_ua_type ua_to_build, ua_to_clear;
494	uint8_t *info;
495	int asc, ascq;
496	uint32_t p, i;
497
498	mtx_assert(&lun->lun_lock, MA_OWNED);
499	p = initidx / CTL_MAX_INIT_PER_PORT;
500	i = initidx % CTL_MAX_INIT_PER_PORT;
501	if (lun->pending_ua[p] == NULL)
502		ua = CTL_UA_POWERON;
503	else
504		ua = lun->pending_ua[p][i];
505	if (ua == CTL_UA_NONE)
506		return (CTL_UA_NONE);
507
508	ua_to_build = (1 << (ffs(ua) - 1));
509	ua_to_clear = ua_to_build;
510	info = NULL;
511	ctl_ua_to_ascq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
512
513	resp[0] = SSD_KEY_UNIT_ATTENTION;
514	if (ua_to_build == ua)
515		resp[0] |= 0x10;
516	else
517		resp[0] |= 0x20;
518	resp[1] = asc;
519	resp[2] = ascq;
520	return (ua_to_build);
521}
522
523ctl_ua_type
524ctl_build_ua(struct ctl_lun *lun, uint32_t initidx,
525    struct scsi_sense_data *sense, u_int *sense_len,
526    scsi_sense_data_type sense_format)
527{
528	ctl_ua_type *ua;
529	ctl_ua_type ua_to_build, ua_to_clear;
530	uint8_t *info;
531	int asc, ascq;
532	uint32_t p, i;
533
534	mtx_assert(&lun->lun_lock, MA_OWNED);
535	mtx_assert(&lun->ctl_softc->ctl_lock, MA_NOTOWNED);
536	p = initidx / CTL_MAX_INIT_PER_PORT;
537	if ((ua = lun->pending_ua[p]) == NULL) {
538		mtx_unlock(&lun->lun_lock);
539		ua = malloc(sizeof(ctl_ua_type) * CTL_MAX_INIT_PER_PORT,
540		    M_CTL, M_WAITOK);
541		mtx_lock(&lun->lun_lock);
542		if (lun->pending_ua[p] == NULL) {
543			lun->pending_ua[p] = ua;
544			for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++)
545				ua[i] = CTL_UA_POWERON;
546		} else {
547			free(ua, M_CTL);
548			ua = lun->pending_ua[p];
549		}
550	}
551	i = initidx % CTL_MAX_INIT_PER_PORT;
552	if (ua[i] == CTL_UA_NONE)
553		return (CTL_UA_NONE);
554
555	ua_to_build = (1 << (ffs(ua[i]) - 1));
556	ua_to_clear = ua_to_build;
557	info = NULL;
558	ctl_ua_to_ascq(lun, ua_to_build, &asc, &ascq, &ua_to_clear, &info);
559
560	ctl_set_sense_data(sense, sense_len, lun, sense_format, 1,
561	    /*sense_key*/ SSD_KEY_UNIT_ATTENTION, asc, ascq,
562	    ((info != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP), 8, info,
563	    SSD_ELEM_NONE);
564
565	/* We're reporting this UA, so clear it */
566	ua[i] &= ~ua_to_clear;
567
568	if (ua_to_build == CTL_UA_LUN_CHANGE) {
569		mtx_unlock(&lun->lun_lock);
570		mtx_lock(&lun->ctl_softc->ctl_lock);
571		ctl_clr_ua_allluns(lun->ctl_softc, initidx, ua_to_build);
572		mtx_unlock(&lun->ctl_softc->ctl_lock);
573		mtx_lock(&lun->lun_lock);
574	} else if (ua_to_build == CTL_UA_THIN_PROV_THRES &&
575	    (lun->MODE_LBP.main.flags & SLBPP_SITUA) != 0) {
576		ctl_clr_ua_all(lun, -1, ua_to_build);
577	}
578
579	return (ua_to_build);
580}
581
582void
583ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
584{
585	/* OVERLAPPED COMMANDS ATTEMPTED */
586	ctl_set_sense(ctsio,
587		      /*current_error*/ 1,
588		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
589		      /*asc*/ 0x4E,
590		      /*ascq*/ 0x00,
591		      SSD_ELEM_NONE);
592}
593
594void
595ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
596{
597	/* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
598	ctl_set_sense(ctsio,
599		      /*current_error*/ 1,
600		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
601		      /*asc*/ 0x4D,
602		      /*ascq*/ tag,
603		      SSD_ELEM_NONE);
604}
605
606/*
607 * Tell the user that there was a problem with the command or data he sent.
608 */
609void
610ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
611		      int field, int bit_valid, int bit)
612{
613	uint8_t sks[3];
614	int asc;
615
616	if (command != 0) {
617		/* "Invalid field in CDB" */
618		asc = 0x24;
619	} else {
620		/* "Invalid field in parameter list" */
621		asc = 0x26;
622	}
623
624	if (sks_valid) {
625		sks[0] = SSD_SCS_VALID;
626		if (command)
627			sks[0] |= SSD_FIELDPTR_CMD;
628		scsi_ulto2b(field, &sks[1]);
629
630		if (bit_valid)
631			sks[0] |= SSD_BITPTR_VALID | bit;
632	}
633
634	ctl_set_sense(ctsio,
635		      /*current_error*/ 1,
636		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
637		      asc,
638		      /*ascq*/ 0x00,
639		      /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
640		      /*size*/ sizeof(sks),
641		      /*data*/ sks,
642		      SSD_ELEM_NONE);
643}
644void
645ctl_set_invalid_field_ciu(struct ctl_scsiio *ctsio)
646{
647
648	/* "Invalid field in command information unit" */
649	ctl_set_sense(ctsio,
650		      /*current_error*/ 1,
651		      /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
652		      /*ascq*/ 0x0E,
653		      /*ascq*/ 0x03,
654		      SSD_ELEM_NONE);
655}
656
657void
658ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
659{
660	uint8_t sks[3];
661
662	sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
663	scsi_ulto2b(0, &sks[1]);
664
665	/* "Invalid command operation code" */
666	ctl_set_sense(ctsio,
667		      /*current_error*/ 1,
668		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
669		      /*asc*/ 0x20,
670		      /*ascq*/ 0x00,
671		      /*type*/ SSD_ELEM_SKS,
672		      /*size*/ sizeof(sks),
673		      /*data*/ sks,
674		      SSD_ELEM_NONE);
675}
676
677void
678ctl_set_param_len_error(struct ctl_scsiio *ctsio)
679{
680	/* "Parameter list length error" */
681	ctl_set_sense(ctsio,
682		      /*current_error*/ 1,
683		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
684		      /*asc*/ 0x1a,
685		      /*ascq*/ 0x00,
686		      SSD_ELEM_NONE);
687}
688
689void
690ctl_set_already_locked(struct ctl_scsiio *ctsio)
691{
692	/* Vendor unique "Somebody already is locked" */
693	ctl_set_sense(ctsio,
694		      /*current_error*/ 1,
695		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
696		      /*asc*/ 0x81,
697		      /*ascq*/ 0x00,
698		      SSD_ELEM_NONE);
699}
700
701void
702ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
703{
704	/* "Logical unit not supported" */
705	ctl_set_sense(ctsio,
706		      /*current_error*/ 1,
707		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
708		      /*asc*/ 0x25,
709		      /*ascq*/ 0x00,
710		      SSD_ELEM_NONE);
711}
712
713void
714ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
715			 uint16_t retry_count)
716{
717	uint8_t sks[3];
718
719	if (sks_valid) {
720		sks[0] = SSD_SCS_VALID;
721		sks[1] = (retry_count >> 8) & 0xff;
722		sks[2] = retry_count & 0xff;
723	}
724
725	/* "Internal target failure" */
726	ctl_set_sense(ctsio,
727		      /*current_error*/ 1,
728		      /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
729		      /*asc*/ 0x44,
730		      /*ascq*/ 0x00,
731		      /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
732		      /*size*/ sizeof(sks),
733		      /*data*/ sks,
734		      SSD_ELEM_NONE);
735}
736
737void
738ctl_set_medium_error(struct ctl_scsiio *ctsio, int read)
739{
740	if (read) {
741		/* "Unrecovered read error" */
742		ctl_set_sense(ctsio,
743			      /*current_error*/ 1,
744			      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
745			      /*asc*/ 0x11,
746			      /*ascq*/ 0x00,
747			      SSD_ELEM_NONE);
748	} else {
749		/* "Write error - auto reallocation failed" */
750		ctl_set_sense(ctsio,
751			      /*current_error*/ 1,
752			      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
753			      /*asc*/ 0x0C,
754			      /*ascq*/ 0x02,
755			      SSD_ELEM_NONE);
756	}
757}
758
759void
760ctl_set_aborted(struct ctl_scsiio *ctsio)
761{
762	ctl_set_sense(ctsio,
763		      /*current_error*/ 1,
764		      /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
765		      /*asc*/ 0x45,
766		      /*ascq*/ 0x00,
767		      SSD_ELEM_NONE);
768}
769
770void
771ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio, uint64_t lba)
772{
773	uint8_t	info[8];
774
775	scsi_u64to8b(lba, info);
776
777	/* "Logical block address out of range" */
778	ctl_set_sense(ctsio,
779		      /*current_error*/ 1,
780		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
781		      /*asc*/ 0x21,
782		      /*ascq*/ 0x00,
783		      /*type*/ (lba != 0) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
784		      /*size*/ sizeof(info), /*data*/ &info,
785		      SSD_ELEM_NONE);
786}
787
788void
789ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
790{
791	/* "Logical unit not ready, initializing cmd. required" */
792	ctl_set_sense(ctsio,
793		      /*current_error*/ 1,
794		      /*sense_key*/ SSD_KEY_NOT_READY,
795		      /*asc*/ 0x04,
796		      /*ascq*/ 0x02,
797		      SSD_ELEM_NONE);
798}
799
800void
801ctl_set_lun_int_reqd(struct ctl_scsiio *ctsio)
802{
803	/* "Logical unit not ready, manual intervention required" */
804	ctl_set_sense(ctsio,
805		      /*current_error*/ 1,
806		      /*sense_key*/ SSD_KEY_NOT_READY,
807		      /*asc*/ 0x04,
808		      /*ascq*/ 0x03,
809		      SSD_ELEM_NONE);
810}
811
812void
813ctl_set_lun_ejected(struct ctl_scsiio *ctsio)
814{
815	/* "Medium not present - tray open" */
816	ctl_set_sense(ctsio,
817		      /*current_error*/ 1,
818		      /*sense_key*/ SSD_KEY_NOT_READY,
819		      /*asc*/ 0x3A,
820		      /*ascq*/ 0x02,
821		      SSD_ELEM_NONE);
822}
823
824void
825ctl_set_lun_no_media(struct ctl_scsiio *ctsio)
826{
827	/* "Medium not present - tray closed" */
828	ctl_set_sense(ctsio,
829		      /*current_error*/ 1,
830		      /*sense_key*/ SSD_KEY_NOT_READY,
831		      /*asc*/ 0x3A,
832		      /*ascq*/ 0x01,
833		      SSD_ELEM_NONE);
834}
835
836void
837ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
838{
839	/* "Invalid release of persistent reservation" */
840	ctl_set_sense(ctsio,
841		      /*current_error*/ 1,
842		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
843		      /*asc*/ 0x26,
844		      /*ascq*/ 0x04,
845		      SSD_ELEM_NONE);
846}
847
848void
849ctl_set_lun_transit(struct ctl_scsiio *ctsio)
850{
851	/* "Logical unit not ready, asymmetric access state transition" */
852	ctl_set_sense(ctsio,
853		      /*current_error*/ 1,
854		      /*sense_key*/ SSD_KEY_NOT_READY,
855		      /*asc*/ 0x04,
856		      /*ascq*/ 0x0a,
857		      SSD_ELEM_NONE);
858}
859
860void
861ctl_set_lun_standby(struct ctl_scsiio *ctsio)
862{
863	/* "Logical unit not ready, target port in standby state" */
864	ctl_set_sense(ctsio,
865		      /*current_error*/ 1,
866		      /*sense_key*/ SSD_KEY_NOT_READY,
867		      /*asc*/ 0x04,
868		      /*ascq*/ 0x0b,
869		      SSD_ELEM_NONE);
870}
871
872void
873ctl_set_lun_unavail(struct ctl_scsiio *ctsio)
874{
875	/* "Logical unit not ready, target port in unavailable state" */
876	ctl_set_sense(ctsio,
877		      /*current_error*/ 1,
878		      /*sense_key*/ SSD_KEY_NOT_READY,
879		      /*asc*/ 0x04,
880		      /*ascq*/ 0x0c,
881		      SSD_ELEM_NONE);
882}
883
884void
885ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
886{
887	/* "Medium format corrupted" */
888	ctl_set_sense(ctsio,
889		      /*current_error*/ 1,
890		      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
891		      /*asc*/ 0x31,
892		      /*ascq*/ 0x00,
893		      SSD_ELEM_NONE);
894}
895
896void
897ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
898{
899	/* "Medium magazine not accessible" */
900	ctl_set_sense(ctsio,
901		      /*current_error*/ 1,
902		      /*sense_key*/ SSD_KEY_NOT_READY,
903		      /*asc*/ 0x3b,
904		      /*ascq*/ 0x11,
905		      SSD_ELEM_NONE);
906}
907
908void
909ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
910{
911	/* "Data phase error" */
912	ctl_set_sense(ctsio,
913		      /*current_error*/ 1,
914		      /*sense_key*/ SSD_KEY_NOT_READY,
915		      /*asc*/ 0x4b,
916		      /*ascq*/ 0x00,
917		      SSD_ELEM_NONE);
918}
919
920void
921ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
922{
923
924	ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
925	ctsio->sense_len = 0;
926	ctsio->io_hdr.status = CTL_SCSI_ERROR;
927}
928
929void
930ctl_set_queue_full(struct ctl_scsiio *ctsio)
931{
932
933	ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
934	ctsio->sense_len = 0;
935	ctsio->io_hdr.status = CTL_SCSI_ERROR;
936}
937
938void
939ctl_set_busy(struct ctl_scsiio *ctsio)
940{
941
942	ctsio->scsi_status = SCSI_STATUS_BUSY;
943	ctsio->sense_len = 0;
944	ctsio->io_hdr.status = CTL_SCSI_ERROR;
945}
946
947void
948ctl_set_task_aborted(struct ctl_scsiio *ctsio)
949{
950
951	ctsio->scsi_status = SCSI_STATUS_TASK_ABORTED;
952	ctsio->sense_len = 0;
953	ctsio->io_hdr.status = CTL_CMD_ABORTED;
954}
955
956void
957ctl_set_hw_write_protected(struct ctl_scsiio *ctsio)
958{
959	/* "Hardware write protected" */
960	ctl_set_sense(ctsio,
961		      /*current_error*/ 1,
962		      /*sense_key*/ SSD_KEY_DATA_PROTECT,
963		      /*asc*/ 0x27,
964		      /*ascq*/ 0x01,
965		      SSD_ELEM_NONE);
966}
967
968void
969ctl_set_space_alloc_fail(struct ctl_scsiio *ctsio)
970{
971	/* "Space allocation failed write protect" */
972	ctl_set_sense(ctsio,
973		      /*current_error*/ 1,
974		      /*sense_key*/ SSD_KEY_DATA_PROTECT,
975		      /*asc*/ 0x27,
976		      /*ascq*/ 0x07,
977		      SSD_ELEM_NONE);
978}
979
980void
981ctl_set_success(struct ctl_scsiio *ctsio)
982{
983
984	ctsio->scsi_status = SCSI_STATUS_OK;
985	ctsio->sense_len = 0;
986	ctsio->io_hdr.status = CTL_SUCCESS;
987}
988