1114b4164SWarner Losh /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 4114b4164SWarner Losh * Copyright (c) 2006 Bernd Walter. All rights reserved. 5114b4164SWarner Losh * Copyright (c) 2006 M. Warner Losh. All rights reserved. 672dec079SMarius Strobl * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 7114b4164SWarner Losh * 8114b4164SWarner Losh * Redistribution and use in source and binary forms, with or without 9114b4164SWarner Losh * modification, are permitted provided that the following conditions 10114b4164SWarner Losh * are met: 11114b4164SWarner Losh * 1. Redistributions of source code must retain the above copyright 12114b4164SWarner Losh * notice, this list of conditions and the following disclaimer. 13114b4164SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 14114b4164SWarner Losh * notice, this list of conditions and the following disclaimer in the 15114b4164SWarner Losh * documentation and/or other materials provided with the distribution. 16114b4164SWarner Losh * 17114b4164SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18114b4164SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19114b4164SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20114b4164SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21114b4164SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22114b4164SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23114b4164SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24114b4164SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25114b4164SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26114b4164SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2714eced72SWarner Losh * 2814eced72SWarner Losh * Portions of this software may have been developed with reference to 2914eced72SWarner Losh * the SD Simplified Specification. The following disclaimer may apply: 3014eced72SWarner Losh * 3114eced72SWarner Losh * The following conditions apply to the release of the simplified 3214eced72SWarner Losh * specification ("Simplified Specification") by the SD Card Association and 3314eced72SWarner Losh * the SD Group. The Simplified Specification is a subset of the complete SD 3414eced72SWarner Losh * Specification which is owned by the SD Card Association and the SD 3514eced72SWarner Losh * Group. This Simplified Specification is provided on a non-confidential 3614eced72SWarner Losh * basis subject to the disclaimers below. Any implementation of the 3714eced72SWarner Losh * Simplified Specification may require a license from the SD Card 3814eced72SWarner Losh * Association, SD Group, SD-3C LLC or other third parties. 3914eced72SWarner Losh * 4014eced72SWarner Losh * Disclaimers: 4114eced72SWarner Losh * 4214eced72SWarner Losh * The information contained in the Simplified Specification is presented only 4314eced72SWarner Losh * as a standard specification for SD Cards and SD Host/Ancillary products and 4414eced72SWarner Losh * is provided "AS-IS" without any representations or warranties of any 4514eced72SWarner Losh * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 4614eced72SWarner Losh * Card Association for any damages, any infringements of patents or other 4714eced72SWarner Losh * right of the SD Group, SD-3C LLC, the SD Card Association or any third 4814eced72SWarner Losh * parties, which may result from its use. No license is granted by 4914eced72SWarner Losh * implication, estoppel or otherwise under any patent or other rights of the 5014eced72SWarner Losh * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 5114eced72SWarner Losh * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 5214eced72SWarner Losh * or the SD Card Association to disclose or distribute any technical 5314eced72SWarner Losh * information, know-how or other confidential information to any third party. 54114b4164SWarner Losh */ 55114b4164SWarner Losh 56114b4164SWarner Losh #include <sys/cdefs.h> 57114b4164SWarner Losh __FBSDID("$FreeBSD$"); 58114b4164SWarner Losh 59114b4164SWarner Losh #include <sys/param.h> 60114b4164SWarner Losh #include <sys/systm.h> 61114b4164SWarner Losh #include <sys/bio.h> 62114b4164SWarner Losh #include <sys/bus.h> 63114b4164SWarner Losh #include <sys/conf.h> 6472dec079SMarius Strobl #include <sys/fcntl.h> 6572dec079SMarius Strobl #include <sys/ioccom.h> 66114b4164SWarner Losh #include <sys/kernel.h> 67114b4164SWarner Losh #include <sys/kthread.h> 68114b4164SWarner Losh #include <sys/lock.h> 69114b4164SWarner Losh #include <sys/malloc.h> 70114b4164SWarner Losh #include <sys/module.h> 71114b4164SWarner Losh #include <sys/mutex.h> 7272dec079SMarius Strobl #include <sys/slicer.h> 735e3dbf8bSIan Lepore #include <sys/time.h> 7472dec079SMarius Strobl 75c55f5707SWarner Losh #include <geom/geom.h> 76114b4164SWarner Losh #include <geom/geom_disk.h> 77114b4164SWarner Losh 7872dec079SMarius Strobl #include <dev/mmc/bridge.h> 7972dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h> 8072dec079SMarius Strobl #include <dev/mmc/mmc_subr.h> 817aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h> 82114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 837aa65846SMarius Strobl #include <dev/mmc/mmcvar.h> 84114b4164SWarner Losh 85114b4164SWarner Losh #include "mmcbus_if.h" 86114b4164SWarner Losh 877aa65846SMarius Strobl #if __FreeBSD_version < 800002 887aa65846SMarius Strobl #define kproc_create kthread_create 897aa65846SMarius Strobl #define kproc_exit kthread_exit 907aa65846SMarius Strobl #endif 917aa65846SMarius Strobl 9272dec079SMarius Strobl #define MMCSD_CMD_RETRIES 5 9372dec079SMarius Strobl 9472dec079SMarius Strobl #define MMCSD_FMT_BOOT "mmcsd%dboot" 9572dec079SMarius Strobl #define MMCSD_FMT_GP "mmcsd%dgp" 9672dec079SMarius Strobl #define MMCSD_FMT_RPMB "mmcsd%drpmb" 9772dec079SMarius Strobl #define MMCSD_LABEL_ENH "enh" 9872dec079SMarius Strobl 9972dec079SMarius Strobl #define MMCSD_PART_NAMELEN (16 + 1) 10072dec079SMarius Strobl 10172dec079SMarius Strobl struct mmcsd_softc; 10272dec079SMarius Strobl 10372dec079SMarius Strobl struct mmcsd_part { 104aca38eabSMarius Strobl struct mtx disk_mtx; 105aca38eabSMarius Strobl struct mtx ioctl_mtx; 10672dec079SMarius Strobl struct mmcsd_softc *sc; 107114b4164SWarner Losh struct disk *disk; 108114b4164SWarner Losh struct proc *p; 109114b4164SWarner Losh struct bio_queue_head bio_queue; 110a7cf6274SAlexander Motin daddr_t eblock, eend; /* Range remaining after the last erase. */ 11172dec079SMarius Strobl u_int cnt; 11272dec079SMarius Strobl u_int type; 1130e1466acSWarner Losh int running; 114eb67f31aSAlexander Motin int suspend; 115aca38eabSMarius Strobl int ioctl; 11672dec079SMarius Strobl bool ro; 11772dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 11872dec079SMarius Strobl }; 11972dec079SMarius Strobl 12072dec079SMarius Strobl struct mmcsd_softc { 12172dec079SMarius Strobl device_t dev; 122cd85acbaSMarius Strobl device_t mmcbus; 12372dec079SMarius Strobl struct mmcsd_part *part[MMC_PART_MAX]; 12472dec079SMarius Strobl enum mmc_card_mode mode; 125aca38eabSMarius Strobl u_int max_data; /* Maximum data size [blocks] */ 126aca38eabSMarius Strobl u_int erase_sector; /* Device native erase sector size [blocks] */ 127aca38eabSMarius Strobl uint8_t high_cap; /* High Capacity device (block addressed) */ 12872dec079SMarius Strobl uint8_t part_curr; /* Partition currently switched to */ 12972dec079SMarius Strobl uint8_t ext_csd[MMC_EXTCSD_SIZE]; 13072dec079SMarius Strobl uint16_t rca; 13179f39c6aSMarius Strobl uint32_t flags; 13279f39c6aSMarius Strobl #define MMCSD_INAND_CMD38 0x0001 13379f39c6aSMarius Strobl #define MMCSD_USE_TRIM 0x0002 13479f39c6aSMarius Strobl uint32_t cmd6_time; /* Generic switch timeout [us] */ 13572dec079SMarius Strobl uint32_t part_time; /* Partition switch timeout [us] */ 13672dec079SMarius Strobl off_t enh_base; /* Enhanced user data area slice base ... */ 13772dec079SMarius Strobl off_t enh_size; /* ... and size [bytes] */ 1385e3dbf8bSIan Lepore int log_count; 1395e3dbf8bSIan Lepore struct timeval log_time; 14072dec079SMarius Strobl struct cdev *rpmb_dev; 141114b4164SWarner Losh }; 142114b4164SWarner Losh 143bb1ef63fSWarner Losh static const char *errmsg[] = 144bb1ef63fSWarner Losh { 145bb1ef63fSWarner Losh "None", 146bb1ef63fSWarner Losh "Timeout", 147bb1ef63fSWarner Losh "Bad CRC", 148bb1ef63fSWarner Losh "Fifo", 149bb1ef63fSWarner Losh "Failed", 150bb1ef63fSWarner Losh "Invalid", 151bb1ef63fSWarner Losh "NO MEMORY" 152bb1ef63fSWarner Losh }; 153bb1ef63fSWarner Losh 1545e3dbf8bSIan Lepore #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 1555e3dbf8bSIan Lepore 156114b4164SWarner Losh /* bus entry points */ 157114b4164SWarner Losh static int mmcsd_attach(device_t dev); 158114b4164SWarner Losh static int mmcsd_detach(device_t dev); 159ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev); 160114b4164SWarner Losh 161114b4164SWarner Losh /* disk routines */ 162114b4164SWarner Losh static int mmcsd_close(struct disk *dp); 163a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 164a1fda318SAlexander Motin off_t offset, size_t length); 16572dec079SMarius Strobl static int mmcsd_getattr(struct bio *); 16672dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, 16772dec079SMarius Strobl int fflag, struct thread *td); 168ae5d8757SMarius Strobl static int mmcsd_open(struct disk *dp); 169ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp); 170114b4164SWarner Losh static void mmcsd_task(void *arg); 171114b4164SWarner Losh 17272dec079SMarius Strobl /* RMPB cdev interface */ 17372dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 17472dec079SMarius Strobl int fflag, struct thread *td); 175a0075e58SWarner Losh 17672dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type, 17779f39c6aSMarius Strobl const char *name, u_int cnt, off_t media_size, bool ro); 17872dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev); 17972dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp); 18079f39c6aSMarius Strobl static const char *mmcsd_errmsg(int e); 18172dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, 18272dec079SMarius Strobl int fflag); 18372dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, 18472dec079SMarius Strobl int fflag); 18572dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit); 18672dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp); 18772dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel); 18872dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider, 18972dec079SMarius Strobl struct flash_slice *slices, int *nslices); 19072dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, 19172dec079SMarius Strobl u_int part); 19272dec079SMarius Strobl 193aca38eabSMarius Strobl #define MMCSD_DISK_LOCK(_part) mtx_lock(&(_part)->disk_mtx) 194aca38eabSMarius Strobl #define MMCSD_DISK_UNLOCK(_part) mtx_unlock(&(_part)->disk_mtx) 195aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_INIT(_part) \ 196aca38eabSMarius Strobl mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF) 197aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_DESTROY(_part) mtx_destroy(&(_part)->disk_mtx); 198aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_LOCKED(_part) \ 199aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_OWNED); 200aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_UNLOCKED(_part) \ 201aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED); 202aca38eabSMarius Strobl 203aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK(_part) mtx_lock(&(_part)->ioctl_mtx) 204aca38eabSMarius Strobl #define MMCSD_IOCTL_UNLOCK(_part) mtx_unlock(&(_part)->ioctl_mtx) 205aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_INIT(_part) \ 206aca38eabSMarius Strobl mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF) 207aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_DESTROY(_part) mtx_destroy(&(_part)->ioctl_mtx); 208aca38eabSMarius Strobl #define MMCSD_IOCTL_ASSERT_LOCKED(_part) \ 209aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_OWNED); 210aca38eabSMarius Strobl #define MMCSD_IOCLT_ASSERT_UNLOCKED(_part) \ 211aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED); 212114b4164SWarner Losh 213114b4164SWarner Losh static int 214114b4164SWarner Losh mmcsd_probe(device_t dev) 215114b4164SWarner Losh { 216114b4164SWarner Losh 217c18f1e26SAlexander Motin device_quiet(dev); 2180c0903b1SWarner Losh device_set_desc(dev, "MMC/SD Memory Card"); 219114b4164SWarner Losh return (0); 220114b4164SWarner Losh } 221114b4164SWarner Losh 222114b4164SWarner Losh static int 223114b4164SWarner Losh mmcsd_attach(device_t dev) 224114b4164SWarner Losh { 225cd85acbaSMarius Strobl device_t mmcbus; 226114b4164SWarner Losh struct mmcsd_softc *sc; 22772dec079SMarius Strobl const uint8_t *ext_csd; 22872dec079SMarius Strobl off_t erase_size, sector_size, size, wp_size; 22972dec079SMarius Strobl uintmax_t bytes; 23072dec079SMarius Strobl int err, i; 23179f39c6aSMarius Strobl uint32_t quirks; 23272dec079SMarius Strobl uint8_t rev; 23372dec079SMarius Strobl bool comp, ro; 23472dec079SMarius Strobl char unit[2]; 235114b4164SWarner Losh 236114b4164SWarner Losh sc = device_get_softc(dev); 237114b4164SWarner Losh sc->dev = dev; 238cd85acbaSMarius Strobl sc->mmcbus = mmcbus = device_get_parent(dev); 239cd85acbaSMarius Strobl sc->mode = mmcbr_get_mode(mmcbus); 240aca38eabSMarius Strobl /* 241aca38eabSMarius Strobl * Note that in principle with an SDHCI-like re-tuning implementation, 242aca38eabSMarius Strobl * the maximum data size can change at runtime due to a device removal/ 243aca38eabSMarius Strobl * insertion that results in switches to/from a transfer mode involving 244aca38eabSMarius Strobl * re-tuning, iff there are multiple devices on a given bus. Until now 245aca38eabSMarius Strobl * mmc(4) lacks support for rescanning already attached buses, however, 24621794534SMarius Strobl * and sdhci(4) to date has no support for shared buses in the first 247aca38eabSMarius Strobl * place either. 248aca38eabSMarius Strobl */ 249aca38eabSMarius Strobl sc->max_data = mmc_get_max_data(dev); 250aca38eabSMarius Strobl sc->high_cap = mmc_get_high_cap(dev); 25172dec079SMarius Strobl sc->rca = mmc_get_rca(dev); 25279f39c6aSMarius Strobl sc->cmd6_time = mmc_get_cmd6_timeout(dev); 25379f39c6aSMarius Strobl quirks = mmc_get_quirks(dev); 254114b4164SWarner Losh 25572dec079SMarius Strobl /* Only MMC >= 4.x devices support EXT_CSD. */ 25672dec079SMarius Strobl if (mmc_get_spec_vers(dev) >= 4) { 257cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 258cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 259cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 26079f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 26179f39c6aSMarius Strobl device_printf(dev, "Error reading EXT_CSD %s\n", 26279f39c6aSMarius Strobl mmcsd_errmsg(err)); 26379f39c6aSMarius Strobl return (ENXIO); 26479f39c6aSMarius Strobl } 26572dec079SMarius Strobl } 26672dec079SMarius Strobl ext_csd = sc->ext_csd; 26772dec079SMarius Strobl 26879f39c6aSMarius Strobl if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) { 26979f39c6aSMarius Strobl if (mmc_get_spec_vers(dev) < 4) { 27079f39c6aSMarius Strobl device_printf(dev, 27179f39c6aSMarius Strobl "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n"); 27279f39c6aSMarius Strobl return (EINVAL); 27379f39c6aSMarius Strobl } 27479f39c6aSMarius Strobl sc->flags |= MMCSD_INAND_CMD38; 27579f39c6aSMarius Strobl } 27679f39c6aSMarius Strobl 27779f39c6aSMarius Strobl /* 27879f39c6aSMarius Strobl * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both 27979f39c6aSMarius Strobl * insecure and secure TRIM. 28079f39c6aSMarius Strobl */ 28179f39c6aSMarius Strobl if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] & 28279f39c6aSMarius Strobl EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 && 28379f39c6aSMarius Strobl (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) { 28479f39c6aSMarius Strobl if (bootverbose) 28579f39c6aSMarius Strobl device_printf(dev, "taking advantage of TRIM\n"); 28679f39c6aSMarius Strobl sc->flags |= MMCSD_USE_TRIM; 28779f39c6aSMarius Strobl sc->erase_sector = 1; 28879f39c6aSMarius Strobl } else 28979f39c6aSMarius Strobl sc->erase_sector = mmc_get_erase_sector(dev); 29079f39c6aSMarius Strobl 29172dec079SMarius Strobl /* 29272dec079SMarius Strobl * Enhanced user data area and general purpose partitions are only 29372dec079SMarius Strobl * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB 29472dec079SMarius Strobl * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later. 29572dec079SMarius Strobl */ 29672dec079SMarius Strobl rev = ext_csd[EXT_CSD_REV]; 29772dec079SMarius Strobl 29872dec079SMarius Strobl /* 29972dec079SMarius Strobl * Ignore user-creatable enhanced user data area and general purpose 30072dec079SMarius Strobl * partitions partitions as long as partitioning hasn't been finished. 30172dec079SMarius Strobl */ 30272dec079SMarius Strobl comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0; 30372dec079SMarius Strobl 30472dec079SMarius Strobl /* 30572dec079SMarius Strobl * Add enhanced user data area slice, unless it spans the entirety of 30672dec079SMarius Strobl * the user data area. The enhanced area is of a multiple of high 30772dec079SMarius Strobl * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) * 30872dec079SMarius Strobl * 512 KB) and its offset given in either sectors or bytes, depending 30972dec079SMarius Strobl * on whether it's a high capacity device or not. 31072dec079SMarius Strobl * NB: The slicer and its slices need to be registered before adding 31172dec079SMarius Strobl * the disk for the corresponding user data area as re-tasting is 31272dec079SMarius Strobl * racy. 31372dec079SMarius Strobl */ 31472dec079SMarius Strobl sector_size = mmc_get_sector_size(dev); 31572dec079SMarius Strobl size = ext_csd[EXT_CSD_ENH_SIZE_MULT] + 31672dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + 31772dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16); 31872dec079SMarius Strobl if (rev >= 4 && comp == TRUE && size > 0 && 31972dec079SMarius Strobl (ext_csd[EXT_CSD_PART_SUPPORT] & 32072dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 32172dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) { 32272dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 32372dec079SMarius Strobl MMC_SECTOR_SIZE; 32472dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 32572dec079SMarius Strobl size *= erase_size * wp_size; 32672dec079SMarius Strobl if (size != mmc_get_media_size(dev) * sector_size) { 32772dec079SMarius Strobl sc->enh_size = size; 32872dec079SMarius Strobl sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] + 32972dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) + 33072dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) + 33172dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) * 332da979d44SMarius Strobl (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1); 33372dec079SMarius Strobl } else if (bootverbose) 33472dec079SMarius Strobl device_printf(dev, 33572dec079SMarius Strobl "enhanced user data area spans entire device\n"); 33672dec079SMarius Strobl } 33772dec079SMarius Strobl 33872dec079SMarius Strobl /* 33972dec079SMarius Strobl * Add default partition. This may be the only one or the user 34072dec079SMarius Strobl * data area in case partitions are supported. 34172dec079SMarius Strobl */ 34272dec079SMarius Strobl ro = mmc_get_read_only(dev); 34372dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd", 34479f39c6aSMarius Strobl device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro); 34572dec079SMarius Strobl 34672dec079SMarius Strobl if (mmc_get_spec_vers(dev) < 3) 34772dec079SMarius Strobl return (0); 34872dec079SMarius Strobl 34972dec079SMarius Strobl /* Belatedly announce enhanced user data slice. */ 35072dec079SMarius Strobl if (sc->enh_size != 0) { 35172dec079SMarius Strobl bytes = mmcsd_pretty_size(size, unit); 35272dec079SMarius Strobl printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area " 35372dec079SMarius Strobl "slice offset 0x%jx at %s\n", device_get_nameunit(dev), 35472dec079SMarius Strobl MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base, 35572dec079SMarius Strobl device_get_nameunit(dev)); 35672dec079SMarius Strobl } 35772dec079SMarius Strobl 35872dec079SMarius Strobl /* 35972dec079SMarius Strobl * Determine partition switch timeout (provided in units of 10 ms) 36072dec079SMarius Strobl * and ensure it's at least 300 ms as some eMMC chips lie. 36172dec079SMarius Strobl */ 36272dec079SMarius Strobl sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000, 36372dec079SMarius Strobl 300 * 1000); 36472dec079SMarius Strobl 36572dec079SMarius Strobl /* Add boot partitions, which are of a fixed multiple of 128 KB. */ 36672dec079SMarius Strobl size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 367cd85acbaSMarius Strobl if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) { 36872dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0, 36979f39c6aSMarius Strobl MMCSD_FMT_BOOT, 0, size, 37072dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 37172dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0)); 37272dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1, 37379f39c6aSMarius Strobl MMCSD_FMT_BOOT, 1, size, 37472dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 37572dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0)); 37672dec079SMarius Strobl } 37772dec079SMarius Strobl 37872dec079SMarius Strobl /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */ 37972dec079SMarius Strobl size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 38072dec079SMarius Strobl if (rev >= 5 && size > 0) 38172dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB, 38279f39c6aSMarius Strobl MMCSD_FMT_RPMB, 0, size, ro); 38372dec079SMarius Strobl 38472dec079SMarius Strobl if (rev <= 3 || comp == FALSE) 38572dec079SMarius Strobl return (0); 38672dec079SMarius Strobl 38772dec079SMarius Strobl /* 38872dec079SMarius Strobl * Add general purpose partitions, which are of a multiple of high 38972dec079SMarius Strobl * capacity write protect groups, too. 39072dec079SMarius Strobl */ 39172dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) { 39272dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 39372dec079SMarius Strobl MMC_SECTOR_SIZE; 39472dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 39572dec079SMarius Strobl for (i = 0; i < MMC_PART_GP_MAX; i++) { 39672dec079SMarius Strobl size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] + 39772dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) + 39872dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16); 39972dec079SMarius Strobl if (size == 0) 40072dec079SMarius Strobl continue; 40172dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i, 40279f39c6aSMarius Strobl MMCSD_FMT_GP, i, size * erase_size * wp_size, ro); 40372dec079SMarius Strobl } 40472dec079SMarius Strobl } 40572dec079SMarius Strobl return (0); 40672dec079SMarius Strobl } 40772dec079SMarius Strobl 40872dec079SMarius Strobl static uintmax_t 40972dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit) 41072dec079SMarius Strobl { 41172dec079SMarius Strobl uintmax_t bytes; 41272dec079SMarius Strobl int i; 41372dec079SMarius Strobl 41472dec079SMarius Strobl /* 41572dec079SMarius Strobl * Display in most natural units. There's no card < 1MB. However, 41672dec079SMarius Strobl * RPMB partitions occasionally are smaller than that, though. The 41772dec079SMarius Strobl * SD standard goes to 2 GiB due to its reliance on FAT, but the data 41872dec079SMarius Strobl * format supports up to 4 GiB and some card makers push it up to this 41972dec079SMarius Strobl * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the 42072dec079SMarius Strobl * data format supports up to 2 TiB however. 2048 GB isn't too ugly, 42172dec079SMarius Strobl * so we note it in passing here and don't add the code to print TB). 42272dec079SMarius Strobl * Since these cards are sold in terms of MB and GB not MiB and GiB, 42372dec079SMarius Strobl * report them like that. We also round to the nearest unit, since 42472dec079SMarius Strobl * many cards are a few percent short, even of the power of 10 size. 42572dec079SMarius Strobl */ 42672dec079SMarius Strobl bytes = size; 42772dec079SMarius Strobl unit[0] = unit[1] = '\0'; 42872dec079SMarius Strobl for (i = 0; i <= 2 && bytes >= 1000; i++) { 42972dec079SMarius Strobl bytes = (bytes + 1000 / 2 - 1) / 1000; 43072dec079SMarius Strobl switch (i) { 43172dec079SMarius Strobl case 0: 43272dec079SMarius Strobl unit[0] = 'k'; 43372dec079SMarius Strobl break; 43472dec079SMarius Strobl case 1: 43572dec079SMarius Strobl unit[0] = 'M'; 43672dec079SMarius Strobl break; 43772dec079SMarius Strobl case 2: 43872dec079SMarius Strobl unit[0] = 'G'; 43972dec079SMarius Strobl break; 44072dec079SMarius Strobl default: 44172dec079SMarius Strobl break; 44272dec079SMarius Strobl } 44372dec079SMarius Strobl } 44472dec079SMarius Strobl return (bytes); 44572dec079SMarius Strobl } 44672dec079SMarius Strobl 44772dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = { 44872dec079SMarius Strobl .d_version = D_VERSION, 44972dec079SMarius Strobl .d_name = "mmcsdrpmb", 45072dec079SMarius Strobl .d_ioctl = mmcsd_ioctl_rpmb 45172dec079SMarius Strobl }; 45272dec079SMarius Strobl 45372dec079SMarius Strobl static void 45472dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt, 45579f39c6aSMarius Strobl off_t media_size, bool ro) 45672dec079SMarius Strobl { 45772dec079SMarius Strobl struct make_dev_args args; 458cd85acbaSMarius Strobl device_t dev, mmcbus; 45972dec079SMarius Strobl const char *ext; 46072dec079SMarius Strobl const uint8_t *ext_csd; 46172dec079SMarius Strobl struct mmcsd_part *part; 46272dec079SMarius Strobl struct disk *d; 46372dec079SMarius Strobl uintmax_t bytes; 46472dec079SMarius Strobl u_int gp; 46572dec079SMarius Strobl uint32_t speed; 46672dec079SMarius Strobl uint8_t extattr; 46772dec079SMarius Strobl bool enh; 46872dec079SMarius Strobl char unit[2]; 46972dec079SMarius Strobl 47072dec079SMarius Strobl dev = sc->dev; 471cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 47272dec079SMarius Strobl part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF, 47372dec079SMarius Strobl M_WAITOK | M_ZERO); 47472dec079SMarius Strobl part->sc = sc; 47572dec079SMarius Strobl part->cnt = cnt; 47672dec079SMarius Strobl part->type = type; 47772dec079SMarius Strobl part->ro = ro; 47872dec079SMarius Strobl snprintf(part->name, sizeof(part->name), name, device_get_unit(dev)); 47972dec079SMarius Strobl 480aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_INIT(part); 481aca38eabSMarius Strobl 482aca38eabSMarius Strobl /* 483aca38eabSMarius Strobl * For the RPMB partition, allow IOCTL access only. 484aca38eabSMarius Strobl * NB: If ever attaching RPMB partitions to disk(9), the re-tuning 485aca38eabSMarius Strobl * implementation and especially its pausing need to be revisited, 486aca38eabSMarius Strobl * because then re-tuning requests may be issued by the IOCTL half 487aca38eabSMarius Strobl * of this driver while re-tuning is already paused by the disk(9) 488aca38eabSMarius Strobl * one and vice versa. 489aca38eabSMarius Strobl */ 49072dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 49172dec079SMarius Strobl make_dev_args_init(&args); 49272dec079SMarius Strobl args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 49372dec079SMarius Strobl args.mda_devsw = &mmcsd_rpmb_cdevsw; 49472dec079SMarius Strobl args.mda_uid = UID_ROOT; 49572dec079SMarius Strobl args.mda_gid = GID_OPERATOR; 49672dec079SMarius Strobl args.mda_mode = 0640; 49772dec079SMarius Strobl args.mda_si_drv1 = part; 49872dec079SMarius Strobl if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) { 49972dec079SMarius Strobl device_printf(dev, "Failed to make RPMB device\n"); 50072dec079SMarius Strobl free(part, M_DEVBUF); 50172dec079SMarius Strobl return; 50272dec079SMarius Strobl } 50372dec079SMarius Strobl } else { 504aca38eabSMarius Strobl MMCSD_DISK_LOCK_INIT(part); 50572dec079SMarius Strobl 50672dec079SMarius Strobl d = part->disk = disk_alloc(); 507a0075e58SWarner Losh d->d_open = mmcsd_open; 508a0075e58SWarner Losh d->d_close = mmcsd_close; 509a0075e58SWarner Losh d->d_strategy = mmcsd_strategy; 51072dec079SMarius Strobl d->d_ioctl = mmcsd_ioctl_disk; 511a1fda318SAlexander Motin d->d_dump = mmcsd_dump; 51272dec079SMarius Strobl d->d_getattr = mmcsd_getattr; 51372dec079SMarius Strobl d->d_name = part->name; 51472dec079SMarius Strobl d->d_drv1 = part; 515a0075e58SWarner Losh d->d_sectorsize = mmc_get_sector_size(dev); 516aca38eabSMarius Strobl d->d_maxsize = sc->max_data * d->d_sectorsize; 51772dec079SMarius Strobl d->d_mediasize = media_size; 51879f39c6aSMarius Strobl d->d_stripesize = sc->erase_sector * d->d_sectorsize; 51972dec079SMarius Strobl d->d_unit = cnt; 5203906d42dSAlexander Motin d->d_flags = DISKFLAG_CANDELETE; 52179f39c6aSMarius Strobl d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize; 52272dec079SMarius Strobl strlcpy(d->d_ident, mmc_get_card_sn_string(dev), 52372dec079SMarius Strobl sizeof(d->d_ident)); 52472dec079SMarius Strobl strlcpy(d->d_descr, mmc_get_card_id_string(dev), 52572dec079SMarius Strobl sizeof(d->d_descr)); 52617160457SAlexander Motin d->d_rotation_rate = DISK_RR_NON_ROTATING; 527f3a4b7f7SIan Lepore 528a0075e58SWarner Losh disk_create(d, DISK_VERSION); 52972dec079SMarius Strobl bioq_init(&part->bio_queue); 5300e1466acSWarner Losh 53172dec079SMarius Strobl part->running = 1; 53272dec079SMarius Strobl kproc_create(&mmcsd_task, part, &part->p, 0, 0, 53372dec079SMarius Strobl "%s%d: mmc/sd card", part->name, cnt); 53472dec079SMarius Strobl } 53572dec079SMarius Strobl 53672dec079SMarius Strobl bytes = mmcsd_pretty_size(media_size, unit); 53772dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) { 538cd85acbaSMarius Strobl speed = mmcbr_get_clock(mmcbus); 53972dec079SMarius Strobl printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 54072dec079SMarius Strobl part->name, cnt, bytes, unit, mmc_get_card_id_string(dev), 541cd85acbaSMarius Strobl ro ? " (read-only)" : "", device_get_nameunit(mmcbus), 54272dec079SMarius Strobl speed / 1000000, (speed / 100000) % 10, 543aca38eabSMarius Strobl mmcsd_bus_bit_width(dev), sc->max_data); 54472dec079SMarius Strobl } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 54572dec079SMarius Strobl printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes, 54672dec079SMarius Strobl unit, type, ro ? " (read-only)" : "", 54788e07d92SRui Paulo device_get_nameunit(dev)); 54872dec079SMarius Strobl } else { 54972dec079SMarius Strobl enh = false; 55072dec079SMarius Strobl ext = NULL; 55172dec079SMarius Strobl extattr = 0; 55272dec079SMarius Strobl if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 && 55372dec079SMarius Strobl type <= EXT_CSD_PART_CONFIG_ACC_GP3) { 55472dec079SMarius Strobl ext_csd = sc->ext_csd; 55572dec079SMarius Strobl gp = type - EXT_CSD_PART_CONFIG_ACC_GP0; 55672dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & 55772dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 55872dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & 55972dec079SMarius Strobl (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0) 56072dec079SMarius Strobl enh = true; 56172dec079SMarius Strobl else if ((ext_csd[EXT_CSD_PART_SUPPORT] & 56272dec079SMarius Strobl EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) { 56372dec079SMarius Strobl extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR + 56472dec079SMarius Strobl (gp / 2)] >> (4 * (gp % 2))) & 0xF; 56572dec079SMarius Strobl switch (extattr) { 56672dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_DEFAULT: 56772dec079SMarius Strobl break; 56872dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE: 56972dec079SMarius Strobl ext = "system code"; 57072dec079SMarius Strobl break; 57172dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_NPERSISTENT: 57272dec079SMarius Strobl ext = "non-persistent"; 57372dec079SMarius Strobl break; 57472dec079SMarius Strobl default: 57572dec079SMarius Strobl ext = "reserved"; 57672dec079SMarius Strobl break; 57772dec079SMarius Strobl } 57872dec079SMarius Strobl } 57972dec079SMarius Strobl } 58072dec079SMarius Strobl if (ext == NULL) 58172dec079SMarius Strobl printf("%s%d: %ju%sB partion %d%s%s at %s\n", 58272dec079SMarius Strobl part->name, cnt, bytes, unit, type, enh ? 58372dec079SMarius Strobl " enhanced" : "", ro ? " (read-only)" : "", 58472dec079SMarius Strobl device_get_nameunit(dev)); 58572dec079SMarius Strobl else 58672dec079SMarius Strobl printf("%s%d: %ju%sB partion %d extended 0x%x " 58772dec079SMarius Strobl "(%s)%s at %s\n", part->name, cnt, bytes, unit, 58872dec079SMarius Strobl type, extattr, ext, ro ? " (read-only)" : "", 58972dec079SMarius Strobl device_get_nameunit(dev)); 59072dec079SMarius Strobl } 59172dec079SMarius Strobl } 592114b4164SWarner Losh 59372dec079SMarius Strobl static int 59472dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider, 59572dec079SMarius Strobl struct flash_slice *slices, int *nslices) 59672dec079SMarius Strobl { 59772dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 59872dec079SMarius Strobl struct mmcsd_softc *sc; 59972dec079SMarius Strobl struct mmcsd_part *part; 60072dec079SMarius Strobl 60172dec079SMarius Strobl *nslices = 0; 60272dec079SMarius Strobl if (slices == NULL) 60372dec079SMarius Strobl return (ENOMEM); 60472dec079SMarius Strobl 60572dec079SMarius Strobl sc = device_get_softc(dev); 60672dec079SMarius Strobl if (sc->enh_size == 0) 60772dec079SMarius Strobl return (ENXIO); 60872dec079SMarius Strobl 60972dec079SMarius Strobl part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT]; 61072dec079SMarius Strobl snprintf(name, sizeof(name), "%s%d", part->disk->d_name, 61172dec079SMarius Strobl part->disk->d_unit); 61272dec079SMarius Strobl if (strcmp(name, provider) != 0) 61372dec079SMarius Strobl return (ENXIO); 61472dec079SMarius Strobl 61572dec079SMarius Strobl *nslices = 1; 61672dec079SMarius Strobl slices[0].base = sc->enh_base; 61772dec079SMarius Strobl slices[0].size = sc->enh_size; 61872dec079SMarius Strobl slices[0].label = MMCSD_LABEL_ENH; 619114b4164SWarner Losh return (0); 620114b4164SWarner Losh } 621114b4164SWarner Losh 622114b4164SWarner Losh static int 623114b4164SWarner Losh mmcsd_detach(device_t dev) 624114b4164SWarner Losh { 6250e1466acSWarner Losh struct mmcsd_softc *sc = device_get_softc(dev); 62672dec079SMarius Strobl struct mmcsd_part *part; 62772dec079SMarius Strobl int i; 6280e1466acSWarner Losh 62972dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 63072dec079SMarius Strobl part = sc->part[i]; 631aca38eabSMarius Strobl if (part != NULL) { 632aca38eabSMarius Strobl if (part->disk != NULL) { 633aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 63472dec079SMarius Strobl part->suspend = 0; 63572dec079SMarius Strobl if (part->running > 0) { 636eb67f31aSAlexander Motin /* kill thread */ 63772dec079SMarius Strobl part->running = 0; 63872dec079SMarius Strobl wakeup(part); 639eb67f31aSAlexander Motin /* wait for thread to finish. */ 64072dec079SMarius Strobl while (part->running != -1) 641aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 642aca38eabSMarius Strobl "mmcsd disk detach", 0); 643eb67f31aSAlexander Motin } 644aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 645aca38eabSMarius Strobl } 646aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 647aca38eabSMarius Strobl while (part->ioctl > 0) 648aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 649aca38eabSMarius Strobl "mmcsd IOCTL detach", 0); 650aca38eabSMarius Strobl part->ioctl = -1; 651aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 65272dec079SMarius Strobl } 65372dec079SMarius Strobl } 6540e1466acSWarner Losh 65572dec079SMarius Strobl if (sc->rpmb_dev != NULL) 65672dec079SMarius Strobl destroy_dev(sc->rpmb_dev); 65772dec079SMarius Strobl 65872dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 65972dec079SMarius Strobl part = sc->part[i]; 66072dec079SMarius Strobl if (part != NULL) { 66172dec079SMarius Strobl if (part->disk != NULL) { 662249b0e85SAlexander Motin /* Flush the request queue. */ 66372dec079SMarius Strobl bioq_flush(&part->bio_queue, NULL, ENXIO); 6640e1466acSWarner Losh /* kill disk */ 66572dec079SMarius Strobl disk_destroy(part->disk); 6660e1466acSWarner Losh 667aca38eabSMarius Strobl MMCSD_DISK_LOCK_DESTROY(part); 66872dec079SMarius Strobl } 669aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_DESTROY(part); 67072dec079SMarius Strobl free(part, M_DEVBUF); 67172dec079SMarius Strobl } 67272dec079SMarius Strobl } 673128d0ff2SWarner Losh return (0); 674114b4164SWarner Losh } 675114b4164SWarner Losh 676114b4164SWarner Losh static int 677eb67f31aSAlexander Motin mmcsd_suspend(device_t dev) 678eb67f31aSAlexander Motin { 679eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 68072dec079SMarius Strobl struct mmcsd_part *part; 68172dec079SMarius Strobl int i; 682eb67f31aSAlexander Motin 68372dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 68472dec079SMarius Strobl part = sc->part[i]; 685aca38eabSMarius Strobl if (part != NULL) { 686aca38eabSMarius Strobl if (part->disk != NULL) { 687aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 68872dec079SMarius Strobl part->suspend = 1; 68972dec079SMarius Strobl if (part->running > 0) { 690eb67f31aSAlexander Motin /* kill thread */ 69172dec079SMarius Strobl part->running = 0; 69272dec079SMarius Strobl wakeup(part); 693eb67f31aSAlexander Motin /* wait for thread to finish. */ 69472dec079SMarius Strobl while (part->running != -1) 695aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 696aca38eabSMarius Strobl "mmcsd disk suspension", 0); 697eb67f31aSAlexander Motin } 698aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 699aca38eabSMarius Strobl } 700aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 701aca38eabSMarius Strobl while (part->ioctl > 0) 702aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 703aca38eabSMarius Strobl "mmcsd IOCTL suspension", 0); 704aca38eabSMarius Strobl part->ioctl = -1; 705aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 70672dec079SMarius Strobl } 70772dec079SMarius Strobl } 708eb67f31aSAlexander Motin return (0); 709eb67f31aSAlexander Motin } 710eb67f31aSAlexander Motin 711eb67f31aSAlexander Motin static int 712eb67f31aSAlexander Motin mmcsd_resume(device_t dev) 713eb67f31aSAlexander Motin { 714eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 71572dec079SMarius Strobl struct mmcsd_part *part; 71672dec079SMarius Strobl int i; 717eb67f31aSAlexander Motin 71872dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 71972dec079SMarius Strobl part = sc->part[i]; 720aca38eabSMarius Strobl if (part != NULL) { 721aca38eabSMarius Strobl if (part->disk != NULL) { 722aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 72372dec079SMarius Strobl part->suspend = 0; 72472dec079SMarius Strobl if (part->running <= 0) { 72572dec079SMarius Strobl part->running = 1; 726aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 727aca38eabSMarius Strobl kproc_create(&mmcsd_task, part, 728aca38eabSMarius Strobl &part->p, 0, 0, "%s%d: mmc/sd card", 729aca38eabSMarius Strobl part->name, part->cnt); 730eb67f31aSAlexander Motin } else 731aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 732aca38eabSMarius Strobl } 733aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 734aca38eabSMarius Strobl part->ioctl = 0; 735aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 73672dec079SMarius Strobl } 73772dec079SMarius Strobl } 738eb67f31aSAlexander Motin return (0); 739eb67f31aSAlexander Motin } 740eb67f31aSAlexander Motin 741eb67f31aSAlexander Motin static int 742b440e965SMarius Strobl mmcsd_open(struct disk *dp __unused) 743114b4164SWarner Losh { 744ae5d8757SMarius Strobl 745128d0ff2SWarner Losh return (0); 746114b4164SWarner Losh } 747114b4164SWarner Losh 748114b4164SWarner Losh static int 749b440e965SMarius Strobl mmcsd_close(struct disk *dp __unused) 750114b4164SWarner Losh { 751ae5d8757SMarius Strobl 752128d0ff2SWarner Losh return (0); 753114b4164SWarner Losh } 754114b4164SWarner Losh 755114b4164SWarner Losh static void 756114b4164SWarner Losh mmcsd_strategy(struct bio *bp) 757114b4164SWarner Losh { 75872dec079SMarius Strobl struct mmcsd_part *part; 759114b4164SWarner Losh 76072dec079SMarius Strobl part = bp->bio_disk->d_drv1; 761aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 76272dec079SMarius Strobl if (part->running > 0 || part->suspend > 0) { 76372dec079SMarius Strobl bioq_disksort(&part->bio_queue, bp); 764aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 76572dec079SMarius Strobl wakeup(part); 766249b0e85SAlexander Motin } else { 767aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 768249b0e85SAlexander Motin biofinish(bp, NULL, ENXIO); 769249b0e85SAlexander Motin } 770114b4164SWarner Losh } 771114b4164SWarner Losh 77272dec079SMarius Strobl static int 77372dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 77472dec079SMarius Strobl int fflag, struct thread *td __unused) 77572dec079SMarius Strobl { 77672dec079SMarius Strobl 77772dec079SMarius Strobl return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag)); 77872dec079SMarius Strobl } 77972dec079SMarius Strobl 78072dec079SMarius Strobl static int 78172dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag, 78272dec079SMarius Strobl struct thread *td __unused) 78372dec079SMarius Strobl { 78472dec079SMarius Strobl 78572dec079SMarius Strobl return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag)); 78672dec079SMarius Strobl } 78772dec079SMarius Strobl 78872dec079SMarius Strobl static int 78972dec079SMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag) 79072dec079SMarius Strobl { 79172dec079SMarius Strobl struct mmc_ioc_cmd *mic; 79272dec079SMarius Strobl struct mmc_ioc_multi_cmd *mimc; 79372dec079SMarius Strobl int i, err; 79472dec079SMarius Strobl u_long cnt, size; 79572dec079SMarius Strobl 79672dec079SMarius Strobl if ((fflag & FREAD) == 0) 79772dec079SMarius Strobl return (EBADF); 79872dec079SMarius Strobl 79972dec079SMarius Strobl err = 0; 80072dec079SMarius Strobl switch (cmd) { 80172dec079SMarius Strobl case MMC_IOC_CMD: 80272dec079SMarius Strobl mic = data; 803aca38eabSMarius Strobl err = mmcsd_ioctl_cmd(part, mic, fflag); 80472dec079SMarius Strobl break; 805aca38eabSMarius Strobl case MMC_IOC_MULTI_CMD: 80672dec079SMarius Strobl mimc = data; 80772dec079SMarius Strobl if (mimc->num_of_cmds == 0) 80872dec079SMarius Strobl break; 80972dec079SMarius Strobl if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS) 81072dec079SMarius Strobl return (EINVAL); 81172dec079SMarius Strobl cnt = mimc->num_of_cmds; 81272dec079SMarius Strobl size = sizeof(*mic) * cnt; 81372dec079SMarius Strobl mic = malloc(size, M_TEMP, M_WAITOK); 81472dec079SMarius Strobl err = copyin((const void *)mimc->cmds, mic, size); 815aca38eabSMarius Strobl if (err == 0) { 81672dec079SMarius Strobl for (i = 0; i < cnt; i++) { 81772dec079SMarius Strobl err = mmcsd_ioctl_cmd(part, &mic[i], fflag); 81872dec079SMarius Strobl if (err != 0) 81972dec079SMarius Strobl break; 82072dec079SMarius Strobl } 821aca38eabSMarius Strobl } 82272dec079SMarius Strobl free(mic, M_TEMP); 82372dec079SMarius Strobl break; 82472dec079SMarius Strobl default: 82572dec079SMarius Strobl return (ENOIOCTL); 82672dec079SMarius Strobl } 82772dec079SMarius Strobl return (err); 82872dec079SMarius Strobl } 82972dec079SMarius Strobl 83072dec079SMarius Strobl static int 83172dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag) 83272dec079SMarius Strobl { 83372dec079SMarius Strobl struct mmc_command cmd; 83472dec079SMarius Strobl struct mmc_data data; 83572dec079SMarius Strobl struct mmcsd_softc *sc; 836cd85acbaSMarius Strobl device_t dev, mmcbus; 83772dec079SMarius Strobl void *dp; 83872dec079SMarius Strobl u_long len; 83972dec079SMarius Strobl int err, retries; 84072dec079SMarius Strobl uint32_t status; 84172dec079SMarius Strobl uint16_t rca; 84272dec079SMarius Strobl 84372dec079SMarius Strobl if ((fflag & FWRITE) == 0 && mic->write_flag != 0) 84472dec079SMarius Strobl return (EBADF); 84572dec079SMarius Strobl 84672dec079SMarius Strobl if (part->ro == TRUE && mic->write_flag != 0) 84772dec079SMarius Strobl return (EROFS); 84872dec079SMarius Strobl 849aca38eabSMarius Strobl /* 850aca38eabSMarius Strobl * We don't need to explicitly lock against the disk(9) half of this 851aca38eabSMarius Strobl * driver as MMCBUS_ACQUIRE_BUS() will serialize us. However, it's 852aca38eabSMarius Strobl * necessary to protect against races with detachment and suspension, 853aca38eabSMarius Strobl * especially since it's required to switch away from RPMB partitions 854aca38eabSMarius Strobl * again after an access (see mmcsd_switch_part()). 855aca38eabSMarius Strobl */ 856aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 857aca38eabSMarius Strobl while (part->ioctl != 0) { 858aca38eabSMarius Strobl if (part->ioctl < 0) { 859aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 860aca38eabSMarius Strobl return (ENXIO); 861aca38eabSMarius Strobl } 862aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0); 863aca38eabSMarius Strobl } 864aca38eabSMarius Strobl part->ioctl = 1; 865aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 866aca38eabSMarius Strobl 86772dec079SMarius Strobl err = 0; 86872dec079SMarius Strobl dp = NULL; 86972dec079SMarius Strobl len = mic->blksz * mic->blocks; 870aca38eabSMarius Strobl if (len > MMC_IOC_MAX_BYTES) { 871aca38eabSMarius Strobl err = EOVERFLOW; 872aca38eabSMarius Strobl goto out; 873aca38eabSMarius Strobl } 87472dec079SMarius Strobl if (len != 0) { 87572dec079SMarius Strobl dp = malloc(len, M_TEMP, M_WAITOK); 87672dec079SMarius Strobl err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len); 87772dec079SMarius Strobl if (err != 0) 87872dec079SMarius Strobl goto out; 87972dec079SMarius Strobl } 88072dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 88172dec079SMarius Strobl memset(&data, 0, sizeof(data)); 88272dec079SMarius Strobl cmd.opcode = mic->opcode; 88372dec079SMarius Strobl cmd.arg = mic->arg; 88472dec079SMarius Strobl cmd.flags = mic->flags; 88572dec079SMarius Strobl if (len != 0) { 88672dec079SMarius Strobl data.len = len; 88772dec079SMarius Strobl data.data = dp; 88872dec079SMarius Strobl data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE : 88972dec079SMarius Strobl MMC_DATA_READ; 89072dec079SMarius Strobl cmd.data = &data; 89172dec079SMarius Strobl } 89272dec079SMarius Strobl sc = part->sc; 89372dec079SMarius Strobl rca = sc->rca; 89472dec079SMarius Strobl if (mic->is_acmd == 0) { 89572dec079SMarius Strobl /* Enforce/patch/restrict RCA-based commands */ 89672dec079SMarius Strobl switch (cmd.opcode) { 89772dec079SMarius Strobl case MMC_SET_RELATIVE_ADDR: 89872dec079SMarius Strobl case MMC_SELECT_CARD: 89972dec079SMarius Strobl err = EPERM; 90072dec079SMarius Strobl goto out; 90172dec079SMarius Strobl case MMC_STOP_TRANSMISSION: 90272dec079SMarius Strobl if ((cmd.arg & 0x1) == 0) 90372dec079SMarius Strobl break; 90472dec079SMarius Strobl /* FALLTHROUGH */ 90572dec079SMarius Strobl case MMC_SLEEP_AWAKE: 90672dec079SMarius Strobl case MMC_SEND_CSD: 90772dec079SMarius Strobl case MMC_SEND_CID: 90872dec079SMarius Strobl case MMC_SEND_STATUS: 90972dec079SMarius Strobl case MMC_GO_INACTIVE_STATE: 91072dec079SMarius Strobl case MMC_FAST_IO: 91172dec079SMarius Strobl case MMC_APP_CMD: 91272dec079SMarius Strobl cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16); 91372dec079SMarius Strobl break; 91472dec079SMarius Strobl default: 91572dec079SMarius Strobl break; 91672dec079SMarius Strobl } 9176c06949fSMarius Strobl /* 9186c06949fSMarius Strobl * No partition switching in userland; it's almost impossible 9196c06949fSMarius Strobl * to recover from that, especially if things go wrong. 9206c06949fSMarius Strobl */ 9216c06949fSMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC && dp != NULL && 9226c06949fSMarius Strobl (((uint8_t *)dp)[EXT_CSD_PART_CONFIG] & 9236c06949fSMarius Strobl EXT_CSD_PART_CONFIG_ACC_MASK) != sc->part_curr) { 9246c06949fSMarius Strobl err = EINVAL; 9256c06949fSMarius Strobl goto out; 9266c06949fSMarius Strobl } 92772dec079SMarius Strobl } 92872dec079SMarius Strobl dev = sc->dev; 929cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 930cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 931cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, part->type); 93272dec079SMarius Strobl if (err != MMC_ERR_NONE) 93372dec079SMarius Strobl goto release; 93472dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 93572dec079SMarius Strobl err = mmcsd_set_blockcount(sc, mic->blocks, 93672dec079SMarius Strobl mic->write_flag & (1 << 31)); 93772dec079SMarius Strobl if (err != MMC_ERR_NONE) 938aca38eabSMarius Strobl goto switch_back; 93972dec079SMarius Strobl } 94072dec079SMarius Strobl if (mic->is_acmd != 0) 941cd85acbaSMarius Strobl (void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0); 94272dec079SMarius Strobl else 943cd85acbaSMarius Strobl (void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0); 94472dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 94572dec079SMarius Strobl /* 94672dec079SMarius Strobl * If the request went to the RPMB partition, try to ensure 9476c06949fSMarius Strobl * that the command actually has completed. 94872dec079SMarius Strobl */ 94972dec079SMarius Strobl retries = MMCSD_CMD_RETRIES; 95072dec079SMarius Strobl do { 951cd85acbaSMarius Strobl err = mmc_send_status(mmcbus, dev, rca, &status); 95272dec079SMarius Strobl if (err != MMC_ERR_NONE) 95372dec079SMarius Strobl break; 95472dec079SMarius Strobl if (R1_STATUS(status) == 0 && 95572dec079SMarius Strobl R1_CURRENT_STATE(status) != R1_STATE_PRG) 95672dec079SMarius Strobl break; 95772dec079SMarius Strobl DELAY(1000); 95872dec079SMarius Strobl } while (retries-- > 0); 95972dec079SMarius Strobl } 96072dec079SMarius Strobl /* 96172dec079SMarius Strobl * If EXT_CSD was changed, our copy is outdated now. Specifically, 96272dec079SMarius Strobl * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(), 96372dec079SMarius Strobl * so retrieve EXT_CSD again. 96472dec079SMarius Strobl */ 96572dec079SMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC) { 966cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 96772dec079SMarius Strobl if (err != MMC_ERR_NONE) 96872dec079SMarius Strobl goto release; 96972dec079SMarius Strobl } 9706c06949fSMarius Strobl switch_back: 9716c06949fSMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 9726c06949fSMarius Strobl /* 9736c06949fSMarius Strobl * If the request went to the RPMB partition, always switch 9746c06949fSMarius Strobl * back to the default partition (see mmcsd_switch_part()). 9756c06949fSMarius Strobl */ 9766c06949fSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, 9776c06949fSMarius Strobl EXT_CSD_PART_CONFIG_ACC_DEFAULT); 9786c06949fSMarius Strobl if (err != MMC_ERR_NONE) 9796c06949fSMarius Strobl goto release; 9806c06949fSMarius Strobl } 981cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 98272dec079SMarius Strobl if (cmd.error != MMC_ERR_NONE) { 98372dec079SMarius Strobl switch (cmd.error) { 98472dec079SMarius Strobl case MMC_ERR_TIMEOUT: 98572dec079SMarius Strobl err = ETIMEDOUT; 98672dec079SMarius Strobl break; 98772dec079SMarius Strobl case MMC_ERR_BADCRC: 98872dec079SMarius Strobl err = EILSEQ; 98972dec079SMarius Strobl break; 99072dec079SMarius Strobl case MMC_ERR_INVALID: 99172dec079SMarius Strobl err = EINVAL; 99272dec079SMarius Strobl break; 99372dec079SMarius Strobl case MMC_ERR_NO_MEMORY: 99472dec079SMarius Strobl err = ENOMEM; 99572dec079SMarius Strobl break; 99672dec079SMarius Strobl default: 99772dec079SMarius Strobl err = EIO; 99872dec079SMarius Strobl break; 99972dec079SMarius Strobl } 100072dec079SMarius Strobl goto out; 100172dec079SMarius Strobl } 100272dec079SMarius Strobl memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t)); 100372dec079SMarius Strobl if (mic->write_flag == 0 && len != 0) { 100472dec079SMarius Strobl err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len); 100572dec079SMarius Strobl if (err != 0) 100672dec079SMarius Strobl goto out; 100772dec079SMarius Strobl } 100872dec079SMarius Strobl goto out; 100972dec079SMarius Strobl 101072dec079SMarius Strobl release: 1011cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 101272dec079SMarius Strobl err = EIO; 101372dec079SMarius Strobl 101472dec079SMarius Strobl out: 1015aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 1016aca38eabSMarius Strobl part->ioctl = 0; 1017aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 1018aca38eabSMarius Strobl wakeup(part); 101972dec079SMarius Strobl if (dp != NULL) 102072dec079SMarius Strobl free(dp, M_TEMP); 102172dec079SMarius Strobl return (err); 102272dec079SMarius Strobl } 102372dec079SMarius Strobl 102472dec079SMarius Strobl static int 102572dec079SMarius Strobl mmcsd_getattr(struct bio *bp) 102672dec079SMarius Strobl { 102772dec079SMarius Strobl struct mmcsd_part *part; 102872dec079SMarius Strobl device_t dev; 102972dec079SMarius Strobl 103072dec079SMarius Strobl if (strcmp(bp->bio_attribute, "MMC::device") == 0) { 103172dec079SMarius Strobl if (bp->bio_length != sizeof(dev)) 103272dec079SMarius Strobl return (EFAULT); 103372dec079SMarius Strobl part = bp->bio_disk->d_drv1; 103472dec079SMarius Strobl dev = part->sc->dev; 103572dec079SMarius Strobl bcopy(&dev, bp->bio_data, sizeof(dev)); 103672dec079SMarius Strobl bp->bio_completed = bp->bio_length; 103772dec079SMarius Strobl return (0); 103872dec079SMarius Strobl } 103972dec079SMarius Strobl return (-1); 104072dec079SMarius Strobl } 104172dec079SMarius Strobl 104272dec079SMarius Strobl static int 104372dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable) 104472dec079SMarius Strobl { 104572dec079SMarius Strobl struct mmc_command cmd; 104672dec079SMarius Strobl struct mmc_request req; 104772dec079SMarius Strobl 104872dec079SMarius Strobl memset(&req, 0, sizeof(req)); 104972dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 105072dec079SMarius Strobl cmd.mrq = &req; 105172dec079SMarius Strobl req.cmd = &cmd; 105272dec079SMarius Strobl cmd.opcode = MMC_SET_BLOCK_COUNT; 105372dec079SMarius Strobl cmd.arg = count & 0x0000FFFF; 105472dec079SMarius Strobl if (reliable) 105572dec079SMarius Strobl cmd.arg |= 1 << 31; 105672dec079SMarius Strobl cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1057cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req); 105872dec079SMarius Strobl return (cmd.error); 105972dec079SMarius Strobl } 106072dec079SMarius Strobl 106172dec079SMarius Strobl static int 106272dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part) 106372dec079SMarius Strobl { 106472dec079SMarius Strobl struct mmcsd_softc *sc; 106572dec079SMarius Strobl int err; 106672dec079SMarius Strobl uint8_t value; 106772dec079SMarius Strobl 106872dec079SMarius Strobl sc = device_get_softc(dev); 106972dec079SMarius Strobl 1070aca38eabSMarius Strobl if (sc->mode == mode_sd) 107172dec079SMarius Strobl return (MMC_ERR_NONE); 107272dec079SMarius Strobl 1073aca38eabSMarius Strobl /* 1074aca38eabSMarius Strobl * According to section "6.2.2 Command restrictions" of the eMMC 1075aca38eabSMarius Strobl * specification v5.1, CMD19/CMD21 aren't allowed to be used with 1076aca38eabSMarius Strobl * RPMB partitions. So we pause re-tuning along with triggering 1077aca38eabSMarius Strobl * it up-front to decrease the likelihood of re-tuning becoming 1078aca38eabSMarius Strobl * necessary while accessing an RPMB partition. Consequently, an 1079aca38eabSMarius Strobl * RPMB partition should immediately be switched away from again 1080aca38eabSMarius Strobl * after an access in order to allow for re-tuning to take place 1081aca38eabSMarius Strobl * anew. 1082aca38eabSMarius Strobl */ 1083aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1084cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true); 1085aca38eabSMarius Strobl 1086aca38eabSMarius Strobl if (sc->part_curr == part) 108772dec079SMarius Strobl return (MMC_ERR_NONE); 108872dec079SMarius Strobl 108972dec079SMarius Strobl value = (sc->ext_csd[EXT_CSD_PART_CONFIG] & 109072dec079SMarius Strobl ~EXT_CSD_PART_CONFIG_ACC_MASK) | part; 109172dec079SMarius Strobl /* Jump! */ 109272dec079SMarius Strobl err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL, 109372dec079SMarius Strobl EXT_CSD_PART_CONFIG, value, sc->part_time, true); 1094aca38eabSMarius Strobl if (err != MMC_ERR_NONE) { 1095aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1096cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 109772dec079SMarius Strobl return (err); 1098aca38eabSMarius Strobl } 109972dec079SMarius Strobl 110072dec079SMarius Strobl sc->ext_csd[EXT_CSD_PART_CONFIG] = value; 1101aca38eabSMarius Strobl if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) 1102cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 110372dec079SMarius Strobl sc->part_curr = part; 110472dec079SMarius Strobl return (MMC_ERR_NONE); 110572dec079SMarius Strobl } 110672dec079SMarius Strobl 1107bb1ef63fSWarner Losh static const char * 1108bb1ef63fSWarner Losh mmcsd_errmsg(int e) 1109bb1ef63fSWarner Losh { 11101bacf3beSMarius Strobl 1111bb1ef63fSWarner Losh if (e < 0 || e > MMC_ERR_MAX) 1112bb1ef63fSWarner Losh return "Bad error code"; 1113aca38eabSMarius Strobl return (errmsg[e]); 1114bb1ef63fSWarner Losh } 1115bb1ef63fSWarner Losh 11163906d42dSAlexander Motin static daddr_t 111772dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp) 1118114b4164SWarner Losh { 1119114b4164SWarner Losh daddr_t block, end; 1120114b4164SWarner Losh struct mmc_command cmd; 1121114b4164SWarner Losh struct mmc_command stop; 1122114b4164SWarner Losh struct mmc_request req; 1123114b4164SWarner Losh struct mmc_data data; 112472dec079SMarius Strobl struct mmcsd_softc *sc; 1125cd85acbaSMarius Strobl device_t dev, mmcbus; 1126aca38eabSMarius Strobl u_int numblocks, sz; 11271bacf3beSMarius Strobl char *vaddr; 1128114b4164SWarner Losh 112972dec079SMarius Strobl sc = part->sc; 113072dec079SMarius Strobl dev = sc->dev; 1131cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 113272dec079SMarius Strobl 11333906d42dSAlexander Motin block = bp->bio_pblkno; 113472dec079SMarius Strobl sz = part->disk->d_sectorsize; 1135114b4164SWarner Losh end = bp->bio_pblkno + (bp->bio_bcount / sz); 11363906d42dSAlexander Motin while (block < end) { 11371bacf3beSMarius Strobl vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 1138aca38eabSMarius Strobl numblocks = min(end - block, sc->max_data); 1139114b4164SWarner Losh memset(&req, 0, sizeof(req)); 1140114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 1141114b4164SWarner Losh memset(&stop, 0, sizeof(stop)); 1142ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1143a350e540SIan Lepore cmd.mrq = &req; 1144114b4164SWarner Losh req.cmd = &cmd; 1145114b4164SWarner Losh cmd.data = &data; 1146114b4164SWarner Losh if (bp->bio_cmd == BIO_READ) { 11470c0903b1SWarner Losh if (numblocks > 1) 1148114b4164SWarner Losh cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 1149114b4164SWarner Losh else 1150114b4164SWarner Losh cmd.opcode = MMC_READ_SINGLE_BLOCK; 11510c0903b1SWarner Losh } else { 11520c0903b1SWarner Losh if (numblocks > 1) 11530c0903b1SWarner Losh cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 11540c0903b1SWarner Losh else 1155114b4164SWarner Losh cmd.opcode = MMC_WRITE_BLOCK; 11560c0903b1SWarner Losh } 1157c18f1e26SAlexander Motin cmd.arg = block; 1158aca38eabSMarius Strobl if (sc->high_cap == 0) 1159c18f1e26SAlexander Motin cmd.arg <<= 9; 1160114b4164SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1161114b4164SWarner Losh data.data = vaddr; 1162114b4164SWarner Losh data.mrq = &req; 11630c0903b1SWarner Losh if (bp->bio_cmd == BIO_READ) 1164114b4164SWarner Losh data.flags = MMC_DATA_READ; 11650c0903b1SWarner Losh else 1166114b4164SWarner Losh data.flags = MMC_DATA_WRITE; 11670c0903b1SWarner Losh data.len = numblocks * sz; 11680c0903b1SWarner Losh if (numblocks > 1) { 11690c0903b1SWarner Losh data.flags |= MMC_DATA_MULTI; 1170114b4164SWarner Losh stop.opcode = MMC_STOP_TRANSMISSION; 1171114b4164SWarner Losh stop.arg = 0; 1172114b4164SWarner Losh stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 1173a350e540SIan Lepore stop.mrq = &req; 11740c0903b1SWarner Losh req.stop = &stop; 11750c0903b1SWarner Losh } 1176cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1177bb1ef63fSWarner Losh if (req.cmd->error != MMC_ERR_NONE) { 11781bacf3beSMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 11791bacf3beSMarius Strobl LOG_PPS)) 1180bb1ef63fSWarner Losh device_printf(dev, "Error indicated: %d %s\n", 11811bacf3beSMarius Strobl req.cmd->error, 11821bacf3beSMarius Strobl mmcsd_errmsg(req.cmd->error)); 11830c0903b1SWarner Losh break; 1184bb1ef63fSWarner Losh } 11850c0903b1SWarner Losh block += numblocks; 1186114b4164SWarner Losh } 11873906d42dSAlexander Motin return (block); 11883906d42dSAlexander Motin } 11893906d42dSAlexander Motin 11903906d42dSAlexander Motin static daddr_t 119172dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp) 11923906d42dSAlexander Motin { 11933906d42dSAlexander Motin daddr_t block, end, start, stop; 11943906d42dSAlexander Motin struct mmc_command cmd; 11953906d42dSAlexander Motin struct mmc_request req; 119672dec079SMarius Strobl struct mmcsd_softc *sc; 1197cd85acbaSMarius Strobl device_t dev, mmcbus; 1198aca38eabSMarius Strobl u_int erase_sector, sz; 119979f39c6aSMarius Strobl int err; 120079f39c6aSMarius Strobl bool use_trim; 120172dec079SMarius Strobl 120272dec079SMarius Strobl sc = part->sc; 120372dec079SMarius Strobl dev = sc->dev; 1204cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 12053906d42dSAlexander Motin 12063906d42dSAlexander Motin block = bp->bio_pblkno; 120772dec079SMarius Strobl sz = part->disk->d_sectorsize; 12083906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 120979f39c6aSMarius Strobl use_trim = sc->flags & MMCSD_USE_TRIM; 121079f39c6aSMarius Strobl if (use_trim == true) { 121179f39c6aSMarius Strobl start = block; 121279f39c6aSMarius Strobl stop = end; 121379f39c6aSMarius Strobl } else { 121479f39c6aSMarius Strobl /* Coalesce with the remainder of the previous request. */ 121572dec079SMarius Strobl if (block > part->eblock && block <= part->eend) 121672dec079SMarius Strobl block = part->eblock; 121772dec079SMarius Strobl if (end >= part->eblock && end < part->eend) 121872dec079SMarius Strobl end = part->eend; 121979f39c6aSMarius Strobl /* Safely round to the erase sector boundaries. */ 1220aca38eabSMarius Strobl erase_sector = sc->erase_sector; 12213906d42dSAlexander Motin start = block + erase_sector - 1; /* Round up. */ 12223906d42dSAlexander Motin start -= start % erase_sector; 12233906d42dSAlexander Motin stop = end; /* Round down. */ 12243906d42dSAlexander Motin stop -= end % erase_sector; 122579f39c6aSMarius Strobl /* 122679f39c6aSMarius Strobl * We can't erase an area smaller than an erase sector, so 122779f39c6aSMarius Strobl * store it for later. 122879f39c6aSMarius Strobl */ 1229a7cf6274SAlexander Motin if (start >= stop) { 123072dec079SMarius Strobl part->eblock = block; 123172dec079SMarius Strobl part->eend = end; 12323906d42dSAlexander Motin return (end); 1233a7cf6274SAlexander Motin } 123479f39c6aSMarius Strobl } 123579f39c6aSMarius Strobl 123679f39c6aSMarius Strobl if ((sc->flags & MMCSD_INAND_CMD38) != 0) { 123779f39c6aSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 123879f39c6aSMarius Strobl EXT_CSD_INAND_CMD38, use_trim == true ? 123979f39c6aSMarius Strobl EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE, 124079f39c6aSMarius Strobl sc->cmd6_time, true); 124179f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 124279f39c6aSMarius Strobl device_printf(dev, 124379f39c6aSMarius Strobl "Setting iNAND erase command failed %s\n", 124479f39c6aSMarius Strobl mmcsd_errmsg(err)); 124579f39c6aSMarius Strobl return (block); 124679f39c6aSMarius Strobl } 124779f39c6aSMarius Strobl } 12483906d42dSAlexander Motin 1249aca38eabSMarius Strobl /* 1250aca38eabSMarius Strobl * Pause re-tuning so it won't interfere with the order of erase 1251aca38eabSMarius Strobl * commands. Note that these latter don't use the data lines, so 1252aca38eabSMarius Strobl * re-tuning shouldn't actually become necessary during erase. 1253aca38eabSMarius Strobl */ 1254cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(mmcbus, dev, false); 12553906d42dSAlexander Motin /* Set erase start position. */ 12563906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 12573906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 1258a350e540SIan Lepore cmd.mrq = &req; 12593906d42dSAlexander Motin req.cmd = &cmd; 12603906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 12613906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_START; 12623906d42dSAlexander Motin else 12633906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_START; 12643906d42dSAlexander Motin cmd.arg = start; 1265aca38eabSMarius Strobl if (sc->high_cap == 0) 12663906d42dSAlexander Motin cmd.arg <<= 9; 12673906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1268cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 12693906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 127079f39c6aSMarius Strobl device_printf(dev, "Setting erase start position failed %s\n", 127179f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1272aca38eabSMarius Strobl block = bp->bio_pblkno; 1273aca38eabSMarius Strobl goto unpause; 12743906d42dSAlexander Motin } 12753906d42dSAlexander Motin /* Set erase stop position. */ 12763906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 12773906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 12783906d42dSAlexander Motin req.cmd = &cmd; 12793906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 12803906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_END; 12813906d42dSAlexander Motin else 12823906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_END; 12833906d42dSAlexander Motin cmd.arg = stop; 1284aca38eabSMarius Strobl if (sc->high_cap == 0) 12853906d42dSAlexander Motin cmd.arg <<= 9; 12863906d42dSAlexander Motin cmd.arg--; 12873906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1288cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 12893906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 129079f39c6aSMarius Strobl device_printf(dev, "Setting erase stop position failed %s\n", 129179f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1292aca38eabSMarius Strobl block = bp->bio_pblkno; 1293aca38eabSMarius Strobl goto unpause; 12943906d42dSAlexander Motin } 12953906d42dSAlexander Motin /* Erase range. */ 12963906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 12973906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 12983906d42dSAlexander Motin req.cmd = &cmd; 12993906d42dSAlexander Motin cmd.opcode = MMC_ERASE; 130079f39c6aSMarius Strobl cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE; 13013906d42dSAlexander Motin cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 1302cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13033906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 130479f39c6aSMarius Strobl device_printf(dev, "Issuing erase command failed %s\n", 130579f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1306aca38eabSMarius Strobl block = bp->bio_pblkno; 1307aca38eabSMarius Strobl goto unpause; 13083906d42dSAlexander Motin } 130979f39c6aSMarius Strobl if (use_trim == false) { 131079f39c6aSMarius Strobl /* Store one of the remaining parts for the next call. */ 131172dec079SMarius Strobl if (bp->bio_pblkno >= part->eblock || block == start) { 131272dec079SMarius Strobl part->eblock = stop; /* Predict next forward. */ 131372dec079SMarius Strobl part->eend = end; 1314a7cf6274SAlexander Motin } else { 131572dec079SMarius Strobl part->eblock = block; /* Predict next backward. */ 131672dec079SMarius Strobl part->eend = start; 1317a7cf6274SAlexander Motin } 131879f39c6aSMarius Strobl } 1319aca38eabSMarius Strobl block = end; 1320aca38eabSMarius Strobl unpause: 1321cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(mmcbus, dev); 1322aca38eabSMarius Strobl return (block); 13233906d42dSAlexander Motin } 13243906d42dSAlexander Motin 1325a1fda318SAlexander Motin static int 13261bacf3beSMarius Strobl mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 13271bacf3beSMarius Strobl size_t length) 1328a1fda318SAlexander Motin { 1329a1fda318SAlexander Motin struct bio bp; 1330a1fda318SAlexander Motin daddr_t block, end; 133172dec079SMarius Strobl struct disk *disk; 133272dec079SMarius Strobl struct mmcsd_softc *sc; 133372dec079SMarius Strobl struct mmcsd_part *part; 1334cd85acbaSMarius Strobl device_t dev, mmcbus; 133572dec079SMarius Strobl int err; 1336a1fda318SAlexander Motin 1337a1fda318SAlexander Motin /* length zero is special and really means flush buffers to media */ 1338a1fda318SAlexander Motin if (!length) 1339a1fda318SAlexander Motin return (0); 1340a1fda318SAlexander Motin 134172dec079SMarius Strobl disk = arg; 134272dec079SMarius Strobl part = disk->d_drv1; 134372dec079SMarius Strobl sc = part->sc; 134472dec079SMarius Strobl dev = sc->dev; 1345cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 134672dec079SMarius Strobl 1347c55f5707SWarner Losh g_reset_bio(&bp); 1348a1fda318SAlexander Motin bp.bio_disk = disk; 1349a1fda318SAlexander Motin bp.bio_pblkno = offset / disk->d_sectorsize; 1350a1fda318SAlexander Motin bp.bio_bcount = length; 1351a1fda318SAlexander Motin bp.bio_data = virtual; 1352a1fda318SAlexander Motin bp.bio_cmd = BIO_WRITE; 135372dec079SMarius Strobl end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize; 1354cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1355cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 135672dec079SMarius Strobl if (err != MMC_ERR_NONE) { 135772dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) 135872dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 1359cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 136072dec079SMarius Strobl return (EIO); 136172dec079SMarius Strobl } 136272dec079SMarius Strobl block = mmcsd_rw(part, &bp); 1363cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1364a1fda318SAlexander Motin return ((end < block) ? EIO : 0); 1365a1fda318SAlexander Motin } 1366a1fda318SAlexander Motin 13673906d42dSAlexander Motin static void 13683906d42dSAlexander Motin mmcsd_task(void *arg) 13693906d42dSAlexander Motin { 13703906d42dSAlexander Motin daddr_t block, end; 137172dec079SMarius Strobl struct mmcsd_part *part; 137272dec079SMarius Strobl struct mmcsd_softc *sc; 137372dec079SMarius Strobl struct bio *bp; 1374cd85acbaSMarius Strobl device_t dev, mmcbus; 137572dec079SMarius Strobl int err, sz; 137672dec079SMarius Strobl 137772dec079SMarius Strobl part = arg; 137872dec079SMarius Strobl sc = part->sc; 137972dec079SMarius Strobl dev = sc->dev; 1380cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 13813906d42dSAlexander Motin 1382249b0e85SAlexander Motin while (1) { 1383aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 13843906d42dSAlexander Motin do { 138572dec079SMarius Strobl if (part->running == 0) 1386249b0e85SAlexander Motin goto out; 138772dec079SMarius Strobl bp = bioq_takefirst(&part->bio_queue); 13883906d42dSAlexander Motin if (bp == NULL) 1389aca38eabSMarius Strobl msleep(part, &part->disk_mtx, PRIBIO, 1390aca38eabSMarius Strobl "mmcsd disk jobqueue", 0); 1391249b0e85SAlexander Motin } while (bp == NULL); 1392aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 139372dec079SMarius Strobl if (bp->bio_cmd != BIO_READ && part->ro) { 13943906d42dSAlexander Motin bp->bio_error = EROFS; 13953906d42dSAlexander Motin bp->bio_resid = bp->bio_bcount; 13963906d42dSAlexander Motin bp->bio_flags |= BIO_ERROR; 13973906d42dSAlexander Motin biodone(bp); 13983906d42dSAlexander Motin continue; 13993906d42dSAlexander Motin } 1400cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 140172dec079SMarius Strobl sz = part->disk->d_sectorsize; 14023906d42dSAlexander Motin block = bp->bio_pblkno; 14033906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 1404cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 140572dec079SMarius Strobl if (err != MMC_ERR_NONE) { 140672dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 140772dec079SMarius Strobl LOG_PPS)) 140872dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 140972dec079SMarius Strobl goto release; 141072dec079SMarius Strobl } 14113906d42dSAlexander Motin if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 1412a7cf6274SAlexander Motin /* Access to the remaining erase block obsoletes it. */ 141372dec079SMarius Strobl if (block < part->eend && end > part->eblock) 141472dec079SMarius Strobl part->eblock = part->eend = 0; 141572dec079SMarius Strobl block = mmcsd_rw(part, bp); 14163906d42dSAlexander Motin } else if (bp->bio_cmd == BIO_DELETE) { 141772dec079SMarius Strobl block = mmcsd_delete(part, bp); 14183906d42dSAlexander Motin } 141972dec079SMarius Strobl release: 1420cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 14210c0903b1SWarner Losh if (block < end) { 14220c0903b1SWarner Losh bp->bio_error = EIO; 14230c0903b1SWarner Losh bp->bio_resid = (end - block) * sz; 14240c0903b1SWarner Losh bp->bio_flags |= BIO_ERROR; 1425d119f637SMarius Strobl } else { 1426d119f637SMarius Strobl bp->bio_resid = 0; 14270c0903b1SWarner Losh } 1428114b4164SWarner Losh biodone(bp); 1429114b4164SWarner Losh } 1430249b0e85SAlexander Motin out: 14310e1466acSWarner Losh /* tell parent we're done */ 143272dec079SMarius Strobl part->running = -1; 1433aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 143472dec079SMarius Strobl wakeup(part); 14350e1466acSWarner Losh 14363745c395SJulian Elischer kproc_exit(0); 1437114b4164SWarner Losh } 1438114b4164SWarner Losh 1439a0075e58SWarner Losh static int 1440a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev) 1441a0075e58SWarner Losh { 1442ae5d8757SMarius Strobl 1443a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_1) 1444a0075e58SWarner Losh return (1); 1445a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_4) 1446a0075e58SWarner Losh return (4); 1447a0075e58SWarner Losh return (8); 1448a0075e58SWarner Losh } 1449a0075e58SWarner Losh 1450114b4164SWarner Losh static device_method_t mmcsd_methods[] = { 1451114b4164SWarner Losh DEVMETHOD(device_probe, mmcsd_probe), 1452114b4164SWarner Losh DEVMETHOD(device_attach, mmcsd_attach), 1453114b4164SWarner Losh DEVMETHOD(device_detach, mmcsd_detach), 1454eb67f31aSAlexander Motin DEVMETHOD(device_suspend, mmcsd_suspend), 1455eb67f31aSAlexander Motin DEVMETHOD(device_resume, mmcsd_resume), 14567aa65846SMarius Strobl DEVMETHOD_END 1457114b4164SWarner Losh }; 1458114b4164SWarner Losh 1459114b4164SWarner Losh static driver_t mmcsd_driver = { 1460114b4164SWarner Losh "mmcsd", 1461114b4164SWarner Losh mmcsd_methods, 1462114b4164SWarner Losh sizeof(struct mmcsd_softc), 1463114b4164SWarner Losh }; 1464114b4164SWarner Losh static devclass_t mmcsd_devclass; 1465114b4164SWarner Losh 146672dec079SMarius Strobl static int 146772dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused) 146872dec079SMarius Strobl { 146972dec079SMarius Strobl 147072dec079SMarius Strobl switch (what) { 147172dec079SMarius Strobl case MOD_LOAD: 147272dec079SMarius Strobl flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC, 147372dec079SMarius Strobl TRUE); 147472dec079SMarius Strobl return (0); 147572dec079SMarius Strobl case MOD_UNLOAD: 147672dec079SMarius Strobl flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE); 147772dec079SMarius Strobl return (0); 147872dec079SMarius Strobl } 147972dec079SMarius Strobl return (0); 148072dec079SMarius Strobl } 148172dec079SMarius Strobl 148272dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL); 148372dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0); 148472dec079SMarius Strobl MMC_DEPEND(mmcsd); 1485