aic7xxx_osm.h revision 65942
1/*
2 * FreeBSD platform specific driver option settings, data structures,
3 * function declarations and includes.
4 *
5 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 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$
33 *
34 * $FreeBSD: head/sys/dev/aic7xxx/aic7xxx_osm.h 65942 2000-09-16 20:02:28Z gibbs $
35 */
36
37#ifndef _AIC7XXX_FREEBSD_H_
38#define _AIC7XXX_FREEBSD_H_
39
40#include <opt_aic7xxx.h>	/* for config options */
41#include <pci.h>		/* for NPCI */
42
43#include <stddef.h>		/* For offsetof */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/bus.h>		/* For device_t */
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/queue.h>
51
52#if NPCI > 0
53#define AHC_SUPPORT_PCI 1
54#include <machine/bus_memio.h>
55#endif
56#include <machine/bus_pio.h>
57#include <machine/bus.h>
58#include <machine/clock.h>
59#include <machine/resource.h>
60
61#include <sys/rman.h>
62
63#if NPCI > 0
64#include <pci/pcireg.h>
65#include <pci/pcivar.h>
66#endif
67
68#include <cam/cam.h>
69#include <cam/cam_ccb.h>
70#include <cam/cam_debug.h>
71#include <cam/cam_sim.h>
72#include <cam/cam_xpt_sim.h>
73
74#include <cam/scsi/scsi_all.h>
75#include <cam/scsi/scsi_message.h>
76
77/****************************** Platform Macros *******************************/
78#define	SIM_IS_SCSIBUS_B(ahc, sim)	\
79	((sim) == ahc->platform_data->sim_b)
80#define	SIM_CHANNEL(ahc, sim)	\
81	(((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
82#define	SIM_SCSI_ID(ahc, sim)	\
83	(((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
84#define	SIM_PATH(ahc, sim)	\
85	(((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
86					      : ahc->platform_data->path)
87#define BUILD_SCSIID(ahc, sim, target_id, our_id) \
88        ((((target_id) << TID_SHIFT) & TID) | (our_id) \
89        | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
90
91#define SCB_GET_SIM(ahc, scb) \
92	(SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
93					  : (ahc)->platform_data->sim_b)
94
95/************************* Forward Declarations *******************************/
96typedef device_t ahc_dev_softc_t;
97typedef union ccb *ahc_io_ctx_t;
98
99/***************************** Bus Space/DMA **********************************/
100#define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary,	\
101			   lowaddr, highaddr, filter, filterarg,	\
102			   maxsize, nsegments, maxsegsz, flags,		\
103			   dma_tagp)					\
104	bus_dma_tag_create(parent_tag, alignment, boundary,		\
105			   lowaddr, highaddr, filter, filterarg,	\
106			   maxsize, nsegments, maxsegsz, flags,		\
107			   dma_tagp)
108
109#define ahc_dma_tag_destroy(ahc, tag)					\
110	bus_dma_tag_destroy(tag)
111
112#define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp)			\
113	bus_dmamem_alloc(dmat, vaddr, flags, mapp)
114
115#define ahc_dmamem_free(ahc, dmat, vaddr, map)				\
116	bus_dmamem_free(dmat, vaddr, map)
117
118#define ahc_dmamap_create(ahc, tag, flags, mapp)			\
119	bus_dmamap_create(tag, flags, mapp)
120
121#define ahc_dmamap_destroy(ahc, tag, map)				\
122	bus_dmamap_destroy(tag, map)
123
124#define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback,		\
125			callback_arg, flags)				\
126	bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
127
128#define ahc_dmamap_unload(ahc, tag, map)				\
129	bus_dmamap_unload(tag, map)
130
131#define ahc_dmamap_sync(ahc, dma_tag, dmamap, op)			\
132	bus_dmamap_sync(dma_tag_dmamap, op)
133
134/************************ Tunable Driver Parameters  **************************/
135/*
136 * The number of dma segments supported.  The sequencer can handle any number
137 * of physically contiguous S/G entrys.  To reduce the driver's memory
138 * consumption, we limit the number supported to be sufficient to handle
139 * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
140 * transfer is as fragmented as possible and unaligned, this turns out to
141 * be the number of paged sized transfers in MAXPHYS plus an extra element
142 * to handle any unaligned residual.  The sequencer fetches SG elements
143 * in 128 byte chucks, so make the number per-transaction a nice multiple
144 * of 16 (8 byte S/G elements).
145 */
146/* XXX Worth the space??? */
147#define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
148
149/* This driver supports target mode */
150#define AHC_TARGET_MODE 1
151
152/************************** Softc/SCB Platform Data ***************************/
153struct ahc_platform_data {
154	/*
155	 * Hooks into the XPT.
156	 */
157	struct	cam_sim		*sim;
158	struct	cam_sim		*sim_b;
159	struct	cam_path	*path;
160	struct	cam_path	*path_b;
161
162	int			 regs_res_type;
163	int			 regs_res_id;
164	int			 irq_res_type;
165	struct resource		*regs;
166	struct resource		*irq;
167	void			*ih;
168};
169
170struct scb_platform_data {
171};
172
173/***************************** Core Includes **********************************/
174#include <dev/aic7xxx/aic7xxx.h>
175
176/*************************** Device Access ************************************/
177#define ahc_inb(ahc, port)				\
178	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
179
180#define ahc_outb(ahc, port, value)			\
181	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
182
183#define ahc_outsb(ahc, port, valp, count)		\
184	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
185
186#define ahc_insb(ahc, port, valp, count)		\
187	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
188
189static __inline void ahc_flush_device_writes(struct ahc_softc *);
190
191static __inline void
192ahc_flush_device_writes(struct ahc_softc *ahc)
193{
194	/* XXX Is this sufficient for all architectures??? */
195	ahc_inb(ahc, INTSTAT);
196}
197
198/**************************** Locking Primitives ******************************/
199/* Lock protecting internal data structures */
200static __inline void ahc_lockinit(struct ahc_softc *);
201static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
202static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
203
204/* Lock held during command compeletion to the upper layer */
205static __inline void ahc_done_lockinit(struct ahc_softc *);
206static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
207static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
208
209static __inline void
210ahc_lockinit(struct ahc_softc *ahc)
211{
212}
213
214static __inline void
215ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
216{
217	*flags = splcam();
218}
219
220static __inline void
221ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
222{
223	splx(*flags);
224}
225
226/* Lock held during command compeletion to the upper layer */
227static __inline void
228ahc_done_lockinit(struct ahc_softc *ahc)
229{
230}
231
232static __inline void
233ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
234{
235}
236
237static __inline void
238ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
239{
240}
241
242/****************************** OS Primitives *********************************/
243#define ahc_delay DELAY
244
245/************************** Transaction Operations ****************************/
246static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
247static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
248static __inline uint32_t ahc_get_transaction_status(struct scb *);
249static __inline uint32_t ahc_get_scsi_status(struct scb *);
250static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
251static __inline u_long ahc_get_transfer_length(struct scb *);
252static __inline int ahc_get_transfer_dir(struct scb *);
253static __inline void ahc_set_residual(struct scb *, u_long);
254static __inline void ahc_set_sense_residual(struct scb *, u_long);
255static __inline u_long ahc_get_residual(struct scb *);
256static __inline int ahc_perform_autosense(struct scb *);
257static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*);
258static __inline void ahc_freeze_ccb(union ccb *ccb);
259static __inline void ahc_freeze_scb(struct scb *scb);
260static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
261static __inline int  ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
262					     char channel, int lun, u_int tag,
263					     role_t role, uint32_t status);
264
265static __inline
266void ahc_set_transaction_status(struct scb *scb, uint32_t status)
267{
268	scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
269	scb->io_ctx->ccb_h.status |= status;
270}
271
272static __inline
273void ahc_set_scsi_status(struct scb *scb, uint32_t status)
274{
275	scb->io_ctx->csio.scsi_status = status;
276}
277
278static __inline
279uint32_t ahc_get_transaction_status(struct scb *scb)
280{
281	return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
282}
283
284static __inline
285uint32_t ahc_get_scsi_status(struct scb *scb)
286{
287	return (scb->io_ctx->csio.scsi_status);
288}
289
290static __inline
291void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
292{
293	scb->io_ctx->csio.tag_action = type;
294	if (enabled)
295		scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
296	else
297		scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
298}
299
300static __inline
301u_long ahc_get_transfer_length(struct scb *scb)
302{
303	return (scb->io_ctx->csio.dxfer_len);
304}
305
306static __inline
307int ahc_get_transfer_dir(struct scb *scb)
308{
309	return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
310}
311
312static __inline
313void ahc_set_residual(struct scb *scb, u_long resid)
314{
315	scb->io_ctx->csio.resid = resid;
316}
317
318static __inline
319void ahc_set_sense_residual(struct scb *scb, u_long resid)
320{
321	scb->io_ctx->csio.sense_resid = resid;
322}
323
324static __inline
325u_long ahc_get_residual(struct scb *scb)
326{
327	return (scb->io_ctx->csio.resid);
328}
329
330static __inline
331int ahc_perform_autosense(struct scb *scb)
332{
333	return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
334}
335
336static __inline uint32_t
337ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
338{
339	return (sizeof(struct scsi_sense_data));
340}
341
342static __inline void
343ahc_freeze_ccb(union ccb *ccb)
344{
345	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
346		ccb->ccb_h.status |= CAM_DEV_QFRZN;
347		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
348	}
349}
350
351static __inline void
352ahc_freeze_scb(struct scb *scb)
353{
354	ahc_freeze_ccb(scb->io_ctx);
355}
356
357static __inline void
358ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
359{
360	/* Nothing to do here for FreeBSD */
361}
362
363static __inline int
364ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
365			char channel, int lun, u_int tag,
366			role_t role, uint32_t status)
367{
368	/* Nothing to do here for FreeBSD */
369	return (0);
370}
371
372/********************************** PCI ***************************************/
373#ifdef AHC_SUPPORT_PCI
374static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
375					     int reg, int width);
376static __inline void	 ahc_pci_write_config(ahc_dev_softc_t pci,
377					      int reg, uint32_t value,
378					      int width);
379static __inline int	 ahc_get_pci_function(ahc_dev_softc_t);
380static __inline int	 ahc_get_pci_slot(ahc_dev_softc_t);
381static __inline int	 ahc_get_pci_bus(ahc_dev_softc_t);
382
383int			 ahc_pci_map_registers(struct ahc_softc *ahc);
384int			 ahc_pci_map_int(struct ahc_softc *ahc);
385
386static __inline uint32_t
387ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
388{
389	return (pci_read_config(pci, reg, width));
390}
391
392static __inline void
393ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
394{
395	pci_write_config(pci, reg, value, width);
396}
397
398static __inline int
399ahc_get_pci_function(ahc_dev_softc_t pci)
400{
401	return (pci_get_function(pci));
402}
403
404static __inline int
405ahc_get_pci_slot(ahc_dev_softc_t pci)
406{
407	return (pci_get_slot(pci));
408}
409
410static __inline int
411ahc_get_pci_bus(ahc_dev_softc_t pci)
412{
413	return (pci_get_bus(pci));
414}
415#endif
416/******************************** VL/EISA *************************************/
417int aic7770_map_registers(struct ahc_softc *ahc);
418int aic7770_map_int(struct ahc_softc *ahc);
419
420/********************************* Debug **************************************/
421static __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
422static __inline void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
423
424static __inline void
425ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
426{
427	xpt_print_path(scb->io_ctx->ccb_h.path);
428}
429
430static __inline void
431ahc_platform_dump_card_state(struct ahc_softc *ahc)
432{
433	/* Nothing to do here for FreeBSD */
434}
435/**************************** Transfer Settings *******************************/
436void	  ahc_notify_xfer_settings_change(struct ahc_softc *,
437					  struct ahc_devinfo *);
438void	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
439				int /*enable*/);
440
441/***************************** Initialization *********************************/
442int	  ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
443void	  ahc_platform_free(struct ahc_softc *ahc);
444int	  ahc_attach(struct ahc_softc *);
445int	  ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
446
447/************************ Misc Function Declarations **************************/
448timeout_t ahc_timeout;
449void	  ahc_done(struct ahc_softc *ahc, struct scb *scb);
450void	  ahc_send_async(struct ahc_softc *, struct ahc_devinfo *, ac_code);
451#endif  /* _AIC7XXX_FREEBSD_H_ */
452