mlx_disk.c revision 111497
1/*-
2 * Copyright (c) 1999 Jonathan Lemon
3 * Copyright (c) 1999 Michael Smith
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/mlx/mlx_disk.c 111497 2003-02-25 20:35:56Z jhb $
28 */
29
30/*
31 * Disk driver for Mylex DAC960 RAID adapters.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37
38#include <sys/bus.h>
39#include <sys/conf.h>
40#include <sys/devicestat.h>
41#include <sys/disk.h>
42
43#include <machine/bus.h>
44#include <sys/rman.h>
45
46#include <dev/mlx/mlx_compat.h>
47#include <dev/mlx/mlxio.h>
48#include <dev/mlx/mlxvar.h>
49#include <dev/mlx/mlxreg.h>
50
51/* prototypes */
52static int mlxd_probe(device_t dev);
53static int mlxd_attach(device_t dev);
54static int mlxd_detach(device_t dev);
55
56devclass_t		mlxd_devclass;
57
58static device_method_t mlxd_methods[] = {
59    DEVMETHOD(device_probe,	mlxd_probe),
60    DEVMETHOD(device_attach,	mlxd_attach),
61    DEVMETHOD(device_detach,	mlxd_detach),
62    { 0, 0 }
63};
64
65static driver_t mlxd_driver = {
66    "mlxd",
67    mlxd_methods,
68    sizeof(struct mlxd_softc)
69};
70
71DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
72
73static int
74mlxd_open(struct disk *dp)
75{
76    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
77
78    debug_called(1);
79
80    if (sc == NULL)
81	return (ENXIO);
82
83    /* controller not active? */
84    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
85	return(ENXIO);
86
87    sc->mlxd_flags |= MLXD_OPEN;
88    return (0);
89}
90
91static int
92mlxd_close(struct disk *dp)
93{
94    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
95
96    debug_called(1);
97
98    if (sc == NULL)
99	return (ENXIO);
100    sc->mlxd_flags &= ~MLXD_OPEN;
101    return (0);
102}
103
104static int
105mlxd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
106{
107    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
108    int error;
109
110    debug_called(1);
111
112    if (sc == NULL)
113	return (ENXIO);
114
115    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
116	debug(0, "mlx_submit_ioctl returned %d\n", error);
117	return(error);
118    }
119    return (ENOTTY);
120}
121
122/*
123 * Read/write routine for a buffer.  Finds the proper unit, range checks
124 * arguments, and schedules the transfer.  Does not wait for the transfer
125 * to complete.  Multi-page transfers are supported.  All I/O requests must
126 * be a multiple of a sector in length.
127 */
128static void
129mlxd_strategy(mlx_bio *bp)
130{
131    struct mlxd_softc	*sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
132
133    debug_called(1);
134
135    /* bogus disk? */
136    if (sc == NULL) {
137	MLX_BIO_SET_ERROR(bp, EINVAL);
138	goto bad;
139    }
140
141    /* XXX may only be temporarily offline - sleep? */
142    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
143	MLX_BIO_SET_ERROR(bp, ENXIO);
144	goto bad;
145    }
146
147    MLX_BIO_STATS_START(bp);
148    mlx_submit_buf(sc->mlxd_controller, bp);
149    return;
150
151 bad:
152    /*
153     * Correctly set the bio to indicate a failed tranfer.
154     */
155    MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
156    MLX_BIO_DONE(bp);
157    return;
158}
159
160void
161mlxd_intr(void *data)
162{
163    mlx_bio 		*bp = (mlx_bio *)data;
164
165    debug_called(1);
166
167    if (MLX_BIO_HAS_ERROR(bp))
168	MLX_BIO_SET_ERROR(bp, EIO);
169    else
170	MLX_BIO_RESID(bp) = 0;
171
172    MLX_BIO_STATS_END(bp);
173    MLX_BIO_DONE(bp);
174}
175
176static int
177mlxd_probe(device_t dev)
178{
179
180    debug_called(1);
181
182    device_set_desc(dev, "Mylex System Drive");
183    return (0);
184}
185
186static int
187mlxd_attach(device_t dev)
188{
189    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
190    device_t		parent;
191    char		*state;
192    int			s1, s2;
193
194    debug_called(1);
195
196    parent = device_get_parent(dev);
197    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
198    sc->mlxd_unit = device_get_unit(dev);
199    sc->mlxd_drive = device_get_ivars(dev);
200    sc->mlxd_dev = dev;
201
202    switch(sc->mlxd_drive->ms_state) {
203    case MLX_SYSD_ONLINE:
204	state = "online";
205	break;
206    case MLX_SYSD_CRITICAL:
207	state = "critical";
208	break;
209    case MLX_SYSD_OFFLINE:
210	state = "offline";
211	break;
212    default:
213	state = "unknown state";
214    }
215
216    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
217		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
218		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
219
220    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
221		      DEVSTAT_NO_ORDERED_TAGS,
222		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
223		      DEVSTAT_PRIORITY_ARRAY);
224
225    sc->mlxd_disk.d_open = mlxd_open;
226    sc->mlxd_disk.d_close = mlxd_close;
227    sc->mlxd_disk.d_ioctl = mlxd_ioctl;
228    sc->mlxd_disk.d_strategy = mlxd_strategy;
229    sc->mlxd_disk.d_name = "mlxd";
230    sc->mlxd_disk.d_drv1 = sc;
231    sc->mlxd_disk.d_sectorsize = MLX_BLKSIZE;
232    sc->mlxd_disk.d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
233    sc->mlxd_disk.d_fwsectors = sc->mlxd_drive->ms_sectors;
234    sc->mlxd_disk.d_fwheads = sc->mlxd_drive->ms_heads;
235
236    /*
237     * Set maximum I/O size to the lesser of the recommended maximum and the practical
238     * maximum.
239     */
240    s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
241    s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
242    sc->mlxd_disk.d_maxsize = imin(s1, s2);
243
244    disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, NULL, NULL);
245
246    return (0);
247}
248
249static int
250mlxd_detach(device_t dev)
251{
252    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
253
254    debug_called(1);
255
256    devstat_remove_entry(&sc->mlxd_stats);
257    disk_destroy(&sc->mlxd_disk);
258
259    return(0);
260}
261
262