2 * Written by: David Jeffery
3 * Copyright (c) 2002 Adaptec Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.7 2006/02/17 19:18:05 dillon Exp $
31 #include <sys/devicestat.h>
32 #include <dev/raid/ips/ips.h>
33 #include <dev/raid/ips/ips_disk.h>
38 #include <machine/md_var.h>
40 static int ipsd_probe(device_t dev);
41 static int ipsd_attach(device_t dev);
42 static int ipsd_detach(device_t dev);
44 static int ipsd_dump_helper(dev_t dev, u_int count, u_int blkno, u_int secsize);
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,
49 static void ipsd_dump_block_complete(ips_command_t *command);
52 static disk_open_t ipsd_open;
53 static disk_close_t ipsd_close;
54 static disk_strategy_t ipsd_strategy;
56 static struct cdevsw ipsd_cdevsw = {
58 .d_maj = IPSD_CDEV_MAJOR,
62 .old_open = ipsd_open,
63 .old_close = ipsd_close,
64 .old_strategy = ipsd_strategy,
66 .old_write = physwrite,
67 .old_dump = ipsd_dump_helper,
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),
78 static driver_t ipsd_driver = {
81 sizeof(ipsdisk_softc_t)
84 static devclass_t ipsd_devclass;
85 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
88 * handle opening of disk device. It must set up all information about
89 * the geometry and size of the disk
92 ipsd_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
94 ipsdisk_softc_t *dsc = dev->si_drv1;
98 dsc->state |= IPS_DEV_OPEN;
99 DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
104 ipsd_close(dev_t dev, int oflags, int devtype, d_thread_t *td)
106 ipsdisk_softc_t *dsc = dev->si_drv1;
108 dsc->state &= ~IPS_DEV_OPEN;
109 DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
113 /* ipsd_finish is called to clean up and return a completed IO request */
115 ipsd_finish(struct bio *bio)
117 struct buf *bp = bio->bio_buf;
118 ipsdisk_softc_t *dsc;
120 dsc = bio->bio_driver_info;
121 if (bp->b_flags & B_ERROR) {
122 device_printf(dsc->dev, "iobuf error %d\n", bp->b_error);
126 devstat_end_transaction_buf(&dsc->stats, bp);
128 ips_start_io_request(dsc->sc);
133 ipsd_strategy(dev_t dev, struct bio *bio)
135 ipsdisk_softc_t *dsc;
138 DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
139 bio->bio_driver_info = dsc;
140 devstat_start_transaction(&dsc->stats);
141 lwkt_exlock(&dsc->sc->queue_lock, __func__);
142 bioq_insert_tail(&dsc->sc->bio_queue, bio);
143 ips_start_io_request(dsc->sc);
144 lwkt_exunlock(&dsc->sc->queue_lock);
148 ipsd_probe(device_t dev)
150 DEVICE_PRINTF(2, dev, "in probe\n");
151 device_set_desc(dev, "Logical Drive");
156 ipsd_attach(device_t dev)
159 ipsdisk_softc_t *dsc;
160 struct disklabel *label;
162 u_int nheads, nsectors;
164 DEVICE_PRINTF(2, dev, "in attach\n");
165 dsc = (ipsdisk_softc_t *)device_get_softc(dev);
166 bzero(dsc, sizeof(ipsdisk_softc_t));
167 adapter = device_get_parent(dev);
169 dsc->sc = device_get_softc(adapter);
170 dsc->unit = device_get_unit(dev);
171 dsc->disk_number = (uintptr_t) device_get_ivars(dev);
172 totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
173 if ((totalsectors > 0x400000) &&
174 ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
175 nheads = IPS_NORM_HEADS;
176 nsectors = IPS_NORM_SECTORS;
178 nheads = IPS_COMP_HEADS;
179 nsectors = IPS_COMP_SECTORS;
181 devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
182 DEVSTAT_NO_ORDERED_TAGS,
183 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
184 DEVSTAT_PRIORITY_DISK);
185 dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, 0,
187 dsc->ipsd_dev_t->si_drv1 = dsc;
188 dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
189 label = &dsc->ipsd_disk.d_label;
190 bzero(label, sizeof(*label));
191 label->d_ntracks = nheads;
192 label->d_nsectors = nsectors;
193 label->d_type = DTYPE_ESDI;
194 label->d_secsize = IPS_BLKSIZE;
195 label->d_ncylinders = totalsectors / nheads / nsectors;
196 label->d_secpercyl = nsectors / nheads;
197 label->d_secperunit = totalsectors;
198 device_printf(dev, "Logical Drive (%dMB)\n",
199 dsc->sc->drives[dsc->disk_number].sector_count >> 11);
204 ipsd_detach(device_t dev)
206 ipsdisk_softc_t *dsc;
208 DEVICE_PRINTF(2, dev, "in detach\n");
209 dsc = (ipsdisk_softc_t *)device_get_softc(dev);
210 if (dsc->state & IPS_DEV_OPEN)
212 devstat_remove_entry(&dsc->stats);
213 disk_destroy(&dsc->ipsd_disk);
218 ipsd_dump_helper(dev_t dev, u_int count, u_int blkno, u_int secsize)
220 printf("dump support for IPS not yet working, will not dump\n");
227 int dumppages = MAXDUMPPGS;
231 blkcnt = howmany(PAGE_SIZE, secsize);
234 if (count / blkcnt < dumppages)
235 dumppages = count / blkcnt;
236 for (i = 0; i < dumppages; i++) {
237 a = addr + (i * PAGE_SIZE);
238 if (!is_physical_memory(a))
240 va = pmap_kenter_temporary(trunc_page(a), i);
243 ipsd_dump(dev, va, 0, blkno, PAGE_SIZE * dumppages);
244 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
246 blkno += blkcnt * dumppages;
247 count -= blkcnt * dumppages;
248 addr += PAGE_SIZE * dumppages;
257 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
262 ips_command_t *command;
263 ips_io_cmd *command_struct;
264 ipsdisk_softc_t *dsc;
275 if (ips_get_free_cmd(sc, &command, 0) != 0) {
276 printf("ipsd: failed to get cmd for dump\n");
280 command->data_dmatag = sc->sg_dmatag;
281 command->callback = ipsd_dump_block_complete;
283 command_struct = (ips_io_cmd *)command->command_buffer;
284 command_struct->id = command->id;
285 command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
291 len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
292 command_struct->lba = off / IPS_BLKSIZE;
293 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
294 va, len, ipsd_dump_map_sg, command, 0) != 0) {
298 if (COMMAND_ERROR(&command->status)) {
307 ips_insert_free_cmd(command->sc, command);
312 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
315 ips_command_t *command;
316 ips_sg_element_t *sg_list;
317 ips_io_cmd *command_struct;
320 command = (ips_command_t *)arg;
325 printf("ipsd_dump_map_sg: error %d\n", error);
326 command->status.value = IPS_ERROR_STATUS;
330 command_struct = (ips_io_cmd *)command->command_buffer;
333 command_struct->segnum = nsegs;
334 sg_list = (ips_sg_element_t *)((uint8_t *)
335 command->command_buffer + IPS_COMMAND_LEN);
336 for (i = 0; i < nsegs; i++) {
337 sg_list[i].addr = segs[i].ds_addr;
338 sg_list[i].len = segs[i].ds_len;
339 length += segs[i].ds_len;
341 command_struct->buffaddr =
342 (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
343 command_struct->command = IPS_SG_WRITE_CMD;
345 command_struct->buffaddr = segs[0].ds_addr;
346 length = segs[0].ds_len;
347 command_struct->segnum = 0;
348 command_struct->command = IPS_WRITE_CMD;
351 length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
352 command_struct->length = length;
353 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
354 BUS_DMASYNC_PREWRITE);
355 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
356 BUS_DMASYNC_PREWRITE);
358 sc->ips_issue_cmd(command);
359 sc->ips_poll_cmd(command);
364 ipsd_dump_block_complete(ips_command_t *command)
366 if (COMMAND_ERROR(&command->status)) {
367 printf("ipsd_dump completion error= 0x%x\n",
368 command->status.value);
370 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
371 BUS_DMASYNC_POSTWRITE);
372 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);