xref: /freebsd/sys/dev/hptmv/osbsd.h (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 #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 	eventhandler_tag	eh;
195 }
196 IAL_ADAPTER_T;
197 
198 extern IAL_ADAPTER_T *gIal_Adapter;
199 
200 /*entry.c*/
201 typedef void (*HPT_DPC)(IAL_ADAPTER_T *,void*,UCHAR);
202 
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);
208 
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 */
216 						);
217 
218 
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)
223 
224 #define DUPLICATE      0
225 #define INITIALIZE     1
226 #define REBUILD_PARITY 2
227 #define VERIFY         3
228 
229 /***********************************************************/
230 
231 static __inline struct cam_periph *
232 hpt_get_periph(int path_id,int target_id)
233 {
234 	struct cam_periph	*periph = NULL;
235     struct cam_path	*path;
236     int			status;
237 
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");
241 		xpt_free_path(path);
242 
243     }
244 	return periph;
245 }
246 
247 #ifdef __i386__
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))
255 #endif
256 
257 #define INVALID_DEVICEID		(-1)
258 #define INVALID_STRIPSIZE		(-1)
259 
260 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
261 
262 #ifndef MinBlockSizeShift
263 #define MinBlockSizeShift 5
264 #define MaxBlockSizeShift 12
265 #endif
266 
267 #pragma pack(1)
268 typedef struct _HPT_IOCTL_TRANSFER_PARAM
269 {
270 	ULONG nInBufferSize;
271 	ULONG nOutBufferSize;
272 	UCHAR buffer[0];
273 }HPT_IOCTL_TRANSFER_PARAM, *PHPT_IOCTL_TRANSFER_PARAM;
274 
275 typedef struct _HPT_SET_STATE_PARAM
276 {
277 	DEVICEID idArray;
278 	DWORD state;
279 } HPT_SET_STATE_PARAM, *PHPT_SET_STATE_PARAM;
280 
281 typedef struct _HPT_SET_ARRAY_INFO
282 {
283 	DEVICEID idArray;
284 	ALTERABLE_ARRAY_INFO Info;
285 } HPT_SET_ARRAY_INFO, *PHPT_SET_ARRAY_INFO;
286 
287 typedef struct _HPT_SET_DEVICE_INFO
288 {
289 	DEVICEID idDisk;
290 	ALTERABLE_DEVICE_INFO Info;
291 } HPT_SET_DEVICE_INFO, *PHPT_SET_DEVICE_INFO;
292 
293 typedef struct _HPT_SET_DEVICE_INFO_V2
294 {
295 	DEVICEID idDisk;
296 	ALTERABLE_DEVICE_INFO_V2 Info;
297 } HPT_SET_DEVICE_INFO_V2, *PHPT_SET_DEVICE_INFO_V2;
298 
299 typedef struct _HPT_ADD_DISK_TO_ARRAY
300 {
301 	DEVICEID idArray;
302 	DEVICEID idDisk;
303 } HPT_ADD_DISK_TO_ARRAY, *PHPT_ADD_DISK_TO_ARRAY;
304 
305 typedef struct _HPT_DEVICE_IO
306 {
307 	DEVICEID	id;
308 	int			cmd;
309 	ULONG		lba;
310 	DWORD		nSector;
311 	UCHAR		buffer[0];
312 } HPT_DEVICE_IO, *PHPT_DEVICE_IO;
313 
314 int check_VDevice_valid(PVDevice);
315 int hpt_default_ioctl(_VBUS_ARG DWORD, PVOID, DWORD, PVOID, DWORD, PDWORD);
316 
317 #define HPT_NULL_ID 0
318 
319 #pragma pack()
320 
321 #endif
322