1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004-2005 HighPoint Technologies, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #ifndef _OSBSD_H_ 29 #define _OSBSD_H_ 30 31 #include <sys/bus.h> 32 #include <sys/resource.h> 33 #include <sys/eventhandler.h> 34 #include <sys/devicestat.h> 35 36 #include <vm/vm.h> 37 #include <vm/pmap.h> 38 #include <vm/vm_extern.h> 39 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 #include <machine/bus.h> 43 44 #include <sys/rman.h> 45 46 #include <cam/cam.h> 47 #include <cam/cam_ccb.h> 48 #include <cam/cam_debug.h> 49 #include <cam/cam_sim.h> 50 #include <cam/cam_xpt_sim.h> 51 #include <cam/cam_periph.h> 52 53 #include <cam/scsi/scsi_all.h> 54 #include <cam/scsi/scsi_message.h> 55 56 57 58 typedef struct 59 { 60 UCHAR status; /* 0 nonbootable; 80h bootable */ 61 UCHAR start_head; 62 USHORT start_sector; 63 UCHAR type; 64 UCHAR end_head; 65 USHORT end_sector; 66 ULONG start_abs_sector; 67 ULONG num_of_sector; 68 } partition; 69 70 typedef struct _INQUIRYDATA { 71 UCHAR DeviceType : 5; 72 UCHAR DeviceTypeQualifier : 3; 73 UCHAR DeviceTypeModifier : 7; 74 UCHAR RemovableMedia : 1; 75 UCHAR Versions; 76 UCHAR ResponseDataFormat; 77 UCHAR AdditionalLength; 78 UCHAR Reserved[2]; 79 UCHAR SoftReset : 1; 80 UCHAR CommandQueue : 1; 81 UCHAR Reserved2 : 1; 82 UCHAR LinkedCommands : 1; 83 UCHAR Synchronous : 1; 84 UCHAR Wide16Bit : 1; 85 UCHAR Wide32Bit : 1; 86 UCHAR RelativeAddressing : 1; 87 UCHAR VendorId[8]; 88 UCHAR ProductId[16]; 89 UCHAR ProductRevisionLevel[4]; 90 UCHAR VendorSpecific[20]; 91 UCHAR Reserved3[40]; 92 } INQUIRYDATA, *PINQUIRYDATA; 93 94 #define MV_IAL_HT_SACOALT_DEFAULT 1 95 #define MV_IAL_HT_SAITMTH_DEFAULT 1 96 97 /****************************************/ 98 /* GENERAL Definitions */ 99 /****************************************/ 100 101 /* Bits for HD_ERROR */ 102 #define NM_ERR 0x02 /* media present */ 103 #define ABRT_ERR 0x04 /* Command aborted */ 104 #define MCR_ERR 0x08 /* media change request */ 105 #define IDNF_ERR 0x10 /* ID field not found */ 106 #define MC_ERR 0x20 /* media changed */ 107 #define UNC_ERR 0x40 /* Uncorrect data */ 108 #define WP_ERR 0x40 /* write protect */ 109 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */ 110 111 #define REQUESTS_ARRAY_SIZE (9 * MV_EDMA_REQUEST_QUEUE_SIZE) /* 9 K bytes */ 112 #define RESPONSES_ARRAY_SIZE (12 * MV_EDMA_RESPONSE_QUEUE_SIZE) /* 3 K bytes */ 113 114 #define PRD_ENTRIES_PER_CMD (MAX_SG_DESCRIPTORS+1) 115 #define PRD_ENTRIES_SIZE (MV_EDMA_PRD_ENTRY_SIZE*PRD_ENTRIES_PER_CMD) 116 #define PRD_TABLES_FOR_VBUS (MV_SATA_CHANNELS_NUM*MV_EDMA_QUEUE_LENGTH) 117 118 typedef enum _SataEvent 119 { 120 SATA_EVENT_NO_CHANGE = 0, 121 SATA_EVENT_CHANNEL_CONNECTED, 122 SATA_EVENT_CHANNEL_DISCONNECTED 123 } SATA_EVENT; 124 125 typedef ULONG_PTR dma_addr_t; 126 127 typedef struct _MV_CHANNEL 128 { 129 unsigned int maxUltraDmaModeSupported; 130 unsigned int maxDmaModeSupported; 131 unsigned int maxPioModeSupported; 132 MV_BOOLEAN online; 133 MV_BOOLEAN writeCacheSupported; 134 MV_BOOLEAN writeCacheEnabled; 135 MV_BOOLEAN readAheadSupported; 136 MV_BOOLEAN readAheadEnabled; 137 MV_U8 queueDepth; 138 139 } MV_CHANNEL; 140 141 typedef struct _BUS_DMAMAP 142 { struct _BUS_DMAMAP *next; 143 struct IALAdapter *pAdapter; 144 bus_dmamap_t dma_map; 145 struct callout timeout; 146 SCAT_GATH psg[MAX_SG_DESCRIPTORS]; 147 } BUS_DMAMAP, *PBUS_DMAMAP; 148 149 typedef struct IALAdapter 150 { 151 struct cam_path *path; 152 struct mtx lock; 153 154 bus_dma_tag_t io_dma_parent; /* I/O buffer DMA tag */ 155 PBUS_DMAMAP pbus_dmamap_list; 156 PBUS_DMAMAP pbus_dmamap; 157 158 device_t hpt_dev; /* bus device */ 159 struct resource *hpt_irq; /* interrupt */ 160 struct resource *mem_res; 161 void *hpt_intr; /* interrupt handle */ 162 struct IALAdapter *next; 163 164 MV_SATA_ADAPTER mvSataAdapter; 165 MV_CHANNEL mvChannel[MV_SATA_CHANNELS_NUM]; 166 MV_U8 *requestsArrayBaseAddr; 167 MV_U8 *requestsArrayBaseAlignedAddr; 168 dma_addr_t requestsArrayBaseDmaAddr; 169 dma_addr_t requestsArrayBaseDmaAlignedAddr; 170 MV_U8 *responsesArrayBaseAddr; 171 MV_U8 *responsesArrayBaseAlignedAddr; 172 dma_addr_t responsesArrayBaseDmaAddr; 173 dma_addr_t responsesArrayBaseDmaAlignedAddr; 174 SATA_EVENT sataEvents[MV_SATA_CHANNELS_NUM]; 175 176 struct callout event_timer_connect; 177 struct callout event_timer_disconnect; 178 179 struct _VBus VBus; 180 struct _VDevice VDevices[MV_SATA_CHANNELS_NUM]; 181 PCommand pCommandBlocks; 182 PUCHAR prdTableAddr; 183 PUCHAR prdTableAlignedAddr; 184 void* pFreePRDLink; 185 186 union ccb *pending_Q; 187 188 MV_U8 outstandingCommands; 189 190 UCHAR status; 191 UCHAR ver_601; 192 UCHAR beeping; 193 } 194 IAL_ADAPTER_T; 195 196 extern IAL_ADAPTER_T *gIal_Adapter; 197 198 /*entry.c*/ 199 typedef void (*HPT_DPC)(IAL_ADAPTER_T *,void*,UCHAR); 200 201 int hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T *pAdapter, void *arg, UCHAR flags); 202 void hpt_rebuild_data_block(IAL_ADAPTER_T *pAdapter, PVDevice pArray, UCHAR flags); 203 void Check_Idle_Call(IAL_ADAPTER_T *pAdapter); 204 void fRescanAllDevice(_VBUS_ARG0); 205 int hpt_add_disk_to_array(_VBUS_ARG DEVICEID idArray, DEVICEID idDisk); 206 207 int Kernel_DeviceIoControl(_VBUS_ARG 208 DWORD dwIoControlCode, /* operation control code */ 209 PVOID lpInBuffer, /* input data buffer */ 210 DWORD nInBufferSize, /* size of input data buffer */ 211 PVOID lpOutBuffer, /* output data buffer */ 212 DWORD nOutBufferSize, /* size of output data buffer */ 213 PDWORD lpBytesReturned /* byte count */ 214 ); 215 216 217 #define __str_direct(x) #x 218 #define __str(x) __str_direct(x) 219 #define KMSG_LEADING __str(PROC_DIR_NAME) ": " 220 #define hpt_printk(_x_) do { printf(KMSG_LEADING); printf _x_ ; } while (0) 221 222 #define DUPLICATE 0 223 #define INITIALIZE 1 224 #define REBUILD_PARITY 2 225 #define VERIFY 3 226 227 /***********************************************************/ 228 229 static __inline struct cam_periph * 230 hpt_get_periph(int path_id,int target_id) 231 { 232 struct cam_periph *periph = NULL; 233 struct cam_path *path; 234 int status; 235 236 status = xpt_create_path(&path, NULL, path_id, target_id, 0); 237 if (status == CAM_REQ_CMP) { 238 periph = cam_periph_find(path, "da"); 239 xpt_free_path(path); 240 241 } 242 return periph; 243 } 244 245 #ifdef __i386__ 246 #define BITS_PER_LONG 32 247 #define VDEV_TO_ID(pVDev) (DEVICEID)(pVDev) 248 #define ID_TO_VDEV(id) (PVDevice)(id) 249 #else /*Only support x86_64(AMD64 and EM64T)*/ 250 #define BITS_PER_LONG 64 251 #define VDEV_TO_ID(pVDev) (DEVICEID)(ULONG_PTR)(pVDev) 252 #define ID_TO_VDEV(id) (PVDevice)(((ULONG_PTR)gIal_Adapter & 0xffffffff00000000) | (id)) 253 #endif 254 255 #define INVALID_DEVICEID (-1) 256 #define INVALID_STRIPSIZE (-1) 257 258 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8)) 259 260 #ifndef MinBlockSizeShift 261 #define MinBlockSizeShift 5 262 #define MaxBlockSizeShift 12 263 #endif 264 265 #pragma pack(1) 266 typedef struct _HPT_IOCTL_TRANSFER_PARAM 267 { 268 ULONG nInBufferSize; 269 ULONG nOutBufferSize; 270 UCHAR buffer[0]; 271 }HPT_IOCTL_TRANSFER_PARAM, *PHPT_IOCTL_TRANSFER_PARAM; 272 273 typedef struct _HPT_SET_STATE_PARAM 274 { 275 DEVICEID idArray; 276 DWORD state; 277 } HPT_SET_STATE_PARAM, *PHPT_SET_STATE_PARAM; 278 279 typedef struct _HPT_SET_ARRAY_INFO 280 { 281 DEVICEID idArray; 282 ALTERABLE_ARRAY_INFO Info; 283 } HPT_SET_ARRAY_INFO, *PHPT_SET_ARRAY_INFO; 284 285 typedef struct _HPT_SET_DEVICE_INFO 286 { 287 DEVICEID idDisk; 288 ALTERABLE_DEVICE_INFO Info; 289 } HPT_SET_DEVICE_INFO, *PHPT_SET_DEVICE_INFO; 290 291 typedef struct _HPT_SET_DEVICE_INFO_V2 292 { 293 DEVICEID idDisk; 294 ALTERABLE_DEVICE_INFO_V2 Info; 295 } HPT_SET_DEVICE_INFO_V2, *PHPT_SET_DEVICE_INFO_V2; 296 297 typedef struct _HPT_ADD_DISK_TO_ARRAY 298 { 299 DEVICEID idArray; 300 DEVICEID idDisk; 301 } HPT_ADD_DISK_TO_ARRAY, *PHPT_ADD_DISK_TO_ARRAY; 302 303 typedef struct _HPT_DEVICE_IO 304 { 305 DEVICEID id; 306 int cmd; 307 ULONG lba; 308 DWORD nSector; 309 UCHAR buffer[0]; 310 } HPT_DEVICE_IO, *PHPT_DEVICE_IO; 311 312 int check_VDevice_valid(PVDevice); 313 int hpt_default_ioctl(_VBUS_ARG DWORD, PVOID, DWORD, PVOID, DWORD, PDWORD); 314 315 #define HPT_NULL_ID 0 316 317 #pragma pack() 318 319 #endif 320