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