xref: /freebsd/sys/dev/aic7xxx/aic_osm_lib.h (revision c7d813a93eeb447470734c9bc0c140d90a54c271)
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$
36  */
37 
38 /******************************** OS Includes *********************************/
39 #if __FreeBSD_version >= 500000
40 #include <sys/mutex.h>
41 #endif
42 
43 /*************************** Library Symbol Mapping ***************************/
44 #define	AIC_LIB_ENTRY_CONCAT(x, prefix)	prefix ## x
45 #define	AIC_LIB_ENTRY_EXPAND(x, prefix)	AIC_LIB_ENTRY_CONCAT(x, prefix)
46 #define	AIC_LIB_ENTRY(x)		AIC_LIB_ENTRY_EXPAND(x, AIC_LIB_PREFIX)
47 #define	AIC_CONST_ENTRY(x)		AIC_LIB_ENTRY_EXPAND(x,AIC_CONST_PREFIX)
48 
49 #define	aic_softc			AIC_LIB_ENTRY(_softc)
50 #define	aic_tailq			AIC_LIB_ENTRY(_tailq)
51 #define	aic_transinfo			AIC_LIB_ENTRY(_transinfo)
52 #define	aic_platform_data		AIC_LIB_ENTRY(_platform_data)
53 #define	aic_devinfo			AIC_LIB_ENTRY(_devinfo)
54 #define	aic_lock			AIC_LIB_ENTRY(_lock)
55 #define	aic_unlock			AIC_LIB_ENTRY(_unlock)
56 #define	aic_callback_t			AIC_LIB_ENTRY(_callback_t)
57 #define	aic_platform_freeze_devq	AIC_LIB_ENTRY(_platform_freeze_devq)
58 #define	aic_platform_abort_scbs		AIC_LIB_ENTRY(_platform_abort_scbs)
59 #define	aic_platform_timeout		AIC_LIB_ENTRY(_platform_timeout)
60 #define	aic_timeout			AIC_LIB_ENTRY(_timeout)
61 #define	aic_set_recoveryscb		AIC_LIB_ENTRY(_set_recoveryscb)
62 #define	aic_spawn_recovery_thread	AIC_LIB_ENTRY(_spawn_recovery_thread)
63 #define	aic_wakeup_recovery_thread	AIC_LIB_ENTRY(_wakeup_recovery_thread)
64 #define	aic_terminate_recovery_thread \
65 	AIC_LIB_ENTRY(_terminate_recovery_thread)
66 #define	aic_recovery_thread		AIC_LIB_ENTRY(_recovery_thread)
67 #define	aic_recover_commands		AIC_LIB_ENTRY(_recover_commands)
68 #define	aic_calc_geometry		AIC_LIB_ENTRY(_calc_geometry)
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 #if __FreeBSD_version > 800001
130 #define	aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
131 	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
132 #else
133 #define	aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
134 	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
135 #endif
136 #else
137 #define	aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
138 	kthread_create(func, farg, proc_ptr, fmtstr, arg)
139 #endif
140 
141 /******************************* Bus Space/DMA ********************************/
142 
143 #if __FreeBSD_version >= 501102
144 #define aic_dma_tag_create(aic, parent_tag, alignment, boundary,	\
145 			   lowaddr, highaddr, filter, filterarg,	\
146 			   maxsize, nsegments, maxsegsz, flags,		\
147 			   dma_tagp)					\
148 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
149 			   lowaddr, highaddr, filter, filterarg,	\
150 			   maxsize, nsegments, maxsegsz, flags,		\
151 			   busdma_lock_mutex, &aic->platform_data->mtx,			\
152 			   dma_tagp)
153 #else
154 #define aic_dma_tag_create(aic, parent_tag, alignment, boundary,	\
155 			   lowaddr, highaddr, filter, filterarg,	\
156 			   maxsize, nsegments, maxsegsz, flags,		\
157 			   dma_tagp)					\
158 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
159 			   lowaddr, highaddr, filter, filterarg,	\
160 			   maxsize, nsegments, maxsegsz, flags,		\
161 			   dma_tagp)
162 #endif
163 
164 #define aic_dma_tag_destroy(aic, tag)					\
165 	bus_dma_tag_destroy(tag)
166 
167 #define aic_dmamem_alloc(aic, dmat, vaddr, flags, mapp)			\
168 	bus_dmamem_alloc(dmat, vaddr, flags, mapp)
169 
170 #define aic_dmamem_free(aic, dmat, vaddr, map)				\
171 	bus_dmamem_free(dmat, vaddr, map)
172 
173 #define aic_dmamap_create(aic, tag, flags, mapp)			\
174 	bus_dmamap_create(tag, flags, mapp)
175 
176 #define aic_dmamap_destroy(aic, tag, map)				\
177 	bus_dmamap_destroy(tag, map)
178 
179 #define aic_dmamap_load(aic, dmat, map, addr, buflen, callback,		\
180 			callback_arg, flags)				\
181 	bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
182 
183 #define aic_dmamap_unload(aic, tag, map)				\
184 	bus_dmamap_unload(tag, map)
185 
186 /* XXX Need to update Bus DMA for partial map syncs */
187 #define aic_dmamap_sync(aic, dma_tag, dmamap, offset, len, op)		\
188 	bus_dmamap_sync(dma_tag, dmamap, op)
189 
190 /***************************** Core Includes **********************************/
191 #include AIC_CORE_INCLUDE
192 
193 /***************************** Timer Facilities *******************************/
194 #if __FreeBSD_version >= 500000
195 #define aic_timer_init(timer) callout_init(timer, /*mpsafe*/1)
196 #else
197 #define aic_timer_init callout_init
198 #endif
199 #define aic_timer_stop callout_stop
200 
201 static __inline void aic_timer_reset(aic_timer_t *, u_int,
202 				     aic_callback_t *, void *);
203 static __inline u_int aic_get_timeout(struct scb *);
204 static __inline void aic_scb_timer_reset(struct scb *, u_int);
205 
206 static __inline void
207 aic_timer_reset(aic_timer_t *timer, u_int msec, aic_callback_t *func, void *arg)
208 {
209 	uint64_t time;
210 
211 	time = msec;
212 	time *= hz;
213 	time /= 1000;
214 	callout_reset(timer, time, func, arg);
215 }
216 
217 static __inline u_int
218 aic_get_timeout(struct scb *scb)
219 {
220 	return (scb->io_ctx->ccb_h.timeout);
221 }
222 
223 static __inline void
224 aic_scb_timer_reset(struct scb *scb, u_int msec)
225 {
226 	uint64_t time;
227 
228 	time = msec;
229 	time *= hz;
230 	time /= 1000;
231 	callout_reset(&scb->io_timer, time, aic_platform_timeout, scb);
232 }
233 
234 static __inline void
235 aic_scb_timer_start(struct scb *scb)
236 {
237 
238 	if (AIC_SCB_DATA(scb->aic_softc)->recovery_scbs == 0
239 	 && scb->io_ctx->ccb_h.timeout != CAM_TIME_INFINITY) {
240 		aic_scb_timer_reset(scb, scb->io_ctx->ccb_h.timeout);
241 	}
242 }
243 
244 /************************** Transaction Operations ****************************/
245 static __inline void aic_set_transaction_status(struct scb *, uint32_t);
246 static __inline void aic_set_scsi_status(struct scb *, uint32_t);
247 static __inline uint32_t aic_get_transaction_status(struct scb *);
248 static __inline uint32_t aic_get_scsi_status(struct scb *);
249 static __inline void aic_set_transaction_tag(struct scb *, int, u_int);
250 static __inline u_long aic_get_transfer_length(struct scb *);
251 static __inline int aic_get_transfer_dir(struct scb *);
252 static __inline void aic_set_residual(struct scb *, u_long);
253 static __inline void aic_set_sense_residual(struct scb *, u_long);
254 static __inline u_long aic_get_residual(struct scb *);
255 static __inline int aic_perform_autosense(struct scb *);
256 static __inline uint32_t aic_get_sense_bufsize(struct aic_softc*, struct scb*);
257 static __inline void aic_freeze_ccb(union ccb *ccb);
258 static __inline void aic_freeze_scb(struct scb *scb);
259 static __inline void aic_platform_freeze_devq(struct aic_softc *, struct scb *);
260 static __inline int  aic_platform_abort_scbs(struct aic_softc *aic, int target,
261 					     char channel, int lun, u_int tag,
262 					     role_t role, uint32_t status);
263 
264 static __inline
265 void aic_set_transaction_status(struct scb *scb, uint32_t status)
266 {
267 	scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
268 	scb->io_ctx->ccb_h.status |= status;
269 }
270 
271 static __inline
272 void aic_set_scsi_status(struct scb *scb, uint32_t status)
273 {
274 	scb->io_ctx->csio.scsi_status = status;
275 }
276 
277 static __inline
278 uint32_t aic_get_transaction_status(struct scb *scb)
279 {
280 	return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
281 }
282 
283 static __inline
284 uint32_t aic_get_scsi_status(struct scb *scb)
285 {
286 	return (scb->io_ctx->csio.scsi_status);
287 }
288 
289 static __inline
290 void aic_set_transaction_tag(struct scb *scb, int enabled, u_int type)
291 {
292 	scb->io_ctx->csio.tag_action = type;
293 	if (enabled)
294 		scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
295 	else
296 		scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
297 }
298 
299 static __inline
300 u_long aic_get_transfer_length(struct scb *scb)
301 {
302 	return (scb->io_ctx->csio.dxfer_len);
303 }
304 
305 static __inline
306 int aic_get_transfer_dir(struct scb *scb)
307 {
308 	return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
309 }
310 
311 static __inline
312 void aic_set_residual(struct scb *scb, u_long resid)
313 {
314 	scb->io_ctx->csio.resid = resid;
315 }
316 
317 static __inline
318 void aic_set_sense_residual(struct scb *scb, u_long resid)
319 {
320 	scb->io_ctx->csio.sense_resid = resid;
321 }
322 
323 static __inline
324 u_long aic_get_residual(struct scb *scb)
325 {
326 	return (scb->io_ctx->csio.resid);
327 }
328 
329 static __inline
330 int aic_perform_autosense(struct scb *scb)
331 {
332 	return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
333 }
334 
335 static __inline uint32_t
336 aic_get_sense_bufsize(struct aic_softc *aic, struct scb *scb)
337 {
338 	return (sizeof(struct scsi_sense_data));
339 }
340 
341 static __inline void
342 aic_freeze_ccb(union ccb *ccb)
343 {
344 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
345 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
346 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
347 	}
348 }
349 
350 static __inline void
351 aic_freeze_scb(struct scb *scb)
352 {
353 	aic_freeze_ccb(scb->io_ctx);
354 }
355 
356 static __inline void
357 aic_platform_freeze_devq(struct aic_softc *aic, struct scb *scb)
358 {
359 	/* Nothing to do here for FreeBSD */
360 }
361 
362 static __inline int
363 aic_platform_abort_scbs(struct aic_softc *aic, int target,
364 			char channel, int lun, u_int tag,
365 			role_t role, uint32_t status)
366 {
367 	/* Nothing to do here for FreeBSD */
368 	return (0);
369 }
370 
371 static __inline void
372 aic_platform_scb_free(struct aic_softc *aic, struct scb *scb)
373 {
374 	/* What do we do to generically handle driver resource shortages??? */
375 	if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0
376 	 && scb->io_ctx != NULL
377 	 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
378 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
379 		aic->flags &= ~AIC_RESOURCE_SHORTAGE;
380 	}
381 	scb->io_ctx = NULL;
382 }
383 
384 /*************************** CAM CCB Operations *******************************/
385 void aic_calc_geometry(struct ccb_calc_geometry *ccg, int extended);
386 
387 /****************************** OS Primitives *********************************/
388 #define aic_delay DELAY
389 
390 /********************************** PCI ***************************************/
391 #ifdef AIC_PCI_CONFIG
392 static __inline uint32_t aic_pci_read_config(aic_dev_softc_t pci,
393 					     int reg, int width);
394 static __inline void	 aic_pci_write_config(aic_dev_softc_t pci,
395 					      int reg, uint32_t value,
396 					      int width);
397 static __inline int	 aic_get_pci_function(aic_dev_softc_t);
398 static __inline int	 aic_get_pci_slot(aic_dev_softc_t);
399 static __inline int	 aic_get_pci_bus(aic_dev_softc_t);
400 
401 
402 static __inline uint32_t
403 aic_pci_read_config(aic_dev_softc_t pci, int reg, int width)
404 {
405 	return (pci_read_config(pci, reg, width));
406 }
407 
408 static __inline void
409 aic_pci_write_config(aic_dev_softc_t pci, int reg, uint32_t value, int width)
410 {
411 	pci_write_config(pci, reg, value, width);
412 }
413 
414 static __inline int
415 aic_get_pci_function(aic_dev_softc_t pci)
416 {
417 	return (pci_get_function(pci));
418 }
419 
420 static __inline int
421 aic_get_pci_slot(aic_dev_softc_t pci)
422 {
423 	return (pci_get_slot(pci));
424 }
425 
426 static __inline int
427 aic_get_pci_bus(aic_dev_softc_t pci)
428 {
429 	return (pci_get_bus(pci));
430 }
431 
432 typedef enum
433 {
434 	AIC_POWER_STATE_D0 = PCI_POWERSTATE_D0,
435 	AIC_POWER_STATE_D1 = PCI_POWERSTATE_D1,
436 	AIC_POWER_STATE_D2 = PCI_POWERSTATE_D2,
437 	AIC_POWER_STATE_D3 = PCI_POWERSTATE_D3
438 } aic_power_state;
439 
440 static __inline int aic_power_state_change(struct aic_softc *aic,
441 					   aic_power_state new_state);
442 
443 static __inline int
444 aic_power_state_change(struct aic_softc *aic, aic_power_state new_state)
445 {
446 	return (pci_set_powerstate(aic->dev_softc, new_state));
447 }
448 #endif
449