xref: /freebsd/sys/dev/mlx/mlx_pci.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  *	$FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33 
34 #include <sys/bus.h>
35 #include <sys/buf.h>
36 #include <sys/conf.h>
37 #include <sys/devicestat.h>
38 #include <sys/disk.h>
39 
40 #include <machine/bus_memio.h>
41 #include <machine/bus_pio.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45 
46 #include <pci/pcireg.h>
47 #include <pci/pcivar.h>
48 
49 #include <dev/mlx/mlxio.h>
50 #include <dev/mlx/mlxvar.h>
51 
52 #if 0
53 #define debug(fmt, args...)	printf("%s: " fmt "\n", __FUNCTION__ , ##args)
54 #else
55 #define debug(fmt, args...)
56 #endif
57 
58 static int			mlx_pci_probe(device_t dev);
59 static int			mlx_pci_attach(device_t dev);
60 
61 static device_method_t mlx_methods[] = {
62     /* Device interface */
63     DEVMETHOD(device_probe,	mlx_pci_probe),
64     DEVMETHOD(device_attach,	mlx_pci_attach),
65     DEVMETHOD(device_detach,	mlx_detach),
66     DEVMETHOD(device_shutdown,	mlx_shutdown),
67     DEVMETHOD(device_suspend,	mlx_suspend),
68     DEVMETHOD(device_resume,	mlx_resume),
69 
70     DEVMETHOD(bus_print_child,	bus_generic_print_child),
71     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
72     { 0, 0 }
73 };
74 
75 static driver_t mlx_pci_driver = {
76 	"mlx",
77 	mlx_methods,
78 	sizeof(struct mlx_softc)
79 };
80 
81 DRIVER_MODULE(mlx, pci, mlx_pci_driver, mlx_devclass, 0, 0);
82 
83 struct
84 {
85     u_int16_t	vendor;
86     u_int16_t	device;
87     int		iftype;
88     char	*desc;
89 } mlx_identifiers[] = {
90     {0x1069, 0x0002, MLX_IFTYPE_3, "Mylex version 3 RAID interface"},	/* Mylex v3 software interface */
91     {0x1069, 0x0010, MLX_IFTYPE_4, "Mylex version 4 RAID interface"},	/* Mylex v4 software interface */
92     {0, 0, 0, 0}
93 };
94 
95 static int
96 mlx_pci_probe(device_t dev)
97 {
98     int		i;
99 
100     debug("called");
101 
102     for (i = 0; mlx_identifiers[i].vendor != 0; i++) {
103 	if ((mlx_identifiers[i].vendor == pci_get_vendor(dev)) &&
104 	    (mlx_identifiers[i].device == pci_get_device(dev))) {
105 
106 	    device_set_desc(dev, mlx_identifiers[i].desc);
107 	    return(0);
108 	}
109     }
110     return(ENXIO);
111 }
112 
113 static int
114 mlx_pci_attach(device_t dev)
115 {
116     struct mlx_softc	*sc;
117     int			i, rid, error;
118     u_int32_t		command;
119 
120     debug("called");
121 
122     /*
123      * Make sure we are going to be able to talk to this board.
124      */
125     command = pci_read_config(dev, PCIR_COMMAND, 1);
126     if ((command & PCIM_CMD_MEMEN) == 0) {
127 	device_printf(dev, "memory window not available\n");
128 	return(ENXIO);
129     }
130 
131     /*
132      * Initialise softc.
133      */
134     sc = device_get_softc(dev);
135     bzero(sc, sizeof(*sc));
136     sc->mlx_dev = dev;
137 
138     /*
139      * Work out what sort of adapter this is (we need to know this in order
140      * to map the appropriate interface resources).
141      */
142     sc->mlx_iftype = 0;
143     for (i = 0; mlx_identifiers[i].vendor != 0; i++) {
144 	if ((mlx_identifiers[i].vendor == pci_get_vendor(dev)) &&
145 	    (mlx_identifiers[i].device == pci_get_device(dev))) {
146 	    sc->mlx_iftype = mlx_identifiers[i].iftype;
147 	    break;
148 	}
149     }
150     if (sc->mlx_iftype == 0)		/* shouldn't happen */
151 	return(ENXIO);
152 
153     /*
154      * Allocate the PCI register window.
155      */
156 
157     /* type 3 adapters have an I/O region we don't use at base 0 */
158     rid = (sc->mlx_iftype == MLX_IFTYPE_3) ? MLX_CFG_BASE1 : MLX_CFG_BASE0;
159     sc->mlx_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1, RF_ACTIVE);
160     if (sc->mlx_mem == NULL) {
161 	device_printf(sc->mlx_dev, "couldn't allocate mailbox window\n");
162 	mlx_free(sc);
163 	return(ENXIO);
164     }
165     sc->mlx_btag = rman_get_bustag(sc->mlx_mem);
166     sc->mlx_bhandle = rman_get_bushandle(sc->mlx_mem);
167 
168     /*
169      * Allocate the parent bus DMA tag appropriate for PCI.
170      */
171     error = bus_dma_tag_create(NULL, 			/* parent */
172 			       1, 0, 			/* alignment, boundary */
173 			       BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
174 			       BUS_SPACE_MAXADDR, 	/* highaddr */
175 			       NULL, NULL, 		/* filter, filterarg */
176 			       MAXBSIZE, MLX_NSEG,	/* maxsize, nsegments */
177 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
178 			       BUS_DMA_ALLOCNOW,	/* flags */
179 			       &sc->mlx_parent_dmat);
180     if (error != 0) {
181 	device_printf(dev, "can't allocate parent DMA tag\n");
182 	mlx_free(sc);
183 	return(ENOMEM);
184     }
185 
186     /*
187      * Do bus-independant initialisation.
188      */
189     error = mlx_attach(sc);
190     if (error != 0) {
191 	mlx_free(sc);
192 	return(error);
193     }
194 
195     /*
196      * Start the controller.
197      */
198     mlx_startup(sc);
199     return(0);
200 }
201