1/*-
2 * Copyright (c) 1999 Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34
35#include <sys/bus.h>
36#include <sys/conf.h>
37
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41
42#include <geom/geom_disk.h>
43
44#include <dev/pci/pcireg.h>
45#include <dev/pci/pcivar.h>
46
47#include <dev/mlx/mlx_compat.h>
48#include <dev/mlx/mlxio.h>
49#include <dev/mlx/mlxvar.h>
50#include <dev/mlx/mlxreg.h>
51
52static int			mlx_pci_probe(device_t dev);
53static int			mlx_pci_attach(device_t dev);
54
55static device_method_t mlx_methods[] = {
56    /* Device interface */
57    DEVMETHOD(device_probe,	mlx_pci_probe),
58    DEVMETHOD(device_attach,	mlx_pci_attach),
59    DEVMETHOD(device_detach,	mlx_detach),
60    DEVMETHOD(device_shutdown,	mlx_shutdown),
61    DEVMETHOD(device_suspend,	mlx_suspend),
62    DEVMETHOD(device_resume,	mlx_resume),
63
64    DEVMETHOD_END
65};
66
67static driver_t mlx_pci_driver = {
68	"mlx",
69	mlx_methods,
70	sizeof(struct mlx_softc)
71};
72
73DRIVER_MODULE(mlx, pci, mlx_pci_driver, mlx_devclass, 0, 0);
74
75struct mlx_ident
76{
77    u_int16_t	vendor;
78    u_int16_t	device;
79    u_int16_t	subvendor;
80    u_int16_t	subdevice;
81    int		iftype;
82    char	*desc;
83} mlx_identifiers[] = {
84    {0x1069, 0x0001, 0x0000, 0x0000, MLX_IFTYPE_2, "Mylex version 2 RAID interface"},
85    {0x1069, 0x0002, 0x0000, 0x0000, MLX_IFTYPE_3, "Mylex version 3 RAID interface"},
86    {0x1069, 0x0010, 0x0000, 0x0000, MLX_IFTYPE_4, "Mylex version 4 RAID interface"},
87    {0x1011, 0x1065, 0x1069, 0x0020, MLX_IFTYPE_5, "Mylex version 5 RAID interface"},
88    {0, 0, 0, 0, 0, 0}
89};
90
91static int
92mlx_pci_probe(device_t dev)
93{
94    struct mlx_ident	*m;
95
96    debug_called(1);
97
98    for (m = mlx_identifiers; m->vendor != 0; m++) {
99	if ((m->vendor == pci_get_vendor(dev)) &&
100	    (m->device == pci_get_device(dev)) &&
101	    ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
102				     (m->subdevice == pci_get_subdevice(dev))))) {
103
104	    device_set_desc(dev, m->desc);
105	    return(BUS_PROBE_DEFAULT);
106	}
107    }
108    return(ENXIO);
109}
110
111static int
112mlx_pci_attach(device_t dev)
113{
114    struct mlx_softc	*sc;
115    int			i, error;
116
117    debug_called(1);
118
119    /* force the busmaster enable bit on */
120    pci_enable_busmaster(dev);
121
122    /*
123     * Initialise softc.
124     */
125    sc = device_get_softc(dev);
126    bzero(sc, sizeof(*sc));
127    sc->mlx_dev = dev;
128
129    /*
130     * Work out what sort of adapter this is (we need to know this in order
131     * to map the appropriate interface resources).
132     */
133    sc->mlx_iftype = 0;
134    for (i = 0; mlx_identifiers[i].vendor != 0; i++) {
135	if ((mlx_identifiers[i].vendor == pci_get_vendor(dev)) &&
136	    (mlx_identifiers[i].device == pci_get_device(dev))) {
137	    sc->mlx_iftype = mlx_identifiers[i].iftype;
138	    break;
139	}
140    }
141    if (sc->mlx_iftype == 0)		/* shouldn't happen */
142	return(ENXIO);
143
144    /*
145     * Allocate the PCI register window.
146     */
147
148    /* type 2/3 adapters have an I/O region we don't prefer at base 0 */
149    switch(sc->mlx_iftype) {
150    case MLX_IFTYPE_2:
151    case MLX_IFTYPE_3:
152	sc->mlx_mem_type = SYS_RES_MEMORY;
153	sc->mlx_mem_rid = MLX_CFG_BASE1;
154	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
155		&sc->mlx_mem_rid, RF_ACTIVE);
156	if (sc->mlx_mem == NULL) {
157	    sc->mlx_mem_type = SYS_RES_IOPORT;
158	    sc->mlx_mem_rid = MLX_CFG_BASE0;
159	    sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
160		&sc->mlx_mem_rid, RF_ACTIVE);
161	}
162	break;
163    case MLX_IFTYPE_4:
164    case MLX_IFTYPE_5:
165	sc->mlx_mem_type = SYS_RES_MEMORY;
166	sc->mlx_mem_rid = MLX_CFG_BASE0;
167	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
168		&sc->mlx_mem_rid, RF_ACTIVE);
169	break;
170    }
171    if (sc->mlx_mem == NULL) {
172	device_printf(sc->mlx_dev, "couldn't allocate mailbox window\n");
173	mlx_free(sc);
174	return(ENXIO);
175    }
176    sc->mlx_btag = rman_get_bustag(sc->mlx_mem);
177    sc->mlx_bhandle = rman_get_bushandle(sc->mlx_mem);
178
179    /*
180     * Allocate the parent bus DMA tag appropriate for PCI.
181     */
182    error = bus_dma_tag_create(bus_get_dma_tag(dev),	/* PCI parent */
183			       1, 0, 			/* alignment, boundary */
184			       BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
185			       BUS_SPACE_MAXADDR, 	/* highaddr */
186			       NULL, NULL, 		/* filter, filterarg */
187			       MAXBSIZE, MLX_NSEG,	/* maxsize, nsegments */
188			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
189			       BUS_DMA_ALLOCNOW,	/* flags */
190			       NULL,			/* lockfunc */
191			       NULL,			/* lockarg */
192			       &sc->mlx_parent_dmat);
193    if (error != 0) {
194	device_printf(dev, "can't allocate parent DMA tag\n");
195	mlx_free(sc);
196	return(ENOMEM);
197    }
198
199    /*
200     * Do bus-independant initialisation.
201     */
202    error = mlx_attach(sc);
203    if (error != 0) {
204	mlx_free(sc);
205	return(error);
206    }
207
208    /*
209     * Start the controller.
210     */
211    mlx_startup(sc);
212    return(0);
213}
214