Merge from vendor branch GDB:
[dragonfly.git] / sys / dev / disk / aic7xxx / aic_osm_lib.h
1 /*
2  * FreeBSD platform specific, shared driver option settings, data structures,
3  * function declarations and includes.
4  *
5  * Copyright (c) 1994-2001 Justin T. Gibbs.
6  * Copyright (c) 2001-2003 Adaptec Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU Public License ("GPL").
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic_osm_lib.h#5 $
34  *
35  * $FreeBSD: src/sys/dev/aic7xxx/aic_osm_lib.h,v 1.4 2004/11/18 20:22:31 gibbs Exp $
36  * $DragonFly: src/sys/dev/disk/aic7xxx/aic_osm_lib.h,v 1.4 2007/07/06 05:58:26 pavalos Exp $
37  */
38
39 /******************************** OS Includes *********************************/
40 #if __FreeBSD_version >= 500000
41 #include <sys/mutex.h>
42 #endif
43
44 /*************************** Library Symbol Mapping ***************************/
45 #define AIC_LIB_ENTRY_CONCAT(x, prefix) prefix ## x
46 #define AIC_LIB_ENTRY_EXPAND(x, prefix) AIC_LIB_ENTRY_CONCAT(x, prefix)
47 #define AIC_LIB_ENTRY(x)                AIC_LIB_ENTRY_EXPAND(x, AIC_LIB_PREFIX)
48 #define AIC_CONST_ENTRY(x)              AIC_LIB_ENTRY_EXPAND(x,AIC_CONST_PREFIX)
49
50 #define aic_softc                       AIC_LIB_ENTRY(_softc)
51 #define aic_tailq                       AIC_LIB_ENTRY(_tailq)
52 #define aic_transinfo                   AIC_LIB_ENTRY(_transinfo)
53 #define aic_platform_data               AIC_LIB_ENTRY(_platform_data)
54 #define aic_devinfo                     AIC_LIB_ENTRY(_devinfo)
55 #define aic_lock                        AIC_LIB_ENTRY(_lock)
56 #define aic_unlock                      AIC_LIB_ENTRY(_unlock)
57 #define aic_callback_t                  AIC_LIB_ENTRY(_callback_t)
58 #define aic_platform_freeze_devq        AIC_LIB_ENTRY(_platform_freeze_devq)
59 #define aic_platform_abort_scbs         AIC_LIB_ENTRY(_platform_abort_scbs)
60 #define aic_platform_timeout            AIC_LIB_ENTRY(_platform_timeout)
61 #define aic_timeout                     AIC_LIB_ENTRY(_timeout)
62 #define aic_set_recoveryscb             AIC_LIB_ENTRY(_set_recoveryscb)
63 #define aic_spawn_recovery_thread       AIC_LIB_ENTRY(_spawn_recovery_thread)
64 #define aic_wakeup_recovery_thread      AIC_LIB_ENTRY(_wakeup_recovery_thread)
65 #define aic_terminate_recovery_thread \
66         AIC_LIB_ENTRY(_terminate_recovery_thread)
67 #define aic_recovery_thread             AIC_LIB_ENTRY(_recovery_thread)
68 #define aic_recover_commands            AIC_LIB_ENTRY(_recover_commands)
69
70 #define AIC_RESOURCE_SHORTAGE           AIC_CONST_ENTRY(_RESOURCE_SHORTAGE)
71 #define AIC_SHUTDOWN_RECOVERY           AIC_CONST_ENTRY(_SHUTDOWN_RECOVERY)
72
73 /********************************* Byte Order *********************************/
74 #if __FreeBSD_version >= 500000
75 #define aic_htobe16(x) htobe16(x)
76 #define aic_htobe32(x) htobe32(x)
77 #define aic_htobe64(x) htobe64(x)
78 #define aic_htole16(x) htole16(x)
79 #define aic_htole32(x) htole32(x)
80 #define aic_htole64(x) htole64(x)
81
82 #define aic_be16toh(x) be16toh(x)
83 #define aic_be32toh(x) be32toh(x)
84 #define aic_be64toh(x) be64toh(x)
85 #define aic_le16toh(x) le16toh(x)
86 #define aic_le32toh(x) le32toh(x)
87 #define aic_le64toh(x) le64toh(x)
88 #else
89 #define aic_htobe16(x) (x)
90 #define aic_htobe32(x) (x)
91 #define aic_htobe64(x) (x)
92 #define aic_htole16(x) (x)
93 #define aic_htole32(x) (x)
94 #define aic_htole64(x) (x)
95
96 #define aic_be16toh(x) (x)
97 #define aic_be32toh(x) (x)
98 #define aic_be64toh(x) (x)
99 #define aic_le16toh(x) (x)
100 #define aic_le32toh(x) (x)
101 #define aic_le64toh(x) (x)
102 #endif
103
104 /************************* Forward Declarations *******************************/
105 typedef device_t aic_dev_softc_t;
106 typedef union ccb *aic_io_ctx_t;
107 struct aic_softc;
108 struct scb;
109
110 /*************************** Timer DataStructures *****************************/
111 typedef struct callout aic_timer_t;
112
113 /****************************** Error Recovery ********************************/
114 void            aic_set_recoveryscb(struct aic_softc *aic, struct scb *scb);
115 timeout_t       aic_platform_timeout;
116 int             aic_spawn_recovery_thread(struct aic_softc *aic);
117 void            aic_terminate_recovery_thread(struct aic_softc *aic);
118
119 static __inline void    aic_wakeup_recovery_thread(struct aic_softc *aic);
120
121 static __inline void
122 aic_wakeup_recovery_thread(struct aic_softc *aic)
123 {
124         wakeup(aic);
125 }
126
127 /****************************** Kernel Threads ********************************/
128 #if __FreeBSD_version > 500005
129 #define aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
130         kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
131 #else
132 #define aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
133         kthread_create(func, farg, proc_ptr, fmtstr, arg)
134 #endif
135
136 /******************************* Bus Space/DMA ********************************/
137
138 #if __FreeBSD_version >= 501102
139 #define aic_dma_tag_create(aic, parent_tag, alignment, boundary,        \
140                            lowaddr, highaddr, filter, filterarg,        \
141                            maxsize, nsegments, maxsegsz, flags,         \
142                            dma_tagp)                                    \
143         bus_dma_tag_create(parent_tag, alignment, boundary,             \
144                            lowaddr, highaddr, filter, filterarg,        \
145                            maxsize, nsegments, maxsegsz, flags,         \
146                            busdma_lock_mutex, &Giant,                   \
147                            dma_tagp)
148 #else
149 #define aic_dma_tag_create(aic, parent_tag, alignment, boundary,        \
150                            lowaddr, highaddr, filter, filterarg,        \
151                            maxsize, nsegments, maxsegsz, flags,         \
152                            dma_tagp)                                    \
153         bus_dma_tag_create(parent_tag, alignment, boundary,             \
154                            lowaddr, highaddr, filter, filterarg,        \
155                            maxsize, nsegments, maxsegsz, flags,         \
156                            dma_tagp)
157 #endif
158
159 #define aic_dma_tag_destroy(aic, tag)                                   \
160         bus_dma_tag_destroy(tag)
161
162 #define aic_dmamem_alloc(aic, dmat, vaddr, flags, mapp)                 \
163         bus_dmamem_alloc(dmat, vaddr, flags, mapp)
164
165 #define aic_dmamem_free(aic, dmat, vaddr, map)                          \
166         bus_dmamem_free(dmat, vaddr, map)
167
168 #define aic_dmamap_create(aic, tag, flags, mapp)                        \
169         bus_dmamap_create(tag, flags, mapp)
170
171 #define aic_dmamap_destroy(aic, tag, map)                               \
172         bus_dmamap_destroy(tag, map)
173
174 #define aic_dmamap_load(aic, dmat, map, addr, buflen, callback,         \
175                         callback_arg, flags)                            \
176         bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
177
178 #define aic_dmamap_unload(aic, tag, map)                                \
179         bus_dmamap_unload(tag, map)
180
181 /* XXX Need to update Bus DMA for partial map syncs */
182 #define aic_dmamap_sync(aic, dma_tag, dmamap, offset, len, op)          \
183         bus_dmamap_sync(dma_tag, dmamap, op)
184
185 /***************************** Core Includes **********************************/
186 #include AIC_CORE_INCLUDE
187
188 /***************************** Timer Facilities *******************************/
189 #if __FreeBSD_version >= 500000
190 #define aic_timer_init(timer) callout_init(timer, /*mpsafe*/0)
191 #else
192 #define aic_timer_init callout_init
193 #endif
194 #define aic_timer_stop callout_stop
195
196 static __inline void aic_timer_reset(aic_timer_t *, u_int,
197                                      aic_callback_t *, void *);
198 static __inline u_int aic_get_timeout(struct scb *);
199 static __inline void aic_scb_timer_reset(struct scb *, u_int);
200
201 static __inline void
202 aic_timer_reset(aic_timer_t *timer, u_int msec, aic_callback_t *func, void *arg)
203 {
204         uint64_t time;
205
206         time = msec;
207         time *= hz;
208         time /= 1000;
209         callout_reset(timer, time, func, arg);
210 }
211
212 static __inline u_int
213 aic_get_timeout(struct scb *scb)
214 {
215         return (scb->io_ctx->ccb_h.timeout);
216 }
217
218 static __inline void
219 aic_scb_timer_reset(struct scb *scb, u_int msec)
220 {
221         uint64_t time;
222
223         time = msec;
224         time *= hz;
225         time /= 1000;
226         callout_reset(&scb->io_ctx->ccb_h.timeout_ch, time,
227                 aic_platform_timeout, scb);
228 }
229
230 static __inline void
231 aic_scb_timer_start(struct scb *scb)
232 {
233         
234         if (AIC_SCB_DATA(scb->aic_softc)->recovery_scbs == 0
235          && scb->io_ctx->ccb_h.timeout != CAM_TIME_INFINITY) {
236                 uint64_t time;
237
238                 time = scb->io_ctx->ccb_h.timeout;
239                 time *= hz;
240                 time /= 1000;
241                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch, time,
242                     aic_platform_timeout, scb);
243         }
244 }
245
246 /************************** Transaction Operations ****************************/
247 static __inline void aic_set_transaction_status(struct scb *, uint32_t);
248 static __inline void aic_set_scsi_status(struct scb *, uint32_t);
249 static __inline uint32_t aic_get_transaction_status(struct scb *);
250 static __inline uint32_t aic_get_scsi_status(struct scb *);
251 static __inline void aic_set_transaction_tag(struct scb *, int, u_int);
252 static __inline u_long aic_get_transfer_length(struct scb *);
253 static __inline int aic_get_transfer_dir(struct scb *);
254 static __inline void aic_set_residual(struct scb *, u_long);
255 static __inline void aic_set_sense_residual(struct scb *, u_long);
256 static __inline u_long aic_get_residual(struct scb *);
257 static __inline int aic_perform_autosense(struct scb *);
258 static __inline uint32_t aic_get_sense_bufsize(struct aic_softc*, struct scb*);
259 static __inline void aic_freeze_ccb(union ccb *ccb);
260 static __inline void aic_freeze_scb(struct scb *scb);
261 static __inline void aic_platform_freeze_devq(struct aic_softc *, struct scb *);
262 static __inline int  aic_platform_abort_scbs(struct aic_softc *aic, int target,
263                                              char channel, int lun, u_int tag,
264                                              role_t role, uint32_t status);
265
266 static __inline
267 void aic_set_transaction_status(struct scb *scb, uint32_t status)
268 {
269         scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
270         scb->io_ctx->ccb_h.status |= status;
271 }
272
273 static __inline
274 void aic_set_scsi_status(struct scb *scb, uint32_t status)
275 {
276         scb->io_ctx->csio.scsi_status = status;
277 }
278
279 static __inline
280 uint32_t aic_get_transaction_status(struct scb *scb)
281 {
282         return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
283 }
284
285 static __inline
286 uint32_t aic_get_scsi_status(struct scb *scb)
287 {
288         return (scb->io_ctx->csio.scsi_status);
289 }
290
291 static __inline
292 void aic_set_transaction_tag(struct scb *scb, int enabled, u_int type)
293 {
294         scb->io_ctx->csio.tag_action = type;
295         if (enabled)
296                 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
297         else
298                 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
299 }
300
301 static __inline
302 u_long aic_get_transfer_length(struct scb *scb)
303 {
304         return (scb->io_ctx->csio.dxfer_len);
305 }
306
307 static __inline
308 int aic_get_transfer_dir(struct scb *scb)
309 {
310         return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
311 }
312
313 static __inline
314 void aic_set_residual(struct scb *scb, u_long resid)
315 {
316         scb->io_ctx->csio.resid = resid;
317 }
318
319 static __inline
320 void aic_set_sense_residual(struct scb *scb, u_long resid)
321 {
322         scb->io_ctx->csio.sense_resid = resid;
323 }
324
325 static __inline
326 u_long aic_get_residual(struct scb *scb)
327 {
328         return (scb->io_ctx->csio.resid);
329 }
330
331 static __inline
332 int aic_perform_autosense(struct scb *scb)
333 {
334         return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
335 }
336
337 static __inline uint32_t
338 aic_get_sense_bufsize(struct aic_softc *aic, struct scb *scb)
339 {
340         return (sizeof(struct scsi_sense_data));
341 }
342
343 static __inline void
344 aic_freeze_ccb(union ccb *ccb)
345 {
346         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
347                 ccb->ccb_h.status |= CAM_DEV_QFRZN;
348                 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
349         }
350 }
351
352 static __inline void
353 aic_freeze_scb(struct scb *scb)
354 {
355         aic_freeze_ccb(scb->io_ctx);
356 }
357
358 static __inline void
359 aic_platform_freeze_devq(struct aic_softc *aic, struct scb *scb)
360 {
361         /* Nothing to do here for FreeBSD */
362 }
363
364 static __inline int
365 aic_platform_abort_scbs(struct aic_softc *aic, int target,
366                         char channel, int lun, u_int tag,
367                         role_t role, uint32_t status)
368 {
369         /* Nothing to do here for FreeBSD */
370         return (0);
371 }
372
373 static __inline void
374 aic_platform_scb_free(struct aic_softc *aic, struct scb *scb)
375 {
376         /* What do we do to generically handle driver resource shortages??? */
377         if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0
378          && scb->io_ctx != NULL
379          && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
380                 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
381                 aic->flags &= ~AIC_RESOURCE_SHORTAGE;
382         }
383         scb->io_ctx = NULL;
384 }
385
386 /****************************** OS Primitives *********************************/
387 #define aic_delay DELAY
388
389 /********************************** PCI ***************************************/
390 #ifdef AIC_PCI_CONFIG
391 static __inline uint32_t aic_pci_read_config(aic_dev_softc_t pci,
392                                              int reg, int width);
393 static __inline void     aic_pci_write_config(aic_dev_softc_t pci,
394                                               int reg, uint32_t value,
395                                               int width);
396 static __inline int      aic_get_pci_function(aic_dev_softc_t);
397 static __inline int      aic_get_pci_slot(aic_dev_softc_t);
398 static __inline int      aic_get_pci_bus(aic_dev_softc_t);
399
400
401 static __inline uint32_t
402 aic_pci_read_config(aic_dev_softc_t pci, int reg, int width)
403 {
404         return (pci_read_config(pci, reg, width));
405 }
406
407 static __inline void
408 aic_pci_write_config(aic_dev_softc_t pci, int reg, uint32_t value, int width)
409 {
410         pci_write_config(pci, reg, value, width);
411 }
412
413 static __inline int
414 aic_get_pci_function(aic_dev_softc_t pci)
415 {
416         return (pci_get_function(pci));
417 }
418
419 static __inline int
420 aic_get_pci_slot(aic_dev_softc_t pci)
421 {
422         return (pci_get_slot(pci));
423 }
424
425 static __inline int
426 aic_get_pci_bus(aic_dev_softc_t pci)
427 {
428         return (pci_get_bus(pci));
429 }
430
431 typedef enum
432 {
433         AIC_POWER_STATE_D0 = PCI_POWERSTATE_D0,
434         AIC_POWER_STATE_D1 = PCI_POWERSTATE_D1,
435         AIC_POWER_STATE_D2 = PCI_POWERSTATE_D2,
436         AIC_POWER_STATE_D3 = PCI_POWERSTATE_D3
437 } aic_power_state;
438
439 static __inline int aic_power_state_change(struct aic_softc *aic,
440                                            aic_power_state new_state);
441
442 static __inline int
443 aic_power_state_change(struct aic_softc *aic, aic_power_state new_state)
444 {
445         return (pci_set_powerstate(aic->dev_softc, new_state));
446 }
447 #endif