mlx_disk.c revision 52785
1169691Skan/*-
2169691Skan * Copyright (c) 1999 Jonathan Lemon
3169691Skan * Copyright (c) 1999 Michael Smith
4169691Skan * All rights reserved.
5169691Skan *
6169691Skan * Redistribution and use in source and binary forms, with or without
7169691Skan * modification, are permitted provided that the following conditions
8169691Skan * are met:
9169691Skan * 1. Redistributions of source code must retain the above copyright
10169691Skan *    notice, this list of conditions and the following disclaimer.
11169691Skan * 2. Redistributions in binary form must reproduce the above copyright
12169691Skan *    notice, this list of conditions and the following disclaimer in the
13169691Skan *    documentation and/or other materials provided with the distribution.
14169691Skan *
15169691Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25169691Skan * SUCH DAMAGE.
26169691Skan *
27169691Skan * $FreeBSD: head/sys/dev/mlx/mlx_disk.c 52785 1999-11-02 03:25:24Z msmith $
28169691Skan */
29169691Skan
30169691Skan/*
31169691Skan * Disk driver for Mylex DAC960 RAID adapters.
32169691Skan */
33169691Skan
34169691Skan#include <sys/param.h>
35169691Skan#include <sys/systm.h>
36169691Skan#include <sys/malloc.h>
37169691Skan#include <sys/kernel.h>
38169691Skan
39169691Skan#include <sys/buf.h>
40169691Skan#include <sys/bus.h>
41169691Skan#include <sys/conf.h>
42169691Skan#include <sys/devicestat.h>
43169691Skan#include <sys/disk.h>
44169691Skan
45169691Skan#include <machine/bus.h>
46169691Skan#include <machine/clock.h>
47169691Skan#include <sys/rman.h>
48169691Skan
49169691Skan#include <dev/mlx/mlxio.h>
50169691Skan#include <dev/mlx/mlxvar.h>
51169691Skan#include <dev/mlx/mlxreg.h>
52169691Skan
53169691Skan#if 0
54169691Skan#define debug(fmt, args...)	printf("%s: " fmt "\n", __FUNCTION__ , ##args)
55169691Skan#else
56169691Skan#define debug(fmt, args...)
57169691Skan#endif
58169691Skan
59169691Skan/* prototypes */
60169691Skanstatic int mlxd_probe(device_t dev);
61169691Skanstatic int mlxd_attach(device_t dev);
62169691Skanstatic int mlxd_detach(device_t dev);
63169691Skan
64169691Skanstatic	d_open_t	mlxd_open;
65169691Skanstatic	d_close_t	mlxd_close;
66169691Skanstatic	d_strategy_t	mlxd_strategy;
67169691Skanstatic	d_ioctl_t	mlxd_ioctl;
68169691Skan
69169691Skan#define MLXD_BDEV_MAJOR	27
70169691Skan#define MLXD_CDEV_MAJOR	131
71169691Skan
72169691Skanstatic struct cdevsw mlxd_cdevsw = {
73169691Skan		/* open */	mlxd_open,
74169691Skan		/* close */	mlxd_close,
75169691Skan		/* read */	physread,
76169691Skan		/* write */	physwrite,
77169691Skan		/* ioctl */	mlxd_ioctl,
78169691Skan		/* poll */	nopoll,
79169691Skan		/* mmap */	nommap,
80169691Skan		/* strategy */	mlxd_strategy,
81169691Skan		/* name */ 	"mlxd",
82169691Skan		/* maj */	MLXD_CDEV_MAJOR,
83169691Skan		/* dump */	nodump,
84169691Skan		/* psize */ 	nopsize,
85169691Skan		/* flags */	D_DISK,
86169691Skan		/* bmaj */	MLXD_BDEV_MAJOR
87169691Skan};
88169691Skan
89169691Skanstatic devclass_t	mlxd_devclass;
90169691Skanstatic struct cdevsw	mlxddisk_cdevsw;
91169691Skanstatic int		disks_registered = 0;
92169691Skan
93169691Skanstatic device_method_t mlxd_methods[] = {
94169691Skan    DEVMETHOD(device_probe,	mlxd_probe),
95169691Skan    DEVMETHOD(device_attach,	mlxd_attach),
96169691Skan    DEVMETHOD(device_detach,	mlxd_detach),
97169691Skan    { 0, 0 }
98169691Skan};
99169691Skan
100static driver_t mlxd_driver = {
101    "mlxd",
102    mlxd_methods,
103    sizeof(struct mlxd_softc)
104};
105
106DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
107
108static int
109mlxd_open(dev_t dev, int flags, int fmt, struct proc *p)
110{
111    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
112    struct disklabel	*label;
113
114    debug("called");
115
116    if (sc == NULL)
117	return (ENXIO);
118
119    /* controller not active? */
120    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
121	return(ENXIO);
122
123    label = &sc->mlxd_disk.d_label;
124    bzero(label, sizeof(*label));
125    label->d_type = DTYPE_SCSI;
126    label->d_secsize    = MLX_BLKSIZE;
127    label->d_nsectors   = sc->mlxd_drive->ms_sectors;
128    label->d_ntracks    = sc->mlxd_drive->ms_heads;
129    label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
130    label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
131    label->d_secperunit = sc->mlxd_drive->ms_size;
132
133    sc->mlxd_flags |= MLXD_OPEN;
134    return (0);
135}
136
137static int
138mlxd_close(dev_t dev, int flags, int fmt, struct proc *p)
139{
140    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
141
142    debug("called");
143
144    if (sc == NULL)
145	return (ENXIO);
146    sc->mlxd_flags &= ~MLXD_OPEN;
147    return (0);
148}
149
150static int
151mlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
152{
153    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
154    int error;
155
156    debug("called");
157
158    if (sc == NULL)
159	return (ENXIO);
160
161    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
162	debug("mlx_submit_ioctl returned %d\n", error);
163	return(error);
164    }
165    return (ENOTTY);
166}
167
168/*
169 * Read/write routine for a buffer.  Finds the proper unit, range checks
170 * arguments, and schedules the transfer.  Does not wait for the transfer
171 * to complete.  Multi-page transfers are supported.  All I/O requests must
172 * be a multiple of a sector in length.
173 */
174static void
175mlxd_strategy(struct buf *bp)
176{
177    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->b_dev->si_drv1;
178
179    debug("called");
180
181    /* bogus disk? */
182    if (sc == NULL) {
183	bp->b_error = EINVAL;
184	goto bad;
185    }
186
187    /* XXX may only be temporarily offline - sleep? */
188    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
189	bp->b_error = ENXIO;
190	goto bad;
191    }
192
193    /* do-nothing operation */
194    if (bp->b_bcount == 0)
195	goto done;
196
197    devstat_start_transaction(&sc->mlxd_stats);
198    mlx_submit_buf(sc->mlxd_controller, bp);
199    return;
200
201 bad:
202    bp->b_flags |= B_ERROR;
203
204 done:
205    /*
206     * Correctly set the buf to indicate a completed transfer
207     */
208    bp->b_resid = bp->b_bcount;
209    biodone(bp);
210    return;
211}
212
213void
214mlxd_intr(void *data)
215{
216    struct buf *bp = (struct buf *)data;
217    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->b_dev->si_drv1;
218
219    debug("called");
220
221    if (bp->b_flags & B_ERROR)
222	bp->b_error = EIO;
223    else
224	bp->b_resid = 0;
225
226    devstat_end_transaction_buf(&sc->mlxd_stats, bp);
227    biodone(bp);
228}
229
230static int
231mlxd_probe(device_t dev)
232{
233
234    debug("called");
235
236    device_set_desc(dev, "Mylex System Drive");
237    return (0);
238}
239
240static int
241mlxd_attach(device_t dev)
242{
243    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
244    device_t		parent;
245    char		*state;
246    dev_t		dsk;
247
248    debug("called");
249
250    parent = device_get_parent(dev);
251    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
252    sc->mlxd_unit = device_get_unit(dev);
253    sc->mlxd_drive = device_get_ivars(dev);
254    sc->mlxd_dev = dev;
255
256    switch(sc->mlxd_drive->ms_state) {
257    case MLX_SYSD_ONLINE:
258	state = "online";
259	break;
260    case MLX_SYSD_CRITICAL:
261	state = "critical";
262	break;
263    case MLX_SYSD_OFFLINE:
264	state = "offline";
265	break;
266    default:
267	state = "unknown state";
268    }
269
270    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
271		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
272		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
273
274    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
275		      DEVSTAT_NO_ORDERED_TAGS,
276		      DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
277		      DEVSTAT_PRIORITY_DA);
278
279    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
280    disks_registered++;
281
282    /* set maximum I/O size */
283    dsk->si_iosize_max = sc->mlxd_controller->mlx_maxiosize * MLX_BLKSIZE;
284    dsk->si_drv1 = sc;
285
286    return (0);
287}
288
289static int
290mlxd_detach(device_t dev)
291{
292    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
293
294    debug("called");
295
296    devstat_remove_entry(&sc->mlxd_stats);
297
298    /* hack to handle lack of destroy_disk() */
299    if (--disks_registered == 0)
300	cdevsw_remove(&mlxddisk_cdevsw);
301
302    return(0);
303}
304
305