* Add in support for the IBM ServeRAID controller.
[dragonfly.git] / sys / dev / raid / ips / ips_disk.c
1 /*-
2  * Written by: David Jeffery
3  * Copyright (c) 2002 Adaptec Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ips/ips_disk.c,v 1.4 2003/09/22 04:59:07 njl 
28  * $DragonFly: src/sys/dev/raid/ips/ips_disk.c,v 1.1 2004/01/15 15:41:23 drhodus Exp $
29  */
30
31 #include <sys/cdefs.h>
32
33 #include <dev/raid/ips/ips.h>
34 #include <dev/raid/ips/ips_disk.h>
35 #include <sys/stat.h>
36
37 static int ipsd_probe(device_t dev);
38 static int ipsd_attach(device_t dev);
39 static int ipsd_detach(device_t dev);
40
41 static disk_open_t ipsd_open;
42 static disk_close_t ipsd_close;
43 static disk_strategy_t ipsd_strategy;
44
45 static struct cdevsw ipsd_cdevsw = {
46         .d_name         = "ipsd",
47         .d_maj          = IPSD_CDEV_MAJOR,
48         .d_flags        = D_DISK,
49         .d_port         = NULL,
50         .d_autoq        = 0,
51         .old_open       = ipsd_open,
52         .old_close      = ipsd_close,
53         .old_strategy   = ipsd_strategy,
54         .old_read       = physread,
55         .old_write      = physwrite,
56 };
57
58 static device_method_t ipsd_methods[] = {
59         DEVMETHOD(device_probe,         ipsd_probe),
60         DEVMETHOD(device_attach,        ipsd_attach),
61         DEVMETHOD(device_detach,        ipsd_detach),
62         { 0, 0 }
63 };
64
65
66 static driver_t ipsd_driver = {
67         "ipsd",
68         ipsd_methods,
69         sizeof(ipsdisk_softc_t)
70 };
71
72 static devclass_t ipsd_devclass;
73 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
74
75 /*
76  * handle opening of disk device.  It must set up all information about
77  * the geometry and size of the disk
78  */
79 static int
80 ipsd_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
81 {
82         ipsdisk_softc_t *dsc = dev->si_drv1;
83
84         if (dsc == NULL)
85                 return (ENXIO);
86         dsc->state |= IPS_DEV_OPEN;
87         DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
88         return 0;
89 }
90
91 static int
92 ipsd_close(dev_t dev, int oflags, int devtype, d_thread_t *td)
93 {
94         ipsdisk_softc_t *dsc = dev->si_drv1;
95
96         dsc->state &= ~IPS_DEV_OPEN;
97         DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
98         return 0;
99 }
100
101 /* ipsd_finish is called to clean up and return a completed IO request */
102 void
103 ipsd_finish(struct bio *iobuf)
104 {
105         if (iobuf->bio_flags & BIO_ERROR) {
106                 ipsdisk_softc_t *dsc;
107                 dsc = iobuf->bio_disk->d_drv1;
108                 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
109         } else
110                 iobuf->bio_resid = 0;
111
112         biodone(iobuf);
113 }
114
115
116 static void
117 ipsd_strategy(struct bio *iobuf)
118 {
119         ipsdisk_softc_t *dsc;
120
121         dsc = iobuf->bio_disk->d_drv1;
122         DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
123         iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
124         ips_start_io_request(dsc->sc, iobuf);
125 }
126
127 static int
128 ipsd_probe(device_t dev)
129 {
130         DEVICE_PRINTF(2, dev, "in probe\n");
131         device_set_desc(dev, "Logical Drive");
132         return 0;
133 }
134
135 static int
136 ipsd_attach(device_t dev)
137 {
138         device_t adapter;
139         ipsdisk_softc_t *dsc;
140         struct  disklabel *label;
141         u_int totalsectors;
142         u_int nheads, nsectors;
143
144         DEVICE_PRINTF(2, dev, "in attach\n");
145         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
146         bzero(dsc, sizeof(ipsdisk_softc_t));
147         adapter = device_get_parent(dev);
148         dsc->dev = dev;
149         dsc->sc = device_get_softc(adapter);
150         dsc->unit = device_get_unit(dev);
151         dsc->disk_number = (uintptr_t) device_get_ivars(dev);
152         totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
153         if ((totalsectors > 0x400000) &&
154             ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
155                 nheads = IPS_NORM_HEADS;
156                 nsectors = IPS_NORM_SECTORS;
157         } else {
158                 nheads = IPS_COMP_HEADS;
159                 nsectors = IPS_COMP_SECTORS;
160         }
161         dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, 0,
162             &ipsd_cdevsw);
163         dsc->ipsd_dev_t->si_drv1 = dsc;
164         dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
165         label = &dsc->ipsd_disk.d_label;
166         bzero(label, sizeof(*label));
167         label->d_ntracks    = nheads;
168         label->d_nsectors   = nsectors;
169         label->d_type       = DTYPE_ESDI;
170         label->d_secsize    = IPS_BLKSIZE;
171         label->d_ncylinders = totalsectors / nheads / nsectors;
172         label->d_secpercyl  = nsectors / nheads;
173         label->d_secperunit = totalsectors;
174         device_printf(dev, "Logical Drive  (%dMB)\n",
175             dsc->sc->drives[dsc->disk_number].sector_count >> 11);
176         return 0;
177 }
178
179 static int
180 ipsd_detach(device_t dev)
181 {
182         ipsdisk_softc_t *dsc;
183
184         DEVICE_PRINTF(2, dev, "in detach\n");
185         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
186         if (dsc->state & IPS_DEV_OPEN)
187                 return (EBUSY);
188         disk_destroy(&dsc->ipsd_disk);
189         return 0;
190 }
191