1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Written by: David Jeffery 5 * Copyright (c) 2002 Adaptec Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <dev/ips/ipsreg.h> 34 #include <dev/ips/ips.h> 35 #include <dev/ips/ips_disk.h> 36 #include <sys/stat.h> 37 38 static int ipsd_probe(device_t dev); 39 static int ipsd_attach(device_t dev); 40 static int ipsd_detach(device_t dev); 41 42 static int ipsd_dump(void *arg, void *virtual, vm_offset_t physical, 43 off_t offset, size_t length); 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 disk_open_t ipsd_open; 49 static disk_close_t ipsd_close; 50 static disk_strategy_t ipsd_strategy; 51 52 static device_method_t ipsd_methods[] = { 53 DEVMETHOD(device_probe, ipsd_probe), 54 DEVMETHOD(device_attach, ipsd_attach), 55 DEVMETHOD(device_detach, ipsd_detach), 56 { 0, 0 } 57 }; 58 59 static driver_t ipsd_driver = { 60 "ipsd", 61 ipsd_methods, 62 sizeof(ipsdisk_softc_t) 63 }; 64 65 DRIVER_MODULE(ipsd, ips, ipsd_driver, 0, 0); 66 67 /* handle opening of disk device. It must set up all 68 information about the geometry and size of the disk */ 69 static int ipsd_open(struct disk *dp) 70 { 71 ipsdisk_softc_t *dsc = dp->d_drv1; 72 73 dsc->state |= IPS_DEV_OPEN; 74 DEVICE_PRINTF(2, dsc->dev, "I'm open\n"); 75 return 0; 76 } 77 78 static int ipsd_close(struct disk *dp) 79 { 80 ipsdisk_softc_t *dsc = dp->d_drv1; 81 dsc->state &= ~IPS_DEV_OPEN; 82 DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n"); 83 return 0; 84 } 85 86 /* ipsd_finish is called to clean up and return a completed IO request */ 87 void ipsd_finish(struct bio *iobuf) 88 { 89 ipsdisk_softc_t *dsc; 90 dsc = iobuf->bio_disk->d_drv1; 91 92 if (iobuf->bio_flags & BIO_ERROR) { 93 ipsdisk_softc_t *dsc; 94 dsc = iobuf->bio_disk->d_drv1; 95 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error); 96 } else 97 iobuf->bio_resid = 0; 98 99 biodone(iobuf); 100 ips_start_io_request(dsc->sc); 101 } 102 103 104 static void ipsd_strategy(struct bio *iobuf) 105 { 106 ipsdisk_softc_t *dsc; 107 108 dsc = iobuf->bio_disk->d_drv1; 109 DEVICE_PRINTF(8,dsc->dev,"in strategy\n"); 110 iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum; 111 112 if ((iobuf->bio_cmd != BIO_READ) && 113 (iobuf->bio_cmd != BIO_WRITE)) { 114 biofinish(iobuf, NULL, EOPNOTSUPP); 115 return; 116 } 117 118 mtx_lock(&dsc->sc->queue_mtx); 119 bioq_insert_tail(&dsc->sc->queue, iobuf); 120 ips_start_io_request(dsc->sc); 121 mtx_unlock(&dsc->sc->queue_mtx); 122 } 123 124 static int ipsd_probe(device_t dev) 125 { 126 DEVICE_PRINTF(2,dev, "in probe\n"); 127 device_set_desc(dev, "Logical Drive"); 128 return 0; 129 } 130 131 static int ipsd_attach(device_t dev) 132 { 133 device_t adapter; 134 ipsdisk_softc_t *dsc; 135 u_int totalsectors; 136 137 DEVICE_PRINTF(2,dev, "in attach\n"); 138 139 dsc = (ipsdisk_softc_t *)device_get_softc(dev); 140 bzero(dsc, sizeof(ipsdisk_softc_t)); 141 adapter = device_get_parent(dev); 142 dsc->dev = dev; 143 dsc->sc = device_get_softc(adapter); 144 dsc->unit = device_get_unit(dev); 145 dsc->disk_number = (uintptr_t) device_get_ivars(dev); 146 dsc->ipsd_disk = disk_alloc(); 147 dsc->ipsd_disk->d_drv1 = dsc; 148 dsc->ipsd_disk->d_name = "ipsd"; 149 dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE; 150 dsc->ipsd_disk->d_open = ipsd_open; 151 dsc->ipsd_disk->d_close = ipsd_close; 152 dsc->ipsd_disk->d_strategy = ipsd_strategy; 153 dsc->ipsd_disk->d_dump = ipsd_dump; 154 155 totalsectors = dsc->sc->drives[dsc->disk_number].sector_count; 156 if ((totalsectors > 0x400000) && 157 ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) { 158 dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS; 159 dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS; 160 } else { 161 dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS; 162 dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS; 163 } 164 dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE; 165 dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE; 166 dsc->ipsd_disk->d_unit = dsc->unit; 167 dsc->ipsd_disk->d_flags = 0; 168 disk_create(dsc->ipsd_disk, DISK_VERSION); 169 170 device_printf(dev, "Logical Drive (%dMB)\n", 171 dsc->sc->drives[dsc->disk_number].sector_count >> 11); 172 return 0; 173 } 174 175 static int ipsd_detach(device_t dev) 176 { 177 ipsdisk_softc_t *dsc; 178 179 DEVICE_PRINTF(2, dev,"in detach\n"); 180 dsc = (ipsdisk_softc_t *)device_get_softc(dev); 181 if(dsc->state & IPS_DEV_OPEN) 182 return (EBUSY); 183 disk_destroy(dsc->ipsd_disk); 184 return 0; 185 } 186 187 static int 188 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 189 size_t length) 190 { 191 ipsdisk_softc_t *dsc; 192 ips_softc_t *sc; 193 ips_command_t *command; 194 ips_io_cmd *command_struct; 195 struct disk *dp; 196 void *va; 197 off_t off; 198 size_t len; 199 int error = 0; 200 201 dp = arg; 202 dsc = dp->d_drv1; 203 204 if (dsc == NULL) 205 return (EINVAL); 206 sc = dsc->sc; 207 208 if (ips_get_free_cmd(sc, &command, 0) != 0) { 209 printf("ipsd: failed to get cmd for dump\n"); 210 return (ENOMEM); 211 } 212 213 command->data_dmatag = sc->sg_dmatag; 214 command->callback = ipsd_dump_block_complete; 215 216 command_struct = (ips_io_cmd *)command->command_buffer; 217 command_struct->id = command->id; 218 command_struct->drivenum= sc->drives[dsc->disk_number].drivenum; 219 220 off = offset; 221 va = virtual; 222 223 while (length > 0) { 224 len = 225 (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length; 226 227 command_struct->lba = off / IPS_BLKSIZE; 228 229 if (bus_dmamap_load(command->data_dmatag, command->data_dmamap, 230 va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) { 231 error = EIO; 232 break; 233 } 234 if (COMMAND_ERROR(command)) { 235 error = EIO; 236 break; 237 } 238 239 length -= len; 240 off += len; 241 va = (uint8_t *)va + len; 242 } 243 244 ips_insert_free_cmd(command->sc, command); 245 return (error); 246 } 247 248 static void 249 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 250 { 251 ips_softc_t *sc; 252 ips_command_t *command; 253 ips_sg_element_t *sg_list; 254 ips_io_cmd *command_struct; 255 int i, length; 256 257 command = (ips_command_t *)arg; 258 sc = command->sc; 259 length = 0; 260 261 if (error) { 262 printf("ipsd_dump_map_sg: error %d\n", error); 263 ips_set_error(command, error); 264 return; 265 } 266 267 command_struct = (ips_io_cmd *)command->command_buffer; 268 269 if (nsegs != 1) { 270 command_struct->segnum = nsegs; 271 sg_list = (ips_sg_element_t *)((uint8_t *) 272 command->command_buffer + IPS_COMMAND_LEN); 273 for (i = 0; i < nsegs; i++) { 274 sg_list[i].addr = segs[i].ds_addr; 275 sg_list[i].len = segs[i].ds_len; 276 length += segs[i].ds_len; 277 } 278 command_struct->buffaddr = 279 (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN; 280 command_struct->command = IPS_SG_WRITE_CMD; 281 } else { 282 command_struct->buffaddr = segs[0].ds_addr; 283 length = segs[0].ds_len; 284 command_struct->segnum = 0; 285 command_struct->command = IPS_WRITE_CMD; 286 } 287 288 length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE; 289 command_struct->length = length; 290 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap, 291 BUS_DMASYNC_PREWRITE); 292 bus_dmamap_sync(command->data_dmatag, command->data_dmamap, 293 BUS_DMASYNC_PREWRITE); 294 295 sc->ips_issue_cmd(command); 296 sc->ips_poll_cmd(command); 297 return; 298 } 299 300 static void 301 ipsd_dump_block_complete(ips_command_t *command) 302 { 303 304 if (COMMAND_ERROR(command)) 305 printf("ipsd_dump completion error= 0x%x\n", 306 command->status.value); 307 308 bus_dmamap_sync(command->data_dmatag, command->data_dmamap, 309 BUS_DMASYNC_POSTWRITE); 310 bus_dmamap_unload(command->data_dmatag, command->data_dmamap); 311 } 312