1139749Simp/*-
265942Sgibbs * FreeBSD platform specific driver option settings, data structures,
365942Sgibbs * function declarations and includes.
465942Sgibbs *
571717Sgibbs * Copyright (c) 1994-2001 Justin T. Gibbs.
665942Sgibbs * All rights reserved.
765942Sgibbs *
865942Sgibbs * Redistribution and use in source and binary forms, with or without
965942Sgibbs * modification, are permitted provided that the following conditions
1065942Sgibbs * are met:
1165942Sgibbs * 1. Redistributions of source code must retain the above copyright
1265942Sgibbs *    notice, this list of conditions, and the following disclaimer,
1365942Sgibbs *    without modification.
1465942Sgibbs * 2. The name of the author may not be used to endorse or promote products
1565942Sgibbs *    derived from this software without specific prior written permission.
1665942Sgibbs *
1765942Sgibbs * Alternatively, this software may be distributed under the terms of the
1865942Sgibbs * GNU Public License ("GPL").
1965942Sgibbs *
2065942Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2165942Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2265942Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2365942Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2465942Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2565942Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2665942Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2765942Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2865942Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2965942Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3065942Sgibbs * SUCH DAMAGE.
3165942Sgibbs *
32123579Sgibbs * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#18 $
3365942Sgibbs *
3465942Sgibbs * $FreeBSD$
3565942Sgibbs */
3665942Sgibbs
3765942Sgibbs#ifndef _AIC7XXX_FREEBSD_H_
3865942Sgibbs#define _AIC7XXX_FREEBSD_H_
3965942Sgibbs
4065942Sgibbs#include <opt_aic7xxx.h>	/* for config options */
4165942Sgibbs
4265942Sgibbs#include <sys/param.h>
4365942Sgibbs#include <sys/systm.h>
4465942Sgibbs#include <sys/bus.h>		/* For device_t */
45102675Sgibbs#if __FreeBSD_version >= 500000
4695533Smike#include <sys/endian.h>
47102675Sgibbs#endif
4870204Sgibbs#include <sys/eventhandler.h>
4965942Sgibbs#include <sys/kernel.h>
5065942Sgibbs#include <sys/malloc.h>
51129879Sphk#include <sys/module.h>
5265942Sgibbs#include <sys/queue.h>
5365942Sgibbs
54100548Speter#if __FreeBSD_version < 500000
55100548Speter#include <pci.h>
56100548Speter#else
57100548Speter#define NPCI 1
58100548Speter#endif
59100548Speter
6065942Sgibbs#if NPCI > 0
61123579Sgibbs#define AIC_PCI_CONFIG 1
6265942Sgibbs#endif
6365942Sgibbs#include <machine/bus.h>
6495378Sgibbs#include <machine/endian.h>
6565942Sgibbs#include <machine/resource.h>
6665942Sgibbs
6765942Sgibbs#include <sys/rman.h>
6865942Sgibbs
6965942Sgibbs#if NPCI > 0
70123579Sgibbs#if __FreeBSD_version >= 500000
71123579Sgibbs#include <dev/pci/pcireg.h>
72123579Sgibbs#include <dev/pci/pcivar.h>
73123579Sgibbs#else
7465942Sgibbs#include <pci/pcireg.h>
7565942Sgibbs#include <pci/pcivar.h>
7665942Sgibbs#endif
77119277Simp#endif
7865942Sgibbs
7965942Sgibbs#include <cam/cam.h>
8065942Sgibbs#include <cam/cam_ccb.h>
8165942Sgibbs#include <cam/cam_debug.h>
8265942Sgibbs#include <cam/cam_sim.h>
8365942Sgibbs#include <cam/cam_xpt_sim.h>
8465942Sgibbs
8565942Sgibbs#include <cam/scsi/scsi_all.h>
8665942Sgibbs#include <cam/scsi/scsi_message.h>
8765942Sgibbs
88103811Sscottl/*************************** Attachment Bookkeeping ***************************/
89103811Sscottlextern devclass_t ahc_devclass;
90103811Sscottl
9165942Sgibbs/****************************** Platform Macros *******************************/
9265942Sgibbs#define	SIM_IS_SCSIBUS_B(ahc, sim)	\
9365942Sgibbs	((sim) == ahc->platform_data->sim_b)
9465942Sgibbs#define	SIM_CHANNEL(ahc, sim)	\
9565942Sgibbs	(((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
9665942Sgibbs#define	SIM_SCSI_ID(ahc, sim)	\
9765942Sgibbs	(((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
9865942Sgibbs#define	SIM_PATH(ahc, sim)	\
9965942Sgibbs	(((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
10065942Sgibbs					      : ahc->platform_data->path)
10165942Sgibbs#define BUILD_SCSIID(ahc, sim, target_id, our_id) \
10265942Sgibbs        ((((target_id) << TID_SHIFT) & TID) | (our_id) \
10365942Sgibbs        | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
10465942Sgibbs
10565942Sgibbs#define SCB_GET_SIM(ahc, scb) \
10665942Sgibbs	(SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
10765942Sgibbs					  : (ahc)->platform_data->sim_b)
10865942Sgibbs
10976634Sgibbs#ifndef offsetof
11076634Sgibbs#define offsetof(type, member)  ((size_t)(&((type *)0)->member))
11176634Sgibbs#endif
11265942Sgibbs
11365942Sgibbs/************************ Tunable Driver Parameters  **************************/
11465942Sgibbs/*
11565942Sgibbs * The number of dma segments supported.  The sequencer can handle any number
11665942Sgibbs * of physically contiguous S/G entrys.  To reduce the driver's memory
11765942Sgibbs * consumption, we limit the number supported to be sufficient to handle
118218909Sbrucec * the largest mapping supported by the legacy kernel MAXPHYS setting of
119195534Sscottl * 128K.  This can be increased once some testing is done.  Assuming the
12065942Sgibbs * be the number of paged sized transfers in MAXPHYS plus an extra element
12165942Sgibbs * to handle any unaligned residual.  The sequencer fetches SG elements
12276634Sgibbs * in cacheline sized chucks, so make the number per-transaction an even
12376634Sgibbs * multiple of 16 which should align us on even the largest of cacheline
12476634Sgibbs * boundaries.
12565942Sgibbs */
126195534Sscottl#define AHC_MAXPHYS (128 * 1024)
127195534Sscottl#define AHC_NSEG (roundup(btoc(AHC_MAXPHYS) + 1, 16))
12865942Sgibbs
12965942Sgibbs/* This driver supports target mode */
13065942Sgibbs#define AHC_TARGET_MODE 1
13165942Sgibbs
13265942Sgibbs/************************** Softc/SCB Platform Data ***************************/
13365942Sgibbsstruct ahc_platform_data {
13465942Sgibbs	/*
13565942Sgibbs	 * Hooks into the XPT.
13665942Sgibbs	 */
13765942Sgibbs	struct	cam_sim		*sim;
13865942Sgibbs	struct	cam_sim		*sim_b;
13965942Sgibbs	struct	cam_path	*path;
14065942Sgibbs	struct	cam_path	*path_b;
14165942Sgibbs
14265942Sgibbs	int			 regs_res_type;
14365942Sgibbs	int			 regs_res_id;
14465942Sgibbs	int			 irq_res_type;
14565942Sgibbs	struct resource		*regs;
14665942Sgibbs	struct resource		*irq;
14765942Sgibbs	void			*ih;
14870204Sgibbs	eventhandler_tag	 eh;
149123579Sgibbs	struct proc		*recovery_thread;
150168807Sscottl	struct mtx		mtx;
15165942Sgibbs};
15265942Sgibbs
15365942Sgibbsstruct scb_platform_data {
15465942Sgibbs};
15565942Sgibbs
15665942Sgibbs/***************************** Core Includes **********************************/
157153110Sru#ifdef AHC_REG_PRETTY_PRINT
158102675Sgibbs#define AIC_DEBUG_REGISTERS 1
159102675Sgibbs#else
160102675Sgibbs#define AIC_DEBUG_REGISTERS 0
161102675Sgibbs#endif
162123579Sgibbs#define AIC_CORE_INCLUDE <dev/aic7xxx/aic7xxx.h>
163123579Sgibbs#define	AIC_LIB_PREFIX ahc
164123579Sgibbs#define	AIC_CONST_PREFIX AHC
165123579Sgibbs#include <dev/aic7xxx/aic_osm_lib.h>
16665942Sgibbs
16765942Sgibbs/*************************** Device Access ************************************/
16865942Sgibbs#define ahc_inb(ahc, port)				\
16965942Sgibbs	bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
17065942Sgibbs
17165942Sgibbs#define ahc_outb(ahc, port, value)			\
17265942Sgibbs	bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
17365942Sgibbs
17465942Sgibbs#define ahc_outsb(ahc, port, valp, count)		\
17565942Sgibbs	bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
17665942Sgibbs
17765942Sgibbs#define ahc_insb(ahc, port, valp, count)		\
17865942Sgibbs	bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
17965942Sgibbs
18065942Sgibbsstatic __inline void ahc_flush_device_writes(struct ahc_softc *);
18165942Sgibbs
18265942Sgibbsstatic __inline void
18365942Sgibbsahc_flush_device_writes(struct ahc_softc *ahc)
18465942Sgibbs{
18565942Sgibbs	/* XXX Is this sufficient for all architectures??? */
18665942Sgibbs	ahc_inb(ahc, INTSTAT);
18765942Sgibbs}
18865942Sgibbs
18965942Sgibbs/**************************** Locking Primitives ******************************/
19065942Sgibbs/* Lock protecting internal data structures */
19165942Sgibbsstatic __inline void ahc_lockinit(struct ahc_softc *);
192168807Sscottlstatic __inline void ahc_lock(struct ahc_softc *);
193168807Sscottlstatic __inline void ahc_unlock(struct ahc_softc *);
19465942Sgibbs
19565942Sgibbsstatic __inline void
19665942Sgibbsahc_lockinit(struct ahc_softc *ahc)
19765942Sgibbs{
198168807Sscottl	mtx_init(&ahc->platform_data->mtx, "ahc_lock", NULL, MTX_DEF);
19965942Sgibbs}
20065942Sgibbs
20165942Sgibbsstatic __inline void
202168807Sscottlahc_lock(struct ahc_softc *ahc)
20365942Sgibbs{
204168807Sscottl	mtx_lock(&ahc->platform_data->mtx);
20565942Sgibbs}
20665942Sgibbs
20765942Sgibbsstatic __inline void
208168807Sscottlahc_unlock(struct ahc_softc *ahc)
20965942Sgibbs{
210168807Sscottl	mtx_unlock(&ahc->platform_data->mtx);
21165942Sgibbs}
21265942Sgibbs
213133911Sgibbs/************************* Initialization/Teardown ****************************/
214133911Sgibbsint	  ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
215133911Sgibbsvoid	  ahc_platform_free(struct ahc_softc *ahc);
216133911Sgibbsint	  ahc_map_int(struct ahc_softc *ahc);
217133911Sgibbsint	  ahc_attach(struct ahc_softc *);
218133911Sgibbsint	  ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
219133911Sgibbsint	  ahc_detach(device_t);
220133911Sgibbs
22165942Sgibbs/********************************** PCI ***************************************/
222123579Sgibbs#ifdef AIC_PCI_CONFIG
223123579Sgibbsint ahc_pci_map_registers(struct ahc_softc *ahc);
224133911Sgibbs#define ahc_pci_map_int ahc_map_int
225123579Sgibbs#endif /*AIC_PCI_CONFIG*/
22665942Sgibbs
22765942Sgibbs/******************************** VL/EISA *************************************/
22895378Sgibbsint aic7770_map_registers(struct ahc_softc *ahc, u_int port);
229133911Sgibbsstatic __inline int aic7770_map_int(struct ahc_softc *, int);
23065942Sgibbs
231133911Sgibbsstatic __inline int
232133911Sgibbsaic7770_map_int(struct ahc_softc *ahc, int irq)
233133911Sgibbs{
234133911Sgibbs	/*
235133911Sgibbs	 * The IRQ is unused in the FreeBSD
236133911Sgibbs	 * implementation since the EISA and
237133911Sgibbs	 * ISA attachments register the IRQ
238133911Sgibbs	 * with newbus before the core is called.
239133911Sgibbs	 */
240133911Sgibbs	return ahc_map_int(ahc);
241133911Sgibbs}
242133911Sgibbs
24365942Sgibbs/********************************* Debug **************************************/
24465942Sgibbsstatic __inline void	ahc_print_path(struct ahc_softc *, struct scb *);
24565942Sgibbsstatic __inline void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
24665942Sgibbs
24765942Sgibbsstatic __inline void
24865942Sgibbsahc_print_path(struct ahc_softc *ahc, struct scb *scb)
24965942Sgibbs{
25065942Sgibbs	xpt_print_path(scb->io_ctx->ccb_h.path);
25165942Sgibbs}
25265942Sgibbs
25365942Sgibbsstatic __inline void
25465942Sgibbsahc_platform_dump_card_state(struct ahc_softc *ahc)
25565942Sgibbs{
25665942Sgibbs	/* Nothing to do here for FreeBSD */
25765942Sgibbs}
25865942Sgibbs/**************************** Transfer Settings *******************************/
25965942Sgibbsvoid	  ahc_notify_xfer_settings_change(struct ahc_softc *,
26065942Sgibbs					  struct ahc_devinfo *);
26165942Sgibbsvoid	  ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
26265942Sgibbs				int /*enable*/);
26365942Sgibbs
26470204Sgibbs/****************************** Interrupts ************************************/
26570204Sgibbsvoid			ahc_platform_intr(void *);
26670204Sgibbsstatic __inline void	ahc_platform_flushwork(struct ahc_softc *ahc);
26770204Sgibbsstatic __inline void
26870204Sgibbsahc_platform_flushwork(struct ahc_softc *ahc)
26970204Sgibbs{
27070204Sgibbs}
27170204Sgibbs
27265942Sgibbs/************************ Misc Function Declarations **************************/
27365942Sgibbsvoid	  ahc_done(struct ahc_softc *ahc, struct scb *scb);
27466269Sgibbsvoid	  ahc_send_async(struct ahc_softc *, char /*channel*/,
27576634Sgibbs			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
27665942Sgibbs#endif  /* _AIC7XXX_FREEBSD_H_ */
277