make_autoclone_dev.9: Fix for return value of devfs_clone_bitmap_set().
[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  */
29
30 #include <sys/devicestat.h>
31 #include <dev/raid/ips/ips.h>
32 #include <dev/raid/ips/ips_disk.h>
33 #include <sys/stat.h>
34 #include <sys/dtype.h>
35
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 #include <machine/md_var.h>
39
40 static int ipsd_probe(device_t dev);
41 static int ipsd_attach(device_t dev);
42 static int ipsd_detach(device_t dev);
43
44 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
45                              int error);
46 static void ipsd_dump_block_complete(ips_command_t *command);
47
48 static d_open_t ipsd_open;
49 static d_close_t ipsd_close;
50 static d_strategy_t ipsd_strategy;
51 static d_dump_t ipsd_dump;
52
53 static struct dev_ops ipsd_ops = {
54         { "ipsd", 0, D_DISK },
55         .d_open =       ipsd_open,
56         .d_close =      ipsd_close,
57         .d_strategy =   ipsd_strategy,
58         .d_read =       physread,
59         .d_write =      physwrite,
60         .d_dump =       ipsd_dump,
61 };
62
63 static device_method_t ipsd_methods[] = {
64         DEVMETHOD(device_probe,         ipsd_probe),
65         DEVMETHOD(device_attach,        ipsd_attach),
66         DEVMETHOD(device_detach,        ipsd_detach),
67         DEVMETHOD_END
68 };
69
70
71 static driver_t ipsd_driver = {
72         "ipsd",
73         ipsd_methods,
74         sizeof(ipsdisk_softc_t)
75 };
76
77 static devclass_t ipsd_devclass;
78 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, NULL, NULL);
79
80 /*
81  * handle opening of disk device.  It must set up all information about
82  * the geometry and size of the disk
83  */
84 static int
85 ipsd_open(struct dev_open_args *ap)
86 {
87         cdev_t dev = ap->a_head.a_dev;
88         ipsdisk_softc_t *dsc = dev->si_drv1;
89
90         if (dsc == NULL)
91                 return (ENXIO);
92         dsc->state |= IPS_DEV_OPEN;
93         DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
94         return 0;
95 }
96
97 static int
98 ipsd_close(struct dev_close_args *ap)
99 {
100         cdev_t dev = ap->a_head.a_dev;
101         ipsdisk_softc_t *dsc = dev->si_drv1;
102
103         dsc->state &= ~IPS_DEV_OPEN;
104         DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
105         return 0;
106 }
107
108 /* ipsd_finish is called to clean up and return a completed IO request */
109 void
110 ipsd_finish(struct bio *bio)
111 {
112         struct buf *bp = bio->bio_buf;
113         ipsdisk_softc_t *dsc;
114
115         dsc = bio->bio_driver_info;
116         if (bp->b_flags & B_ERROR) {
117                 device_printf(dsc->dev, "iobuf error %d\n", bp->b_error);
118         } else {
119                 bp->b_resid = 0;
120         }
121         devstat_end_transaction_buf(&dsc->stats, bp);
122         biodone(bio);
123         ips_start_io_request(dsc->sc);
124 }
125
126
127 static int
128 ipsd_strategy(struct dev_strategy_args *ap)
129 {
130         cdev_t dev = ap->a_head.a_dev;
131         struct bio *bio = ap->a_bio;
132         ipsdisk_softc_t *dsc;
133
134         dsc = dev->si_drv1;
135         DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
136         bio->bio_driver_info = dsc;
137         devstat_start_transaction(&dsc->stats);
138         lockmgr(&dsc->sc->queue_lock, LK_EXCLUSIVE|LK_RETRY);
139         bioqdisksort(&dsc->sc->bio_queue, bio);
140         ips_start_io_request(dsc->sc);
141         lockmgr(&dsc->sc->queue_lock, LK_RELEASE);
142         return(0);
143 }
144
145 static int
146 ipsd_probe(device_t dev)
147 {
148         DEVICE_PRINTF(2, dev, "in probe\n");
149         device_set_desc(dev, "Logical Drive");
150         return 0;
151 }
152
153 static int
154 ipsd_attach(device_t dev)
155 {
156         device_t adapter;
157         ipsdisk_softc_t *dsc;
158         struct disk_info info;
159         u_int totalsectors;
160         u_int nheads, nsectors;
161
162         DEVICE_PRINTF(2, dev, "in attach\n");
163         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
164         bzero(dsc, sizeof(ipsdisk_softc_t));
165         adapter = device_get_parent(dev);
166         dsc->dev = dev;
167         dsc->sc = device_get_softc(adapter);
168         dsc->unit = device_get_unit(dev);
169         dsc->disk_number = (uintptr_t) device_get_ivars(dev);
170         totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
171         if ((totalsectors > 0x400000) &&
172             ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
173                 nheads = IPS_NORM_HEADS;
174                 nsectors = IPS_NORM_SECTORS;
175         } else {
176                 nheads = IPS_COMP_HEADS;
177                 nsectors = IPS_COMP_SECTORS;
178         }
179         devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
180                           DEVSTAT_NO_ORDERED_TAGS,
181                           DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
182                           DEVSTAT_PRIORITY_DISK);
183         dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, &ipsd_ops);
184         dsc->ipsd_dev_t->si_drv1 = dsc;
185         dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
186
187         bzero(&info, sizeof(info));
188         info.d_media_blksize    = IPS_BLKSIZE;          /* mandatory */
189         info.d_media_blocks     = totalsectors;
190
191         info.d_type             = DTYPE_ESDI;           /* optional */
192         info.d_nheads           = nheads;
193         info.d_secpertrack      = nsectors;
194         info.d_ncylinders       = totalsectors / nheads / nsectors;
195         info.d_secpercyl        = nsectors / nheads;
196
197         disk_setdiskinfo(&dsc->ipsd_disk, &info);
198
199         device_printf(dev, "Logical Drive  (%dMB)\n",
200             dsc->sc->drives[dsc->disk_number].sector_count >> 11);
201         return 0;
202 }
203
204 static int
205 ipsd_detach(device_t dev)
206 {
207         ipsdisk_softc_t *dsc;
208
209         DEVICE_PRINTF(2, dev, "in detach\n");
210         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
211         if (dsc->state & IPS_DEV_OPEN)
212                 return (EBUSY);
213         devstat_remove_entry(&dsc->stats);
214         disk_destroy(&dsc->ipsd_disk);
215         return 0;
216 }
217
218
219 static int
220 ipsd_dump(struct dev_dump_args *ap)
221 {
222         cdev_t dev = ap->a_head.a_dev;
223         ips_softc_t *sc;
224         ips_command_t *command;
225         ips_io_cmd *command_struct;
226         ipsdisk_softc_t *dsc;
227         off_t off;
228         uint8_t *va;
229         size_t len;
230         int error = 0;
231
232         dsc = dev->si_drv1;
233         if (dsc == NULL)
234                 return (EINVAL);
235         sc = dsc->sc;
236
237         if (ips_get_free_cmd(sc, &command, 0) != 0) {
238                 kprintf("ipsd: failed to get cmd for dump\n");
239                 return (ENOMEM);
240         }
241
242         command->data_dmatag = sc->sg_dmatag;
243         command->callback = ipsd_dump_block_complete;
244
245         command_struct = (ips_io_cmd *)command->command_buffer;
246         command_struct->id = command->id;
247         command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
248
249         off = ap->a_offset;
250         va = ap->a_virtual;
251
252         size_t length = ap->a_length;
253         while (length > 0) {
254                 len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
255                 command_struct->lba = off / IPS_BLKSIZE;
256                 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
257                     va, len, ipsd_dump_map_sg, command, 0) != 0) {
258                         error = EIO;
259                         break;
260                 }
261                 if (COMMAND_ERROR(&command->status)) {
262                         error = EIO;
263                         break;
264                 }
265
266                 length -= len;
267                 off += len;
268                 va += len;
269         }
270         ips_insert_free_cmd(command->sc, command);
271         return(error);
272 }
273
274 static void
275 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
276 {
277         ips_softc_t *sc;
278         ips_command_t *command;
279         ips_sg_element_t *sg_list;
280         ips_io_cmd *command_struct;
281         int i, length;
282
283         command = (ips_command_t *)arg;
284         sc = command->sc;
285         length = 0;
286
287         if (error) {
288                 kprintf("ipsd_dump_map_sg: error %d\n", error);
289                 command->status.value = IPS_ERROR_STATUS;
290                 return;
291         }
292
293         command_struct = (ips_io_cmd *)command->command_buffer;
294
295         if (nsegs != 1) {
296                 command_struct->segnum = nsegs;
297                 sg_list = (ips_sg_element_t *)((uint8_t *)
298                     command->command_buffer + IPS_COMMAND_LEN);
299                 for (i = 0; i < nsegs; i++) {
300                         sg_list[i].addr = segs[i].ds_addr;
301                         sg_list[i].len = segs[i].ds_len;
302                         length += segs[i].ds_len;
303                 }
304                 command_struct->buffaddr =
305                     (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
306                 command_struct->command = IPS_SG_WRITE_CMD;
307         } else {
308                 command_struct->buffaddr = segs[0].ds_addr;
309                 length = segs[0].ds_len;
310                 command_struct->segnum = 0;
311                 command_struct->command = IPS_WRITE_CMD;
312         }
313
314         length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
315         command_struct->length = length;
316         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
317             BUS_DMASYNC_PREWRITE);
318         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
319             BUS_DMASYNC_PREWRITE);
320
321         sc->ips_issue_cmd(command);
322         sc->ips_poll_cmd(command);
323         return;
324 }
325
326 static void
327 ipsd_dump_block_complete(ips_command_t *command)
328 {
329         if (COMMAND_ERROR(&command->status)) {
330                 kprintf("ipsd_dump completion error= 0x%x\n",
331                        command->status.value);
332         }
333         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
334             BUS_DMASYNC_POSTWRITE);
335         bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
336 }