Merge from vendor branch DIFFUTILS:
[dragonfly.git] / sys / dev / raid / ida / ida_disk.c
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 $
27  * $DragonFly: src/sys/dev/raid/ida/ida_disk.c,v 1.6 2003/08/07 21:17:09 dillon Exp $
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>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <machine/md_var.h>
57
58 #include "idareg.h"
59 #include "idavar.h"
60
61 /* prototypes */
62 static int idad_probe(device_t dev);
63 static int idad_attach(device_t dev);
64 static int idad_detach(device_t dev);
65
66 static  d_open_t        idad_open;
67 static  d_close_t       idad_close;
68 static  d_strategy_t    idad_strategy;
69 static  d_dump_t        idad_dump;
70
71 #define IDAD_BDEV_MAJOR 29
72 #define IDAD_CDEV_MAJOR 109
73
74 static struct cdevsw id_cdevsw = {
75         /* name */      "idad",
76         /* maj */       IDAD_CDEV_MAJOR,
77         /* flags */     D_DISK,
78         /* port */      NULL,
79         /* autoq */     0,
80
81         /* open */      idad_open,
82         /* close */     idad_close,
83         /* read */      physread,
84         /* write */     physwrite,
85         /* ioctl */     noioctl,
86         /* poll */      nopoll,
87         /* mmap */      nommap,
88         /* strategy */  idad_strategy,
89         /* dump */      idad_dump,
90         /* psize */     nopsize
91 };
92
93 static devclass_t       idad_devclass;
94
95 static device_method_t idad_methods[] = {
96         DEVMETHOD(device_probe,         idad_probe),
97         DEVMETHOD(device_attach,        idad_attach),
98         DEVMETHOD(device_detach,        idad_detach),
99         { 0, 0 }
100 };
101
102 static driver_t idad_driver = {
103         "idad",
104         idad_methods,
105         sizeof(struct idad_softc)
106 };
107
108 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
109
110 static __inline struct idad_softc *
111 idad_getsoftc(dev_t dev)
112 {
113
114         return ((struct idad_softc *)dev->si_drv1);
115 }
116
117 static int
118 idad_open(dev_t dev, int flags, int fmt, d_thread_t *td)
119 {
120         struct idad_softc *drv;
121         struct disklabel *label;
122
123         drv = idad_getsoftc(dev);
124         if (drv == NULL)
125                 return (ENXIO);
126
127         label = &drv->disk.d_label;
128         bzero(label, sizeof(*label));
129         label->d_type = DTYPE_SCSI;
130         label->d_secsize = drv->secsize;
131         label->d_nsectors = drv->sectors;
132         label->d_ntracks = drv->heads;
133         label->d_ncylinders = drv->cylinders;
134         label->d_secpercyl = drv->sectors * drv->heads;
135         label->d_secperunit = drv->secperunit;
136
137         return (0);
138 }
139
140 static int
141 idad_close(dev_t dev, int flags, int fmt, d_thread_t *td)
142 {
143         struct idad_softc *drv;
144
145         drv = idad_getsoftc(dev);
146         if (drv == NULL)
147                 return (ENXIO);
148         return (0);
149 }
150
151 /*
152  * Read/write routine for a buffer.  Finds the proper unit, range checks
153  * arguments, and schedules the transfer.  Does not wait for the transfer
154  * to complete.  Multi-page transfers are supported.  All I/O requests must
155  * be a multiple of a sector in length.
156  */
157 static void
158 idad_strategy(struct buf *bp)
159 {
160         struct idad_softc *drv;
161         int s;
162
163         drv = idad_getsoftc(bp->b_dev);
164         if (drv == NULL) {
165                 bp->b_error = EINVAL;
166                 goto bad;
167         }
168
169         /*
170          * software write protect check
171          */
172         if (drv->flags & DRV_WRITEPROT && (bp->b_flags & B_READ) == 0) {
173                 bp->b_error = EROFS;
174                 goto bad;
175         }
176
177         /*
178          * If it's a null transfer, return immediately
179          */
180         if (bp->b_bcount == 0)
181                 goto done;
182
183         bp->b_driver1 = drv;
184         s = splbio();
185         devstat_start_transaction(&drv->stats);
186         ida_submit_buf(drv->controller, bp);
187         splx(s);
188         return;
189
190 bad:
191         bp->b_flags |= B_ERROR;
192
193 done:
194         /*
195          * Correctly set the buf to indicate a completed transfer
196          */
197         bp->b_resid = bp->b_bcount;
198         biodone(bp);
199         return;
200 }
201
202 static int
203 idad_dump(dev_t dev)
204 {
205         struct idad_softc *drv;
206         u_int count, blkno, secsize;
207         long blkcnt;
208         int i, error, dumppages;
209         caddr_t va;
210         vm_offset_t addr, a;
211
212         if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
213                 return (error);
214
215         drv = idad_getsoftc(dev);
216         if (drv == NULL)
217                 return (ENXIO);
218
219         addr = 0;
220         blkcnt = howmany(PAGE_SIZE, secsize);
221
222         while (count > 0) {
223                 va = NULL;
224
225                 dumppages = imin(count / blkcnt, MAXDUMPPGS); 
226
227                 for (i = 0; i < dumppages; i++) {
228                         a = addr + (i * PAGE_SIZE);
229                         if (is_physical_memory(a))
230                                 va = pmap_kenter_temporary(trunc_page(a), i);
231                         else
232                                 va = pmap_kenter_temporary(trunc_page(0), i);
233                 }
234
235                 error = ida_command(drv->controller, CMD_WRITE, va,
236                     PAGE_SIZE * dumppages, drv->drive, blkno, DMA_DATA_OUT);
237                 if (error)
238                         return (error);
239
240                 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
241                         return (EINTR);
242
243                 blkno += blkcnt * dumppages;
244                 count -= blkcnt * dumppages;
245                 addr += PAGE_SIZE * dumppages;
246         }
247         return (0);
248 }
249
250 void
251 idad_intr(struct buf *bp)
252 {
253         struct idad_softc *drv = (struct idad_softc *)bp->b_driver1;
254
255         if (bp->b_flags & B_ERROR)
256                 bp->b_error = EIO;
257         else
258                 bp->b_resid = 0;
259
260         devstat_end_transaction_buf(&drv->stats, bp);
261         biodone(bp);
262 }
263
264 static int
265 idad_probe(device_t dev)
266 {
267
268         device_set_desc(dev, "Compaq Logical Drive");
269         return (0);
270 }
271
272 static int
273 idad_attach(device_t dev)
274 {
275         struct ida_drive_info dinfo;
276         struct idad_softc *drv;
277         device_t parent;
278         dev_t dsk;
279         int error;
280
281         drv = (struct idad_softc *)device_get_softc(dev);
282         parent = device_get_parent(dev);
283         drv->controller = (struct ida_softc *)device_get_softc(parent);
284         drv->unit = device_get_unit(dev);
285         drv->drive = drv->controller->num_drives;
286         drv->controller->num_drives++;
287
288         error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
289             &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
290         if (error) {
291                 device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
292                 return (ENXIO);
293         }
294
295         drv->cylinders = dinfo.ncylinders;
296         drv->heads = dinfo.nheads;
297         drv->sectors = dinfo.nsectors;
298         drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
299         drv->secperunit = dinfo.secperunit;
300
301         /* XXX
302          * other initialization
303          */
304         device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
305             drv->secperunit / ((1024 * 1024) / drv->secsize),
306             drv->secperunit, drv->secsize);
307
308         devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize,
309             DEVSTAT_NO_ORDERED_TAGS,
310             DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER,
311             DEVSTAT_PRIORITY_ARRAY);
312
313         dsk = disk_create(drv->unit, &drv->disk, 0, &id_cdevsw);
314
315         dsk->si_drv1 = drv;
316         dsk->si_iosize_max = DFLTPHYS;          /* XXX guess? */
317
318         return (0);
319 }
320
321 static int
322 idad_detach(device_t dev)
323 {
324         struct idad_softc *drv;
325
326         drv = (struct idad_softc *)device_get_softc(dev);
327         devstat_remove_entry(&drv->stats);
328         disk_destroy(&drv->disk);
329         return (0);
330 }