Initial import from FreeBSD RELENG_4:
[games.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  */
31
32 #include "opt_aac.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38
39 #include <dev/aac/aac_compat.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/devicestat.h>
43 #include <sys/disk.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47
48 #include <machine/md_var.h>
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51
52 #include <dev/aac/aacreg.h>
53 #include <dev/aac/aac_ioctl.h>
54 #include <dev/aac/aacvar.h>
55
56 /*
57  * Interface to parent.
58  */
59 static int aac_disk_probe(device_t dev);
60 static int aac_disk_attach(device_t dev);
61 static int aac_disk_detach(device_t dev);
62
63 /*
64  * Interface to the device switch.
65  */
66 static  d_open_t        aac_disk_open;
67 static  d_close_t       aac_disk_close;
68 static  d_strategy_t    aac_disk_strategy;
69 static  d_dump_t        aac_disk_dump;
70
71 #define AAC_DISK_CDEV_MAJOR     151
72
73 static struct cdevsw aac_disk_cdevsw = {
74         /* open */              aac_disk_open,
75         /* close */             aac_disk_close,
76         /* read */              physread,
77         /* write */             physwrite,
78         /* ioctl */             noioctl,
79         /* poll */              nopoll,
80         /* mmap */              nommap,
81         /* strategy */          aac_disk_strategy,
82         /* name */              "aacd",
83         /* maj */               AAC_DISK_CDEV_MAJOR,
84         /* dump */              aac_disk_dump,
85         /* psize */             nopsize,
86         /* flags */             D_DISK,
87 #if __FreeBSD_version < 500005
88         /* bmaj */              -1
89 #endif
90 };
91
92 devclass_t              aac_disk_devclass;
93 static struct cdevsw    aac_disk_disk_cdevsw;
94 #ifdef FREEBSD_4
95 static int              disks_registered = 0;
96 #endif
97
98 static device_method_t aac_disk_methods[] = {
99         DEVMETHOD(device_probe, aac_disk_probe),
100         DEVMETHOD(device_attach,        aac_disk_attach),
101         DEVMETHOD(device_detach,        aac_disk_detach),
102         { 0, 0 }
103 };
104
105 static driver_t aac_disk_driver = {
106         "aacd",
107         aac_disk_methods,
108         sizeof(struct aac_disk)
109 };
110
111 #define AAC_MAXIO       65536
112
113 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
114
115 /* sysctl tunables */
116 static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */
117 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
118
119 SYSCTL_DECL(_hw_aac);
120 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0,
121             "Max I/O size per transfer to an array");
122
123 /*
124  * Handle open from generic layer.
125  *
126  * This is called by the diskslice code on first open in order to get the 
127  * basic device geometry paramters.
128  */
129 static int
130 aac_disk_open(dev_t dev, int flags, int fmt, d_thread_t *td)
131 {
132         struct aac_disk *sc;
133         struct disklabel *label;
134
135         debug_called(0);
136
137         sc = (struct aac_disk *)dev->si_drv1;
138         
139         if (sc == NULL) {
140                 printf("aac_disk_open: No Softc\n");
141                 return (ENXIO);
142         }
143
144         /* check that the controller is up and running */
145         if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
146                 printf("Controller Suspended controller state = 0x%x\n",
147                        sc->ad_controller->aac_state);
148                 return(ENXIO);
149         }
150
151         /* build synthetic label */
152         label = &sc->ad_disk.d_label;
153         bzero(label, sizeof(*label));
154         label->d_type = DTYPE_ESDI;
155         label->d_secsize        = AAC_BLOCK_SIZE;
156         label->d_nsectors   = sc->ad_sectors;
157         label->d_ntracks        = sc->ad_heads;
158         label->d_ncylinders = sc->ad_cylinders;
159         label->d_secpercyl  = sc->ad_sectors * sc->ad_heads;
160         label->d_secperunit = sc->ad_size;
161
162         sc->ad_flags |= AAC_DISK_OPEN;
163         return (0);
164 }
165
166 /*
167  * Handle last close of the disk device.
168  */
169 static int
170 aac_disk_close(dev_t dev, int flags, int fmt, d_thread_t *td)
171 {
172         struct aac_disk *sc;
173
174         debug_called(0);
175
176         sc = (struct aac_disk *)dev->si_drv1;
177         
178         if (sc == NULL)
179                 return (ENXIO);
180
181         sc->ad_flags &= ~AAC_DISK_OPEN;
182         return (0);
183 }
184
185 /*
186  * Handle an I/O request.
187  */
188 static void
189 aac_disk_strategy(struct bio *bp)
190 {
191         struct aac_disk *sc;
192
193         debug_called(4);
194
195         sc = (struct aac_disk *)bp->bio_dev->si_drv1;
196
197         /* bogus disk? */
198         if (sc == NULL) {
199                 bp->bio_flags |= BIO_ERROR;
200                 bp->bio_error = EINVAL;
201                 biodone(bp);
202                 return;
203         }
204
205         /* do-nothing operation? */
206         if (bp->bio_bcount == 0) {
207                 bp->bio_resid = bp->bio_bcount;
208                 biodone(bp);
209                 return;
210         }
211
212         /* perform accounting */
213         devstat_start_transaction(&sc->ad_stats);
214
215         /* pass the bio to the controller - it can work out who we are */
216         aac_submit_bio(bp);
217         return;
218 }
219
220 /*
221  * Dump memory out to an array
222  *
223  * This queues blocks of memory of size AAC_MAXIO to the controller and waits
224  * for the controller to complete the requests.
225  */
226 static int
227 aac_disk_dump(dev_t dev)
228 {
229         struct aac_disk *ad;
230         struct aac_softc *sc;
231         vm_offset_t addr;
232         long blkcnt;
233         unsigned int count, blkno, secsize;
234         int dumppages;
235         int i, error;
236
237         ad = dev->si_drv1;
238         addr = 0;
239         dumppages = AAC_MAXIO / PAGE_SIZE;
240
241         if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
242                 return (error);
243
244         if (ad == NULL)
245                 return (ENXIO);
246
247         sc= ad->ad_controller;
248
249         blkcnt = howmany(PAGE_SIZE, secsize);
250
251         while (count > 0) {
252                 caddr_t va = NULL;
253
254                 if ((count / blkcnt) < dumppages)
255                         dumppages = count / blkcnt;
256
257                 for (i = 0; i < dumppages; ++i) {
258                         vm_offset_t a = addr + (i * PAGE_SIZE);
259                         if (is_physical_memory(a)) {
260                                 va = pmap_kenter_temporary(trunc_page(a), i);
261                         } else {
262                                 va = pmap_kenter_temporary(trunc_page(0), i);
263                         }
264                 }
265
266 retry:
267                 /*
268                  * Queue the block to the controller.  If the queue is full,
269                  * EBUSY will be returned.
270                  */
271                 error = aac_dump_enqueue(ad, blkno, va, dumppages);
272                 if (error && (error != EBUSY))
273                         return (error);
274
275                 if (!error) {
276                         if (dumpstatus(addr, (off_t)(count * DEV_BSIZE)) < 0)
277                         return (EINTR);
278
279                         blkno += blkcnt * dumppages;
280                         count -= blkcnt * dumppages;
281                         addr += PAGE_SIZE * dumppages;
282                         if (count > 0)
283                         continue;
284                 }
285
286                 /*
287                  * Either the queue was full on the last attemp, or we have no
288                  * more data to dump.  Let the queue drain out and retry the
289                  * block if the queue was full.
290                  */
291                 aac_dump_complete(sc);
292
293                 if (error == EBUSY)
294                         goto retry;
295         }
296
297         return (0);
298 }
299
300 /*
301  * Handle completion of an I/O request.
302  */
303 void
304 aac_biodone(struct bio *bp)
305 {
306         struct aac_disk *sc;
307         int blkno;
308
309         debug_called(4);
310
311         sc = (struct aac_disk *)bp->bio_dev->si_drv1;
312
313         devstat_end_transaction_bio(&sc->ad_stats, bp);
314         if (bp->bio_flags & BIO_ERROR) {
315                 blkno = (sc->ad_label.d_nsectors) ? 0 : -1;
316 #if __FreeBSD_version > 500005
317                 diskerr(bp, (char *)bp->bio_driver1, blkno, &sc->ad_label);
318 #else
319                 diskerr(bp, (char *)bp->bio_driver1, 0, blkno, &sc->ad_label);
320 #endif
321         }
322         biodone(bp);
323 }
324
325 /*
326  * Stub only.
327  */
328 static int
329 aac_disk_probe(device_t dev)
330 {
331
332         debug_called(2);
333
334         return (0);
335 }
336
337 /*
338  * Attach a unit to the controller.
339  */
340 static int
341 aac_disk_attach(device_t dev)
342 {
343         struct aac_disk *sc;
344         
345         debug_called(0);
346
347         sc = (struct aac_disk *)device_get_softc(dev);
348
349         /* initialise our softc */
350         sc->ad_controller =
351             (struct aac_softc *)device_get_softc(device_get_parent(dev));
352         sc->ad_container = device_get_ivars(dev);
353         sc->ad_dev = dev;
354
355         /*
356          * require that extended translation be enabled - other drivers read the
357          * disk!
358          */
359         sc->ad_size = sc->ad_container->co_mntobj.Capacity;
360         if (sc->ad_size >= (2 * 1024 * 1024)) {         /* 2GB */
361                 sc->ad_heads = 255;
362                 sc->ad_sectors = 63;
363         } else if (sc->ad_size >= (1 * 1024 * 1024)) {  /* 1GB */
364                 sc->ad_heads = 128;
365                 sc->ad_sectors = 32;
366         } else {
367                 sc->ad_heads = 64;
368                 sc->ad_sectors = 32;
369         }
370         sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
371
372         device_printf(dev, "%uMB (%u sectors)\n",
373                       sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
374                       sc->ad_size);
375
376         devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev),
377                           AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS,
378                           DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 
379                           DEVSTAT_PRIORITY_ARRAY);
380
381         /* attach a generic disk device to ourselves */
382         sc->ad_dev_t = disk_create(device_get_unit(dev), &sc->ad_disk, 0,
383                                    &aac_disk_cdevsw, &aac_disk_disk_cdevsw);
384         sc->ad_dev_t->si_drv1 = sc;
385 #ifdef FREEBSD_4
386         disks_registered++;
387 #endif
388
389         sc->ad_dev_t->si_iosize_max = aac_iosize_max;
390         sc->unit = device_get_unit(dev);
391
392         return (0);
393 }
394
395 /*
396  * Disconnect ourselves from the system.
397  */
398 static int
399 aac_disk_detach(device_t dev)
400 {
401         struct aac_disk *sc;
402
403         debug_called(2);
404
405         sc = (struct aac_disk *)device_get_softc(dev);
406
407         if (sc->ad_flags & AAC_DISK_OPEN)
408                 return(EBUSY);
409
410         devstat_remove_entry(&sc->ad_stats);
411         disk_destroy(sc->ad_dev_t);
412 #ifdef FREEBSD_4
413         if (--disks_registered == 0)
414                 cdevsw_remove(&aac_disk_cdevsw);
415 #endif
416
417         return(0);
418 }