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