/*-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#endif3637#ifndef _KERNEL38#include <stdbool.h>39#include <stdio.h>40#endif4142typedef u_int path_id_t;43typedef u_int target_id_t;44typedef uint64_t lun_id_t;4546#define CAM_XPT_PATH_ID ((path_id_t)~0)47#define CAM_BUS_WILDCARD ((path_id_t)~0)48#define CAM_TARGET_WILDCARD ((target_id_t)~0)49#define CAM_LUN_WILDCARD (~(u_int)0)5051#define CAM_EXTLUN_BYTE_SWIZZLE(lun) ( \52((((uint64_t)lun) & 0xffff000000000000L) >> 48) | \53((((uint64_t)lun) & 0x0000ffff00000000L) >> 16) | \54((((uint64_t)lun) & 0x00000000ffff0000L) << 16) | \55((((uint64_t)lun) & 0x000000000000ffffL) << 48))5657/*58* Maximum length for a CAM CDB.59*/60#define CAM_MAX_CDBLEN 166162/*63* Definition of a CAM peripheral driver entry. Peripheral drivers instantiate64* one of these for each device they wish to communicate with and pass it into65* the xpt layer when they wish to schedule work on that device via the66* xpt_schedule API.67*/68struct cam_periph;6970/*71* Priority information for a CAM structure.72*/73typedef enum {74CAM_RL_HOST,75CAM_RL_BUS,76CAM_RL_XPT,77CAM_RL_DEV,78CAM_RL_NORMAL,79CAM_RL_VALUES80} cam_rl;81/*82* The generation number is incremented every time a new entry is entered into83* the queue giving round robin per priority level scheduling.84*/85typedef struct {86uint32_t priority;87#define CAM_PRIORITY_HOST ((CAM_RL_HOST << 8) + 0x80)88#define CAM_PRIORITY_BUS ((CAM_RL_BUS << 8) + 0x80)89#define CAM_PRIORITY_XPT ((CAM_RL_XPT << 8) + 0x80)90#define CAM_PRIORITY_DEV ((CAM_RL_DEV << 8) + 0x80)91#define CAM_PRIORITY_OOB (CAM_RL_DEV << 8)92#define CAM_PRIORITY_NORMAL ((CAM_RL_NORMAL << 8) + 0x80)93#define CAM_PRIORITY_NONE (uint32_t)-194uint32_t generation;95int index;96#define CAM_UNQUEUED_INDEX -197#define CAM_ACTIVE_INDEX -298#define CAM_DONEQ_INDEX -399#define CAM_ASYNC_INDEX -4100#define CAM_EXTRAQ_INDEX INT_MAX101} cam_pinfo;102103/*104* Macro to compare two generation numbers. It is used like this:105*106* if (GENERATIONCMP(a, >=, b))107* ...;108*109* GERERATIONCMP uses modular arithmetic to guard against wraps110* wraps in the generation number.111*/112#define GENERATIONCMP(x, op, y) ((int32_t)((x) - (y)) op 0)113114/* CAM flags XXX Move to cam_periph.h ??? */115typedef enum {116CAM_FLAG_NONE = 0x00,117CAM_EXPECT_INQ_CHANGE = 0x01,118CAM_RETRY_SELTO = 0x02 /* Retry Selection Timeouts */119} cam_flags;120121enum {122SF_RETRY_UA = 0x01, /* Retry UNIT ATTENTION conditions. */123SF_NO_PRINT = 0x02, /* Never print error status. */124SF_QUIET_IR = 0x04, /* Be quiet about Illegal Request responses */125SF_PRINT_ALWAYS = 0x08, /* Always print error status. */126SF_NO_RECOVERY = 0x10, /* Don't do active error recovery. */127SF_NO_RETRY = 0x20, /* Don't do any retries. */128SF_RETRY_BUSY = 0x40 /* Retry BUSY status. */129};130131/* CAM Status field values */132typedef enum {133/* CCB request is in progress */134CAM_REQ_INPROG = 0x00,135136/* CCB request completed without error */137CAM_REQ_CMP = 0x01,138139/* CCB request aborted by the host */140CAM_REQ_ABORTED = 0x02,141142/* Unable to abort CCB request */143CAM_UA_ABORT = 0x03,144145/* CCB request completed with an error */146CAM_REQ_CMP_ERR = 0x04,147148/* CAM subsystem is busy */149CAM_BUSY = 0x05,150151/* CCB request was invalid */152CAM_REQ_INVALID = 0x06,153154/* Supplied Path ID is invalid */155CAM_PATH_INVALID = 0x07,156157/* SCSI Device Not Installed/there */158CAM_DEV_NOT_THERE = 0x08,159160/* Unable to terminate I/O CCB request */161CAM_UA_TERMIO = 0x09,162163/* Target Selection Timeout */164CAM_SEL_TIMEOUT = 0x0a,165166/* Command timeout */167CAM_CMD_TIMEOUT = 0x0b,168169/* SCSI error, look at error code in CCB */170CAM_SCSI_STATUS_ERROR = 0x0c,171172/* Message Reject Received */173CAM_MSG_REJECT_REC = 0x0d,174175/* SCSI Bus Reset Sent/Received */176CAM_SCSI_BUS_RESET = 0x0e,177178/* Uncorrectable parity error occurred */179CAM_UNCOR_PARITY = 0x0f,180181/* Autosense: request sense cmd fail */182CAM_AUTOSENSE_FAIL = 0x10,183184/* No HBA Detected error */185CAM_NO_HBA = 0x11,186187/* Data Overrun error */188CAM_DATA_RUN_ERR = 0x12,189190/* Unexpected Bus Free */191CAM_UNEXP_BUSFREE = 0x13,192193/* Target Bus Phase Sequence Failure */194CAM_SEQUENCE_FAIL = 0x14,195196/* CCB length supplied is inadequate */197CAM_CCB_LEN_ERR = 0x15,198199/* Unable to provide requested capability*/200CAM_PROVIDE_FAIL = 0x16,201202/* A SCSI BDR msg was sent to target */203CAM_BDR_SENT = 0x17,204205/* CCB request terminated by the host */206CAM_REQ_TERMIO = 0x18,207208/* Unrecoverable Host Bus Adapter Error */209CAM_UNREC_HBA_ERROR = 0x19,210211/* Request was too large for this host */212CAM_REQ_TOO_BIG = 0x1a,213214/*215* This request should be requeued to preserve216* transaction ordering. This typically occurs217* when the SIM recognizes an error that should218* freeze the queue and must place additional219* requests for the target at the sim level220* back into the XPT queue.221*/222CAM_REQUEUE_REQ = 0x1b,223224/* ATA error, look at error code in CCB */225CAM_ATA_STATUS_ERROR = 0x1c,226227/* Initiator/Target Nexus lost. */228CAM_SCSI_IT_NEXUS_LOST = 0x1d,229230/* SMP error, look at error code in CCB */231CAM_SMP_STATUS_ERROR = 0x1e,232233/*234* Command completed without error but exceeded the soft235* timeout threshold.236*/237CAM_REQ_SOFTTIMEOUT = 0x1f,238239/*240* NVME error, look at errro code in CCB241*/242CAM_NVME_STATUS_ERROR = 0x20,243244/*245* 0x21 - 0x32 are unassigned246*/247248/* Initiator Detected Error */249CAM_IDE = 0x33,250251/* Resource Unavailable */252CAM_RESRC_UNAVAIL = 0x34,253254/* Unacknowledged Event by Host */255CAM_UNACKED_EVENT = 0x35,256257/* Message Received in Host Target Mode */258CAM_MESSAGE_RECV = 0x36,259260/* Invalid CDB received in Host Target Mode */261CAM_INVALID_CDB = 0x37,262263/* Lun supplied is invalid */264CAM_LUN_INVALID = 0x38,265266/* Target ID supplied is invalid */267CAM_TID_INVALID = 0x39,268269/* The requested function is not available */270CAM_FUNC_NOTAVAIL = 0x3a,271272/* Nexus is not established */273CAM_NO_NEXUS = 0x3b,274275/* The initiator ID is invalid */276CAM_IID_INVALID = 0x3c,277278/* The SCSI CDB has been received */279CAM_CDB_RECVD = 0x3d,280281/* The LUN is already enabled for target mode */282CAM_LUN_ALRDY_ENA = 0x3e,283284/* SCSI Bus Busy */285CAM_SCSI_BUSY = 0x3f,286287/*288* Flags289*/290291/* The DEV queue is frozen w/this err */292CAM_DEV_QFRZN = 0x40,293294/* Autosense data valid for target */295CAM_AUTOSNS_VALID = 0x80,296297/* SIM ready to take more commands */298CAM_RELEASE_SIMQ = 0x100,299300/* SIM has this command in its queue */301CAM_SIM_QUEUED = 0x200,302303/* Quality of service data is valid */304CAM_QOS_VALID = 0x400,305306/* Mask bits for just the status # */307CAM_STATUS_MASK = 0x3F,308309/*310* Target Specific Adjunct Status311*/312313/* sent sense with status */314CAM_SENT_SENSE = 0x40000000315} cam_status;316317typedef enum {318CAM_ESF_NONE = 0x00,319CAM_ESF_COMMAND = 0x01,320CAM_ESF_CAM_STATUS = 0x02,321CAM_ESF_PROTO_STATUS = 0x04,322CAM_ESF_ALL = 0xff323} cam_error_string_flags;324325typedef enum {326CAM_EPF_NONE = 0x00,327CAM_EPF_MINIMAL = 0x01,328CAM_EPF_NORMAL = 0x02,329CAM_EPF_ALL = 0x03,330CAM_EPF_LEVEL_MASK = 0x0f331/* All bits above bit 3 are protocol-specific */332} cam_error_proto_flags;333334typedef enum {335CAM_ESF_PRINT_NONE = 0x00,336CAM_ESF_PRINT_STATUS = 0x10,337CAM_ESF_PRINT_SENSE = 0x20338} cam_error_scsi_flags;339340typedef enum {341CAM_ESMF_PRINT_NONE = 0x00,342CAM_ESMF_PRINT_STATUS = 0x10,343CAM_ESMF_PRINT_FULL_CMD = 0x20,344} cam_error_smp_flags;345346typedef enum {347CAM_EAF_PRINT_NONE = 0x00,348CAM_EAF_PRINT_STATUS = 0x10,349CAM_EAF_PRINT_RESULT = 0x20350} cam_error_ata_flags;351352typedef enum {353CAM_ENF_PRINT_NONE = 0x00,354CAM_ENF_PRINT_STATUS = 0x10,355} cam_error_nvme_flags;356357typedef enum {358CAM_STRVIS_FLAG_NONE = 0x00,359CAM_STRVIS_FLAG_NONASCII_MASK = 0x03,360CAM_STRVIS_FLAG_NONASCII_TRIM = 0x00,361CAM_STRVIS_FLAG_NONASCII_RAW = 0x01,362CAM_STRVIS_FLAG_NONASCII_SPC = 0x02,363CAM_STRVIS_FLAG_NONASCII_ESC = 0x03364} cam_strvis_flags;365366struct cam_status_entry367{368cam_status status_code;369const char *status_text;370};371372extern const struct cam_status_entry cam_status_table[];373extern const int num_cam_status_entries;374#ifdef _KERNEL375extern int cam_sort_io_queues;376#endif377union ccb;378struct sbuf;379380#ifdef SYSCTL_DECL /* from sysctl.h */381SYSCTL_DECL(_kern_cam);382#endif383384__BEGIN_DECLS385typedef int (cam_quirkmatch_t)(caddr_t, caddr_t);386387caddr_t cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,388int entry_size, cam_quirkmatch_t *comp_func);389390void cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen);391void cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen,392int dstlen, uint32_t flags);393void cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,394uint32_t flags);395396int cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len);397const struct cam_status_entry*398cam_fetch_status_entry(cam_status status);399#ifdef _KERNEL400char * cam_error_string(union ccb *ccb, char *str, int str_len,401cam_error_string_flags flags,402cam_error_proto_flags proto_flags);403void cam_error_print(union ccb *ccb, cam_error_string_flags flags,404cam_error_proto_flags proto_flags);405#else /* _KERNEL */406struct cam_device;407408char * cam_error_string(struct cam_device *device, union ccb *ccb, char *str,409int str_len, cam_error_string_flags flags,410cam_error_proto_flags proto_flags);411void cam_error_print(struct cam_device *device, union ccb *ccb,412cam_error_string_flags flags,413cam_error_proto_flags proto_flags, FILE *ofile);414#endif /* _KERNEL */415__END_DECLS416417#ifdef _KERNEL418static __inline void cam_init_pinfo(cam_pinfo *pinfo)419{420pinfo->priority = CAM_PRIORITY_NONE;421pinfo->index = CAM_UNQUEUED_INDEX;422}423#endif424425#endif /* _CAM_CAM_H */426427428