2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/dev/aac/aac_disk.c,v 1.3.2.8 2003/01/11 18:39:39 scottl Exp $
30 * $DragonFly: src/sys/dev/raid/aac/aac_disk.c,v 1.2 2003/06/17 04:28:21 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <dev/aac/aac_compat.h>
43 #include <sys/devicestat.h>
49 #include <machine/md_var.h>
50 #include <machine/bus.h>
53 #include <dev/aac/aacreg.h>
54 #include <dev/aac/aac_ioctl.h>
55 #include <dev/aac/aacvar.h>
58 * Interface to parent.
60 static int aac_disk_probe(device_t dev);
61 static int aac_disk_attach(device_t dev);
62 static int aac_disk_detach(device_t dev);
65 * Interface to the device switch.
67 static d_open_t aac_disk_open;
68 static d_close_t aac_disk_close;
69 static d_strategy_t aac_disk_strategy;
70 static d_dump_t aac_disk_dump;
72 #define AAC_DISK_CDEV_MAJOR 151
74 static struct cdevsw aac_disk_cdevsw = {
75 /* open */ aac_disk_open,
76 /* close */ aac_disk_close,
78 /* write */ physwrite,
82 /* strategy */ aac_disk_strategy,
84 /* maj */ AAC_DISK_CDEV_MAJOR,
85 /* dump */ aac_disk_dump,
88 #if __FreeBSD_version < 500005
93 devclass_t aac_disk_devclass;
94 static struct cdevsw aac_disk_disk_cdevsw;
96 static int disks_registered = 0;
99 static device_method_t aac_disk_methods[] = {
100 DEVMETHOD(device_probe, aac_disk_probe),
101 DEVMETHOD(device_attach, aac_disk_attach),
102 DEVMETHOD(device_detach, aac_disk_detach),
106 static driver_t aac_disk_driver = {
109 sizeof(struct aac_disk)
112 #define AAC_MAXIO 65536
114 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
116 /* sysctl tunables */
117 static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */
118 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
120 SYSCTL_DECL(_hw_aac);
121 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0,
122 "Max I/O size per transfer to an array");
125 * Handle open from generic layer.
127 * This is called by the diskslice code on first open in order to get the
128 * basic device geometry paramters.
131 aac_disk_open(dev_t dev, int flags, int fmt, d_thread_t *td)
134 struct disklabel *label;
138 sc = (struct aac_disk *)dev->si_drv1;
141 printf("aac_disk_open: No Softc\n");
145 /* check that the controller is up and running */
146 if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
147 printf("Controller Suspended controller state = 0x%x\n",
148 sc->ad_controller->aac_state);
152 /* build synthetic label */
153 label = &sc->ad_disk.d_label;
154 bzero(label, sizeof(*label));
155 label->d_type = DTYPE_ESDI;
156 label->d_secsize = AAC_BLOCK_SIZE;
157 label->d_nsectors = sc->ad_sectors;
158 label->d_ntracks = sc->ad_heads;
159 label->d_ncylinders = sc->ad_cylinders;
160 label->d_secpercyl = sc->ad_sectors * sc->ad_heads;
161 label->d_secperunit = sc->ad_size;
163 sc->ad_flags |= AAC_DISK_OPEN;
168 * Handle last close of the disk device.
171 aac_disk_close(dev_t dev, int flags, int fmt, d_thread_t *td)
177 sc = (struct aac_disk *)dev->si_drv1;
182 sc->ad_flags &= ~AAC_DISK_OPEN;
187 * Handle an I/O request.
190 aac_disk_strategy(struct bio *bp)
196 sc = (struct aac_disk *)bp->bio_dev->si_drv1;
200 bp->bio_flags |= BIO_ERROR;
201 bp->bio_error = EINVAL;
206 /* do-nothing operation? */
207 if (bp->bio_bcount == 0) {
208 bp->bio_resid = bp->bio_bcount;
213 /* perform accounting */
214 devstat_start_transaction(&sc->ad_stats);
216 /* pass the bio to the controller - it can work out who we are */
222 * Dump memory out to an array
224 * This queues blocks of memory of size AAC_MAXIO to the controller and waits
225 * for the controller to complete the requests.
228 aac_disk_dump(dev_t dev)
231 struct aac_softc *sc;
234 unsigned int count, blkno, secsize;
240 dumppages = AAC_MAXIO / PAGE_SIZE;
242 if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
248 sc= ad->ad_controller;
250 blkcnt = howmany(PAGE_SIZE, secsize);
255 if ((count / blkcnt) < dumppages)
256 dumppages = count / blkcnt;
258 for (i = 0; i < dumppages; ++i) {
259 vm_offset_t a = addr + (i * PAGE_SIZE);
260 if (is_physical_memory(a)) {
261 va = pmap_kenter_temporary(trunc_page(a), i);
263 va = pmap_kenter_temporary(trunc_page(0), i);
269 * Queue the block to the controller. If the queue is full,
270 * EBUSY will be returned.
272 error = aac_dump_enqueue(ad, blkno, va, dumppages);
273 if (error && (error != EBUSY))
277 if (dumpstatus(addr, (off_t)(count * DEV_BSIZE)) < 0)
280 blkno += blkcnt * dumppages;
281 count -= blkcnt * dumppages;
282 addr += PAGE_SIZE * dumppages;
288 * Either the queue was full on the last attemp, or we have no
289 * more data to dump. Let the queue drain out and retry the
290 * block if the queue was full.
292 aac_dump_complete(sc);
302 * Handle completion of an I/O request.
305 aac_biodone(struct bio *bp)
312 sc = (struct aac_disk *)bp->bio_dev->si_drv1;
314 devstat_end_transaction_bio(&sc->ad_stats, bp);
315 if (bp->bio_flags & BIO_ERROR) {
316 blkno = (sc->ad_label.d_nsectors) ? 0 : -1;
317 #if __FreeBSD_version > 500005
318 diskerr(bp, (char *)bp->bio_driver1, blkno, &sc->ad_label);
320 diskerr(bp, (char *)bp->bio_driver1, 0, blkno, &sc->ad_label);
330 aac_disk_probe(device_t dev)
339 * Attach a unit to the controller.
342 aac_disk_attach(device_t dev)
348 sc = (struct aac_disk *)device_get_softc(dev);
350 /* initialise our softc */
352 (struct aac_softc *)device_get_softc(device_get_parent(dev));
353 sc->ad_container = device_get_ivars(dev);
357 * require that extended translation be enabled - other drivers read the
360 sc->ad_size = sc->ad_container->co_mntobj.Capacity;
361 if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */
364 } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */
371 sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
373 device_printf(dev, "%uMB (%u sectors)\n",
374 sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
377 devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev),
378 AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS,
379 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
380 DEVSTAT_PRIORITY_ARRAY);
382 /* attach a generic disk device to ourselves */
383 sc->ad_dev_t = disk_create(device_get_unit(dev), &sc->ad_disk, 0,
384 &aac_disk_cdevsw, &aac_disk_disk_cdevsw);
385 sc->ad_dev_t->si_drv1 = sc;
390 sc->ad_dev_t->si_iosize_max = aac_iosize_max;
391 sc->unit = device_get_unit(dev);
397 * Disconnect ourselves from the system.
400 aac_disk_detach(device_t dev)
406 sc = (struct aac_disk *)device_get_softc(dev);
408 if (sc->ad_flags & AAC_DISK_OPEN)
411 devstat_remove_entry(&sc->ad_stats);
412 disk_destroy(sc->ad_dev_t);
414 if (--disks_registered == 0)
415 cdevsw_remove(&aac_disk_cdevsw);