1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2001 Scott Long 4 * Copyright (c) 2000 BSDi 5 * Copyright (c) 2001 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 * $FreeBSD$ 30 */ 31 32 #include "opt_aac.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/sysctl.h> 38 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/devicestat.h> 42 #include <sys/disk.h> 43 44 #include <vm/vm.h> 45 #include <vm/pmap.h> 46 47 #include <machine/md_var.h> 48 #include <machine/bus.h> 49 #include <sys/rman.h> 50 51 #include <dev/aac/aacreg.h> 52 #include <dev/aac/aac_ioctl.h> 53 #include <dev/aac/aacvar.h> 54 55 /* 56 * Interface to parent. 57 */ 58 static int aac_disk_probe(device_t dev); 59 static int aac_disk_attach(device_t dev); 60 static int aac_disk_detach(device_t dev); 61 62 /* 63 * Interface to the device switch. 64 */ 65 static disk_open_t aac_disk_open; 66 static disk_close_t aac_disk_close; 67 static disk_strategy_t aac_disk_strategy; 68 static dumper_t aac_disk_dump; 69 70 static devclass_t aac_disk_devclass; 71 72 static device_method_t aac_disk_methods[] = { 73 DEVMETHOD(device_probe, aac_disk_probe), 74 DEVMETHOD(device_attach, aac_disk_attach), 75 DEVMETHOD(device_detach, aac_disk_detach), 76 { 0, 0 } 77 }; 78 79 static driver_t aac_disk_driver = { 80 "aacd", 81 aac_disk_methods, 82 sizeof(struct aac_disk) 83 }; 84 85 #define AAC_MAXIO 65536 86 87 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0); 88 89 /* sysctl tunables */ 90 static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */ 91 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max); 92 93 SYSCTL_DECL(_hw_aac); 94 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0, 95 "Max I/O size per transfer to an array"); 96 97 /* 98 * Handle open from generic layer. 99 * 100 * This is called by the diskslice code on first open in order to get the 101 * basic device geometry paramters. 102 */ 103 static int 104 aac_disk_open(struct disk *dp) 105 { 106 struct aac_disk *sc; 107 108 debug_called(4); 109 110 sc = (struct aac_disk *)dp->d_drv1; 111 112 if (sc == NULL) { 113 printf("aac_disk_open: No Softc\n"); 114 return (ENXIO); 115 } 116 117 /* check that the controller is up and running */ 118 if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) { 119 printf("Controller Suspended controller state = 0x%x\n", 120 sc->ad_controller->aac_state); 121 return(ENXIO); 122 } 123 124 sc->ad_flags |= AAC_DISK_OPEN; 125 return (0); 126 } 127 128 /* 129 * Handle last close of the disk device. 130 */ 131 static int 132 aac_disk_close(struct disk *dp) 133 { 134 struct aac_disk *sc; 135 136 debug_called(4); 137 138 sc = (struct aac_disk *)dp->d_drv1; 139 140 if (sc == NULL) 141 return (ENXIO); 142 143 sc->ad_flags &= ~AAC_DISK_OPEN; 144 return (0); 145 } 146 147 /* 148 * Handle an I/O request. 149 */ 150 static void 151 aac_disk_strategy(struct bio *bp) 152 { 153 struct aac_disk *sc; 154 155 debug_called(4); 156 157 sc = (struct aac_disk *)bp->bio_disk->d_drv1; 158 159 /* bogus disk? */ 160 if (sc == NULL) { 161 bp->bio_flags |= BIO_ERROR; 162 bp->bio_error = EINVAL; 163 biodone(bp); 164 return; 165 } 166 167 /* do-nothing operation? */ 168 if (bp->bio_bcount == 0) { 169 bp->bio_resid = bp->bio_bcount; 170 biodone(bp); 171 return; 172 } 173 174 /* perform accounting */ 175 176 /* pass the bio to the controller - it can work out who we are */ 177 AAC_LOCK_ACQUIRE(&sc->ad_controller->aac_io_lock); 178 devstat_start_transaction(&sc->ad_stats); 179 aac_submit_bio(bp); 180 AAC_LOCK_RELEASE(&sc->ad_controller->aac_io_lock); 181 182 return; 183 } 184 185 /* 186 * Map the S/G elements for doing a dump. 187 */ 188 static void 189 aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 190 { 191 struct aac_fib *fib; 192 struct aac_blockwrite *bw; 193 struct aac_sg_table *sg; 194 int i; 195 196 fib = (struct aac_fib *)arg; 197 bw = (struct aac_blockwrite *)&fib->data[0]; 198 sg = &bw->SgMap; 199 200 if (sg != NULL) { 201 sg->SgCount = nsegs; 202 for (i = 0; i < nsegs; i++) { 203 sg->SgEntry[i].SgAddress = segs[i].ds_addr; 204 sg->SgEntry[i].SgByteCount = segs[i].ds_len; 205 } 206 fib->Header.Size = nsegs * sizeof(struct aac_sg_entry); 207 } 208 } 209 210 /* 211 * Dump memory out to an array 212 * 213 * Send out one command at a time with up to AAC_MAXIO of data. 214 */ 215 static int 216 aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 217 { 218 struct aac_disk *ad; 219 struct aac_softc *sc; 220 struct aac_fib *fib; 221 struct aac_blockwrite *bw; 222 size_t len; 223 int size; 224 static bus_dmamap_t dump_datamap; 225 static int first = 0; 226 struct disk *dp; 227 228 dp = arg; 229 ad = dp->d_drv1; 230 231 if (ad == NULL) 232 return (EINVAL); 233 234 sc= ad->ad_controller; 235 236 if (!first) { 237 first = 1; 238 if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) { 239 printf("bus_dmamap_create failed\n"); 240 return (ENOMEM); 241 } 242 } 243 244 aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE); 245 bw = (struct aac_blockwrite *)&fib->data[0]; 246 247 while (length > 0) { 248 len = (length > AAC_MAXIO) ? AAC_MAXIO : length; 249 bw->Command = VM_CtBlockWrite; 250 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 251 bw->BlockNumber = offset / AAC_BLOCK_SIZE; 252 bw->ByteCount = len; 253 bw->Stable = CUNSTABLE; 254 bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual, 255 len, aac_dump_map_sg, fib, 0); 256 bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap, 257 BUS_DMASYNC_PREWRITE); 258 259 /* fib->Header.Size is set in aac_dump_map_sg */ 260 size = fib->Header.Size + sizeof(struct aac_blockwrite); 261 262 if (aac_sync_fib(sc, ContainerCommand, 0, fib, size)) { 263 printf("Error dumping block 0x%x\n", physical); 264 return (EIO); 265 } 266 length -= len; 267 offset += len; 268 } 269 270 return (0); 271 } 272 273 /* 274 * Handle completion of an I/O request. 275 */ 276 void 277 aac_biodone(struct bio *bp) 278 { 279 struct aac_disk *sc; 280 281 debug_called(4); 282 283 sc = (struct aac_disk *)bp->bio_disk->d_drv1; 284 285 devstat_end_transaction_bio(&sc->ad_stats, bp); 286 if (bp->bio_flags & BIO_ERROR) 287 disk_err(bp, "hard error", -1, 1); 288 289 biodone(bp); 290 } 291 292 /* 293 * Stub only. 294 */ 295 static int 296 aac_disk_probe(device_t dev) 297 { 298 299 debug_called(2); 300 301 return (0); 302 } 303 304 /* 305 * Attach a unit to the controller. 306 */ 307 static int 308 aac_disk_attach(device_t dev) 309 { 310 struct aac_disk *sc; 311 312 debug_called(1); 313 314 sc = (struct aac_disk *)device_get_softc(dev); 315 316 /* initialise our softc */ 317 sc->ad_controller = 318 (struct aac_softc *)device_get_softc(device_get_parent(dev)); 319 sc->ad_container = device_get_ivars(dev); 320 sc->ad_dev = dev; 321 322 /* 323 * require that extended translation be enabled - other drivers read the 324 * disk! 325 */ 326 sc->ad_size = sc->ad_container->co_mntobj.Capacity; 327 if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */ 328 sc->ad_heads = 255; 329 sc->ad_sectors = 63; 330 } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */ 331 sc->ad_heads = 128; 332 sc->ad_sectors = 32; 333 } else { 334 sc->ad_heads = 64; 335 sc->ad_sectors = 32; 336 } 337 sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors)); 338 339 device_printf(dev, "%uMB (%u sectors)\n", 340 sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE), 341 sc->ad_size); 342 343 devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev), 344 AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS, 345 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 346 DEVSTAT_PRIORITY_ARRAY); 347 348 /* attach a generic disk device to ourselves */ 349 sc->unit = device_get_unit(dev); 350 sc->ad_disk.d_drv1 = sc; 351 sc->ad_disk.d_name = "aacd"; 352 sc->ad_disk.d_maxsize = aac_iosize_max; 353 sc->ad_disk.d_open = aac_disk_open; 354 sc->ad_disk.d_close = aac_disk_close; 355 sc->ad_disk.d_strategy = aac_disk_strategy; 356 sc->ad_disk.d_dump = aac_disk_dump; 357 sc->ad_disk.d_sectorsize = AAC_BLOCK_SIZE; 358 sc->ad_disk.d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE; 359 sc->ad_disk.d_fwsectors = sc->ad_sectors; 360 sc->ad_disk.d_fwheads = sc->ad_heads; 361 disk_create(sc->unit, &sc->ad_disk, DISKFLAG_NOGIANT, NULL, NULL); 362 363 return (0); 364 } 365 366 /* 367 * Disconnect ourselves from the system. 368 */ 369 static int 370 aac_disk_detach(device_t dev) 371 { 372 struct aac_disk *sc; 373 374 debug_called(2); 375 376 sc = (struct aac_disk *)device_get_softc(dev); 377 378 if (sc->ad_flags & AAC_DISK_OPEN) 379 return(EBUSY); 380 381 devstat_remove_entry(&sc->ad_stats); 382 disk_destroy(&sc->ad_disk); 383 384 return(0); 385 } 386