68268fca0c68a1b53eb052401536ff51d3c5900d
[dragonfly.git] / sys / dev / raid / ips / ips_disk.c
1 /*-
2  * Written by: David Jeffery
3  * Copyright (c) 2002 Adaptec Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ips/ips_disk.c,v 1.4 2003/09/22 04:59:07 njl Exp $
28  * $DragonFly: src/sys/dev/raid/ips/ips_disk.c,v 1.14 2008/06/10 17:20:52 dillon Exp $
29  */
30
31 #include <sys/devicestat.h>
32 #include <dev/raid/ips/ips.h>
33 #include <dev/raid/ips/ips_disk.h>
34 #include <sys/stat.h>
35 #include <sys/dtype.h>
36
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <machine/md_var.h>
40
41 static int ipsd_probe(device_t dev);
42 static int ipsd_attach(device_t dev);
43 static int ipsd_detach(device_t dev);
44
45 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
46                              int error);
47 static void ipsd_dump_block_complete(ips_command_t *command);
48
49 static d_open_t ipsd_open;
50 static d_close_t ipsd_close;
51 static d_strategy_t ipsd_strategy;
52 static d_dump_t ipsd_dump;
53
54 static struct dev_ops ipsd_ops = {
55         { "ipsd", IPSD_CDEV_MAJOR, D_DISK },
56         .d_open =       ipsd_open,
57         .d_close =      ipsd_close,
58         .d_strategy =   ipsd_strategy,
59         .d_read =       physread,
60         .d_write =      physwrite,
61         .d_dump =       ipsd_dump,
62 };
63
64 static device_method_t ipsd_methods[] = {
65         DEVMETHOD(device_probe,         ipsd_probe),
66         DEVMETHOD(device_attach,        ipsd_attach),
67         DEVMETHOD(device_detach,        ipsd_detach),
68         { 0, 0 }
69 };
70
71
72 static driver_t ipsd_driver = {
73         "ipsd",
74         ipsd_methods,
75         sizeof(ipsdisk_softc_t)
76 };
77
78 static devclass_t ipsd_devclass;
79 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
80
81 /*
82  * handle opening of disk device.  It must set up all information about
83  * the geometry and size of the disk
84  */
85 static int
86 ipsd_open(struct dev_open_args *ap)
87 {
88         cdev_t dev = ap->a_head.a_dev;
89         ipsdisk_softc_t *dsc = dev->si_drv1;
90
91         if (dsc == NULL)
92                 return (ENXIO);
93         dsc->state |= IPS_DEV_OPEN;
94         DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
95         return 0;
96 }
97
98 static int
99 ipsd_close(struct dev_close_args *ap)
100 {
101         cdev_t dev = ap->a_head.a_dev;
102         ipsdisk_softc_t *dsc = dev->si_drv1;
103
104         dsc->state &= ~IPS_DEV_OPEN;
105         DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
106         return 0;
107 }
108
109 /* ipsd_finish is called to clean up and return a completed IO request */
110 void
111 ipsd_finish(struct bio *bio)
112 {
113         struct buf *bp = bio->bio_buf;
114         ipsdisk_softc_t *dsc;
115
116         dsc = bio->bio_driver_info;
117         if (bp->b_flags & B_ERROR) {
118                 device_printf(dsc->dev, "iobuf error %d\n", bp->b_error);
119         } else {
120                 bp->b_resid = 0;
121         }
122         devstat_end_transaction_buf(&dsc->stats, bp);
123         biodone(bio);
124         ips_start_io_request(dsc->sc);
125 }
126
127
128 static int
129 ipsd_strategy(struct dev_strategy_args *ap)
130 {
131         cdev_t dev = ap->a_head.a_dev;
132         struct bio *bio = ap->a_bio;
133         ipsdisk_softc_t *dsc;
134
135         dsc = dev->si_drv1;
136         DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
137         bio->bio_driver_info = dsc;
138         devstat_start_transaction(&dsc->stats);
139         lockmgr(&dsc->sc->queue_lock, LK_EXCLUSIVE|LK_RETRY);
140         bioqdisksort(&dsc->sc->bio_queue, bio);
141         ips_start_io_request(dsc->sc);
142         lockmgr(&dsc->sc->queue_lock, LK_RELEASE);
143         return(0);
144 }
145
146 static int
147 ipsd_probe(device_t dev)
148 {
149         DEVICE_PRINTF(2, dev, "in probe\n");
150         device_set_desc(dev, "Logical Drive");
151         return 0;
152 }
153
154 static int
155 ipsd_attach(device_t dev)
156 {
157         device_t adapter;
158         ipsdisk_softc_t *dsc;
159         struct disk_info info;
160         u_int totalsectors;
161         u_int nheads, nsectors;
162
163         DEVICE_PRINTF(2, dev, "in attach\n");
164         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
165         bzero(dsc, sizeof(ipsdisk_softc_t));
166         adapter = device_get_parent(dev);
167         dsc->dev = dev;
168         dsc->sc = device_get_softc(adapter);
169         dsc->unit = device_get_unit(dev);
170         dsc->disk_number = (uintptr_t) device_get_ivars(dev);
171         totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
172         if ((totalsectors > 0x400000) &&
173             ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
174                 nheads = IPS_NORM_HEADS;
175                 nsectors = IPS_NORM_SECTORS;
176         } else {
177                 nheads = IPS_COMP_HEADS;
178                 nsectors = IPS_COMP_SECTORS;
179         }
180         devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
181                           DEVSTAT_NO_ORDERED_TAGS,
182                           DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
183                           DEVSTAT_PRIORITY_DISK);
184         dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, &ipsd_ops);
185         dsc->ipsd_dev_t->si_drv1 = dsc;
186         dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
187
188         bzero(&info, sizeof(info));
189         info.d_media_blksize    = IPS_BLKSIZE;          /* mandatory */
190         info.d_media_blocks     = totalsectors;
191
192         info.d_type             = DTYPE_ESDI;           /* optional */
193         info.d_nheads           = nheads;
194         info.d_secpertrack      = nsectors;
195         info.d_ncylinders       = totalsectors / nheads / nsectors;
196         info.d_secpercyl        = nsectors / nheads;
197
198         disk_setdiskinfo(&dsc->ipsd_disk, &info);
199
200         device_printf(dev, "Logical Drive  (%dMB)\n",
201             dsc->sc->drives[dsc->disk_number].sector_count >> 11);
202         return 0;
203 }
204
205 static int
206 ipsd_detach(device_t dev)
207 {
208         ipsdisk_softc_t *dsc;
209
210         DEVICE_PRINTF(2, dev, "in detach\n");
211         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
212         if (dsc->state & IPS_DEV_OPEN)
213                 return (EBUSY);
214         devstat_remove_entry(&dsc->stats);
215         disk_destroy(&dsc->ipsd_disk);
216         return 0;
217 }
218
219
220 static int
221 ipsd_dump(struct dev_dump_args *ap)
222 {
223         cdev_t dev = ap->a_head.a_dev;
224         ips_softc_t *sc;
225         ips_command_t *command;
226         ips_io_cmd *command_struct;
227         ipsdisk_softc_t *dsc;
228         off_t off;
229         uint8_t *va;
230         size_t len;
231         int error = 0;
232
233         dsc = dev->si_drv1;
234         if (dsc == NULL)
235                 return (EINVAL);
236         sc = dsc->sc;
237
238         if (ips_get_free_cmd(sc, &command, 0) != 0) {
239                 kprintf("ipsd: failed to get cmd for dump\n");
240                 return (ENOMEM);
241         }
242
243         command->data_dmatag = sc->sg_dmatag;
244         command->callback = ipsd_dump_block_complete;
245
246         command_struct = (ips_io_cmd *)command->command_buffer;
247         command_struct->id = command->id;
248         command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
249
250         off = ap->a_offset;
251         va = ap->a_virtual;
252
253         size_t length = ap->a_length;
254         while (length > 0) {
255                 len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
256                 command_struct->lba = off / IPS_BLKSIZE;
257                 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
258                     va, len, ipsd_dump_map_sg, command, 0) != 0) {
259                         error = EIO;
260                         break;
261                 }
262                 if (COMMAND_ERROR(&command->status)) {
263                         error = EIO;
264                         break;
265                 }
266
267                 length -= len;
268                 off += len;
269                 va += len;
270         }
271         ips_insert_free_cmd(command->sc, command);
272         return(error);
273 }
274
275 static void
276 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
277 {
278         ips_softc_t *sc;
279         ips_command_t *command;
280         ips_sg_element_t *sg_list;
281         ips_io_cmd *command_struct;
282         int i, length;
283
284         command = (ips_command_t *)arg;
285         sc = command->sc;
286         length = 0;
287
288         if (error) {
289                 kprintf("ipsd_dump_map_sg: error %d\n", error);
290                 command->status.value = IPS_ERROR_STATUS;
291                 return;
292         }
293
294         command_struct = (ips_io_cmd *)command->command_buffer;
295
296         if (nsegs != 1) {
297                 command_struct->segnum = nsegs;
298                 sg_list = (ips_sg_element_t *)((uint8_t *)
299                     command->command_buffer + IPS_COMMAND_LEN);
300                 for (i = 0; i < nsegs; i++) {
301                         sg_list[i].addr = segs[i].ds_addr;
302                         sg_list[i].len = segs[i].ds_len;
303                         length += segs[i].ds_len;
304                 }
305                 command_struct->buffaddr =
306                     (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
307                 command_struct->command = IPS_SG_WRITE_CMD;
308         } else {
309                 command_struct->buffaddr = segs[0].ds_addr;
310                 length = segs[0].ds_len;
311                 command_struct->segnum = 0;
312                 command_struct->command = IPS_WRITE_CMD;
313         }
314
315         length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
316         command_struct->length = length;
317         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
318             BUS_DMASYNC_PREWRITE);
319         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
320             BUS_DMASYNC_PREWRITE);
321
322         sc->ips_issue_cmd(command);
323         sc->ips_poll_cmd(command);
324         return;
325 }
326
327 static void
328 ipsd_dump_block_complete(ips_command_t *command)
329 {
330         if (COMMAND_ERROR(&command->status)) {
331                 kprintf("ipsd_dump completion error= 0x%x\n",
332                        command->status.value);
333         }
334         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
335             BUS_DMASYNC_POSTWRITE);
336         bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
337 }