1 /*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4 * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * Portions of this software may have been developed with reference to 27 * the SD Simplified Specification. The following disclaimer may apply: 28 * 29 * The following conditions apply to the release of the simplified 30 * specification ("Simplified Specification") by the SD Card Association and 31 * the SD Group. The Simplified Specification is a subset of the complete SD 32 * Specification which is owned by the SD Card Association and the SD 33 * Group. This Simplified Specification is provided on a non-confidential 34 * basis subject to the disclaimers below. Any implementation of the 35 * Simplified Specification may require a license from the SD Card 36 * Association, SD Group, SD-3C LLC or other third parties. 37 * 38 * Disclaimers: 39 * 40 * The information contained in the Simplified Specification is presented only 41 * as a standard specification for SD Cards and SD Host/Ancillary products and 42 * is provided "AS-IS" without any representations or warranties of any 43 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 44 * Card Association for any damages, any infringements of patents or other 45 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 46 * parties, which may result from its use. No license is granted by 47 * implication, estoppel or otherwise under any patent or other rights of the 48 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 49 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 50 * or the SD Card Association to disclose or distribute any technical 51 * information, know-how or other confidential information to any third party. 52 */ 53 54 #include <sys/cdefs.h> 55 __FBSDID("$FreeBSD$"); 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/bio.h> 60 #include <sys/bus.h> 61 #include <sys/conf.h> 62 #include <sys/fcntl.h> 63 #include <sys/ioccom.h> 64 #include <sys/kernel.h> 65 #include <sys/kthread.h> 66 #include <sys/lock.h> 67 #include <sys/malloc.h> 68 #include <sys/module.h> 69 #include <sys/mutex.h> 70 #include <sys/slicer.h> 71 #include <sys/time.h> 72 73 #include <geom/geom.h> 74 #include <geom/geom_disk.h> 75 76 #include <dev/mmc/bridge.h> 77 #include <dev/mmc/mmc_ioctl.h> 78 #include <dev/mmc/mmc_subr.h> 79 #include <dev/mmc/mmcbrvar.h> 80 #include <dev/mmc/mmcreg.h> 81 #include <dev/mmc/mmcvar.h> 82 83 #include "mmcbus_if.h" 84 85 #if __FreeBSD_version < 800002 86 #define kproc_create kthread_create 87 #define kproc_exit kthread_exit 88 #endif 89 90 #define MMCSD_CMD_RETRIES 5 91 92 #define MMCSD_FMT_BOOT "mmcsd%dboot" 93 #define MMCSD_FMT_GP "mmcsd%dgp" 94 #define MMCSD_FMT_RPMB "mmcsd%drpmb" 95 #define MMCSD_LABEL_ENH "enh" 96 97 #define MMCSD_PART_NAMELEN (16 + 1) 98 99 struct mmcsd_softc; 100 101 struct mmcsd_part { 102 struct mtx disk_mtx; 103 struct mtx ioctl_mtx; 104 struct mmcsd_softc *sc; 105 struct disk *disk; 106 struct proc *p; 107 struct bio_queue_head bio_queue; 108 daddr_t eblock, eend; /* Range remaining after the last erase. */ 109 u_int cnt; 110 u_int type; 111 int running; 112 int suspend; 113 int ioctl; 114 bool ro; 115 char name[MMCSD_PART_NAMELEN]; 116 }; 117 118 struct mmcsd_softc { 119 device_t dev; 120 device_t mmcbus; 121 struct mmcsd_part *part[MMC_PART_MAX]; 122 enum mmc_card_mode mode; 123 u_int max_data; /* Maximum data size [blocks] */ 124 u_int erase_sector; /* Device native erase sector size [blocks] */ 125 uint8_t high_cap; /* High Capacity device (block addressed) */ 126 uint8_t part_curr; /* Partition currently switched to */ 127 uint8_t ext_csd[MMC_EXTCSD_SIZE]; 128 uint16_t rca; 129 uint32_t flags; 130 #define MMCSD_INAND_CMD38 0x0001 131 #define MMCSD_USE_TRIM 0x0002 132 uint32_t cmd6_time; /* Generic switch timeout [us] */ 133 uint32_t part_time; /* Partition switch timeout [us] */ 134 off_t enh_base; /* Enhanced user data area slice base ... */ 135 off_t enh_size; /* ... and size [bytes] */ 136 int log_count; 137 struct timeval log_time; 138 struct cdev *rpmb_dev; 139 }; 140 141 static const char *errmsg[] = 142 { 143 "None", 144 "Timeout", 145 "Bad CRC", 146 "Fifo", 147 "Failed", 148 "Invalid", 149 "NO MEMORY" 150 }; 151 152 #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 153 154 /* bus entry points */ 155 static int mmcsd_attach(device_t dev); 156 static int mmcsd_detach(device_t dev); 157 static int mmcsd_probe(device_t dev); 158 159 /* disk routines */ 160 static int mmcsd_close(struct disk *dp); 161 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 162 off_t offset, size_t length); 163 static int mmcsd_getattr(struct bio *); 164 static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, 165 int fflag, struct thread *td); 166 static int mmcsd_open(struct disk *dp); 167 static void mmcsd_strategy(struct bio *bp); 168 static void mmcsd_task(void *arg); 169 170 /* RMPB cdev interface */ 171 static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 172 int fflag, struct thread *td); 173 174 static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type, 175 const char *name, u_int cnt, off_t media_size, bool ro); 176 static int mmcsd_bus_bit_width(device_t dev); 177 static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp); 178 static const char *mmcsd_errmsg(int e); 179 static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, 180 int fflag); 181 static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, 182 int fflag); 183 static uintmax_t mmcsd_pretty_size(off_t size, char *unit); 184 static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp); 185 static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel); 186 static int mmcsd_slicer(device_t dev, const char *provider, 187 struct flash_slice *slices, int *nslices); 188 static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, 189 u_int part); 190 191 #define MMCSD_DISK_LOCK(_part) mtx_lock(&(_part)->disk_mtx) 192 #define MMCSD_DISK_UNLOCK(_part) mtx_unlock(&(_part)->disk_mtx) 193 #define MMCSD_DISK_LOCK_INIT(_part) \ 194 mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF) 195 #define MMCSD_DISK_LOCK_DESTROY(_part) mtx_destroy(&(_part)->disk_mtx); 196 #define MMCSD_DISK_ASSERT_LOCKED(_part) \ 197 mtx_assert(&(_part)->disk_mtx, MA_OWNED); 198 #define MMCSD_DISK_ASSERT_UNLOCKED(_part) \ 199 mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED); 200 201 #define MMCSD_IOCTL_LOCK(_part) mtx_lock(&(_part)->ioctl_mtx) 202 #define MMCSD_IOCTL_UNLOCK(_part) mtx_unlock(&(_part)->ioctl_mtx) 203 #define MMCSD_IOCTL_LOCK_INIT(_part) \ 204 mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF) 205 #define MMCSD_IOCTL_LOCK_DESTROY(_part) mtx_destroy(&(_part)->ioctl_mtx); 206 #define MMCSD_IOCTL_ASSERT_LOCKED(_part) \ 207 mtx_assert(&(_part)->ioctl_mtx, MA_OWNED); 208 #define MMCSD_IOCLT_ASSERT_UNLOCKED(_part) \ 209 mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED); 210 211 static int 212 mmcsd_probe(device_t dev) 213 { 214 215 device_quiet(dev); 216 device_set_desc(dev, "MMC/SD Memory Card"); 217 return (0); 218 } 219 220 static int 221 mmcsd_attach(device_t dev) 222 { 223 device_t mmcbus; 224 struct mmcsd_softc *sc; 225 const uint8_t *ext_csd; 226 off_t erase_size, sector_size, size, wp_size; 227 uintmax_t bytes; 228 int err, i; 229 uint32_t quirks; 230 uint8_t rev; 231 bool comp, ro; 232 char unit[2]; 233 234 sc = device_get_softc(dev); 235 sc->dev = dev; 236 sc->mmcbus = mmcbus = device_get_parent(dev); 237 sc->mode = mmcbr_get_mode(mmcbus); 238 /* 239 * Note that in principle with an SDHCI-like re-tuning implementation, 240 * the maximum data size can change at runtime due to a device removal/ 241 * insertion that results in switches to/from a transfer mode involving 242 * re-tuning, iff there are multiple devices on a given bus. Until now 243 * mmc(4) lacks support for rescanning already attached buses, however, 244 * and sdhci(4) to date has no support for shared buses in the first 245 * place either. 246 */ 247 sc->max_data = mmc_get_max_data(dev); 248 sc->high_cap = mmc_get_high_cap(dev); 249 sc->rca = mmc_get_rca(dev); 250 sc->cmd6_time = mmc_get_cmd6_timeout(dev); 251 quirks = mmc_get_quirks(dev); 252 253 /* Only MMC >= 4.x devices support EXT_CSD. */ 254 if (mmc_get_spec_vers(dev) >= 4) { 255 MMCBUS_ACQUIRE_BUS(mmcbus, dev); 256 err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 257 MMCBUS_RELEASE_BUS(mmcbus, dev); 258 if (err != MMC_ERR_NONE) { 259 device_printf(dev, "Error reading EXT_CSD %s\n", 260 mmcsd_errmsg(err)); 261 return (ENXIO); 262 } 263 } 264 ext_csd = sc->ext_csd; 265 266 if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) { 267 if (mmc_get_spec_vers(dev) < 4) { 268 device_printf(dev, 269 "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n"); 270 return (EINVAL); 271 } 272 sc->flags |= MMCSD_INAND_CMD38; 273 } 274 275 /* 276 * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both 277 * insecure and secure TRIM. 278 */ 279 if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] & 280 EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 && 281 (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) { 282 if (bootverbose) 283 device_printf(dev, "taking advantage of TRIM\n"); 284 sc->flags |= MMCSD_USE_TRIM; 285 sc->erase_sector = 1; 286 } else 287 sc->erase_sector = mmc_get_erase_sector(dev); 288 289 /* 290 * Enhanced user data area and general purpose partitions are only 291 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB 292 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later. 293 */ 294 rev = ext_csd[EXT_CSD_REV]; 295 296 /* 297 * Ignore user-creatable enhanced user data area and general purpose 298 * partitions partitions as long as partitioning hasn't been finished. 299 */ 300 comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0; 301 302 /* 303 * Add enhanced user data area slice, unless it spans the entirety of 304 * the user data area. The enhanced area is of a multiple of high 305 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) * 306 * 512 KB) and its offset given in either sectors or bytes, depending 307 * on whether it's a high capacity device or not. 308 * NB: The slicer and its slices need to be registered before adding 309 * the disk for the corresponding user data area as re-tasting is 310 * racy. 311 */ 312 sector_size = mmc_get_sector_size(dev); 313 size = ext_csd[EXT_CSD_ENH_SIZE_MULT] + 314 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + 315 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16); 316 if (rev >= 4 && comp == TRUE && size > 0 && 317 (ext_csd[EXT_CSD_PART_SUPPORT] & 318 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 319 (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) { 320 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 321 MMC_SECTOR_SIZE; 322 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 323 size *= erase_size * wp_size; 324 if (size != mmc_get_media_size(dev) * sector_size) { 325 sc->enh_size = size; 326 sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] + 327 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) + 328 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) + 329 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) * 330 (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1); 331 } else if (bootverbose) 332 device_printf(dev, 333 "enhanced user data area spans entire device\n"); 334 } 335 336 /* 337 * Add default partition. This may be the only one or the user 338 * data area in case partitions are supported. 339 */ 340 ro = mmc_get_read_only(dev); 341 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd", 342 device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro); 343 344 if (mmc_get_spec_vers(dev) < 3) 345 return (0); 346 347 /* Belatedly announce enhanced user data slice. */ 348 if (sc->enh_size != 0) { 349 bytes = mmcsd_pretty_size(size, unit); 350 printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area " 351 "slice offset 0x%jx at %s\n", device_get_nameunit(dev), 352 MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base, 353 device_get_nameunit(dev)); 354 } 355 356 /* 357 * Determine partition switch timeout (provided in units of 10 ms) 358 * and ensure it's at least 300 ms as some eMMC chips lie. 359 */ 360 sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000, 361 300 * 1000); 362 363 /* Add boot partitions, which are of a fixed multiple of 128 KB. */ 364 size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 365 if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) { 366 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0, 367 MMCSD_FMT_BOOT, 0, size, 368 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 369 EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0)); 370 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1, 371 MMCSD_FMT_BOOT, 1, size, 372 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 373 EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0)); 374 } 375 376 /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */ 377 size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 378 if (rev >= 5 && size > 0) 379 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB, 380 MMCSD_FMT_RPMB, 0, size, ro); 381 382 if (rev <= 3 || comp == FALSE) 383 return (0); 384 385 /* 386 * Add general purpose partitions, which are of a multiple of high 387 * capacity write protect groups, too. 388 */ 389 if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) { 390 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 391 MMC_SECTOR_SIZE; 392 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 393 for (i = 0; i < MMC_PART_GP_MAX; i++) { 394 size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] + 395 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) + 396 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16); 397 if (size == 0) 398 continue; 399 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i, 400 MMCSD_FMT_GP, i, size * erase_size * wp_size, ro); 401 } 402 } 403 return (0); 404 } 405 406 static uintmax_t 407 mmcsd_pretty_size(off_t size, char *unit) 408 { 409 uintmax_t bytes; 410 int i; 411 412 /* 413 * Display in most natural units. There's no card < 1MB. However, 414 * RPMB partitions occasionally are smaller than that, though. The 415 * SD standard goes to 2 GiB due to its reliance on FAT, but the data 416 * format supports up to 4 GiB and some card makers push it up to this 417 * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the 418 * data format supports up to 2 TiB however. 2048 GB isn't too ugly, 419 * so we note it in passing here and don't add the code to print TB). 420 * Since these cards are sold in terms of MB and GB not MiB and GiB, 421 * report them like that. We also round to the nearest unit, since 422 * many cards are a few percent short, even of the power of 10 size. 423 */ 424 bytes = size; 425 unit[0] = unit[1] = '\0'; 426 for (i = 0; i <= 2 && bytes >= 1000; i++) { 427 bytes = (bytes + 1000 / 2 - 1) / 1000; 428 switch (i) { 429 case 0: 430 unit[0] = 'k'; 431 break; 432 case 1: 433 unit[0] = 'M'; 434 break; 435 case 2: 436 unit[0] = 'G'; 437 break; 438 default: 439 break; 440 } 441 } 442 return (bytes); 443 } 444 445 static struct cdevsw mmcsd_rpmb_cdevsw = { 446 .d_version = D_VERSION, 447 .d_name = "mmcsdrpmb", 448 .d_ioctl = mmcsd_ioctl_rpmb 449 }; 450 451 static void 452 mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt, 453 off_t media_size, bool ro) 454 { 455 struct make_dev_args args; 456 device_t dev, mmcbus; 457 const char *ext; 458 const uint8_t *ext_csd; 459 struct mmcsd_part *part; 460 struct disk *d; 461 uintmax_t bytes; 462 u_int gp; 463 uint32_t speed; 464 uint8_t extattr; 465 bool enh; 466 char unit[2]; 467 468 dev = sc->dev; 469 mmcbus = sc->mmcbus; 470 part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF, 471 M_WAITOK | M_ZERO); 472 part->sc = sc; 473 part->cnt = cnt; 474 part->type = type; 475 part->ro = ro; 476 snprintf(part->name, sizeof(part->name), name, device_get_unit(dev)); 477 478 MMCSD_IOCTL_LOCK_INIT(part); 479 480 /* 481 * For the RPMB partition, allow IOCTL access only. 482 * NB: If ever attaching RPMB partitions to disk(9), the re-tuning 483 * implementation and especially its pausing need to be revisited, 484 * because then re-tuning requests may be issued by the IOCTL half 485 * of this driver while re-tuning is already paused by the disk(9) 486 * one and vice versa. 487 */ 488 if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 489 make_dev_args_init(&args); 490 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 491 args.mda_devsw = &mmcsd_rpmb_cdevsw; 492 args.mda_uid = UID_ROOT; 493 args.mda_gid = GID_OPERATOR; 494 args.mda_mode = 0640; 495 args.mda_si_drv1 = part; 496 if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) { 497 device_printf(dev, "Failed to make RPMB device\n"); 498 free(part, M_DEVBUF); 499 return; 500 } 501 } else { 502 MMCSD_DISK_LOCK_INIT(part); 503 504 d = part->disk = disk_alloc(); 505 d->d_open = mmcsd_open; 506 d->d_close = mmcsd_close; 507 d->d_strategy = mmcsd_strategy; 508 d->d_ioctl = mmcsd_ioctl_disk; 509 d->d_dump = mmcsd_dump; 510 d->d_getattr = mmcsd_getattr; 511 d->d_name = part->name; 512 d->d_drv1 = part; 513 d->d_sectorsize = mmc_get_sector_size(dev); 514 d->d_maxsize = sc->max_data * d->d_sectorsize; 515 d->d_mediasize = media_size; 516 d->d_stripesize = sc->erase_sector * d->d_sectorsize; 517 d->d_unit = cnt; 518 d->d_flags = DISKFLAG_CANDELETE; 519 d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize; 520 strlcpy(d->d_ident, mmc_get_card_sn_string(dev), 521 sizeof(d->d_ident)); 522 strlcpy(d->d_descr, mmc_get_card_id_string(dev), 523 sizeof(d->d_descr)); 524 d->d_rotation_rate = DISK_RR_NON_ROTATING; 525 526 disk_create(d, DISK_VERSION); 527 bioq_init(&part->bio_queue); 528 529 part->running = 1; 530 kproc_create(&mmcsd_task, part, &part->p, 0, 0, 531 "%s%d: mmc/sd card", part->name, cnt); 532 } 533 534 bytes = mmcsd_pretty_size(media_size, unit); 535 if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) { 536 speed = mmcbr_get_clock(mmcbus); 537 printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 538 part->name, cnt, bytes, unit, mmc_get_card_id_string(dev), 539 ro ? " (read-only)" : "", device_get_nameunit(mmcbus), 540 speed / 1000000, (speed / 100000) % 10, 541 mmcsd_bus_bit_width(dev), sc->max_data); 542 } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 543 printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes, 544 unit, type, ro ? " (read-only)" : "", 545 device_get_nameunit(dev)); 546 } else { 547 enh = false; 548 ext = NULL; 549 extattr = 0; 550 if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 && 551 type <= EXT_CSD_PART_CONFIG_ACC_GP3) { 552 ext_csd = sc->ext_csd; 553 gp = type - EXT_CSD_PART_CONFIG_ACC_GP0; 554 if ((ext_csd[EXT_CSD_PART_SUPPORT] & 555 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 556 (ext_csd[EXT_CSD_PART_ATTR] & 557 (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0) 558 enh = true; 559 else if ((ext_csd[EXT_CSD_PART_SUPPORT] & 560 EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) { 561 extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR + 562 (gp / 2)] >> (4 * (gp % 2))) & 0xF; 563 switch (extattr) { 564 case EXT_CSD_EXT_PART_ATTR_DEFAULT: 565 break; 566 case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE: 567 ext = "system code"; 568 break; 569 case EXT_CSD_EXT_PART_ATTR_NPERSISTENT: 570 ext = "non-persistent"; 571 break; 572 default: 573 ext = "reserved"; 574 break; 575 } 576 } 577 } 578 if (ext == NULL) 579 printf("%s%d: %ju%sB partion %d%s%s at %s\n", 580 part->name, cnt, bytes, unit, type, enh ? 581 " enhanced" : "", ro ? " (read-only)" : "", 582 device_get_nameunit(dev)); 583 else 584 printf("%s%d: %ju%sB partion %d extended 0x%x " 585 "(%s)%s at %s\n", part->name, cnt, bytes, unit, 586 type, extattr, ext, ro ? " (read-only)" : "", 587 device_get_nameunit(dev)); 588 } 589 } 590 591 static int 592 mmcsd_slicer(device_t dev, const char *provider, 593 struct flash_slice *slices, int *nslices) 594 { 595 char name[MMCSD_PART_NAMELEN]; 596 struct mmcsd_softc *sc; 597 struct mmcsd_part *part; 598 599 *nslices = 0; 600 if (slices == NULL) 601 return (ENOMEM); 602 603 sc = device_get_softc(dev); 604 if (sc->enh_size == 0) 605 return (ENXIO); 606 607 part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT]; 608 snprintf(name, sizeof(name), "%s%d", part->disk->d_name, 609 part->disk->d_unit); 610 if (strcmp(name, provider) != 0) 611 return (ENXIO); 612 613 *nslices = 1; 614 slices[0].base = sc->enh_base; 615 slices[0].size = sc->enh_size; 616 slices[0].label = MMCSD_LABEL_ENH; 617 return (0); 618 } 619 620 static int 621 mmcsd_detach(device_t dev) 622 { 623 struct mmcsd_softc *sc = device_get_softc(dev); 624 struct mmcsd_part *part; 625 int i; 626 627 for (i = 0; i < MMC_PART_MAX; i++) { 628 part = sc->part[i]; 629 if (part != NULL) { 630 if (part->disk != NULL) { 631 MMCSD_DISK_LOCK(part); 632 part->suspend = 0; 633 if (part->running > 0) { 634 /* kill thread */ 635 part->running = 0; 636 wakeup(part); 637 /* wait for thread to finish. */ 638 while (part->running != -1) 639 msleep(part, &part->disk_mtx, 0, 640 "mmcsd disk detach", 0); 641 } 642 MMCSD_DISK_UNLOCK(part); 643 } 644 MMCSD_IOCTL_LOCK(part); 645 while (part->ioctl > 0) 646 msleep(part, &part->ioctl_mtx, 0, 647 "mmcsd IOCTL detach", 0); 648 part->ioctl = -1; 649 MMCSD_IOCTL_UNLOCK(part); 650 } 651 } 652 653 if (sc->rpmb_dev != NULL) 654 destroy_dev(sc->rpmb_dev); 655 656 for (i = 0; i < MMC_PART_MAX; i++) { 657 part = sc->part[i]; 658 if (part != NULL) { 659 if (part->disk != NULL) { 660 /* Flush the request queue. */ 661 bioq_flush(&part->bio_queue, NULL, ENXIO); 662 /* kill disk */ 663 disk_destroy(part->disk); 664 665 MMCSD_DISK_LOCK_DESTROY(part); 666 } 667 MMCSD_IOCTL_LOCK_DESTROY(part); 668 free(part, M_DEVBUF); 669 } 670 } 671 return (0); 672 } 673 674 static int 675 mmcsd_suspend(device_t dev) 676 { 677 struct mmcsd_softc *sc = device_get_softc(dev); 678 struct mmcsd_part *part; 679 int i; 680 681 for (i = 0; i < MMC_PART_MAX; i++) { 682 part = sc->part[i]; 683 if (part != NULL) { 684 if (part->disk != NULL) { 685 MMCSD_DISK_LOCK(part); 686 part->suspend = 1; 687 if (part->running > 0) { 688 /* kill thread */ 689 part->running = 0; 690 wakeup(part); 691 /* wait for thread to finish. */ 692 while (part->running != -1) 693 msleep(part, &part->disk_mtx, 0, 694 "mmcsd disk suspension", 0); 695 } 696 MMCSD_DISK_UNLOCK(part); 697 } 698 MMCSD_IOCTL_LOCK(part); 699 while (part->ioctl > 0) 700 msleep(part, &part->ioctl_mtx, 0, 701 "mmcsd IOCTL suspension", 0); 702 part->ioctl = -1; 703 MMCSD_IOCTL_UNLOCK(part); 704 } 705 } 706 return (0); 707 } 708 709 static int 710 mmcsd_resume(device_t dev) 711 { 712 struct mmcsd_softc *sc = device_get_softc(dev); 713 struct mmcsd_part *part; 714 int i; 715 716 for (i = 0; i < MMC_PART_MAX; i++) { 717 part = sc->part[i]; 718 if (part != NULL) { 719 if (part->disk != NULL) { 720 MMCSD_DISK_LOCK(part); 721 part->suspend = 0; 722 if (part->running <= 0) { 723 part->running = 1; 724 MMCSD_DISK_UNLOCK(part); 725 kproc_create(&mmcsd_task, part, 726 &part->p, 0, 0, "%s%d: mmc/sd card", 727 part->name, part->cnt); 728 } else 729 MMCSD_DISK_UNLOCK(part); 730 } 731 MMCSD_IOCTL_LOCK(part); 732 part->ioctl = 0; 733 MMCSD_IOCTL_UNLOCK(part); 734 } 735 } 736 return (0); 737 } 738 739 static int 740 mmcsd_open(struct disk *dp __unused) 741 { 742 743 return (0); 744 } 745 746 static int 747 mmcsd_close(struct disk *dp __unused) 748 { 749 750 return (0); 751 } 752 753 static void 754 mmcsd_strategy(struct bio *bp) 755 { 756 struct mmcsd_softc *sc; 757 struct mmcsd_part *part; 758 759 part = bp->bio_disk->d_drv1; 760 sc = part->sc; 761 MMCSD_DISK_LOCK(part); 762 if (part->running > 0 || part->suspend > 0) { 763 bioq_disksort(&part->bio_queue, bp); 764 MMCSD_DISK_UNLOCK(part); 765 wakeup(part); 766 } else { 767 MMCSD_DISK_UNLOCK(part); 768 biofinish(bp, NULL, ENXIO); 769 } 770 } 771 772 static int 773 mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 774 int fflag, struct thread *td __unused) 775 { 776 777 return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag)); 778 } 779 780 static int 781 mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag, 782 struct thread *td __unused) 783 { 784 785 return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag)); 786 } 787 788 static int 789 mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag) 790 { 791 struct mmc_ioc_cmd *mic; 792 struct mmc_ioc_multi_cmd *mimc; 793 int i, err; 794 u_long cnt, size; 795 796 if ((fflag & FREAD) == 0) 797 return (EBADF); 798 799 err = 0; 800 switch (cmd) { 801 case MMC_IOC_CMD: 802 mic = data; 803 err = mmcsd_ioctl_cmd(part, mic, fflag); 804 break; 805 case MMC_IOC_MULTI_CMD: 806 mimc = data; 807 if (mimc->num_of_cmds == 0) 808 break; 809 if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS) 810 return (EINVAL); 811 cnt = mimc->num_of_cmds; 812 size = sizeof(*mic) * cnt; 813 mic = malloc(size, M_TEMP, M_WAITOK); 814 err = copyin((const void *)mimc->cmds, mic, size); 815 if (err == 0) { 816 for (i = 0; i < cnt; i++) { 817 err = mmcsd_ioctl_cmd(part, &mic[i], fflag); 818 if (err != 0) 819 break; 820 } 821 } 822 free(mic, M_TEMP); 823 break; 824 default: 825 return (ENOIOCTL); 826 } 827 return (err); 828 } 829 830 static int 831 mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag) 832 { 833 struct mmc_command cmd; 834 struct mmc_data data; 835 struct mmcsd_softc *sc; 836 device_t dev, mmcbus; 837 void *dp; 838 u_long len; 839 int err, retries; 840 uint32_t status; 841 uint16_t rca; 842 843 if ((fflag & FWRITE) == 0 && mic->write_flag != 0) 844 return (EBADF); 845 846 if (part->ro == TRUE && mic->write_flag != 0) 847 return (EROFS); 848 849 /* 850 * We don't need to explicitly lock against the disk(9) half of this 851 * driver as MMCBUS_ACQUIRE_BUS() will serialize us. However, it's 852 * necessary to protect against races with detachment and suspension, 853 * especially since it's required to switch away from RPMB partitions 854 * again after an access (see mmcsd_switch_part()). 855 */ 856 MMCSD_IOCTL_LOCK(part); 857 while (part->ioctl != 0) { 858 if (part->ioctl < 0) { 859 MMCSD_IOCTL_UNLOCK(part); 860 return (ENXIO); 861 } 862 msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0); 863 } 864 part->ioctl = 1; 865 MMCSD_IOCTL_UNLOCK(part); 866 867 err = 0; 868 dp = NULL; 869 len = mic->blksz * mic->blocks; 870 if (len > MMC_IOC_MAX_BYTES) { 871 err = EOVERFLOW; 872 goto out; 873 } 874 if (len != 0) { 875 dp = malloc(len, M_TEMP, M_WAITOK); 876 err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len); 877 if (err != 0) 878 goto out; 879 } 880 memset(&cmd, 0, sizeof(cmd)); 881 memset(&data, 0, sizeof(data)); 882 cmd.opcode = mic->opcode; 883 cmd.arg = mic->arg; 884 cmd.flags = mic->flags; 885 if (len != 0) { 886 data.len = len; 887 data.data = dp; 888 data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE : 889 MMC_DATA_READ; 890 cmd.data = &data; 891 } 892 sc = part->sc; 893 rca = sc->rca; 894 if (mic->is_acmd == 0) { 895 /* Enforce/patch/restrict RCA-based commands */ 896 switch (cmd.opcode) { 897 case MMC_SET_RELATIVE_ADDR: 898 case MMC_SELECT_CARD: 899 err = EPERM; 900 goto out; 901 case MMC_STOP_TRANSMISSION: 902 if ((cmd.arg & 0x1) == 0) 903 break; 904 /* FALLTHROUGH */ 905 case MMC_SLEEP_AWAKE: 906 case MMC_SEND_CSD: 907 case MMC_SEND_CID: 908 case MMC_SEND_STATUS: 909 case MMC_GO_INACTIVE_STATE: 910 case MMC_FAST_IO: 911 case MMC_APP_CMD: 912 cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16); 913 break; 914 default: 915 break; 916 } 917 } 918 dev = sc->dev; 919 mmcbus = sc->mmcbus; 920 MMCBUS_ACQUIRE_BUS(mmcbus, dev); 921 err = mmcsd_switch_part(mmcbus, dev, rca, part->type); 922 if (err != MMC_ERR_NONE) 923 goto release; 924 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 925 err = mmcsd_set_blockcount(sc, mic->blocks, 926 mic->write_flag & (1 << 31)); 927 if (err != MMC_ERR_NONE) 928 goto switch_back; 929 } 930 if (mic->is_acmd != 0) 931 (void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0); 932 else 933 (void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0); 934 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 935 /* 936 * If the request went to the RPMB partition, try to ensure 937 * that the command actually has completed ... 938 */ 939 retries = MMCSD_CMD_RETRIES; 940 do { 941 err = mmc_send_status(mmcbus, dev, rca, &status); 942 if (err != MMC_ERR_NONE) 943 break; 944 if (R1_STATUS(status) == 0 && 945 R1_CURRENT_STATE(status) != R1_STATE_PRG) 946 break; 947 DELAY(1000); 948 } while (retries-- > 0); 949 950 switch_back: 951 /* ... and always switch back to the default partition. */ 952 err = mmcsd_switch_part(mmcbus, dev, rca, 953 EXT_CSD_PART_CONFIG_ACC_DEFAULT); 954 if (err != MMC_ERR_NONE) 955 goto release; 956 } 957 /* 958 * If EXT_CSD was changed, our copy is outdated now. Specifically, 959 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(), 960 * so retrieve EXT_CSD again. 961 */ 962 if (cmd.opcode == MMC_SWITCH_FUNC) { 963 err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 964 if (err != MMC_ERR_NONE) 965 goto release; 966 } 967 MMCBUS_RELEASE_BUS(mmcbus, dev); 968 if (cmd.error != MMC_ERR_NONE) { 969 switch (cmd.error) { 970 case MMC_ERR_TIMEOUT: 971 err = ETIMEDOUT; 972 break; 973 case MMC_ERR_BADCRC: 974 err = EILSEQ; 975 break; 976 case MMC_ERR_INVALID: 977 err = EINVAL; 978 break; 979 case MMC_ERR_NO_MEMORY: 980 err = ENOMEM; 981 break; 982 default: 983 err = EIO; 984 break; 985 } 986 goto out; 987 } 988 memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t)); 989 if (mic->write_flag == 0 && len != 0) { 990 err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len); 991 if (err != 0) 992 goto out; 993 } 994 goto out; 995 996 release: 997 MMCBUS_RELEASE_BUS(mmcbus, dev); 998 err = EIO; 999 1000 out: 1001 MMCSD_IOCTL_LOCK(part); 1002 part->ioctl = 0; 1003 MMCSD_IOCTL_UNLOCK(part); 1004 wakeup(part); 1005 if (dp != NULL) 1006 free(dp, M_TEMP); 1007 return (err); 1008 } 1009 1010 static int 1011 mmcsd_getattr(struct bio *bp) 1012 { 1013 struct mmcsd_part *part; 1014 device_t dev; 1015 1016 if (strcmp(bp->bio_attribute, "MMC::device") == 0) { 1017 if (bp->bio_length != sizeof(dev)) 1018 return (EFAULT); 1019 part = bp->bio_disk->d_drv1; 1020 dev = part->sc->dev; 1021 bcopy(&dev, bp->bio_data, sizeof(dev)); 1022 bp->bio_completed = bp->bio_length; 1023 return (0); 1024 } 1025 return (-1); 1026 } 1027 1028 static int 1029 mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable) 1030 { 1031 struct mmc_command cmd; 1032 struct mmc_request req; 1033 1034 memset(&req, 0, sizeof(req)); 1035 memset(&cmd, 0, sizeof(cmd)); 1036 cmd.mrq = &req; 1037 req.cmd = &cmd; 1038 cmd.opcode = MMC_SET_BLOCK_COUNT; 1039 cmd.arg = count & 0x0000FFFF; 1040 if (reliable) 1041 cmd.arg |= 1 << 31; 1042 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1043 MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req); 1044 return (cmd.error); 1045 } 1046 1047 static int 1048 mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part) 1049 { 1050 struct mmcsd_softc *sc; 1051 int err; 1052 uint8_t value; 1053 1054 sc = device_get_softc(dev); 1055 1056 if (sc->mode == mode_sd) 1057 return (MMC_ERR_NONE); 1058 1059 /* 1060 * According to section "6.2.2 Command restrictions" of the eMMC 1061 * specification v5.1, CMD19/CMD21 aren't allowed to be used with 1062 * RPMB partitions. So we pause re-tuning along with triggering 1063 * it up-front to decrease the likelihood of re-tuning becoming 1064 * necessary while accessing an RPMB partition. Consequently, an 1065 * RPMB partition should immediately be switched away from again 1066 * after an access in order to allow for re-tuning to take place 1067 * anew. 1068 */ 1069 if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1070 MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true); 1071 1072 if (sc->part_curr == part) 1073 return (MMC_ERR_NONE); 1074 1075 value = (sc->ext_csd[EXT_CSD_PART_CONFIG] & 1076 ~EXT_CSD_PART_CONFIG_ACC_MASK) | part; 1077 /* Jump! */ 1078 err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL, 1079 EXT_CSD_PART_CONFIG, value, sc->part_time, true); 1080 if (err != MMC_ERR_NONE) { 1081 if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1082 MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 1083 return (err); 1084 } 1085 1086 sc->ext_csd[EXT_CSD_PART_CONFIG] = value; 1087 if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) 1088 MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 1089 sc->part_curr = part; 1090 return (MMC_ERR_NONE); 1091 } 1092 1093 static const char * 1094 mmcsd_errmsg(int e) 1095 { 1096 1097 if (e < 0 || e > MMC_ERR_MAX) 1098 return "Bad error code"; 1099 return (errmsg[e]); 1100 } 1101 1102 static daddr_t 1103 mmcsd_rw(struct mmcsd_part *part, struct bio *bp) 1104 { 1105 daddr_t block, end; 1106 struct mmc_command cmd; 1107 struct mmc_command stop; 1108 struct mmc_request req; 1109 struct mmc_data data; 1110 struct mmcsd_softc *sc; 1111 device_t dev, mmcbus; 1112 u_int numblocks, sz; 1113 char *vaddr; 1114 1115 sc = part->sc; 1116 dev = sc->dev; 1117 mmcbus = sc->mmcbus; 1118 1119 block = bp->bio_pblkno; 1120 sz = part->disk->d_sectorsize; 1121 end = bp->bio_pblkno + (bp->bio_bcount / sz); 1122 while (block < end) { 1123 vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 1124 numblocks = min(end - block, sc->max_data); 1125 memset(&req, 0, sizeof(req)); 1126 memset(&cmd, 0, sizeof(cmd)); 1127 memset(&stop, 0, sizeof(stop)); 1128 memset(&data, 0, sizeof(data)); 1129 cmd.mrq = &req; 1130 req.cmd = &cmd; 1131 cmd.data = &data; 1132 if (bp->bio_cmd == BIO_READ) { 1133 if (numblocks > 1) 1134 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 1135 else 1136 cmd.opcode = MMC_READ_SINGLE_BLOCK; 1137 } else { 1138 if (numblocks > 1) 1139 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 1140 else 1141 cmd.opcode = MMC_WRITE_BLOCK; 1142 } 1143 cmd.arg = block; 1144 if (sc->high_cap == 0) 1145 cmd.arg <<= 9; 1146 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1147 data.data = vaddr; 1148 data.mrq = &req; 1149 if (bp->bio_cmd == BIO_READ) 1150 data.flags = MMC_DATA_READ; 1151 else 1152 data.flags = MMC_DATA_WRITE; 1153 data.len = numblocks * sz; 1154 if (numblocks > 1) { 1155 data.flags |= MMC_DATA_MULTI; 1156 stop.opcode = MMC_STOP_TRANSMISSION; 1157 stop.arg = 0; 1158 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 1159 stop.mrq = &req; 1160 req.stop = &stop; 1161 } 1162 MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1163 if (req.cmd->error != MMC_ERR_NONE) { 1164 if (ppsratecheck(&sc->log_time, &sc->log_count, 1165 LOG_PPS)) 1166 device_printf(dev, "Error indicated: %d %s\n", 1167 req.cmd->error, 1168 mmcsd_errmsg(req.cmd->error)); 1169 break; 1170 } 1171 block += numblocks; 1172 } 1173 return (block); 1174 } 1175 1176 static daddr_t 1177 mmcsd_delete(struct mmcsd_part *part, struct bio *bp) 1178 { 1179 daddr_t block, end, start, stop; 1180 struct mmc_command cmd; 1181 struct mmc_request req; 1182 struct mmcsd_softc *sc; 1183 device_t dev, mmcbus; 1184 u_int erase_sector, sz; 1185 int err; 1186 bool use_trim; 1187 1188 sc = part->sc; 1189 dev = sc->dev; 1190 mmcbus = sc->mmcbus; 1191 1192 block = bp->bio_pblkno; 1193 sz = part->disk->d_sectorsize; 1194 end = bp->bio_pblkno + (bp->bio_bcount / sz); 1195 use_trim = sc->flags & MMCSD_USE_TRIM; 1196 if (use_trim == true) { 1197 start = block; 1198 stop = end; 1199 } else { 1200 /* Coalesce with the remainder of the previous request. */ 1201 if (block > part->eblock && block <= part->eend) 1202 block = part->eblock; 1203 if (end >= part->eblock && end < part->eend) 1204 end = part->eend; 1205 /* Safely round to the erase sector boundaries. */ 1206 erase_sector = sc->erase_sector; 1207 start = block + erase_sector - 1; /* Round up. */ 1208 start -= start % erase_sector; 1209 stop = end; /* Round down. */ 1210 stop -= end % erase_sector; 1211 /* 1212 * We can't erase an area smaller than an erase sector, so 1213 * store it for later. 1214 */ 1215 if (start >= stop) { 1216 part->eblock = block; 1217 part->eend = end; 1218 return (end); 1219 } 1220 } 1221 1222 if ((sc->flags & MMCSD_INAND_CMD38) != 0) { 1223 err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 1224 EXT_CSD_INAND_CMD38, use_trim == true ? 1225 EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE, 1226 sc->cmd6_time, true); 1227 if (err != MMC_ERR_NONE) { 1228 device_printf(dev, 1229 "Setting iNAND erase command failed %s\n", 1230 mmcsd_errmsg(err)); 1231 return (block); 1232 } 1233 } 1234 1235 /* 1236 * Pause re-tuning so it won't interfere with the order of erase 1237 * commands. Note that these latter don't use the data lines, so 1238 * re-tuning shouldn't actually become necessary during erase. 1239 */ 1240 MMCBUS_RETUNE_PAUSE(mmcbus, dev, false); 1241 /* Set erase start position. */ 1242 memset(&req, 0, sizeof(req)); 1243 memset(&cmd, 0, sizeof(cmd)); 1244 cmd.mrq = &req; 1245 req.cmd = &cmd; 1246 if (mmc_get_card_type(dev) == mode_sd) 1247 cmd.opcode = SD_ERASE_WR_BLK_START; 1248 else 1249 cmd.opcode = MMC_ERASE_GROUP_START; 1250 cmd.arg = start; 1251 if (sc->high_cap == 0) 1252 cmd.arg <<= 9; 1253 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1254 MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1255 if (req.cmd->error != MMC_ERR_NONE) { 1256 device_printf(dev, "Setting erase start position failed %s\n", 1257 mmcsd_errmsg(req.cmd->error)); 1258 block = bp->bio_pblkno; 1259 goto unpause; 1260 } 1261 /* Set erase stop position. */ 1262 memset(&req, 0, sizeof(req)); 1263 memset(&cmd, 0, sizeof(cmd)); 1264 req.cmd = &cmd; 1265 if (mmc_get_card_type(dev) == mode_sd) 1266 cmd.opcode = SD_ERASE_WR_BLK_END; 1267 else 1268 cmd.opcode = MMC_ERASE_GROUP_END; 1269 cmd.arg = stop; 1270 if (sc->high_cap == 0) 1271 cmd.arg <<= 9; 1272 cmd.arg--; 1273 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1274 MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1275 if (req.cmd->error != MMC_ERR_NONE) { 1276 device_printf(dev, "Setting erase stop position failed %s\n", 1277 mmcsd_errmsg(req.cmd->error)); 1278 block = bp->bio_pblkno; 1279 goto unpause; 1280 } 1281 /* Erase range. */ 1282 memset(&req, 0, sizeof(req)); 1283 memset(&cmd, 0, sizeof(cmd)); 1284 req.cmd = &cmd; 1285 cmd.opcode = MMC_ERASE; 1286 cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE; 1287 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 1288 MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1289 if (req.cmd->error != MMC_ERR_NONE) { 1290 device_printf(dev, "Issuing erase command failed %s\n", 1291 mmcsd_errmsg(req.cmd->error)); 1292 block = bp->bio_pblkno; 1293 goto unpause; 1294 } 1295 if (use_trim == false) { 1296 /* Store one of the remaining parts for the next call. */ 1297 if (bp->bio_pblkno >= part->eblock || block == start) { 1298 part->eblock = stop; /* Predict next forward. */ 1299 part->eend = end; 1300 } else { 1301 part->eblock = block; /* Predict next backward. */ 1302 part->eend = start; 1303 } 1304 } 1305 block = end; 1306 unpause: 1307 MMCBUS_RETUNE_UNPAUSE(mmcbus, dev); 1308 return (block); 1309 } 1310 1311 static int 1312 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 1313 size_t length) 1314 { 1315 struct bio bp; 1316 daddr_t block, end; 1317 struct disk *disk; 1318 struct mmcsd_softc *sc; 1319 struct mmcsd_part *part; 1320 device_t dev, mmcbus; 1321 int err; 1322 1323 /* length zero is special and really means flush buffers to media */ 1324 if (!length) 1325 return (0); 1326 1327 disk = arg; 1328 part = disk->d_drv1; 1329 sc = part->sc; 1330 dev = sc->dev; 1331 mmcbus = sc->mmcbus; 1332 1333 g_reset_bio(&bp); 1334 bp.bio_disk = disk; 1335 bp.bio_pblkno = offset / disk->d_sectorsize; 1336 bp.bio_bcount = length; 1337 bp.bio_data = virtual; 1338 bp.bio_cmd = BIO_WRITE; 1339 end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize; 1340 MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1341 err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 1342 if (err != MMC_ERR_NONE) { 1343 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) 1344 device_printf(dev, "Partition switch error\n"); 1345 MMCBUS_RELEASE_BUS(mmcbus, dev); 1346 return (EIO); 1347 } 1348 block = mmcsd_rw(part, &bp); 1349 MMCBUS_RELEASE_BUS(mmcbus, dev); 1350 return ((end < block) ? EIO : 0); 1351 } 1352 1353 static void 1354 mmcsd_task(void *arg) 1355 { 1356 daddr_t block, end; 1357 struct mmcsd_part *part; 1358 struct mmcsd_softc *sc; 1359 struct bio *bp; 1360 device_t dev, mmcbus; 1361 int err, sz; 1362 1363 part = arg; 1364 sc = part->sc; 1365 dev = sc->dev; 1366 mmcbus = sc->mmcbus; 1367 1368 while (1) { 1369 MMCSD_DISK_LOCK(part); 1370 do { 1371 if (part->running == 0) 1372 goto out; 1373 bp = bioq_takefirst(&part->bio_queue); 1374 if (bp == NULL) 1375 msleep(part, &part->disk_mtx, PRIBIO, 1376 "mmcsd disk jobqueue", 0); 1377 } while (bp == NULL); 1378 MMCSD_DISK_UNLOCK(part); 1379 if (bp->bio_cmd != BIO_READ && part->ro) { 1380 bp->bio_error = EROFS; 1381 bp->bio_resid = bp->bio_bcount; 1382 bp->bio_flags |= BIO_ERROR; 1383 biodone(bp); 1384 continue; 1385 } 1386 MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1387 sz = part->disk->d_sectorsize; 1388 block = bp->bio_pblkno; 1389 end = bp->bio_pblkno + (bp->bio_bcount / sz); 1390 err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 1391 if (err != MMC_ERR_NONE) { 1392 if (ppsratecheck(&sc->log_time, &sc->log_count, 1393 LOG_PPS)) 1394 device_printf(dev, "Partition switch error\n"); 1395 goto release; 1396 } 1397 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 1398 /* Access to the remaining erase block obsoletes it. */ 1399 if (block < part->eend && end > part->eblock) 1400 part->eblock = part->eend = 0; 1401 block = mmcsd_rw(part, bp); 1402 } else if (bp->bio_cmd == BIO_DELETE) { 1403 block = mmcsd_delete(part, bp); 1404 } 1405 release: 1406 MMCBUS_RELEASE_BUS(mmcbus, dev); 1407 if (block < end) { 1408 bp->bio_error = EIO; 1409 bp->bio_resid = (end - block) * sz; 1410 bp->bio_flags |= BIO_ERROR; 1411 } else { 1412 bp->bio_resid = 0; 1413 } 1414 biodone(bp); 1415 } 1416 out: 1417 /* tell parent we're done */ 1418 part->running = -1; 1419 MMCSD_DISK_UNLOCK(part); 1420 wakeup(part); 1421 1422 kproc_exit(0); 1423 } 1424 1425 static int 1426 mmcsd_bus_bit_width(device_t dev) 1427 { 1428 1429 if (mmc_get_bus_width(dev) == bus_width_1) 1430 return (1); 1431 if (mmc_get_bus_width(dev) == bus_width_4) 1432 return (4); 1433 return (8); 1434 } 1435 1436 static device_method_t mmcsd_methods[] = { 1437 DEVMETHOD(device_probe, mmcsd_probe), 1438 DEVMETHOD(device_attach, mmcsd_attach), 1439 DEVMETHOD(device_detach, mmcsd_detach), 1440 DEVMETHOD(device_suspend, mmcsd_suspend), 1441 DEVMETHOD(device_resume, mmcsd_resume), 1442 DEVMETHOD_END 1443 }; 1444 1445 static driver_t mmcsd_driver = { 1446 "mmcsd", 1447 mmcsd_methods, 1448 sizeof(struct mmcsd_softc), 1449 }; 1450 static devclass_t mmcsd_devclass; 1451 1452 static int 1453 mmcsd_handler(module_t mod __unused, int what, void *arg __unused) 1454 { 1455 1456 switch (what) { 1457 case MOD_LOAD: 1458 flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC, 1459 TRUE); 1460 return (0); 1461 case MOD_UNLOAD: 1462 flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE); 1463 return (0); 1464 } 1465 return (0); 1466 } 1467 1468 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL); 1469 MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0); 1470 MMC_DEPEND(mmcsd); 1471