mlx_disk.c revision 67164
1117397Skan/*-
2117397Skan * Copyright (c) 1999 Jonathan Lemon
3117397Skan * Copyright (c) 1999 Michael Smith
4117397Skan * All rights reserved.
5117397Skan *
6117397Skan * Redistribution and use in source and binary forms, with or without
7117397Skan * modification, are permitted provided that the following conditions
8117397Skan * are met:
9117397Skan * 1. Redistributions of source code must retain the above copyright
10117397Skan *    notice, this list of conditions and the following disclaimer.
11117397Skan * 2. Redistributions in binary form must reproduce the above copyright
12117397Skan *    notice, this list of conditions and the following disclaimer in the
13117397Skan *    documentation and/or other materials provided with the distribution.
14117397Skan *
15117397Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16117397Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17117397Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19117397Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20117397Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21117397Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22117397Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23117397Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24117397Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25117397Skan * SUCH DAMAGE.
26117397Skan *
27117397Skan * $FreeBSD: head/sys/dev/mlx/mlx_disk.c 67164 2000-10-15 14:19:01Z phk $
28117397Skan */
29117397Skan
30169691Skan/*
31169691Skan * Disk driver for Mylex DAC960 RAID adapters.
32169691Skan */
33169691Skan
34169691Skan#include <sys/param.h>
35117397Skan#include <sys/systm.h>
36117397Skan#include <sys/kernel.h>
37117397Skan
38117397Skan#include <sys/bio.h>
39117397Skan#include <sys/bus.h>
40117397Skan#include <sys/conf.h>
41117397Skan#include <sys/devicestat.h>
42117397Skan#include <sys/disk.h>
43117397Skan
44117397Skan#include <machine/bus.h>
45117397Skan#include <sys/rman.h>
46117397Skan
47117397Skan#include <dev/mlx/mlxio.h>
48117397Skan#include <dev/mlx/mlxvar.h>
49169691Skan#include <dev/mlx/mlxreg.h>
50169691Skan
51117397Skan/* prototypes */
52117397Skanstatic int mlxd_probe(device_t dev);
53117397Skanstatic int mlxd_attach(device_t dev);
54117397Skanstatic int mlxd_detach(device_t dev);
55117397Skan
56117397Skanstatic	d_open_t	mlxd_open;
57117397Skanstatic	d_close_t	mlxd_close;
58117397Skanstatic	d_strategy_t	mlxd_strategy;
59132720Skanstatic	d_ioctl_t	mlxd_ioctl;
60132720Skan
61122182Skan#define MLXD_BDEV_MAJOR	27
62132720Skan#define MLXD_CDEV_MAJOR	131
63117397Skan
64132720Skanstatic struct cdevsw mlxd_cdevsw = {
65117397Skan		/* open */	mlxd_open,
66117397Skan		/* close */	mlxd_close,
67117397Skan		/* read */	physread,
68117397Skan		/* write */	physwrite,
69117397Skan		/* ioctl */	mlxd_ioctl,
70117397Skan		/* poll */	nopoll,
71117397Skan		/* mmap */	nommap,
72117397Skan		/* strategy */	mlxd_strategy,
73117397Skan		/* name */ 	"mlxd",
74117397Skan		/* maj */	MLXD_CDEV_MAJOR,
75117397Skan		/* dump */	nodump,
76117397Skan		/* psize */ 	nopsize,
77117397Skan		/* flags */	D_DISK,
78117397Skan		/* bmaj */	MLXD_BDEV_MAJOR
79117397Skan};
80117397Skan
81117397Skandevclass_t		mlxd_devclass;
82117397Skanstatic struct cdevsw	mlxddisk_cdevsw;
83117397Skan
84117397Skanstatic device_method_t mlxd_methods[] = {
85117397Skan    DEVMETHOD(device_probe,	mlxd_probe),
86117397Skan    DEVMETHOD(device_attach,	mlxd_attach),
87117397Skan    DEVMETHOD(device_detach,	mlxd_detach),
88117397Skan    { 0, 0 }
89117397Skan};
90117397Skan
91117397Skanstatic driver_t mlxd_driver = {
92117397Skan    "mlxd",
93117397Skan    mlxd_methods,
94117397Skan    sizeof(struct mlxd_softc)
95117397Skan};
96117397Skan
97117397SkanDRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
98117397Skan
99117397Skanstatic int
100117397Skanmlxd_open(dev_t dev, int flags, int fmt, struct proc *p)
101117397Skan{
102117397Skan    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
103117397Skan    struct disklabel	*label;
104117397Skan
105117397Skan    debug_called(1);
106117397Skan
107132720Skan    if (sc == NULL)
108117397Skan	return (ENXIO);
109117397Skan
110132720Skan    /* controller not active? */
111117397Skan    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
112117397Skan	return(ENXIO);
113117397Skan
114117397Skan    label = &sc->mlxd_disk.d_label;
115117397Skan    bzero(label, sizeof(*label));
116117397Skan    label->d_type = DTYPE_SCSI;
117117397Skan    label->d_secsize    = MLX_BLKSIZE;
118117397Skan    label->d_nsectors   = sc->mlxd_drive->ms_sectors;
119117397Skan    label->d_ntracks    = sc->mlxd_drive->ms_heads;
120117397Skan    label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
121117397Skan    label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
122117397Skan    label->d_secperunit = sc->mlxd_drive->ms_size;
123117397Skan
124132720Skan    sc->mlxd_flags |= MLXD_OPEN;
125117397Skan    return (0);
126117397Skan}
127117397Skan
128117397Skanstatic int
129122182Skanmlxd_close(dev_t dev, int flags, int fmt, struct proc *p)
130122182Skan{
131117397Skan    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
132132720Skan
133117397Skan    debug_called(1);
134117397Skan
135117397Skan    if (sc == NULL)
136117397Skan	return (ENXIO);
137117397Skan    sc->mlxd_flags &= ~MLXD_OPEN;
138117397Skan    return (0);
139117397Skan}
140117397Skan
141117397Skanstatic int
142117397Skanmlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
143117397Skan{
144117397Skan    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
145117397Skan    int error;
146117397Skan
147117397Skan    debug_called(1);
148117397Skan
149117397Skan    if (sc == NULL)
150117397Skan	return (ENXIO);
151117397Skan
152117397Skan    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
153117397Skan	debug(0, "mlx_submit_ioctl returned %d\n", error);
154117397Skan	return(error);
155117397Skan    }
156117397Skan    return (ENOTTY);
157117397Skan}
158117397Skan
159117397Skan/*
160117397Skan * Read/write routine for a buffer.  Finds the proper unit, range checks
161117397Skan * arguments, and schedules the transfer.  Does not wait for the transfer
162117397Skan * to complete.  Multi-page transfers are supported.  All I/O requests must
163117397Skan * be a multiple of a sector in length.
164117397Skan */
165117397Skanstatic void
166117397Skanmlxd_strategy(struct bio *bp)
167117397Skan{
168117397Skan    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->bio_dev->si_drv1;
169169691Skan
170169691Skan    debug_called(1);
171
172    /* bogus disk? */
173    if (sc == NULL) {
174	bp->bio_error = EINVAL;
175	goto bad;
176    }
177
178    /* XXX may only be temporarily offline - sleep? */
179    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
180	bp->bio_error = ENXIO;
181	goto bad;
182    }
183
184    /* do-nothing operation */
185    if (bp->bio_bcount == 0)
186	goto done;
187
188    devstat_start_transaction(&sc->mlxd_stats);
189    mlx_submit_buf(sc->mlxd_controller, bp);
190    return;
191
192 bad:
193    bp->bio_flags |= BIO_ERROR;
194
195 done:
196    /*
197     * Correctly set the buf to indicate a completed transfer
198     */
199    bp->bio_resid = bp->bio_bcount;
200    biodone(bp);
201    return;
202}
203
204void
205mlxd_intr(void *data)
206{
207    struct bio *bp = (struct bio *)data;
208    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->bio_dev->si_drv1;
209
210    debug_called(1);
211
212    if (bp->bio_flags & BIO_ERROR)
213	bp->bio_error = EIO;
214    else
215	bp->bio_resid = 0;
216
217    devstat_end_transaction_bio(&sc->mlxd_stats, bp);
218    biodone(bp);
219}
220
221static int
222mlxd_probe(device_t dev)
223{
224
225    debug_called(1);
226
227    device_set_desc(dev, "Mylex System Drive");
228    return (0);
229}
230
231static int
232mlxd_attach(device_t dev)
233{
234    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
235    device_t		parent;
236    char		*state;
237    dev_t		dsk;
238    int			s1, s2;
239
240    debug_called(1);
241
242    parent = device_get_parent(dev);
243    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
244    sc->mlxd_unit = device_get_unit(dev);
245    sc->mlxd_drive = device_get_ivars(dev);
246    sc->mlxd_dev = dev;
247
248    switch(sc->mlxd_drive->ms_state) {
249    case MLX_SYSD_ONLINE:
250	state = "online";
251	break;
252    case MLX_SYSD_CRITICAL:
253	state = "critical";
254	break;
255    case MLX_SYSD_OFFLINE:
256	state = "offline";
257	break;
258    default:
259	state = "unknown state";
260    }
261
262    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
263		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
264		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
265
266    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
267		      DEVSTAT_NO_ORDERED_TAGS,
268		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
269		      DEVSTAT_PRIORITY_ARRAY);
270
271    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
272    dsk->si_drv1 = sc;
273    sc->mlxd_dev_t = dsk;
274
275    /*
276     * Set maximum I/O size to the lesser of the recommended maximum and the practical
277     * maximum.
278     */
279    s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
280    s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
281    dsk->si_iosize_max = imin(s1, s2);
282
283    return (0);
284}
285
286static int
287mlxd_detach(device_t dev)
288{
289    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
290
291    debug_called(1);
292
293    devstat_remove_entry(&sc->mlxd_stats);
294    disk_destroy(sc->mlxd_dev_t);
295
296    return(0);
297}
298
299