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