mlx_disk.c revision 76322
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 76322 2001-05-06 20:00:03Z phk $
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/bio.h>
39#include <sys/bus.h>
40#include <sys/conf.h>
41#include <sys/devicestat.h>
42#include <sys/disk.h>
43
44#include <machine/bus.h>
45#include <sys/rman.h>
46
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
56static	d_open_t	mlxd_open;
57static	d_close_t	mlxd_close;
58static	d_strategy_t	mlxd_strategy;
59static	d_ioctl_t	mlxd_ioctl;
60
61#define MLXD_CDEV_MAJOR	131
62
63static struct cdevsw mlxd_cdevsw = {
64		/* open */	mlxd_open,
65		/* close */	mlxd_close,
66		/* read */	physread,
67		/* write */	physwrite,
68		/* ioctl */	mlxd_ioctl,
69		/* poll */	nopoll,
70		/* mmap */	nommap,
71		/* strategy */	mlxd_strategy,
72		/* name */ 	"mlxd",
73		/* maj */	MLXD_CDEV_MAJOR,
74		/* dump */	nodump,
75		/* psize */ 	nopsize,
76		/* flags */	D_DISK,
77};
78
79devclass_t		mlxd_devclass;
80static struct cdevsw	mlxddisk_cdevsw;
81
82static device_method_t mlxd_methods[] = {
83    DEVMETHOD(device_probe,	mlxd_probe),
84    DEVMETHOD(device_attach,	mlxd_attach),
85    DEVMETHOD(device_detach,	mlxd_detach),
86    { 0, 0 }
87};
88
89static driver_t mlxd_driver = {
90    "mlxd",
91    mlxd_methods,
92    sizeof(struct mlxd_softc)
93};
94
95DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
96
97static int
98mlxd_open(dev_t dev, int flags, int fmt, struct proc *p)
99{
100    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
101    struct disklabel	*label;
102
103    debug_called(1);
104
105    if (sc == NULL)
106	return (ENXIO);
107
108    /* controller not active? */
109    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
110	return(ENXIO);
111
112    label = &sc->mlxd_disk.d_label;
113    bzero(label, sizeof(*label));
114    label->d_type = DTYPE_SCSI;
115    label->d_secsize    = MLX_BLKSIZE;
116    label->d_nsectors   = sc->mlxd_drive->ms_sectors;
117    label->d_ntracks    = sc->mlxd_drive->ms_heads;
118    label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
119    label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
120    label->d_secperunit = sc->mlxd_drive->ms_size;
121
122    sc->mlxd_flags |= MLXD_OPEN;
123    return (0);
124}
125
126static int
127mlxd_close(dev_t dev, int flags, int fmt, struct proc *p)
128{
129    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
130
131    debug_called(1);
132
133    if (sc == NULL)
134	return (ENXIO);
135    sc->mlxd_flags &= ~MLXD_OPEN;
136    return (0);
137}
138
139static int
140mlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
141{
142    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
143    int error;
144
145    debug_called(1);
146
147    if (sc == NULL)
148	return (ENXIO);
149
150    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
151	debug(0, "mlx_submit_ioctl returned %d\n", error);
152	return(error);
153    }
154    return (ENOTTY);
155}
156
157/*
158 * Read/write routine for a buffer.  Finds the proper unit, range checks
159 * arguments, and schedules the transfer.  Does not wait for the transfer
160 * to complete.  Multi-page transfers are supported.  All I/O requests must
161 * be a multiple of a sector in length.
162 */
163static void
164mlxd_strategy(struct bio *bp)
165{
166    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->bio_dev->si_drv1;
167
168    debug_called(1);
169
170    /* bogus disk? */
171    if (sc == NULL) {
172	bp->bio_error = EINVAL;
173	goto bad;
174    }
175
176    /* XXX may only be temporarily offline - sleep? */
177    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
178	bp->bio_error = ENXIO;
179	goto bad;
180    }
181
182    /* do-nothing operation */
183    if (bp->bio_bcount == 0)
184	goto done;
185
186    devstat_start_transaction(&sc->mlxd_stats);
187    mlx_submit_buf(sc->mlxd_controller, bp);
188    return;
189
190 bad:
191    bp->bio_flags |= BIO_ERROR;
192
193 done:
194    /*
195     * Correctly set the buf to indicate a completed transfer
196     */
197    bp->bio_resid = bp->bio_bcount;
198    biodone(bp);
199    return;
200}
201
202void
203mlxd_intr(void *data)
204{
205    struct bio *bp = (struct bio *)data;
206    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->bio_dev->si_drv1;
207
208    debug_called(1);
209
210    if (bp->bio_flags & BIO_ERROR)
211	bp->bio_error = EIO;
212    else
213	bp->bio_resid = 0;
214
215    biofinish(bp, &sc->mlxd_stats, 0);
216}
217
218static int
219mlxd_probe(device_t dev)
220{
221
222    debug_called(1);
223
224    device_set_desc(dev, "Mylex System Drive");
225    return (0);
226}
227
228static int
229mlxd_attach(device_t dev)
230{
231    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
232    device_t		parent;
233    char		*state;
234    dev_t		dsk;
235    int			s1, s2;
236
237    debug_called(1);
238
239    parent = device_get_parent(dev);
240    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
241    sc->mlxd_unit = device_get_unit(dev);
242    sc->mlxd_drive = device_get_ivars(dev);
243    sc->mlxd_dev = dev;
244
245    switch(sc->mlxd_drive->ms_state) {
246    case MLX_SYSD_ONLINE:
247	state = "online";
248	break;
249    case MLX_SYSD_CRITICAL:
250	state = "critical";
251	break;
252    case MLX_SYSD_OFFLINE:
253	state = "offline";
254	break;
255    default:
256	state = "unknown state";
257    }
258
259    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
260		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
261		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
262
263    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
264		      DEVSTAT_NO_ORDERED_TAGS,
265		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
266		      DEVSTAT_PRIORITY_ARRAY);
267
268    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
269    dsk->si_drv1 = sc;
270    sc->mlxd_dev_t = dsk;
271
272    /*
273     * Set maximum I/O size to the lesser of the recommended maximum and the practical
274     * maximum.
275     */
276    s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
277    s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
278    dsk->si_iosize_max = imin(s1, s2);
279
280    return (0);
281}
282
283static int
284mlxd_detach(device_t dev)
285{
286    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
287
288    debug_called(1);
289
290    devstat_remove_entry(&sc->mlxd_stats);
291    disk_destroy(sc->mlxd_dev_t);
292
293    return(0);
294}
295
296