Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / dev / raid / ida / ida_disk.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1999,2000 Jonathan Lemon
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/dev/ida/ida_disk.c,v 1.12.2.6 2001/11/27 20:21:02 ps Exp $
b13267a5 27 * $DragonFly: src/sys/dev/raid/ida/ida_disk.c,v 1.13 2006/09/10 01:26:35 dillon Exp $
984263bc
MD
28 */
29
30/*
31 * Disk driver for Compaq SMART RAID adapters.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/malloc.h>
37#include <sys/kernel.h>
38
39#include <sys/buf.h>
40#include <sys/bus.h>
41#include <sys/conf.h>
42#include <sys/cons.h>
43#include <sys/devicestat.h>
44#include <sys/disk.h>
45
46#if NPCI > 0
47#include <machine/bus_memio.h>
48#endif
49#include <machine/bus_pio.h>
50#include <machine/bus.h>
51#include <machine/clock.h>
52#include <sys/rman.h>
73c4645c 53#include <sys/thread2.h>
984263bc
MD
54
55#include <vm/vm.h>
56#include <vm/pmap.h>
57#include <machine/md_var.h>
58
1f2de5d4
MD
59#include "idareg.h"
60#include "idavar.h"
984263bc
MD
61
62/* prototypes */
63static int idad_probe(device_t dev);
64static int idad_attach(device_t dev);
65static int idad_detach(device_t dev);
66
67static d_open_t idad_open;
68static d_close_t idad_close;
69static d_strategy_t idad_strategy;
70static d_dump_t idad_dump;
71
72#define IDAD_BDEV_MAJOR 29
73#define IDAD_CDEV_MAJOR 109
74
fef8985e
MD
75static struct dev_ops id_ops = {
76 { "idad", IDAD_CDEV_MAJOR, D_DISK },
77 .d_open = idad_open,
78 .d_close = idad_close,
79 .d_read = physread,
80 .d_write = physwrite,
81 .d_strategy = idad_strategy,
82 .d_dump = idad_dump,
984263bc
MD
83};
84
85static devclass_t idad_devclass;
984263bc
MD
86
87static device_method_t idad_methods[] = {
88 DEVMETHOD(device_probe, idad_probe),
89 DEVMETHOD(device_attach, idad_attach),
90 DEVMETHOD(device_detach, idad_detach),
91 { 0, 0 }
92};
93
94static driver_t idad_driver = {
95 "idad",
96 idad_methods,
97 sizeof(struct idad_softc)
98};
99
100DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
101
102static __inline struct idad_softc *
b13267a5 103idad_getsoftc(cdev_t dev)
984263bc
MD
104{
105
106 return ((struct idad_softc *)dev->si_drv1);
107}
108
109static int
fef8985e 110idad_open(struct dev_open_args *ap)
984263bc 111{
b13267a5 112 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
113 struct idad_softc *drv;
114 struct disklabel *label;
115
116 drv = idad_getsoftc(dev);
117 if (drv == NULL)
118 return (ENXIO);
119
120 label = &drv->disk.d_label;
121 bzero(label, sizeof(*label));
122 label->d_type = DTYPE_SCSI;
123 label->d_secsize = drv->secsize;
124 label->d_nsectors = drv->sectors;
125 label->d_ntracks = drv->heads;
126 label->d_ncylinders = drv->cylinders;
127 label->d_secpercyl = drv->sectors * drv->heads;
128 label->d_secperunit = drv->secperunit;
129
130 return (0);
131}
132
133static int
fef8985e 134idad_close(struct dev_close_args *ap)
984263bc 135{
b13267a5 136 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
137 struct idad_softc *drv;
138
139 drv = idad_getsoftc(dev);
140 if (drv == NULL)
141 return (ENXIO);
142 return (0);
143}
144
145/*
146 * Read/write routine for a buffer. Finds the proper unit, range checks
147 * arguments, and schedules the transfer. Does not wait for the transfer
148 * to complete. Multi-page transfers are supported. All I/O requests must
149 * be a multiple of a sector in length.
150 */
fef8985e
MD
151static int
152idad_strategy(struct dev_strategy_args *ap)
984263bc 153{
b13267a5 154 cdev_t dev = ap->a_head.a_dev;
fef8985e 155 struct bio *bio = ap->a_bio;
81b5c339 156 struct buf *bp = bio->bio_buf;
984263bc 157 struct idad_softc *drv;
984263bc 158
81b5c339 159 drv = idad_getsoftc(dev);
984263bc
MD
160 if (drv == NULL) {
161 bp->b_error = EINVAL;
162 goto bad;
163 }
164
165 /*
166 * software write protect check
167 */
10f3fee5 168 if ((drv->flags & DRV_WRITEPROT) && bp->b_cmd != BUF_CMD_READ) {
984263bc
MD
169 bp->b_error = EROFS;
170 goto bad;
171 }
172
173 /*
174 * If it's a null transfer, return immediately
175 */
176 if (bp->b_bcount == 0)
177 goto done;
178
81b5c339 179 bio->bio_driver_info = drv;
73c4645c 180 crit_enter();
984263bc 181 devstat_start_transaction(&drv->stats);
81b5c339 182 ida_submit_buf(drv->controller, bio);
73c4645c 183 crit_exit();
fef8985e 184 return(0);
984263bc
MD
185
186bad:
187 bp->b_flags |= B_ERROR;
188
189done:
190 /*
191 * Correctly set the buf to indicate a completed transfer
192 */
193 bp->b_resid = bp->b_bcount;
81b5c339 194 biodone(bio);
fef8985e 195 return(0);
984263bc
MD
196}
197
198static int
fef8985e 199idad_dump(struct dev_dump_args *ap)
984263bc 200{
b13267a5 201 cdev_t dev = ap->a_head.a_dev;
984263bc 202 struct idad_softc *drv;
984263bc
MD
203 long blkcnt;
204 int i, error, dumppages;
205 caddr_t va;
206 vm_offset_t addr, a;
207
984263bc
MD
208 drv = idad_getsoftc(dev);
209 if (drv == NULL)
210 return (ENXIO);
211
212 addr = 0;
fef8985e 213 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
984263bc 214
fef8985e 215 while (ap->a_count > 0) {
984263bc
MD
216 va = NULL;
217
fef8985e 218 dumppages = imin(ap->a_count / blkcnt, MAXDUMPPGS);
984263bc
MD
219
220 for (i = 0; i < dumppages; i++) {
221 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
228 error = ida_command(drv->controller, CMD_WRITE, va,
fef8985e 229 PAGE_SIZE * dumppages, drv->drive, ap->a_blkno, DMA_DATA_OUT);
984263bc
MD
230 if (error)
231 return (error);
232
fef8985e 233 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
984263bc
MD
234 return (EINTR);
235
fef8985e
MD
236 ap->a_blkno += blkcnt * dumppages;
237 ap->a_count -= blkcnt * dumppages;
984263bc
MD
238 addr += PAGE_SIZE * dumppages;
239 }
240 return (0);
241}
242
243void
81b5c339 244idad_intr(struct bio *bio)
984263bc 245{
81b5c339
MD
246 struct idad_softc *drv = (struct idad_softc *)bio->bio_driver_info;
247 struct buf *bp = bio->bio_buf;
984263bc
MD
248
249 if (bp->b_flags & B_ERROR)
250 bp->b_error = EIO;
251 else
252 bp->b_resid = 0;
253
254 devstat_end_transaction_buf(&drv->stats, bp);
81b5c339 255 biodone(bio);
984263bc
MD
256}
257
258static int
259idad_probe(device_t dev)
260{
261
262 device_set_desc(dev, "Compaq Logical Drive");
263 return (0);
264}
265
266static int
267idad_attach(device_t dev)
268{
269 struct ida_drive_info dinfo;
270 struct idad_softc *drv;
271 device_t parent;
b13267a5 272 cdev_t dsk;
984263bc
MD
273 int error;
274
275 drv = (struct idad_softc *)device_get_softc(dev);
276 parent = device_get_parent(dev);
277 drv->controller = (struct ida_softc *)device_get_softc(parent);
278 drv->unit = device_get_unit(dev);
279 drv->drive = drv->controller->num_drives;
280 drv->controller->num_drives++;
281
282 error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
283 &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
284 if (error) {
285 device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
286 return (ENXIO);
287 }
288
289 drv->cylinders = dinfo.ncylinders;
290 drv->heads = dinfo.nheads;
291 drv->sectors = dinfo.nsectors;
292 drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
293 drv->secperunit = dinfo.secperunit;
294
295 /* XXX
296 * other initialization
297 */
298 device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
299 drv->secperunit / ((1024 * 1024) / drv->secsize),
300 drv->secperunit, drv->secsize);
301
302 devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize,
303 DEVSTAT_NO_ORDERED_TAGS,
304 DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER,
305 DEVSTAT_PRIORITY_ARRAY);
306
fef8985e 307 dsk = disk_create(drv->unit, &drv->disk, 0, &id_ops);
984263bc
MD
308
309 dsk->si_drv1 = drv;
310 dsk->si_iosize_max = DFLTPHYS; /* XXX guess? */
984263bc
MD
311
312 return (0);
313}
314
315static int
316idad_detach(device_t dev)
317{
318 struct idad_softc *drv;
319
320 drv = (struct idad_softc *)device_get_softc(dev);
321 devstat_remove_entry(&drv->stats);
335dda38 322 disk_destroy(&drv->disk);
984263bc
MD
323 return (0);
324}