amr_disk.c revision 67164
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 67164 2000-10-15 14:19:01Z phk $
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 <sys/rman.h>
47
48#include <dev/amr/amrio.h>
49#include <dev/amr/amrreg.h>
50#include <dev/amr/amrvar.h>
51#include <dev/amr/amr_tables.h>
52
53/* prototypes */
54static int amrd_probe(device_t dev);
55static int amrd_attach(device_t dev);
56static int amrd_detach(device_t dev);
57
58static	d_open_t	amrd_open;
59static	d_close_t	amrd_close;
60static	d_strategy_t	amrd_strategy;
61static	d_ioctl_t	amrd_ioctl;
62
63#define AMRD_CDEV_MAJOR	133
64
65static struct cdevsw amrd_cdevsw = {
66		/* open */	amrd_open,
67		/* close */	amrd_close,
68		/* read */	physread,
69		/* write */	physwrite,
70		/* ioctl */	amrd_ioctl,
71		/* poll */	nopoll,
72		/* mmap */	nommap,
73		/* strategy */	amrd_strategy,
74		/* name */ 	"amrd",
75		/* maj */	AMRD_CDEV_MAJOR,
76		/* dump */	nodump,
77		/* psize */ 	nopsize,
78		/* flags */	D_DISK,
79		/* bmaj */	254
80};
81
82static devclass_t	amrd_devclass;
83static struct cdevsw	amrddisk_cdevsw;
84#ifdef FREEBSD_4
85static int		disks_registered = 0;
86#endif
87
88static device_method_t amrd_methods[] = {
89    DEVMETHOD(device_probe,	amrd_probe),
90    DEVMETHOD(device_attach,	amrd_attach),
91    DEVMETHOD(device_detach,	amrd_detach),
92    { 0, 0 }
93};
94
95static driver_t amrd_driver = {
96    "amrd",
97    amrd_methods,
98    sizeof(struct amrd_softc)
99};
100
101DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
102
103static int
104amrd_open(dev_t dev, int flags, int fmt, struct proc *p)
105{
106    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
107    struct disklabel	*label;
108
109    debug_called(1);
110
111    if (sc == NULL)
112	return (ENXIO);
113
114    /* controller not active? */
115    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
116	return(ENXIO);
117
118    label = &sc->amrd_disk.d_label;
119    bzero(label, sizeof(*label));
120    label->d_type 	= DTYPE_SCSI;
121    label->d_secsize    = AMR_BLKSIZE;
122    label->d_nsectors   = sc->amrd_drive->al_sectors;
123    label->d_ntracks    = sc->amrd_drive->al_heads;
124    label->d_ncylinders = sc->amrd_drive->al_cylinders;
125    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
126    label->d_secperunit = sc->amrd_drive->al_size;
127
128    sc->amrd_flags |= AMRD_OPEN;
129    return (0);
130}
131
132static int
133amrd_close(dev_t dev, int flags, int fmt, struct proc *p)
134{
135    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
136
137    debug_called(1);
138
139    if (sc == NULL)
140	return (ENXIO);
141    sc->amrd_flags &= ~AMRD_OPEN;
142    return (0);
143}
144
145static int
146amrd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
147{
148
149    return (ENOTTY);
150}
151
152/*
153 * Read/write routine for a buffer.  Finds the proper unit, range checks
154 * arguments, and schedules the transfer.  Does not wait for the transfer
155 * to complete.  Multi-page transfers are supported.  All I/O requests must
156 * be a multiple of a sector in length.
157 */
158static void
159amrd_strategy(struct bio *bio)
160{
161    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
162
163    /* bogus disk? */
164    if (sc == NULL) {
165	bio->bio_error = EINVAL;
166	goto bad;
167    }
168
169    /* do-nothing operation */
170    if (bio->bio_bcount == 0)
171	goto done;
172
173    devstat_start_transaction(&sc->amrd_stats);
174    amr_submit_bio(sc->amrd_controller, bio);
175    return;
176
177 bad:
178    bio->bio_flags |= BIO_ERROR;
179
180 done:
181    /*
182     * Correctly set the buf to indicate a completed transfer
183     */
184    bio->bio_resid = bio->bio_bcount;
185    biodone(bio);
186    return;
187}
188
189void
190amrd_intr(void *data)
191{
192    struct bio *bio = (struct bio *)data;
193    struct amrd_softc *sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
194
195    debug_called(2);
196
197    if (bio->bio_flags & BIO_ERROR) {
198	bio->bio_error = EIO;
199	debug(1, "i/o error\n");
200    } else {
201	bio->bio_resid = 0;
202    }
203
204    devstat_end_transaction_bio(&sc->amrd_stats, bio);
205    biodone(bio);
206}
207
208static int
209amrd_probe(device_t dev)
210{
211
212    debug_called(1);
213
214    device_set_desc(dev, "MegaRAID logical drive");
215    return (0);
216}
217
218static int
219amrd_attach(device_t dev)
220{
221    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
222    device_t		parent;
223
224    debug_called(1);
225
226    parent = device_get_parent(dev);
227    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
228    sc->amrd_unit = device_get_unit(dev);
229    sc->amrd_drive = device_get_ivars(dev);
230    sc->amrd_dev = dev;
231
232    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
233		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
234		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
235		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
236
237    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
238		      DEVSTAT_NO_ORDERED_TAGS,
239		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
240		      DEVSTAT_PRIORITY_ARRAY);
241
242    sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_cdevsw, &amrddisk_cdevsw);
243    sc->amrd_dev_t->si_drv1 = sc;
244#ifdef FREEBSD_4
245    disks_registered++;
246#endif
247
248    /* set maximum I/O size to match the maximum s/g size */
249    sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
250
251    return (0);
252}
253
254static int
255amrd_detach(device_t dev)
256{
257    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
258
259    debug_called(1);
260
261    if (sc->amrd_flags & AMRD_OPEN)
262	return(EBUSY);
263
264    devstat_remove_entry(&sc->amrd_stats);
265#ifdef FREEBSD_4
266    if (--disks_registered == 0)
267	cdevsw_remove(&amrddisk_cdevsw);
268#else
269    disk_destroy(sc->amrd_dev_t);
270#endif
271    return(0);
272}
273
274