MPSAFE locking for the ahc/ahd drivers using lockmgr locks.
[dragonfly.git] / sys / dev / disk / aic7xxx / aic_osm_lib.h
CommitLineData
750f3593
PA
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 *
263a6b5f 33 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic_osm_lib.h#5 $
750f3593 34 *
ce48b5fa 35 * $FreeBSD: src/sys/dev/aic7xxx/aic_osm_lib.h,v 1.4 2004/11/18 20:22:31 gibbs Exp $
ef8ef949 36 * $DragonFly: src/sys/dev/disk/aic7xxx/aic_osm_lib.h,v 1.5 2008/02/09 18:13:13 pavalos Exp $
750f3593
PA
37 */
38
39/******************************** OS Includes *********************************/
40#if __FreeBSD_version >= 500000
41#include <sys/mutex.h>
ef8ef949
PA
42#else
43#include <sys/lock.h>
750f3593
PA
44#endif
45
46/*************************** Library Symbol Mapping ***************************/
47#define AIC_LIB_ENTRY_CONCAT(x, prefix) prefix ## x
48#define AIC_LIB_ENTRY_EXPAND(x, prefix) AIC_LIB_ENTRY_CONCAT(x, prefix)
49#define AIC_LIB_ENTRY(x) AIC_LIB_ENTRY_EXPAND(x, AIC_LIB_PREFIX)
50#define AIC_CONST_ENTRY(x) AIC_LIB_ENTRY_EXPAND(x,AIC_CONST_PREFIX)
51
52#define aic_softc AIC_LIB_ENTRY(_softc)
53#define aic_tailq AIC_LIB_ENTRY(_tailq)
54#define aic_transinfo AIC_LIB_ENTRY(_transinfo)
55#define aic_platform_data AIC_LIB_ENTRY(_platform_data)
56#define aic_devinfo AIC_LIB_ENTRY(_devinfo)
57#define aic_lock AIC_LIB_ENTRY(_lock)
58#define aic_unlock AIC_LIB_ENTRY(_unlock)
59#define aic_callback_t AIC_LIB_ENTRY(_callback_t)
60#define aic_platform_freeze_devq AIC_LIB_ENTRY(_platform_freeze_devq)
61#define aic_platform_abort_scbs AIC_LIB_ENTRY(_platform_abort_scbs)
62#define aic_platform_timeout AIC_LIB_ENTRY(_platform_timeout)
63#define aic_timeout AIC_LIB_ENTRY(_timeout)
64#define aic_set_recoveryscb AIC_LIB_ENTRY(_set_recoveryscb)
65#define aic_spawn_recovery_thread AIC_LIB_ENTRY(_spawn_recovery_thread)
66#define aic_wakeup_recovery_thread AIC_LIB_ENTRY(_wakeup_recovery_thread)
67#define aic_terminate_recovery_thread \
68 AIC_LIB_ENTRY(_terminate_recovery_thread)
69#define aic_recovery_thread AIC_LIB_ENTRY(_recovery_thread)
70#define aic_recover_commands AIC_LIB_ENTRY(_recover_commands)
71
72#define AIC_RESOURCE_SHORTAGE AIC_CONST_ENTRY(_RESOURCE_SHORTAGE)
73#define AIC_SHUTDOWN_RECOVERY AIC_CONST_ENTRY(_SHUTDOWN_RECOVERY)
74
75/********************************* Byte Order *********************************/
76#if __FreeBSD_version >= 500000
77#define aic_htobe16(x) htobe16(x)
78#define aic_htobe32(x) htobe32(x)
79#define aic_htobe64(x) htobe64(x)
80#define aic_htole16(x) htole16(x)
81#define aic_htole32(x) htole32(x)
82#define aic_htole64(x) htole64(x)
83
84#define aic_be16toh(x) be16toh(x)
85#define aic_be32toh(x) be32toh(x)
86#define aic_be64toh(x) be64toh(x)
87#define aic_le16toh(x) le16toh(x)
88#define aic_le32toh(x) le32toh(x)
89#define aic_le64toh(x) le64toh(x)
90#else
91#define aic_htobe16(x) (x)
92#define aic_htobe32(x) (x)
93#define aic_htobe64(x) (x)
94#define aic_htole16(x) (x)
95#define aic_htole32(x) (x)
96#define aic_htole64(x) (x)
97
98#define aic_be16toh(x) (x)
99#define aic_be32toh(x) (x)
100#define aic_be64toh(x) (x)
101#define aic_le16toh(x) (x)
102#define aic_le32toh(x) (x)
103#define aic_le64toh(x) (x)
104#endif
105
106/************************* Forward Declarations *******************************/
107typedef device_t aic_dev_softc_t;
108typedef union ccb *aic_io_ctx_t;
109struct aic_softc;
110struct scb;
111
112/*************************** Timer DataStructures *****************************/
113typedef struct callout aic_timer_t;
114
115/****************************** Error Recovery ********************************/
116void aic_set_recoveryscb(struct aic_softc *aic, struct scb *scb);
117timeout_t aic_platform_timeout;
118int aic_spawn_recovery_thread(struct aic_softc *aic);
119void aic_terminate_recovery_thread(struct aic_softc *aic);
120
121static __inline void aic_wakeup_recovery_thread(struct aic_softc *aic);
122
123static __inline void
124aic_wakeup_recovery_thread(struct aic_softc *aic)
125{
126 wakeup(aic);
127}
128
129/****************************** Kernel Threads ********************************/
130#if __FreeBSD_version > 500005
131#define aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
132 kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
133#else
134#define aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
135 kthread_create(func, farg, proc_ptr, fmtstr, arg)
136#endif
137
138/******************************* Bus Space/DMA ********************************/
139
140#if __FreeBSD_version >= 501102
141#define aic_dma_tag_create(aic, parent_tag, alignment, boundary, \
142 lowaddr, highaddr, filter, filterarg, \
143 maxsize, nsegments, maxsegsz, flags, \
144 dma_tagp) \
145 bus_dma_tag_create(parent_tag, alignment, boundary, \
146 lowaddr, highaddr, filter, filterarg, \
147 maxsize, nsegments, maxsegsz, flags, \
148 busdma_lock_mutex, &Giant, \
149 dma_tagp)
150#else
151#define aic_dma_tag_create(aic, parent_tag, alignment, boundary, \
152 lowaddr, highaddr, filter, filterarg, \
153 maxsize, nsegments, maxsegsz, flags, \
154 dma_tagp) \
155 bus_dma_tag_create(parent_tag, alignment, boundary, \
156 lowaddr, highaddr, filter, filterarg, \
157 maxsize, nsegments, maxsegsz, flags, \
158 dma_tagp)
159#endif
160
161#define aic_dma_tag_destroy(aic, tag) \
162 bus_dma_tag_destroy(tag)
163
164#define aic_dmamem_alloc(aic, dmat, vaddr, flags, mapp) \
165 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
166
167#define aic_dmamem_free(aic, dmat, vaddr, map) \
168 bus_dmamem_free(dmat, vaddr, map)
169
170#define aic_dmamap_create(aic, tag, flags, mapp) \
171 bus_dmamap_create(tag, flags, mapp)
172
173#define aic_dmamap_destroy(aic, tag, map) \
174 bus_dmamap_destroy(tag, map)
175
176#define aic_dmamap_load(aic, dmat, map, addr, buflen, callback, \
177 callback_arg, flags) \
178 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
179
180#define aic_dmamap_unload(aic, tag, map) \
181 bus_dmamap_unload(tag, map)
182
183/* XXX Need to update Bus DMA for partial map syncs */
184#define aic_dmamap_sync(aic, dma_tag, dmamap, offset, len, op) \
185 bus_dmamap_sync(dma_tag, dmamap, op)
186
187/***************************** Core Includes **********************************/
188#include AIC_CORE_INCLUDE
189
190/***************************** Timer Facilities *******************************/
191#if __FreeBSD_version >= 500000
192#define aic_timer_init(timer) callout_init(timer, /*mpsafe*/0)
193#else
194#define aic_timer_init callout_init
195#endif
196#define aic_timer_stop callout_stop
197
198static __inline void aic_timer_reset(aic_timer_t *, u_int,
199 aic_callback_t *, void *);
200static __inline u_int aic_get_timeout(struct scb *);
201static __inline void aic_scb_timer_reset(struct scb *, u_int);
202
203static __inline void
ce48b5fa 204aic_timer_reset(aic_timer_t *timer, u_int msec, aic_callback_t *func, void *arg)
750f3593 205{
ce48b5fa
PA
206 uint64_t time;
207
208 time = msec;
209 time *= hz;
210 time /= 1000;
211 callout_reset(timer, time, func, arg);
750f3593
PA
212}
213
214static __inline u_int
215aic_get_timeout(struct scb *scb)
216{
ce48b5fa 217 return (scb->io_ctx->ccb_h.timeout);
750f3593
PA
218}
219
220static __inline void
ce48b5fa 221aic_scb_timer_reset(struct scb *scb, u_int msec)
750f3593 222{
ce48b5fa
PA
223 uint64_t time;
224
225 time = msec;
226 time *= hz;
227 time /= 1000;
ef8ef949 228 callout_reset(&scb->io_timer, time, aic_platform_timeout, scb);
750f3593
PA
229}
230
263a6b5f
PA
231static __inline void
232aic_scb_timer_start(struct scb *scb)
233{
234
dff3fb2d
PA
235 if (AIC_SCB_DATA(scb->aic_softc)->recovery_scbs == 0
236 && scb->io_ctx->ccb_h.timeout != CAM_TIME_INFINITY) {
ef8ef949 237 aic_scb_timer_reset(scb, scb->io_ctx->ccb_h.timeout);
263a6b5f
PA
238 }
239}
240
750f3593
PA
241/************************** Transaction Operations ****************************/
242static __inline void aic_set_transaction_status(struct scb *, uint32_t);
243static __inline void aic_set_scsi_status(struct scb *, uint32_t);
244static __inline uint32_t aic_get_transaction_status(struct scb *);
245static __inline uint32_t aic_get_scsi_status(struct scb *);
246static __inline void aic_set_transaction_tag(struct scb *, int, u_int);
247static __inline u_long aic_get_transfer_length(struct scb *);
248static __inline int aic_get_transfer_dir(struct scb *);
249static __inline void aic_set_residual(struct scb *, u_long);
250static __inline void aic_set_sense_residual(struct scb *, u_long);
251static __inline u_long aic_get_residual(struct scb *);
252static __inline int aic_perform_autosense(struct scb *);
253static __inline uint32_t aic_get_sense_bufsize(struct aic_softc*, struct scb*);
254static __inline void aic_freeze_ccb(union ccb *ccb);
255static __inline void aic_freeze_scb(struct scb *scb);
256static __inline void aic_platform_freeze_devq(struct aic_softc *, struct scb *);
257static __inline int aic_platform_abort_scbs(struct aic_softc *aic, int target,
258 char channel, int lun, u_int tag,
259 role_t role, uint32_t status);
260
261static __inline
262void aic_set_transaction_status(struct scb *scb, uint32_t status)
263{
264 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
265 scb->io_ctx->ccb_h.status |= status;
266}
267
268static __inline
269void aic_set_scsi_status(struct scb *scb, uint32_t status)
270{
271 scb->io_ctx->csio.scsi_status = status;
272}
273
274static __inline
275uint32_t aic_get_transaction_status(struct scb *scb)
276{
277 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
278}
279
280static __inline
281uint32_t aic_get_scsi_status(struct scb *scb)
282{
283 return (scb->io_ctx->csio.scsi_status);
284}
285
286static __inline
287void aic_set_transaction_tag(struct scb *scb, int enabled, u_int type)
288{
289 scb->io_ctx->csio.tag_action = type;
290 if (enabled)
291 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
292 else
293 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
294}
295
296static __inline
297u_long aic_get_transfer_length(struct scb *scb)
298{
299 return (scb->io_ctx->csio.dxfer_len);
300}
301
302static __inline
303int aic_get_transfer_dir(struct scb *scb)
304{
305 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
306}
307
308static __inline
309void aic_set_residual(struct scb *scb, u_long resid)
310{
311 scb->io_ctx->csio.resid = resid;
312}
313
314static __inline
315void aic_set_sense_residual(struct scb *scb, u_long resid)
316{
317 scb->io_ctx->csio.sense_resid = resid;
318}
319
320static __inline
321u_long aic_get_residual(struct scb *scb)
322{
323 return (scb->io_ctx->csio.resid);
324}
325
326static __inline
327int aic_perform_autosense(struct scb *scb)
328{
329 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
330}
331
332static __inline uint32_t
333aic_get_sense_bufsize(struct aic_softc *aic, struct scb *scb)
334{
335 return (sizeof(struct scsi_sense_data));
336}
337
338static __inline void
339aic_freeze_ccb(union ccb *ccb)
340{
341 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
342 ccb->ccb_h.status |= CAM_DEV_QFRZN;
343 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
344 }
345}
346
347static __inline void
348aic_freeze_scb(struct scb *scb)
349{
350 aic_freeze_ccb(scb->io_ctx);
351}
352
353static __inline void
354aic_platform_freeze_devq(struct aic_softc *aic, struct scb *scb)
355{
356 /* Nothing to do here for FreeBSD */
357}
358
359static __inline int
360aic_platform_abort_scbs(struct aic_softc *aic, int target,
361 char channel, int lun, u_int tag,
362 role_t role, uint32_t status)
363{
364 /* Nothing to do here for FreeBSD */
365 return (0);
366}
367
368static __inline void
369aic_platform_scb_free(struct aic_softc *aic, struct scb *scb)
370{
371 /* What do we do to generically handle driver resource shortages??? */
372 if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0
373 && scb->io_ctx != NULL
374 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
375 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
376 aic->flags &= ~AIC_RESOURCE_SHORTAGE;
377 }
378 scb->io_ctx = NULL;
379}
380
381/****************************** OS Primitives *********************************/
382#define aic_delay DELAY
383
384/********************************** PCI ***************************************/
385#ifdef AIC_PCI_CONFIG
386static __inline uint32_t aic_pci_read_config(aic_dev_softc_t pci,
387 int reg, int width);
388static __inline void aic_pci_write_config(aic_dev_softc_t pci,
389 int reg, uint32_t value,
390 int width);
391static __inline int aic_get_pci_function(aic_dev_softc_t);
392static __inline int aic_get_pci_slot(aic_dev_softc_t);
393static __inline int aic_get_pci_bus(aic_dev_softc_t);
394
395
396static __inline uint32_t
397aic_pci_read_config(aic_dev_softc_t pci, int reg, int width)
398{
399 return (pci_read_config(pci, reg, width));
400}
401
402static __inline void
403aic_pci_write_config(aic_dev_softc_t pci, int reg, uint32_t value, int width)
404{
405 pci_write_config(pci, reg, value, width);
406}
407
408static __inline int
409aic_get_pci_function(aic_dev_softc_t pci)
410{
411 return (pci_get_function(pci));
412}
413
414static __inline int
415aic_get_pci_slot(aic_dev_softc_t pci)
416{
417 return (pci_get_slot(pci));
418}
419
420static __inline int
421aic_get_pci_bus(aic_dev_softc_t pci)
422{
423 return (pci_get_bus(pci));
424}
425
426typedef enum
427{
428 AIC_POWER_STATE_D0 = PCI_POWERSTATE_D0,
429 AIC_POWER_STATE_D1 = PCI_POWERSTATE_D1,
430 AIC_POWER_STATE_D2 = PCI_POWERSTATE_D2,
431 AIC_POWER_STATE_D3 = PCI_POWERSTATE_D3
432} aic_power_state;
433
434static __inline int aic_power_state_change(struct aic_softc *aic,
435 aic_power_state new_state);
436
437static __inline int
438aic_power_state_change(struct aic_softc *aic, aic_power_state new_state)
439{
440 return (pci_set_powerstate(aic->dev_softc, new_state));
441}
442#endif