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