xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#18 $
33  *
34  * $FreeBSD$
35  */
36 
37 #ifndef _AIC7XXX_FREEBSD_H_
38 #define _AIC7XXX_FREEBSD_H_
39 
40 #include "opt_aic7xxx.h"	/* for config options */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>		/* For device_t */
45 #include <sys/endian.h>
46 #include <sys/eventhandler.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/queue.h>
51 
52 #define AIC_PCI_CONFIG 1
53 #include <machine/bus.h>
54 #include <machine/endian.h>
55 #include <machine/resource.h>
56 
57 #include <sys/rman.h>
58 
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 
62 #include <cam/cam.h>
63 #include <cam/cam_ccb.h>
64 #include <cam/cam_debug.h>
65 #include <cam/cam_sim.h>
66 #include <cam/cam_xpt_sim.h>
67 
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_message.h>
70 
71 /*************************** Attachment Bookkeeping ***************************/
72 extern devclass_t ahc_devclass;
73 
74 /****************************** Platform Macros *******************************/
75 #define	SIM_IS_SCSIBUS_B(ahc, sim)	\
76 	((sim) == ahc->platform_data->sim_b)
77 #define	SIM_CHANNEL(ahc, sim)	\
78 	(((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
79 #define	SIM_SCSI_ID(ahc, sim)	\
80 	(((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
81 #define	SIM_PATH(ahc, sim)	\
82 	(((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
83 					      : ahc->platform_data->path)
84 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
85         ((((target_id) << TID_SHIFT) & TID) | (our_id) \
86         | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
87 
88 #define SCB_GET_SIM(ahc, scb) \
89 	(SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
90 					  : (ahc)->platform_data->sim_b)
91 
92 #ifndef offsetof
93 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
94 #endif
95 
96 /************************ Tunable Driver Parameters  **************************/
97 /*
98  * The number of dma segments supported.  The sequencer can handle any number
99  * of physically contiguous S/G entrys.  To reduce the driver's memory
100  * consumption, we limit the number supported to be sufficient to handle
101  * the largest mapping supported by the legacy kernel MAXPHYS setting of
102  * 128K.  This can be increased once some testing is done.  Assuming the
103  * be the number of paged sized transfers in MAXPHYS plus an extra element
104  * to handle any unaligned residual.  The sequencer fetches SG elements
105  * in cacheline sized chucks, so make the number per-transaction an even
106  * multiple of 16 which should align us on even the largest of cacheline
107  * boundaries.
108  */
109 #define AHC_MAXPHYS (128 * 1024)
110 #define AHC_NSEG (roundup(btoc(AHC_MAXPHYS) + 1, 16))
111 
112 /* This driver supports target mode */
113 #define AHC_TARGET_MODE 1
114 
115 /************************** Softc/SCB Platform Data ***************************/
116 struct ahc_platform_data {
117 	/*
118 	 * Hooks into the XPT.
119 	 */
120 	struct	cam_sim		*sim;
121 	struct	cam_sim		*sim_b;
122 	struct	cam_path	*path;
123 	struct	cam_path	*path_b;
124 
125 	int			 regs_res_type;
126 	int			 regs_res_id;
127 	int			 irq_res_type;
128 	struct resource		*regs;
129 	struct resource		*irq;
130 	void			*ih;
131 	eventhandler_tag	 eh;
132 	struct proc		*recovery_thread;
133 	struct mtx		mtx;
134 };
135 
136 struct scb_platform_data {
137 };
138 
139 /***************************** Core Includes **********************************/
140 #ifdef AHC_REG_PRETTY_PRINT
141 #define AIC_DEBUG_REGISTERS 1
142 #else
143 #define AIC_DEBUG_REGISTERS 0
144 #endif
145 #define AIC_CORE_INCLUDE <dev/aic7xxx/aic7xxx.h>
146 #define	AIC_LIB_PREFIX ahc
147 #define	AIC_CONST_PREFIX AHC
148 #include <dev/aic7xxx/aic_osm_lib.h>
149 
150 /*************************** Device Access ************************************/
151 #define ahc_inb(ahc, port)				\
152 	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
153 
154 #define ahc_outb(ahc, port, value)			\
155 	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
156 
157 #define ahc_outsb(ahc, port, valp, count)		\
158 	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
159 
160 #define ahc_insb(ahc, port, valp, count)		\
161 	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
162 
163 static __inline void ahc_flush_device_writes(struct ahc_softc *);
164 
165 static __inline void
166 ahc_flush_device_writes(struct ahc_softc *ahc)
167 {
168 	/* XXX Is this sufficient for all architectures??? */
169 	ahc_inb(ahc, INTSTAT);
170 }
171 
172 /**************************** Locking Primitives ******************************/
173 /* Lock protecting internal data structures */
174 static __inline void ahc_lockinit(struct ahc_softc *);
175 static __inline void ahc_lock(struct ahc_softc *);
176 static __inline void ahc_unlock(struct ahc_softc *);
177 
178 static __inline void
179 ahc_lockinit(struct ahc_softc *ahc)
180 {
181 	mtx_init(&ahc->platform_data->mtx, "ahc_lock", NULL, MTX_DEF);
182 }
183 
184 static __inline void
185 ahc_lock(struct ahc_softc *ahc)
186 {
187 	mtx_lock(&ahc->platform_data->mtx);
188 }
189 
190 static __inline void
191 ahc_unlock(struct ahc_softc *ahc)
192 {
193 	mtx_unlock(&ahc->platform_data->mtx);
194 }
195 
196 /************************* Initialization/Teardown ****************************/
197 int	  ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
198 void	  ahc_platform_free(struct ahc_softc *ahc);
199 int	  ahc_map_int(struct ahc_softc *ahc);
200 int	  ahc_attach(struct ahc_softc *);
201 int	  ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
202 int	  ahc_detach(device_t);
203 
204 /********************************** PCI ***************************************/
205 #ifdef AIC_PCI_CONFIG
206 int ahc_pci_map_registers(struct ahc_softc *ahc);
207 #define ahc_pci_map_int ahc_map_int
208 #endif /*AIC_PCI_CONFIG*/
209 
210 /******************************** VL/EISA/ISA *********************************/
211 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
212 static __inline int aic7770_map_int(struct ahc_softc *, int);
213 
214 static __inline int
215 aic7770_map_int(struct ahc_softc *ahc, int irq)
216 {
217 	/*
218 	 * The IRQ is unused in the FreeBSD
219 	 * implementation since the ISA attachment
220 	 * registers the IRQ with newbus before
221 	 * the core is called.
222 	 */
223 	return ahc_map_int(ahc);
224 }
225 
226 /********************************* Debug **************************************/
227 static __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
228 static __inline void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
229 
230 static __inline void
231 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
232 {
233 	xpt_print_path(scb->io_ctx->ccb_h.path);
234 }
235 
236 static __inline void
237 ahc_platform_dump_card_state(struct ahc_softc *ahc)
238 {
239 	/* Nothing to do here for FreeBSD */
240 }
241 /**************************** Transfer Settings *******************************/
242 void	  ahc_notify_xfer_settings_change(struct ahc_softc *,
243 					  struct ahc_devinfo *);
244 void	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
245 				int /*enable*/);
246 
247 /****************************** Interrupts ************************************/
248 void			ahc_platform_intr(void *);
249 static __inline void	ahc_platform_flushwork(struct ahc_softc *ahc);
250 static __inline void
251 ahc_platform_flushwork(struct ahc_softc *ahc)
252 {
253 }
254 
255 /************************ Misc Function Declarations **************************/
256 void	  ahc_done(struct ahc_softc *ahc, struct scb *scb);
257 void	  ahc_send_async(struct ahc_softc *, char /*channel*/,
258 			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
259 #endif  /* _AIC7XXX_FREEBSD_H_ */
260