Merge from vendor branch LIBARCHIVE:
[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 Exp $
28  * $DragonFly: src/sys/dev/raid/ips/ips_disk.c,v 1.5 2004/10/06 02:12:31 y0netan1 Exp $
29  */
30
31 #include <sys/devicestat.h>
32 #include <dev/raid/ips/ips.h>
33 #include <dev/raid/ips/ips_disk.h>
34 #include <sys/stat.h>
35
36 static int ipsd_probe(device_t dev);
37 static int ipsd_attach(device_t dev);
38 static int ipsd_detach(device_t dev);
39
40 static disk_open_t ipsd_open;
41 static disk_close_t ipsd_close;
42 static disk_strategy_t ipsd_strategy;
43
44 static struct cdevsw ipsd_cdevsw = {
45         .d_name         = "ipsd",
46         .d_maj          = IPSD_CDEV_MAJOR,
47         .d_flags        = D_DISK,
48         .d_port         = NULL,
49         .d_clone        = NULL,
50         .old_open       = ipsd_open,
51         .old_close      = ipsd_close,
52         .old_strategy   = ipsd_strategy,
53         .old_read       = physread,
54         .old_write      = physwrite,
55 };
56
57 static device_method_t ipsd_methods[] = {
58         DEVMETHOD(device_probe,         ipsd_probe),
59         DEVMETHOD(device_attach,        ipsd_attach),
60         DEVMETHOD(device_detach,        ipsd_detach),
61         { 0, 0 }
62 };
63
64
65 static driver_t ipsd_driver = {
66         "ipsd",
67         ipsd_methods,
68         sizeof(ipsdisk_softc_t)
69 };
70
71 static devclass_t ipsd_devclass;
72 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
73
74 /*
75  * handle opening of disk device.  It must set up all information about
76  * the geometry and size of the disk
77  */
78 static int
79 ipsd_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
80 {
81         ipsdisk_softc_t *dsc = dev->si_drv1;
82
83         if (dsc == NULL)
84                 return (ENXIO);
85         dsc->state |= IPS_DEV_OPEN;
86         DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
87         return 0;
88 }
89
90 static int
91 ipsd_close(dev_t dev, int oflags, int devtype, d_thread_t *td)
92 {
93         ipsdisk_softc_t *dsc = dev->si_drv1;
94
95         dsc->state &= ~IPS_DEV_OPEN;
96         DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
97         return 0;
98 }
99
100 /* ipsd_finish is called to clean up and return a completed IO request */
101 void
102 ipsd_finish(struct bio *iobuf)
103 {
104         ipsdisk_softc_t *dsc;
105
106         dsc = iobuf->bio_disk->d_drv1;
107         if (iobuf->bio_flags & BIO_ERROR) {
108                 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
109         } else
110                 iobuf->bio_resid = 0;
111         devstat_end_transaction_buf(&dsc->stats, iobuf);
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         devstat_start_transaction(&dsc->stats);
125         ips_start_io_request(dsc->sc, iobuf);
126 }
127
128 static int
129 ipsd_probe(device_t dev)
130 {
131         DEVICE_PRINTF(2, dev, "in probe\n");
132         device_set_desc(dev, "Logical Drive");
133         return 0;
134 }
135
136 static int
137 ipsd_attach(device_t dev)
138 {
139         device_t adapter;
140         ipsdisk_softc_t *dsc;
141         struct  disklabel *label;
142         u_int totalsectors;
143         u_int nheads, nsectors;
144
145         DEVICE_PRINTF(2, dev, "in attach\n");
146         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
147         bzero(dsc, sizeof(ipsdisk_softc_t));
148         adapter = device_get_parent(dev);
149         dsc->dev = dev;
150         dsc->sc = device_get_softc(adapter);
151         dsc->unit = device_get_unit(dev);
152         dsc->disk_number = (uintptr_t) device_get_ivars(dev);
153         totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
154         if ((totalsectors > 0x400000) &&
155             ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
156                 nheads = IPS_NORM_HEADS;
157                 nsectors = IPS_NORM_SECTORS;
158         } else {
159                 nheads = IPS_COMP_HEADS;
160                 nsectors = IPS_COMP_SECTORS;
161         }
162         devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
163                           DEVSTAT_NO_ORDERED_TAGS,
164                           DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
165                           DEVSTAT_PRIORITY_DISK);
166         dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, 0,
167             &ipsd_cdevsw);
168         dsc->ipsd_dev_t->si_drv1 = dsc;
169         dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
170         label = &dsc->ipsd_disk.d_label;
171         bzero(label, sizeof(*label));
172         label->d_ntracks    = nheads;
173         label->d_nsectors   = nsectors;
174         label->d_type       = DTYPE_ESDI;
175         label->d_secsize    = IPS_BLKSIZE;
176         label->d_ncylinders = totalsectors / nheads / nsectors;
177         label->d_secpercyl  = nsectors / nheads;
178         label->d_secperunit = totalsectors;
179         device_printf(dev, "Logical Drive  (%dMB)\n",
180             dsc->sc->drives[dsc->disk_number].sector_count >> 11);
181         return 0;
182 }
183
184 static int
185 ipsd_detach(device_t dev)
186 {
187         ipsdisk_softc_t *dsc;
188
189         DEVICE_PRINTF(2, dev, "in detach\n");
190         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
191         if (dsc->state & IPS_DEV_OPEN)
192                 return (EBUSY);
193         devstat_remove_entry(&dsc->stats);
194         disk_destroy(&dsc->ipsd_disk);
195         return 0;
196 }
197