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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
26 * Portions of this software may have been developed with reference to
27 * the SD Simplified Specification. The following disclaimer may apply:
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.
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.
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/systm.h>
62 #include <sys/fcntl.h>
63 #include <sys/ioccom.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
67 #include <sys/malloc.h>
68 #include <sys/module.h>
69 #include <sys/mutex.h>
70 #include <sys/slicer.h>
73 #include <geom/geom.h>
74 #include <geom/geom_disk.h>
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>
83 #include "mmcbus_if.h"
85 #if __FreeBSD_version < 800002
86 #define kproc_create kthread_create
87 #define kproc_exit kthread_exit
90 #define MMCSD_CMD_RETRIES 5
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"
97 #define MMCSD_PART_NAMELEN (16 + 1)
103 struct mmcsd_softc *sc;
106 struct bio_queue_head bio_queue;
107 daddr_t eblock, eend; /* Range remaining after the last erase. */
113 char name[MMCSD_PART_NAMELEN];
119 struct mmcsd_part *part[MMC_PART_MAX];
120 enum mmc_card_mode mode;
121 uint8_t part_curr; /* Partition currently switched to */
122 uint8_t ext_csd[MMC_EXTCSD_SIZE];
124 uint32_t part_time; /* Partition switch timeout [us] */
125 off_t enh_base; /* Enhanced user data area slice base ... */
126 off_t enh_size; /* ... and size [bytes] */
128 struct timeval log_time;
129 struct cdev *rpmb_dev;
132 static const char *errmsg[] =
143 #define LOG_PPS 5 /* Log no more than 5 errors per second. */
145 /* bus entry points */
146 static int mmcsd_attach(device_t dev);
147 static int mmcsd_detach(device_t dev);
148 static int mmcsd_probe(device_t dev);
151 static int mmcsd_close(struct disk *dp);
152 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
153 off_t offset, size_t length);
154 static int mmcsd_getattr(struct bio *);
155 static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
156 int fflag, struct thread *td);
157 static int mmcsd_open(struct disk *dp);
158 static void mmcsd_strategy(struct bio *bp);
159 static void mmcsd_task(void *arg);
161 /* RMPB cdev interface */
162 static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
163 int fflag, struct thread *td);
165 static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
166 const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
167 static int mmcsd_bus_bit_width(device_t dev);
168 static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
169 static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
171 static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
173 static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
174 static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
175 static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
176 static int mmcsd_slicer(device_t dev, const char *provider,
177 struct flash_slice *slices, int *nslices);
178 static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
181 #define MMCSD_PART_LOCK(_part) mtx_lock(&(_part)->part_mtx)
182 #define MMCSD_PART_UNLOCK(_part) mtx_unlock(&(_part)->part_mtx)
183 #define MMCSD_PART_LOCK_INIT(_part) \
184 mtx_init(&(_part)->part_mtx, (_part)->name, "mmcsd part", MTX_DEF)
185 #define MMCSD_PART_LOCK_DESTROY(_part) mtx_destroy(&(_part)->part_mtx);
186 #define MMCSD_PART_ASSERT_LOCKED(_part) \
187 mtx_assert(&(_part)->part_mtx, MA_OWNED);
188 #define MMCSD_PART_ASSERT_UNLOCKED(_part) \
189 mtx_assert(&(_part)->part_mtx, MA_NOTOWNED);
192 mmcsd_probe(device_t dev)
196 device_set_desc(dev, "MMC/SD Memory Card");
201 mmcsd_attach(device_t dev)
204 struct mmcsd_softc *sc;
205 const uint8_t *ext_csd;
206 off_t erase_size, sector_size, size, wp_size;
213 sc = device_get_softc(dev);
215 sc->mmcbr = mmcbr = device_get_parent(dev);
216 sc->mode = mmcbr_get_mode(mmcbr);
217 sc->rca = mmc_get_rca(dev);
219 /* Only MMC >= 4.x devices support EXT_CSD. */
220 if (mmc_get_spec_vers(dev) >= 4) {
221 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
222 err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
223 MMCBUS_RELEASE_BUS(mmcbr, dev);
224 if (err != MMC_ERR_NONE)
225 bzero(sc->ext_csd, sizeof(sc->ext_csd));
227 ext_csd = sc->ext_csd;
230 * Enhanced user data area and general purpose partitions are only
231 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
232 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
234 rev = ext_csd[EXT_CSD_REV];
237 * Ignore user-creatable enhanced user data area and general purpose
238 * partitions partitions as long as partitioning hasn't been finished.
240 comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
243 * Add enhanced user data area slice, unless it spans the entirety of
244 * the user data area. The enhanced area is of a multiple of high
245 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
246 * 512 KB) and its offset given in either sectors or bytes, depending
247 * on whether it's a high capacity device or not.
248 * NB: The slicer and its slices need to be registered before adding
249 * the disk for the corresponding user data area as re-tasting is
252 sector_size = mmc_get_sector_size(dev);
253 size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
254 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
255 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
256 if (rev >= 4 && comp == TRUE && size > 0 &&
257 (ext_csd[EXT_CSD_PART_SUPPORT] &
258 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
259 (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
260 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
262 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
263 size *= erase_size * wp_size;
264 if (size != mmc_get_media_size(dev) * sector_size) {
266 sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
267 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
268 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
269 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
270 (mmc_get_high_cap(dev) ? MMC_SECTOR_SIZE : 1);
271 } else if (bootverbose)
273 "enhanced user data area spans entire device\n");
277 * Add default partition. This may be the only one or the user
278 * data area in case partitions are supported.
280 ro = mmc_get_read_only(dev);
281 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
282 device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
283 mmc_get_erase_sector(dev) * sector_size, ro);
285 if (mmc_get_spec_vers(dev) < 3)
288 /* Belatedly announce enhanced user data slice. */
289 if (sc->enh_size != 0) {
290 bytes = mmcsd_pretty_size(size, unit);
291 printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
292 "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
293 MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
294 device_get_nameunit(dev));
298 * Determine partition switch timeout (provided in units of 10 ms)
299 * and ensure it's at least 300 ms as some eMMC chips lie.
301 sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
304 /* Add boot partitions, which are of a fixed multiple of 128 KB. */
305 size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
306 if (size > 0 && (mmcbr_get_caps(mmcbr) & MMC_CAP_BOOT_NOACC) == 0) {
307 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
308 MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
309 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
310 EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
311 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
312 MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
313 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
314 EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
317 /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
318 size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
319 if (rev >= 5 && size > 0)
320 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
321 MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
323 if (rev <= 3 || comp == FALSE)
327 * Add general purpose partitions, which are of a multiple of high
328 * capacity write protect groups, too.
330 if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
331 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
333 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
334 for (i = 0; i < MMC_PART_GP_MAX; i++) {
335 size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
336 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
337 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
340 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
341 MMCSD_FMT_GP, i, size * erase_size * wp_size,
349 mmcsd_pretty_size(off_t size, char *unit)
355 * Display in most natural units. There's no card < 1MB. However,
356 * RPMB partitions occasionally are smaller than that, though. The
357 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
358 * format supports up to 4 GiB and some card makers push it up to this
359 * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the
360 * data format supports up to 2 TiB however. 2048 GB isn't too ugly,
361 * so we note it in passing here and don't add the code to print TB).
362 * Since these cards are sold in terms of MB and GB not MiB and GiB,
363 * report them like that. We also round to the nearest unit, since
364 * many cards are a few percent short, even of the power of 10 size.
367 unit[0] = unit[1] = '\0';
368 for (i = 0; i <= 2 && bytes >= 1000; i++) {
369 bytes = (bytes + 1000 / 2 - 1) / 1000;
387 static struct cdevsw mmcsd_rpmb_cdevsw = {
388 .d_version = D_VERSION,
389 .d_name = "mmcsdrpmb",
390 .d_ioctl = mmcsd_ioctl_rpmb
394 mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
395 off_t media_size, off_t erase_size, bool ro)
397 struct make_dev_args args;
400 const uint8_t *ext_csd;
401 struct mmcsd_part *part;
412 part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
418 snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
420 /* For the RPMB partition, allow IOCTL access only. */
421 if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
422 make_dev_args_init(&args);
423 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
424 args.mda_devsw = &mmcsd_rpmb_cdevsw;
425 args.mda_uid = UID_ROOT;
426 args.mda_gid = GID_OPERATOR;
427 args.mda_mode = 0640;
428 args.mda_si_drv1 = part;
429 if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
430 device_printf(dev, "Failed to make RPMB device\n");
431 free(part, M_DEVBUF);
435 MMCSD_PART_LOCK_INIT(part);
437 d = part->disk = disk_alloc();
438 d->d_open = mmcsd_open;
439 d->d_close = mmcsd_close;
440 d->d_strategy = mmcsd_strategy;
441 d->d_ioctl = mmcsd_ioctl_disk;
442 d->d_dump = mmcsd_dump;
443 d->d_getattr = mmcsd_getattr;
444 d->d_name = part->name;
446 d->d_sectorsize = mmc_get_sector_size(dev);
447 d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
448 d->d_mediasize = media_size;
449 d->d_stripesize = erase_size;
451 d->d_flags = DISKFLAG_CANDELETE;
452 d->d_delmaxsize = erase_size;
453 strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
455 strlcpy(d->d_descr, mmc_get_card_id_string(dev),
457 d->d_rotation_rate = DISK_RR_NON_ROTATING;
459 disk_create(d, DISK_VERSION);
460 bioq_init(&part->bio_queue);
463 kproc_create(&mmcsd_task, part, &part->p, 0, 0,
464 "%s%d: mmc/sd card", part->name, cnt);
467 bytes = mmcsd_pretty_size(media_size, unit);
468 if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
469 speed = mmcbr_get_clock(mmcbr);
470 printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
471 part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
472 ro ? " (read-only)" : "", device_get_nameunit(mmcbr),
473 speed / 1000000, (speed / 100000) % 10,
474 mmcsd_bus_bit_width(dev), mmc_get_max_data(dev));
475 } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
476 printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
477 unit, type, ro ? " (read-only)" : "",
478 device_get_nameunit(dev));
483 if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
484 type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
485 ext_csd = sc->ext_csd;
486 gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
487 if ((ext_csd[EXT_CSD_PART_SUPPORT] &
488 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
489 (ext_csd[EXT_CSD_PART_ATTR] &
490 (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
492 else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
493 EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
494 extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
495 (gp / 2)] >> (4 * (gp % 2))) & 0xF;
497 case EXT_CSD_EXT_PART_ATTR_DEFAULT:
499 case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
502 case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
503 ext = "non-persistent";
512 printf("%s%d: %ju%sB partion %d%s%s at %s\n",
513 part->name, cnt, bytes, unit, type, enh ?
514 " enhanced" : "", ro ? " (read-only)" : "",
515 device_get_nameunit(dev));
517 printf("%s%d: %ju%sB partion %d extended 0x%x "
518 "(%s)%s at %s\n", part->name, cnt, bytes, unit,
519 type, extattr, ext, ro ? " (read-only)" : "",
520 device_get_nameunit(dev));
525 mmcsd_slicer(device_t dev, const char *provider,
526 struct flash_slice *slices, int *nslices)
528 char name[MMCSD_PART_NAMELEN];
529 struct mmcsd_softc *sc;
530 struct mmcsd_part *part;
536 sc = device_get_softc(dev);
537 if (sc->enh_size == 0)
540 part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
541 snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
543 if (strcmp(name, provider) != 0)
547 slices[0].base = sc->enh_base;
548 slices[0].size = sc->enh_size;
549 slices[0].label = MMCSD_LABEL_ENH;
554 mmcsd_detach(device_t dev)
556 struct mmcsd_softc *sc = device_get_softc(dev);
557 struct mmcsd_part *part;
560 for (i = 0; i < MMC_PART_MAX; i++) {
562 if (part != NULL && part->disk != NULL) {
563 MMCSD_PART_LOCK(part);
565 if (part->running > 0) {
569 /* wait for thread to finish. */
570 while (part->running != -1)
571 msleep(part, &part->part_mtx, 0,
574 MMCSD_PART_UNLOCK(part);
578 if (sc->rpmb_dev != NULL)
579 destroy_dev(sc->rpmb_dev);
581 for (i = 0; i < MMC_PART_MAX; i++) {
584 if (part->disk != NULL) {
585 /* Flush the request queue. */
586 bioq_flush(&part->bio_queue, NULL, ENXIO);
588 disk_destroy(part->disk);
590 MMCSD_PART_LOCK_DESTROY(part);
592 free(part, M_DEVBUF);
599 mmcsd_suspend(device_t dev)
601 struct mmcsd_softc *sc = device_get_softc(dev);
602 struct mmcsd_part *part;
605 for (i = 0; i < MMC_PART_MAX; i++) {
607 if (part != NULL && part->disk != NULL) {
608 MMCSD_PART_LOCK(part);
610 if (part->running > 0) {
614 /* wait for thread to finish. */
615 while (part->running != -1)
616 msleep(part, &part->part_mtx, 0,
619 MMCSD_PART_UNLOCK(part);
626 mmcsd_resume(device_t dev)
628 struct mmcsd_softc *sc = device_get_softc(dev);
629 struct mmcsd_part *part;
632 for (i = 0; i < MMC_PART_MAX; i++) {
634 if (part != NULL && part->disk != NULL) {
635 MMCSD_PART_LOCK(part);
637 if (part->running <= 0) {
639 kproc_create(&mmcsd_task, part, &part->p, 0, 0,
640 "%s%d: mmc/sd card", part->name, part->cnt);
641 MMCSD_PART_UNLOCK(part);
643 MMCSD_PART_UNLOCK(part);
650 mmcsd_open(struct disk *dp __unused)
657 mmcsd_close(struct disk *dp __unused)
664 mmcsd_strategy(struct bio *bp)
666 struct mmcsd_softc *sc;
667 struct mmcsd_part *part;
669 part = bp->bio_disk->d_drv1;
671 MMCSD_PART_LOCK(part);
672 if (part->running > 0 || part->suspend > 0) {
673 bioq_disksort(&part->bio_queue, bp);
674 MMCSD_PART_UNLOCK(part);
677 MMCSD_PART_UNLOCK(part);
678 biofinish(bp, NULL, ENXIO);
683 mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
684 int fflag, struct thread *td __unused)
687 return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
691 mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
692 struct thread *td __unused)
695 return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
699 mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
701 struct mmc_ioc_cmd *mic;
702 struct mmc_ioc_multi_cmd *mimc;
706 if ((fflag & FREAD) == 0)
713 err = mmcsd_ioctl_cmd(part, data, fflag);
715 case MMC_IOC_CMD_MULTI:
717 if (mimc->num_of_cmds == 0)
719 if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
721 cnt = mimc->num_of_cmds;
722 size = sizeof(*mic) * cnt;
723 mic = malloc(size, M_TEMP, M_WAITOK);
724 err = copyin((const void *)mimc->cmds, mic, size);
727 for (i = 0; i < cnt; i++) {
728 err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
741 mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
743 struct mmc_command cmd;
744 struct mmc_data data;
745 struct mmcsd_softc *sc;
753 if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
756 if (part->ro == TRUE && mic->write_flag != 0)
761 len = mic->blksz * mic->blocks;
762 if (len > MMC_IOC_MAX_BYTES)
765 dp = malloc(len, M_TEMP, M_WAITOK);
766 err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
770 memset(&cmd, 0, sizeof(cmd));
771 memset(&data, 0, sizeof(data));
772 cmd.opcode = mic->opcode;
774 cmd.flags = mic->flags;
778 data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
784 if (mic->is_acmd == 0) {
785 /* Enforce/patch/restrict RCA-based commands */
786 switch (cmd.opcode) {
787 case MMC_SET_RELATIVE_ADDR:
788 case MMC_SELECT_CARD:
791 case MMC_STOP_TRANSMISSION:
792 if ((cmd.arg & 0x1) == 0)
795 case MMC_SLEEP_AWAKE:
798 case MMC_SEND_STATUS:
799 case MMC_GO_INACTIVE_STATE:
802 cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
810 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
811 err = mmcsd_switch_part(mmcbr, dev, rca, part->type);
812 if (err != MMC_ERR_NONE)
814 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
815 err = mmcsd_set_blockcount(sc, mic->blocks,
816 mic->write_flag & (1 << 31));
817 if (err != MMC_ERR_NONE)
820 if (mic->is_acmd != 0)
821 (void)mmc_wait_for_app_cmd(mmcbr, dev, rca, &cmd, 0);
823 (void)mmc_wait_for_cmd(mmcbr, dev, &cmd, 0);
824 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
826 * If the request went to the RPMB partition, try to ensure
827 * that the command actually has completed ...
829 retries = MMCSD_CMD_RETRIES;
831 err = mmc_send_status(mmcbr, dev, rca, &status);
832 if (err != MMC_ERR_NONE)
834 if (R1_STATUS(status) == 0 &&
835 R1_CURRENT_STATE(status) != R1_STATE_PRG)
838 } while (retries-- > 0);
840 /* ... and always switch back to the default partition. */
841 err = mmcsd_switch_part(mmcbr, dev, rca,
842 EXT_CSD_PART_CONFIG_ACC_DEFAULT);
843 if (err != MMC_ERR_NONE)
847 * If EXT_CSD was changed, our copy is outdated now. Specifically,
848 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
849 * so retrieve EXT_CSD again.
851 if (cmd.opcode == MMC_SWITCH_FUNC) {
852 err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
853 if (err != MMC_ERR_NONE)
856 MMCBUS_RELEASE_BUS(mmcbr, dev);
857 if (cmd.error != MMC_ERR_NONE) {
859 case MMC_ERR_TIMEOUT:
865 case MMC_ERR_INVALID:
868 case MMC_ERR_NO_MEMORY:
877 memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
878 if (mic->write_flag == 0 && len != 0) {
879 err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
886 MMCBUS_RELEASE_BUS(mmcbr, dev);
896 mmcsd_getattr(struct bio *bp)
898 struct mmcsd_part *part;
901 if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
902 if (bp->bio_length != sizeof(dev))
904 part = bp->bio_disk->d_drv1;
906 bcopy(&dev, bp->bio_data, sizeof(dev));
907 bp->bio_completed = bp->bio_length;
914 mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
916 struct mmc_command cmd;
917 struct mmc_request req;
919 memset(&req, 0, sizeof(req));
920 memset(&cmd, 0, sizeof(cmd));
923 cmd.opcode = MMC_SET_BLOCK_COUNT;
924 cmd.arg = count & 0x0000FFFF;
927 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
928 MMCBUS_WAIT_FOR_REQUEST(sc->mmcbr, sc->dev, &req);
933 mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
935 struct mmcsd_softc *sc;
939 sc = device_get_softc(dev);
941 if (sc->part_curr == part)
942 return (MMC_ERR_NONE);
944 if (sc->mode == mode_sd)
945 return (MMC_ERR_NONE);
947 value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
948 ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
950 err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
951 EXT_CSD_PART_CONFIG, value, sc->part_time, true);
952 if (err != MMC_ERR_NONE)
955 sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
956 sc->part_curr = part;
957 return (MMC_ERR_NONE);
964 if (e < 0 || e > MMC_ERR_MAX)
965 return "Bad error code";
970 mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
973 struct mmc_command cmd;
974 struct mmc_command stop;
975 struct mmc_request req;
976 struct mmc_data data;
977 struct mmcsd_softc *sc;
986 block = bp->bio_pblkno;
987 sz = part->disk->d_sectorsize;
988 end = bp->bio_pblkno + (bp->bio_bcount / sz);
989 while (block < end) {
990 vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
991 numblocks = min(end - block, mmc_get_max_data(dev));
992 memset(&req, 0, sizeof(req));
993 memset(&cmd, 0, sizeof(cmd));
994 memset(&stop, 0, sizeof(stop));
995 memset(&data, 0, sizeof(data));
999 if (bp->bio_cmd == BIO_READ) {
1001 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1003 cmd.opcode = MMC_READ_SINGLE_BLOCK;
1006 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1008 cmd.opcode = MMC_WRITE_BLOCK;
1011 if (!mmc_get_high_cap(dev))
1013 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1016 if (bp->bio_cmd == BIO_READ)
1017 data.flags = MMC_DATA_READ;
1019 data.flags = MMC_DATA_WRITE;
1020 data.len = numblocks * sz;
1021 if (numblocks > 1) {
1022 data.flags |= MMC_DATA_MULTI;
1023 stop.opcode = MMC_STOP_TRANSMISSION;
1025 stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1029 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1030 if (req.cmd->error != MMC_ERR_NONE) {
1031 if (ppsratecheck(&sc->log_time, &sc->log_count,
1033 device_printf(dev, "Error indicated: %d %s\n",
1035 mmcsd_errmsg(req.cmd->error));
1044 mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
1046 daddr_t block, end, start, stop;
1047 struct mmc_command cmd;
1048 struct mmc_request req;
1049 struct mmcsd_softc *sc;
1050 device_t dev, mmcbr;
1051 int erase_sector, sz;
1057 block = bp->bio_pblkno;
1058 sz = part->disk->d_sectorsize;
1059 end = bp->bio_pblkno + (bp->bio_bcount / sz);
1060 /* Coalesce with part remaining from previous request. */
1061 if (block > part->eblock && block <= part->eend)
1062 block = part->eblock;
1063 if (end >= part->eblock && end < part->eend)
1065 /* Safe round to the erase sector boundaries. */
1066 erase_sector = mmc_get_erase_sector(dev);
1067 start = block + erase_sector - 1; /* Round up. */
1068 start -= start % erase_sector;
1069 stop = end; /* Round down. */
1070 stop -= end % erase_sector;
1071 /* We can't erase an area smaller than a sector, store it for later. */
1072 if (start >= stop) {
1073 part->eblock = block;
1078 /* Set erase start position. */
1079 memset(&req, 0, sizeof(req));
1080 memset(&cmd, 0, sizeof(cmd));
1083 if (mmc_get_card_type(dev) == mode_sd)
1084 cmd.opcode = SD_ERASE_WR_BLK_START;
1086 cmd.opcode = MMC_ERASE_GROUP_START;
1088 if (!mmc_get_high_cap(dev))
1090 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1091 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1092 if (req.cmd->error != MMC_ERR_NONE) {
1093 printf("erase err1: %d\n", req.cmd->error);
1096 /* Set erase stop position. */
1097 memset(&req, 0, sizeof(req));
1098 memset(&cmd, 0, sizeof(cmd));
1100 if (mmc_get_card_type(dev) == mode_sd)
1101 cmd.opcode = SD_ERASE_WR_BLK_END;
1103 cmd.opcode = MMC_ERASE_GROUP_END;
1105 if (!mmc_get_high_cap(dev))
1108 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1109 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1110 if (req.cmd->error != MMC_ERR_NONE) {
1111 printf("erase err2: %d\n", req.cmd->error);
1115 memset(&req, 0, sizeof(req));
1116 memset(&cmd, 0, sizeof(cmd));
1118 cmd.opcode = MMC_ERASE;
1120 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1121 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1122 if (req.cmd->error != MMC_ERR_NONE) {
1123 printf("erase err3 %d\n", req.cmd->error);
1126 /* Store one of remaining parts for the next call. */
1127 if (bp->bio_pblkno >= part->eblock || block == start) {
1128 part->eblock = stop; /* Predict next forward. */
1131 part->eblock = block; /* Predict next backward. */
1138 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1144 struct mmcsd_softc *sc;
1145 struct mmcsd_part *part;
1146 device_t dev, mmcbr;
1149 /* length zero is special and really means flush buffers to media */
1154 part = disk->d_drv1;
1159 bzero(&bp, sizeof(struct bio));
1161 bp.bio_pblkno = offset / disk->d_sectorsize;
1162 bp.bio_bcount = length;
1163 bp.bio_data = virtual;
1164 bp.bio_cmd = BIO_WRITE;
1165 end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
1166 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1167 err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1168 if (err != MMC_ERR_NONE) {
1169 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
1170 device_printf(dev, "Partition switch error\n");
1171 MMCBUS_RELEASE_BUS(mmcbr, dev);
1174 block = mmcsd_rw(part, &bp);
1175 MMCBUS_RELEASE_BUS(mmcbr, dev);
1176 return ((end < block) ? EIO : 0);
1180 mmcsd_task(void *arg)
1183 struct mmcsd_part *part;
1184 struct mmcsd_softc *sc;
1186 device_t dev, mmcbr;
1195 MMCSD_PART_LOCK(part);
1197 if (part->running == 0)
1199 bp = bioq_takefirst(&part->bio_queue);
1201 msleep(part, &part->part_mtx, PRIBIO,
1203 } while (bp == NULL);
1204 MMCSD_PART_UNLOCK(part);
1205 if (bp->bio_cmd != BIO_READ && part->ro) {
1206 bp->bio_error = EROFS;
1207 bp->bio_resid = bp->bio_bcount;
1208 bp->bio_flags |= BIO_ERROR;
1212 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1213 sz = part->disk->d_sectorsize;
1214 block = bp->bio_pblkno;
1215 end = bp->bio_pblkno + (bp->bio_bcount / sz);
1216 err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1217 if (err != MMC_ERR_NONE) {
1218 if (ppsratecheck(&sc->log_time, &sc->log_count,
1220 device_printf(dev, "Partition switch error\n");
1223 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1224 /* Access to the remaining erase block obsoletes it. */
1225 if (block < part->eend && end > part->eblock)
1226 part->eblock = part->eend = 0;
1227 block = mmcsd_rw(part, bp);
1228 } else if (bp->bio_cmd == BIO_DELETE) {
1229 block = mmcsd_delete(part, bp);
1232 MMCBUS_RELEASE_BUS(mmcbr, dev);
1234 bp->bio_error = EIO;
1235 bp->bio_resid = (end - block) * sz;
1236 bp->bio_flags |= BIO_ERROR;
1243 /* tell parent we're done */
1245 MMCSD_PART_UNLOCK(part);
1252 mmcsd_bus_bit_width(device_t dev)
1255 if (mmc_get_bus_width(dev) == bus_width_1)
1257 if (mmc_get_bus_width(dev) == bus_width_4)
1262 static device_method_t mmcsd_methods[] = {
1263 DEVMETHOD(device_probe, mmcsd_probe),
1264 DEVMETHOD(device_attach, mmcsd_attach),
1265 DEVMETHOD(device_detach, mmcsd_detach),
1266 DEVMETHOD(device_suspend, mmcsd_suspend),
1267 DEVMETHOD(device_resume, mmcsd_resume),
1271 static driver_t mmcsd_driver = {
1274 sizeof(struct mmcsd_softc),
1276 static devclass_t mmcsd_devclass;
1279 mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
1284 flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
1288 flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
1294 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
1295 MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);