Do a major clean-up of the BUSDMA architecture. A large number of
[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.13 2006/10/25 20:56:00 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 "amr_compat.h"
69 #include <sys/bus.h>
70 #include <sys/conf.h>
71 #include <sys/devicestat.h>
72 #include <sys/disk.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 #define AMRD_CDEV_MAJOR 133
97
98 static struct dev_ops amrd_ops = {
99         { "amrd", AMRD_CDEV_MAJOR, D_DISK },
100         .d_open = amrd_open,
101         .d_close = amrd_close,
102         .d_read = physread,
103         .d_write = physwrite,
104         .d_ioctl = amrd_ioctl,
105         .d_strategy = amrd_strategy,
106         .d_dump = amrd_dump
107 };
108
109 static devclass_t       amrd_devclass;
110
111 static device_method_t amrd_methods[] = {
112     DEVMETHOD(device_probe,     amrd_probe),
113     DEVMETHOD(device_attach,    amrd_attach),
114     DEVMETHOD(device_detach,    amrd_detach),
115     { 0, 0 }
116 };
117
118 static driver_t amrd_driver = {
119     "amrd",
120     amrd_methods,
121     sizeof(struct amrd_softc)
122 };
123
124 DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
125
126 static int
127 amrd_open(struct dev_open_args *ap)
128 {
129     cdev_t dev = ap->a_head.a_dev;
130     struct amrd_softc   *sc = (struct amrd_softc *)dev->si_drv1;
131 #if defined(__DragonFly__) || __FreeBSD_version < 500000                /* old buf style */
132     struct disklabel    *label;
133 #endif
134
135     debug_called(1);
136
137     if (sc == NULL)
138         return (ENXIO);
139
140     /* controller not active? */
141     if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
142         return(ENXIO);
143
144 #if defined(__DragonFly__) || __FreeBSD_version < 500000                /* old buf style */
145     label = &sc->amrd_disk.d_label;
146     bzero(label, sizeof(*label));
147     label->d_type       = DTYPE_SCSI;
148     label->d_secsize    = AMR_BLKSIZE;
149     label->d_nsectors   = sc->amrd_drive->al_sectors;
150     label->d_ntracks    = sc->amrd_drive->al_heads;
151     label->d_ncylinders = sc->amrd_drive->al_cylinders;
152     label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
153     label->d_secperunit = sc->amrd_drive->al_size;
154 #else
155     sc->amrd_disk.d_sectorsize = AMR_BLKSIZE;
156     sc->amrd_disk.d_mediasize = (off_t)sc->amrd_drive->al_size * AMR_BLKSIZE;
157     sc->amrd_disk.d_fwsectors = sc->amrd_drive->al_sectors;
158     sc->amrd_disk.d_fwheads = sc->amrd_drive->al_heads;
159 #endif
160
161     sc->amrd_flags |= AMRD_OPEN;
162     return (0);
163 }
164
165 static int
166 amrd_close(struct dev_close_args *ap)
167 {
168     cdev_t dev = ap->a_head.a_dev;
169     struct amrd_softc   *sc = (struct amrd_softc *)dev->si_drv1;
170
171     debug_called(1);
172
173     if (sc == NULL)
174         return (ENXIO);
175     sc->amrd_flags &= ~AMRD_OPEN;
176     return (0);
177 }
178
179 static int
180 amrd_ioctl(struct dev_ioctl_args *ap)
181 {
182     return (ENOTTY);
183 }
184
185
186 /********************************************************************************
187  * System crashdump support
188  */
189 int
190 amrd_dump(struct dev_dump_args *ap)
191 {
192     cdev_t dev = ap->a_head.a_dev;
193     struct amrd_softc   *amrd_sc = (struct amrd_softc *)dev->si_drv1;
194     struct amr_softc    *amr_sc;
195     vm_paddr_t          addr = 0;
196     long                blkcnt;
197     int                 dumppages = MAXDUMPPGS;
198     int                 error = 0;
199     int                 driveno;
200     int                 i;
201
202     debug_called(1);
203
204     amr_sc  = (struct amr_softc *)amrd_sc->amrd_controller;
205
206     if (!amrd_sc || !amr_sc)
207         return(ENXIO);
208
209     blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
210
211     driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
212
213     while (ap->a_count > 0) {
214         caddr_t va = NULL;
215
216         if ((ap->a_count / blkcnt) < dumppages)
217             dumppages = ap->a_count / blkcnt;
218
219         for (i = 0; i < dumppages; ++i) {
220             vm_paddr_t a = addr + (i * PAGE_SIZE);
221             if (is_physical_memory(a))
222                 va = pmap_kenter_temporary(trunc_page(a), i);
223             else
224                 va = pmap_kenter_temporary(trunc_page(0), i);
225         }
226
227         if ((error = amr_dump_blocks(amr_sc, driveno, ap->a_blkno, (void *)va,
228                                       (PAGE_SIZE * dumppages) / AMR_BLKSIZE)) != 0)
229                 return(error);
230
231         if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
232             return(EINTR);
233
234         ap->a_blkno += blkcnt * dumppages;
235         ap->a_count -= blkcnt * dumppages;
236         addr += PAGE_SIZE * dumppages;
237     }
238     return (0);
239 }
240 /*
241  * Read/write routine for a buffer.  Finds the proper unit, range checks
242  * arguments, and schedules the transfer.  Does not wait for the transfer
243  * to complete.  Multi-page transfers are supported.  All I/O requests must
244  * be a multiple of a sector in length.
245  */
246 static int
247 amrd_strategy(struct dev_strategy_args *ap)
248 {
249     cdev_t dev = ap->a_head.a_dev;
250     struct bio *bio = ap->a_bio;
251     struct buf *bp = bio->bio_buf;
252     struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
253
254     /* bogus disk? */
255     if (sc == NULL) {
256         bp->b_error = EINVAL;
257         goto bad;
258     }
259     bio->bio_driver_info = sc;
260
261     devstat_start_transaction(&sc->amrd_stats);
262     amr_submit_bio(sc->amrd_controller, bio);
263     return(0);
264
265  bad:
266     bp->b_flags |= B_ERROR;
267
268     /*
269      * Correctly set the buf to indicate a completed transfer
270      */
271     bp->b_resid = bp->b_bcount;
272     biodone(bio);
273     return(0);
274 }
275
276 void
277 amrd_intr(struct bio *bio)
278 {
279     struct buf *bp = bio->bio_buf;
280
281     debug_called(2);
282
283     if (bp->b_flags & B_ERROR) {
284         bp->b_error = EIO;
285         debug(1, "i/o error\n");
286     } else {
287         bp->b_resid = 0;
288     }
289     biodone(bio);
290 }
291
292 static int
293 amrd_probe(device_t dev)
294 {
295
296     debug_called(1);
297
298     device_set_desc(dev, "LSILogic MegaRAID logical drive");
299     return (0);
300 }
301
302 static int
303 amrd_attach(device_t dev)
304 {
305     struct amrd_softc   *sc = (struct amrd_softc *)device_get_softc(dev);
306     device_t            parent;
307     
308     debug_called(1);
309
310     parent = device_get_parent(dev);
311     sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
312     sc->amrd_unit = device_get_unit(dev);
313     sc->amrd_drive = device_get_ivars(dev);
314     sc->amrd_dev = dev;
315
316     device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
317                   sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
318                   sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK, 
319                   amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
320
321     devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
322                       DEVSTAT_NO_ORDERED_TAGS,
323                       DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 
324                       DEVSTAT_PRIORITY_ARRAY);
325
326     sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_ops);
327     sc->amrd_dev_t->si_drv1 = sc;
328
329     /* set maximum I/O size to match the maximum s/g size */
330     sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
331
332     return (0);
333 }
334
335 static int
336 amrd_detach(device_t dev)
337 {
338     struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
339
340     debug_called(1);
341
342     if (sc->amrd_flags & AMRD_OPEN)
343         return(EBUSY);
344
345     devstat_remove_entry(&sc->amrd_stats);
346     disk_destroy(&sc->amrd_disk);
347     return(0);
348 }
349