amr_disk.c revision 59249
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/amr/amr_disk.c 59249 2000-04-15 05:54:02Z phk $
28 */
29
30/*
31 * Disk driver for AMI MegaRaid controllers
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/malloc.h>
37#include <sys/kernel.h>
38
39#include <sys/buf.h>
40#include <sys/bus.h>
41#include <sys/conf.h>
42#include <sys/devicestat.h>
43#include <sys/disk.h>
44
45#include <machine/bus.h>
46#include <machine/clock.h>
47#include <sys/rman.h>
48
49#include <dev/amr/amrio.h>
50#include <dev/amr/amrreg.h>
51#include <dev/amr/amrvar.h>
52
53#if 0
54#define debug(fmt, args...)	printf("%s: " fmt "\n", __FUNCTION__ , ##args)
55#else
56#define debug(fmt, args...)
57#endif
58
59/* prototypes */
60static int amrd_probe(device_t dev);
61static int amrd_attach(device_t dev);
62static int amrd_detach(device_t dev);
63
64static	d_open_t	amrd_open;
65static	d_close_t	amrd_close;
66static	d_strategy_t	amrd_strategy;
67static	d_ioctl_t	amrd_ioctl;
68
69#define AMRD_BDEV_MAJOR	35
70#define AMRD_CDEV_MAJOR	133
71
72static struct cdevsw amrd_cdevsw = {
73		/* open */	amrd_open,
74		/* close */	amrd_close,
75		/* read */	physread,
76		/* write */	physwrite,
77		/* ioctl */	amrd_ioctl,
78		/* poll */	nopoll,
79		/* mmap */	nommap,
80		/* strategy */	amrd_strategy,
81		/* name */ 	"amrd",
82		/* maj */	AMRD_CDEV_MAJOR,
83		/* dump */	nodump,
84		/* psize */ 	nopsize,
85		/* flags */	D_DISK,
86		/* bmaj */	AMRD_BDEV_MAJOR
87};
88
89static devclass_t	amrd_devclass;
90static struct cdevsw	amrddisk_cdevsw;
91static int		disks_registered = 0;
92
93static device_method_t amrd_methods[] = {
94    DEVMETHOD(device_probe,	amrd_probe),
95    DEVMETHOD(device_attach,	amrd_attach),
96    DEVMETHOD(device_detach,	amrd_detach),
97    { 0, 0 }
98};
99
100static driver_t amrd_driver = {
101    "amrd",
102    amrd_methods,
103    sizeof(struct amrd_softc)
104};
105
106DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
107
108static int
109amrd_open(dev_t dev, int flags, int fmt, struct proc *p)
110{
111    struct amrd_softc	*sc = (struct amrd_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->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
121	return(ENXIO);
122
123    label = &sc->amrd_disk.d_label;
124    bzero(label, sizeof(*label));
125    label->d_type 	= DTYPE_SCSI;
126    label->d_secsize    = AMR_BLKSIZE;
127    label->d_nsectors   = sc->amrd_drive->al_sectors;
128    label->d_ntracks    = sc->amrd_drive->al_heads;
129    label->d_ncylinders = sc->amrd_drive->al_cylinders;
130    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
131    label->d_secperunit = sc->amrd_drive->al_size;
132
133    sc->amrd_flags |= AMRD_OPEN;
134    return (0);
135}
136
137static int
138amrd_close(dev_t dev, int flags, int fmt, struct proc *p)
139{
140    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
141
142    debug("called");
143
144    if (sc == NULL)
145	return (ENXIO);
146    sc->amrd_flags &= ~AMRD_OPEN;
147    return (0);
148}
149
150static int
151amrd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
152{
153    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
154    int error;
155
156    debug("called");
157
158    if (sc == NULL)
159	return (ENXIO);
160
161    if ((error = amr_submit_ioctl(sc->amrd_controller, sc->amrd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
162	debug("amr_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
175amrd_strategy(struct bio *bp)
176{
177    struct amrd_softc	*sc = (struct amrd_softc *)bp->bio_dev->si_drv1;
178
179    debug("called to %s %d bytes at b_blkno 0x%x  b_pblkno 0x%x",
180	  (bp->b_flags & B_READ) ? "read" : "write", bp->bio_bcount, bp->bio_blkno, bp->bio_pblkno);
181
182    /* bogus disk? */
183    if (sc == NULL) {
184	bp->bio_error = EINVAL;
185	goto bad;
186    }
187
188#if 0
189    /* XXX may only be temporarily offline - sleep? */
190    if (sc->amrd_drive->ld_state == AMR_SYSD_OFFLINE) {
191	bp->bio_error = ENXIO;
192	goto bad;
193    }
194#endif
195
196    /* do-nothing operation */
197    if (bp->bio_bcount == 0)
198	goto done;
199
200    devstat_start_transaction(&sc->amrd_stats);
201    amr_submit_buf(sc->amrd_controller, bp);
202    return;
203
204 bad:
205    bp->bio_flags |= BIO_ERROR;
206
207 done:
208    /*
209     * Correctly set the buf to indicate a completed transfer
210     */
211    bp->bio_resid = bp->bio_bcount;
212    biodone(bp);
213    return;
214}
215
216void
217amrd_intr(void *data)
218{
219    struct bio *bp = (struct bio *)data;
220    struct amrd_softc *sc = (struct amrd_softc *)bp->bio_dev->si_drv1;
221
222    debug("called");
223
224    if (bp->bio_flags & BIO_ERROR) {
225	bp->bio_error = EIO;
226	debug("i/o error\n");
227    } else {
228#if 0
229	int i;
230	for (i = 0; i < 512; i += 16)
231	    debug(" %04x  %16D", i, bp->bio_data + i, " ");
232#endif
233	bp->bio_resid = 0;
234    }
235
236    devstat_end_transaction_bio(&sc->amrd_stats, bp);
237    biodone(bp);
238}
239
240static int
241amrd_probe(device_t dev)
242{
243
244    debug("called");
245
246    device_set_desc(dev, "MegaRAID logical drive");
247    return (0);
248}
249
250static int
251amrd_attach(device_t dev)
252{
253    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
254    device_t		parent;
255    char		*state;
256
257    debug("called");
258
259    parent = device_get_parent(dev);
260    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
261    sc->amrd_unit = device_get_unit(dev);
262    sc->amrd_drive = device_get_ivars(dev);
263    sc->amrd_dev = dev;
264
265    switch(sc->amrd_drive->al_state) {
266    case AMRD_OFFLINE:
267	state = "offline";
268	break;
269    case AMRD_DEGRADED:
270	state = "degraded";
271	break;
272    case AMRD_OPTIMAL:
273	state = "optimal";
274	break;
275    case AMRD_DELETED:
276	state = "deleted";
277	break;
278    default:
279	state = "unknown state";
280    }
281
282    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
283		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
284		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & 0xf, state);
285
286    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
287		      DEVSTAT_NO_ORDERED_TAGS,
288		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
289		      DEVSTAT_PRIORITY_ARRAY);
290
291    sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_cdevsw, &amrddisk_cdevsw);
292    sc->amrd_dev_t->si_drv1 = sc;
293    disks_registered++;
294
295    /* set maximum I/O size */
296    /* dsk->si_iosize_max = ??? */;
297
298    return (0);
299}
300
301static int
302amrd_detach(device_t dev)
303{
304    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
305
306    debug("called");
307
308    devstat_remove_entry(&sc->amrd_stats);
309    disk_destroy(sc->amrd_dev_t);
310    return(0);
311}
312
313