2 * SPDX-License-Identifier: BSD-2-Clause
4 * Written by: David Jeffery
5 * Copyright (c) 2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <dev/ips/ipsreg.h>
34 #include <dev/ips/ips.h>
35 #include <dev/ips/ips_disk.h>
38 static int ipsd_probe(device_t dev);
39 static int ipsd_attach(device_t dev);
40 static int ipsd_detach(device_t dev);
42 static int ipsd_dump(void *arg, void *virtual, off_t offset, size_t length);
43 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
45 static void ipsd_dump_block_complete(ips_command_t *command);
47 static disk_open_t ipsd_open;
48 static disk_close_t ipsd_close;
49 static disk_strategy_t ipsd_strategy;
51 static device_method_t ipsd_methods[] = {
52 DEVMETHOD(device_probe, ipsd_probe),
53 DEVMETHOD(device_attach, ipsd_attach),
54 DEVMETHOD(device_detach, ipsd_detach),
58 static driver_t ipsd_driver = {
61 sizeof(ipsdisk_softc_t)
64 DRIVER_MODULE(ipsd, ips, ipsd_driver, 0, 0);
66 /* handle opening of disk device. It must set up all
67 information about the geometry and size of the disk */
68 static int ipsd_open(struct disk *dp)
70 ipsdisk_softc_t *dsc = dp->d_drv1;
72 dsc->state |= IPS_DEV_OPEN;
73 DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
77 static int ipsd_close(struct disk *dp)
79 ipsdisk_softc_t *dsc = dp->d_drv1;
80 dsc->state &= ~IPS_DEV_OPEN;
81 DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
85 /* ipsd_finish is called to clean up and return a completed IO request */
86 void ipsd_finish(struct bio *iobuf)
89 dsc = iobuf->bio_disk->d_drv1;
91 if (iobuf->bio_flags & BIO_ERROR) {
93 dsc = iobuf->bio_disk->d_drv1;
94 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
99 ips_start_io_request(dsc->sc);
103 static void ipsd_strategy(struct bio *iobuf)
105 ipsdisk_softc_t *dsc;
107 dsc = iobuf->bio_disk->d_drv1;
108 DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
109 iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
111 if ((iobuf->bio_cmd != BIO_READ) &&
112 (iobuf->bio_cmd != BIO_WRITE)) {
113 biofinish(iobuf, NULL, EOPNOTSUPP);
117 mtx_lock(&dsc->sc->queue_mtx);
118 bioq_insert_tail(&dsc->sc->queue, iobuf);
119 ips_start_io_request(dsc->sc);
120 mtx_unlock(&dsc->sc->queue_mtx);
123 static int ipsd_probe(device_t dev)
125 DEVICE_PRINTF(2,dev, "in probe\n");
126 device_set_desc(dev, "Logical Drive");
130 static int ipsd_attach(device_t dev)
133 ipsdisk_softc_t *dsc;
136 DEVICE_PRINTF(2,dev, "in attach\n");
138 dsc = (ipsdisk_softc_t *)device_get_softc(dev);
139 bzero(dsc, sizeof(ipsdisk_softc_t));
140 adapter = device_get_parent(dev);
142 dsc->sc = device_get_softc(adapter);
143 dsc->unit = device_get_unit(dev);
144 dsc->disk_number = (uintptr_t) device_get_ivars(dev);
145 dsc->ipsd_disk = disk_alloc();
146 dsc->ipsd_disk->d_drv1 = dsc;
147 dsc->ipsd_disk->d_name = "ipsd";
148 dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
149 dsc->ipsd_disk->d_open = ipsd_open;
150 dsc->ipsd_disk->d_close = ipsd_close;
151 dsc->ipsd_disk->d_strategy = ipsd_strategy;
152 dsc->ipsd_disk->d_dump = ipsd_dump;
154 totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
155 if ((totalsectors > 0x400000) &&
156 ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
157 dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
158 dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
160 dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
161 dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
163 dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
164 dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
165 dsc->ipsd_disk->d_unit = dsc->unit;
166 dsc->ipsd_disk->d_flags = 0;
167 disk_create(dsc->ipsd_disk, DISK_VERSION);
169 device_printf(dev, "Logical Drive (%dMB)\n",
170 dsc->sc->drives[dsc->disk_number].sector_count >> 11);
174 static int ipsd_detach(device_t dev)
176 ipsdisk_softc_t *dsc;
178 DEVICE_PRINTF(2, dev,"in detach\n");
179 dsc = (ipsdisk_softc_t *)device_get_softc(dev);
180 if(dsc->state & IPS_DEV_OPEN)
182 disk_destroy(dsc->ipsd_disk);
187 ipsd_dump(void *arg, void *virtual, off_t offset, size_t length)
189 ipsdisk_softc_t *dsc;
191 ips_command_t *command;
192 ips_io_cmd *command_struct;
206 if (ips_get_free_cmd(sc, &command, 0) != 0) {
207 printf("ipsd: failed to get cmd for dump\n");
211 command->data_dmatag = sc->sg_dmatag;
212 command->callback = ipsd_dump_block_complete;
214 command_struct = (ips_io_cmd *)command->command_buffer;
215 command_struct->id = command->id;
216 command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
223 (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
225 command_struct->lba = off / IPS_BLKSIZE;
227 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
228 va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
232 if (COMMAND_ERROR(command)) {
239 va = (uint8_t *)va + len;
242 ips_insert_free_cmd(command->sc, command);
247 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
250 ips_command_t *command;
251 ips_sg_element_t *sg_list;
252 ips_io_cmd *command_struct;
255 command = (ips_command_t *)arg;
260 printf("ipsd_dump_map_sg: error %d\n", error);
261 ips_set_error(command, error);
265 command_struct = (ips_io_cmd *)command->command_buffer;
268 command_struct->segnum = nsegs;
269 sg_list = (ips_sg_element_t *)((uint8_t *)
270 command->command_buffer + IPS_COMMAND_LEN);
271 for (i = 0; i < nsegs; i++) {
272 sg_list[i].addr = segs[i].ds_addr;
273 sg_list[i].len = segs[i].ds_len;
274 length += segs[i].ds_len;
276 command_struct->buffaddr =
277 (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
278 command_struct->command = IPS_SG_WRITE_CMD;
280 command_struct->buffaddr = segs[0].ds_addr;
281 length = segs[0].ds_len;
282 command_struct->segnum = 0;
283 command_struct->command = IPS_WRITE_CMD;
286 length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
287 command_struct->length = length;
288 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
289 BUS_DMASYNC_PREWRITE);
290 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
291 BUS_DMASYNC_PREWRITE);
293 sc->ips_issue_cmd(command);
294 sc->ips_poll_cmd(command);
299 ipsd_dump_block_complete(ips_command_t *command)
302 if (COMMAND_ERROR(command))
303 printf("ipsd_dump completion error= 0x%x\n",
304 command->status.value);
306 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
307 BUS_DMASYNC_POSTWRITE);
308 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);