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