1 /*- 2 * Copyright (c) 2018 VMware, Inc. 3 * 4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0) 5 * 6 * $NetBSD: pvscsi.h,v 1.1 2025/08/04 20:03:12 skrll Exp $ 7 */ 8 9 /* 10 11 These files are provided under a dual BSD-2 Clause/GPLv2 license. When 12 using or redistributing this file, you may do so under either license. 13 14 BSD-2 Clause License 15 16 Copyright (c) 2018 VMware, Inc. 17 18 Redistribution and use in source and binary forms, with or without 19 modification, are permitted provided that the following conditions 20 are met: 21 22 * Redistributions of source code must retain the above copyright 23 notice, this list of conditions and the following disclaimer. 24 25 * Redistributions in binary form must reproduce the above copyright 26 notice, this list of conditions and the following disclaimer in 27 the documentation and/or other materials provided with the 28 distribution. 29 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 42 GPL License Summary 43 44 Copyright (c) 2018 VMware, Inc. 45 46 This program is free software; you can redistribute it and/or modify 47 it under the terms of version 2 of the GNU General Public License as 48 published by the Free Software Foundation. 49 50 This program is distributed in the hope that it will be useful, but 51 WITHOUT ANY WARRANTY; without even the implied warranty of 52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 53 General Public License for more details. 54 55 You should have received a copy of the GNU General Public License 56 along with this program; if not, write to the Free Software 57 Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 58 The full GNU General Public License is included in this distribution 59 in the file called LICENSE.GPL. 60 61 */ 62 63 #ifndef _PVSCSI_H_ 64 #define _PVSCSI_H_ 65 66 #define MASK(v) ((1 << (v)) - 1) 67 68 enum pvscsi_reg_offset { 69 PVSCSI_REG_OFFSET_COMMAND = 0x0000, 70 PVSCSI_REG_OFFSET_COMMAND_DATA = 0x0004, 71 PVSCSI_REG_OFFSET_COMMAND_STATUS = 0x0008, 72 PVSCSI_REG_OFFSET_LAST_STS_0 = 0x0100, 73 PVSCSI_REG_OFFSET_LAST_STS_1 = 0x0104, 74 PVSCSI_REG_OFFSET_LAST_STS_2 = 0x0108, 75 PVSCSI_REG_OFFSET_LAST_STS_3 = 0x010c, 76 PVSCSI_REG_OFFSET_INTR_STATUS = 0x100c, 77 PVSCSI_REG_OFFSET_INTR_MASK = 0x2010, 78 PVSCSI_REG_OFFSET_KICK_NON_RW_IO = 0x3014, 79 PVSCSI_REG_OFFSET_DEBUG = 0x3018, 80 PVSCSI_REG_OFFSET_KICK_RW_IO = 0x4018, 81 }; 82 83 enum pvscsi_commands { 84 PVSCSI_CMD_FIRST = 0, 85 86 PVSCSI_CMD_ADAPTER_RESET = 1, 87 PVSCSI_CMD_ISSUE_SCSI = 2, 88 PVSCSI_CMD_SETUP_RINGS = 3, 89 PVSCSI_CMD_RESET_BUS = 4, 90 PVSCSI_CMD_RESET_DEVICE = 5, 91 PVSCSI_CMD_ABORT_CMD = 6, 92 PVSCSI_CMD_CONFIG = 7, 93 PVSCSI_CMD_SETUP_MSG_RING = 8, 94 PVSCSI_CMD_DEVICE_UNPLUG = 9, 95 PVSCSI_CMD_SETUP_REQCALLTHRESHOLD = 10, 96 PVSCSI_CMD_GET_MAX_TARGETS = 11, 97 98 PVSCSI_CMD_LAST = 12, 99 }; 100 101 struct pvscsi_cmd_desc_reset_device { 102 uint32_t target; 103 uint8_t lun[8]; 104 }; 105 106 struct pvscsi_cmd_desc_abort_cmd { 107 uint64_t context; 108 uint32_t target; 109 uint32_t pad; 110 }; 111 112 #define PVSCSI_SETUP_RINGS_MAX_NUM_PAGES 32 113 #define PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES 16 114 115 struct pvscsi_cmd_desc_setup_rings { 116 uint32_t req_ring_num_pages; 117 uint32_t cmp_ring_num_pages; 118 uint64_t rings_state_ppn; 119 uint64_t req_ring_ppns[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES]; 120 uint64_t cmp_ring_ppns[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES]; 121 }; 122 123 struct pvscsi_cmd_desc_setup_msg_ring { 124 uint32_t num_pages; 125 uint32_t pad_; 126 uint64_t ring_ppns[PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES]; 127 }; 128 129 struct pvscsi_rings_state { 130 uint32_t req_prod_idx; 131 uint32_t req_cons_idx; 132 uint32_t req_num_entries_log2; 133 uint32_t cmp_prod_idx; 134 uint32_t cmp_cons_idx; 135 uint32_t cmp_num_entries_log2; 136 uint32_t req_call_threshold; 137 uint8_t _pad[100]; 138 uint32_t msg_prod_idx; 139 uint32_t msg_cons_idx; 140 uint32_t msg_num_entries_log2; 141 }; 142 143 #define PVSCSI_FLAG_CMD_WITH_SG_LIST (1 << 0) 144 #define PVSCSI_FLAG_CMD_OUT_OF_BAND_CDB (1 << 1) 145 #define PVSCSI_FLAG_CMD_DIR_NONE (1 << 2) 146 #define PVSCSI_FLAG_CMD_DIR_TOHOST (1 << 3) 147 #define PVSCSI_FLAG_CMD_DIR_TODEVICE (1 << 4) 148 149 #define PVSCSI_FLAG_RESERVED_MASK (~MASK(5)) 150 151 #define PVSCSI_INTR_CMPL_0 (1 << 0) 152 #define PVSCSI_INTR_CMPL_1 (1 << 1) 153 #define PVSCSI_INTR_CMPL_MASK MASK(2) 154 155 #define PVSCSI_INTR_MSG_0 (1 << 2) 156 #define PVSCSI_INTR_MSG_1 (1 << 3) 157 #define PVSCSI_INTR_MSG_MASK (MASK(2) << 2) 158 159 #define PVSCSI_INTR_ALL_SUPPORTED MASK(4) 160 161 struct pvscsi_ring_req_desc { 162 uint64_t context; 163 uint64_t data_addr; 164 uint64_t data_len; 165 uint64_t sense_addr; 166 uint32_t sense_len; 167 uint32_t flags; 168 uint8_t cdb[16]; 169 uint8_t cdb_len; 170 uint8_t lun[8]; 171 uint8_t tag; 172 uint8_t bus; 173 uint8_t target; 174 uint16_t vcpu_hint; 175 uint8_t unused[58]; 176 }; 177 178 CTASSERT(sizeof(struct pvscsi_ring_req_desc) == 128); 179 180 struct pvscsi_ring_cmp_desc { 181 uint64_t context; 182 uint64_t data_len; 183 uint32_t sense_len; 184 uint16_t host_status; 185 uint16_t scsi_status; 186 uint32_t _pad[2]; 187 }; 188 189 CTASSERT(sizeof(struct pvscsi_ring_cmp_desc) == 32); 190 191 #define PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT 128 192 #define PVSCSI_MAX_NUM_SG_SEGMENTS 128 193 #define PVSCSI_SGE_FLAG_CHAIN_ELEMENT (1 << 0) 194 195 struct pvscsi_sg_element { 196 uint64_t addr; 197 uint32_t length; 198 uint32_t flags; 199 }; 200 201 enum pvscsi_msg_type { 202 PVSCSI_MSG_DEV_ADDED = 0, 203 PVSCSI_MSG_DEV_REMOVED = 1, 204 PVSCSI_MSG_LAST = 2, 205 }; 206 207 struct pvscsi_ring_msg_desc { 208 uint32_t type; 209 uint32_t args[31]; 210 }; 211 212 struct pvscsi_ring_msg_dev_status_changed { 213 uint32_t type; 214 uint32_t bus; 215 uint32_t target; 216 uint8_t lun[8]; 217 uint32_t pad[27]; 218 }; 219 220 struct pvscsi_cmd_desc_setup_req_call { 221 uint32_t enable; 222 }; 223 224 #define PVSCSI_MAX_NUM_PAGES_REQ_RING PVSCSI_SETUP_RINGS_MAX_NUM_PAGES 225 #define PVSCSI_MAX_NUM_PAGES_CMP_RING PVSCSI_SETUP_RINGS_MAX_NUM_PAGES 226 #define PVSCSI_MAX_NUM_PAGES_MSG_RING PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES 227 228 #define PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE \ 229 (PAGE_SIZE / sizeof(struct pvscsi_ring_req_desc)) 230 #define PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE \ 231 (PAGE_SIZE / sizeof(struct pvscs_ring_cmp_desc)) 232 #define PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE \ 233 (PAGE_SIZE / sizeof(struct pvscsi_ring_msg_desc)) 234 235 #define PVSCSI_MAX_REQ_QUEUE_DEPTH \ 236 (PVSCSI_MAX_NUM_PAGES_REQ_RING * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE) 237 #define PVSCSI_MAX_CMP_QUEUE_DEPTH \ 238 (PVSCSI_MAX_NUM_PAGES_CMP_RING * PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE) 239 #define PVSCSI_MAX_QUEUE_DEPTH \ 240 MAX(PVSCSI_MAX_REQ_QUEUE_DEPTH, PVSCSI_MAX_CMP_QUEUE_DEPTH) 241 242 enum pvscsi_host_status { 243 BTSTAT_SUCCESS = 0x00, 244 BTSTAT_LINKED_COMMAND_COMPLETED = 0x0a, 245 BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG = 0x0b, 246 BTSTAT_DATA_UNDERRUN = 0x0c, 247 BTSTAT_SELTIMEO = 0x11, 248 BTSTAT_DATARUN = 0x12, 249 BTSTAT_BUSFREE = 0x13, 250 BTSTAT_INVPHASE = 0x14, 251 BTSTAT_INVCODE = 0x15, 252 BTSTAT_INVOPCODE = 0x16, 253 BTSTAT_LUNMISMATCH = 0x17, 254 BTSTAT_INVPARAM = 0x1a, 255 BTSTAT_SENSFAILED = 0x1b, 256 BTSTAT_TAGREJECT = 0x1c, 257 BTSTAT_BADMSG = 0x1d, 258 BTSTAT_HAHARDWARE = 0x20, 259 BTSTAT_NORESPONSE = 0x21, 260 BTSTAT_SENTRST = 0x22, 261 BTSTAT_RECVRST = 0x23, 262 BTSTAT_DISCONNECT = 0x24, 263 BTSTAT_BUSRESET = 0x25, 264 BTSTAT_ABORTQUEUE = 0x26, 265 BTSTAT_HASOFTWARE = 0x27, 266 BTSTAT_HATIMEOUT = 0x30, 267 BTSTAT_SCSIPARITY = 0x34, 268 }; 269 270 #endif /* !_PVSCSI_H_ */ 271