xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 
35 #ifndef _AIC7XXX_FREEBSD_H_
36 #define _AIC7XXX_FREEBSD_H_
37 
38 #include "opt_aic7xxx.h"	/* for config options */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>		/* For device_t */
43 #include <sys/endian.h>
44 #include <sys/eventhandler.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
49 
50 #define AIC_PCI_CONFIG 1
51 #include <machine/bus.h>
52 #include <machine/endian.h>
53 #include <machine/resource.h>
54 
55 #include <sys/rman.h>
56 
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59 
60 #include <cam/cam.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_xpt_sim.h>
65 
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68 
69 /****************************** Platform Macros *******************************/
70 #define	SIM_IS_SCSIBUS_B(ahc, sim)	\
71 	((sim) == ahc->platform_data->sim_b)
72 #define	SIM_CHANNEL(ahc, sim)	\
73 	(((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
74 #define	SIM_SCSI_ID(ahc, sim)	\
75 	(((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
76 #define	SIM_PATH(ahc, sim)	\
77 	(((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
78 					      : ahc->platform_data->path)
79 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
80         ((((target_id) << TID_SHIFT) & TID) | (our_id) \
81         | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
82 
83 #define SCB_GET_SIM(ahc, scb) \
84 	(SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
85 					  : (ahc)->platform_data->sim_b)
86 
87 #ifndef offsetof
88 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
89 #endif
90 
91 /************************ Tunable Driver Parameters  **************************/
92 /*
93  * The number of dma segments supported.  The sequencer can handle any number
94  * of physically contiguous S/G entrys.  To reduce the driver's memory
95  * consumption, we limit the number supported to be sufficient to handle
96  * the largest mapping supported by the legacy kernel MAXPHYS setting of
97  * 128K.  This can be increased once some testing is done.  Assuming the
98  * be the number of paged sized transfers in MAXPHYS plus an extra element
99  * to handle any unaligned residual.  The sequencer fetches SG elements
100  * in cacheline sized chucks, so make the number per-transaction an even
101  * multiple of 16 which should align us on even the largest of cacheline
102  * boundaries.
103  */
104 #define AHC_MAXPHYS (128 * 1024)
105 #define AHC_NSEG (roundup(btoc(AHC_MAXPHYS) + 1, 16))
106 
107 /* This driver supports target mode */
108 #define AHC_TARGET_MODE 1
109 
110 /************************** Softc/SCB Platform Data ***************************/
111 struct ahc_platform_data {
112 	/*
113 	 * Hooks into the XPT.
114 	 */
115 	struct	cam_sim		*sim;
116 	struct	cam_sim		*sim_b;
117 	struct	cam_path	*path;
118 	struct	cam_path	*path_b;
119 
120 	int			 regs_res_type;
121 	int			 regs_res_id;
122 	int			 irq_res_type;
123 	struct resource		*regs;
124 	struct resource		*irq;
125 	void			*ih;
126 	eventhandler_tag	 eh;
127 	struct proc		*recovery_thread;
128 	struct mtx		mtx;
129 };
130 
131 struct scb_platform_data {
132 };
133 
134 /***************************** Core Includes **********************************/
135 #ifdef AHC_REG_PRETTY_PRINT
136 #define AIC_DEBUG_REGISTERS 1
137 #else
138 #define AIC_DEBUG_REGISTERS 0
139 #endif
140 #define AIC_CORE_INCLUDE <dev/aic7xxx/aic7xxx.h>
141 #define	AIC_LIB_PREFIX ahc
142 #define	AIC_CONST_PREFIX AHC
143 #include <dev/aic7xxx/aic_osm_lib.h>
144 
145 /*************************** Device Access ************************************/
146 #define ahc_inb(ahc, port)				\
147 	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
148 
149 #define ahc_outb(ahc, port, value)			\
150 	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
151 
152 #define ahc_outsb(ahc, port, valp, count)		\
153 	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
154 
155 #define ahc_insb(ahc, port, valp, count)		\
156 	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
157 
158 static __inline void ahc_flush_device_writes(struct ahc_softc *);
159 
160 static __inline void
161 ahc_flush_device_writes(struct ahc_softc *ahc)
162 {
163 	/* XXX Is this sufficient for all architectures??? */
164 	ahc_inb(ahc, INTSTAT);
165 }
166 
167 /**************************** Locking Primitives ******************************/
168 /* Lock protecting internal data structures */
169 static __inline void ahc_lockinit(struct ahc_softc *);
170 static __inline void ahc_lock(struct ahc_softc *);
171 static __inline void ahc_unlock(struct ahc_softc *);
172 
173 static __inline void
174 ahc_lockinit(struct ahc_softc *ahc)
175 {
176 	mtx_init(&ahc->platform_data->mtx, "ahc_lock", NULL, MTX_DEF);
177 }
178 
179 static __inline void
180 ahc_lock(struct ahc_softc *ahc)
181 {
182 	mtx_lock(&ahc->platform_data->mtx);
183 }
184 
185 static __inline void
186 ahc_unlock(struct ahc_softc *ahc)
187 {
188 	mtx_unlock(&ahc->platform_data->mtx);
189 }
190 
191 /************************* Initialization/Teardown ****************************/
192 int	  ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
193 void	  ahc_platform_free(struct ahc_softc *ahc);
194 int	  ahc_map_int(struct ahc_softc *ahc);
195 int	  ahc_attach(struct ahc_softc *);
196 int	  ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
197 int	  ahc_detach(device_t);
198 
199 /********************************** PCI ***************************************/
200 #ifdef AIC_PCI_CONFIG
201 int ahc_pci_map_registers(struct ahc_softc *ahc);
202 #define ahc_pci_map_int ahc_map_int
203 #endif /*AIC_PCI_CONFIG*/
204 
205 /******************************** VL/EISA/ISA *********************************/
206 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
207 static __inline int aic7770_map_int(struct ahc_softc *, int);
208 
209 static __inline int
210 aic7770_map_int(struct ahc_softc *ahc, int irq)
211 {
212 	/*
213 	 * The IRQ is unused in the FreeBSD
214 	 * implementation since the ISA attachment
215 	 * registers the IRQ with newbus before
216 	 * the core is called.
217 	 */
218 	return ahc_map_int(ahc);
219 }
220 
221 /********************************* Debug **************************************/
222 static __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
223 static __inline void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
224 
225 static __inline void
226 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
227 {
228 	xpt_print_path(scb->io_ctx->ccb_h.path);
229 }
230 
231 static __inline void
232 ahc_platform_dump_card_state(struct ahc_softc *ahc)
233 {
234 	/* Nothing to do here for FreeBSD */
235 }
236 /**************************** Transfer Settings *******************************/
237 void	  ahc_notify_xfer_settings_change(struct ahc_softc *,
238 					  struct ahc_devinfo *);
239 void	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
240 				int /*enable*/);
241 
242 /****************************** Interrupts ************************************/
243 void			ahc_platform_intr(void *);
244 static __inline void	ahc_platform_flushwork(struct ahc_softc *ahc);
245 static __inline void
246 ahc_platform_flushwork(struct ahc_softc *ahc)
247 {
248 }
249 
250 /************************ Misc Function Declarations **************************/
251 void	  ahc_done(struct ahc_softc *ahc, struct scb *scb);
252 void	  ahc_send_async(struct ahc_softc *, char /*channel*/,
253 			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
254 #endif  /* _AIC7XXX_FREEBSD_H_ */
255