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