1 /*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * Portions of this software may have been developed with reference to 26 * the SD Simplified Specification. The following disclaimer may apply: 27 * 28 * The following conditions apply to the release of the simplified 29 * specification ("Simplified Specification") by the SD Card Association and 30 * the SD Group. The Simplified Specification is a subset of the complete SD 31 * Specification which is owned by the SD Card Association and the SD 32 * Group. This Simplified Specification is provided on a non-confidential 33 * basis subject to the disclaimers below. Any implementation of the 34 * Simplified Specification may require a license from the SD Card 35 * Association, SD Group, SD-3C LLC or other third parties. 36 * 37 * Disclaimers: 38 * 39 * The information contained in the Simplified Specification is presented only 40 * as a standard specification for SD Cards and SD Host/Ancillary products and 41 * is provided "AS-IS" without any representations or warranties of any 42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43 * Card Association for any damages, any infringements of patents or other 44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45 * parties, which may result from its use. No license is granted by 46 * implication, estoppel or otherwise under any patent or other rights of the 47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49 * or the SD Card Association to disclose or distribute any technical 50 * information, know-how or other confidential information to any third party. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/bio.h> 59 #include <sys/bus.h> 60 #include <sys/conf.h> 61 #include <sys/kernel.h> 62 #include <sys/kthread.h> 63 #include <sys/lock.h> 64 #include <sys/malloc.h> 65 #include <sys/module.h> 66 #include <sys/mutex.h> 67 #include <geom/geom_disk.h> 68 69 #include <dev/mmc/mmcbrvar.h> 70 #include <dev/mmc/mmcreg.h> 71 #include <dev/mmc/mmcvar.h> 72 73 #include "mmcbus_if.h" 74 75 #if __FreeBSD_version < 800002 76 #define kproc_create kthread_create 77 #define kproc_exit kthread_exit 78 #endif 79 80 struct mmcsd_softc { 81 device_t dev; 82 struct mtx sc_mtx; 83 struct disk *disk; 84 struct proc *p; 85 struct bio_queue_head bio_queue; 86 daddr_t eblock, eend; /* Range remaining after the last erase. */ 87 int running; 88 int suspend; 89 }; 90 91 static const char *errmsg[] = 92 { 93 "None", 94 "Timeout", 95 "Bad CRC", 96 "Fifo", 97 "Failed", 98 "Invalid", 99 "NO MEMORY" 100 }; 101 102 /* bus entry points */ 103 static int mmcsd_attach(device_t dev); 104 static int mmcsd_detach(device_t dev); 105 static int mmcsd_probe(device_t dev); 106 107 /* disk routines */ 108 static int mmcsd_close(struct disk *dp); 109 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 110 off_t offset, size_t length); 111 static int mmcsd_open(struct disk *dp); 112 static void mmcsd_strategy(struct bio *bp); 113 static void mmcsd_task(void *arg); 114 115 static int mmcsd_bus_bit_width(device_t dev); 116 static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp); 117 static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp); 118 119 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 120 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 121 #define MMCSD_LOCK_INIT(_sc) \ 122 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 123 "mmcsd", MTX_DEF) 124 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 125 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 126 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 127 128 static int 129 mmcsd_probe(device_t dev) 130 { 131 132 device_quiet(dev); 133 device_set_desc(dev, "MMC/SD Memory Card"); 134 return (0); 135 } 136 137 static int 138 mmcsd_attach(device_t dev) 139 { 140 struct mmcsd_softc *sc; 141 struct disk *d; 142 intmax_t mb; 143 uint32_t speed; 144 uint32_t maxblocks; 145 char unit; 146 147 sc = device_get_softc(dev); 148 sc->dev = dev; 149 MMCSD_LOCK_INIT(sc); 150 151 d = sc->disk = disk_alloc(); 152 d->d_open = mmcsd_open; 153 d->d_close = mmcsd_close; 154 d->d_strategy = mmcsd_strategy; 155 d->d_dump = mmcsd_dump; 156 d->d_name = "mmcsd"; 157 d->d_drv1 = sc; 158 d->d_sectorsize = mmc_get_sector_size(dev); 159 d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize; 160 d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize; 161 d->d_stripeoffset = 0; 162 d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize; 163 d->d_unit = device_get_unit(dev); 164 d->d_flags = DISKFLAG_CANDELETE; 165 d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize * 1; /* conservative */ 166 /* 167 * The d_fw* values are fake. However, layout is aided by making the 168 * number of fwsectors equal to the erase sectors from the drive since 169 * we set the stripe size equal to that. We set fwheads such that there 170 * are ~20 cylinder groups since all values are somewhat arbitrary here 171 * and this gives good behavior with ffs without wasting too much 172 * space. Sadly, geom_part wants to round partitions to these 173 * values. While not bad, in and of itself, the values we present here 174 * will almost certainly be different then the values that USB SD 175 * adapters use and there's too much variation between brands to just 176 * use those values here. Also SD to ATA adapters favor traditional 177 * ata sizes, which are different again from the USB adapters (which 178 * favor SCSI values). This rounding leads to a loss of up to 5% of the 179 * usable space (usually much less, but that's why 20 was selected: to 180 * limit this effect at a few percent). gpart needs a way to override 181 * this behavior for situations like this, but doesn't provide 182 * one. Perhaps this behavior should be tunable as well, but maybe that 183 * belongs in the disk layer. These values will be much better than 184 * the default ones. 185 */ 186 d->d_fwsectors = mmc_get_erase_sector(dev); 187 d->d_fwheads = mmc_get_media_size(dev) / (d->d_fwsectors * 20); 188 strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident)); 189 strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr)); 190 191 /* 192 * Display in most natural units. There's no cards < 1MB. The SD 193 * standard goes to 2GiB due to its reliance on FAT, but the data 194 * format supports up to 4GiB and some card makers push it up to this 195 * limit. The SDHC standard only goes to 32GiB due to FAT32, but the 196 * data format supports up to 2TiB however. 2048GB isn't too ugly, so 197 * we note it in passing here and don't add the code to print 198 * TB). Since these cards are sold in terms of MB and GB not MiB and 199 * GiB, report them like that. We also round to the nearest unit, since 200 * many cards are a few percent short, even of the power of 10 size. 201 */ 202 mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000; 203 unit = 'M'; 204 if (mb >= 1000) { 205 unit = 'G'; 206 mb = (mb + 1000 / 2 - 1) / 1000; 207 } 208 /* 209 * Report the clock speed of the underlying hardware, which might be 210 * different than what the card reports due to hardware limitations. 211 * Report how many blocks the hardware transfers at once. 212 */ 213 speed = mmcbr_get_clock(device_get_parent(dev)); 214 maxblocks = mmc_get_max_data(dev); 215 device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 216 mb, unit, d->d_descr, 217 mmc_get_read_only(dev) ? " (read-only)" : "", 218 device_get_nameunit(device_get_parent(dev)), 219 speed / 1000000, (speed / 100000) % 10, 220 mmcsd_bus_bit_width(dev), maxblocks); 221 disk_create(d, DISK_VERSION); 222 bioq_init(&sc->bio_queue); 223 224 sc->running = 1; 225 sc->suspend = 0; 226 sc->eblock = sc->eend = 0; 227 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card", 228 device_get_nameunit(dev)); 229 230 return (0); 231 } 232 233 static int 234 mmcsd_detach(device_t dev) 235 { 236 struct mmcsd_softc *sc = device_get_softc(dev); 237 238 MMCSD_LOCK(sc); 239 sc->suspend = 0; 240 if (sc->running > 0) { 241 /* kill thread */ 242 sc->running = 0; 243 wakeup(sc); 244 /* wait for thread to finish. */ 245 while (sc->running != -1) 246 msleep(sc, &sc->sc_mtx, 0, "detach", 0); 247 } 248 MMCSD_UNLOCK(sc); 249 250 /* Flush the request queue. */ 251 bioq_flush(&sc->bio_queue, NULL, ENXIO); 252 /* kill disk */ 253 disk_destroy(sc->disk); 254 255 MMCSD_LOCK_DESTROY(sc); 256 257 return (0); 258 } 259 260 static int 261 mmcsd_suspend(device_t dev) 262 { 263 struct mmcsd_softc *sc = device_get_softc(dev); 264 265 MMCSD_LOCK(sc); 266 sc->suspend = 1; 267 if (sc->running > 0) { 268 /* kill thread */ 269 sc->running = 0; 270 wakeup(sc); 271 /* wait for thread to finish. */ 272 while (sc->running != -1) 273 msleep(sc, &sc->sc_mtx, 0, "detach", 0); 274 } 275 MMCSD_UNLOCK(sc); 276 return (0); 277 } 278 279 static int 280 mmcsd_resume(device_t dev) 281 { 282 struct mmcsd_softc *sc = device_get_softc(dev); 283 284 MMCSD_LOCK(sc); 285 sc->suspend = 0; 286 if (sc->running <= 0) { 287 sc->running = 1; 288 MMCSD_UNLOCK(sc); 289 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card", 290 device_get_nameunit(dev)); 291 } else 292 MMCSD_UNLOCK(sc); 293 return (0); 294 } 295 296 static int 297 mmcsd_open(struct disk *dp) 298 { 299 300 return (0); 301 } 302 303 static int 304 mmcsd_close(struct disk *dp) 305 { 306 307 return (0); 308 } 309 310 static void 311 mmcsd_strategy(struct bio *bp) 312 { 313 struct mmcsd_softc *sc; 314 315 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 316 MMCSD_LOCK(sc); 317 if (sc->running > 0 || sc->suspend > 0) { 318 bioq_disksort(&sc->bio_queue, bp); 319 MMCSD_UNLOCK(sc); 320 wakeup(sc); 321 } else { 322 MMCSD_UNLOCK(sc); 323 biofinish(bp, NULL, ENXIO); 324 } 325 } 326 327 static const char * 328 mmcsd_errmsg(int e) 329 { 330 if (e < 0 || e > MMC_ERR_MAX) 331 return "Bad error code"; 332 return errmsg[e]; 333 } 334 335 static daddr_t 336 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 337 { 338 daddr_t block, end; 339 struct mmc_command cmd; 340 struct mmc_command stop; 341 struct mmc_request req; 342 struct mmc_data data; 343 device_t dev = sc->dev; 344 int sz = sc->disk->d_sectorsize; 345 device_t mmcbr = device_get_parent(dev); 346 347 block = bp->bio_pblkno; 348 end = bp->bio_pblkno + (bp->bio_bcount / sz); 349 while (block < end) { 350 char *vaddr = bp->bio_data + 351 (block - bp->bio_pblkno) * sz; 352 int numblocks = min(end - block, mmc_get_max_data(dev)); 353 memset(&req, 0, sizeof(req)); 354 memset(&cmd, 0, sizeof(cmd)); 355 memset(&stop, 0, sizeof(stop)); 356 memset(&data, 0, sizeof(data)); 357 cmd.mrq = &req; 358 req.cmd = &cmd; 359 cmd.data = &data; 360 if (bp->bio_cmd == BIO_READ) { 361 if (numblocks > 1) 362 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 363 else 364 cmd.opcode = MMC_READ_SINGLE_BLOCK; 365 } else { 366 if (numblocks > 1) 367 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 368 else 369 cmd.opcode = MMC_WRITE_BLOCK; 370 } 371 cmd.arg = block; 372 if (!mmc_get_high_cap(dev)) 373 cmd.arg <<= 9; 374 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 375 data.data = vaddr; 376 data.mrq = &req; 377 if (bp->bio_cmd == BIO_READ) 378 data.flags = MMC_DATA_READ; 379 else 380 data.flags = MMC_DATA_WRITE; 381 data.len = numblocks * sz; 382 if (numblocks > 1) { 383 data.flags |= MMC_DATA_MULTI; 384 stop.opcode = MMC_STOP_TRANSMISSION; 385 stop.arg = 0; 386 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 387 stop.mrq = &req; 388 req.stop = &stop; 389 } 390 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 391 if (req.cmd->error != MMC_ERR_NONE) { 392 device_printf(dev, "Error indicated: %d %s\n", 393 req.cmd->error, mmcsd_errmsg(req.cmd->error)); 394 break; 395 } 396 block += numblocks; 397 } 398 return (block); 399 } 400 401 static daddr_t 402 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 403 { 404 daddr_t block, end, start, stop; 405 struct mmc_command cmd; 406 struct mmc_request req; 407 device_t dev = sc->dev; 408 int sz = sc->disk->d_sectorsize; 409 int erase_sector; 410 device_t mmcbr = device_get_parent(dev); 411 412 block = bp->bio_pblkno; 413 end = bp->bio_pblkno + (bp->bio_bcount / sz); 414 /* Coalesce with part remaining from previous request. */ 415 if (block > sc->eblock && block <= sc->eend) 416 block = sc->eblock; 417 if (end >= sc->eblock && end < sc->eend) 418 end = sc->eend; 419 /* Safe round to the erase sector boundaries. */ 420 erase_sector = mmc_get_erase_sector(dev); 421 start = block + erase_sector - 1; /* Round up. */ 422 start -= start % erase_sector; 423 stop = end; /* Round down. */ 424 stop -= end % erase_sector; 425 /* We can't erase area smaller then sector, store it for later. */ 426 if (start >= stop) { 427 sc->eblock = block; 428 sc->eend = end; 429 return (end); 430 } 431 432 /* Set erase start position. */ 433 memset(&req, 0, sizeof(req)); 434 memset(&cmd, 0, sizeof(cmd)); 435 cmd.mrq = &req; 436 req.cmd = &cmd; 437 if (mmc_get_card_type(dev) == mode_sd) 438 cmd.opcode = SD_ERASE_WR_BLK_START; 439 else 440 cmd.opcode = MMC_ERASE_GROUP_START; 441 cmd.arg = start; 442 if (!mmc_get_high_cap(dev)) 443 cmd.arg <<= 9; 444 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 445 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 446 if (req.cmd->error != MMC_ERR_NONE) { 447 printf("erase err1: %d\n", req.cmd->error); 448 return (block); 449 } 450 /* Set erase stop position. */ 451 memset(&req, 0, sizeof(req)); 452 memset(&cmd, 0, sizeof(cmd)); 453 req.cmd = &cmd; 454 if (mmc_get_card_type(dev) == mode_sd) 455 cmd.opcode = SD_ERASE_WR_BLK_END; 456 else 457 cmd.opcode = MMC_ERASE_GROUP_END; 458 cmd.arg = stop; 459 if (!mmc_get_high_cap(dev)) 460 cmd.arg <<= 9; 461 cmd.arg--; 462 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 463 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 464 if (req.cmd->error != MMC_ERR_NONE) { 465 printf("erase err2: %d\n", req.cmd->error); 466 return (block); 467 } 468 /* Erase range. */ 469 memset(&req, 0, sizeof(req)); 470 memset(&cmd, 0, sizeof(cmd)); 471 req.cmd = &cmd; 472 cmd.opcode = MMC_ERASE; 473 cmd.arg = 0; 474 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 475 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 476 if (req.cmd->error != MMC_ERR_NONE) { 477 printf("erase err3 %d\n", req.cmd->error); 478 return (block); 479 } 480 /* Store one of remaining parts for the next call. */ 481 if (bp->bio_pblkno >= sc->eblock || block == start) { 482 sc->eblock = stop; /* Predict next forward. */ 483 sc->eend = end; 484 } else { 485 sc->eblock = block; /* Predict next backward. */ 486 sc->eend = start; 487 } 488 return (end); 489 } 490 491 static int 492 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 493 off_t offset, size_t length) 494 { 495 struct disk *disk = arg; 496 struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1; 497 device_t dev = sc->dev; 498 struct bio bp; 499 daddr_t block, end; 500 device_t mmcbr = device_get_parent(dev); 501 502 /* length zero is special and really means flush buffers to media */ 503 if (!length) 504 return (0); 505 506 bzero(&bp, sizeof(struct bio)); 507 bp.bio_disk = disk; 508 bp.bio_pblkno = offset / disk->d_sectorsize; 509 bp.bio_bcount = length; 510 bp.bio_data = virtual; 511 bp.bio_cmd = BIO_WRITE; 512 end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize; 513 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 514 block = mmcsd_rw(sc, &bp); 515 MMCBUS_RELEASE_BUS(mmcbr, dev); 516 return ((end < block) ? EIO : 0); 517 } 518 519 static void 520 mmcsd_task(void *arg) 521 { 522 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 523 struct bio *bp; 524 int sz; 525 daddr_t block, end; 526 device_t dev = sc->dev; 527 device_t mmcbr = device_get_parent(sc->dev); 528 529 while (1) { 530 MMCSD_LOCK(sc); 531 do { 532 if (sc->running == 0) 533 goto out; 534 bp = bioq_takefirst(&sc->bio_queue); 535 if (bp == NULL) 536 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 537 } while (bp == NULL); 538 MMCSD_UNLOCK(sc); 539 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 540 bp->bio_error = EROFS; 541 bp->bio_resid = bp->bio_bcount; 542 bp->bio_flags |= BIO_ERROR; 543 biodone(bp); 544 continue; 545 } 546 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 547 sz = sc->disk->d_sectorsize; 548 block = bp->bio_pblkno; 549 end = bp->bio_pblkno + (bp->bio_bcount / sz); 550 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 551 /* Access to the remaining erase block obsoletes it. */ 552 if (block < sc->eend && end > sc->eblock) 553 sc->eblock = sc->eend = 0; 554 block = mmcsd_rw(sc, bp); 555 } else if (bp->bio_cmd == BIO_DELETE) { 556 block = mmcsd_delete(sc, bp); 557 } 558 MMCBUS_RELEASE_BUS(mmcbr, dev); 559 if (block < end) { 560 bp->bio_error = EIO; 561 bp->bio_resid = (end - block) * sz; 562 bp->bio_flags |= BIO_ERROR; 563 } 564 biodone(bp); 565 } 566 out: 567 /* tell parent we're done */ 568 sc->running = -1; 569 MMCSD_UNLOCK(sc); 570 wakeup(sc); 571 572 kproc_exit(0); 573 } 574 575 static int 576 mmcsd_bus_bit_width(device_t dev) 577 { 578 579 if (mmc_get_bus_width(dev) == bus_width_1) 580 return (1); 581 if (mmc_get_bus_width(dev) == bus_width_4) 582 return (4); 583 return (8); 584 } 585 586 static device_method_t mmcsd_methods[] = { 587 DEVMETHOD(device_probe, mmcsd_probe), 588 DEVMETHOD(device_attach, mmcsd_attach), 589 DEVMETHOD(device_detach, mmcsd_detach), 590 DEVMETHOD(device_suspend, mmcsd_suspend), 591 DEVMETHOD(device_resume, mmcsd_resume), 592 DEVMETHOD_END 593 }; 594 595 static driver_t mmcsd_driver = { 596 "mmcsd", 597 mmcsd_methods, 598 sizeof(struct mmcsd_softc), 599 }; 600 static devclass_t mmcsd_devclass; 601 602 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL); 603