kernel: Remove most definitions of CDEV_MAJOR.
[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 "amr_compat.h"
68 #include <sys/bus.h>
69 #include <sys/conf.h>
70 #include <sys/devicestat.h>
71 #include <sys/disk.h>
72 #include <sys/dtype.h>
73 #include <sys/rman.h>
74
75 #include <vm/vm.h>
76 #include <vm/pmap.h>
77
78 #include <machine/md_var.h>
79
80 #include "amrio.h"
81 #include "amrreg.h"
82 #include "amrvar.h"
83 #include "amr_tables.h"
84
85 /* prototypes */
86 static int amrd_probe(device_t dev);
87 static int amrd_attach(device_t dev);
88 static int amrd_detach(device_t dev);
89
90 static  d_open_t        amrd_open;
91 static  d_close_t       amrd_close;
92 static  d_strategy_t    amrd_strategy;
93 static  d_ioctl_t       amrd_ioctl;
94 static  d_dump_t        amrd_dump;
95
96 static struct dev_ops amrd_ops = {
97         { "amrd", 0, D_DISK },
98         .d_open = amrd_open,
99         .d_close = amrd_close,
100         .d_read = physread,
101         .d_write = physwrite,
102         .d_ioctl = amrd_ioctl,
103         .d_strategy = amrd_strategy,
104         .d_dump = amrd_dump
105 };
106
107 static devclass_t       amrd_devclass;
108
109 static device_method_t amrd_methods[] = {
110     DEVMETHOD(device_probe,     amrd_probe),
111     DEVMETHOD(device_attach,    amrd_attach),
112     DEVMETHOD(device_detach,    amrd_detach),
113     { 0, 0 }
114 };
115
116 static driver_t amrd_driver = {
117     "amrd",
118     amrd_methods,
119     sizeof(struct amrd_softc)
120 };
121
122 DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
123
124 static int
125 amrd_open(struct dev_open_args *ap)
126 {
127     cdev_t dev = ap->a_head.a_dev;
128     struct amrd_softc   *sc = (struct amrd_softc *)dev->si_drv1;
129
130     debug_called(1);
131
132     if (sc == NULL)
133         return (ENXIO);
134
135     /* controller not active? */
136     if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
137         return(ENXIO);
138 #if 0
139     bzero(&info, sizeof(info));
140     info.d_media_blksize = AMR_BLKSIZE;                 /* optional */
141     info.d_media_blocks = sc->amrd_drive->al_size;
142
143     info.d_type       = DTYPE_SCSI;                     /* mandatory */
144     info.d_secpertrack = sc->amrd_drive->al_sectors;
145     info.d_nheads       = sc->amrd_drive->al_heads;
146     info.d_ncylinders = sc->amrd_drive->al_cylinders;
147     info.d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
148
149     disk_setdiskinfo(&sc->amrd_disk, &info);
150 #endif
151     sc->amrd_flags |= AMRD_OPEN;
152     return (0);
153 }
154
155 static int
156 amrd_close(struct dev_close_args *ap)
157 {
158     cdev_t dev = ap->a_head.a_dev;
159     struct amrd_softc   *sc = (struct amrd_softc *)dev->si_drv1;
160
161     debug_called(1);
162
163     if (sc == NULL)
164         return (ENXIO);
165     sc->amrd_flags &= ~AMRD_OPEN;
166     return (0);
167 }
168
169 static int
170 amrd_ioctl(struct dev_ioctl_args *ap)
171 {
172     return (ENOTTY);
173 }
174
175
176 /********************************************************************************
177  * System crashdump support
178  */
179 int
180 amrd_dump(struct dev_dump_args *ap)
181 {
182     cdev_t dev = ap->a_head.a_dev;
183     struct amrd_softc   *amrd_sc = (struct amrd_softc *)dev->si_drv1;
184     struct amr_softc    *amr_sc;
185     int                 error = 0;
186     int                 driveno;
187
188     debug_called(1);
189
190     amr_sc  = (struct amr_softc *)amrd_sc->amrd_controller;
191
192     if (!amrd_sc || !amr_sc)
193         return(ENXIO);
194
195     driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
196
197     if (ap->a_length > 0) {
198         if ((error = amr_dump_blocks(amr_sc,driveno,ap->a_offset / AMR_BLKSIZE,
199             (void *)ap->a_virtual,(int) ap->a_length / AMR_BLKSIZE  )) != 0)
200                 return(error);
201     }
202     return(0);
203
204 }
205 /*
206  * Read/write routine for a buffer.  Finds the proper unit, range checks
207  * arguments, and schedules the transfer.  Does not wait for the transfer
208  * to complete.  Multi-page transfers are supported.  All I/O requests must
209  * be a multiple of a sector in length.
210  */
211 static int
212 amrd_strategy(struct dev_strategy_args *ap)
213 {
214     cdev_t dev = ap->a_head.a_dev;
215     struct bio *bio = ap->a_bio;
216     struct buf *bp = bio->bio_buf;
217     struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
218
219     /* bogus disk? */
220     if (sc == NULL) {
221         bp->b_error = EINVAL;
222         goto bad;
223     }
224     bio->bio_driver_info = sc;
225
226     devstat_start_transaction(&sc->amrd_stats);
227     amr_submit_bio(sc->amrd_controller, bio);
228     return(0);
229
230  bad:
231     bp->b_flags |= B_ERROR;
232
233     /*
234      * Correctly set the buf to indicate a completed transfer
235      */
236     bp->b_resid = bp->b_bcount;
237     biodone(bio);
238     return(0);
239 }
240
241 void
242 amrd_intr(struct bio *bio)
243 {
244     struct buf *bp = bio->bio_buf;
245
246     debug_called(2);
247
248     if (bp->b_flags & B_ERROR) {
249         bp->b_error = EIO;
250         debug(1, "i/o error\n");
251     } else {
252         bp->b_resid = 0;
253     }
254     biodone(bio);
255 }
256
257 static int
258 amrd_probe(device_t dev)
259 {
260
261     debug_called(1);
262
263     device_set_desc(dev, "LSILogic MegaRAID logical drive");
264     return (0);
265 }
266
267 static int
268 amrd_attach(device_t dev)
269 {
270         struct disk_info info;
271     struct amrd_softc   *sc = (struct amrd_softc *)device_get_softc(dev);
272     device_t            parent;
273     
274     debug_called(1);
275
276     parent = device_get_parent(dev);
277     sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
278     sc->amrd_unit = device_get_unit(dev);
279     sc->amrd_drive = device_get_ivars(dev);
280     sc->amrd_dev = dev;
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 & AMR_DRV_RAID_MASK, 
285                   amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
286
287     devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
288                       DEVSTAT_NO_ORDERED_TAGS,
289                       DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 
290                       DEVSTAT_PRIORITY_ARRAY);
291
292     sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, &amrd_ops);
293     sc->amrd_dev_t->si_drv1 = sc;
294
295     /* set maximum I/O size to match the maximum s/g size */
296     sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
297
298         /*
299          * Set disk info, as it appears that all needed data is available already.
300          * Setting the disk info will also cause the probing to start.
301          */
302         bzero(&info, sizeof(info));
303     info.d_media_blksize = AMR_BLKSIZE;                 /* optional */
304     info.d_media_blocks = sc->amrd_drive->al_size;
305
306     info.d_type       = DTYPE_SCSI;                     /* mandatory */
307     info.d_secpertrack = sc->amrd_drive->al_sectors;
308     info.d_nheads       = sc->amrd_drive->al_heads;
309     info.d_ncylinders = sc->amrd_drive->al_cylinders;
310     info.d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
311
312     disk_setdiskinfo(&sc->amrd_disk, &info);
313
314     return (0);
315 }
316
317 static int
318 amrd_detach(device_t dev)
319 {
320     struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
321
322     debug_called(1);
323
324     if (sc->amrd_flags & AMRD_OPEN)
325         return(EBUSY);
326
327     devstat_remove_entry(&sc->amrd_stats);
328     disk_destroy(&sc->amrd_disk);
329     return(0);
330 }
331