167555Smsmith/*-
267555Smsmith * Copyright (c) 2000 Michael Smith
3123103Sps * Copyright (c) 2003 Paul Saab
4123103Sps * Copyright (c) 2003 Vinod Kashyap
567555Smsmith * Copyright (c) 2000 BSDi
667555Smsmith * All rights reserved.
767555Smsmith *
867555Smsmith * Redistribution and use in source and binary forms, with or without
967555Smsmith * modification, are permitted provided that the following conditions
1067555Smsmith * are met:
1167555Smsmith * 1. Redistributions of source code must retain the above copyright
1267555Smsmith *    notice, this list of conditions and the following disclaimer.
1367555Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1467555Smsmith *    notice, this list of conditions and the following disclaimer in the
1567555Smsmith *    documentation and/or other materials provided with the distribution.
1667555Smsmith *
1767555Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1867555Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1967555Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2067555Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2167555Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2267555Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2367555Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2467555Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2567555Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2667555Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2767555Smsmith * SUCH DAMAGE.
2867555Smsmith *
2967555Smsmith * $FreeBSD$
3067555Smsmith */
3167555Smsmith/*
3267555Smsmith * Portability and compatibility interfaces.
3367555Smsmith */
3467555Smsmith
3567555Smsmith#ifdef __FreeBSD__
3667555Smsmith/******************************************************************************
3767555Smsmith * FreeBSD
3867555Smsmith */
3967555Smsmith#define TWE_SUPPORTED_PLATFORM
4067555Smsmith
4167555Smsmith#include <sys/param.h>
42118816Sps#include <sys/endian.h>
4367555Smsmith#include <sys/systm.h>
4467555Smsmith#include <sys/malloc.h>
4567555Smsmith#include <sys/kernel.h>
46239244Sjhb#include <sys/lock.h>
47129879Sphk#include <sys/module.h>
48239244Sjhb#include <sys/mutex.h>
49118508Sps#include <sys/sysctl.h>
50239244Sjhb#include <sys/sx.h>
5167555Smsmith
52240209Sjhb#include <sys/bio.h>
5367555Smsmith#include <sys/bus.h>
5467555Smsmith#include <sys/conf.h>
5567555Smsmith#include <sys/disk.h>
5667555Smsmith#include <sys/stat.h>
5767555Smsmith
5867555Smsmith#include <machine/bus.h>
5967555Smsmith#include <machine/resource.h>
6067555Smsmith#include <sys/rman.h>
6167555Smsmith
62119287Simp#include <dev/pci/pcireg.h>
63119287Simp#include <dev/pci/pcivar.h>
6467555Smsmith
65240209Sjhb#include <geom/geom_disk.h>
66240209Sjhb
6767555Smsmith#define TWE_DRIVER_NAME		twe
6867555Smsmith#define TWED_DRIVER_NAME	twed
6967555Smsmith#define TWE_MALLOC_CLASS	M_TWE
7067555Smsmith
7167555Smsmith/*
7267555Smsmith * Wrappers for bus-space actions
7367555Smsmith */
74239244Sjhb#define TWE_CONTROL(sc, val)		bus_write_4((sc)->twe_io, 0x0, (u_int32_t)val)
75239244Sjhb#define TWE_STATUS(sc)			(u_int32_t)bus_read_4((sc)->twe_io, 0x4)
76239244Sjhb#define TWE_COMMAND_QUEUE(sc, val)	bus_write_4((sc)->twe_io, 0x8, (u_int32_t)val)
77239244Sjhb#define TWE_RESPONSE_QUEUE(sc)		(TWE_Response_Queue)bus_read_4((sc)->twe_io, 0xc)
7867555Smsmith
7967555Smsmith/*
8067555Smsmith * FreeBSD-specific softc elements
8167555Smsmith */
8267555Smsmith#define TWE_PLATFORM_SOFTC								\
83118816Sps    bus_dmamap_t		twe_cmdmap;	/* DMA map for command */				\
84118816Sps    u_int32_t			twe_cmdphys;	/* address of command in controller space */		\
8567555Smsmith    device_t			twe_dev;		/* bus device */		\
86130585Sphk    struct cdev *twe_dev_t;		/* control device */		\
8767555Smsmith    struct resource		*twe_io;		/* register interface window */	\
8867555Smsmith    bus_dma_tag_t		twe_parent_dmat;	/* parent DMA tag */		\
8967555Smsmith    bus_dma_tag_t		twe_buffer_dmat;	/* data buffer DMA tag */	\
90118816Sps    bus_dma_tag_t		twe_cmd_dmat;		/* command buffer DMA tag */	\
91118816Sps    bus_dma_tag_t		twe_immediate_dmat;	/* command buffer DMA tag */	\
9267555Smsmith    struct resource		*twe_irq;		/* interrupt */			\
9367555Smsmith    void			*twe_intr;		/* interrupt handle */		\
94118508Sps    struct intr_config_hook	twe_ich;		/* delayed-startup hook */	\
95118816Sps    void			*twe_cmd;		/* command structures */	\
96118816Sps    void			*twe_immediate;		/* immediate commands */	\
97118816Sps    bus_dmamap_t		twe_immediate_map;					\
98239244Sjhb    struct mtx			twe_io_lock;						\
99239244Sjhb    struct sx			twe_config_lock;
10067555Smsmith
10167555Smsmith/*
10267555Smsmith * FreeBSD-specific request elements
10367555Smsmith */
10467555Smsmith#define TWE_PLATFORM_REQUEST										\
10567555Smsmith    bus_dmamap_t		tr_dmamap;	/* DMA map for data */					\
10667555Smsmith    u_int32_t			tr_dataphys;	/* data buffer base address in controller space */
10767555Smsmith
10867555Smsmith/*
10967555Smsmith * Output identifying the controller/disk
11067555Smsmith */
11167555Smsmith#define twe_printf(sc, fmt, args...)	device_printf(sc->twe_dev, fmt , ##args)
11267555Smsmith#define twed_printf(twed, fmt, args...)	device_printf(twed->twed_dev, fmt , ##args)
11367555Smsmith
114239244Sjhb#define	TWE_IO_LOCK(sc)			mtx_lock(&(sc)->twe_io_lock)
115239244Sjhb#define	TWE_IO_UNLOCK(sc)		mtx_unlock(&(sc)->twe_io_lock)
116239244Sjhb#define	TWE_IO_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->twe_io_lock, MA_OWNED)
117239244Sjhb#define	TWE_CONFIG_LOCK(sc)		sx_xlock(&(sc)->twe_config_lock)
118239244Sjhb#define	TWE_CONFIG_UNLOCK(sc)		sx_xunlock(&(sc)->twe_config_lock)
119239244Sjhb#define	TWE_CONFIG_ASSERT_LOCKED(sc)	sx_assert(&(sc)->twe_config_lock, SA_XLOCKED)
120239244Sjhb
12167555Smsmith#endif /* FreeBSD */
12267555Smsmith
12367555Smsmith#ifndef TWE_SUPPORTED_PLATFORM
12467555Smsmith#error platform not supported
12567555Smsmith#endif
126