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/mmcvar.h> 70 #include <dev/mmc/mmcreg.h> 71 72 #include "mmcbus_if.h" 73 74 struct mmcsd_softc { 75 device_t dev; 76 struct mtx sc_mtx; 77 struct disk *disk; 78 struct proc *p; 79 struct bio_queue_head bio_queue; 80 daddr_t eblock, eend; /* Range remaining after the last erase. */ 81 int running; 82 }; 83 84 /* bus entry points */ 85 static int mmcsd_probe(device_t dev); 86 static int mmcsd_attach(device_t dev); 87 static int mmcsd_detach(device_t dev); 88 89 /* disk routines */ 90 static int mmcsd_open(struct disk *dp); 91 static int mmcsd_close(struct disk *dp); 92 static void mmcsd_strategy(struct bio *bp); 93 static void mmcsd_task(void *arg); 94 95 static const char *mmcsd_card_name(device_t dev); 96 static int mmcsd_bus_bit_width(device_t dev); 97 98 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 99 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 100 #define MMCSD_LOCK_INIT(_sc) \ 101 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 102 "mmcsd", MTX_DEF) 103 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 104 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 105 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 106 107 static int 108 mmcsd_probe(device_t dev) 109 { 110 111 device_quiet(dev); 112 device_set_desc(dev, "MMC/SD Memory Card"); 113 return (0); 114 } 115 116 static int 117 mmcsd_attach(device_t dev) 118 { 119 struct mmcsd_softc *sc; 120 struct disk *d; 121 intmax_t mb; 122 char unit; 123 124 sc = device_get_softc(dev); 125 sc->dev = dev; 126 MMCSD_LOCK_INIT(sc); 127 128 d = sc->disk = disk_alloc(); 129 d->d_open = mmcsd_open; 130 d->d_close = mmcsd_close; 131 d->d_strategy = mmcsd_strategy; 132 // d->d_dump = mmcsd_dump; Need polling mmc layer 133 d->d_name = "mmcsd"; 134 d->d_drv1 = sc; 135 d->d_maxsize = 4*1024*1024; /* Maximum defined SD card AU size. */ 136 d->d_sectorsize = mmc_get_sector_size(dev); 137 d->d_mediasize = mmc_get_media_size(dev) * d->d_sectorsize; 138 d->d_stripeoffset = 0; 139 d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize; 140 d->d_unit = device_get_unit(dev); 141 d->d_flags = DISKFLAG_CANDELETE; 142 /* 143 * Display in most natural units. There's no cards < 1MB. 144 * The SD standard goes to 2GiB, but the data format supports 145 * up to 4GiB and some card makers push it up to this limit. 146 * The SDHC standard only goes to 32GiB (the data format in 147 * SDHC is good to 2TiB however, which isn't too ugly at 148 * 2048GiBm, so we note it in passing here and don't add the 149 * code to print TiB). 150 */ 151 mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */ 152 unit = 'M'; 153 if (mb >= 10240) { /* 1GiB = 1024 MiB */ 154 unit = 'G'; 155 mb /= 1024; 156 } 157 device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n", 158 mb, unit, mmcsd_card_name(dev), 159 mmc_get_read_only(dev) ? " (read-only)" : "", 160 device_get_nameunit(device_get_parent(dev)), 161 mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev)); 162 disk_create(d, DISK_VERSION); 163 bioq_init(&sc->bio_queue); 164 165 sc->running = 1; 166 sc->eblock = sc->eend = 0; 167 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card"); 168 169 return (0); 170 } 171 172 static int 173 mmcsd_detach(device_t dev) 174 { 175 struct mmcsd_softc *sc = device_get_softc(dev); 176 177 /* kill thread */ 178 MMCSD_LOCK(sc); 179 sc->running = 0; 180 wakeup(sc); 181 MMCSD_UNLOCK(sc); 182 183 /* wait for thread to finish. XXX probably want timeout. -sorbo */ 184 MMCSD_LOCK(sc); 185 while (sc->running != -1) 186 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0); 187 MMCSD_UNLOCK(sc); 188 189 /* Flush the request queue. */ 190 bioq_flush(&sc->bio_queue, NULL, ENXIO); 191 /* kill disk */ 192 disk_destroy(sc->disk); 193 194 MMCSD_LOCK_DESTROY(sc); 195 196 return (0); 197 } 198 199 static int 200 mmcsd_open(struct disk *dp) 201 { 202 return (0); 203 } 204 205 static int 206 mmcsd_close(struct disk *dp) 207 { 208 return (0); 209 } 210 211 static void 212 mmcsd_strategy(struct bio *bp) 213 { 214 struct mmcsd_softc *sc; 215 216 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 217 MMCSD_LOCK(sc); 218 if (sc->running > 0) { 219 bioq_disksort(&sc->bio_queue, bp); 220 wakeup(sc); 221 MMCSD_UNLOCK(sc); 222 } else { 223 MMCSD_UNLOCK(sc); 224 biofinish(bp, NULL, ENXIO); 225 } 226 } 227 228 static daddr_t 229 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 230 { 231 daddr_t block, end; 232 struct mmc_command cmd; 233 struct mmc_command stop; 234 struct mmc_request req; 235 struct mmc_data data; 236 device_t dev = sc->dev; 237 int sz = sc->disk->d_sectorsize; 238 239 block = bp->bio_pblkno; 240 end = bp->bio_pblkno + (bp->bio_bcount / sz); 241 while (block < end) { 242 char *vaddr = bp->bio_data + 243 (block - bp->bio_pblkno) * sz; 244 int numblocks = min(end - block, mmc_get_max_data(dev)); 245 memset(&req, 0, sizeof(req)); 246 memset(&cmd, 0, sizeof(cmd)); 247 memset(&stop, 0, sizeof(stop)); 248 req.cmd = &cmd; 249 cmd.data = &data; 250 if (bp->bio_cmd == BIO_READ) { 251 if (numblocks > 1) 252 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 253 else 254 cmd.opcode = MMC_READ_SINGLE_BLOCK; 255 } else { 256 if (numblocks > 1) 257 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 258 else 259 cmd.opcode = MMC_WRITE_BLOCK; 260 } 261 cmd.arg = block; 262 if (!mmc_get_high_cap(dev)) 263 cmd.arg <<= 9; 264 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 265 data.data = vaddr; 266 data.mrq = &req; 267 if (bp->bio_cmd == BIO_READ) 268 data.flags = MMC_DATA_READ; 269 else 270 data.flags = MMC_DATA_WRITE; 271 data.len = numblocks * sz; 272 if (numblocks > 1) { 273 data.flags |= MMC_DATA_MULTI; 274 stop.opcode = MMC_STOP_TRANSMISSION; 275 stop.arg = 0; 276 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 277 req.stop = &stop; 278 } 279 // printf("Len %d %lld-%lld flags %#x sz %d\n", 280 // (int)data.len, (long long)block, (long long)end, data.flags, sz); 281 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 282 &req); 283 if (req.cmd->error != MMC_ERR_NONE) 284 break; 285 block += numblocks; 286 } 287 return (block); 288 } 289 290 static daddr_t 291 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 292 { 293 daddr_t block, end, start, stop; 294 struct mmc_command cmd; 295 struct mmc_request req; 296 device_t dev = sc->dev; 297 int sz = sc->disk->d_sectorsize; 298 int erase_sector; 299 300 block = bp->bio_pblkno; 301 end = bp->bio_pblkno + (bp->bio_bcount / sz); 302 /* Coalesce with part remaining from previous request. */ 303 if (block > sc->eblock && block <= sc->eend) 304 block = sc->eblock; 305 if (end >= sc->eblock && end < sc->eend) 306 end = sc->eend; 307 /* Safe round to the erase sector boundaries. */ 308 erase_sector = mmc_get_erase_sector(dev); 309 start = block + erase_sector - 1; /* Round up. */ 310 start -= start % erase_sector; 311 stop = end; /* Round down. */ 312 stop -= end % erase_sector; 313 /* We can't erase area smaller then sector, store it for later. */ 314 if (start >= stop) { 315 sc->eblock = block; 316 sc->eend = end; 317 return (end); 318 } 319 320 /* Set erase start position. */ 321 memset(&req, 0, sizeof(req)); 322 memset(&cmd, 0, sizeof(cmd)); 323 req.cmd = &cmd; 324 if (mmc_get_card_type(dev) == mode_sd) 325 cmd.opcode = SD_ERASE_WR_BLK_START; 326 else 327 cmd.opcode = MMC_ERASE_GROUP_START; 328 cmd.arg = start; 329 if (!mmc_get_high_cap(dev)) 330 cmd.arg <<= 9; 331 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 332 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 333 &req); 334 if (req.cmd->error != MMC_ERR_NONE) { 335 printf("erase err1: %d\n", req.cmd->error); 336 return (block); 337 } 338 /* Set erase stop position. */ 339 memset(&req, 0, sizeof(req)); 340 memset(&cmd, 0, sizeof(cmd)); 341 req.cmd = &cmd; 342 if (mmc_get_card_type(dev) == mode_sd) 343 cmd.opcode = SD_ERASE_WR_BLK_END; 344 else 345 cmd.opcode = MMC_ERASE_GROUP_END; 346 cmd.arg = stop; 347 if (!mmc_get_high_cap(dev)) 348 cmd.arg <<= 9; 349 cmd.arg--; 350 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 351 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 352 &req); 353 if (req.cmd->error != MMC_ERR_NONE) { 354 printf("erase err2: %d\n", req.cmd->error); 355 return (block); 356 } 357 /* Erase range. */ 358 memset(&req, 0, sizeof(req)); 359 memset(&cmd, 0, sizeof(cmd)); 360 req.cmd = &cmd; 361 cmd.opcode = MMC_ERASE; 362 cmd.arg = 0; 363 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 364 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 365 &req); 366 if (req.cmd->error != MMC_ERR_NONE) { 367 printf("erase err3 %d\n", req.cmd->error); 368 return (block); 369 } 370 /* Store one of remaining parts for the next call. */ 371 if (bp->bio_pblkno >= sc->eblock || block == start) { 372 sc->eblock = stop; /* Predict next forward. */ 373 sc->eend = end; 374 } else { 375 sc->eblock = block; /* Predict next backward. */ 376 sc->eend = start; 377 } 378 return (end); 379 } 380 381 static void 382 mmcsd_task(void *arg) 383 { 384 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 385 struct bio *bp; 386 int sz; 387 daddr_t block, end; 388 device_t dev; 389 390 dev = sc->dev; 391 while (1) { 392 MMCSD_LOCK(sc); 393 do { 394 if (sc->running == 0) 395 goto out; 396 bp = bioq_takefirst(&sc->bio_queue); 397 if (bp == NULL) 398 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 399 } while (bp == NULL); 400 MMCSD_UNLOCK(sc); 401 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 402 bp->bio_error = EROFS; 403 bp->bio_resid = bp->bio_bcount; 404 bp->bio_flags |= BIO_ERROR; 405 biodone(bp); 406 continue; 407 } 408 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 409 sz = sc->disk->d_sectorsize; 410 block = bp->bio_pblkno; 411 end = bp->bio_pblkno + (bp->bio_bcount / sz); 412 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 413 /* Access to the remaining erase block obsoletes it. */ 414 if (block < sc->eend && end > sc->eblock) 415 sc->eblock = sc->eend = 0; 416 block = mmcsd_rw(sc, bp); 417 } else if (bp->bio_cmd == BIO_DELETE) { 418 block = mmcsd_delete(sc, bp); 419 } 420 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 421 if (block < end) { 422 bp->bio_error = EIO; 423 bp->bio_resid = (end - block) * sz; 424 bp->bio_flags |= BIO_ERROR; 425 } 426 biodone(bp); 427 } 428 out: 429 /* tell parent we're done */ 430 sc->running = -1; 431 wakeup(sc); 432 MMCSD_UNLOCK(sc); 433 434 kproc_exit(0); 435 } 436 437 static const char * 438 mmcsd_card_name(device_t dev) 439 { 440 if (mmc_get_card_type(dev) == mode_mmc) 441 return ("MMC"); 442 if (mmc_get_high_cap(dev)) 443 return ("SDHC"); 444 return ("SD"); 445 } 446 447 static int 448 mmcsd_bus_bit_width(device_t dev) 449 { 450 if (mmc_get_bus_width(dev) == bus_width_1) 451 return (1); 452 if (mmc_get_bus_width(dev) == bus_width_4) 453 return (4); 454 return (8); 455 } 456 457 static device_method_t mmcsd_methods[] = { 458 DEVMETHOD(device_probe, mmcsd_probe), 459 DEVMETHOD(device_attach, mmcsd_attach), 460 DEVMETHOD(device_detach, mmcsd_detach), 461 {0, 0}, 462 }; 463 464 static driver_t mmcsd_driver = { 465 "mmcsd", 466 mmcsd_methods, 467 sizeof(struct mmcsd_softc), 468 }; 469 static devclass_t mmcsd_devclass; 470 471 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0); 472