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 <sys/time.h> 68 #include <geom/geom.h> 69 #include <geom/geom_disk.h> 70 71 #include <dev/mmc/mmcbrvar.h> 72 #include <dev/mmc/mmcreg.h> 73 #include <dev/mmc/mmcvar.h> 74 75 #include "mmcbus_if.h" 76 77 #if __FreeBSD_version < 800002 78 #define kproc_create kthread_create 79 #define kproc_exit kthread_exit 80 #endif 81 82 struct mmcsd_softc { 83 device_t dev; 84 struct mtx sc_mtx; 85 struct disk *disk; 86 struct proc *p; 87 struct bio_queue_head bio_queue; 88 daddr_t eblock, eend; /* Range remaining after the last erase. */ 89 int running; 90 int suspend; 91 int log_count; 92 struct timeval log_time; 93 }; 94 95 static const char *errmsg[] = 96 { 97 "None", 98 "Timeout", 99 "Bad CRC", 100 "Fifo", 101 "Failed", 102 "Invalid", 103 "NO MEMORY" 104 }; 105 106 #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 107 108 /* bus entry points */ 109 static int mmcsd_attach(device_t dev); 110 static int mmcsd_detach(device_t dev); 111 static int mmcsd_probe(device_t dev); 112 113 /* disk routines */ 114 static int mmcsd_close(struct disk *dp); 115 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 116 off_t offset, size_t length); 117 static int mmcsd_open(struct disk *dp); 118 static void mmcsd_strategy(struct bio *bp); 119 static void mmcsd_task(void *arg); 120 121 static int mmcsd_bus_bit_width(device_t dev); 122 static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp); 123 static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp); 124 125 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 126 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 127 #define MMCSD_LOCK_INIT(_sc) \ 128 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 129 "mmcsd", MTX_DEF) 130 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 131 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 132 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 133 134 static int 135 mmcsd_probe(device_t dev) 136 { 137 138 device_quiet(dev); 139 device_set_desc(dev, "MMC/SD Memory Card"); 140 return (0); 141 } 142 143 static int 144 mmcsd_attach(device_t dev) 145 { 146 struct mmcsd_softc *sc; 147 struct disk *d; 148 intmax_t mb; 149 uint32_t speed; 150 uint32_t maxblocks; 151 char unit; 152 153 sc = device_get_softc(dev); 154 sc->dev = dev; 155 MMCSD_LOCK_INIT(sc); 156 157 d = sc->disk = disk_alloc(); 158 d->d_open = mmcsd_open; 159 d->d_close = mmcsd_close; 160 d->d_strategy = mmcsd_strategy; 161 d->d_dump = mmcsd_dump; 162 d->d_name = "mmcsd"; 163 d->d_drv1 = sc; 164 d->d_sectorsize = mmc_get_sector_size(dev); 165 d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize; 166 d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize; 167 d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize; 168 d->d_unit = device_get_unit(dev); 169 d->d_flags = DISKFLAG_CANDELETE; 170 d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize; 171 strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident)); 172 strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr)); 173 d->d_rotation_rate = DISK_RR_NON_ROTATING; 174 175 /* 176 * Display in most natural units. There's no cards < 1MB. The SD 177 * standard goes to 2GiB due to its reliance on FAT, but the data 178 * format supports up to 4GiB and some card makers push it up to this 179 * limit. The SDHC standard only goes to 32GiB due to FAT32, but the 180 * data format supports up to 2TiB however. 2048GB isn't too ugly, so 181 * we note it in passing here and don't add the code to print 182 * TB). Since these cards are sold in terms of MB and GB not MiB and 183 * GiB, report them like that. We also round to the nearest unit, since 184 * many cards are a few percent short, even of the power of 10 size. 185 */ 186 mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000; 187 unit = 'M'; 188 if (mb >= 1000) { 189 unit = 'G'; 190 mb = (mb + 1000 / 2 - 1) / 1000; 191 } 192 /* 193 * Report the clock speed of the underlying hardware, which might be 194 * different than what the card reports due to hardware limitations. 195 * Report how many blocks the hardware transfers at once. 196 */ 197 speed = mmcbr_get_clock(device_get_parent(dev)); 198 maxblocks = mmc_get_max_data(dev); 199 device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 200 mb, unit, d->d_descr, 201 mmc_get_read_only(dev) ? " (read-only)" : "", 202 device_get_nameunit(device_get_parent(dev)), 203 speed / 1000000, (speed / 100000) % 10, 204 mmcsd_bus_bit_width(dev), maxblocks); 205 disk_create(d, DISK_VERSION); 206 bioq_init(&sc->bio_queue); 207 208 sc->running = 1; 209 sc->suspend = 0; 210 sc->eblock = sc->eend = 0; 211 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card", 212 device_get_nameunit(dev)); 213 214 return (0); 215 } 216 217 static int 218 mmcsd_detach(device_t dev) 219 { 220 struct mmcsd_softc *sc = device_get_softc(dev); 221 222 MMCSD_LOCK(sc); 223 sc->suspend = 0; 224 if (sc->running > 0) { 225 /* kill thread */ 226 sc->running = 0; 227 wakeup(sc); 228 /* wait for thread to finish. */ 229 while (sc->running != -1) 230 msleep(sc, &sc->sc_mtx, 0, "detach", 0); 231 } 232 MMCSD_UNLOCK(sc); 233 234 /* Flush the request queue. */ 235 bioq_flush(&sc->bio_queue, NULL, ENXIO); 236 /* kill disk */ 237 disk_destroy(sc->disk); 238 239 MMCSD_LOCK_DESTROY(sc); 240 241 return (0); 242 } 243 244 static int 245 mmcsd_suspend(device_t dev) 246 { 247 struct mmcsd_softc *sc = device_get_softc(dev); 248 249 MMCSD_LOCK(sc); 250 sc->suspend = 1; 251 if (sc->running > 0) { 252 /* kill thread */ 253 sc->running = 0; 254 wakeup(sc); 255 /* wait for thread to finish. */ 256 while (sc->running != -1) 257 msleep(sc, &sc->sc_mtx, 0, "detach", 0); 258 } 259 MMCSD_UNLOCK(sc); 260 return (0); 261 } 262 263 static int 264 mmcsd_resume(device_t dev) 265 { 266 struct mmcsd_softc *sc = device_get_softc(dev); 267 268 MMCSD_LOCK(sc); 269 sc->suspend = 0; 270 if (sc->running <= 0) { 271 sc->running = 1; 272 MMCSD_UNLOCK(sc); 273 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card", 274 device_get_nameunit(dev)); 275 } else 276 MMCSD_UNLOCK(sc); 277 return (0); 278 } 279 280 static int 281 mmcsd_open(struct disk *dp) 282 { 283 284 return (0); 285 } 286 287 static int 288 mmcsd_close(struct disk *dp) 289 { 290 291 return (0); 292 } 293 294 static void 295 mmcsd_strategy(struct bio *bp) 296 { 297 struct mmcsd_softc *sc; 298 299 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 300 MMCSD_LOCK(sc); 301 if (sc->running > 0 || sc->suspend > 0) { 302 bioq_disksort(&sc->bio_queue, bp); 303 MMCSD_UNLOCK(sc); 304 wakeup(sc); 305 } else { 306 MMCSD_UNLOCK(sc); 307 biofinish(bp, NULL, ENXIO); 308 } 309 } 310 311 static const char * 312 mmcsd_errmsg(int e) 313 { 314 315 if (e < 0 || e > MMC_ERR_MAX) 316 return "Bad error code"; 317 return errmsg[e]; 318 } 319 320 static daddr_t 321 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 322 { 323 daddr_t block, end; 324 struct mmc_command cmd; 325 struct mmc_command stop; 326 struct mmc_request req; 327 struct mmc_data data; 328 device_t dev = sc->dev; 329 int numblocks, sz; 330 device_t mmcbr = device_get_parent(dev); 331 char *vaddr; 332 333 block = bp->bio_pblkno; 334 sz = sc->disk->d_sectorsize; 335 end = bp->bio_pblkno + (bp->bio_bcount / sz); 336 while (block < end) { 337 vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 338 numblocks = min(end - block, mmc_get_max_data(dev)); 339 memset(&req, 0, sizeof(req)); 340 memset(&cmd, 0, sizeof(cmd)); 341 memset(&stop, 0, sizeof(stop)); 342 memset(&data, 0, sizeof(data)); 343 cmd.mrq = &req; 344 req.cmd = &cmd; 345 cmd.data = &data; 346 if (bp->bio_cmd == BIO_READ) { 347 if (numblocks > 1) 348 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 349 else 350 cmd.opcode = MMC_READ_SINGLE_BLOCK; 351 } else { 352 if (numblocks > 1) 353 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 354 else 355 cmd.opcode = MMC_WRITE_BLOCK; 356 } 357 cmd.arg = block; 358 if (!mmc_get_high_cap(dev)) 359 cmd.arg <<= 9; 360 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 361 data.data = vaddr; 362 data.mrq = &req; 363 if (bp->bio_cmd == BIO_READ) 364 data.flags = MMC_DATA_READ; 365 else 366 data.flags = MMC_DATA_WRITE; 367 data.len = numblocks * sz; 368 if (numblocks > 1) { 369 data.flags |= MMC_DATA_MULTI; 370 stop.opcode = MMC_STOP_TRANSMISSION; 371 stop.arg = 0; 372 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 373 stop.mrq = &req; 374 req.stop = &stop; 375 } 376 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 377 if (req.cmd->error != MMC_ERR_NONE) { 378 if (ppsratecheck(&sc->log_time, &sc->log_count, 379 LOG_PPS)) 380 device_printf(dev, "Error indicated: %d %s\n", 381 req.cmd->error, 382 mmcsd_errmsg(req.cmd->error)); 383 break; 384 } 385 block += numblocks; 386 } 387 return (block); 388 } 389 390 static daddr_t 391 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 392 { 393 daddr_t block, end, start, stop; 394 struct mmc_command cmd; 395 struct mmc_request req; 396 device_t dev = sc->dev; 397 int sz = sc->disk->d_sectorsize; 398 int erase_sector; 399 device_t mmcbr = device_get_parent(dev); 400 401 block = bp->bio_pblkno; 402 end = bp->bio_pblkno + (bp->bio_bcount / sz); 403 /* Coalesce with part remaining from previous request. */ 404 if (block > sc->eblock && block <= sc->eend) 405 block = sc->eblock; 406 if (end >= sc->eblock && end < sc->eend) 407 end = sc->eend; 408 /* Safe round to the erase sector boundaries. */ 409 erase_sector = mmc_get_erase_sector(dev); 410 start = block + erase_sector - 1; /* Round up. */ 411 start -= start % erase_sector; 412 stop = end; /* Round down. */ 413 stop -= end % erase_sector; 414 /* We can't erase area smaller then sector, store it for later. */ 415 if (start >= stop) { 416 sc->eblock = block; 417 sc->eend = end; 418 return (end); 419 } 420 421 /* Set erase start position. */ 422 memset(&req, 0, sizeof(req)); 423 memset(&cmd, 0, sizeof(cmd)); 424 cmd.mrq = &req; 425 req.cmd = &cmd; 426 if (mmc_get_card_type(dev) == mode_sd) 427 cmd.opcode = SD_ERASE_WR_BLK_START; 428 else 429 cmd.opcode = MMC_ERASE_GROUP_START; 430 cmd.arg = start; 431 if (!mmc_get_high_cap(dev)) 432 cmd.arg <<= 9; 433 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 434 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 435 if (req.cmd->error != MMC_ERR_NONE) { 436 printf("erase err1: %d\n", req.cmd->error); 437 return (block); 438 } 439 /* Set erase stop position. */ 440 memset(&req, 0, sizeof(req)); 441 memset(&cmd, 0, sizeof(cmd)); 442 req.cmd = &cmd; 443 if (mmc_get_card_type(dev) == mode_sd) 444 cmd.opcode = SD_ERASE_WR_BLK_END; 445 else 446 cmd.opcode = MMC_ERASE_GROUP_END; 447 cmd.arg = stop; 448 if (!mmc_get_high_cap(dev)) 449 cmd.arg <<= 9; 450 cmd.arg--; 451 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 452 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 453 if (req.cmd->error != MMC_ERR_NONE) { 454 printf("erase err2: %d\n", req.cmd->error); 455 return (block); 456 } 457 /* Erase range. */ 458 memset(&req, 0, sizeof(req)); 459 memset(&cmd, 0, sizeof(cmd)); 460 req.cmd = &cmd; 461 cmd.opcode = MMC_ERASE; 462 cmd.arg = 0; 463 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 464 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 465 if (req.cmd->error != MMC_ERR_NONE) { 466 printf("erase err3 %d\n", req.cmd->error); 467 return (block); 468 } 469 /* Store one of remaining parts for the next call. */ 470 if (bp->bio_pblkno >= sc->eblock || block == start) { 471 sc->eblock = stop; /* Predict next forward. */ 472 sc->eend = end; 473 } else { 474 sc->eblock = block; /* Predict next backward. */ 475 sc->eend = start; 476 } 477 return (end); 478 } 479 480 static int 481 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 482 size_t length) 483 { 484 struct disk *disk = arg; 485 struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1; 486 device_t dev = sc->dev; 487 struct bio bp; 488 daddr_t block, end; 489 device_t mmcbr = device_get_parent(dev); 490 491 /* length zero is special and really means flush buffers to media */ 492 if (!length) 493 return (0); 494 495 g_reset_bio(&bp); 496 bp.bio_disk = disk; 497 bp.bio_pblkno = offset / disk->d_sectorsize; 498 bp.bio_bcount = length; 499 bp.bio_data = virtual; 500 bp.bio_cmd = BIO_WRITE; 501 end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize; 502 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 503 block = mmcsd_rw(sc, &bp); 504 MMCBUS_RELEASE_BUS(mmcbr, dev); 505 return ((end < block) ? EIO : 0); 506 } 507 508 static void 509 mmcsd_task(void *arg) 510 { 511 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 512 struct bio *bp; 513 int sz; 514 daddr_t block, end; 515 device_t dev = sc->dev; 516 device_t mmcbr = device_get_parent(sc->dev); 517 518 while (1) { 519 MMCSD_LOCK(sc); 520 do { 521 if (sc->running == 0) 522 goto out; 523 bp = bioq_takefirst(&sc->bio_queue); 524 if (bp == NULL) 525 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 526 } while (bp == NULL); 527 MMCSD_UNLOCK(sc); 528 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 529 bp->bio_error = EROFS; 530 bp->bio_resid = bp->bio_bcount; 531 bp->bio_flags |= BIO_ERROR; 532 biodone(bp); 533 continue; 534 } 535 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 536 sz = sc->disk->d_sectorsize; 537 block = bp->bio_pblkno; 538 end = bp->bio_pblkno + (bp->bio_bcount / sz); 539 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 540 /* Access to the remaining erase block obsoletes it. */ 541 if (block < sc->eend && end > sc->eblock) 542 sc->eblock = sc->eend = 0; 543 block = mmcsd_rw(sc, bp); 544 } else if (bp->bio_cmd == BIO_DELETE) { 545 block = mmcsd_delete(sc, bp); 546 } 547 MMCBUS_RELEASE_BUS(mmcbr, dev); 548 if (block < end) { 549 bp->bio_error = EIO; 550 bp->bio_resid = (end - block) * sz; 551 bp->bio_flags |= BIO_ERROR; 552 } else { 553 bp->bio_resid = 0; 554 } 555 biodone(bp); 556 } 557 out: 558 /* tell parent we're done */ 559 sc->running = -1; 560 MMCSD_UNLOCK(sc); 561 wakeup(sc); 562 563 kproc_exit(0); 564 } 565 566 static int 567 mmcsd_bus_bit_width(device_t dev) 568 { 569 570 if (mmc_get_bus_width(dev) == bus_width_1) 571 return (1); 572 if (mmc_get_bus_width(dev) == bus_width_4) 573 return (4); 574 return (8); 575 } 576 577 static device_method_t mmcsd_methods[] = { 578 DEVMETHOD(device_probe, mmcsd_probe), 579 DEVMETHOD(device_attach, mmcsd_attach), 580 DEVMETHOD(device_detach, mmcsd_detach), 581 DEVMETHOD(device_suspend, mmcsd_suspend), 582 DEVMETHOD(device_resume, mmcsd_resume), 583 DEVMETHOD_END 584 }; 585 586 static driver_t mmcsd_driver = { 587 "mmcsd", 588 mmcsd_methods, 589 sizeof(struct mmcsd_softc), 590 }; 591 static devclass_t mmcsd_devclass; 592 593 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL); 594