xref: /dragonfly/sys/dev/raid/amr/amr_disk.c (revision 89a89091)
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 /*-
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  *
57  * $FreeBSD: src/sys/dev/amr/amr_disk.c,v 1.39 2006/10/31 21:19:25 pjd 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 #include <sys/module.h>
68 
69 #include <sys/bio.h>
70 #include <sys/bus.h>
71 #include <sys/conf.h>
72 #include <sys/dtype.h>
73 
74 #include <sys/rman.h>
75 
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>
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_strategy_t	amrd_strategy;
88 static	d_dump_t	amrd_dump;
89 
90 static struct dev_ops amrd_ops = {
91 	{ "amrd", 0, D_DISK },
92 	.d_open = amrd_open,
93 	.d_strategy = amrd_strategy,
94 	.d_dump = amrd_dump,
95 };
96 
97 static devclass_t	amrd_devclass;
98 
99 static device_method_t amrd_methods[] = {
100     DEVMETHOD(device_probe,	amrd_probe),
101     DEVMETHOD(device_attach,	amrd_attach),
102     DEVMETHOD(device_detach,	amrd_detach),
103     { 0, 0 }
104 };
105 
106 static driver_t amrd_driver = {
107     "amrd",
108     amrd_methods,
109     sizeof(struct amrd_softc)
110 };
111 
112 DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, NULL, NULL);
113 
114 static int
115 amrd_open(struct dev_open_args *ap)
116 {
117     struct amrd_softc	*sc = ap->a_head.a_dev->si_drv1;
118 
119     debug_called(1);
120 
121     if (sc == NULL)
122 	return (ENXIO);
123 
124     /* controller not active? */
125     if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
126 	return(ENXIO);
127 
128     return (0);
129 }
130 /********************************************************************************
131  * System crashdump support
132  */
133 
134 static int
135 amrd_dump(struct dev_dump_args *ap)
136 {
137     cdev_t dev = ap->a_head.a_dev;
138     off_t offset = ap->a_offset;
139     void *virtual = ap->a_virtual;
140     size_t length = ap->a_length;
141     struct amrd_softc	*amrd_sc;
142     struct amr_softc	*amr_sc;
143     int			error = 0;
144 
145     amrd_sc = (struct amrd_softc *)dev->si_drv1;
146     if (amrd_sc == NULL)
147 	return(ENXIO);
148     amr_sc  = (struct amr_softc *)amrd_sc->amrd_controller;
149 
150     if (length > 0) {
151 	int	driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
152 
153 	error = amr_dump_blocks(amr_sc, driveno, offset / AMR_BLKSIZE,
154 	    virtual, (int)length / AMR_BLKSIZE);
155     }
156     return(error);
157 }
158 
159 /*
160  * Read/write routine for a buffer.  Finds the proper unit, range checks
161  * arguments, and schedules the transfer.  Does not wait for the transfer
162  * to complete.  Multi-page transfers are supported.  All I/O requests must
163  * be a multiple of a sector in length.
164  */
165 static int
166 amrd_strategy(struct dev_strategy_args *ap)
167 {
168     cdev_t dev = ap->a_head.a_dev;
169     struct bio *bio = ap->a_bio;
170     struct buf *bp = bio->bio_buf;
171     struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
172 
173     /* bogus disk? */
174     if (sc == NULL) {
175 	bp->b_error = EINVAL;
176 	goto bad;
177     }
178     bio->bio_driver_info = sc;
179 
180     devstat_start_transaction(&sc->amrd_stats);
181     amr_submit_bio(sc->amrd_controller, bio);
182     return (0);
183 
184  bad:
185     bp->b_flags |= B_ERROR;
186 
187     /*
188      * Correctly set the buf to indicate a completed transfer
189      */
190     bp->b_resid = bp->b_bcount;
191     biodone(bio);
192     return (0);
193 }
194 
195 void
196 amrd_intr(void *data)
197 {
198     struct bio *bio = (struct bio *)data;
199     struct buf *bp = bio->bio_buf;
200     struct amrd_softc *sc = (struct amrd_softc *)bio->bio_driver_info;
201 
202     debug_called(2);
203 
204     devstat_end_transaction_buf(&sc->amrd_stats, bp);
205     if (bp->b_flags & B_ERROR) {
206 	bp->b_error = EIO;
207 	debug(1, "i/o error\n");
208     } else {
209 	bp->b_resid = 0;
210     }
211 
212     biodone(bio);
213 }
214 
215 static int
216 amrd_probe(device_t dev)
217 {
218 
219     debug_called(1);
220 
221     device_set_desc(dev, "LSILogic MegaRAID logical drive");
222     return (0);
223 }
224 
225 static int
226 amrd_attach(device_t dev)
227 {
228     struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
229     device_t		parent;
230     struct disk_info info;
231 
232     debug_called(1);
233 
234     parent = device_get_parent(dev);
235     sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
236     sc->amrd_unit = device_get_unit(dev);
237     sc->amrd_drive = device_get_ivars(dev);
238     sc->amrd_dev = dev;
239 
240     device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
241 		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
242 		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
243 		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
244 
245     devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
246 		      DEVSTAT_NO_ORDERED_TAGS,
247 		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
248 		      DEVSTAT_PRIORITY_ARRAY);
249 
250     sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, &amrd_ops);
251     sc->amrd_dev_t->si_drv1 = sc;
252 
253     /* set maximum I/O size to match the maximum s/g size */
254     sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
255 
256     /*
257      * Set disk info, as it appears that all needed data is available already.
258      * Setting the disk info will also cause the probing to start.
259      */
260     bzero(&info, sizeof(info));
261     info.d_media_blksize = AMR_BLKSIZE;			/* optional */
262     info.d_media_blocks = sc->amrd_drive->al_size;
263 
264     info.d_type = DTYPE_SCSI;				/* mandatory */
265     info.d_secpertrack = sc->amrd_drive->al_sectors;
266     info.d_nheads = sc->amrd_drive->al_heads;
267     info.d_ncylinders = sc->amrd_drive->al_cylinders;
268     info.d_secpercyl = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
269 
270     disk_setdiskinfo(&sc->amrd_disk, &info);
271 
272     return (0);
273 }
274 
275 static int
276 amrd_detach(device_t dev)
277 {
278     struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
279 
280     debug_called(1);
281 
282 #if 0 /* XXX swildner */
283     if (sc->amrd_disk->d_flags & DISKFLAG_OPEN)
284 	return(EBUSY);
285 #endif
286 
287     devstat_remove_entry(&sc->amrd_stats);
288     disk_destroy(&sc->amrd_disk);
289 
290     return(0);
291 }
292 
293