2 * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/resource.h>
33 #include <sys/eventhandler.h>
34 #include <sys/devicestat.h>
38 #include <vm/vm_extern.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <machine/bus.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>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_message.h>
60 UCHAR status; /* 0 nonbootable; 80h bootable */
66 ULONG start_abs_sector;
70 typedef struct _INQUIRYDATA {
72 UCHAR DeviceTypeQualifier : 3;
73 UCHAR DeviceTypeModifier : 7;
74 UCHAR RemovableMedia : 1;
76 UCHAR ResponseDataFormat;
77 UCHAR AdditionalLength;
80 UCHAR CommandQueue : 1;
82 UCHAR LinkedCommands : 1;
83 UCHAR Synchronous : 1;
86 UCHAR RelativeAddressing : 1;
89 UCHAR ProductRevisionLevel[4];
90 UCHAR VendorSpecific[20];
92 } INQUIRYDATA, *PINQUIRYDATA;
94 #define MV_IAL_HT_SACOALT_DEFAULT 1
95 #define MV_IAL_HT_SAITMTH_DEFAULT 1
97 /****************************************/
98 /* GENERAL Definitions */
99 /****************************************/
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 */
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 */
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)
118 typedef enum _SataEvent
120 SATA_EVENT_NO_CHANGE = 0,
121 SATA_EVENT_CHANNEL_CONNECTED,
122 SATA_EVENT_CHANNEL_DISCONNECTED
125 typedef ULONG_PTR dma_addr_t;
127 typedef struct _MV_CHANNEL
129 unsigned int maxUltraDmaModeSupported;
130 unsigned int maxDmaModeSupported;
131 unsigned int maxPioModeSupported;
133 MV_BOOLEAN writeCacheSupported;
134 MV_BOOLEAN writeCacheEnabled;
135 MV_BOOLEAN readAheadSupported;
136 MV_BOOLEAN readAheadEnabled;
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;
149 typedef struct IALAdapter
151 struct cam_path *path;
154 bus_dma_tag_t io_dma_parent; /* I/O buffer DMA tag */
155 PBUS_DMAMAP pbus_dmamap_list;
156 PBUS_DMAMAP pbus_dmamap;
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;
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];
176 struct callout event_timer_connect;
177 struct callout event_timer_disconnect;
180 struct _VDevice VDevices[MV_SATA_CHANNELS_NUM];
181 PCommand pCommandBlocks;
183 PUCHAR prdTableAlignedAddr;
186 union ccb *pending_Q;
188 MV_U8 outstandingCommands;
198 extern IAL_ADAPTER_T *gIal_Adapter;
201 typedef void (*HPT_DPC)(IAL_ADAPTER_T *,void*,UCHAR);
203 int hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T *pAdapter, void *arg, UCHAR flags);
204 void hpt_rebuild_data_block(IAL_ADAPTER_T *pAdapter, PVDevice pArray, UCHAR flags);
205 void Check_Idle_Call(IAL_ADAPTER_T *pAdapter);
206 void fRescanAllDevice(_VBUS_ARG0);
207 int hpt_add_disk_to_array(_VBUS_ARG DEVICEID idArray, DEVICEID idDisk);
209 int Kernel_DeviceIoControl(_VBUS_ARG
210 DWORD dwIoControlCode, /* operation control code */
211 PVOID lpInBuffer, /* input data buffer */
212 DWORD nInBufferSize, /* size of input data buffer */
213 PVOID lpOutBuffer, /* output data buffer */
214 DWORD nOutBufferSize, /* size of output data buffer */
215 PDWORD lpBytesReturned /* byte count */
219 #define __str_direct(x) #x
220 #define __str(x) __str_direct(x)
221 #define KMSG_LEADING __str(PROC_DIR_NAME) ": "
222 #define hpt_printk(_x_) do { printf(KMSG_LEADING); printf _x_ ; } while (0)
226 #define REBUILD_PARITY 2
229 /***********************************************************/
231 static __inline struct cam_periph *
232 hpt_get_periph(int path_id,int target_id)
234 struct cam_periph *periph = NULL;
235 struct cam_path *path;
238 status = xpt_create_path(&path, NULL, path_id, target_id, 0);
239 if (status == CAM_REQ_CMP) {
240 periph = cam_periph_find(path, "da");
248 #define BITS_PER_LONG 32
249 #define VDEV_TO_ID(pVDev) (DEVICEID)(pVDev)
250 #define ID_TO_VDEV(id) (PVDevice)(id)
251 #else /*Only support x86_64(AMD64 and EM64T)*/
252 #define BITS_PER_LONG 64
253 #define VDEV_TO_ID(pVDev) (DEVICEID)(ULONG_PTR)(pVDev)
254 #define ID_TO_VDEV(id) (PVDevice)(((ULONG_PTR)gIal_Adapter & 0xffffffff00000000) | (id))
257 #define INVALID_DEVICEID (-1)
258 #define INVALID_STRIPSIZE (-1)
260 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
262 #ifndef MinBlockSizeShift
263 #define MinBlockSizeShift 5
264 #define MaxBlockSizeShift 12
268 typedef struct _HPT_IOCTL_TRANSFER_PARAM
271 ULONG nOutBufferSize;
273 }HPT_IOCTL_TRANSFER_PARAM, *PHPT_IOCTL_TRANSFER_PARAM;
275 typedef struct _HPT_SET_STATE_PARAM
279 } HPT_SET_STATE_PARAM, *PHPT_SET_STATE_PARAM;
281 typedef struct _HPT_SET_ARRAY_INFO
284 ALTERABLE_ARRAY_INFO Info;
285 } HPT_SET_ARRAY_INFO, *PHPT_SET_ARRAY_INFO;
287 typedef struct _HPT_SET_DEVICE_INFO
290 ALTERABLE_DEVICE_INFO Info;
291 } HPT_SET_DEVICE_INFO, *PHPT_SET_DEVICE_INFO;
293 typedef struct _HPT_SET_DEVICE_INFO_V2
296 ALTERABLE_DEVICE_INFO_V2 Info;
297 } HPT_SET_DEVICE_INFO_V2, *PHPT_SET_DEVICE_INFO_V2;
299 typedef struct _HPT_ADD_DISK_TO_ARRAY
303 } HPT_ADD_DISK_TO_ARRAY, *PHPT_ADD_DISK_TO_ARRAY;
305 typedef struct _HPT_DEVICE_IO
312 } HPT_DEVICE_IO, *PHPT_DEVICE_IO;
314 int check_VDevice_valid(PVDevice);
315 int hpt_default_ioctl(_VBUS_ARG DWORD, PVOID, DWORD, PVOID, DWORD, PDWORD);
317 #define HPT_NULL_ID 0