kernel: Remove some bogus casts to the own type.
[dragonfly.git] / sys / dev / raid / amr / amr_disk.c
CommitLineData
984263bc
MD
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.
1fcd0ba2
SW
27 */
28/*-
984263bc
MD
29 * Copyright (c) 2002 Eric Moore
30 * Copyright (c) 2002 LSI Logic Corporation
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The party using or redistributing the source code and binary forms
42 * agrees to the disclaimer below and the terms and conditions set forth
43 * herein.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
1fcd0ba2 57 * $FreeBSD: src/sys/dev/amr/amr_disk.c,v 1.39 2006/10/31 21:19:25 pjd Exp $
984263bc
MD
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>
1fcd0ba2 67#include <sys/module.h>
984263bc 68
1fcd0ba2 69#include <sys/bio.h>
984263bc
MD
70#include <sys/bus.h>
71#include <sys/conf.h>
ba0cc1ab 72#include <sys/dtype.h>
984263bc 73
1fcd0ba2 74#include <sys/rman.h>
984263bc 75
1fcd0ba2
SW
76#include <dev/raid/amr/amrio.h>
77#include <dev/raid/amr/amrreg.h>
78#include <dev/raid/amr/amrvar.h>
79#include <dev/raid/amr/amr_tables.h>
984263bc
MD
80
81/* prototypes */
82static int amrd_probe(device_t dev);
83static int amrd_attach(device_t dev);
84static int amrd_detach(device_t dev);
85
86static d_open_t amrd_open;
984263bc 87static d_strategy_t amrd_strategy;
510931cd 88static d_dump_t amrd_dump;
984263bc 89
fef8985e 90static struct dev_ops amrd_ops = {
88abd8b5 91 { "amrd", 0, D_DISK },
fef8985e 92 .d_open = amrd_open,
fef8985e 93 .d_strategy = amrd_strategy,
1fcd0ba2 94 .d_dump = amrd_dump,
984263bc
MD
95};
96
97static devclass_t amrd_devclass;
1fcd0ba2
SW
98#ifdef FREEBSD_4
99int amr_disks_registered = 0;
100#endif
984263bc
MD
101
102static device_method_t amrd_methods[] = {
103 DEVMETHOD(device_probe, amrd_probe),
104 DEVMETHOD(device_attach, amrd_attach),
105 DEVMETHOD(device_detach, amrd_detach),
106 { 0, 0 }
107};
108
109static driver_t amrd_driver = {
110 "amrd",
111 amrd_methods,
112 sizeof(struct amrd_softc)
113};
114
aa2b9d05 115DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, NULL, NULL);
984263bc
MD
116
117static int
fef8985e 118amrd_open(struct dev_open_args *ap)
984263bc 119{
1fcd0ba2 120 struct amrd_softc *sc = ap->a_head.a_dev->si_drv1;
984263bc
MD
121
122 debug_called(1);
123
124 if (sc == NULL)
125 return (ENXIO);
126
127 /* controller not active? */
128 if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
129 return(ENXIO);
a688b15c 130
984263bc
MD
131 return (0);
132}
510931cd
HP
133/********************************************************************************
134 * System crashdump support
135 */
1fcd0ba2
SW
136
137static int
fef8985e 138amrd_dump(struct dev_dump_args *ap)
510931cd 139{
b13267a5 140 cdev_t dev = ap->a_head.a_dev;
1fcd0ba2
SW
141 off_t offset = ap->a_offset;
142 void *virtual = ap->a_virtual;
143 size_t length = ap->a_length;
144 struct amrd_softc *amrd_sc;
510931cd 145 struct amr_softc *amr_sc;
362298e4 146 int error = 0;
510931cd 147
1fcd0ba2
SW
148 amrd_sc = (struct amrd_softc *)dev->si_drv1;
149 if (amrd_sc == NULL)
510931cd 150 return(ENXIO);
1fcd0ba2 151 amr_sc = (struct amr_softc *)amrd_sc->amrd_controller;
510931cd 152
1fcd0ba2
SW
153 if (length > 0) {
154 int driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
1fcd0ba2 155
362298e4
SW
156 error = amr_dump_blocks(amr_sc, driveno, offset / AMR_BLKSIZE,
157 virtual, (int)length / AMR_BLKSIZE);
510931cd 158 }
362298e4 159 return(error);
510931cd 160}
1fcd0ba2 161
984263bc
MD
162/*
163 * Read/write routine for a buffer. Finds the proper unit, range checks
164 * arguments, and schedules the transfer. Does not wait for the transfer
165 * to complete. Multi-page transfers are supported. All I/O requests must
166 * be a multiple of a sector in length.
167 */
fef8985e
MD
168static int
169amrd_strategy(struct dev_strategy_args *ap)
984263bc 170{
b13267a5 171 cdev_t dev = ap->a_head.a_dev;
fef8985e 172 struct bio *bio = ap->a_bio;
81b5c339
MD
173 struct buf *bp = bio->bio_buf;
174 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
984263bc
MD
175
176 /* bogus disk? */
177 if (sc == NULL) {
81b5c339 178 bp->b_error = EINVAL;
984263bc
MD
179 goto bad;
180 }
81b5c339 181 bio->bio_driver_info = sc;
984263bc
MD
182
183 devstat_start_transaction(&sc->amrd_stats);
184 amr_submit_bio(sc->amrd_controller, bio);
1fcd0ba2 185 return (0);
984263bc
MD
186
187 bad:
81b5c339 188 bp->b_flags |= B_ERROR;
984263bc
MD
189
190 /*
191 * Correctly set the buf to indicate a completed transfer
192 */
81b5c339 193 bp->b_resid = bp->b_bcount;
984263bc 194 biodone(bio);
1fcd0ba2 195 return (0);
984263bc
MD
196}
197
198void
1fcd0ba2 199amrd_intr(void *data)
984263bc 200{
1fcd0ba2 201 struct bio *bio = (struct bio *)data;
81b5c339 202 struct buf *bp = bio->bio_buf;
720762e1 203 struct amrd_softc *sc = (struct amrd_softc *)bio->bio_driver_info;
984263bc
MD
204
205 debug_called(2);
206
720762e1 207 devstat_end_transaction_buf(&sc->amrd_stats, bp);
81b5c339
MD
208 if (bp->b_flags & B_ERROR) {
209 bp->b_error = EIO;
984263bc
MD
210 debug(1, "i/o error\n");
211 } else {
81b5c339 212 bp->b_resid = 0;
984263bc 213 }
1fcd0ba2 214
81b5c339 215 biodone(bio);
984263bc
MD
216}
217
218static int
219amrd_probe(device_t dev)
220{
221
222 debug_called(1);
223
224 device_set_desc(dev, "LSILogic MegaRAID logical drive");
225 return (0);
226}
227
228static int
229amrd_attach(device_t dev)
230{
231 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
232 device_t parent;
1fcd0ba2
SW
233 struct disk_info info;
234
984263bc
MD
235 debug_called(1);
236
237 parent = device_get_parent(dev);
238 sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
239 sc->amrd_unit = device_get_unit(dev);
240 sc->amrd_drive = device_get_ivars(dev);
241 sc->amrd_dev = dev;
242
243 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
244 sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
245 sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
246 amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
247
248 devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
249 DEVSTAT_NO_ORDERED_TAGS,
250 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
251 DEVSTAT_PRIORITY_ARRAY);
252
a688b15c 253 sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, &amrd_ops);
984263bc 254 sc->amrd_dev_t->si_drv1 = sc;
984263bc
MD
255
256 /* set maximum I/O size to match the maximum s/g size */
257 sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
258
1fcd0ba2
SW
259 /*
260 * Set disk info, as it appears that all needed data is available already.
261 * Setting the disk info will also cause the probing to start.
262 */
263 bzero(&info, sizeof(info));
cd29885a 264 info.d_media_blksize = AMR_BLKSIZE; /* optional */
1fcd0ba2 265 info.d_media_blocks = sc->amrd_drive->al_size;
cd29885a 266
1fcd0ba2 267 info.d_type = DTYPE_SCSI; /* mandatory */
cd29885a 268 info.d_secpertrack = sc->amrd_drive->al_sectors;
1fcd0ba2 269 info.d_nheads = sc->amrd_drive->al_heads;
cd29885a 270 info.d_ncylinders = sc->amrd_drive->al_cylinders;
1fcd0ba2 271 info.d_secpercyl = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
cd29885a
MD
272
273 disk_setdiskinfo(&sc->amrd_disk, &info);
274
984263bc
MD
275 return (0);
276}
277
278static int
279amrd_detach(device_t dev)
280{
281 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
282
283 debug_called(1);
284
1fcd0ba2
SW
285#if 0 /* XXX swildner */
286 if (sc->amrd_disk->d_flags & DISKFLAG_OPEN)
984263bc 287 return(EBUSY);
1fcd0ba2 288#endif
984263bc
MD
289
290 devstat_remove_entry(&sc->amrd_stats);
1fcd0ba2
SW
291#ifdef FREEBSD_4
292 if (--amr_disks_registered == 0)
293 cdevsw_remove(&amrddisk_cdevsw);
294#else
335dda38 295 disk_destroy(&sc->amrd_disk);
1fcd0ba2 296#endif
984263bc
MD
297 return(0);
298}
299