xref: /openbsd/sys/dev/ic/aic7xxx_openbsd.h (revision 528abb89)
1 /*	$OpenBSD: aic7xxx_openbsd.h,v 1.32 2024/05/29 00:48:15 jsg Exp $	*/
2 /*	$NetBSD: aic7xxx_osm.h,v 1.7 2003/11/02 11:07:44 wiz Exp $	*/
3 
4 /*
5  * OpenBSD platform specific driver option settings, data structures,
6  * function declarations and includes.
7  *
8  * Copyright (c) 1994-2001 Justin T. Gibbs.
9  * Copyright (c) 2001-2002 Steve Murphree, Jr.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. The name of the author(s) may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU Public License ("GPL").
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#14 $
37  *
38  * $FreeBSD: /repoman/r/ncvs/src/sys/dev/aic7xxx/aic7xxx_osm.h,v 1.20 2002/12/04 22:51:29 scottl Exp $
39  */
40 /*
41  * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
42  */
43 
44 #ifndef _AIC7XXX_OPENBSD_H_
45 #define _AIC7XXX_OPENBSD_H_
46 
47 #include "pci.h"		/* for config options */
48 
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #include <sys/queue.h>
56 
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59 
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62 
63 #include <scsi/scsi_all.h>
64 #include <scsi/scsi_message.h>
65 #include <scsi/scsiconf.h>
66 
67 #include <uvm/uvm_extern.h>
68 
69 #ifdef CAM_NEW_TRAN_CODE
70 #define AHC_NEW_TRAN_SETTINGS
71 #endif /* CAM_NEW_TRAN_CODE */
72 
73 #if NPCI > 0
74 #define AHC_PCI_CONFIG 1
75 #endif
76 
77 #if 0
78 #define AHC_DEBUG	AHC_SHOW_SENSE | AHC_SHOW_MISC | AHC_SHOW_CMDS
79 #endif
80 
81 #ifdef DEBUG
82 #define bootverbose	1
83 #else
84 #define bootverbose	0
85 #endif
86 /****************************** Platform Macros *******************************/
87 
88 #define	SCSI_IS_SCSIBUS_B(ahc, sc_link)	\
89 	(((ahc)->sc_child != NULL) && ((sc_link)->bus != (ahc)->sc_child))
90 #define	SCSI_SCSI_ID(ahc, sc_link)	\
91 	(SCSI_IS_SCSIBUS_B(ahc, sc_link) ? ahc->our_id_b : ahc->our_id)
92 #define	SCSI_CHANNEL(ahc, sc_link)	\
93 	(SCSI_IS_SCSIBUS_B(ahc, sc_link) ? 'B' : 'A')
94 #define BUILD_SCSIID(ahc, sc_link, target_id, our_id) \
95         ((((target_id) << TID_SHIFT) & TID) | (our_id) \
96         | (SCSI_IS_SCSIBUS_B(ahc, sc_link) ? TWIN_CHNLB : 0))
97 
98 #ifndef offsetof
99 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
100 #endif
101 /************************* Forward Declarations *******************************/
102 typedef struct pci_attach_args * ahc_dev_softc_t;
103 
104 /***************************** Bus Space/DMA **********************************/
105 
106 /* XXX Need to update Bus DMA for partial map syncs */
107 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)		\
108 	bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
109 
110 /************************ Tunable Driver Parameters  **************************/
111 /*
112  * The number of DMA segments supported.  The sequencer can handle any number
113  * of physically contiguous S/G entries.  To reduce the driver's memory
114  * consumption, we limit the number supported to be sufficient to handle
115  * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
116  * transfer is as fragmented as possible and unaligned, this turns out to
117  * be the number of paged sized transfers in MAXPHYS plus an extra element
118  * to handle any unaligned residual.  The sequencer fetches SG elements
119  * in cacheline sized chucks, so make the number per-transaction an even
120  * multiple of 16 which should align us on even the largest of cacheline
121  * boundaries.
122  */
123 #define AHC_NSEG (roundup(atop(MAXPHYS) + 1, 16))
124 
125 /* This driver supports target mode */
126 //#define AHC_TARGET_MODE 1
127 
128 #include <dev/ic/aic7xxxvar.h>
129 
130 /*
131  * Some ISA devices (e.g. on a VLB) can perform 32-bit DMA.  This
132  * flag is passed to bus_dmamap_create() to indicate that fact.
133  */
134 #ifndef ISABUS_DMA_32BIT
135 #define ISABUS_DMA_32BIT	BUS_DMA_BUS1
136 #endif
137 
138 /************************** Timer DataStructures ******************************/
139 typedef struct timeout ahc_timer_t;
140 
141 /***************************** Core Includes **********************************/
142 #if AHC_REG_PRETTY_PRINT
143 #define AIC_DEBUG_REGISTERS 1
144 #else
145 #define AIC_DEBUG_REGISTERS 0
146 #endif
147 
148 /***************************** Timer Facilities *******************************/
149 void ahc_timeout(void*);
150 
151 #define ahc_timer_init callout_init
152 #define ahc_timer_stop callout_stop
153 
154 static __inline void
ahc_timer_reset(ahc_timer_t * timer,u_int usec,ahc_callback_t * func,void * arg)155 ahc_timer_reset(ahc_timer_t *timer, u_int usec, ahc_callback_t *func, void *arg)
156 {
157 	callout_reset(timer, (usec * hz)/1000000, func, arg);
158 }
159 
160 static __inline void
ahc_scb_timer_reset(struct scb * scb,u_int usec)161 ahc_scb_timer_reset(struct scb *scb, u_int usec)
162 {
163 	if (!(scb->xs->xs_control & XS_CTL_POLL)) {
164 		callout_reset(&scb->xs->xs_callout,
165 			      (usec * hz)/1000000, ahc_timeout, scb);
166 	}
167 }
168 
169 /*************************** Device Access ************************************/
170 #define ahc_inb(ahc, port)				\
171 	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
172 
173 #define ahc_outb(ahc, port, value)			\
174 	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
175 
176 #define ahc_outsb(ahc, port, valp, count)		\
177 	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
178 
179 #define ahc_insb(ahc, port, valp, count)		\
180 	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
181 
182 static __inline void ahc_flush_device_writes(struct ahc_softc *);
183 
184 static __inline void
ahc_flush_device_writes(struct ahc_softc * ahc)185 ahc_flush_device_writes(struct ahc_softc *ahc)
186 {
187 	bus_space_barrier(ahc->tag, ahc->bsh, 0, 0x100,
188 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
189 	ahc_inb(ahc, INTSTAT);
190 }
191 
192 /**************************** Locking Primitives ******************************/
193 
194 /****************************** OS Primitives *********************************/
195 
196 /************************** Transaction Operations ****************************/
197 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
198 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
199 static __inline uint32_t ahc_get_transaction_status(struct scb *);
200 static __inline uint32_t ahc_get_scsi_status(struct scb *);
201 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
202 static __inline u_long ahc_get_transfer_length(struct scb *);
203 static __inline int ahc_get_transfer_dir(struct scb *);
204 static __inline void ahc_set_residual(struct scb *, u_long);
205 static __inline void ahc_set_sense_residual(struct scb *, u_long);
206 static __inline u_long ahc_get_residual(struct scb *);
207 static __inline int ahc_perform_autosense(struct scb *);
208 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
209     struct scb *);
210 static __inline void ahc_freeze_scb(struct scb *);
211 
212 static __inline
ahc_set_transaction_status(struct scb * scb,uint32_t status)213 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
214 {
215 	scb->xs->error = status;
216 }
217 
218 static __inline
ahc_set_scsi_status(struct scb * scb,uint32_t status)219 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
220 {
221 	scb->xs->status = status;
222 }
223 
224 static __inline
ahc_get_transaction_status(struct scb * scb)225 uint32_t ahc_get_transaction_status(struct scb *scb)
226 {
227 	if (scb->xs->flags & ITSDONE)
228 		return CAM_REQ_CMP;
229 	else
230 		return scb->xs->error;
231 }
232 
233 static __inline
ahc_get_scsi_status(struct scb * scb)234 uint32_t ahc_get_scsi_status(struct scb *scb)
235 {
236 	return (scb->xs->status);
237 }
238 
239 static __inline
ahc_set_transaction_tag(struct scb * scb,int enabled,u_int type)240 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
241 {
242 }
243 
244 static __inline
ahc_get_transfer_length(struct scb * scb)245 u_long ahc_get_transfer_length(struct scb *scb)
246 {
247 	return (scb->xs->datalen);
248 }
249 
250 static __inline
ahc_get_transfer_dir(struct scb * scb)251 int ahc_get_transfer_dir(struct scb *scb)
252 {
253 	return (scb->xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT));
254 }
255 
256 static __inline
ahc_set_residual(struct scb * scb,u_long resid)257 void ahc_set_residual(struct scb *scb, u_long resid)
258 {
259 	scb->xs->resid = resid;
260 }
261 
262 static __inline
ahc_set_sense_residual(struct scb * scb,u_long resid)263 void ahc_set_sense_residual(struct scb *scb, u_long resid)
264 {
265 	scb->xs->resid = resid;
266 }
267 
268 static __inline
ahc_get_residual(struct scb * scb)269 u_long ahc_get_residual(struct scb *scb)
270 {
271 	return (scb->xs->resid);
272 }
273 
274 static __inline
ahc_perform_autosense(struct scb * scb)275 int ahc_perform_autosense(struct scb *scb)
276 {
277 	/* Return true for OpenBSD */
278 	return (1);
279 }
280 
281 static __inline uint32_t
ahc_get_sense_bufsize(struct ahc_softc * ahc,struct scb * scb)282 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
283 {
284 	return (sizeof(struct scsi_sense_data));
285 }
286 
287 static __inline void
ahc_freeze_scb(struct scb * scb)288 ahc_freeze_scb(struct scb *scb)
289 {
290 }
291 
292 static __inline void
ahc_platform_scb_free(struct ahc_softc * ahc,struct scb * scb)293 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
294 {
295 	int s;
296 
297 	s = splbio();
298 
299 	if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0) {
300 		ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
301 	}
302 
303 	timeout_del(&scb->xs->stimeout);
304 
305 	splx(s);
306 }
307 
308 /********************************** PCI ***************************************/
309 #ifdef AHC_PCI_CONFIG
310 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t, int, int);
311 static __inline void	 ahc_pci_write_config(ahc_dev_softc_t, int, uint32_t,
312     int);
313 static __inline int	 ahc_get_pci_function(ahc_dev_softc_t);
314 static __inline int	 ahc_get_pci_slot(ahc_dev_softc_t);
315 static __inline int	 ahc_get_pci_bus(ahc_dev_softc_t);
316 
317 int			 ahc_pci_map_registers(struct ahc_softc *);
318 int			 ahc_pci_map_int(struct ahc_softc *);
319 
320 static __inline uint32_t
ahc_pci_read_config(ahc_dev_softc_t pci,int reg,int width)321 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
322 {
323 	return (pci_conf_read(pci->pa_pc, pci->pa_tag, reg));
324 }
325 
326 static __inline void
ahc_pci_write_config(ahc_dev_softc_t pci,int reg,uint32_t value,int width)327 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
328 {
329 	pci_conf_write(pci->pa_pc, pci->pa_tag, reg, value);
330 }
331 
332 static __inline int
ahc_get_pci_function(ahc_dev_softc_t pci)333 ahc_get_pci_function(ahc_dev_softc_t pci)
334 {
335 	return (pci->pa_function);
336 }
337 
338 static __inline int
ahc_get_pci_slot(ahc_dev_softc_t pci)339 ahc_get_pci_slot(ahc_dev_softc_t pci)
340 {
341 	return (pci->pa_device);
342 }
343 
344 static __inline int
ahc_get_pci_bus(ahc_dev_softc_t pci)345 ahc_get_pci_bus(ahc_dev_softc_t pci)
346 {
347 	return (pci->pa_bus);
348 }
349 
350 typedef enum
351 {
352 	AHC_POWER_STATE_D0,
353 	AHC_POWER_STATE_D1,
354 	AHC_POWER_STATE_D2,
355 	AHC_POWER_STATE_D3
356 } ahc_power_state;
357 
358 void ahc_power_state_change(struct ahc_softc *, ahc_power_state);
359 #endif
360 /********************************* Debug **************************************/
361 static __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
362 static __inline void	ahc_platform_dump_card_state(struct ahc_softc *);
363 
364 static __inline void
ahc_print_path(struct ahc_softc * ahc,struct scb * scb)365 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
366 {
367 	sc_print_addr(scb->xs->sc_link);
368 }
369 
370 static __inline void
ahc_platform_dump_card_state(struct ahc_softc * ahc)371 ahc_platform_dump_card_state(struct ahc_softc *ahc)
372 {
373 	/* Nothing to do here for OpenBSD */
374 	printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
375 	       ahc->features, ahc->flags, ahc->chip, ahc->bugs);
376 }
377 /**************************** Transfer Settings *******************************/
378 void	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *, int);
379 
380 /************************* Initialization/Teardown ****************************/
381 int	  ahc_attach(struct ahc_softc *);
382 int	  ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
383 
384 /****************************** Interrupts ************************************/
385 int                     ahc_platform_intr(void *);
386 
387 /************************ Misc Function Declarations **************************/
388 void	  ahc_done(struct ahc_softc *, struct scb *);
389 void	  ahc_send_async(struct ahc_softc *, char, u_int, u_int, ac_code,
390     void *);
391 void	 *ahc_scb_alloc(void *);
392 void	  ahc_scb_free(void *, void *);
393 
394 #endif  /* _AIC7XXX_OPENBSD_H_ */
395