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 if (e < 0 || e > MMC_ERR_MAX) 315 return "Bad error code"; 316 return errmsg[e]; 317 } 318 319 static daddr_t 320 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 321 { 322 daddr_t block, end; 323 struct mmc_command cmd; 324 struct mmc_command stop; 325 struct mmc_request req; 326 struct mmc_data data; 327 device_t dev = sc->dev; 328 int sz = sc->disk->d_sectorsize; 329 device_t mmcbr = device_get_parent(dev); 330 331 block = bp->bio_pblkno; 332 end = bp->bio_pblkno + (bp->bio_bcount / sz); 333 while (block < end) { 334 char *vaddr = bp->bio_data + 335 (block - bp->bio_pblkno) * sz; 336 int numblocks = min(end - block, mmc_get_max_data(dev)); 337 memset(&req, 0, sizeof(req)); 338 memset(&cmd, 0, sizeof(cmd)); 339 memset(&stop, 0, sizeof(stop)); 340 memset(&data, 0, sizeof(data)); 341 cmd.mrq = &req; 342 req.cmd = &cmd; 343 cmd.data = &data; 344 if (bp->bio_cmd == BIO_READ) { 345 if (numblocks > 1) 346 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 347 else 348 cmd.opcode = MMC_READ_SINGLE_BLOCK; 349 } else { 350 if (numblocks > 1) 351 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 352 else 353 cmd.opcode = MMC_WRITE_BLOCK; 354 } 355 cmd.arg = block; 356 if (!mmc_get_high_cap(dev)) 357 cmd.arg <<= 9; 358 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 359 data.data = vaddr; 360 data.mrq = &req; 361 if (bp->bio_cmd == BIO_READ) 362 data.flags = MMC_DATA_READ; 363 else 364 data.flags = MMC_DATA_WRITE; 365 data.len = numblocks * sz; 366 if (numblocks > 1) { 367 data.flags |= MMC_DATA_MULTI; 368 stop.opcode = MMC_STOP_TRANSMISSION; 369 stop.arg = 0; 370 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 371 stop.mrq = &req; 372 req.stop = &stop; 373 } 374 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 375 if (req.cmd->error != MMC_ERR_NONE) { 376 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) { 377 device_printf(dev, "Error indicated: %d %s\n", 378 req.cmd->error, mmcsd_errmsg(req.cmd->error)); 379 } 380 break; 381 } 382 block += numblocks; 383 } 384 return (block); 385 } 386 387 static daddr_t 388 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 389 { 390 daddr_t block, end, start, stop; 391 struct mmc_command cmd; 392 struct mmc_request req; 393 device_t dev = sc->dev; 394 int sz = sc->disk->d_sectorsize; 395 int erase_sector; 396 device_t mmcbr = device_get_parent(dev); 397 398 block = bp->bio_pblkno; 399 end = bp->bio_pblkno + (bp->bio_bcount / sz); 400 /* Coalesce with part remaining from previous request. */ 401 if (block > sc->eblock && block <= sc->eend) 402 block = sc->eblock; 403 if (end >= sc->eblock && end < sc->eend) 404 end = sc->eend; 405 /* Safe round to the erase sector boundaries. */ 406 erase_sector = mmc_get_erase_sector(dev); 407 start = block + erase_sector - 1; /* Round up. */ 408 start -= start % erase_sector; 409 stop = end; /* Round down. */ 410 stop -= end % erase_sector; 411 /* We can't erase area smaller then sector, store it for later. */ 412 if (start >= stop) { 413 sc->eblock = block; 414 sc->eend = end; 415 return (end); 416 } 417 418 /* Set erase start position. */ 419 memset(&req, 0, sizeof(req)); 420 memset(&cmd, 0, sizeof(cmd)); 421 cmd.mrq = &req; 422 req.cmd = &cmd; 423 if (mmc_get_card_type(dev) == mode_sd) 424 cmd.opcode = SD_ERASE_WR_BLK_START; 425 else 426 cmd.opcode = MMC_ERASE_GROUP_START; 427 cmd.arg = start; 428 if (!mmc_get_high_cap(dev)) 429 cmd.arg <<= 9; 430 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 431 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 432 if (req.cmd->error != MMC_ERR_NONE) { 433 printf("erase err1: %d\n", req.cmd->error); 434 return (block); 435 } 436 /* Set erase stop position. */ 437 memset(&req, 0, sizeof(req)); 438 memset(&cmd, 0, sizeof(cmd)); 439 req.cmd = &cmd; 440 if (mmc_get_card_type(dev) == mode_sd) 441 cmd.opcode = SD_ERASE_WR_BLK_END; 442 else 443 cmd.opcode = MMC_ERASE_GROUP_END; 444 cmd.arg = stop; 445 if (!mmc_get_high_cap(dev)) 446 cmd.arg <<= 9; 447 cmd.arg--; 448 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 449 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 450 if (req.cmd->error != MMC_ERR_NONE) { 451 printf("erase err2: %d\n", req.cmd->error); 452 return (block); 453 } 454 /* Erase range. */ 455 memset(&req, 0, sizeof(req)); 456 memset(&cmd, 0, sizeof(cmd)); 457 req.cmd = &cmd; 458 cmd.opcode = MMC_ERASE; 459 cmd.arg = 0; 460 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 461 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req); 462 if (req.cmd->error != MMC_ERR_NONE) { 463 printf("erase err3 %d\n", req.cmd->error); 464 return (block); 465 } 466 /* Store one of remaining parts for the next call. */ 467 if (bp->bio_pblkno >= sc->eblock || block == start) { 468 sc->eblock = stop; /* Predict next forward. */ 469 sc->eend = end; 470 } else { 471 sc->eblock = block; /* Predict next backward. */ 472 sc->eend = start; 473 } 474 return (end); 475 } 476 477 static int 478 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 479 off_t offset, size_t length) 480 { 481 struct disk *disk = arg; 482 struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1; 483 device_t dev = sc->dev; 484 struct bio bp; 485 daddr_t block, end; 486 device_t mmcbr = device_get_parent(dev); 487 488 /* length zero is special and really means flush buffers to media */ 489 if (!length) 490 return (0); 491 492 g_reset_bio(&bp); 493 bp.bio_disk = disk; 494 bp.bio_pblkno = offset / disk->d_sectorsize; 495 bp.bio_bcount = length; 496 bp.bio_data = virtual; 497 bp.bio_cmd = BIO_WRITE; 498 end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize; 499 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 500 block = mmcsd_rw(sc, &bp); 501 MMCBUS_RELEASE_BUS(mmcbr, dev); 502 return ((end < block) ? EIO : 0); 503 } 504 505 static void 506 mmcsd_task(void *arg) 507 { 508 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 509 struct bio *bp; 510 int sz; 511 daddr_t block, end; 512 device_t dev = sc->dev; 513 device_t mmcbr = device_get_parent(sc->dev); 514 515 while (1) { 516 MMCSD_LOCK(sc); 517 do { 518 if (sc->running == 0) 519 goto out; 520 bp = bioq_takefirst(&sc->bio_queue); 521 if (bp == NULL) 522 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 523 } while (bp == NULL); 524 MMCSD_UNLOCK(sc); 525 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 526 bp->bio_error = EROFS; 527 bp->bio_resid = bp->bio_bcount; 528 bp->bio_flags |= BIO_ERROR; 529 biodone(bp); 530 continue; 531 } 532 MMCBUS_ACQUIRE_BUS(mmcbr, dev); 533 sz = sc->disk->d_sectorsize; 534 block = bp->bio_pblkno; 535 end = bp->bio_pblkno + (bp->bio_bcount / sz); 536 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 537 /* Access to the remaining erase block obsoletes it. */ 538 if (block < sc->eend && end > sc->eblock) 539 sc->eblock = sc->eend = 0; 540 block = mmcsd_rw(sc, bp); 541 } else if (bp->bio_cmd == BIO_DELETE) { 542 block = mmcsd_delete(sc, bp); 543 } 544 MMCBUS_RELEASE_BUS(mmcbr, dev); 545 if (block < end) { 546 bp->bio_error = EIO; 547 bp->bio_resid = (end - block) * sz; 548 bp->bio_flags |= BIO_ERROR; 549 } else { 550 bp->bio_resid = 0; 551 } 552 biodone(bp); 553 } 554 out: 555 /* tell parent we're done */ 556 sc->running = -1; 557 MMCSD_UNLOCK(sc); 558 wakeup(sc); 559 560 kproc_exit(0); 561 } 562 563 static int 564 mmcsd_bus_bit_width(device_t dev) 565 { 566 567 if (mmc_get_bus_width(dev) == bus_width_1) 568 return (1); 569 if (mmc_get_bus_width(dev) == bus_width_4) 570 return (4); 571 return (8); 572 } 573 574 static device_method_t mmcsd_methods[] = { 575 DEVMETHOD(device_probe, mmcsd_probe), 576 DEVMETHOD(device_attach, mmcsd_attach), 577 DEVMETHOD(device_detach, mmcsd_detach), 578 DEVMETHOD(device_suspend, mmcsd_suspend), 579 DEVMETHOD(device_resume, mmcsd_resume), 580 DEVMETHOD_END 581 }; 582 583 static driver_t mmcsd_driver = { 584 "mmcsd", 585 mmcsd_methods, 586 sizeof(struct mmcsd_softc), 587 }; 588 static devclass_t mmcsd_devclass; 589 590 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL); 591