xref: /freebsd/sys/dev/aic7xxx/aic79xx_osm.h (revision 6b806d21d144c25f4fad714e1c0cf780f5e27d7e)
1 /*-
2  * FreeBSD platform specific driver option settings, data structures,
3  * function declarations and includes.
4  *
5  * Copyright (c) 1994-2001 Justin T. Gibbs.
6  * Copyright (c) 2001-2002 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/aic79xx_osm.h#23 $
34  *
35  * $FreeBSD$
36  */
37 
38 #ifndef _AIC79XX_FREEBSD_H_
39 #define _AIC79XX_FREEBSD_H_
40 
41 #include <opt_aic79xx.h>	/* for config options */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>		/* For device_t */
46 #if __FreeBSD_version >= 500000
47 #include <sys/endian.h>
48 #endif
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/queue.h>
54 
55 #define AIC_PCI_CONFIG 1
56 #include <machine/bus_memio.h>
57 #include <machine/bus_pio.h>
58 #include <machine/bus.h>
59 #include <machine/endian.h>
60 #include <machine/clock.h>
61 #include <machine/resource.h>
62 
63 #include <sys/rman.h>
64 
65 #if __FreeBSD_version >= 500000
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
68 #else
69 #include <pci/pcireg.h>
70 #include <pci/pcivar.h>
71 #endif
72 
73 #include <cam/cam.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_debug.h>
76 #include <cam/cam_sim.h>
77 #include <cam/cam_xpt_sim.h>
78 
79 #include <cam/scsi/scsi_all.h>
80 #include <cam/scsi/scsi_message.h>
81 #include <cam/scsi/scsi_iu.h>
82 
83 #ifdef CAM_NEW_TRAN_CODE
84 #define AHD_NEW_TRAN_SETTINGS
85 #endif /* CAM_NEW_TRAN_CODE */
86 
87 /****************************** Platform Macros *******************************/
88 #define	SIM_IS_SCSIBUS_B(ahd, sim)	\
89 	(0)
90 #define	SIM_CHANNEL(ahd, sim)	\
91 	('A')
92 #define	SIM_SCSI_ID(ahd, sim)	\
93 	(ahd->our_id)
94 #define	SIM_PATH(ahd, sim)	\
95 	(ahd->platform_data->path)
96 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \
97         ((((target_id) << TID_SHIFT) & TID) | (our_id))
98 
99 
100 #define SCB_GET_SIM(ahd, scb) \
101 	((ahd)->platform_data->sim)
102 
103 #ifndef offsetof
104 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
105 #endif
106 
107 /************************ Tunable Driver Parameters  **************************/
108 /*
109  * The number of dma segments supported.  The sequencer can handle any number
110  * of physically contiguous S/G entrys.  To reduce the driver's memory
111  * consumption, we limit the number supported to be sufficient to handle
112  * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
113  * transfer is as fragmented as possible and unaligned, this turns out to
114  * be the number of paged sized transfers in MAXPHYS plus an extra element
115  * to handle any unaligned residual.  The sequencer fetches SG elements
116  * in cacheline sized chucks, so make the number per-transaction an even
117  * multiple of 16 which should align us on even the largest of cacheline
118  * boundaries.
119  */
120 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
121 
122 /* This driver supports target mode */
123 #if NOT_YET
124 #define AHD_TARGET_MODE 1
125 #endif
126 
127 /************************** Softc/SCB Platform Data ***************************/
128 struct ahd_platform_data {
129 	/*
130 	 * Hooks into the XPT.
131 	 */
132 	struct	cam_sim		*sim;
133 	struct	cam_path	*path;
134 
135 	int			 regs_res_type[2];
136 	int			 regs_res_id[2];
137 	int			 irq_res_type;
138 	struct resource		*regs[2];
139 	struct resource		*irq;
140 	void			*ih;
141 	eventhandler_tag	 eh;
142 	struct proc		*recovery_thread;
143 };
144 
145 struct scb_platform_data {
146 };
147 
148 /***************************** Core Includes **********************************/
149 #if AHD_REG_PRETTY_PRINT
150 #define AIC_DEBUG_REGISTERS 1
151 #else
152 #define AIC_DEBUG_REGISTERS 0
153 #endif
154 #define AIC_CORE_INCLUDE <dev/aic7xxx/aic79xx.h>
155 #define	AIC_LIB_PREFIX ahd
156 #define	AIC_CONST_PREFIX AHD
157 #include <dev/aic7xxx/aic_osm_lib.h>
158 
159 /*************************** Device Access ************************************/
160 #define ahd_inb(ahd, port)					\
161 	bus_space_read_1((ahd)->tags[(port) >> 8],		\
162 			 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
163 
164 #define ahd_outb(ahd, port, value)				\
165 	bus_space_write_1((ahd)->tags[(port) >> 8],		\
166 			  (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
167 
168 #define ahd_inw_atomic(ahd, port)				\
169 	aic_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8],	\
170 				     (ahd)->bshs[(port) >> 8], (port) & 0xFF))
171 
172 #define ahd_outw_atomic(ahd, port, value)			\
173 	bus_space_write_2((ahd)->tags[(port) >> 8],		\
174 			  (ahd)->bshs[(port) >> 8],		\
175 			  (port & 0xFF), aic_htole16(value))
176 
177 #define ahd_outsb(ahd, port, valp, count)			\
178 	bus_space_write_multi_1((ahd)->tags[(port) >> 8],	\
179 				(ahd)->bshs[(port) >> 8],	\
180 				(port & 0xFF), valp, count)
181 
182 #define ahd_insb(ahd, port, valp, count)			\
183 	bus_space_read_multi_1((ahd)->tags[(port) >> 8],	\
184 			       (ahd)->bshs[(port) >> 8],	\
185 			       (port & 0xFF), valp, count)
186 
187 static __inline void ahd_flush_device_writes(struct ahd_softc *);
188 
189 static __inline void
190 ahd_flush_device_writes(struct ahd_softc *ahd)
191 {
192 	/* XXX Is this sufficient for all architectures??? */
193 	ahd_inb(ahd, INTSTAT);
194 }
195 
196 /**************************** Locking Primitives ******************************/
197 /* Lock protecting internal data structures */
198 static __inline void ahd_lockinit(struct ahd_softc *);
199 static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
200 static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
201 
202 /* Lock held during command compeletion to the upper layer */
203 static __inline void ahd_done_lockinit(struct ahd_softc *);
204 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
205 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
206 
207 /* Lock held during ahd_list manipulation and ahd softc frees */
208 static __inline void ahd_list_lockinit(void);
209 static __inline void ahd_list_lock(unsigned long *flags);
210 static __inline void ahd_list_unlock(unsigned long *flags);
211 
212 static __inline void
213 ahd_lockinit(struct ahd_softc *ahd)
214 {
215 }
216 
217 static __inline void
218 ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
219 {
220 	*flags = splcam();
221 }
222 
223 static __inline void
224 ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
225 {
226 	splx(*flags);
227 }
228 
229 /* Lock held during command compeletion to the upper layer */
230 static __inline void
231 ahd_done_lockinit(struct ahd_softc *ahd)
232 {
233 }
234 
235 static __inline void
236 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
237 {
238 }
239 
240 static __inline void
241 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
242 {
243 }
244 
245 /* Lock held during ahd_list manipulation and ahd softc frees */
246 static __inline void
247 ahd_list_lockinit(void)
248 {
249 }
250 
251 static __inline void
252 ahd_list_lock(unsigned long *flags)
253 {
254 }
255 
256 static __inline void
257 ahd_list_unlock(unsigned long *flags)
258 {
259 }
260 
261 /********************************** PCI ***************************************/
262 int ahd_pci_map_registers(struct ahd_softc *ahd);
263 int ahd_pci_map_int(struct ahd_softc *ahd);
264 
265 /************************** Transaction Operations ****************************/
266 static __inline void aic_freeze_simq(struct aic_softc*);
267 static __inline void aic_release_simq(struct aic_softc*);
268 
269 static __inline void
270 aic_freeze_simq(struct aic_softc *aic)
271 {
272 	xpt_freeze_simq(aic->platform_data->sim, /*count*/1);
273 }
274 
275 static __inline void
276 aic_release_simq(struct aic_softc *aic)
277 {
278 	xpt_release_simq(aic->platform_data->sim, /*run queue*/TRUE);
279 }
280 /********************************* Debug **************************************/
281 static __inline void	ahd_print_path(struct ahd_softc *, struct scb *);
282 static __inline void	ahd_platform_dump_card_state(struct ahd_softc *ahd);
283 
284 static __inline void
285 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
286 {
287 	xpt_print_path(scb->io_ctx->ccb_h.path);
288 }
289 
290 static __inline void
291 ahd_platform_dump_card_state(struct ahd_softc *ahd)
292 {
293 	/* Nothing to do here for FreeBSD */
294 }
295 /**************************** Transfer Settings *******************************/
296 void	  ahd_notify_xfer_settings_change(struct ahd_softc *,
297 					  struct ahd_devinfo *);
298 void	  ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
299 				int /*enable*/);
300 
301 /************************* Initialization/Teardown ****************************/
302 int	  ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
303 void	  ahd_platform_free(struct ahd_softc *ahd);
304 int	  ahd_map_int(struct ahd_softc *ahd);
305 int	  ahd_attach(struct ahd_softc *);
306 int	  ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
307 int	  ahd_detach(device_t);
308 #define	ahd_platform_init(arg)
309 
310 
311 /****************************** Interrupts ************************************/
312 void			ahd_platform_intr(void *);
313 static __inline void	ahd_platform_flushwork(struct ahd_softc *ahd);
314 static __inline void
315 ahd_platform_flushwork(struct ahd_softc *ahd)
316 {
317 }
318 
319 /************************ Misc Function Declarations **************************/
320 void	  ahd_done(struct ahd_softc *ahd, struct scb *scb);
321 void	  ahd_send_async(struct ahd_softc *, char /*channel*/,
322 			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
323 #endif  /* _AIC79XX_FREEBSD_H_ */
324