xref: /openbsd/sys/dev/ic/aic7xxx_openbsd.h (revision 898184e3)
1 /*	$OpenBSD: aic7xxx_openbsd.h,v 1.23 2012/02/12 14:30:51 deraadt 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/proc.h>
56 #include <sys/queue.h>
57 
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60 
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63 
64 #include <scsi/scsi_all.h>
65 #include <scsi/scsi_message.h>
66 #include <scsi/scsi_debug.h>
67 #include <scsi/scsiconf.h>
68 
69 #include <uvm/uvm_extern.h>
70 
71 #ifdef CAM_NEW_TRAN_CODE
72 #define AHC_NEW_TRAN_SETTINGS
73 #endif /* CAM_NEW_TRAN_CODE */
74 
75 #if NPCI > 0
76 #define AHC_PCI_CONFIG 1
77 #endif
78 
79 #if 0
80 #define AHC_DEBUG	AHC_SHOW_SENSE | AHC_SHOW_MISC | AHC_SHOW_CMDS
81 #endif
82 
83 #ifdef DEBUG
84 #define bootverbose	1
85 #else
86 #define bootverbose	0
87 #endif
88 /****************************** Platform Macros *******************************/
89 
90 #define	SCSI_IS_SCSIBUS_B(ahc, sc_link)	\
91 	((sc_link)->scsibus == (ahc)->sc_channel_b.scsibus)
92 #define	SCSI_SCSI_ID(ahc, sc_link)	\
93 	(SCSI_IS_SCSIBUS_B(ahc, sc_link) ? ahc->our_id_b : ahc->our_id)
94 #define	SCSI_CHANNEL(ahc, sc_link)	\
95 	(SCSI_IS_SCSIBUS_B(ahc, sc_link) ? 'B' : 'A')
96 #define BUILD_SCSIID(ahc, sc_link, target_id, our_id) \
97         ((((target_id) << TID_SHIFT) & TID) | (our_id) \
98         | (SCSI_IS_SCSIBUS_B(ahc, sc_link) ? TWIN_CHNLB : 0))
99 
100 #ifndef offsetof
101 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
102 #endif
103 /************************* Forward Declarations *******************************/
104 typedef struct pci_attach_args * ahc_dev_softc_t;
105 
106 /***************************** Bus Space/DMA **********************************/
107 
108 /* XXX Need to update Bus DMA for partial map syncs */
109 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)		\
110 	bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
111 
112 /************************ Tunable Driver Parameters  **************************/
113 /*
114  * The number of DMA segments supported.  The sequencer can handle any number
115  * of physically contiguous S/G entries.  To reduce the driver's memory
116  * consumption, we limit the number supported to be sufficient to handle
117  * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
118  * transfer is as fragmented as possible and unaligned, this turns out to
119  * be the number of paged sized transfers in MAXPHYS plus an extra element
120  * to handle any unaligned residual.  The sequencer fetches SG elements
121  * in cacheline sized chucks, so make the number per-transaction an even
122  * multiple of 16 which should align us on even the largest of cacheline
123  * boundaries.
124  */
125 #define AHC_NSEG (roundup(atop(MAXPHYS) + 1, 16))
126 
127 /* This driver supports target mode */
128 //#define AHC_TARGET_MODE 1
129 
130 #include <dev/ic/aic7xxxvar.h>
131 
132 /************************** Softc/SCB Platform Data ***************************/
133 struct ahc_platform_data {
134 };
135 
136 struct scb_platform_data {
137 };
138 
139 /*
140  * Some ISA devices (e.g. on a VLB) can perform 32-bit DMA.  This
141  * flag is passed to bus_dmamap_create() to indicate that fact.
142  */
143 #ifndef ISABUS_DMA_32BIT
144 #define ISABUS_DMA_32BIT	BUS_DMA_BUS1
145 #endif
146 
147 /************************** Timer DataStructures ******************************/
148 typedef struct timeout ahc_timer_t;
149 
150 /***************************** Core Includes **********************************/
151 #if AHC_REG_PRETTY_PRINT
152 #define AIC_DEBUG_REGISTERS 1
153 #else
154 #define AIC_DEBUG_REGISTERS 0
155 #endif
156 
157 /***************************** Timer Facilities *******************************/
158 void ahc_timeout(void*);
159 
160 #define ahc_timer_init callout_init
161 #define ahc_timer_stop callout_stop
162 
163 static __inline void
164 ahc_timer_reset(ahc_timer_t *timer, u_int usec, ahc_callback_t *func, void *arg)
165 {
166 	callout_reset(timer, (usec * hz)/1000000, func, arg);
167 }
168 
169 static __inline void
170 ahc_scb_timer_reset(struct scb *scb, u_int usec)
171 {
172   	if (!(scb->xs->xs_control & XS_CTL_POLL)) {
173 		callout_reset(&scb->xs->xs_callout,
174 			      (usec * hz)/1000000, ahc_timeout, scb);
175 	}
176 }
177 
178 /*************************** Device Access ************************************/
179 #define ahc_inb(ahc, port)				\
180 	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
181 
182 #define ahc_outb(ahc, port, value)			\
183 	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
184 
185 #define ahc_outsb(ahc, port, valp, count)		\
186 	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
187 
188 #define ahc_insb(ahc, port, valp, count)		\
189 	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
190 
191 static __inline void ahc_flush_device_writes(struct ahc_softc *);
192 
193 static __inline void
194 ahc_flush_device_writes(struct ahc_softc *ahc)
195 {
196 	bus_space_barrier(ahc->tag, ahc->bsh, 0, 0x100,
197 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
198 	ahc_inb(ahc, INTSTAT);
199 }
200 
201 /**************************** Locking Primitives ******************************/
202 
203 /****************************** OS Primitives *********************************/
204 
205 /************************** Transaction Operations ****************************/
206 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
207 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
208 static __inline uint32_t ahc_get_transaction_status(struct scb *);
209 static __inline uint32_t ahc_get_scsi_status(struct scb *);
210 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
211 static __inline u_long ahc_get_transfer_length(struct scb *);
212 static __inline int ahc_get_transfer_dir(struct scb *);
213 static __inline void ahc_set_residual(struct scb *, u_long);
214 static __inline void ahc_set_sense_residual(struct scb *, u_long);
215 static __inline u_long ahc_get_residual(struct scb *);
216 static __inline int ahc_perform_autosense(struct scb *);
217 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
218     struct scb *);
219 static __inline void ahc_freeze_scb(struct scb *);
220 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
221 static __inline int  ahc_platform_abort_scbs(struct ahc_softc *, int, char,
222     int, u_int, role_t, uint32_t);
223 
224 static __inline
225 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
226 {
227 	scb->xs->error = status;
228 }
229 
230 static __inline
231 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
232 {
233 	scb->xs->status = status;
234 }
235 
236 static __inline
237 uint32_t ahc_get_transaction_status(struct scb *scb)
238 {
239 	if (scb->xs->flags & ITSDONE)
240 		return CAM_REQ_CMP;
241 	else
242 		return scb->xs->error;
243 }
244 
245 static __inline
246 uint32_t ahc_get_scsi_status(struct scb *scb)
247 {
248 	return (scb->xs->status);
249 }
250 
251 static __inline
252 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
253 {
254 }
255 
256 static __inline
257 u_long ahc_get_transfer_length(struct scb *scb)
258 {
259 	return (scb->xs->datalen);
260 }
261 
262 static __inline
263 int ahc_get_transfer_dir(struct scb *scb)
264 {
265 	return (scb->xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT));
266 }
267 
268 static __inline
269 void ahc_set_residual(struct scb *scb, u_long resid)
270 {
271 	scb->xs->resid = resid;
272 }
273 
274 static __inline
275 void ahc_set_sense_residual(struct scb *scb, u_long resid)
276 {
277 	scb->xs->resid = resid;
278 }
279 
280 static __inline
281 u_long ahc_get_residual(struct scb *scb)
282 {
283 	return (scb->xs->resid);
284 }
285 
286 static __inline
287 int ahc_perform_autosense(struct scb *scb)
288 {
289 	/* Return true for OpenBSD */
290 	return (1);
291 }
292 
293 static __inline uint32_t
294 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
295 {
296 	return (sizeof(struct scsi_sense_data));
297 }
298 
299 static __inline void
300 ahc_freeze_scb(struct scb *scb)
301 {
302 }
303 
304 static __inline void
305 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
306 {
307 }
308 
309 static __inline int
310 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
311 			char channel, int lun, u_int tag,
312 			role_t role, uint32_t status)
313 {
314 	return (0);
315 }
316 
317 static __inline void
318 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
319 {
320 	int s;
321 
322 	s = splbio();
323 
324 	if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0) {
325 		ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
326 	}
327 
328 	timeout_del(&scb->xs->stimeout);
329 
330 	splx(s);
331 }
332 
333 /********************************** PCI ***************************************/
334 #ifdef AHC_PCI_CONFIG
335 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t, int, int);
336 static __inline void	 ahc_pci_write_config(ahc_dev_softc_t, int, uint32_t,
337     int);
338 static __inline int	 ahc_get_pci_function(ahc_dev_softc_t);
339 static __inline int	 ahc_get_pci_slot(ahc_dev_softc_t);
340 static __inline int	 ahc_get_pci_bus(ahc_dev_softc_t);
341 
342 int			 ahc_pci_map_registers(struct ahc_softc *);
343 int			 ahc_pci_map_int(struct ahc_softc *);
344 
345 static __inline uint32_t
346 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
347 {
348 	return (pci_conf_read(pci->pa_pc, pci->pa_tag, reg));
349 }
350 
351 static __inline void
352 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
353 {
354 	pci_conf_write(pci->pa_pc, pci->pa_tag, reg, value);
355 }
356 
357 static __inline int
358 ahc_get_pci_function(ahc_dev_softc_t pci)
359 {
360 	return (pci->pa_function);
361 }
362 
363 static __inline int
364 ahc_get_pci_slot(ahc_dev_softc_t pci)
365 {
366 	return (pci->pa_device);
367 }
368 
369 static __inline int
370 ahc_get_pci_bus(ahc_dev_softc_t pci)
371 {
372 	return (pci->pa_bus);
373 }
374 
375 typedef enum
376 {
377 	AHC_POWER_STATE_D0,
378 	AHC_POWER_STATE_D1,
379 	AHC_POWER_STATE_D2,
380 	AHC_POWER_STATE_D3
381 } ahc_power_state;
382 
383 void ahc_power_state_change(struct ahc_softc *, ahc_power_state);
384 #endif
385 /******************************** VL/EISA *************************************/
386 int aic7770_map_registers(struct ahc_softc *, u_int);
387 int aic7770_map_int(struct ahc_softc *, int);
388 
389 /********************************* Debug **************************************/
390 static __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
391 static __inline void	ahc_platform_dump_card_state(struct ahc_softc *);
392 
393 static __inline void
394 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
395 {
396 	sc_print_addr(scb->xs->sc_link);
397 }
398 
399 static __inline void
400 ahc_platform_dump_card_state(struct ahc_softc *ahc)
401 {
402 	/* Nothing to do here for OpenBSD */
403 	printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
404 	       ahc->features, ahc->flags, ahc->chip, ahc->bugs);
405 }
406 /**************************** Transfer Settings *******************************/
407 void	  ahc_notify_xfer_settings_change(struct ahc_softc *,
408 					  struct ahc_devinfo *);
409 void	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *, int);
410 
411 /************************* Initialization/Teardown ****************************/
412 int	  ahc_platform_alloc(struct ahc_softc *, void *);
413 void	  ahc_platform_free(struct ahc_softc *);
414 int	  ahc_map_int(struct ahc_softc *);
415 int	  ahc_attach(struct ahc_softc *);
416 int	  ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
417 int	  ahc_detach(struct device *, int);
418 
419 /****************************** Interrupts ************************************/
420 int                     ahc_platform_intr(void *);
421 static __inline void    ahc_platform_flushwork(struct ahc_softc *);
422 static __inline void
423 ahc_platform_flushwork(struct ahc_softc *ahc)
424 {
425 }
426 
427 /************************ Misc Function Declarations **************************/
428 void	  ahc_done(struct ahc_softc *, struct scb *);
429 void	  ahc_send_async(struct ahc_softc *, char, u_int, u_int, ac_code,
430     void *);
431 
432 #endif  /* _AIC7XXX_OPENBSD_H_ */
433