Merge from vendor branch BINUTILS:
[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.6 2005/08/09 16:23:13 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
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 int ipsd_dump_helper(dev_t dev, u_int count, u_int blkno, u_int secsize);
45 #if 0
46 static int ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length);
47 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
48                              int error);
49 static void ipsd_dump_block_complete(ips_command_t *command);
50 #endif
51
52 static disk_open_t ipsd_open;
53 static disk_close_t ipsd_close;
54 static disk_strategy_t ipsd_strategy;
55
56 static struct cdevsw ipsd_cdevsw = {
57         .d_name         = "ipsd",
58         .d_maj          = IPSD_CDEV_MAJOR,
59         .d_flags        = D_DISK,
60         .d_port         = NULL,
61         .d_clone        = NULL,
62         .old_open       = ipsd_open,
63         .old_close      = ipsd_close,
64         .old_strategy   = ipsd_strategy,
65         .old_read       = physread,
66         .old_write      = physwrite,
67         .old_dump       = ipsd_dump_helper,
68 };
69
70 static device_method_t ipsd_methods[] = {
71         DEVMETHOD(device_probe,         ipsd_probe),
72         DEVMETHOD(device_attach,        ipsd_attach),
73         DEVMETHOD(device_detach,        ipsd_detach),
74         { 0, 0 }
75 };
76
77
78 static driver_t ipsd_driver = {
79         "ipsd",
80         ipsd_methods,
81         sizeof(ipsdisk_softc_t)
82 };
83
84 static devclass_t ipsd_devclass;
85 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
86
87 /*
88  * handle opening of disk device.  It must set up all information about
89  * the geometry and size of the disk
90  */
91 static int
92 ipsd_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
93 {
94         ipsdisk_softc_t *dsc = dev->si_drv1;
95
96         if (dsc == NULL)
97                 return (ENXIO);
98         dsc->state |= IPS_DEV_OPEN;
99         DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
100         return 0;
101 }
102
103 static int
104 ipsd_close(dev_t dev, int oflags, int devtype, d_thread_t *td)
105 {
106         ipsdisk_softc_t *dsc = dev->si_drv1;
107
108         dsc->state &= ~IPS_DEV_OPEN;
109         DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
110         return 0;
111 }
112
113 /* ipsd_finish is called to clean up and return a completed IO request */
114 void
115 ipsd_finish(struct bio *iobuf)
116 {
117         ipsdisk_softc_t *dsc;
118
119         dsc = iobuf->bio_disk->d_drv1;
120         if (iobuf->bio_flags & BIO_ERROR) {
121                 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
122         } else
123                 iobuf->bio_resid = 0;
124         devstat_end_transaction_buf(&dsc->stats, iobuf);
125         biodone(iobuf);
126         ips_start_io_request(dsc->sc);
127 }
128
129
130 static void
131 ipsd_strategy(struct bio *iobuf)
132 {
133         ipsdisk_softc_t *dsc;
134
135         dsc = iobuf->bio_disk->d_drv1;
136         DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
137         iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
138         devstat_start_transaction(&dsc->stats);
139         lwkt_exlock(&dsc->sc->queue_lock, __func__);
140         bufq_insert_tail(&dsc->sc->queue, iobuf);
141         ips_start_io_request(dsc->sc);
142         lwkt_exunlock(&dsc->sc->queue_lock);
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  disklabel *label;
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, 0,
184             &ipsd_cdevsw);
185         dsc->ipsd_dev_t->si_drv1 = dsc;
186         dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
187         label = &dsc->ipsd_disk.d_label;
188         bzero(label, sizeof(*label));
189         label->d_ntracks    = nheads;
190         label->d_nsectors   = nsectors;
191         label->d_type       = DTYPE_ESDI;
192         label->d_secsize    = IPS_BLKSIZE;
193         label->d_ncylinders = totalsectors / nheads / nsectors;
194         label->d_secpercyl  = nsectors / nheads;
195         label->d_secperunit = totalsectors;
196         device_printf(dev, "Logical Drive  (%dMB)\n",
197             dsc->sc->drives[dsc->disk_number].sector_count >> 11);
198         return 0;
199 }
200
201 static int
202 ipsd_detach(device_t dev)
203 {
204         ipsdisk_softc_t *dsc;
205
206         DEVICE_PRINTF(2, dev, "in detach\n");
207         dsc = (ipsdisk_softc_t *)device_get_softc(dev);
208         if (dsc->state & IPS_DEV_OPEN)
209                 return (EBUSY);
210         devstat_remove_entry(&dsc->stats);
211         disk_destroy(&dsc->ipsd_disk);
212         return 0;
213 }
214
215 static int
216 ipsd_dump_helper(dev_t dev, u_int count, u_int blkno, u_int secsize)
217 {
218         printf("dump support for IPS not yet working, will not dump\n");
219         return (ENODEV);
220
221 #if 0
222         long blkcnt;
223         caddr_t va;
224         vm_offset_t addr, a;
225         int dumppages = MAXDUMPPGS;
226         int i;
227
228         addr = 0;
229         blkcnt = howmany(PAGE_SIZE, secsize);
230         while (count > 0) {
231                 va = NULL;
232                 if (count / blkcnt < dumppages)
233                         dumppages = count / blkcnt;
234                 for (i = 0; i < dumppages; i++) {
235                         a = addr + (i * PAGE_SIZE);
236                         if (!is_physical_memory(a))
237                                 a = 0;
238                         va = pmap_kenter_temporary(trunc_page(a), i);
239                 }
240
241                 ipsd_dump(dev, va, 0, blkno, PAGE_SIZE * dumppages);
242                 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
243                         return (EINTR);
244                 blkno += blkcnt * dumppages;
245                 count -= blkcnt * dumppages;
246                 addr += PAGE_SIZE * dumppages;
247         }
248         return (0);
249 #endif
250 }
251
252 #if 0
253
254 static int
255 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
256           size_t length)
257 {
258         dev_t dev = arg;
259         ips_softc_t *sc;
260         ips_command_t *command;
261         ips_io_cmd *command_struct;
262         ipsdisk_softc_t *dsc;
263         off_t off;
264         uint8_t *va;
265         int len;
266         int error = 0;
267
268         dsc = dev->si_drv1;
269         if (dsc == NULL)
270                 return (EINVAL);
271         sc = dsc->sc;
272
273         if (ips_get_free_cmd(sc, &command, 0) != 0) {
274                 printf("ipsd: failed to get cmd for dump\n");
275                 return (ENOMEM);
276         }
277
278         command->data_dmatag = sc->sg_dmatag;
279         command->callback = ipsd_dump_block_complete;
280
281         command_struct = (ips_io_cmd *)command->command_buffer;
282         command_struct->id = command->id;
283         command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
284
285         off = offset;
286         va = virtual;
287
288         while (length > 0) {
289                 len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
290                 command_struct->lba = off / IPS_BLKSIZE;
291                 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
292                     va, len, ipsd_dump_map_sg, command, 0) != 0) {
293                         error = EIO;
294                         break;
295                 }
296                 if (COMMAND_ERROR(&command->status)) {
297                         error = EIO;
298                         break;
299                 }
300
301                 length -= len;
302                 off += len;
303                 va += len;
304         }
305         ips_insert_free_cmd(command->sc, command);
306         return(error);
307 }
308
309 static void
310 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
311 {
312         ips_softc_t *sc;
313         ips_command_t *command;
314         ips_sg_element_t *sg_list;
315         ips_io_cmd *command_struct;
316         int i, length;
317
318         command = (ips_command_t *)arg;
319         sc = command->sc;
320         length = 0;
321
322         if (error) {
323                 printf("ipsd_dump_map_sg: error %d\n", error);
324                 command->status.value = IPS_ERROR_STATUS;
325                 return;
326         }
327
328         command_struct = (ips_io_cmd *)command->command_buffer;
329
330         if (nsegs != 1) {
331                 command_struct->segnum = nsegs;
332                 sg_list = (ips_sg_element_t *)((uint8_t *)
333                     command->command_buffer + IPS_COMMAND_LEN);
334                 for (i = 0; i < nsegs; i++) {
335                         sg_list[i].addr = segs[i].ds_addr;
336                         sg_list[i].len = segs[i].ds_len;
337                         length += segs[i].ds_len;
338                 }
339                 command_struct->buffaddr =
340                     (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
341                 command_struct->command = IPS_SG_WRITE_CMD;
342         } else {
343                 command_struct->buffaddr = segs[0].ds_addr;
344                 length = segs[0].ds_len;
345                 command_struct->segnum = 0;
346                 command_struct->command = IPS_WRITE_CMD;
347         }
348
349         length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
350         command_struct->length = length;
351         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
352             BUS_DMASYNC_PREWRITE);
353         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
354             BUS_DMASYNC_PREWRITE);
355
356         sc->ips_issue_cmd(command);
357         sc->ips_poll_cmd(command);
358         return;
359 }
360
361 static void
362 ipsd_dump_block_complete(ips_command_t *command)
363 {
364         if (COMMAND_ERROR(&command->status)) {
365                 printf("ipsd_dump completion error= 0x%x\n",
366                        command->status.value);
367         }
368         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
369             BUS_DMASYNC_POSTWRITE);
370         bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
371 }
372
373 #endif