Path: blob/main/share/examples/scsi_target/scsi_target.h
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* SCSI Target Emulator4*5* Copyright (c) 2002 Nate Lawson.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 _SCSI_TARGET_H31#define _SCSI_TARGET_H3233/*34* Maximum number of parallel commands to accept,35* 1024 for Fibre Channel (SPI is 16).36*/37#define MAX_INITIATORS 838#define SECTOR_SIZE 51239#define MAX_EVENTS (MAX_INITIATORS + 5)40/* kqueue for AIO, signals */4142/* Additional SCSI 3 defines for inquiry response */43#define SID_Addr16 0x01004445TAILQ_HEAD(io_queue, ccb_hdr);4647/* Offset into the private CCB area for storing our descriptor */48#define targ_descr periph_priv.entries[1].ptr4950/* Descriptor attached to each ATIO */51struct atio_descr {52off_t base_off; /* Base offset for ATIO */53uint total_len; /* Total xfer len for this ATIO */54uint init_req; /* Transfer count requested to/from init */55uint init_ack; /* Data transferred ok to/from init */56uint targ_req; /* Transfer count requested to/from target */57uint targ_ack; /* Data transferred ok to/from target */58int flags; /* Flags for CTIOs */59u_int8_t *cdb; /* Pointer to received CDB */60/* List of completed AIO/CTIOs */61struct io_queue cmplt_io;62};6364typedef enum {65ATIO_WORK,66AIO_DONE,67CTIO_DONE68} io_ops;6970/* Descriptor attached to each CTIO */71struct ctio_descr {72void *buf; /* Backing store */73off_t offset; /* Position in transfer (for file, */74/* doesn't start at 0) */75struct aiocb aiocb; /* AIO descriptor for this CTIO */76struct ccb_accept_tio *atio;77/* ATIO we are satisfying */78io_ops event; /* Event that queued this CTIO */79};8081typedef enum {82UA_NONE = 0x00,83UA_POWER_ON = 0x01,84UA_BUS_RESET = 0x02,85UA_BDR = 0x0486} ua_types;8788typedef enum {89CA_NONE = 0x00,90CA_UNIT_ATTN = 0x01,91CA_CMD_SENSE = 0x0292} ca_types;9394struct initiator_state {95ua_types orig_ua;96ca_types orig_ca;97ua_types pending_ua;98ca_types pending_ca;99struct scsi_sense_data sense_data;100};101102/* Global functions */103extern cam_status tcmd_init(u_int16_t req_inq_flags,104u_int16_t sim_inq_flags);105extern int tcmd_handle(struct ccb_accept_tio *atio,106struct ccb_scsiio *ctio, io_ops event);107extern void tcmd_sense(u_int init_id, struct ccb_scsiio *ctio,108u_int8_t flags,109u_int8_t asc, u_int8_t ascq);110extern void tcmd_ua(u_int init_id, ua_types new_ua);111extern int work_atio(struct ccb_accept_tio *atio);112extern void send_ccb(union ccb *ccb, int priority);113extern void free_ccb(union ccb *ccb);114static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }115116/* Global Data */117extern int notaio;118extern int debug;119extern off_t volume_size;120extern u_int sector_size;121extern size_t buf_size;122123/*124* Compat Defines125*/126#if __FreeBSD_version >= 500000127#define OFF_FMT "%ju"128#else129#define OFF_FMT "%llu"130#endif131132#endif /* _SCSI_TARGET_H */133134135