Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * SCSI functions used by both the initiator and the target code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <scsi/scsi_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * You may not alter any existing entry (although adding new ones is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * encouraged once assigned by ANSI/INCITS T10).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static const char *const scsi_device_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	"Direct-Access    ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	"Sequential-Access",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	"Printer          ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	"Processor        ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	"WORM             ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	"CD-ROM           ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	"Scanner          ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	"Optical Device   ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	"Medium Changer   ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	"Communications   ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	"ASC IT8          ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	"ASC IT8          ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	"RAID             ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	"Enclosure        ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	"Direct-Access-RBC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	"Optical card     ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	"Bridge controller",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	"Object storage   ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	"Automation/Drive ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	"Security Manager ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	"Direct-Access-ZBC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * scsi_device_type - Return 17-char string indicating device type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * @type: type number to look up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) const char *scsi_device_type(unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (type == 0x1e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		return "Well-known LUN   ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (type == 0x1f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return "No Device        ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (type >= ARRAY_SIZE(scsi_device_types))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return "Unknown          ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return scsi_device_types[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) EXPORT_SYMBOL(scsi_device_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * scsilun_to_int - convert a scsi_lun to an int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @scsilun:	struct scsi_lun to be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *     Convert @scsilun from a struct scsi_lun to a four-byte host byte-ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  *     integer, and return the result. The caller must check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *     truncation before using this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *     For a description of the LUN format, post SCSI-3 see the SCSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *     Architecture Model, for SCSI-3 see the SCSI Controller Commands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *     Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *     returns the integer: 0x0b03d204
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *     This encoding will return a standard integer LUN for LUNs smaller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *     than 256, which typically use a single level LUN structure with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *     addressing method 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) u64 scsilun_to_int(struct scsi_lun *scsilun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u64 lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	lun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	for (i = 0; i < sizeof(lun); i += 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			     ((u64)scsilun->scsi_lun[i + 1] << (i * 8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) EXPORT_SYMBOL(scsilun_to_int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * int_to_scsilun - reverts an int into a scsi_lun
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * @lun:        integer to be reverted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * @scsilun:	struct scsi_lun to be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  *     Reverts the functionality of the scsilun_to_int, which packed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  *     an 8-byte lun value into an int. This routine unpacks the int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *     back into the lun value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *     Given an integer : 0x0b03d204, this function returns a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *     struct scsi_lun of: d2 04 0b 03 00 00 00 00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	for (i = 0; i < sizeof(lun); i += 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		scsilun->scsi_lun[i+1] = lun & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		lun = lun >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL(int_to_scsilun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * scsi_normalize_sense - normalize main elements from either fixed or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  *			descriptor sense data format into a common format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * @sense_buffer:	byte array containing sense data returned by device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @sb_len:		number of valid bytes in sense_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @sshdr:		pointer to instance of structure that common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  *			elements are written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  *	The "main elements" from sense data are: response_code, sense_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  *	asc, ascq and additional_length (only for descriptor format).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  *	Typically this function can be called after a device has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  *	responded to a SCSI command with the CHECK_CONDITION status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  *	true if valid sense data information found, else false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			  struct scsi_sense_hdr *sshdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!sense_buffer || !sb_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	sshdr->response_code = (sense_buffer[0] & 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!scsi_sense_valid(sshdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (sshdr->response_code >= 0x72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		 * descriptor format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (sb_len > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			sshdr->sense_key = (sense_buffer[1] & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (sb_len > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			sshdr->asc = sense_buffer[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (sb_len > 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			sshdr->ascq = sense_buffer[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (sb_len > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			sshdr->additional_length = sense_buffer[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		 * fixed format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (sb_len > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			sshdr->sense_key = (sense_buffer[2] & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (sb_len > 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			sb_len = (sb_len < (sense_buffer[7] + 8)) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					 sb_len : (sense_buffer[7] + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			if (sb_len > 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				sshdr->asc = sense_buffer[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			if (sb_len > 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				sshdr->ascq = sense_buffer[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) EXPORT_SYMBOL(scsi_normalize_sense);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * scsi_sense_desc_find - search for a given descriptor type in	descriptor sense data format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * @sense_buffer:	byte array of descriptor format sense data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * @sb_len:		number of valid bytes in sense_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * @desc_type:		value of descriptor type to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  *			(e.g. 0 -> information)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *	only valid when sense data is in descriptor format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  *	pointer to start of (first) descriptor if found else NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				int desc_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	int add_sen_len, add_len, desc_len, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	const u8 * descp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	add_sen_len = (add_sen_len < (sb_len - 8)) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			add_sen_len : (sb_len - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	descp = &sense_buffer[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		descp += desc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		desc_len = add_len + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (descp[0] == desc_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			return descp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (add_len < 0) // short descriptor ??
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) EXPORT_SYMBOL(scsi_sense_desc_find);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * scsi_build_sense_buffer - build sense data in a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * @desc:	Sense format (non-zero == descriptor format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  *              0 == fixed format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * @buf:	Where to build sense data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * @key:	Sense key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @asc:	Additional sense code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * @ascq:	Additional sense code qualifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		buf[0] = 0x72;	/* descriptor, current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		buf[1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		buf[2] = asc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		buf[3] = ascq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		buf[7] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		buf[0] = 0x70;	/* fixed, current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		buf[2] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		buf[7] = 0xa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		buf[12] = asc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		buf[13] = ascq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) EXPORT_SYMBOL(scsi_build_sense_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * scsi_set_sense_information - set the information field in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  *		formatted sense data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @buf:	Where to build sense data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * @buf_len:    buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * @info:	64-bit information value to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  *	0 on success or -EINVAL for invalid sense buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int scsi_set_sense_information(u8 *buf, int buf_len, u64 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if ((buf[0] & 0x7f) == 0x72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		u8 *ucp, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		len = buf[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ucp = (char *)scsi_sense_desc_find(buf, len + 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (!ucp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			buf[7] = len + 0xc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			ucp = buf + 8 + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		if (buf_len < len + 0xc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			/* Not enough room for info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ucp[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		ucp[1] = 0xa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		ucp[2] = 0x80; /* Valid bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		ucp[3] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		put_unaligned_be64(info, &ucp[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	} else if ((buf[0] & 0x7f) == 0x70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		 * Only set the 'VALID' bit if we can represent the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		 * correctly; otherwise just fill out the lower bytes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		 * clear the 'VALID' flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		if (info <= 0xffffffffUL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			buf[0] |= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			buf[0] &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		put_unaligned_be32((u32)info, &buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) EXPORT_SYMBOL(scsi_set_sense_information);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * scsi_set_sense_field_pointer - set the field pointer sense key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  *		specific information in a formatted sense data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * @buf:	Where to build sense data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * @buf_len:    buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * @fp:		field pointer to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * @bp:		bit pointer to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * @cd:		command/data bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *	0 on success or -EINVAL for invalid sense buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int scsi_set_sense_field_pointer(u8 *buf, int buf_len, u16 fp, u8 bp, bool cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	u8 *ucp, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if ((buf[0] & 0x7f) == 0x72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		len = buf[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		ucp = (char *)scsi_sense_desc_find(buf, len + 8, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (!ucp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			buf[7] = len + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			ucp = buf + 8 + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		if (buf_len < len + 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			/* Not enough room for info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		ucp[0] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		ucp[1] = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		ucp[4] = 0x80; /* Valid bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			ucp[4] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if (bp < 0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			ucp[4] |= 0x8 | bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		put_unaligned_be16(fp, &ucp[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	} else if ((buf[0] & 0x7f) == 0x70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		len = buf[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (len < 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			buf[7] = 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		buf[15] = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			buf[15] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		if (bp < 0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			buf[15] |= 0x8 | bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		put_unaligned_be16(fp, &buf[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) EXPORT_SYMBOL(scsi_set_sense_field_pointer);