18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Functions used by both the SCSI initiator code and the SCSI target code. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#ifndef _SCSI_COMMON_H_ 78c2ecf20Sopenharmony_ci#define _SCSI_COMMON_H_ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/types.h> 108c2ecf20Sopenharmony_ci#include <scsi/scsi_proto.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistatic inline unsigned 138c2ecf20Sopenharmony_ciscsi_varlen_cdb_length(const void *hdr) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci return ((struct scsi_varlen_cdb_hdr *)hdr)->additional_cdb_length + 8; 168c2ecf20Sopenharmony_ci} 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ciextern const unsigned char scsi_command_size_tbl[8]; 198c2ecf20Sopenharmony_ci#define COMMAND_SIZE(opcode) scsi_command_size_tbl[((opcode) >> 5) & 7] 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic inline unsigned 228c2ecf20Sopenharmony_ciscsi_command_size(const unsigned char *cmnd) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci return (cmnd[0] == VARIABLE_LENGTH_CMD) ? 258c2ecf20Sopenharmony_ci scsi_varlen_cdb_length(cmnd) : COMMAND_SIZE(cmnd[0]); 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic inline unsigned char 298c2ecf20Sopenharmony_ciscsi_command_control(const unsigned char *cmnd) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci return (cmnd[0] == VARIABLE_LENGTH_CMD) ? 328c2ecf20Sopenharmony_ci cmnd[1] : cmnd[COMMAND_SIZE(cmnd[0]) - 1]; 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* Returns a human-readable name for the device */ 368c2ecf20Sopenharmony_ciextern const char *scsi_device_type(unsigned type); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciextern void int_to_scsilun(u64, struct scsi_lun *); 398c2ecf20Sopenharmony_ciextern u64 scsilun_to_int(struct scsi_lun *); 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* 428c2ecf20Sopenharmony_ci * This is a slightly modified SCSI sense "descriptor" format header. 438c2ecf20Sopenharmony_ci * The addition is to allow the 0x70 and 0x71 response codes. The idea 448c2ecf20Sopenharmony_ci * is to place the salient data from either "fixed" or "descriptor" sense 458c2ecf20Sopenharmony_ci * format into one structure to ease application processing. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * The original sense buffer should be kept around for those cases 488c2ecf20Sopenharmony_ci * in which more information is required (e.g. the LBA of a MEDIUM ERROR). 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_cistruct scsi_sense_hdr { /* See SPC-3 section 4.5 */ 518c2ecf20Sopenharmony_ci u8 response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ 528c2ecf20Sopenharmony_ci u8 sense_key; 538c2ecf20Sopenharmony_ci u8 asc; 548c2ecf20Sopenharmony_ci u8 ascq; 558c2ecf20Sopenharmony_ci u8 byte4; 568c2ecf20Sopenharmony_ci u8 byte5; 578c2ecf20Sopenharmony_ci u8 byte6; 588c2ecf20Sopenharmony_ci u8 additional_length; /* always 0 for fixed sense format */ 598c2ecf20Sopenharmony_ci}; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic inline bool scsi_sense_valid(const struct scsi_sense_hdr *sshdr) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci if (!sshdr) 648c2ecf20Sopenharmony_ci return false; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci return (sshdr->response_code & 0x70) == 0x70; 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciextern bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len, 708c2ecf20Sopenharmony_ci struct scsi_sense_hdr *sshdr); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciextern void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq); 738c2ecf20Sopenharmony_ciint scsi_set_sense_information(u8 *buf, int buf_len, u64 info); 748c2ecf20Sopenharmony_ciint scsi_set_sense_field_pointer(u8 *buf, int buf_len, u16 fp, u8 bp, bool cd); 758c2ecf20Sopenharmony_ciextern const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len, 768c2ecf20Sopenharmony_ci int desc_type); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci#endif /* _SCSI_COMMON_H_ */ 79