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. 5f86e6000SWarner Losh * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org> 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> 64602a05b0SMarius Strobl #include <sys/endian.h> 6572dec079SMarius Strobl #include <sys/fcntl.h> 6672dec079SMarius Strobl #include <sys/ioccom.h> 67114b4164SWarner Losh #include <sys/kernel.h> 68114b4164SWarner Losh #include <sys/kthread.h> 69114b4164SWarner Losh #include <sys/lock.h> 70114b4164SWarner Losh #include <sys/malloc.h> 71114b4164SWarner Losh #include <sys/module.h> 72114b4164SWarner Losh #include <sys/mutex.h> 737217ea7cSMarius Strobl #include <sys/priv.h> 7472dec079SMarius Strobl #include <sys/slicer.h> 75646fd30cSMarius Strobl #include <sys/sysctl.h> 765e3dbf8bSIan Lepore #include <sys/time.h> 7772dec079SMarius Strobl 78c55f5707SWarner Losh #include <geom/geom.h> 79114b4164SWarner Losh #include <geom/geom_disk.h> 80114b4164SWarner Losh 8172dec079SMarius Strobl #include <dev/mmc/bridge.h> 8272dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h> 8372dec079SMarius Strobl #include <dev/mmc/mmc_subr.h> 847aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h> 85114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 867aa65846SMarius Strobl #include <dev/mmc/mmcvar.h> 87114b4164SWarner Losh 88114b4164SWarner Losh #include "mmcbus_if.h" 89114b4164SWarner Losh 907aa65846SMarius Strobl #if __FreeBSD_version < 800002 917aa65846SMarius Strobl #define kproc_create kthread_create 927aa65846SMarius Strobl #define kproc_exit kthread_exit 937aa65846SMarius Strobl #endif 947aa65846SMarius Strobl 9572dec079SMarius Strobl #define MMCSD_CMD_RETRIES 5 9672dec079SMarius Strobl 9772dec079SMarius Strobl #define MMCSD_FMT_BOOT "mmcsd%dboot" 9872dec079SMarius Strobl #define MMCSD_FMT_GP "mmcsd%dgp" 9972dec079SMarius Strobl #define MMCSD_FMT_RPMB "mmcsd%drpmb" 10072dec079SMarius Strobl #define MMCSD_LABEL_ENH "enh" 10172dec079SMarius Strobl 10272dec079SMarius Strobl #define MMCSD_PART_NAMELEN (16 + 1) 10372dec079SMarius Strobl 10472dec079SMarius Strobl struct mmcsd_softc; 10572dec079SMarius Strobl 10672dec079SMarius Strobl struct mmcsd_part { 107aca38eabSMarius Strobl struct mtx disk_mtx; 108aca38eabSMarius Strobl struct mtx ioctl_mtx; 10972dec079SMarius Strobl struct mmcsd_softc *sc; 110114b4164SWarner Losh struct disk *disk; 111114b4164SWarner Losh struct proc *p; 112114b4164SWarner Losh struct bio_queue_head bio_queue; 113a7cf6274SAlexander Motin daddr_t eblock, eend; /* Range remaining after the last erase. */ 11472dec079SMarius Strobl u_int cnt; 11572dec079SMarius Strobl u_int type; 1160e1466acSWarner Losh int running; 117eb67f31aSAlexander Motin int suspend; 118aca38eabSMarius Strobl int ioctl; 11972dec079SMarius Strobl bool ro; 12072dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 12172dec079SMarius Strobl }; 12272dec079SMarius Strobl 12372dec079SMarius Strobl struct mmcsd_softc { 12472dec079SMarius Strobl device_t dev; 125cd85acbaSMarius Strobl device_t mmcbus; 12672dec079SMarius Strobl struct mmcsd_part *part[MMC_PART_MAX]; 12772dec079SMarius Strobl enum mmc_card_mode mode; 128aca38eabSMarius Strobl u_int max_data; /* Maximum data size [blocks] */ 129aca38eabSMarius Strobl u_int erase_sector; /* Device native erase sector size [blocks] */ 130aca38eabSMarius Strobl uint8_t high_cap; /* High Capacity device (block addressed) */ 13172dec079SMarius Strobl uint8_t part_curr; /* Partition currently switched to */ 13272dec079SMarius Strobl uint8_t ext_csd[MMC_EXTCSD_SIZE]; 13372dec079SMarius Strobl uint16_t rca; 13479f39c6aSMarius Strobl uint32_t flags; 13579f39c6aSMarius Strobl #define MMCSD_INAND_CMD38 0x0001 13679f39c6aSMarius Strobl #define MMCSD_USE_TRIM 0x0002 137646fd30cSMarius Strobl #define MMCSD_FLUSH_CACHE 0x0004 138646fd30cSMarius Strobl #define MMCSD_DIRTY 0x0008 13979f39c6aSMarius Strobl uint32_t cmd6_time; /* Generic switch timeout [us] */ 14072dec079SMarius Strobl uint32_t part_time; /* Partition switch timeout [us] */ 14172dec079SMarius Strobl off_t enh_base; /* Enhanced user data area slice base ... */ 14272dec079SMarius Strobl off_t enh_size; /* ... and size [bytes] */ 1435e3dbf8bSIan Lepore int log_count; 1445e3dbf8bSIan Lepore struct timeval log_time; 14572dec079SMarius Strobl struct cdev *rpmb_dev; 146114b4164SWarner Losh }; 147114b4164SWarner Losh 148bb1ef63fSWarner Losh static const char *errmsg[] = 149bb1ef63fSWarner Losh { 150bb1ef63fSWarner Losh "None", 151bb1ef63fSWarner Losh "Timeout", 152bb1ef63fSWarner Losh "Bad CRC", 153bb1ef63fSWarner Losh "Fifo", 154bb1ef63fSWarner Losh "Failed", 155bb1ef63fSWarner Losh "Invalid", 156bb1ef63fSWarner Losh "NO MEMORY" 157bb1ef63fSWarner Losh }; 158bb1ef63fSWarner Losh 159646fd30cSMarius Strobl static SYSCTL_NODE(_hw, OID_AUTO, mmcsd, CTLFLAG_RD, NULL, "mmcsd driver"); 160646fd30cSMarius Strobl 161646fd30cSMarius Strobl static int mmcsd_cache = 1; 162646fd30cSMarius Strobl SYSCTL_INT(_hw_mmcsd, OID_AUTO, cache, CTLFLAG_RDTUN, &mmcsd_cache, 0, 163646fd30cSMarius Strobl "Device R/W cache enabled if present"); 164646fd30cSMarius Strobl 1655e3dbf8bSIan Lepore #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 1665e3dbf8bSIan Lepore 167114b4164SWarner Losh /* bus entry points */ 168114b4164SWarner Losh static int mmcsd_attach(device_t dev); 169114b4164SWarner Losh static int mmcsd_detach(device_t dev); 170ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev); 171646fd30cSMarius Strobl static int mmcsd_shutdown(device_t dev); 172114b4164SWarner Losh 173114b4164SWarner Losh /* disk routines */ 174114b4164SWarner Losh static int mmcsd_close(struct disk *dp); 175a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 176a1fda318SAlexander Motin off_t offset, size_t length); 17772dec079SMarius Strobl static int mmcsd_getattr(struct bio *); 17872dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, 17972dec079SMarius Strobl int fflag, struct thread *td); 180ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp); 181114b4164SWarner Losh static void mmcsd_task(void *arg); 182114b4164SWarner Losh 18372dec079SMarius Strobl /* RMPB cdev interface */ 18472dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 18572dec079SMarius Strobl int fflag, struct thread *td); 186a0075e58SWarner Losh 18772dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type, 18879f39c6aSMarius Strobl const char *name, u_int cnt, off_t media_size, bool ro); 18972dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev); 19072dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp); 19179f39c6aSMarius Strobl static const char *mmcsd_errmsg(int e); 192646fd30cSMarius Strobl static int mmcsd_flush_cache(struct mmcsd_softc *sc); 19372dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, 1947217ea7cSMarius Strobl int fflag, struct thread *td); 19572dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, 19672dec079SMarius Strobl int fflag); 19772dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit); 19872dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp); 19972dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel); 20072dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider, 20172dec079SMarius Strobl struct flash_slice *slices, int *nslices); 20272dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, 20372dec079SMarius Strobl u_int part); 20472dec079SMarius Strobl 205aca38eabSMarius Strobl #define MMCSD_DISK_LOCK(_part) mtx_lock(&(_part)->disk_mtx) 206aca38eabSMarius Strobl #define MMCSD_DISK_UNLOCK(_part) mtx_unlock(&(_part)->disk_mtx) 207aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_INIT(_part) \ 208aca38eabSMarius Strobl mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF) 209aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_DESTROY(_part) mtx_destroy(&(_part)->disk_mtx); 210aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_LOCKED(_part) \ 211aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_OWNED); 212aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_UNLOCKED(_part) \ 213aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED); 214aca38eabSMarius Strobl 215aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK(_part) mtx_lock(&(_part)->ioctl_mtx) 216aca38eabSMarius Strobl #define MMCSD_IOCTL_UNLOCK(_part) mtx_unlock(&(_part)->ioctl_mtx) 217aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_INIT(_part) \ 218aca38eabSMarius Strobl mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF) 219aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_DESTROY(_part) mtx_destroy(&(_part)->ioctl_mtx); 220aca38eabSMarius Strobl #define MMCSD_IOCTL_ASSERT_LOCKED(_part) \ 221aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_OWNED); 222aca38eabSMarius Strobl #define MMCSD_IOCLT_ASSERT_UNLOCKED(_part) \ 223aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED); 224114b4164SWarner Losh 225114b4164SWarner Losh static int 226114b4164SWarner Losh mmcsd_probe(device_t dev) 227114b4164SWarner Losh { 228114b4164SWarner Losh 229c18f1e26SAlexander Motin device_quiet(dev); 2300c0903b1SWarner Losh device_set_desc(dev, "MMC/SD Memory Card"); 231114b4164SWarner Losh return (0); 232114b4164SWarner Losh } 233114b4164SWarner Losh 234114b4164SWarner Losh static int 235114b4164SWarner Losh mmcsd_attach(device_t dev) 236114b4164SWarner Losh { 237cd85acbaSMarius Strobl device_t mmcbus; 238114b4164SWarner Losh struct mmcsd_softc *sc; 23972dec079SMarius Strobl const uint8_t *ext_csd; 24072dec079SMarius Strobl off_t erase_size, sector_size, size, wp_size; 24172dec079SMarius Strobl uintmax_t bytes; 24272dec079SMarius Strobl int err, i; 24379f39c6aSMarius Strobl uint32_t quirks; 24472dec079SMarius Strobl uint8_t rev; 24572dec079SMarius Strobl bool comp, ro; 24672dec079SMarius Strobl char unit[2]; 247114b4164SWarner Losh 248114b4164SWarner Losh sc = device_get_softc(dev); 249114b4164SWarner Losh sc->dev = dev; 250cd85acbaSMarius Strobl sc->mmcbus = mmcbus = device_get_parent(dev); 251608226d5SMarius Strobl sc->mode = mmc_get_card_type(dev); 252aca38eabSMarius Strobl /* 253aca38eabSMarius Strobl * Note that in principle with an SDHCI-like re-tuning implementation, 254aca38eabSMarius Strobl * the maximum data size can change at runtime due to a device removal/ 255aca38eabSMarius Strobl * insertion that results in switches to/from a transfer mode involving 256aca38eabSMarius Strobl * re-tuning, iff there are multiple devices on a given bus. Until now 257aca38eabSMarius Strobl * mmc(4) lacks support for rescanning already attached buses, however, 25821794534SMarius Strobl * and sdhci(4) to date has no support for shared buses in the first 259aca38eabSMarius Strobl * place either. 260aca38eabSMarius Strobl */ 261aca38eabSMarius Strobl sc->max_data = mmc_get_max_data(dev); 262aca38eabSMarius Strobl sc->high_cap = mmc_get_high_cap(dev); 26372dec079SMarius Strobl sc->rca = mmc_get_rca(dev); 26479f39c6aSMarius Strobl sc->cmd6_time = mmc_get_cmd6_timeout(dev); 26579f39c6aSMarius Strobl quirks = mmc_get_quirks(dev); 266114b4164SWarner Losh 26772dec079SMarius Strobl /* Only MMC >= 4.x devices support EXT_CSD. */ 26872dec079SMarius Strobl if (mmc_get_spec_vers(dev) >= 4) { 269cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 270cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 271cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 27279f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 27379f39c6aSMarius Strobl device_printf(dev, "Error reading EXT_CSD %s\n", 27479f39c6aSMarius Strobl mmcsd_errmsg(err)); 27579f39c6aSMarius Strobl return (ENXIO); 27679f39c6aSMarius Strobl } 27772dec079SMarius Strobl } 27872dec079SMarius Strobl ext_csd = sc->ext_csd; 27972dec079SMarius Strobl 28079f39c6aSMarius Strobl if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) { 28179f39c6aSMarius Strobl if (mmc_get_spec_vers(dev) < 4) { 28279f39c6aSMarius Strobl device_printf(dev, 28379f39c6aSMarius Strobl "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n"); 28479f39c6aSMarius Strobl return (EINVAL); 28579f39c6aSMarius Strobl } 28679f39c6aSMarius Strobl sc->flags |= MMCSD_INAND_CMD38; 28779f39c6aSMarius Strobl } 28879f39c6aSMarius Strobl 28979f39c6aSMarius Strobl /* 29079f39c6aSMarius Strobl * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both 29179f39c6aSMarius Strobl * insecure and secure TRIM. 29279f39c6aSMarius Strobl */ 29379f39c6aSMarius Strobl if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] & 29479f39c6aSMarius Strobl EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 && 29579f39c6aSMarius Strobl (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) { 29679f39c6aSMarius Strobl if (bootverbose) 29779f39c6aSMarius Strobl device_printf(dev, "taking advantage of TRIM\n"); 29879f39c6aSMarius Strobl sc->flags |= MMCSD_USE_TRIM; 29979f39c6aSMarius Strobl sc->erase_sector = 1; 30079f39c6aSMarius Strobl } else 30179f39c6aSMarius Strobl sc->erase_sector = mmc_get_erase_sector(dev); 30279f39c6aSMarius Strobl 30372dec079SMarius Strobl /* 30472dec079SMarius Strobl * Enhanced user data area and general purpose partitions are only 30572dec079SMarius Strobl * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB 30672dec079SMarius Strobl * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later. 30772dec079SMarius Strobl */ 30872dec079SMarius Strobl rev = ext_csd[EXT_CSD_REV]; 30972dec079SMarius Strobl 31072dec079SMarius Strobl /* 311646fd30cSMarius Strobl * With revision 1.5 (MMC v4.5, EXT_CSD_REV == 6) and later, take 312646fd30cSMarius Strobl * advantage of the device R/W cache if present and useage is not 313646fd30cSMarius Strobl * disabled. 314646fd30cSMarius Strobl */ 315646fd30cSMarius Strobl if (rev >= 6 && mmcsd_cache != 0) { 316602a05b0SMarius Strobl size = le32dec(&ext_csd[EXT_CSD_CACHE_SIZE]); 317646fd30cSMarius Strobl if (bootverbose) 318646fd30cSMarius Strobl device_printf(dev, "cache size %juKB\n", size); 319646fd30cSMarius Strobl if (size > 0) { 320646fd30cSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 321646fd30cSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, 322646fd30cSMarius Strobl EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CACHE_CTRL, 323646fd30cSMarius Strobl EXT_CSD_CACHE_CTRL_CACHE_EN, sc->cmd6_time, true); 324646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 325646fd30cSMarius Strobl if (err != MMC_ERR_NONE) 326646fd30cSMarius Strobl device_printf(dev, "failed to enable cache\n"); 327646fd30cSMarius Strobl else 328646fd30cSMarius Strobl sc->flags |= MMCSD_FLUSH_CACHE; 329646fd30cSMarius Strobl } 330646fd30cSMarius Strobl } 331646fd30cSMarius Strobl 332646fd30cSMarius Strobl /* 33372dec079SMarius Strobl * Ignore user-creatable enhanced user data area and general purpose 33472dec079SMarius Strobl * partitions partitions as long as partitioning hasn't been finished. 33572dec079SMarius Strobl */ 33672dec079SMarius Strobl comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0; 33772dec079SMarius Strobl 33872dec079SMarius Strobl /* 33972dec079SMarius Strobl * Add enhanced user data area slice, unless it spans the entirety of 34072dec079SMarius Strobl * the user data area. The enhanced area is of a multiple of high 34172dec079SMarius Strobl * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) * 34272dec079SMarius Strobl * 512 KB) and its offset given in either sectors or bytes, depending 34372dec079SMarius Strobl * on whether it's a high capacity device or not. 34472dec079SMarius Strobl * NB: The slicer and its slices need to be registered before adding 34572dec079SMarius Strobl * the disk for the corresponding user data area as re-tasting is 34672dec079SMarius Strobl * racy. 34772dec079SMarius Strobl */ 34872dec079SMarius Strobl sector_size = mmc_get_sector_size(dev); 34972dec079SMarius Strobl size = ext_csd[EXT_CSD_ENH_SIZE_MULT] + 35072dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + 35172dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16); 35272dec079SMarius Strobl if (rev >= 4 && comp == TRUE && size > 0 && 35372dec079SMarius Strobl (ext_csd[EXT_CSD_PART_SUPPORT] & 35472dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 35572dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) { 35672dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 35772dec079SMarius Strobl MMC_SECTOR_SIZE; 35872dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 35972dec079SMarius Strobl size *= erase_size * wp_size; 36072dec079SMarius Strobl if (size != mmc_get_media_size(dev) * sector_size) { 36172dec079SMarius Strobl sc->enh_size = size; 362602a05b0SMarius Strobl sc->enh_base = 363602a05b0SMarius Strobl le32dec(&ext_csd[EXT_CSD_ENH_START_ADDR]) * 364da979d44SMarius Strobl (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1); 36572dec079SMarius Strobl } else if (bootverbose) 36672dec079SMarius Strobl device_printf(dev, 36772dec079SMarius Strobl "enhanced user data area spans entire device\n"); 36872dec079SMarius Strobl } 36972dec079SMarius Strobl 37072dec079SMarius Strobl /* 37172dec079SMarius Strobl * Add default partition. This may be the only one or the user 37272dec079SMarius Strobl * data area in case partitions are supported. 37372dec079SMarius Strobl */ 37472dec079SMarius Strobl ro = mmc_get_read_only(dev); 37572dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd", 37679f39c6aSMarius Strobl device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro); 37772dec079SMarius Strobl 37872dec079SMarius Strobl if (mmc_get_spec_vers(dev) < 3) 37972dec079SMarius Strobl return (0); 38072dec079SMarius Strobl 38172dec079SMarius Strobl /* Belatedly announce enhanced user data slice. */ 38272dec079SMarius Strobl if (sc->enh_size != 0) { 38372dec079SMarius Strobl bytes = mmcsd_pretty_size(size, unit); 38472dec079SMarius Strobl printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area " 38572dec079SMarius Strobl "slice offset 0x%jx at %s\n", device_get_nameunit(dev), 38672dec079SMarius Strobl MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base, 38772dec079SMarius Strobl device_get_nameunit(dev)); 38872dec079SMarius Strobl } 38972dec079SMarius Strobl 39072dec079SMarius Strobl /* 39172dec079SMarius Strobl * Determine partition switch timeout (provided in units of 10 ms) 39272dec079SMarius Strobl * and ensure it's at least 300 ms as some eMMC chips lie. 39372dec079SMarius Strobl */ 39472dec079SMarius Strobl sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000, 39572dec079SMarius Strobl 300 * 1000); 39672dec079SMarius Strobl 39772dec079SMarius Strobl /* Add boot partitions, which are of a fixed multiple of 128 KB. */ 39872dec079SMarius Strobl size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 399cd85acbaSMarius Strobl if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) { 40072dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0, 40179f39c6aSMarius Strobl MMCSD_FMT_BOOT, 0, size, 40272dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 40372dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0)); 40472dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1, 40579f39c6aSMarius Strobl MMCSD_FMT_BOOT, 1, size, 40672dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 40772dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0)); 40872dec079SMarius Strobl } 40972dec079SMarius Strobl 41072dec079SMarius Strobl /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */ 41172dec079SMarius Strobl size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 41272dec079SMarius Strobl if (rev >= 5 && size > 0) 41372dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB, 41479f39c6aSMarius Strobl MMCSD_FMT_RPMB, 0, size, ro); 41572dec079SMarius Strobl 41672dec079SMarius Strobl if (rev <= 3 || comp == FALSE) 41772dec079SMarius Strobl return (0); 41872dec079SMarius Strobl 41972dec079SMarius Strobl /* 42072dec079SMarius Strobl * Add general purpose partitions, which are of a multiple of high 42172dec079SMarius Strobl * capacity write protect groups, too. 42272dec079SMarius Strobl */ 42372dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) { 42472dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 42572dec079SMarius Strobl MMC_SECTOR_SIZE; 42672dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 42772dec079SMarius Strobl for (i = 0; i < MMC_PART_GP_MAX; i++) { 42872dec079SMarius Strobl size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] + 42972dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) + 43072dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16); 43172dec079SMarius Strobl if (size == 0) 43272dec079SMarius Strobl continue; 43372dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i, 43479f39c6aSMarius Strobl MMCSD_FMT_GP, i, size * erase_size * wp_size, ro); 43572dec079SMarius Strobl } 43672dec079SMarius Strobl } 43772dec079SMarius Strobl return (0); 43872dec079SMarius Strobl } 43972dec079SMarius Strobl 44072dec079SMarius Strobl static uintmax_t 44172dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit) 44272dec079SMarius Strobl { 44372dec079SMarius Strobl uintmax_t bytes; 44472dec079SMarius Strobl int i; 44572dec079SMarius Strobl 44672dec079SMarius Strobl /* 44772dec079SMarius Strobl * Display in most natural units. There's no card < 1MB. However, 44872dec079SMarius Strobl * RPMB partitions occasionally are smaller than that, though. The 44972dec079SMarius Strobl * SD standard goes to 2 GiB due to its reliance on FAT, but the data 45072dec079SMarius Strobl * format supports up to 4 GiB and some card makers push it up to this 45172dec079SMarius Strobl * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the 45272dec079SMarius Strobl * data format supports up to 2 TiB however. 2048 GB isn't too ugly, 45372dec079SMarius Strobl * so we note it in passing here and don't add the code to print TB). 45472dec079SMarius Strobl * Since these cards are sold in terms of MB and GB not MiB and GiB, 45572dec079SMarius Strobl * report them like that. We also round to the nearest unit, since 45672dec079SMarius Strobl * many cards are a few percent short, even of the power of 10 size. 45772dec079SMarius Strobl */ 45872dec079SMarius Strobl bytes = size; 45972dec079SMarius Strobl unit[0] = unit[1] = '\0'; 46072dec079SMarius Strobl for (i = 0; i <= 2 && bytes >= 1000; i++) { 46172dec079SMarius Strobl bytes = (bytes + 1000 / 2 - 1) / 1000; 46272dec079SMarius Strobl switch (i) { 46372dec079SMarius Strobl case 0: 46472dec079SMarius Strobl unit[0] = 'k'; 46572dec079SMarius Strobl break; 46672dec079SMarius Strobl case 1: 46772dec079SMarius Strobl unit[0] = 'M'; 46872dec079SMarius Strobl break; 46972dec079SMarius Strobl case 2: 47072dec079SMarius Strobl unit[0] = 'G'; 47172dec079SMarius Strobl break; 47272dec079SMarius Strobl default: 47372dec079SMarius Strobl break; 47472dec079SMarius Strobl } 47572dec079SMarius Strobl } 47672dec079SMarius Strobl return (bytes); 47772dec079SMarius Strobl } 47872dec079SMarius Strobl 47972dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = { 48072dec079SMarius Strobl .d_version = D_VERSION, 48172dec079SMarius Strobl .d_name = "mmcsdrpmb", 48272dec079SMarius Strobl .d_ioctl = mmcsd_ioctl_rpmb 48372dec079SMarius Strobl }; 48472dec079SMarius Strobl 48572dec079SMarius Strobl static void 48672dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt, 48779f39c6aSMarius Strobl off_t media_size, bool ro) 48872dec079SMarius Strobl { 48972dec079SMarius Strobl struct make_dev_args args; 490cd85acbaSMarius Strobl device_t dev, mmcbus; 49172dec079SMarius Strobl const char *ext; 49272dec079SMarius Strobl const uint8_t *ext_csd; 49372dec079SMarius Strobl struct mmcsd_part *part; 49472dec079SMarius Strobl struct disk *d; 49572dec079SMarius Strobl uintmax_t bytes; 49672dec079SMarius Strobl u_int gp; 49772dec079SMarius Strobl uint32_t speed; 49872dec079SMarius Strobl uint8_t extattr; 49972dec079SMarius Strobl bool enh; 50072dec079SMarius Strobl char unit[2]; 50172dec079SMarius Strobl 50272dec079SMarius Strobl dev = sc->dev; 503cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 50472dec079SMarius Strobl part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF, 50572dec079SMarius Strobl M_WAITOK | M_ZERO); 50672dec079SMarius Strobl part->sc = sc; 50772dec079SMarius Strobl part->cnt = cnt; 50872dec079SMarius Strobl part->type = type; 50972dec079SMarius Strobl part->ro = ro; 51072dec079SMarius Strobl snprintf(part->name, sizeof(part->name), name, device_get_unit(dev)); 51172dec079SMarius Strobl 512aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_INIT(part); 513aca38eabSMarius Strobl 514aca38eabSMarius Strobl /* 515aca38eabSMarius Strobl * For the RPMB partition, allow IOCTL access only. 516aca38eabSMarius Strobl * NB: If ever attaching RPMB partitions to disk(9), the re-tuning 517aca38eabSMarius Strobl * implementation and especially its pausing need to be revisited, 518aca38eabSMarius Strobl * because then re-tuning requests may be issued by the IOCTL half 519aca38eabSMarius Strobl * of this driver while re-tuning is already paused by the disk(9) 520aca38eabSMarius Strobl * one and vice versa. 521aca38eabSMarius Strobl */ 52272dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 52372dec079SMarius Strobl make_dev_args_init(&args); 52472dec079SMarius Strobl args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 52572dec079SMarius Strobl args.mda_devsw = &mmcsd_rpmb_cdevsw; 52672dec079SMarius Strobl args.mda_uid = UID_ROOT; 52772dec079SMarius Strobl args.mda_gid = GID_OPERATOR; 52872dec079SMarius Strobl args.mda_mode = 0640; 52972dec079SMarius Strobl args.mda_si_drv1 = part; 53072dec079SMarius Strobl if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) { 53172dec079SMarius Strobl device_printf(dev, "Failed to make RPMB device\n"); 53272dec079SMarius Strobl free(part, M_DEVBUF); 53372dec079SMarius Strobl return; 53472dec079SMarius Strobl } 53572dec079SMarius Strobl } else { 536aca38eabSMarius Strobl MMCSD_DISK_LOCK_INIT(part); 53772dec079SMarius Strobl 53872dec079SMarius Strobl d = part->disk = disk_alloc(); 539a0075e58SWarner Losh d->d_close = mmcsd_close; 540a0075e58SWarner Losh d->d_strategy = mmcsd_strategy; 54172dec079SMarius Strobl d->d_ioctl = mmcsd_ioctl_disk; 542a1fda318SAlexander Motin d->d_dump = mmcsd_dump; 54372dec079SMarius Strobl d->d_getattr = mmcsd_getattr; 54472dec079SMarius Strobl d->d_name = part->name; 54572dec079SMarius Strobl d->d_drv1 = part; 546a0075e58SWarner Losh d->d_sectorsize = mmc_get_sector_size(dev); 547aca38eabSMarius Strobl d->d_maxsize = sc->max_data * d->d_sectorsize; 54872dec079SMarius Strobl d->d_mediasize = media_size; 54979f39c6aSMarius Strobl d->d_stripesize = sc->erase_sector * d->d_sectorsize; 55072dec079SMarius Strobl d->d_unit = cnt; 5513906d42dSAlexander Motin d->d_flags = DISKFLAG_CANDELETE; 552646fd30cSMarius Strobl if ((sc->flags & MMCSD_FLUSH_CACHE) != 0) 553646fd30cSMarius Strobl d->d_flags |= DISKFLAG_CANFLUSHCACHE; 55479f39c6aSMarius Strobl d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize; 55572dec079SMarius Strobl strlcpy(d->d_ident, mmc_get_card_sn_string(dev), 55672dec079SMarius Strobl sizeof(d->d_ident)); 55772dec079SMarius Strobl strlcpy(d->d_descr, mmc_get_card_id_string(dev), 55872dec079SMarius Strobl sizeof(d->d_descr)); 55917160457SAlexander Motin d->d_rotation_rate = DISK_RR_NON_ROTATING; 560f3a4b7f7SIan Lepore 561a0075e58SWarner Losh disk_create(d, DISK_VERSION); 56272dec079SMarius Strobl bioq_init(&part->bio_queue); 5630e1466acSWarner Losh 56472dec079SMarius Strobl part->running = 1; 56572dec079SMarius Strobl kproc_create(&mmcsd_task, part, &part->p, 0, 0, 56672dec079SMarius Strobl "%s%d: mmc/sd card", part->name, cnt); 56772dec079SMarius Strobl } 56872dec079SMarius Strobl 56972dec079SMarius Strobl bytes = mmcsd_pretty_size(media_size, unit); 57072dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) { 571cd85acbaSMarius Strobl speed = mmcbr_get_clock(mmcbus); 57272dec079SMarius Strobl printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 57372dec079SMarius Strobl part->name, cnt, bytes, unit, mmc_get_card_id_string(dev), 574cd85acbaSMarius Strobl ro ? " (read-only)" : "", device_get_nameunit(mmcbus), 57572dec079SMarius Strobl speed / 1000000, (speed / 100000) % 10, 576aca38eabSMarius Strobl mmcsd_bus_bit_width(dev), sc->max_data); 57772dec079SMarius Strobl } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 57839a289c3SIan Lepore printf("%s: %ju%sB partition %d%s at %s\n", part->name, bytes, 57972dec079SMarius Strobl unit, type, ro ? " (read-only)" : "", 58088e07d92SRui Paulo device_get_nameunit(dev)); 58172dec079SMarius Strobl } else { 58272dec079SMarius Strobl enh = false; 58372dec079SMarius Strobl ext = NULL; 58472dec079SMarius Strobl extattr = 0; 58572dec079SMarius Strobl if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 && 58672dec079SMarius Strobl type <= EXT_CSD_PART_CONFIG_ACC_GP3) { 58772dec079SMarius Strobl ext_csd = sc->ext_csd; 58872dec079SMarius Strobl gp = type - EXT_CSD_PART_CONFIG_ACC_GP0; 58972dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & 59072dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 59172dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & 59272dec079SMarius Strobl (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0) 59372dec079SMarius Strobl enh = true; 59472dec079SMarius Strobl else if ((ext_csd[EXT_CSD_PART_SUPPORT] & 59572dec079SMarius Strobl EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) { 59672dec079SMarius Strobl extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR + 59772dec079SMarius Strobl (gp / 2)] >> (4 * (gp % 2))) & 0xF; 59872dec079SMarius Strobl switch (extattr) { 59972dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_DEFAULT: 60072dec079SMarius Strobl break; 60172dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE: 60272dec079SMarius Strobl ext = "system code"; 60372dec079SMarius Strobl break; 60472dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_NPERSISTENT: 60572dec079SMarius Strobl ext = "non-persistent"; 60672dec079SMarius Strobl break; 60772dec079SMarius Strobl default: 60872dec079SMarius Strobl ext = "reserved"; 60972dec079SMarius Strobl break; 61072dec079SMarius Strobl } 61172dec079SMarius Strobl } 61272dec079SMarius Strobl } 61372dec079SMarius Strobl if (ext == NULL) 61439a289c3SIan Lepore printf("%s%d: %ju%sB partition %d%s%s at %s\n", 61572dec079SMarius Strobl part->name, cnt, bytes, unit, type, enh ? 61672dec079SMarius Strobl " enhanced" : "", ro ? " (read-only)" : "", 61772dec079SMarius Strobl device_get_nameunit(dev)); 61872dec079SMarius Strobl else 61939a289c3SIan Lepore printf("%s%d: %ju%sB partition %d extended 0x%x " 62072dec079SMarius Strobl "(%s)%s at %s\n", part->name, cnt, bytes, unit, 62172dec079SMarius Strobl type, extattr, ext, ro ? " (read-only)" : "", 62272dec079SMarius Strobl device_get_nameunit(dev)); 62372dec079SMarius Strobl } 62472dec079SMarius Strobl } 625114b4164SWarner Losh 62672dec079SMarius Strobl static int 62772dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider, 62872dec079SMarius Strobl struct flash_slice *slices, int *nslices) 62972dec079SMarius Strobl { 63072dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 63172dec079SMarius Strobl struct mmcsd_softc *sc; 63272dec079SMarius Strobl struct mmcsd_part *part; 63372dec079SMarius Strobl 63472dec079SMarius Strobl *nslices = 0; 63572dec079SMarius Strobl if (slices == NULL) 63672dec079SMarius Strobl return (ENOMEM); 63772dec079SMarius Strobl 63872dec079SMarius Strobl sc = device_get_softc(dev); 63972dec079SMarius Strobl if (sc->enh_size == 0) 64072dec079SMarius Strobl return (ENXIO); 64172dec079SMarius Strobl 64272dec079SMarius Strobl part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT]; 64372dec079SMarius Strobl snprintf(name, sizeof(name), "%s%d", part->disk->d_name, 64472dec079SMarius Strobl part->disk->d_unit); 64572dec079SMarius Strobl if (strcmp(name, provider) != 0) 64672dec079SMarius Strobl return (ENXIO); 64772dec079SMarius Strobl 64872dec079SMarius Strobl *nslices = 1; 64972dec079SMarius Strobl slices[0].base = sc->enh_base; 65072dec079SMarius Strobl slices[0].size = sc->enh_size; 65172dec079SMarius Strobl slices[0].label = MMCSD_LABEL_ENH; 652114b4164SWarner Losh return (0); 653114b4164SWarner Losh } 654114b4164SWarner Losh 655114b4164SWarner Losh static int 656114b4164SWarner Losh mmcsd_detach(device_t dev) 657114b4164SWarner Losh { 6580e1466acSWarner Losh struct mmcsd_softc *sc = device_get_softc(dev); 65972dec079SMarius Strobl struct mmcsd_part *part; 66072dec079SMarius Strobl int i; 6610e1466acSWarner Losh 66272dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 66372dec079SMarius Strobl part = sc->part[i]; 664aca38eabSMarius Strobl if (part != NULL) { 665aca38eabSMarius Strobl if (part->disk != NULL) { 666aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 66772dec079SMarius Strobl part->suspend = 0; 66872dec079SMarius Strobl if (part->running > 0) { 669eb67f31aSAlexander Motin /* kill thread */ 67072dec079SMarius Strobl part->running = 0; 67172dec079SMarius Strobl wakeup(part); 672eb67f31aSAlexander Motin /* wait for thread to finish. */ 67372dec079SMarius Strobl while (part->running != -1) 674aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 675aca38eabSMarius Strobl "mmcsd disk detach", 0); 676eb67f31aSAlexander Motin } 677aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 678aca38eabSMarius Strobl } 679aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 680aca38eabSMarius Strobl while (part->ioctl > 0) 681aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 682aca38eabSMarius Strobl "mmcsd IOCTL detach", 0); 683aca38eabSMarius Strobl part->ioctl = -1; 684aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 68572dec079SMarius Strobl } 68672dec079SMarius Strobl } 6870e1466acSWarner Losh 68872dec079SMarius Strobl if (sc->rpmb_dev != NULL) 68972dec079SMarius Strobl destroy_dev(sc->rpmb_dev); 69072dec079SMarius Strobl 69172dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 69272dec079SMarius Strobl part = sc->part[i]; 69372dec079SMarius Strobl if (part != NULL) { 69472dec079SMarius Strobl if (part->disk != NULL) { 695249b0e85SAlexander Motin /* Flush the request queue. */ 69672dec079SMarius Strobl bioq_flush(&part->bio_queue, NULL, ENXIO); 6970e1466acSWarner Losh /* kill disk */ 69872dec079SMarius Strobl disk_destroy(part->disk); 6990e1466acSWarner Losh 700aca38eabSMarius Strobl MMCSD_DISK_LOCK_DESTROY(part); 70172dec079SMarius Strobl } 702aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_DESTROY(part); 70372dec079SMarius Strobl free(part, M_DEVBUF); 70472dec079SMarius Strobl } 70572dec079SMarius Strobl } 706646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 707646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 708646fd30cSMarius Strobl return (0); 709646fd30cSMarius Strobl } 710646fd30cSMarius Strobl 711646fd30cSMarius Strobl static int 712646fd30cSMarius Strobl mmcsd_shutdown(device_t dev) 713646fd30cSMarius Strobl { 714646fd30cSMarius Strobl struct mmcsd_softc *sc = device_get_softc(dev); 715646fd30cSMarius Strobl 716646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 717646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 718128d0ff2SWarner Losh return (0); 719114b4164SWarner Losh } 720114b4164SWarner Losh 721114b4164SWarner Losh static int 722eb67f31aSAlexander Motin mmcsd_suspend(device_t dev) 723eb67f31aSAlexander Motin { 724eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 72572dec079SMarius Strobl struct mmcsd_part *part; 72672dec079SMarius Strobl int i; 727eb67f31aSAlexander Motin 72872dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 72972dec079SMarius Strobl part = sc->part[i]; 730aca38eabSMarius Strobl if (part != NULL) { 731aca38eabSMarius Strobl if (part->disk != NULL) { 732aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 73372dec079SMarius Strobl part->suspend = 1; 73472dec079SMarius Strobl if (part->running > 0) { 735eb67f31aSAlexander Motin /* kill thread */ 73672dec079SMarius Strobl part->running = 0; 73772dec079SMarius Strobl wakeup(part); 738eb67f31aSAlexander Motin /* wait for thread to finish. */ 73972dec079SMarius Strobl while (part->running != -1) 740aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 741aca38eabSMarius Strobl "mmcsd disk suspension", 0); 742eb67f31aSAlexander Motin } 743aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 744aca38eabSMarius Strobl } 745aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 746aca38eabSMarius Strobl while (part->ioctl > 0) 747aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 748aca38eabSMarius Strobl "mmcsd IOCTL suspension", 0); 749aca38eabSMarius Strobl part->ioctl = -1; 750aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 75172dec079SMarius Strobl } 75272dec079SMarius Strobl } 753646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 754646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 755eb67f31aSAlexander Motin return (0); 756eb67f31aSAlexander Motin } 757eb67f31aSAlexander Motin 758eb67f31aSAlexander Motin static int 759eb67f31aSAlexander Motin mmcsd_resume(device_t dev) 760eb67f31aSAlexander Motin { 761eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 76272dec079SMarius Strobl struct mmcsd_part *part; 76372dec079SMarius Strobl int i; 764eb67f31aSAlexander Motin 76572dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 76672dec079SMarius Strobl part = sc->part[i]; 767aca38eabSMarius Strobl if (part != NULL) { 768aca38eabSMarius Strobl if (part->disk != NULL) { 769aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 77072dec079SMarius Strobl part->suspend = 0; 77172dec079SMarius Strobl if (part->running <= 0) { 77272dec079SMarius Strobl part->running = 1; 773aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 774aca38eabSMarius Strobl kproc_create(&mmcsd_task, part, 775aca38eabSMarius Strobl &part->p, 0, 0, "%s%d: mmc/sd card", 776aca38eabSMarius Strobl part->name, part->cnt); 777eb67f31aSAlexander Motin } else 778aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 779aca38eabSMarius Strobl } 780aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 781aca38eabSMarius Strobl part->ioctl = 0; 782aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 78372dec079SMarius Strobl } 78472dec079SMarius Strobl } 785eb67f31aSAlexander Motin return (0); 786eb67f31aSAlexander Motin } 787eb67f31aSAlexander Motin 788eb67f31aSAlexander Motin static int 789646fd30cSMarius Strobl mmcsd_close(struct disk *dp) 790114b4164SWarner Losh { 791646fd30cSMarius Strobl struct mmcsd_softc *sc; 792ae5d8757SMarius Strobl 793646fd30cSMarius Strobl if ((dp->d_flags & DISKFLAG_OPEN) != 0) { 794646fd30cSMarius Strobl sc = ((struct mmcsd_part *)dp->d_drv1)->sc; 795646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 796646fd30cSMarius Strobl device_printf(sc->dev, "failed to flush cache\n"); 797114b4164SWarner Losh } 798128d0ff2SWarner Losh return (0); 799114b4164SWarner Losh } 800114b4164SWarner Losh 801114b4164SWarner Losh static void 802114b4164SWarner Losh mmcsd_strategy(struct bio *bp) 803114b4164SWarner Losh { 80472dec079SMarius Strobl struct mmcsd_part *part; 805114b4164SWarner Losh 80672dec079SMarius Strobl part = bp->bio_disk->d_drv1; 807aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 80872dec079SMarius Strobl if (part->running > 0 || part->suspend > 0) { 80972dec079SMarius Strobl bioq_disksort(&part->bio_queue, bp); 810aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 81172dec079SMarius Strobl wakeup(part); 812249b0e85SAlexander Motin } else { 813aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 814249b0e85SAlexander Motin biofinish(bp, NULL, ENXIO); 815249b0e85SAlexander Motin } 816114b4164SWarner Losh } 817114b4164SWarner Losh 81872dec079SMarius Strobl static int 81972dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 8207217ea7cSMarius Strobl int fflag, struct thread *td) 82172dec079SMarius Strobl { 82272dec079SMarius Strobl 8237217ea7cSMarius Strobl return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag, td)); 82472dec079SMarius Strobl } 82572dec079SMarius Strobl 82672dec079SMarius Strobl static int 82772dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag, 8287217ea7cSMarius Strobl struct thread *td) 82972dec079SMarius Strobl { 83072dec079SMarius Strobl 8317217ea7cSMarius Strobl return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag, td)); 83272dec079SMarius Strobl } 83372dec079SMarius Strobl 83472dec079SMarius Strobl static int 8357217ea7cSMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag, 8367217ea7cSMarius Strobl struct thread *td) 83772dec079SMarius Strobl { 83872dec079SMarius Strobl struct mmc_ioc_cmd *mic; 83972dec079SMarius Strobl struct mmc_ioc_multi_cmd *mimc; 84072dec079SMarius Strobl int i, err; 84172dec079SMarius Strobl u_long cnt, size; 84272dec079SMarius Strobl 84372dec079SMarius Strobl if ((fflag & FREAD) == 0) 84472dec079SMarius Strobl return (EBADF); 84572dec079SMarius Strobl 8467217ea7cSMarius Strobl err = priv_check(td, PRIV_DRIVER); 8477217ea7cSMarius Strobl if (err != 0) 8487217ea7cSMarius Strobl return (err); 8497217ea7cSMarius Strobl 85072dec079SMarius Strobl err = 0; 85172dec079SMarius Strobl switch (cmd) { 85272dec079SMarius Strobl case MMC_IOC_CMD: 85372dec079SMarius Strobl mic = data; 854aca38eabSMarius Strobl err = mmcsd_ioctl_cmd(part, mic, fflag); 85572dec079SMarius Strobl break; 856aca38eabSMarius Strobl case MMC_IOC_MULTI_CMD: 85772dec079SMarius Strobl mimc = data; 85872dec079SMarius Strobl if (mimc->num_of_cmds == 0) 85972dec079SMarius Strobl break; 86072dec079SMarius Strobl if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS) 86172dec079SMarius Strobl return (EINVAL); 86272dec079SMarius Strobl cnt = mimc->num_of_cmds; 86372dec079SMarius Strobl size = sizeof(*mic) * cnt; 86472dec079SMarius Strobl mic = malloc(size, M_TEMP, M_WAITOK); 86572dec079SMarius Strobl err = copyin((const void *)mimc->cmds, mic, size); 866aca38eabSMarius Strobl if (err == 0) { 86772dec079SMarius Strobl for (i = 0; i < cnt; i++) { 86872dec079SMarius Strobl err = mmcsd_ioctl_cmd(part, &mic[i], fflag); 86972dec079SMarius Strobl if (err != 0) 87072dec079SMarius Strobl break; 87172dec079SMarius Strobl } 872aca38eabSMarius Strobl } 87372dec079SMarius Strobl free(mic, M_TEMP); 87472dec079SMarius Strobl break; 87572dec079SMarius Strobl default: 87672dec079SMarius Strobl return (ENOIOCTL); 87772dec079SMarius Strobl } 87872dec079SMarius Strobl return (err); 87972dec079SMarius Strobl } 88072dec079SMarius Strobl 88172dec079SMarius Strobl static int 88272dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag) 88372dec079SMarius Strobl { 88472dec079SMarius Strobl struct mmc_command cmd; 88572dec079SMarius Strobl struct mmc_data data; 88672dec079SMarius Strobl struct mmcsd_softc *sc; 887cd85acbaSMarius Strobl device_t dev, mmcbus; 88872dec079SMarius Strobl void *dp; 88972dec079SMarius Strobl u_long len; 89072dec079SMarius Strobl int err, retries; 89172dec079SMarius Strobl uint32_t status; 89272dec079SMarius Strobl uint16_t rca; 89372dec079SMarius Strobl 89472dec079SMarius Strobl if ((fflag & FWRITE) == 0 && mic->write_flag != 0) 89572dec079SMarius Strobl return (EBADF); 89672dec079SMarius Strobl 89772dec079SMarius Strobl if (part->ro == TRUE && mic->write_flag != 0) 89872dec079SMarius Strobl return (EROFS); 89972dec079SMarius Strobl 900aca38eabSMarius Strobl /* 901aca38eabSMarius Strobl * We don't need to explicitly lock against the disk(9) half of this 902aca38eabSMarius Strobl * driver as MMCBUS_ACQUIRE_BUS() will serialize us. However, it's 903aca38eabSMarius Strobl * necessary to protect against races with detachment and suspension, 904aca38eabSMarius Strobl * especially since it's required to switch away from RPMB partitions 905aca38eabSMarius Strobl * again after an access (see mmcsd_switch_part()). 906aca38eabSMarius Strobl */ 907aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 908aca38eabSMarius Strobl while (part->ioctl != 0) { 909aca38eabSMarius Strobl if (part->ioctl < 0) { 910aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 911aca38eabSMarius Strobl return (ENXIO); 912aca38eabSMarius Strobl } 913aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0); 914aca38eabSMarius Strobl } 915aca38eabSMarius Strobl part->ioctl = 1; 916aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 917aca38eabSMarius Strobl 91872dec079SMarius Strobl err = 0; 91972dec079SMarius Strobl dp = NULL; 92072dec079SMarius Strobl len = mic->blksz * mic->blocks; 921aca38eabSMarius Strobl if (len > MMC_IOC_MAX_BYTES) { 922aca38eabSMarius Strobl err = EOVERFLOW; 923aca38eabSMarius Strobl goto out; 924aca38eabSMarius Strobl } 92572dec079SMarius Strobl if (len != 0) { 92672dec079SMarius Strobl dp = malloc(len, M_TEMP, M_WAITOK); 92772dec079SMarius Strobl err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len); 92872dec079SMarius Strobl if (err != 0) 92972dec079SMarius Strobl goto out; 93072dec079SMarius Strobl } 93172dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 93272dec079SMarius Strobl memset(&data, 0, sizeof(data)); 93372dec079SMarius Strobl cmd.opcode = mic->opcode; 93472dec079SMarius Strobl cmd.arg = mic->arg; 93572dec079SMarius Strobl cmd.flags = mic->flags; 93672dec079SMarius Strobl if (len != 0) { 93772dec079SMarius Strobl data.len = len; 93872dec079SMarius Strobl data.data = dp; 93972dec079SMarius Strobl data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE : 94072dec079SMarius Strobl MMC_DATA_READ; 94172dec079SMarius Strobl cmd.data = &data; 94272dec079SMarius Strobl } 94372dec079SMarius Strobl sc = part->sc; 94472dec079SMarius Strobl rca = sc->rca; 94572dec079SMarius Strobl if (mic->is_acmd == 0) { 94672dec079SMarius Strobl /* Enforce/patch/restrict RCA-based commands */ 94772dec079SMarius Strobl switch (cmd.opcode) { 94872dec079SMarius Strobl case MMC_SET_RELATIVE_ADDR: 94972dec079SMarius Strobl case MMC_SELECT_CARD: 95072dec079SMarius Strobl err = EPERM; 95172dec079SMarius Strobl goto out; 95272dec079SMarius Strobl case MMC_STOP_TRANSMISSION: 95372dec079SMarius Strobl if ((cmd.arg & 0x1) == 0) 95472dec079SMarius Strobl break; 95572dec079SMarius Strobl /* FALLTHROUGH */ 95672dec079SMarius Strobl case MMC_SLEEP_AWAKE: 95772dec079SMarius Strobl case MMC_SEND_CSD: 95872dec079SMarius Strobl case MMC_SEND_CID: 95972dec079SMarius Strobl case MMC_SEND_STATUS: 96072dec079SMarius Strobl case MMC_GO_INACTIVE_STATE: 96172dec079SMarius Strobl case MMC_FAST_IO: 96272dec079SMarius Strobl case MMC_APP_CMD: 96372dec079SMarius Strobl cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16); 96472dec079SMarius Strobl break; 96572dec079SMarius Strobl default: 96672dec079SMarius Strobl break; 96772dec079SMarius Strobl } 9686c06949fSMarius Strobl /* 9696c06949fSMarius Strobl * No partition switching in userland; it's almost impossible 9706c06949fSMarius Strobl * to recover from that, especially if things go wrong. 9716c06949fSMarius Strobl */ 9726c06949fSMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC && dp != NULL && 9736c06949fSMarius Strobl (((uint8_t *)dp)[EXT_CSD_PART_CONFIG] & 97461ef738dSMarius Strobl EXT_CSD_PART_CONFIG_ACC_MASK) != part->type) { 9756c06949fSMarius Strobl err = EINVAL; 9766c06949fSMarius Strobl goto out; 9776c06949fSMarius Strobl } 97872dec079SMarius Strobl } 97972dec079SMarius Strobl dev = sc->dev; 980cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 981cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 982cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, part->type); 98372dec079SMarius Strobl if (err != MMC_ERR_NONE) 98472dec079SMarius Strobl goto release; 98572dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 98672dec079SMarius Strobl err = mmcsd_set_blockcount(sc, mic->blocks, 98772dec079SMarius Strobl mic->write_flag & (1 << 31)); 98872dec079SMarius Strobl if (err != MMC_ERR_NONE) 989aca38eabSMarius Strobl goto switch_back; 99072dec079SMarius Strobl } 991646fd30cSMarius Strobl if (mic->write_flag != 0) 992646fd30cSMarius Strobl sc->flags |= MMCSD_DIRTY; 99372dec079SMarius Strobl if (mic->is_acmd != 0) 994cd85acbaSMarius Strobl (void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0); 99572dec079SMarius Strobl else 996cd85acbaSMarius Strobl (void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0); 99772dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 99872dec079SMarius Strobl /* 99972dec079SMarius Strobl * If the request went to the RPMB partition, try to ensure 10006c06949fSMarius Strobl * that the command actually has completed. 100172dec079SMarius Strobl */ 100272dec079SMarius Strobl retries = MMCSD_CMD_RETRIES; 100372dec079SMarius Strobl do { 1004cd85acbaSMarius Strobl err = mmc_send_status(mmcbus, dev, rca, &status); 100572dec079SMarius Strobl if (err != MMC_ERR_NONE) 100672dec079SMarius Strobl break; 100772dec079SMarius Strobl if (R1_STATUS(status) == 0 && 100872dec079SMarius Strobl R1_CURRENT_STATE(status) != R1_STATE_PRG) 100972dec079SMarius Strobl break; 101072dec079SMarius Strobl DELAY(1000); 101172dec079SMarius Strobl } while (retries-- > 0); 101272dec079SMarius Strobl } 101372dec079SMarius Strobl /* 101472dec079SMarius Strobl * If EXT_CSD was changed, our copy is outdated now. Specifically, 101572dec079SMarius Strobl * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(), 101672dec079SMarius Strobl * so retrieve EXT_CSD again. 101772dec079SMarius Strobl */ 101872dec079SMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC) { 1019cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 102072dec079SMarius Strobl if (err != MMC_ERR_NONE) 102172dec079SMarius Strobl goto release; 102272dec079SMarius Strobl } 10236c06949fSMarius Strobl switch_back: 10246c06949fSMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 10256c06949fSMarius Strobl /* 10266c06949fSMarius Strobl * If the request went to the RPMB partition, always switch 10276c06949fSMarius Strobl * back to the default partition (see mmcsd_switch_part()). 10286c06949fSMarius Strobl */ 10296c06949fSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, 10306c06949fSMarius Strobl EXT_CSD_PART_CONFIG_ACC_DEFAULT); 10316c06949fSMarius Strobl if (err != MMC_ERR_NONE) 10326c06949fSMarius Strobl goto release; 10336c06949fSMarius Strobl } 1034cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 103572dec079SMarius Strobl if (cmd.error != MMC_ERR_NONE) { 103672dec079SMarius Strobl switch (cmd.error) { 103772dec079SMarius Strobl case MMC_ERR_TIMEOUT: 103872dec079SMarius Strobl err = ETIMEDOUT; 103972dec079SMarius Strobl break; 104072dec079SMarius Strobl case MMC_ERR_BADCRC: 104172dec079SMarius Strobl err = EILSEQ; 104272dec079SMarius Strobl break; 104372dec079SMarius Strobl case MMC_ERR_INVALID: 104472dec079SMarius Strobl err = EINVAL; 104572dec079SMarius Strobl break; 104672dec079SMarius Strobl case MMC_ERR_NO_MEMORY: 104772dec079SMarius Strobl err = ENOMEM; 104872dec079SMarius Strobl break; 104972dec079SMarius Strobl default: 105072dec079SMarius Strobl err = EIO; 105172dec079SMarius Strobl break; 105272dec079SMarius Strobl } 105372dec079SMarius Strobl goto out; 105472dec079SMarius Strobl } 105572dec079SMarius Strobl memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t)); 105672dec079SMarius Strobl if (mic->write_flag == 0 && len != 0) { 105772dec079SMarius Strobl err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len); 105872dec079SMarius Strobl if (err != 0) 105972dec079SMarius Strobl goto out; 106072dec079SMarius Strobl } 106172dec079SMarius Strobl goto out; 106272dec079SMarius Strobl 106372dec079SMarius Strobl release: 1064cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 106572dec079SMarius Strobl err = EIO; 106672dec079SMarius Strobl 106772dec079SMarius Strobl out: 1068aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 1069aca38eabSMarius Strobl part->ioctl = 0; 1070aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 1071aca38eabSMarius Strobl wakeup(part); 107272dec079SMarius Strobl if (dp != NULL) 107372dec079SMarius Strobl free(dp, M_TEMP); 107472dec079SMarius Strobl return (err); 107572dec079SMarius Strobl } 107672dec079SMarius Strobl 107772dec079SMarius Strobl static int 107872dec079SMarius Strobl mmcsd_getattr(struct bio *bp) 107972dec079SMarius Strobl { 108072dec079SMarius Strobl struct mmcsd_part *part; 108172dec079SMarius Strobl device_t dev; 108272dec079SMarius Strobl 108372dec079SMarius Strobl if (strcmp(bp->bio_attribute, "MMC::device") == 0) { 108472dec079SMarius Strobl if (bp->bio_length != sizeof(dev)) 108572dec079SMarius Strobl return (EFAULT); 108672dec079SMarius Strobl part = bp->bio_disk->d_drv1; 108772dec079SMarius Strobl dev = part->sc->dev; 108872dec079SMarius Strobl bcopy(&dev, bp->bio_data, sizeof(dev)); 108972dec079SMarius Strobl bp->bio_completed = bp->bio_length; 109072dec079SMarius Strobl return (0); 109172dec079SMarius Strobl } 109272dec079SMarius Strobl return (-1); 109372dec079SMarius Strobl } 109472dec079SMarius Strobl 109572dec079SMarius Strobl static int 109672dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable) 109772dec079SMarius Strobl { 109872dec079SMarius Strobl struct mmc_command cmd; 109972dec079SMarius Strobl struct mmc_request req; 110072dec079SMarius Strobl 110172dec079SMarius Strobl memset(&req, 0, sizeof(req)); 110272dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 110372dec079SMarius Strobl cmd.mrq = &req; 110472dec079SMarius Strobl req.cmd = &cmd; 110572dec079SMarius Strobl cmd.opcode = MMC_SET_BLOCK_COUNT; 110672dec079SMarius Strobl cmd.arg = count & 0x0000FFFF; 110772dec079SMarius Strobl if (reliable) 110872dec079SMarius Strobl cmd.arg |= 1 << 31; 110972dec079SMarius Strobl cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1110cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req); 111172dec079SMarius Strobl return (cmd.error); 111272dec079SMarius Strobl } 111372dec079SMarius Strobl 111472dec079SMarius Strobl static int 111572dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part) 111672dec079SMarius Strobl { 111772dec079SMarius Strobl struct mmcsd_softc *sc; 111872dec079SMarius Strobl int err; 111972dec079SMarius Strobl uint8_t value; 112072dec079SMarius Strobl 112172dec079SMarius Strobl sc = device_get_softc(dev); 112272dec079SMarius Strobl 1123aca38eabSMarius Strobl if (sc->mode == mode_sd) 112472dec079SMarius Strobl return (MMC_ERR_NONE); 112572dec079SMarius Strobl 1126aca38eabSMarius Strobl /* 1127aca38eabSMarius Strobl * According to section "6.2.2 Command restrictions" of the eMMC 1128aca38eabSMarius Strobl * specification v5.1, CMD19/CMD21 aren't allowed to be used with 1129aca38eabSMarius Strobl * RPMB partitions. So we pause re-tuning along with triggering 1130aca38eabSMarius Strobl * it up-front to decrease the likelihood of re-tuning becoming 1131aca38eabSMarius Strobl * necessary while accessing an RPMB partition. Consequently, an 1132aca38eabSMarius Strobl * RPMB partition should immediately be switched away from again 1133aca38eabSMarius Strobl * after an access in order to allow for re-tuning to take place 1134aca38eabSMarius Strobl * anew. 1135aca38eabSMarius Strobl */ 1136aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1137cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true); 1138aca38eabSMarius Strobl 1139aca38eabSMarius Strobl if (sc->part_curr == part) 114072dec079SMarius Strobl return (MMC_ERR_NONE); 114172dec079SMarius Strobl 114272dec079SMarius Strobl value = (sc->ext_csd[EXT_CSD_PART_CONFIG] & 114372dec079SMarius Strobl ~EXT_CSD_PART_CONFIG_ACC_MASK) | part; 114472dec079SMarius Strobl /* Jump! */ 114572dec079SMarius Strobl err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL, 114672dec079SMarius Strobl EXT_CSD_PART_CONFIG, value, sc->part_time, true); 1147aca38eabSMarius Strobl if (err != MMC_ERR_NONE) { 1148aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1149cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 115072dec079SMarius Strobl return (err); 1151aca38eabSMarius Strobl } 115272dec079SMarius Strobl 115372dec079SMarius Strobl sc->ext_csd[EXT_CSD_PART_CONFIG] = value; 1154aca38eabSMarius Strobl if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) 1155cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 115672dec079SMarius Strobl sc->part_curr = part; 115772dec079SMarius Strobl return (MMC_ERR_NONE); 115872dec079SMarius Strobl } 115972dec079SMarius Strobl 1160bb1ef63fSWarner Losh static const char * 1161bb1ef63fSWarner Losh mmcsd_errmsg(int e) 1162bb1ef63fSWarner Losh { 11631bacf3beSMarius Strobl 1164bb1ef63fSWarner Losh if (e < 0 || e > MMC_ERR_MAX) 1165bb1ef63fSWarner Losh return "Bad error code"; 1166aca38eabSMarius Strobl return (errmsg[e]); 1167bb1ef63fSWarner Losh } 1168bb1ef63fSWarner Losh 11693906d42dSAlexander Motin static daddr_t 117072dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp) 1171114b4164SWarner Losh { 1172114b4164SWarner Losh daddr_t block, end; 1173114b4164SWarner Losh struct mmc_command cmd; 1174114b4164SWarner Losh struct mmc_command stop; 1175114b4164SWarner Losh struct mmc_request req; 1176114b4164SWarner Losh struct mmc_data data; 117772dec079SMarius Strobl struct mmcsd_softc *sc; 1178cd85acbaSMarius Strobl device_t dev, mmcbus; 1179aca38eabSMarius Strobl u_int numblocks, sz; 11801bacf3beSMarius Strobl char *vaddr; 1181114b4164SWarner Losh 118272dec079SMarius Strobl sc = part->sc; 118372dec079SMarius Strobl dev = sc->dev; 1184cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 118572dec079SMarius Strobl 11863906d42dSAlexander Motin block = bp->bio_pblkno; 118772dec079SMarius Strobl sz = part->disk->d_sectorsize; 1188114b4164SWarner Losh end = bp->bio_pblkno + (bp->bio_bcount / sz); 11893906d42dSAlexander Motin while (block < end) { 11901bacf3beSMarius Strobl vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 1191aca38eabSMarius Strobl numblocks = min(end - block, sc->max_data); 1192114b4164SWarner Losh memset(&req, 0, sizeof(req)); 1193114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 1194114b4164SWarner Losh memset(&stop, 0, sizeof(stop)); 1195ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1196a350e540SIan Lepore cmd.mrq = &req; 1197114b4164SWarner Losh req.cmd = &cmd; 1198114b4164SWarner Losh cmd.data = &data; 1199114b4164SWarner Losh if (bp->bio_cmd == BIO_READ) { 12000c0903b1SWarner Losh if (numblocks > 1) 1201114b4164SWarner Losh cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 1202114b4164SWarner Losh else 1203114b4164SWarner Losh cmd.opcode = MMC_READ_SINGLE_BLOCK; 12040c0903b1SWarner Losh } else { 1205646fd30cSMarius Strobl sc->flags |= MMCSD_DIRTY; 12060c0903b1SWarner Losh if (numblocks > 1) 12070c0903b1SWarner Losh cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 12080c0903b1SWarner Losh else 1209114b4164SWarner Losh cmd.opcode = MMC_WRITE_BLOCK; 12100c0903b1SWarner Losh } 1211c18f1e26SAlexander Motin cmd.arg = block; 1212aca38eabSMarius Strobl if (sc->high_cap == 0) 1213c18f1e26SAlexander Motin cmd.arg <<= 9; 1214114b4164SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1215114b4164SWarner Losh data.data = vaddr; 1216114b4164SWarner Losh data.mrq = &req; 12170c0903b1SWarner Losh if (bp->bio_cmd == BIO_READ) 1218114b4164SWarner Losh data.flags = MMC_DATA_READ; 12190c0903b1SWarner Losh else 1220114b4164SWarner Losh data.flags = MMC_DATA_WRITE; 12210c0903b1SWarner Losh data.len = numblocks * sz; 12220c0903b1SWarner Losh if (numblocks > 1) { 12230c0903b1SWarner Losh data.flags |= MMC_DATA_MULTI; 1224114b4164SWarner Losh stop.opcode = MMC_STOP_TRANSMISSION; 1225114b4164SWarner Losh stop.arg = 0; 1226114b4164SWarner Losh stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 1227a350e540SIan Lepore stop.mrq = &req; 12280c0903b1SWarner Losh req.stop = &stop; 12290c0903b1SWarner Losh } 1230cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1231bb1ef63fSWarner Losh if (req.cmd->error != MMC_ERR_NONE) { 12321bacf3beSMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 12331bacf3beSMarius Strobl LOG_PPS)) 1234bb1ef63fSWarner Losh device_printf(dev, "Error indicated: %d %s\n", 12351bacf3beSMarius Strobl req.cmd->error, 12361bacf3beSMarius Strobl mmcsd_errmsg(req.cmd->error)); 12370c0903b1SWarner Losh break; 1238bb1ef63fSWarner Losh } 12390c0903b1SWarner Losh block += numblocks; 1240114b4164SWarner Losh } 12413906d42dSAlexander Motin return (block); 12423906d42dSAlexander Motin } 12433906d42dSAlexander Motin 12443906d42dSAlexander Motin static daddr_t 124572dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp) 12463906d42dSAlexander Motin { 12473906d42dSAlexander Motin daddr_t block, end, start, stop; 12483906d42dSAlexander Motin struct mmc_command cmd; 12493906d42dSAlexander Motin struct mmc_request req; 125072dec079SMarius Strobl struct mmcsd_softc *sc; 1251cd85acbaSMarius Strobl device_t dev, mmcbus; 1252aca38eabSMarius Strobl u_int erase_sector, sz; 125379f39c6aSMarius Strobl int err; 125479f39c6aSMarius Strobl bool use_trim; 125572dec079SMarius Strobl 125672dec079SMarius Strobl sc = part->sc; 125772dec079SMarius Strobl dev = sc->dev; 1258cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 12593906d42dSAlexander Motin 12603906d42dSAlexander Motin block = bp->bio_pblkno; 126172dec079SMarius Strobl sz = part->disk->d_sectorsize; 12623906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 126379f39c6aSMarius Strobl use_trim = sc->flags & MMCSD_USE_TRIM; 126479f39c6aSMarius Strobl if (use_trim == true) { 126579f39c6aSMarius Strobl start = block; 126679f39c6aSMarius Strobl stop = end; 126779f39c6aSMarius Strobl } else { 126879f39c6aSMarius Strobl /* Coalesce with the remainder of the previous request. */ 126972dec079SMarius Strobl if (block > part->eblock && block <= part->eend) 127072dec079SMarius Strobl block = part->eblock; 127172dec079SMarius Strobl if (end >= part->eblock && end < part->eend) 127272dec079SMarius Strobl end = part->eend; 127379f39c6aSMarius Strobl /* Safely round to the erase sector boundaries. */ 1274aca38eabSMarius Strobl erase_sector = sc->erase_sector; 12753906d42dSAlexander Motin start = block + erase_sector - 1; /* Round up. */ 12763906d42dSAlexander Motin start -= start % erase_sector; 12773906d42dSAlexander Motin stop = end; /* Round down. */ 12783906d42dSAlexander Motin stop -= end % erase_sector; 127979f39c6aSMarius Strobl /* 128079f39c6aSMarius Strobl * We can't erase an area smaller than an erase sector, so 128179f39c6aSMarius Strobl * store it for later. 128279f39c6aSMarius Strobl */ 1283a7cf6274SAlexander Motin if (start >= stop) { 128472dec079SMarius Strobl part->eblock = block; 128572dec079SMarius Strobl part->eend = end; 12863906d42dSAlexander Motin return (end); 1287a7cf6274SAlexander Motin } 128879f39c6aSMarius Strobl } 128979f39c6aSMarius Strobl 129079f39c6aSMarius Strobl if ((sc->flags & MMCSD_INAND_CMD38) != 0) { 129179f39c6aSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 129279f39c6aSMarius Strobl EXT_CSD_INAND_CMD38, use_trim == true ? 129379f39c6aSMarius Strobl EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE, 129479f39c6aSMarius Strobl sc->cmd6_time, true); 129579f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 129679f39c6aSMarius Strobl device_printf(dev, 129779f39c6aSMarius Strobl "Setting iNAND erase command failed %s\n", 129879f39c6aSMarius Strobl mmcsd_errmsg(err)); 129979f39c6aSMarius Strobl return (block); 130079f39c6aSMarius Strobl } 130179f39c6aSMarius Strobl } 13023906d42dSAlexander Motin 1303aca38eabSMarius Strobl /* 1304aca38eabSMarius Strobl * Pause re-tuning so it won't interfere with the order of erase 1305aca38eabSMarius Strobl * commands. Note that these latter don't use the data lines, so 1306aca38eabSMarius Strobl * re-tuning shouldn't actually become necessary during erase. 1307aca38eabSMarius Strobl */ 1308cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(mmcbus, dev, false); 13093906d42dSAlexander Motin /* Set erase start position. */ 13103906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13113906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 1312a350e540SIan Lepore cmd.mrq = &req; 13133906d42dSAlexander Motin req.cmd = &cmd; 1314608226d5SMarius Strobl if (sc->mode == mode_sd) 13153906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_START; 13163906d42dSAlexander Motin else 13173906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_START; 13183906d42dSAlexander Motin cmd.arg = start; 1319aca38eabSMarius Strobl if (sc->high_cap == 0) 13203906d42dSAlexander Motin cmd.arg <<= 9; 13213906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1322cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13233906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 132479f39c6aSMarius Strobl device_printf(dev, "Setting erase start position failed %s\n", 132579f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1326aca38eabSMarius Strobl block = bp->bio_pblkno; 1327aca38eabSMarius Strobl goto unpause; 13283906d42dSAlexander Motin } 13293906d42dSAlexander Motin /* Set erase stop position. */ 13303906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13313906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 13323906d42dSAlexander Motin req.cmd = &cmd; 1333608226d5SMarius Strobl if (sc->mode == mode_sd) 13343906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_END; 13353906d42dSAlexander Motin else 13363906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_END; 13373906d42dSAlexander Motin cmd.arg = stop; 1338aca38eabSMarius Strobl if (sc->high_cap == 0) 13393906d42dSAlexander Motin cmd.arg <<= 9; 13403906d42dSAlexander Motin cmd.arg--; 13413906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1342cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13433906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 134479f39c6aSMarius Strobl device_printf(dev, "Setting erase stop position failed %s\n", 134579f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1346aca38eabSMarius Strobl block = bp->bio_pblkno; 1347aca38eabSMarius Strobl goto unpause; 13483906d42dSAlexander Motin } 13493906d42dSAlexander Motin /* Erase range. */ 13503906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13513906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 13523906d42dSAlexander Motin req.cmd = &cmd; 13533906d42dSAlexander Motin cmd.opcode = MMC_ERASE; 135479f39c6aSMarius Strobl cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE; 13553906d42dSAlexander Motin cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 1356cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13573906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 135879f39c6aSMarius Strobl device_printf(dev, "Issuing erase command failed %s\n", 135979f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1360aca38eabSMarius Strobl block = bp->bio_pblkno; 1361aca38eabSMarius Strobl goto unpause; 13623906d42dSAlexander Motin } 136379f39c6aSMarius Strobl if (use_trim == false) { 136479f39c6aSMarius Strobl /* Store one of the remaining parts for the next call. */ 136572dec079SMarius Strobl if (bp->bio_pblkno >= part->eblock || block == start) { 136672dec079SMarius Strobl part->eblock = stop; /* Predict next forward. */ 136772dec079SMarius Strobl part->eend = end; 1368a7cf6274SAlexander Motin } else { 136972dec079SMarius Strobl part->eblock = block; /* Predict next backward. */ 137072dec079SMarius Strobl part->eend = start; 1371a7cf6274SAlexander Motin } 137279f39c6aSMarius Strobl } 1373aca38eabSMarius Strobl block = end; 1374aca38eabSMarius Strobl unpause: 1375cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(mmcbus, dev); 1376aca38eabSMarius Strobl return (block); 13773906d42dSAlexander Motin } 13783906d42dSAlexander Motin 1379a1fda318SAlexander Motin static int 13801bacf3beSMarius Strobl mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 13811bacf3beSMarius Strobl size_t length) 1382a1fda318SAlexander Motin { 1383a1fda318SAlexander Motin struct bio bp; 1384a1fda318SAlexander Motin daddr_t block, end; 138572dec079SMarius Strobl struct disk *disk; 138672dec079SMarius Strobl struct mmcsd_softc *sc; 138772dec079SMarius Strobl struct mmcsd_part *part; 1388cd85acbaSMarius Strobl device_t dev, mmcbus; 138972dec079SMarius Strobl int err; 1390a1fda318SAlexander Motin 139172dec079SMarius Strobl disk = arg; 139272dec079SMarius Strobl part = disk->d_drv1; 139372dec079SMarius Strobl sc = part->sc; 1394646fd30cSMarius Strobl 1395646fd30cSMarius Strobl /* length zero is special and really means flush buffers to media */ 1396646fd30cSMarius Strobl if (length == 0) { 1397646fd30cSMarius Strobl err = mmcsd_flush_cache(sc); 1398646fd30cSMarius Strobl if (err != MMC_ERR_NONE) 1399646fd30cSMarius Strobl return (EIO); 1400646fd30cSMarius Strobl return (0); 1401646fd30cSMarius Strobl } 1402646fd30cSMarius Strobl 140372dec079SMarius Strobl dev = sc->dev; 1404cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 140572dec079SMarius Strobl 1406c55f5707SWarner Losh g_reset_bio(&bp); 1407a1fda318SAlexander Motin bp.bio_disk = disk; 1408a1fda318SAlexander Motin bp.bio_pblkno = offset / disk->d_sectorsize; 1409a1fda318SAlexander Motin bp.bio_bcount = length; 1410a1fda318SAlexander Motin bp.bio_data = virtual; 1411a1fda318SAlexander Motin bp.bio_cmd = BIO_WRITE; 141272dec079SMarius Strobl end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize; 1413cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1414cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 141572dec079SMarius Strobl if (err != MMC_ERR_NONE) { 141672dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) 141772dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 1418cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 141972dec079SMarius Strobl return (EIO); 142072dec079SMarius Strobl } 142172dec079SMarius Strobl block = mmcsd_rw(part, &bp); 1422cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1423a1fda318SAlexander Motin return ((end < block) ? EIO : 0); 1424a1fda318SAlexander Motin } 1425a1fda318SAlexander Motin 14263906d42dSAlexander Motin static void 14273906d42dSAlexander Motin mmcsd_task(void *arg) 14283906d42dSAlexander Motin { 14293906d42dSAlexander Motin daddr_t block, end; 143072dec079SMarius Strobl struct mmcsd_part *part; 143172dec079SMarius Strobl struct mmcsd_softc *sc; 143272dec079SMarius Strobl struct bio *bp; 1433cd85acbaSMarius Strobl device_t dev, mmcbus; 1434d176b803SScott Long int bio_error, err, sz; 143572dec079SMarius Strobl 143672dec079SMarius Strobl part = arg; 143772dec079SMarius Strobl sc = part->sc; 143872dec079SMarius Strobl dev = sc->dev; 1439cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 14403906d42dSAlexander Motin 1441249b0e85SAlexander Motin while (1) { 1442aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 14433906d42dSAlexander Motin do { 144472dec079SMarius Strobl if (part->running == 0) 1445249b0e85SAlexander Motin goto out; 144672dec079SMarius Strobl bp = bioq_takefirst(&part->bio_queue); 14473906d42dSAlexander Motin if (bp == NULL) 1448aca38eabSMarius Strobl msleep(part, &part->disk_mtx, PRIBIO, 1449aca38eabSMarius Strobl "mmcsd disk jobqueue", 0); 1450249b0e85SAlexander Motin } while (bp == NULL); 1451aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 1452646fd30cSMarius Strobl if (__predict_false(bp->bio_cmd == BIO_FLUSH)) { 1453646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) { 1454646fd30cSMarius Strobl bp->bio_error = EIO; 1455646fd30cSMarius Strobl bp->bio_flags |= BIO_ERROR; 1456646fd30cSMarius Strobl } 1457646fd30cSMarius Strobl biodone(bp); 1458646fd30cSMarius Strobl continue; 1459646fd30cSMarius Strobl } 146072dec079SMarius Strobl if (bp->bio_cmd != BIO_READ && part->ro) { 14613906d42dSAlexander Motin bp->bio_error = EROFS; 14623906d42dSAlexander Motin bp->bio_resid = bp->bio_bcount; 14633906d42dSAlexander Motin bp->bio_flags |= BIO_ERROR; 14643906d42dSAlexander Motin biodone(bp); 14653906d42dSAlexander Motin continue; 14663906d42dSAlexander Motin } 1467cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 146872dec079SMarius Strobl sz = part->disk->d_sectorsize; 14693906d42dSAlexander Motin block = bp->bio_pblkno; 14703906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 1471cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 147272dec079SMarius Strobl if (err != MMC_ERR_NONE) { 147372dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 147472dec079SMarius Strobl LOG_PPS)) 147572dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 147672dec079SMarius Strobl goto release; 147772dec079SMarius Strobl } 14783906d42dSAlexander Motin if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 1479a7cf6274SAlexander Motin /* Access to the remaining erase block obsoletes it. */ 148072dec079SMarius Strobl if (block < part->eend && end > part->eblock) 148172dec079SMarius Strobl part->eblock = part->eend = 0; 148272dec079SMarius Strobl block = mmcsd_rw(part, bp); 14833906d42dSAlexander Motin } else if (bp->bio_cmd == BIO_DELETE) { 148472dec079SMarius Strobl block = mmcsd_delete(part, bp); 1485d176b803SScott Long } else { 1486d176b803SScott Long bio_error = EOPNOTSUPP; 1487d176b803SScott Long goto release; 14883906d42dSAlexander Motin } 148972dec079SMarius Strobl release: 1490cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 14910c0903b1SWarner Losh if (block < end) { 1492d176b803SScott Long bp->bio_error = (bio_error == 0) ? EIO : bio_error; 14930c0903b1SWarner Losh bp->bio_resid = (end - block) * sz; 14940c0903b1SWarner Losh bp->bio_flags |= BIO_ERROR; 1495d119f637SMarius Strobl } else { 1496d119f637SMarius Strobl bp->bio_resid = 0; 14970c0903b1SWarner Losh } 1498114b4164SWarner Losh biodone(bp); 1499114b4164SWarner Losh } 1500249b0e85SAlexander Motin out: 15010e1466acSWarner Losh /* tell parent we're done */ 150272dec079SMarius Strobl part->running = -1; 1503aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 150472dec079SMarius Strobl wakeup(part); 15050e1466acSWarner Losh 15063745c395SJulian Elischer kproc_exit(0); 1507114b4164SWarner Losh } 1508114b4164SWarner Losh 1509a0075e58SWarner Losh static int 1510a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev) 1511a0075e58SWarner Losh { 1512ae5d8757SMarius Strobl 1513a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_1) 1514a0075e58SWarner Losh return (1); 1515a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_4) 1516a0075e58SWarner Losh return (4); 1517a0075e58SWarner Losh return (8); 1518a0075e58SWarner Losh } 1519a0075e58SWarner Losh 1520646fd30cSMarius Strobl static int 1521646fd30cSMarius Strobl mmcsd_flush_cache(struct mmcsd_softc *sc) 1522646fd30cSMarius Strobl { 1523646fd30cSMarius Strobl device_t dev, mmcbus; 1524646fd30cSMarius Strobl int err; 1525646fd30cSMarius Strobl 1526646fd30cSMarius Strobl if ((sc->flags & MMCSD_FLUSH_CACHE) == 0) 1527646fd30cSMarius Strobl return (MMC_ERR_NONE); 1528646fd30cSMarius Strobl 1529646fd30cSMarius Strobl dev = sc->dev; 1530646fd30cSMarius Strobl mmcbus = sc->mmcbus; 1531646fd30cSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1532646fd30cSMarius Strobl if ((sc->flags & MMCSD_DIRTY) == 0) { 1533646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1534646fd30cSMarius Strobl return (MMC_ERR_NONE); 1535646fd30cSMarius Strobl } 1536646fd30cSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 1537646fd30cSMarius Strobl EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE_FLUSH, 60 * 1000, true); 1538646fd30cSMarius Strobl if (err == MMC_ERR_NONE) 1539646fd30cSMarius Strobl sc->flags &= ~MMCSD_DIRTY; 1540646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1541646fd30cSMarius Strobl return (err); 1542646fd30cSMarius Strobl } 1543646fd30cSMarius Strobl 1544114b4164SWarner Losh static device_method_t mmcsd_methods[] = { 1545114b4164SWarner Losh DEVMETHOD(device_probe, mmcsd_probe), 1546114b4164SWarner Losh DEVMETHOD(device_attach, mmcsd_attach), 1547114b4164SWarner Losh DEVMETHOD(device_detach, mmcsd_detach), 1548646fd30cSMarius Strobl DEVMETHOD(device_shutdown, mmcsd_shutdown), 1549eb67f31aSAlexander Motin DEVMETHOD(device_suspend, mmcsd_suspend), 1550eb67f31aSAlexander Motin DEVMETHOD(device_resume, mmcsd_resume), 15517aa65846SMarius Strobl DEVMETHOD_END 1552114b4164SWarner Losh }; 1553114b4164SWarner Losh 1554114b4164SWarner Losh static driver_t mmcsd_driver = { 1555114b4164SWarner Losh "mmcsd", 1556114b4164SWarner Losh mmcsd_methods, 1557114b4164SWarner Losh sizeof(struct mmcsd_softc), 1558114b4164SWarner Losh }; 1559114b4164SWarner Losh static devclass_t mmcsd_devclass; 1560114b4164SWarner Losh 156172dec079SMarius Strobl static int 156272dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused) 156372dec079SMarius Strobl { 156472dec079SMarius Strobl 156572dec079SMarius Strobl switch (what) { 156672dec079SMarius Strobl case MOD_LOAD: 156772dec079SMarius Strobl flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC, 156872dec079SMarius Strobl TRUE); 156972dec079SMarius Strobl return (0); 157072dec079SMarius Strobl case MOD_UNLOAD: 157172dec079SMarius Strobl flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE); 157272dec079SMarius Strobl return (0); 157372dec079SMarius Strobl } 157472dec079SMarius Strobl return (0); 157572dec079SMarius Strobl } 157672dec079SMarius Strobl 157772dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL); 157872dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0); 157972dec079SMarius Strobl MMC_DEPEND(mmcsd); 1580