amr_disk.c revision 65245
1/*-
2 * Copyright (c) 1999 Jonathan Lemon
3 * Copyright (c) 1999, 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/amr/amr_disk.c 65245 2000-08-30 07:52:50Z msmith $
29 */
30
31/*
32 * Disk driver for AMI MegaRaid controllers
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38
39#include <dev/amr/amr_compat.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#include <dev/amr/amr_tables.h>
53
54/* prototypes */
55static int amrd_probe(device_t dev);
56static int amrd_attach(device_t dev);
57static int amrd_detach(device_t dev);
58
59static	d_open_t	amrd_open;
60static	d_close_t	amrd_close;
61static	d_strategy_t	amrd_strategy;
62static	d_ioctl_t	amrd_ioctl;
63
64#define AMRD_CDEV_MAJOR	133
65
66static struct cdevsw amrd_cdevsw = {
67		/* open */	amrd_open,
68		/* close */	amrd_close,
69		/* read */	physread,
70		/* write */	physwrite,
71		/* ioctl */	amrd_ioctl,
72		/* poll */	nopoll,
73		/* mmap */	nommap,
74		/* strategy */	amrd_strategy,
75		/* name */ 	"amrd",
76		/* maj */	AMRD_CDEV_MAJOR,
77		/* dump */	nodump,
78		/* psize */ 	nopsize,
79		/* flags */	D_DISK,
80		/* bmaj */	254
81};
82
83static devclass_t	amrd_devclass;
84static struct cdevsw	amrddisk_cdevsw;
85#ifdef FREEBSD_4
86static int		disks_registered = 0;
87#endif
88
89static device_method_t amrd_methods[] = {
90    DEVMETHOD(device_probe,	amrd_probe),
91    DEVMETHOD(device_attach,	amrd_attach),
92    DEVMETHOD(device_detach,	amrd_detach),
93    { 0, 0 }
94};
95
96static driver_t amrd_driver = {
97    "amrd",
98    amrd_methods,
99    sizeof(struct amrd_softc)
100};
101
102DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
103
104static int
105amrd_open(dev_t dev, int flags, int fmt, struct proc *p)
106{
107    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
108    struct disklabel	*label;
109
110    debug_called(1);
111
112    if (sc == NULL)
113	return (ENXIO);
114
115    /* controller not active? */
116    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
117	return(ENXIO);
118
119    label = &sc->amrd_disk.d_label;
120    bzero(label, sizeof(*label));
121    label->d_type 	= DTYPE_SCSI;
122    label->d_secsize    = AMR_BLKSIZE;
123    label->d_nsectors   = sc->amrd_drive->al_sectors;
124    label->d_ntracks    = sc->amrd_drive->al_heads;
125    label->d_ncylinders = sc->amrd_drive->al_cylinders;
126    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
127    label->d_secperunit = sc->amrd_drive->al_size;
128
129    sc->amrd_flags |= AMRD_OPEN;
130    return (0);
131}
132
133static int
134amrd_close(dev_t dev, int flags, int fmt, struct proc *p)
135{
136    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
137
138    debug_called(1);
139
140    if (sc == NULL)
141	return (ENXIO);
142    sc->amrd_flags &= ~AMRD_OPEN;
143    return (0);
144}
145
146static int
147amrd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
148{
149
150    return (ENOTTY);
151}
152
153/*
154 * Read/write routine for a buffer.  Finds the proper unit, range checks
155 * arguments, and schedules the transfer.  Does not wait for the transfer
156 * to complete.  Multi-page transfers are supported.  All I/O requests must
157 * be a multiple of a sector in length.
158 */
159static void
160amrd_strategy(struct bio *bio)
161{
162    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
163
164    /* bogus disk? */
165    if (sc == NULL) {
166	bio->bio_error = EINVAL;
167	goto bad;
168    }
169
170    /* do-nothing operation */
171    if (bio->bio_bcount == 0)
172	goto done;
173
174    devstat_start_transaction(&sc->amrd_stats);
175    amr_submit_bio(sc->amrd_controller, bio);
176    return;
177
178 bad:
179    bio->bio_flags |= BIO_ERROR;
180
181 done:
182    /*
183     * Correctly set the buf to indicate a completed transfer
184     */
185    bio->bio_resid = bio->bio_bcount;
186    biodone(bio);
187    return;
188}
189
190void
191amrd_intr(void *data)
192{
193    struct bio *bio = (struct bio *)data;
194    struct amrd_softc *sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
195
196    debug_called(2);
197
198    if (bio->bio_flags & BIO_ERROR) {
199	bio->bio_error = EIO;
200	debug(1, "i/o error\n");
201    } else {
202	bio->bio_resid = 0;
203    }
204
205    devstat_end_transaction_bio(&sc->amrd_stats, bio);
206    biodone(bio);
207}
208
209static int
210amrd_probe(device_t dev)
211{
212
213    debug_called(1);
214
215    device_set_desc(dev, "MegaRAID logical drive");
216    return (0);
217}
218
219static int
220amrd_attach(device_t dev)
221{
222    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
223    device_t		parent;
224
225    debug_called(1);
226
227    parent = device_get_parent(dev);
228    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
229    sc->amrd_unit = device_get_unit(dev);
230    sc->amrd_drive = device_get_ivars(dev);
231    sc->amrd_dev = dev;
232
233    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
234		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
235		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
236		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
237
238    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
239		      DEVSTAT_NO_ORDERED_TAGS,
240		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
241		      DEVSTAT_PRIORITY_ARRAY);
242
243    sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_cdevsw, &amrddisk_cdevsw);
244    sc->amrd_dev_t->si_drv1 = sc;
245#ifdef FREEBSD_4
246    disks_registered++;
247#endif
248
249    /* set maximum I/O size to match the maximum s/g size */
250    sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
251
252    return (0);
253}
254
255static int
256amrd_detach(device_t dev)
257{
258    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
259
260    debug_called(1);
261
262    if (sc->amrd_flags & AMRD_OPEN)
263	return(EBUSY);
264
265    devstat_remove_entry(&sc->amrd_stats);
266#ifdef FREEBSD_4
267    if (--disks_registered == 0)
268	cdevsw_remove(&amrddisk_cdevsw);
269#else
270    disk_destroy(sc->amrd_dev_t);
271#endif
272    return(0);
273}
274
275