Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / raid / aac / aac_disk.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 Adaptec, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
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 $
31  */
32
33 #include "opt_aac.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39
40 #include <dev/aac/aac_compat.h>
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/disk.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48
49 #include <machine/md_var.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52
53 #include <dev/aac/aacreg.h>
54 #include <dev/aac/aac_ioctl.h>
55 #include <dev/aac/aacvar.h>
56
57 /*
58  * Interface to parent.
59  */
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);
63
64 /*
65  * Interface to the device switch.
66  */
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;
71
72 #define AAC_DISK_CDEV_MAJOR     151
73
74 static struct cdevsw aac_disk_cdevsw = {
75         /* open */              aac_disk_open,
76         /* close */             aac_disk_close,
77         /* read */              physread,
78         /* write */             physwrite,
79         /* ioctl */             noioctl,
80         /* poll */              nopoll,
81         /* mmap */              nommap,
82         /* strategy */          aac_disk_strategy,
83         /* name */              "aacd",
84         /* maj */               AAC_DISK_CDEV_MAJOR,
85         /* dump */              aac_disk_dump,
86         /* psize */             nopsize,
87         /* flags */             D_DISK,
88 #if __FreeBSD_version < 500005
89         /* bmaj */              -1
90 #endif
91 };
92
93 devclass_t              aac_disk_devclass;
94 static struct cdevsw    aac_disk_disk_cdevsw;
95 #ifdef FREEBSD_4
96 static int              disks_registered = 0;
97 #endif
98
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),
103         { 0, 0 }
104 };
105
106 static driver_t aac_disk_driver = {
107         "aacd",
108         aac_disk_methods,
109         sizeof(struct aac_disk)
110 };
111
112 #define AAC_MAXIO       65536
113
114 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
115
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);
119
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");
123
124 /*
125  * Handle open from generic layer.
126  *
127  * This is called by the diskslice code on first open in order to get the 
128  * basic device geometry paramters.
129  */
130 static int
131 aac_disk_open(dev_t dev, int flags, int fmt, d_thread_t *td)
132 {
133         struct aac_disk *sc;
134         struct disklabel *label;
135
136         debug_called(0);
137
138         sc = (struct aac_disk *)dev->si_drv1;
139         
140         if (sc == NULL) {
141                 printf("aac_disk_open: No Softc\n");
142                 return (ENXIO);
143         }
144
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);
149                 return(ENXIO);
150         }
151
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;
162
163         sc->ad_flags |= AAC_DISK_OPEN;
164         return (0);
165 }
166
167 /*
168  * Handle last close of the disk device.
169  */
170 static int
171 aac_disk_close(dev_t dev, int flags, int fmt, d_thread_t *td)
172 {
173         struct aac_disk *sc;
174
175         debug_called(0);
176
177         sc = (struct aac_disk *)dev->si_drv1;
178         
179         if (sc == NULL)
180                 return (ENXIO);
181
182         sc->ad_flags &= ~AAC_DISK_OPEN;
183         return (0);
184 }
185
186 /*
187  * Handle an I/O request.
188  */
189 static void
190 aac_disk_strategy(struct bio *bp)
191 {
192         struct aac_disk *sc;
193
194         debug_called(4);
195
196         sc = (struct aac_disk *)bp->bio_dev->si_drv1;
197
198         /* bogus disk? */
199         if (sc == NULL) {
200                 bp->bio_flags |= BIO_ERROR;
201                 bp->bio_error = EINVAL;
202                 biodone(bp);
203                 return;
204         }
205
206         /* do-nothing operation? */
207         if (bp->bio_bcount == 0) {
208                 bp->bio_resid = bp->bio_bcount;
209                 biodone(bp);
210                 return;
211         }
212
213         /* perform accounting */
214         devstat_start_transaction(&sc->ad_stats);
215
216         /* pass the bio to the controller - it can work out who we are */
217         aac_submit_bio(bp);
218         return;
219 }
220
221 /*
222  * Dump memory out to an array
223  *
224  * This queues blocks of memory of size AAC_MAXIO to the controller and waits
225  * for the controller to complete the requests.
226  */
227 static int
228 aac_disk_dump(dev_t dev)
229 {
230         struct aac_disk *ad;
231         struct aac_softc *sc;
232         vm_offset_t addr;
233         long blkcnt;
234         unsigned int count, blkno, secsize;
235         int dumppages;
236         int i, error;
237
238         ad = dev->si_drv1;
239         addr = 0;
240         dumppages = AAC_MAXIO / PAGE_SIZE;
241
242         if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
243                 return (error);
244
245         if (ad == NULL)
246                 return (ENXIO);
247
248         sc= ad->ad_controller;
249
250         blkcnt = howmany(PAGE_SIZE, secsize);
251
252         while (count > 0) {
253                 caddr_t va = NULL;
254
255                 if ((count / blkcnt) < dumppages)
256                         dumppages = count / blkcnt;
257
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);
262                         } else {
263                                 va = pmap_kenter_temporary(trunc_page(0), i);
264                         }
265                 }
266
267 retry:
268                 /*
269                  * Queue the block to the controller.  If the queue is full,
270                  * EBUSY will be returned.
271                  */
272                 error = aac_dump_enqueue(ad, blkno, va, dumppages);
273                 if (error && (error != EBUSY))
274                         return (error);
275
276                 if (!error) {
277                         if (dumpstatus(addr, (off_t)(count * DEV_BSIZE)) < 0)
278                         return (EINTR);
279
280                         blkno += blkcnt * dumppages;
281                         count -= blkcnt * dumppages;
282                         addr += PAGE_SIZE * dumppages;
283                         if (count > 0)
284                         continue;
285                 }
286
287                 /*
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.
291                  */
292                 aac_dump_complete(sc);
293
294                 if (error == EBUSY)
295                         goto retry;
296         }
297
298         return (0);
299 }
300
301 /*
302  * Handle completion of an I/O request.
303  */
304 void
305 aac_biodone(struct bio *bp)
306 {
307         struct aac_disk *sc;
308         int blkno;
309
310         debug_called(4);
311
312         sc = (struct aac_disk *)bp->bio_dev->si_drv1;
313
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);
319 #else
320                 diskerr(bp, (char *)bp->bio_driver1, 0, blkno, &sc->ad_label);
321 #endif
322         }
323         biodone(bp);
324 }
325
326 /*
327  * Stub only.
328  */
329 static int
330 aac_disk_probe(device_t dev)
331 {
332
333         debug_called(2);
334
335         return (0);
336 }
337
338 /*
339  * Attach a unit to the controller.
340  */
341 static int
342 aac_disk_attach(device_t dev)
343 {
344         struct aac_disk *sc;
345         
346         debug_called(0);
347
348         sc = (struct aac_disk *)device_get_softc(dev);
349
350         /* initialise our softc */
351         sc->ad_controller =
352             (struct aac_softc *)device_get_softc(device_get_parent(dev));
353         sc->ad_container = device_get_ivars(dev);
354         sc->ad_dev = dev;
355
356         /*
357          * require that extended translation be enabled - other drivers read the
358          * disk!
359          */
360         sc->ad_size = sc->ad_container->co_mntobj.Capacity;
361         if (sc->ad_size >= (2 * 1024 * 1024)) {         /* 2GB */
362                 sc->ad_heads = 255;
363                 sc->ad_sectors = 63;
364         } else if (sc->ad_size >= (1 * 1024 * 1024)) {  /* 1GB */
365                 sc->ad_heads = 128;
366                 sc->ad_sectors = 32;
367         } else {
368                 sc->ad_heads = 64;
369                 sc->ad_sectors = 32;
370         }
371         sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
372
373         device_printf(dev, "%uMB (%u sectors)\n",
374                       sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
375                       sc->ad_size);
376
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);
381
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;
386 #ifdef FREEBSD_4
387         disks_registered++;
388 #endif
389
390         sc->ad_dev_t->si_iosize_max = aac_iosize_max;
391         sc->unit = device_get_unit(dev);
392
393         return (0);
394 }
395
396 /*
397  * Disconnect ourselves from the system.
398  */
399 static int
400 aac_disk_detach(device_t dev)
401 {
402         struct aac_disk *sc;
403
404         debug_called(2);
405
406         sc = (struct aac_disk *)device_get_softc(dev);
407
408         if (sc->ad_flags & AAC_DISK_OPEN)
409                 return(EBUSY);
410
411         devstat_remove_entry(&sc->ad_stats);
412         disk_destroy(sc->ad_dev_t);
413 #ifdef FREEBSD_4
414         if (--disks_registered == 0)
415                 cdevsw_remove(&aac_disk_cdevsw);
416 #endif
417
418         return(0);
419 }