/*-1* Data structures and definitions for the CAM system.2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 1997 Justin T. Gibbs.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions, and the following disclaimer,13* without modification, immediately at the beginning of the file.14* 2. The name of the author may not be used to endorse or promote products15* derived from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR21* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#ifndef _CAM_CAM_H31#define _CAM_CAM_H 13233#ifdef _KERNEL34#include "opt_cam.h"35/* Only need the hooks here so no opt_kdtrace.h */36#include <sys/queue.h>37#include <sys/sdt.h>38#endif3940#ifndef _KERNEL41#include <stdbool.h>42#include <stdio.h>43#endif4445typedef u_int path_id_t;46typedef u_int target_id_t;47typedef uint64_t lun_id_t;4849#define CAM_XPT_PATH_ID ((path_id_t)~0)50#define CAM_BUS_WILDCARD ((path_id_t)~0)51#define CAM_TARGET_WILDCARD ((target_id_t)~0)52#define CAM_LUN_WILDCARD (~(u_int)0)5354#define CAM_EXTLUN_BYTE_SWIZZLE(lun) ( \55((((uint64_t)lun) & 0xffff000000000000L) >> 48) | \56((((uint64_t)lun) & 0x0000ffff00000000L) >> 16) | \57((((uint64_t)lun) & 0x00000000ffff0000L) << 16) | \58((((uint64_t)lun) & 0x000000000000ffffL) << 48))5960/*61* Maximum length for a CAM CDB.62*/63#define CAM_MAX_CDBLEN 166465/*66* Definition of a CAM peripheral driver entry. Peripheral drivers instantiate67* one of these for each device they wish to communicate with and pass it into68* the xpt layer when they wish to schedule work on that device via the69* xpt_schedule API.70*/71struct cam_periph;7273/*74* Priority information for a CAM structure.75*/76typedef enum {77CAM_RL_HOST,78CAM_RL_BUS,79CAM_RL_XPT,80CAM_RL_DEV,81CAM_RL_NORMAL,82CAM_RL_VALUES83} cam_rl;84/*85* The generation number is incremented every time a new entry is entered into86* the queue giving round robin per priority level scheduling.87*/88typedef struct {89uint32_t priority;90#define CAM_PRIORITY_HOST ((CAM_RL_HOST << 8) + 0x80)91#define CAM_PRIORITY_BUS ((CAM_RL_BUS << 8) + 0x80)92#define CAM_PRIORITY_XPT ((CAM_RL_XPT << 8) + 0x80)93#define CAM_PRIORITY_DEV ((CAM_RL_DEV << 8) + 0x80)94#define CAM_PRIORITY_OOB (CAM_RL_DEV << 8)95#define CAM_PRIORITY_NORMAL ((CAM_RL_NORMAL << 8) + 0x80)96#define CAM_PRIORITY_NONE (uint32_t)-197uint32_t generation;98int index;99#define CAM_UNQUEUED_INDEX -1100#define CAM_ACTIVE_INDEX -2101#define CAM_DONEQ_INDEX -3102#define CAM_ASYNC_INDEX -4103#define CAM_EXTRAQ_INDEX INT_MAX104} cam_pinfo;105106/*107* Macro to compare two generation numbers. It is used like this:108*109* if (GENERATIONCMP(a, >=, b))110* ...;111*112* GERERATIONCMP uses modular arithmetic to guard against wraps113* wraps in the generation number.114*/115#define GENERATIONCMP(x, op, y) ((int32_t)((x) - (y)) op 0)116117/* CAM flags XXX Move to cam_periph.h ??? */118typedef enum {119CAM_FLAG_NONE = 0x00,120CAM_EXPECT_INQ_CHANGE = 0x01,121CAM_RETRY_SELTO = 0x02 /* Retry Selection Timeouts */122} cam_flags;123124enum {125SF_RETRY_UA = 0x01, /* Retry UNIT ATTENTION conditions. */126SF_NO_PRINT = 0x02, /* Never print error status. */127SF_QUIET_IR = 0x04, /* Be quiet about Illegal Request responses */128SF_PRINT_ALWAYS = 0x08, /* Always print error status. */129SF_NO_RECOVERY = 0x10, /* Don't do active error recovery. */130SF_NO_RETRY = 0x20, /* Don't do any retries. */131SF_RETRY_BUSY = 0x40 /* Retry BUSY status. */132};133134/* CAM Status field values */135typedef enum {136/* CCB request is in progress */137CAM_REQ_INPROG = 0x00,138139/* CCB request completed without error */140CAM_REQ_CMP = 0x01,141142/* CCB request aborted by the host */143CAM_REQ_ABORTED = 0x02,144145/* Unable to abort CCB request */146CAM_UA_ABORT = 0x03,147148/* CCB request completed with an error */149CAM_REQ_CMP_ERR = 0x04,150151/* CAM subsystem is busy */152CAM_BUSY = 0x05,153154/* CCB request was invalid */155CAM_REQ_INVALID = 0x06,156157/* Supplied Path ID is invalid */158CAM_PATH_INVALID = 0x07,159160/* SCSI Device Not Installed/there */161CAM_DEV_NOT_THERE = 0x08,162163/* Unable to terminate I/O CCB request */164CAM_UA_TERMIO = 0x09,165166/* Target Selection Timeout */167CAM_SEL_TIMEOUT = 0x0a,168169/* Command timeout */170CAM_CMD_TIMEOUT = 0x0b,171172/* SCSI error, look at error code in CCB */173CAM_SCSI_STATUS_ERROR = 0x0c,174175/* Message Reject Received */176CAM_MSG_REJECT_REC = 0x0d,177178/* SCSI Bus Reset Sent/Received */179CAM_SCSI_BUS_RESET = 0x0e,180181/* Uncorrectable parity error occurred */182CAM_UNCOR_PARITY = 0x0f,183184/* Autosense: request sense cmd fail */185CAM_AUTOSENSE_FAIL = 0x10,186187/* No HBA Detected error */188CAM_NO_HBA = 0x11,189190/* Data Overrun error */191CAM_DATA_RUN_ERR = 0x12,192193/* Unexpected Bus Free */194CAM_UNEXP_BUSFREE = 0x13,195196/* Target Bus Phase Sequence Failure */197CAM_SEQUENCE_FAIL = 0x14,198199/* CCB length supplied is inadequate */200CAM_CCB_LEN_ERR = 0x15,201202/* Unable to provide requested capability*/203CAM_PROVIDE_FAIL = 0x16,204205/* A SCSI BDR msg was sent to target */206CAM_BDR_SENT = 0x17,207208/* CCB request terminated by the host */209CAM_REQ_TERMIO = 0x18,210211/* Unrecoverable Host Bus Adapter Error */212CAM_UNREC_HBA_ERROR = 0x19,213214/* Request was too large for this host */215CAM_REQ_TOO_BIG = 0x1a,216217/*218* This request should be requeued to preserve219* transaction ordering. This typically occurs220* when the SIM recognizes an error that should221* freeze the queue and must place additional222* requests for the target at the sim level223* back into the XPT queue.224*/225CAM_REQUEUE_REQ = 0x1b,226227/* ATA error, look at error code in CCB */228CAM_ATA_STATUS_ERROR = 0x1c,229230/* Initiator/Target Nexus lost. */231CAM_SCSI_IT_NEXUS_LOST = 0x1d,232233/* SMP error, look at error code in CCB */234CAM_SMP_STATUS_ERROR = 0x1e,235236/*237* Command completed without error but exceeded the soft238* timeout threshold.239*/240CAM_REQ_SOFTTIMEOUT = 0x1f,241242/*243* NVME error, look at errro code in CCB244*/245CAM_NVME_STATUS_ERROR = 0x20,246247/*248* 0x21 - 0x32 are unassigned249*/250251/* Initiator Detected Error */252CAM_IDE = 0x33,253254/* Resource Unavailable */255CAM_RESRC_UNAVAIL = 0x34,256257/* Unacknowledged Event by Host */258CAM_UNACKED_EVENT = 0x35,259260/* Message Received in Host Target Mode */261CAM_MESSAGE_RECV = 0x36,262263/* Invalid CDB received in Host Target Mode */264CAM_INVALID_CDB = 0x37,265266/* Lun supplied is invalid */267CAM_LUN_INVALID = 0x38,268269/* Target ID supplied is invalid */270CAM_TID_INVALID = 0x39,271272/* The requested function is not available */273CAM_FUNC_NOTAVAIL = 0x3a,274275/* Nexus is not established */276CAM_NO_NEXUS = 0x3b,277278/* The initiator ID is invalid */279CAM_IID_INVALID = 0x3c,280281/* The SCSI CDB has been received */282CAM_CDB_RECVD = 0x3d,283284/* The LUN is already enabled for target mode */285CAM_LUN_ALRDY_ENA = 0x3e,286287/* SCSI Bus Busy */288CAM_SCSI_BUSY = 0x3f,289290/*291* Flags292*/293294/* The DEV queue is frozen w/this err */295CAM_DEV_QFRZN = 0x40,296297/* Autosense data valid for target */298CAM_AUTOSNS_VALID = 0x80,299300/* SIM ready to take more commands */301CAM_RELEASE_SIMQ = 0x100,302303/* SIM has this command in its queue */304CAM_SIM_QUEUED = 0x200,305306/* Quality of service data is valid */307CAM_QOS_VALID = 0x400,308309/* Mask bits for just the status # */310CAM_STATUS_MASK = 0x3F,311312/*313* Target Specific Adjunct Status314*/315316/* sent sense with status */317CAM_SENT_SENSE = 0x40000000318} cam_status;319320typedef enum {321CAM_ESF_NONE = 0x00,322CAM_ESF_COMMAND = 0x01,323CAM_ESF_CAM_STATUS = 0x02,324CAM_ESF_PROTO_STATUS = 0x04,325CAM_ESF_ALL = 0xff326} cam_error_string_flags;327328typedef enum {329CAM_EPF_NONE = 0x00,330CAM_EPF_MINIMAL = 0x01,331CAM_EPF_NORMAL = 0x02,332CAM_EPF_ALL = 0x03,333CAM_EPF_LEVEL_MASK = 0x0f334/* All bits above bit 3 are protocol-specific */335} cam_error_proto_flags;336337typedef enum {338CAM_ESF_PRINT_NONE = 0x00,339CAM_ESF_PRINT_STATUS = 0x10,340CAM_ESF_PRINT_SENSE = 0x20341} cam_error_scsi_flags;342343typedef enum {344CAM_ESMF_PRINT_NONE = 0x00,345CAM_ESMF_PRINT_STATUS = 0x10,346CAM_ESMF_PRINT_FULL_CMD = 0x20,347} cam_error_smp_flags;348349typedef enum {350CAM_EAF_PRINT_NONE = 0x00,351CAM_EAF_PRINT_STATUS = 0x10,352CAM_EAF_PRINT_RESULT = 0x20353} cam_error_ata_flags;354355typedef enum {356CAM_ENF_PRINT_NONE = 0x00,357CAM_ENF_PRINT_STATUS = 0x10,358} cam_error_nvme_flags;359360typedef enum {361CAM_STRVIS_FLAG_NONE = 0x00,362CAM_STRVIS_FLAG_NONASCII_MASK = 0x03,363CAM_STRVIS_FLAG_NONASCII_TRIM = 0x00,364CAM_STRVIS_FLAG_NONASCII_RAW = 0x01,365CAM_STRVIS_FLAG_NONASCII_SPC = 0x02,366CAM_STRVIS_FLAG_NONASCII_ESC = 0x03367} cam_strvis_flags;368369struct cam_status_entry370{371cam_status status_code;372const char *status_text;373};374375extern const struct cam_status_entry cam_status_table[];376extern const int num_cam_status_entries;377#ifdef _KERNEL378extern int cam_sort_io_queues;379#ifdef SDT_PROVIDER_DECLARE380SDT_PROVIDER_DECLARE(cam);381#endif382#define CAM_PROBE1(group, probe, arg0) \383SDT_PROBE1(cam, , group, probe, arg0)384#define CAM_PROBE2(group, probe, arg0, arg1) \385SDT_PROBE2(cam, , group, probe, arg0, arg1)386#define CAM_PROBE3(group, probe, arg0, arg1, arg2) \387SDT_PROBE3(cam, , group, probe, arg0, arg1, arg2)388#define CAM_PROBE4(group, probe, arg0, arg1, arg2, arg3) \389SDT_PROBE4(cam, , group, probe, arg0, arg1, arg2, arg3)390#endif391union ccb;392struct sbuf;393394#ifdef SYSCTL_DECL /* from sysctl.h */395SYSCTL_DECL(_kern_cam);396#endif397398__BEGIN_DECLS399typedef int (cam_quirkmatch_t)(caddr_t, caddr_t);400401caddr_t cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,402int entry_size, cam_quirkmatch_t *comp_func);403404void cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen);405void cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen,406int dstlen, uint32_t flags);407void cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,408uint32_t flags);409410int cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len);411const struct cam_status_entry*412cam_fetch_status_entry(cam_status status);413#ifdef _KERNEL414char * cam_error_string(union ccb *ccb, char *str, int str_len,415cam_error_string_flags flags,416cam_error_proto_flags proto_flags);417void cam_error_print(union ccb *ccb, cam_error_string_flags flags,418cam_error_proto_flags proto_flags);419#else /* _KERNEL */420struct cam_device;421422char * cam_error_string(struct cam_device *device, union ccb *ccb, char *str,423int str_len, cam_error_string_flags flags,424cam_error_proto_flags proto_flags);425void cam_error_print(struct cam_device *device, union ccb *ccb,426cam_error_string_flags flags,427cam_error_proto_flags proto_flags, FILE *ofile);428#endif /* _KERNEL */429__END_DECLS430431#ifdef _KERNEL432static __inline void cam_init_pinfo(cam_pinfo *pinfo)433{434pinfo->priority = CAM_PRIORITY_NONE;435pinfo->index = CAM_UNQUEUED_INDEX;436}437#endif438439#endif /* _CAM_CAM_H */440441442