xref: /freebsd/sys/dev/hptmv/osbsd.h (revision 9517e866259191fcd39434a97ad849a9b59b9b9f)
1 /*
2  * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
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 #if (__FreeBSD_version < 500043)
37 #include <stddef.h>
38 #include <sys/buf.h>
39 #endif
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_extern.h>
44 
45 #if (__FreeBSD_version < 600000)
46 #include <machine/bus_memio.h>
47 #endif
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
51 
52 #include <sys/rman.h>
53 
54 #include <cam/cam.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 
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 
64 
65 
66 extern intrmask_t lock_driver(void);
67 extern void unlock_driver(intrmask_t spl);
68 
69 typedef struct
70 {
71 	UCHAR		status;     		/* 0 nonbootable; 80h bootable */
72 	UCHAR      	start_head;
73 	USHORT     	start_sector;
74 	UCHAR      	type;
75 	UCHAR      	end_head;
76 	USHORT     	end_sector;
77 	ULONG      	start_abs_sector;
78 	ULONG      	num_of_sector;
79 } partition;
80 
81 typedef struct _INQUIRYDATA {
82 	UCHAR DeviceType : 5;
83 	UCHAR DeviceTypeQualifier : 3;
84 	UCHAR DeviceTypeModifier : 7;
85 	UCHAR RemovableMedia : 1;
86 	UCHAR Versions;
87 	UCHAR ResponseDataFormat;
88 	UCHAR AdditionalLength;
89 	UCHAR Reserved[2];
90 	UCHAR SoftReset : 1;
91 	UCHAR CommandQueue : 1;
92 	UCHAR Reserved2 : 1;
93 	UCHAR LinkedCommands : 1;
94 	UCHAR Synchronous : 1;
95 	UCHAR Wide16Bit : 1;
96 	UCHAR Wide32Bit : 1;
97 	UCHAR RelativeAddressing : 1;
98 	UCHAR VendorId[8];
99 	UCHAR ProductId[16];
100 	UCHAR ProductRevisionLevel[4];
101 	UCHAR VendorSpecific[20];
102 	UCHAR Reserved3[40];
103 } INQUIRYDATA, *PINQUIRYDATA;
104 
105 #define MV_IAL_HT_SACOALT_DEFAULT	1
106 #define MV_IAL_HT_SAITMTH_DEFAULT	1
107 
108 /****************************************/
109 /*          GENERAL Definitions         */
110 /****************************************/
111 
112 /* Bits for HD_ERROR */
113 #define NM_ERR			0x02	/* media present */
114 #define ABRT_ERR		0x04	/* Command aborted */
115 #define MCR_ERR         0x08	/* media change request */
116 #define IDNF_ERR        0x10	/* ID field not found */
117 #define MC_ERR          0x20	/* media changed */
118 #define UNC_ERR         0x40	/* Uncorrect data */
119 #define WP_ERR          0x40	/* write protect */
120 #define ICRC_ERR        0x80	/* new meaning:  CRC error during transfer */
121 
122 #define REQUESTS_ARRAY_SIZE			(9 * MV_EDMA_REQUEST_QUEUE_SIZE) /* 9 K bytes */
123 #define RESPONSES_ARRAY_SIZE		(12 * MV_EDMA_RESPONSE_QUEUE_SIZE) /* 3 K bytes */
124 
125 #define PRD_ENTRIES_PER_CMD         (MAX_SG_DESCRIPTORS+1)
126 #define PRD_ENTRIES_SIZE            (MV_EDMA_PRD_ENTRY_SIZE*PRD_ENTRIES_PER_CMD)
127 #define PRD_TABLES_FOR_VBUS         (MV_SATA_CHANNELS_NUM*MV_EDMA_QUEUE_LENGTH)
128 
129 typedef enum _SataEvent
130 {
131     SATA_EVENT_NO_CHANGE = 0,
132 	SATA_EVENT_CHANNEL_CONNECTED,
133 	SATA_EVENT_CHANNEL_DISCONNECTED
134 } SATA_EVENT;
135 
136 typedef ULONG_PTR dma_addr_t;
137 
138 typedef struct _MV_CHANNEL
139 {
140 	unsigned int		maxUltraDmaModeSupported;
141 	unsigned int		maxDmaModeSupported;
142 	unsigned int		maxPioModeSupported;
143 	MV_BOOLEAN		online;
144 	MV_BOOLEAN		writeCacheSupported;
145 	MV_BOOLEAN		writeCacheEnabled;
146 	MV_BOOLEAN		readAheadSupported;
147 	MV_BOOLEAN		readAheadEnabled;
148 	MV_U8			queueDepth;
149 
150 } MV_CHANNEL;
151 
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;
158 
159 typedef struct IALAdapter
160 {
161 	struct cam_path 	*path;
162 
163 	bus_dma_tag_t	  io_dma_parent; /* I/O buffer DMA tag */
164 	PBUS_DMAMAP	  pbus_dmamap_list;
165 	PBUS_DMAMAP	  pbus_dmamap;
166 
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;
172 
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];
184 
185    	struct	callout_handle event_timer_connect;
186   	struct	callout_handle event_timer_disconnect;
187 
188 	struct _VBus        VBus;
189 	struct _VDevice     VDevices[MV_SATA_CHANNELS_NUM];
190 	PCommand			pCommandBlocks;
191 	PUCHAR				prdTableAddr;
192 	PUCHAR				prdTableAlignedAddr;
193 	void*				pFreePRDLink;
194 
195 	union ccb           *pending_Q;
196 
197 	MV_U8   			outstandingCommands;
198 
199 	UCHAR               status;
200 	UCHAR               ver_601;
201 	UCHAR               beeping;
202 
203 	eventhandler_tag	eh;
204 }
205 IAL_ADAPTER_T;
206 
207 extern IAL_ADAPTER_T *gIal_Adapter;
208 
209 /*entry.c*/
210 typedef void (*HPT_DPC)(IAL_ADAPTER_T *,void*,UCHAR);
211 
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);
217 
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 */
225 						);
226 
227 
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)
232 
233 #define DUPLICATE      0
234 #define INITIALIZE     1
235 #define REBUILD_PARITY 2
236 #define VERIFY         3
237 
238 /***********************************************************/
239 
240 static __inline struct cam_periph *
241 hpt_get_periph(int path_id,int target_id)
242 {
243 	struct cam_periph	*periph = NULL;
244     struct cam_path	*path;
245     int			status;
246 
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");
250 		xpt_free_path(path);
251 
252     }
253 	return periph;
254 }
255 
256 #if (__FreeBSD_version < 500000)
257 #define YIELD_THREAD yield(curproc, 0)
258 #endif
259 
260 #ifdef __i386__
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))
268 #endif
269 
270 #define INVALID_DEVICEID		(-1)
271 #define INVALID_STRIPSIZE		(-1)
272 
273 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
274 
275 #ifndef MinBlockSizeShift
276 #define MinBlockSizeShift 5
277 #define MaxBlockSizeShift 12
278 #endif
279 
280 #pragma pack(1)
281 typedef struct _HPT_IOCTL_TRANSFER_PARAM
282 {
283 	ULONG nInBufferSize;
284 	ULONG nOutBufferSize;
285 	UCHAR buffer[0];
286 }HPT_IOCTL_TRANSFER_PARAM, *PHPT_IOCTL_TRANSFER_PARAM;
287 
288 typedef struct _HPT_SET_STATE_PARAM
289 {
290 	DEVICEID idArray;
291 	DWORD state;
292 } HPT_SET_STATE_PARAM, *PHPT_SET_STATE_PARAM;
293 
294 typedef struct _HPT_SET_ARRAY_INFO
295 {
296 	DEVICEID idArray;
297 	ALTERABLE_ARRAY_INFO Info;
298 } HPT_SET_ARRAY_INFO, *PHPT_SET_ARRAY_INFO;
299 
300 typedef struct _HPT_SET_DEVICE_INFO
301 {
302 	DEVICEID idDisk;
303 	ALTERABLE_DEVICE_INFO Info;
304 } HPT_SET_DEVICE_INFO, *PHPT_SET_DEVICE_INFO;
305 
306 typedef struct _HPT_SET_DEVICE_INFO_V2
307 {
308 	DEVICEID idDisk;
309 	ALTERABLE_DEVICE_INFO_V2 Info;
310 } HPT_SET_DEVICE_INFO_V2, *PHPT_SET_DEVICE_INFO_V2;
311 
312 typedef struct _HPT_ADD_DISK_TO_ARRAY
313 {
314 	DEVICEID idArray;
315 	DEVICEID idDisk;
316 } HPT_ADD_DISK_TO_ARRAY, *PHPT_ADD_DISK_TO_ARRAY;
317 
318 typedef struct _HPT_DEVICE_IO
319 {
320 	DEVICEID	id;
321 	int			cmd;
322 	ULONG		lba;
323 	DWORD		nSector;
324 	UCHAR		buffer[0];
325 } HPT_DEVICE_IO, *PHPT_DEVICE_IO;
326 
327 int check_VDevice_valid(PVDevice);
328 int hpt_default_ioctl(_VBUS_ARG DWORD, PVOID, DWORD, PVOID, DWORD, PDWORD);
329 
330 #define HPT_NULL_ID 0
331 
332 #pragma pack()
333 
334 #endif
335