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>
36 #if (__FreeBSD_version < 500043)
43 #include <vm/vm_extern.h>
45 #if (__FreeBSD_version < 600030)
46 #include <machine/bus_memio.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_debug.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_xpt_periph.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
67 extern intrmask_t lock_driver(void);
68 extern void unlock_driver(intrmask_t spl);
72 UCHAR status; /* 0 nonbootable; 80h bootable */
78 ULONG start_abs_sector;
82 typedef struct _INQUIRYDATA {
84 UCHAR DeviceTypeQualifier : 3;
85 UCHAR DeviceTypeModifier : 7;
86 UCHAR RemovableMedia : 1;
88 UCHAR ResponseDataFormat;
89 UCHAR AdditionalLength;
92 UCHAR CommandQueue : 1;
94 UCHAR LinkedCommands : 1;
95 UCHAR Synchronous : 1;
98 UCHAR RelativeAddressing : 1;
101 UCHAR ProductRevisionLevel[4];
102 UCHAR VendorSpecific[20];
104 } INQUIRYDATA, *PINQUIRYDATA;
106 typedef struct _READ_CAPACITY_DATA {
107 ULONG LogicalBlockAddress;
109 } READ_CAPACITY_DATA, *PREAD_CAPACITY_DATA;
111 #define MV_IAL_HT_SACOALT_DEFAULT 1
112 #define MV_IAL_HT_SAITMTH_DEFAULT 1
114 /****************************************/
115 /* GENERAL Definitions */
116 /****************************************/
118 /* Bits for HD_ERROR */
119 #define NM_ERR 0x02 /* media present */
120 #define ABRT_ERR 0x04 /* Command aborted */
121 #define MCR_ERR 0x08 /* media change request */
122 #define IDNF_ERR 0x10 /* ID field not found */
123 #define MC_ERR 0x20 /* media changed */
124 #define UNC_ERR 0x40 /* Uncorrect data */
125 #define WP_ERR 0x40 /* write protect */
126 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
128 #define REQUESTS_ARRAY_SIZE (9 * MV_EDMA_REQUEST_QUEUE_SIZE) /* 9 K bytes */
129 #define RESPONSES_ARRAY_SIZE (12 * MV_EDMA_RESPONSE_QUEUE_SIZE) /* 3 K bytes */
131 #define PRD_ENTRIES_PER_CMD (MAX_SG_DESCRIPTORS+1)
132 #define PRD_ENTRIES_SIZE (MV_EDMA_PRD_ENTRY_SIZE*PRD_ENTRIES_PER_CMD)
133 #define PRD_TABLES_FOR_VBUS (MV_SATA_CHANNELS_NUM*MV_EDMA_QUEUE_LENGTH)
135 typedef enum _SataEvent
137 SATA_EVENT_NO_CHANGE = 0,
138 SATA_EVENT_CHANNEL_CONNECTED,
139 SATA_EVENT_CHANNEL_DISCONNECTED
142 typedef ULONG_PTR dma_addr_t;
144 typedef struct _MV_CHANNEL
146 unsigned int maxUltraDmaModeSupported;
147 unsigned int maxDmaModeSupported;
148 unsigned int maxPioModeSupported;
152 typedef struct _BUS_DMAMAP
153 { struct _BUS_DMAMAP *next;
154 struct IALAdapter *pAdapter;
155 bus_dmamap_t dma_map;
156 SCAT_GATH psg[MAX_SG_DESCRIPTORS];
157 } BUS_DMAMAP, *PBUS_DMAMAP;
159 typedef struct IALAdapter
161 struct cam_path *path;
163 bus_dma_tag_t io_dma_parent; /* I/O buffer DMA tag */
164 PBUS_DMAMAP pbus_dmamap_list;
165 PBUS_DMAMAP pbus_dmamap;
167 device_t hpt_dev; /* bus device */
168 struct resource *hpt_irq; /* interrupt */
169 struct resource *mem_res;
170 void *hpt_intr; /* interrupt handle */
171 struct IALAdapter *next;
173 MV_SATA_ADAPTER mvSataAdapter;
174 MV_CHANNEL mvChannel[MV_SATA_CHANNELS_NUM];
175 MV_U8 *requestsArrayBaseAddr;
176 MV_U8 *requestsArrayBaseAlignedAddr;
177 dma_addr_t requestsArrayBaseDmaAddr;
178 dma_addr_t requestsArrayBaseDmaAlignedAddr;
179 MV_U8 *responsesArrayBaseAddr;
180 MV_U8 *responsesArrayBaseAlignedAddr;
181 dma_addr_t responsesArrayBaseDmaAddr;
182 dma_addr_t responsesArrayBaseDmaAlignedAddr;
183 SATA_EVENT sataEvents[MV_SATA_CHANNELS_NUM];
185 struct callout_handle event_timer_connect;
186 struct callout_handle event_timer_disconnect;
189 struct _VDevice VDevices[MV_SATA_CHANNELS_NUM];
190 PCommand pCommandBlocks;
192 PUCHAR prdTableAlignedAddr;
195 union ccb *pending_Q;
197 MV_U8 outstandingCommands;
207 extern IAL_ADAPTER_T *gIal_Adapter;
210 typedef void (*HPT_DPC)(IAL_ADAPTER_T *,void*,UCHAR);
212 int hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T *pAdapter, void *arg, UCHAR flags);
213 void hpt_rebuild_data_block(IAL_ADAPTER_T *pAdapter, PVDevice pArray, UCHAR flags);
214 void Check_Idle_Call(IAL_ADAPTER_T *pAdapter);
215 void fRescanAllDevice(_VBUS_ARG0);
216 int hpt_add_disk_to_array(_VBUS_ARG DEVICEID idArray, DEVICEID idDisk);
218 int Kernel_DeviceIoControl(_VBUS_ARG
219 DWORD dwIoControlCode, /* operation control code */
220 PVOID lpInBuffer, /* input data buffer */
221 DWORD nInBufferSize, /* size of input data buffer */
222 PVOID lpOutBuffer, /* output data buffer */
223 DWORD nOutBufferSize, /* size of output data buffer */
224 PDWORD lpBytesReturned /* byte count */
228 #define __str_direct(x) #x
229 #define __str(x) __str_direct(x)
230 #define KMSG_LEADING __str(PROC_DIR_NAME) ": "
231 #define hpt_printk(_x_) do { printf(KMSG_LEADING); printf _x_ ; } while (0)
235 #define REBUILD_PARITY 2
238 /***********************************************************/
240 static __inline struct cam_periph *
241 hpt_get_periph(int path_id,int target_id)
243 struct cam_periph *periph = NULL;
244 struct cam_path *path;
247 status = xpt_create_path(&path, NULL, path_id, target_id, 0);
248 if (status == CAM_REQ_CMP) {
249 periph = cam_periph_find(path, "da");
256 #if (__FreeBSD_version < 500000)
257 #define YIELD_THREAD yield(curproc, 0)
261 #define BITS_PER_LONG 32
262 #define VDEV_TO_ID(pVDev) (DEVICEID)(pVDev)
263 #define ID_TO_VDEV(id) (PVDevice)(id)
264 #else /*Only support x86_64(AMD64 and EM64T)*/
265 #define BITS_PER_LONG 64
266 #define VDEV_TO_ID(pVDev) (DEVICEID)(ULONG_PTR)(pVDev)
267 #define ID_TO_VDEV(id) (PVDevice)(((ULONG_PTR)gIal_Adapter & 0xffffffff00000000) | (id))
270 #define INVALID_DEVICEID (-1)
271 #define INVALID_STRIPSIZE (-1)
273 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
275 #ifndef MinBlockSizeShift
276 #define MinBlockSizeShift 5
277 #define MaxBlockSizeShift 12
281 typedef struct _HPT_IOCTL_TRANSFER_PARAM
284 ULONG nOutBufferSize;
286 }HPT_IOCTL_TRANSFER_PARAM, *PHPT_IOCTL_TRANSFER_PARAM;
288 typedef struct _HPT_SET_STATE_PARAM
292 } HPT_SET_STATE_PARAM, *PHPT_SET_STATE_PARAM;
294 typedef struct _HPT_SET_ARRAY_INFO
297 ALTERABLE_ARRAY_INFO Info;
298 } HPT_SET_ARRAY_INFO, *PHPT_SET_ARRAY_INFO;
300 typedef struct _HPT_SET_DEVICE_INFO
303 ALTERABLE_DEVICE_INFO Info;
304 } HPT_SET_DEVICE_INFO, *PHPT_SET_DEVICE_INFO;
306 typedef struct _HPT_ADD_DISK_TO_ARRAY
310 } HPT_ADD_DISK_TO_ARRAY, *PHPT_ADD_DISK_TO_ARRAY;
312 int check_VDevice_valid(PVDevice);
313 int hpt_default_ioctl(_VBUS_ARG DWORD, PVOID, DWORD, PVOID, DWORD, PDWORD);
315 #define HPT_NULL_ID 0