MASSIVE reorganization of the device operations vector. Change cdevsw
[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.13 2006/07/28 02:17:37 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 "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 "aacreg.h"
54 #include "aac_ioctl.h"
55 #include "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 dev_ops aac_disk_ops = {
75         { "aacd", AAC_DISK_CDEV_MAJOR, D_DISK },
76         .d_open =               aac_disk_open,
77         .d_close =              aac_disk_close,
78         .d_read =               physread,
79         .d_write =              physwrite,
80         .d_strategy =           aac_disk_strategy,
81         .d_dump =               aac_disk_dump,
82 };
83
84 devclass_t              aac_disk_devclass;
85
86 static device_method_t aac_disk_methods[] = {
87         DEVMETHOD(device_probe, aac_disk_probe),
88         DEVMETHOD(device_attach,        aac_disk_attach),
89         DEVMETHOD(device_detach,        aac_disk_detach),
90         { 0, 0 }
91 };
92
93 static driver_t aac_disk_driver = {
94         "aacd",
95         aac_disk_methods,
96         sizeof(struct aac_disk)
97 };
98
99 #define AAC_MAXIO       65536
100
101 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
102
103 /* sysctl tunables */
104 static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */
105 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
106
107 SYSCTL_DECL(_hw_aac);
108 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0,
109             "Max I/O size per transfer to an array");
110
111 /*
112  * Handle open from generic layer.
113  *
114  * This is called by the diskslice code on first open in order to get the 
115  * basic device geometry paramters.
116  */
117 static int
118 aac_disk_open(struct dev_open_args *ap)
119 {
120         dev_t dev = ap->a_head.a_dev;
121         struct aac_disk *sc;
122         struct disklabel *label;
123
124         debug_called(0);
125
126         sc = (struct aac_disk *)dev->si_drv1;
127         
128         if (sc == NULL) {
129                 printf("aac_disk_open: No Softc\n");
130                 return (ENXIO);
131         }
132
133         /* check that the controller is up and running */
134         if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
135                 printf("Controller Suspended controller state = 0x%x\n",
136                        sc->ad_controller->aac_state);
137                 return(ENXIO);
138         }
139
140         /* build synthetic label */
141         label = &sc->ad_disk.d_label;
142         bzero(label, sizeof(*label));
143         label->d_type = DTYPE_ESDI;
144         label->d_secsize        = AAC_BLOCK_SIZE;
145         label->d_nsectors   = sc->ad_sectors;
146         label->d_ntracks        = sc->ad_heads;
147         label->d_ncylinders = sc->ad_cylinders;
148         label->d_secpercyl  = sc->ad_sectors * sc->ad_heads;
149         label->d_secperunit = sc->ad_size;
150
151         sc->ad_flags |= AAC_DISK_OPEN;
152         return (0);
153 }
154
155 /*
156  * Handle last close of the disk device.
157  */
158 static int
159 aac_disk_close(struct dev_close_args *ap)
160 {
161         dev_t dev = ap->a_head.a_dev;
162         struct aac_disk *sc;
163
164         debug_called(0);
165
166         sc = (struct aac_disk *)dev->si_drv1;
167         
168         if (sc == NULL)
169                 return (ENXIO);
170
171         sc->ad_flags &= ~AAC_DISK_OPEN;
172         return (0);
173 }
174
175 /*
176  * Handle an I/O request.
177  */
178 static int
179 aac_disk_strategy(struct dev_strategy_args *ap)
180 {
181         dev_t dev = ap->a_head.a_dev;
182         struct bio *bio = ap->a_bio;
183         struct buf *bp = bio->bio_buf;
184         struct aac_disk *sc;
185
186         debug_called(4);
187
188         sc = (struct aac_disk *)dev->si_drv1;
189
190         /* bogus disk? */
191         if (sc == NULL) {
192                 bp->b_flags |= B_ERROR;
193                 bp->b_error = EINVAL;
194                 biodone(bio);
195                 return(0);
196         }
197
198         /* do-nothing operation? */
199         if (bp->b_bcount == 0) {
200                 bp->b_resid = bp->b_bcount;
201                 biodone(bio);
202                 return(0);
203         }
204
205         /* perform accounting */
206         devstat_start_transaction(&sc->ad_stats);
207
208         /* pass the bio to the controller - it can work out who we are */
209         aac_submit_bio(sc, bio);
210         return(0);
211 }
212
213 /*
214  * Dump memory out to an array
215  *
216  * This queues blocks of memory of size AAC_MAXIO to the controller and waits
217  * for the controller to complete the requests.
218  */
219 static int
220 aac_disk_dump(struct dev_dump_args *ap)
221 {
222         dev_t dev = ap->a_head.a_dev;
223         struct aac_disk *ad;
224         struct aac_softc *sc;
225         vm_offset_t addr;
226         long blkcnt;
227         int dumppages;
228         int i, error;
229
230         ad = dev->si_drv1;
231         addr = 0;
232         dumppages = AAC_MAXIO / PAGE_SIZE;
233
234         if (ad == NULL)
235                 return (ENXIO);
236
237         sc= ad->ad_controller;
238
239         blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
240
241         while (ap->a_count > 0) {
242                 caddr_t va = NULL;
243
244                 if ((ap->a_count / blkcnt) < dumppages)
245                         dumppages = ap->a_count / blkcnt;
246
247                 for (i = 0; i < dumppages; ++i) {
248                         vm_offset_t a = addr + (i * PAGE_SIZE);
249                         if (is_physical_memory(a)) {
250                                 va = pmap_kenter_temporary(trunc_page(a), i);
251                         } else {
252                                 va = pmap_kenter_temporary(trunc_page(0), i);
253                         }
254                 }
255
256 retry:
257                 /*
258                  * Queue the block to the controller.  If the queue is full,
259                  * EBUSY will be returned.
260                  */
261                 error = aac_dump_enqueue(ad, ap->a_blkno, va, dumppages);
262                 if (error && (error != EBUSY))
263                         return (error);
264
265                 if (!error) {
266                         if (dumpstatus(addr, (off_t)(ap->a_count * DEV_BSIZE)) < 0)
267                         return (EINTR);
268
269                         ap->a_blkno += blkcnt * dumppages;
270                         ap->a_count -= blkcnt * dumppages;
271                         addr += PAGE_SIZE * dumppages;
272                         if (ap->a_count > 0)
273                         continue;
274                 }
275
276                 /*
277                  * Either the queue was full on the last attemp, or we have no
278                  * more data to dump.  Let the queue drain out and retry the
279                  * block if the queue was full.
280                  */
281                 aac_dump_complete(sc);
282
283                 if (error == EBUSY)
284                         goto retry;
285         }
286
287         return (0);
288 }
289
290 /*
291  * Handle completion of an I/O request.
292  */
293 void
294 aac_biodone(struct bio *bio, const char *code)
295 {
296         struct buf *bp = bio->bio_buf;
297         struct aac_disk *sc;
298
299         debug_called(4);
300
301         sc = (struct aac_disk *)bio->bio_driver_info;
302
303         devstat_end_transaction_buf(&sc->ad_stats, bp);
304         if (bp->b_flags & B_ERROR) {
305                 diskerr(bio, sc->ad_dev_t,
306                         code, 0, 0, &sc->ad_label);
307         }
308         biodone(bio);
309 }
310
311 /*
312  * Stub only.
313  */
314 static int
315 aac_disk_probe(device_t dev)
316 {
317
318         debug_called(2);
319
320         return (0);
321 }
322
323 /*
324  * Attach a unit to the controller.
325  */
326 static int
327 aac_disk_attach(device_t dev)
328 {
329         struct aac_disk *sc;
330         
331         debug_called(0);
332
333         sc = (struct aac_disk *)device_get_softc(dev);
334
335         /* initialise our softc */
336         sc->ad_controller =
337             (struct aac_softc *)device_get_softc(device_get_parent(dev));
338         sc->ad_container = device_get_ivars(dev);
339         sc->ad_dev = dev;
340
341         /*
342          * require that extended translation be enabled - other drivers read the
343          * disk!
344          */
345         sc->ad_size = sc->ad_container->co_mntobj.Capacity;
346         if (sc->ad_size >= (2 * 1024 * 1024)) {         /* 2GB */
347                 sc->ad_heads = 255;
348                 sc->ad_sectors = 63;
349         } else if (sc->ad_size >= (1 * 1024 * 1024)) {  /* 1GB */
350                 sc->ad_heads = 128;
351                 sc->ad_sectors = 32;
352         } else {
353                 sc->ad_heads = 64;
354                 sc->ad_sectors = 32;
355         }
356         sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
357
358         device_printf(dev, "%uMB (%u sectors)\n",
359                       sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
360                       sc->ad_size);
361
362         devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev),
363                           AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS,
364                           DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 
365                           DEVSTAT_PRIORITY_ARRAY);
366
367         /* attach a generic disk device to ourselves */
368         sc->ad_dev_t = disk_create(device_get_unit(dev), &sc->ad_disk, 0,
369                                    &aac_disk_ops);
370         sc->ad_dev_t->si_drv1 = sc;
371
372         sc->ad_dev_t->si_iosize_max = aac_iosize_max;
373         sc->unit = device_get_unit(dev);
374
375         return (0);
376 }
377
378 /*
379  * Disconnect ourselves from the system.
380  */
381 static int
382 aac_disk_detach(device_t dev)
383 {
384         struct aac_disk *sc;
385
386         debug_called(2);
387
388         sc = (struct aac_disk *)device_get_softc(dev);
389
390         if (sc->ad_flags & AAC_DISK_OPEN)
391                 return(EBUSY);
392
393         devstat_remove_entry(&sc->ad_stats);
394         disk_destroy(&sc->ad_disk);
395         return(0);
396 }