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
58#include <sys/cdefs.h>
59__FBSDID("$FreeBSD$");
60
61/*
62 * Disk driver for AMI MegaRaid controllers
63 */
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/module.h>
69
70#include <sys/bio.h>
71#include <sys/bus.h>
72#include <sys/conf.h>
73
74#include <machine/bus.h>
75#include <sys/rman.h>
76
77#include <dev/amr/amrio.h>
78#include <dev/amr/amrreg.h>
79#include <dev/amr/amrvar.h>
80#include <dev/amr/amr_tables.h>
81
82/* prototypes */
83static int amrd_probe(device_t dev);
84static int amrd_attach(device_t dev);
85static int amrd_detach(device_t dev);
86
87static	disk_open_t	amrd_open;
88static	disk_strategy_t	amrd_strategy;
89
90static devclass_t	amrd_devclass;
91#ifdef FREEBSD_4
92int			amr_disks_registered = 0;
93#endif
94
95static device_method_t amrd_methods[] = {
96    DEVMETHOD(device_probe,	amrd_probe),
97    DEVMETHOD(device_attach,	amrd_attach),
98    DEVMETHOD(device_detach,	amrd_detach),
99    { 0, 0 }
100};
101
102static driver_t amrd_driver = {
103    "amrd",
104    amrd_methods,
105    sizeof(struct amrd_softc)
106};
107
108DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
109
110static int
111amrd_open(struct disk *dp)
112{
113    struct amrd_softc	*sc = (struct amrd_softc *)dp->d_drv1;
114
115    debug_called(1);
116
117    if (sc == NULL)
118	return (ENXIO);
119
120    /* controller not active? */
121    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
122	return(ENXIO);
123
124    return (0);
125}
126/********************************************************************************
127 * System crashdump support
128 */
129
130static int
131amrd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
132{
133
134    struct amrd_softc	*amrd_sc;
135    struct amr_softc	*amr_sc;
136    int			error;
137    struct disk		*dp;
138
139    dp = arg;
140    amrd_sc = (struct amrd_softc *)dp->d_drv1;
141    if (amrd_sc == NULL)
142	return(ENXIO);
143    amr_sc  = (struct amr_softc *)amrd_sc->amrd_controller;
144
145    if (length > 0) {
146	int	driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
147	if ((error = amr_dump_blocks(amr_sc,driveno,offset / AMR_BLKSIZE ,(void *)virtual,(int) length / AMR_BLKSIZE  )) != 0)
148	    	return(error);
149
150    }
151    return(0);
152}
153
154/*
155 * Read/write routine for a buffer.  Finds the proper unit, range checks
156 * arguments, and schedules the transfer.  Does not wait for the transfer
157 * to complete.  Multi-page transfers are supported.  All I/O requests must
158 * be a multiple of a sector in length.
159 */
160static void
161amrd_strategy(struct bio *bio)
162{
163    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
164
165    /* bogus disk? */
166    if (sc == NULL) {
167	bio->bio_error = EINVAL;
168	goto bad;
169    }
170
171    amr_submit_bio(sc->amrd_controller, bio);
172    return;
173
174 bad:
175    bio->bio_flags |= BIO_ERROR;
176
177    /*
178     * Correctly set the buf to indicate a completed transfer
179     */
180    bio->bio_resid = bio->bio_bcount;
181    biodone(bio);
182    return;
183}
184
185void
186amrd_intr(void *data)
187{
188    struct bio *bio = (struct bio *)data;
189
190    debug_called(2);
191
192    if (bio->bio_flags & BIO_ERROR) {
193	bio->bio_error = EIO;
194	debug(1, "i/o error\n");
195    } else {
196	bio->bio_resid = 0;
197    }
198
199    biodone(bio);
200}
201
202static int
203amrd_probe(device_t dev)
204{
205
206    debug_called(1);
207
208    device_set_desc(dev, "LSILogic MegaRAID logical drive");
209    return (0);
210}
211
212static int
213amrd_attach(device_t dev)
214{
215    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
216    device_t		parent;
217
218    debug_called(1);
219
220    parent = device_get_parent(dev);
221    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
222    sc->amrd_unit = device_get_unit(dev);
223    sc->amrd_drive = device_get_ivars(dev);
224    sc->amrd_dev = dev;
225
226    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
227		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
228		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
229		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
230
231    sc->amrd_disk = disk_alloc();
232    sc->amrd_disk->d_drv1 = sc;
233    sc->amrd_disk->d_maxsize = (AMR_NSEG - 1) * PAGE_SIZE;
234    sc->amrd_disk->d_open = amrd_open;
235    sc->amrd_disk->d_strategy = amrd_strategy;
236    sc->amrd_disk->d_name = "amrd";
237    sc->amrd_disk->d_dump = (dumper_t *)amrd_dump;
238    sc->amrd_disk->d_unit = sc->amrd_unit;
239    sc->amrd_disk->d_flags = DISKFLAG_CANFLUSHCACHE;
240    sc->amrd_disk->d_sectorsize = AMR_BLKSIZE;
241    sc->amrd_disk->d_mediasize = (off_t)sc->amrd_drive->al_size * AMR_BLKSIZE;
242    sc->amrd_disk->d_fwsectors = sc->amrd_drive->al_sectors;
243    sc->amrd_disk->d_fwheads = sc->amrd_drive->al_heads;
244    disk_create(sc->amrd_disk, DISK_VERSION);
245
246    return (0);
247}
248
249static int
250amrd_detach(device_t dev)
251{
252    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
253
254    debug_called(1);
255
256    if (sc->amrd_disk->d_flags & DISKFLAG_OPEN)
257	return(EBUSY);
258
259#ifdef FREEBSD_4
260    if (--amr_disks_registered == 0)
261	cdevsw_remove(&amrddisk_cdevsw);
262#else
263    disk_destroy(sc->amrd_disk);
264#endif
265    return(0);
266}
267
268