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