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> 727217ea7cSMarius Strobl #include <sys/priv.h> 7372dec079SMarius Strobl #include <sys/slicer.h> 74646fd30cSMarius Strobl #include <sys/sysctl.h> 755e3dbf8bSIan Lepore #include <sys/time.h> 7672dec079SMarius Strobl 77c55f5707SWarner Losh #include <geom/geom.h> 78114b4164SWarner Losh #include <geom/geom_disk.h> 79114b4164SWarner Losh 8072dec079SMarius Strobl #include <dev/mmc/bridge.h> 8172dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h> 8272dec079SMarius Strobl #include <dev/mmc/mmc_subr.h> 837aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h> 84114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 857aa65846SMarius Strobl #include <dev/mmc/mmcvar.h> 86114b4164SWarner Losh 87114b4164SWarner Losh #include "mmcbus_if.h" 88114b4164SWarner Losh 897aa65846SMarius Strobl #if __FreeBSD_version < 800002 907aa65846SMarius Strobl #define kproc_create kthread_create 917aa65846SMarius Strobl #define kproc_exit kthread_exit 927aa65846SMarius Strobl #endif 937aa65846SMarius Strobl 9472dec079SMarius Strobl #define MMCSD_CMD_RETRIES 5 9572dec079SMarius Strobl 9672dec079SMarius Strobl #define MMCSD_FMT_BOOT "mmcsd%dboot" 9772dec079SMarius Strobl #define MMCSD_FMT_GP "mmcsd%dgp" 9872dec079SMarius Strobl #define MMCSD_FMT_RPMB "mmcsd%drpmb" 9972dec079SMarius Strobl #define MMCSD_LABEL_ENH "enh" 10072dec079SMarius Strobl 10172dec079SMarius Strobl #define MMCSD_PART_NAMELEN (16 + 1) 10272dec079SMarius Strobl 10372dec079SMarius Strobl struct mmcsd_softc; 10472dec079SMarius Strobl 10572dec079SMarius Strobl struct mmcsd_part { 106aca38eabSMarius Strobl struct mtx disk_mtx; 107aca38eabSMarius Strobl struct mtx ioctl_mtx; 10872dec079SMarius Strobl struct mmcsd_softc *sc; 109114b4164SWarner Losh struct disk *disk; 110114b4164SWarner Losh struct proc *p; 111114b4164SWarner Losh struct bio_queue_head bio_queue; 112a7cf6274SAlexander Motin daddr_t eblock, eend; /* Range remaining after the last erase. */ 11372dec079SMarius Strobl u_int cnt; 11472dec079SMarius Strobl u_int type; 1150e1466acSWarner Losh int running; 116eb67f31aSAlexander Motin int suspend; 117aca38eabSMarius Strobl int ioctl; 11872dec079SMarius Strobl bool ro; 11972dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 12072dec079SMarius Strobl }; 12172dec079SMarius Strobl 12272dec079SMarius Strobl struct mmcsd_softc { 12372dec079SMarius Strobl device_t dev; 124cd85acbaSMarius Strobl device_t mmcbus; 12572dec079SMarius Strobl struct mmcsd_part *part[MMC_PART_MAX]; 12672dec079SMarius Strobl enum mmc_card_mode mode; 127aca38eabSMarius Strobl u_int max_data; /* Maximum data size [blocks] */ 128aca38eabSMarius Strobl u_int erase_sector; /* Device native erase sector size [blocks] */ 129aca38eabSMarius Strobl uint8_t high_cap; /* High Capacity device (block addressed) */ 13072dec079SMarius Strobl uint8_t part_curr; /* Partition currently switched to */ 13172dec079SMarius Strobl uint8_t ext_csd[MMC_EXTCSD_SIZE]; 13272dec079SMarius Strobl uint16_t rca; 13379f39c6aSMarius Strobl uint32_t flags; 13479f39c6aSMarius Strobl #define MMCSD_INAND_CMD38 0x0001 13579f39c6aSMarius Strobl #define MMCSD_USE_TRIM 0x0002 136646fd30cSMarius Strobl #define MMCSD_FLUSH_CACHE 0x0004 137646fd30cSMarius Strobl #define MMCSD_DIRTY 0x0008 13879f39c6aSMarius Strobl uint32_t cmd6_time; /* Generic switch timeout [us] */ 13972dec079SMarius Strobl uint32_t part_time; /* Partition switch timeout [us] */ 14072dec079SMarius Strobl off_t enh_base; /* Enhanced user data area slice base ... */ 14172dec079SMarius Strobl off_t enh_size; /* ... and size [bytes] */ 1425e3dbf8bSIan Lepore int log_count; 1435e3dbf8bSIan Lepore struct timeval log_time; 14472dec079SMarius Strobl struct cdev *rpmb_dev; 145114b4164SWarner Losh }; 146114b4164SWarner Losh 147bb1ef63fSWarner Losh static const char *errmsg[] = 148bb1ef63fSWarner Losh { 149bb1ef63fSWarner Losh "None", 150bb1ef63fSWarner Losh "Timeout", 151bb1ef63fSWarner Losh "Bad CRC", 152bb1ef63fSWarner Losh "Fifo", 153bb1ef63fSWarner Losh "Failed", 154bb1ef63fSWarner Losh "Invalid", 155bb1ef63fSWarner Losh "NO MEMORY" 156bb1ef63fSWarner Losh }; 157bb1ef63fSWarner Losh 158646fd30cSMarius Strobl static SYSCTL_NODE(_hw, OID_AUTO, mmcsd, CTLFLAG_RD, NULL, "mmcsd driver"); 159646fd30cSMarius Strobl 160646fd30cSMarius Strobl static int mmcsd_cache = 1; 161646fd30cSMarius Strobl SYSCTL_INT(_hw_mmcsd, OID_AUTO, cache, CTLFLAG_RDTUN, &mmcsd_cache, 0, 162646fd30cSMarius Strobl "Device R/W cache enabled if present"); 163646fd30cSMarius Strobl 1645e3dbf8bSIan Lepore #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 1655e3dbf8bSIan Lepore 166114b4164SWarner Losh /* bus entry points */ 167114b4164SWarner Losh static int mmcsd_attach(device_t dev); 168114b4164SWarner Losh static int mmcsd_detach(device_t dev); 169ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev); 170646fd30cSMarius Strobl static int mmcsd_shutdown(device_t dev); 171114b4164SWarner Losh 172114b4164SWarner Losh /* disk routines */ 173114b4164SWarner Losh static int mmcsd_close(struct disk *dp); 174a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, 175a1fda318SAlexander Motin off_t offset, size_t length); 17672dec079SMarius Strobl static int mmcsd_getattr(struct bio *); 17772dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, 17872dec079SMarius Strobl int fflag, struct thread *td); 179ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp); 180114b4164SWarner Losh static void mmcsd_task(void *arg); 181114b4164SWarner Losh 18272dec079SMarius Strobl /* RMPB cdev interface */ 18372dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 18472dec079SMarius Strobl int fflag, struct thread *td); 185a0075e58SWarner Losh 18672dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type, 18779f39c6aSMarius Strobl const char *name, u_int cnt, off_t media_size, bool ro); 18872dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev); 18972dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp); 19079f39c6aSMarius Strobl static const char *mmcsd_errmsg(int e); 191646fd30cSMarius Strobl static int mmcsd_flush_cache(struct mmcsd_softc *sc); 19272dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, 1937217ea7cSMarius Strobl int fflag, struct thread *td); 19472dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, 19572dec079SMarius Strobl int fflag); 19672dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit); 19772dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp); 19872dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel); 19972dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider, 20072dec079SMarius Strobl struct flash_slice *slices, int *nslices); 20172dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, 20272dec079SMarius Strobl u_int part); 20372dec079SMarius Strobl 204aca38eabSMarius Strobl #define MMCSD_DISK_LOCK(_part) mtx_lock(&(_part)->disk_mtx) 205aca38eabSMarius Strobl #define MMCSD_DISK_UNLOCK(_part) mtx_unlock(&(_part)->disk_mtx) 206aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_INIT(_part) \ 207aca38eabSMarius Strobl mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF) 208aca38eabSMarius Strobl #define MMCSD_DISK_LOCK_DESTROY(_part) mtx_destroy(&(_part)->disk_mtx); 209aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_LOCKED(_part) \ 210aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_OWNED); 211aca38eabSMarius Strobl #define MMCSD_DISK_ASSERT_UNLOCKED(_part) \ 212aca38eabSMarius Strobl mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED); 213aca38eabSMarius Strobl 214aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK(_part) mtx_lock(&(_part)->ioctl_mtx) 215aca38eabSMarius Strobl #define MMCSD_IOCTL_UNLOCK(_part) mtx_unlock(&(_part)->ioctl_mtx) 216aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_INIT(_part) \ 217aca38eabSMarius Strobl mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF) 218aca38eabSMarius Strobl #define MMCSD_IOCTL_LOCK_DESTROY(_part) mtx_destroy(&(_part)->ioctl_mtx); 219aca38eabSMarius Strobl #define MMCSD_IOCTL_ASSERT_LOCKED(_part) \ 220aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_OWNED); 221aca38eabSMarius Strobl #define MMCSD_IOCLT_ASSERT_UNLOCKED(_part) \ 222aca38eabSMarius Strobl mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED); 223114b4164SWarner Losh 224114b4164SWarner Losh static int 225114b4164SWarner Losh mmcsd_probe(device_t dev) 226114b4164SWarner Losh { 227114b4164SWarner Losh 228c18f1e26SAlexander Motin device_quiet(dev); 2290c0903b1SWarner Losh device_set_desc(dev, "MMC/SD Memory Card"); 230114b4164SWarner Losh return (0); 231114b4164SWarner Losh } 232114b4164SWarner Losh 233114b4164SWarner Losh static int 234114b4164SWarner Losh mmcsd_attach(device_t dev) 235114b4164SWarner Losh { 236cd85acbaSMarius Strobl device_t mmcbus; 237114b4164SWarner Losh struct mmcsd_softc *sc; 23872dec079SMarius Strobl const uint8_t *ext_csd; 23972dec079SMarius Strobl off_t erase_size, sector_size, size, wp_size; 24072dec079SMarius Strobl uintmax_t bytes; 24172dec079SMarius Strobl int err, i; 24279f39c6aSMarius Strobl uint32_t quirks; 24372dec079SMarius Strobl uint8_t rev; 24472dec079SMarius Strobl bool comp, ro; 24572dec079SMarius Strobl char unit[2]; 246114b4164SWarner Losh 247114b4164SWarner Losh sc = device_get_softc(dev); 248114b4164SWarner Losh sc->dev = dev; 249cd85acbaSMarius Strobl sc->mmcbus = mmcbus = device_get_parent(dev); 250cd85acbaSMarius Strobl sc->mode = mmcbr_get_mode(mmcbus); 251aca38eabSMarius Strobl /* 252aca38eabSMarius Strobl * Note that in principle with an SDHCI-like re-tuning implementation, 253aca38eabSMarius Strobl * the maximum data size can change at runtime due to a device removal/ 254aca38eabSMarius Strobl * insertion that results in switches to/from a transfer mode involving 255aca38eabSMarius Strobl * re-tuning, iff there are multiple devices on a given bus. Until now 256aca38eabSMarius Strobl * mmc(4) lacks support for rescanning already attached buses, however, 25721794534SMarius Strobl * and sdhci(4) to date has no support for shared buses in the first 258aca38eabSMarius Strobl * place either. 259aca38eabSMarius Strobl */ 260aca38eabSMarius Strobl sc->max_data = mmc_get_max_data(dev); 261aca38eabSMarius Strobl sc->high_cap = mmc_get_high_cap(dev); 26272dec079SMarius Strobl sc->rca = mmc_get_rca(dev); 26379f39c6aSMarius Strobl sc->cmd6_time = mmc_get_cmd6_timeout(dev); 26479f39c6aSMarius Strobl quirks = mmc_get_quirks(dev); 265114b4164SWarner Losh 26672dec079SMarius Strobl /* Only MMC >= 4.x devices support EXT_CSD. */ 26772dec079SMarius Strobl if (mmc_get_spec_vers(dev) >= 4) { 268cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 269cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 270cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 27179f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 27279f39c6aSMarius Strobl device_printf(dev, "Error reading EXT_CSD %s\n", 27379f39c6aSMarius Strobl mmcsd_errmsg(err)); 27479f39c6aSMarius Strobl return (ENXIO); 27579f39c6aSMarius Strobl } 27672dec079SMarius Strobl } 27772dec079SMarius Strobl ext_csd = sc->ext_csd; 27872dec079SMarius Strobl 27979f39c6aSMarius Strobl if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) { 28079f39c6aSMarius Strobl if (mmc_get_spec_vers(dev) < 4) { 28179f39c6aSMarius Strobl device_printf(dev, 28279f39c6aSMarius Strobl "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n"); 28379f39c6aSMarius Strobl return (EINVAL); 28479f39c6aSMarius Strobl } 28579f39c6aSMarius Strobl sc->flags |= MMCSD_INAND_CMD38; 28679f39c6aSMarius Strobl } 28779f39c6aSMarius Strobl 28879f39c6aSMarius Strobl /* 28979f39c6aSMarius Strobl * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both 29079f39c6aSMarius Strobl * insecure and secure TRIM. 29179f39c6aSMarius Strobl */ 29279f39c6aSMarius Strobl if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] & 29379f39c6aSMarius Strobl EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 && 29479f39c6aSMarius Strobl (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) { 29579f39c6aSMarius Strobl if (bootverbose) 29679f39c6aSMarius Strobl device_printf(dev, "taking advantage of TRIM\n"); 29779f39c6aSMarius Strobl sc->flags |= MMCSD_USE_TRIM; 29879f39c6aSMarius Strobl sc->erase_sector = 1; 29979f39c6aSMarius Strobl } else 30079f39c6aSMarius Strobl sc->erase_sector = mmc_get_erase_sector(dev); 30179f39c6aSMarius Strobl 30272dec079SMarius Strobl /* 30372dec079SMarius Strobl * Enhanced user data area and general purpose partitions are only 30472dec079SMarius Strobl * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB 30572dec079SMarius Strobl * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later. 30672dec079SMarius Strobl */ 30772dec079SMarius Strobl rev = ext_csd[EXT_CSD_REV]; 30872dec079SMarius Strobl 30972dec079SMarius Strobl /* 310646fd30cSMarius Strobl * With revision 1.5 (MMC v4.5, EXT_CSD_REV == 6) and later, take 311646fd30cSMarius Strobl * advantage of the device R/W cache if present and useage is not 312646fd30cSMarius Strobl * disabled. 313646fd30cSMarius Strobl */ 314646fd30cSMarius Strobl if (rev >= 6 && mmcsd_cache != 0) { 315646fd30cSMarius Strobl size = ext_csd[EXT_CSD_CACHE_SIZE] | 316646fd30cSMarius Strobl ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 | 317646fd30cSMarius Strobl ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 | 318646fd30cSMarius Strobl ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24; 319646fd30cSMarius Strobl if (bootverbose) 320646fd30cSMarius Strobl device_printf(dev, "cache size %juKB\n", size); 321646fd30cSMarius Strobl if (size > 0) { 322646fd30cSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 323646fd30cSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, 324646fd30cSMarius Strobl EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CACHE_CTRL, 325646fd30cSMarius Strobl EXT_CSD_CACHE_CTRL_CACHE_EN, sc->cmd6_time, true); 326646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 327646fd30cSMarius Strobl if (err != MMC_ERR_NONE) 328646fd30cSMarius Strobl device_printf(dev, "failed to enable cache\n"); 329646fd30cSMarius Strobl else 330646fd30cSMarius Strobl sc->flags |= MMCSD_FLUSH_CACHE; 331646fd30cSMarius Strobl } 332646fd30cSMarius Strobl } 333646fd30cSMarius Strobl 334646fd30cSMarius Strobl /* 33572dec079SMarius Strobl * Ignore user-creatable enhanced user data area and general purpose 33672dec079SMarius Strobl * partitions partitions as long as partitioning hasn't been finished. 33772dec079SMarius Strobl */ 33872dec079SMarius Strobl comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0; 33972dec079SMarius Strobl 34072dec079SMarius Strobl /* 34172dec079SMarius Strobl * Add enhanced user data area slice, unless it spans the entirety of 34272dec079SMarius Strobl * the user data area. The enhanced area is of a multiple of high 34372dec079SMarius Strobl * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) * 34472dec079SMarius Strobl * 512 KB) and its offset given in either sectors or bytes, depending 34572dec079SMarius Strobl * on whether it's a high capacity device or not. 34672dec079SMarius Strobl * NB: The slicer and its slices need to be registered before adding 34772dec079SMarius Strobl * the disk for the corresponding user data area as re-tasting is 34872dec079SMarius Strobl * racy. 34972dec079SMarius Strobl */ 35072dec079SMarius Strobl sector_size = mmc_get_sector_size(dev); 35172dec079SMarius Strobl size = ext_csd[EXT_CSD_ENH_SIZE_MULT] + 35272dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + 35372dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16); 35472dec079SMarius Strobl if (rev >= 4 && comp == TRUE && size > 0 && 35572dec079SMarius Strobl (ext_csd[EXT_CSD_PART_SUPPORT] & 35672dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 35772dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) { 35872dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 35972dec079SMarius Strobl MMC_SECTOR_SIZE; 36072dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 36172dec079SMarius Strobl size *= erase_size * wp_size; 36272dec079SMarius Strobl if (size != mmc_get_media_size(dev) * sector_size) { 36372dec079SMarius Strobl sc->enh_size = size; 36472dec079SMarius Strobl sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] + 36572dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) + 36672dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) + 36772dec079SMarius Strobl (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) * 368da979d44SMarius Strobl (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1); 36972dec079SMarius Strobl } else if (bootverbose) 37072dec079SMarius Strobl device_printf(dev, 37172dec079SMarius Strobl "enhanced user data area spans entire device\n"); 37272dec079SMarius Strobl } 37372dec079SMarius Strobl 37472dec079SMarius Strobl /* 37572dec079SMarius Strobl * Add default partition. This may be the only one or the user 37672dec079SMarius Strobl * data area in case partitions are supported. 37772dec079SMarius Strobl */ 37872dec079SMarius Strobl ro = mmc_get_read_only(dev); 37972dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd", 38079f39c6aSMarius Strobl device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro); 38172dec079SMarius Strobl 38272dec079SMarius Strobl if (mmc_get_spec_vers(dev) < 3) 38372dec079SMarius Strobl return (0); 38472dec079SMarius Strobl 38572dec079SMarius Strobl /* Belatedly announce enhanced user data slice. */ 38672dec079SMarius Strobl if (sc->enh_size != 0) { 38772dec079SMarius Strobl bytes = mmcsd_pretty_size(size, unit); 38872dec079SMarius Strobl printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area " 38972dec079SMarius Strobl "slice offset 0x%jx at %s\n", device_get_nameunit(dev), 39072dec079SMarius Strobl MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base, 39172dec079SMarius Strobl device_get_nameunit(dev)); 39272dec079SMarius Strobl } 39372dec079SMarius Strobl 39472dec079SMarius Strobl /* 39572dec079SMarius Strobl * Determine partition switch timeout (provided in units of 10 ms) 39672dec079SMarius Strobl * and ensure it's at least 300 ms as some eMMC chips lie. 39772dec079SMarius Strobl */ 39872dec079SMarius Strobl sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000, 39972dec079SMarius Strobl 300 * 1000); 40072dec079SMarius Strobl 40172dec079SMarius Strobl /* Add boot partitions, which are of a fixed multiple of 128 KB. */ 40272dec079SMarius Strobl size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 403cd85acbaSMarius Strobl if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) { 40472dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0, 40579f39c6aSMarius Strobl MMCSD_FMT_BOOT, 0, size, 40672dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 40772dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0)); 40872dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1, 40979f39c6aSMarius Strobl MMCSD_FMT_BOOT, 1, size, 41072dec079SMarius Strobl ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 41172dec079SMarius Strobl EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0)); 41272dec079SMarius Strobl } 41372dec079SMarius Strobl 41472dec079SMarius Strobl /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */ 41572dec079SMarius Strobl size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 41672dec079SMarius Strobl if (rev >= 5 && size > 0) 41772dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB, 41879f39c6aSMarius Strobl MMCSD_FMT_RPMB, 0, size, ro); 41972dec079SMarius Strobl 42072dec079SMarius Strobl if (rev <= 3 || comp == FALSE) 42172dec079SMarius Strobl return (0); 42272dec079SMarius Strobl 42372dec079SMarius Strobl /* 42472dec079SMarius Strobl * Add general purpose partitions, which are of a multiple of high 42572dec079SMarius Strobl * capacity write protect groups, too. 42672dec079SMarius Strobl */ 42772dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) { 42872dec079SMarius Strobl erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 42972dec079SMarius Strobl MMC_SECTOR_SIZE; 43072dec079SMarius Strobl wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 43172dec079SMarius Strobl for (i = 0; i < MMC_PART_GP_MAX; i++) { 43272dec079SMarius Strobl size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] + 43372dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) + 43472dec079SMarius Strobl (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16); 43572dec079SMarius Strobl if (size == 0) 43672dec079SMarius Strobl continue; 43772dec079SMarius Strobl mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i, 43879f39c6aSMarius Strobl MMCSD_FMT_GP, i, size * erase_size * wp_size, ro); 43972dec079SMarius Strobl } 44072dec079SMarius Strobl } 44172dec079SMarius Strobl return (0); 44272dec079SMarius Strobl } 44372dec079SMarius Strobl 44472dec079SMarius Strobl static uintmax_t 44572dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit) 44672dec079SMarius Strobl { 44772dec079SMarius Strobl uintmax_t bytes; 44872dec079SMarius Strobl int i; 44972dec079SMarius Strobl 45072dec079SMarius Strobl /* 45172dec079SMarius Strobl * Display in most natural units. There's no card < 1MB. However, 45272dec079SMarius Strobl * RPMB partitions occasionally are smaller than that, though. The 45372dec079SMarius Strobl * SD standard goes to 2 GiB due to its reliance on FAT, but the data 45472dec079SMarius Strobl * format supports up to 4 GiB and some card makers push it up to this 45572dec079SMarius Strobl * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the 45672dec079SMarius Strobl * data format supports up to 2 TiB however. 2048 GB isn't too ugly, 45772dec079SMarius Strobl * so we note it in passing here and don't add the code to print TB). 45872dec079SMarius Strobl * Since these cards are sold in terms of MB and GB not MiB and GiB, 45972dec079SMarius Strobl * report them like that. We also round to the nearest unit, since 46072dec079SMarius Strobl * many cards are a few percent short, even of the power of 10 size. 46172dec079SMarius Strobl */ 46272dec079SMarius Strobl bytes = size; 46372dec079SMarius Strobl unit[0] = unit[1] = '\0'; 46472dec079SMarius Strobl for (i = 0; i <= 2 && bytes >= 1000; i++) { 46572dec079SMarius Strobl bytes = (bytes + 1000 / 2 - 1) / 1000; 46672dec079SMarius Strobl switch (i) { 46772dec079SMarius Strobl case 0: 46872dec079SMarius Strobl unit[0] = 'k'; 46972dec079SMarius Strobl break; 47072dec079SMarius Strobl case 1: 47172dec079SMarius Strobl unit[0] = 'M'; 47272dec079SMarius Strobl break; 47372dec079SMarius Strobl case 2: 47472dec079SMarius Strobl unit[0] = 'G'; 47572dec079SMarius Strobl break; 47672dec079SMarius Strobl default: 47772dec079SMarius Strobl break; 47872dec079SMarius Strobl } 47972dec079SMarius Strobl } 48072dec079SMarius Strobl return (bytes); 48172dec079SMarius Strobl } 48272dec079SMarius Strobl 48372dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = { 48472dec079SMarius Strobl .d_version = D_VERSION, 48572dec079SMarius Strobl .d_name = "mmcsdrpmb", 48672dec079SMarius Strobl .d_ioctl = mmcsd_ioctl_rpmb 48772dec079SMarius Strobl }; 48872dec079SMarius Strobl 48972dec079SMarius Strobl static void 49072dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt, 49179f39c6aSMarius Strobl off_t media_size, bool ro) 49272dec079SMarius Strobl { 49372dec079SMarius Strobl struct make_dev_args args; 494cd85acbaSMarius Strobl device_t dev, mmcbus; 49572dec079SMarius Strobl const char *ext; 49672dec079SMarius Strobl const uint8_t *ext_csd; 49772dec079SMarius Strobl struct mmcsd_part *part; 49872dec079SMarius Strobl struct disk *d; 49972dec079SMarius Strobl uintmax_t bytes; 50072dec079SMarius Strobl u_int gp; 50172dec079SMarius Strobl uint32_t speed; 50272dec079SMarius Strobl uint8_t extattr; 50372dec079SMarius Strobl bool enh; 50472dec079SMarius Strobl char unit[2]; 50572dec079SMarius Strobl 50672dec079SMarius Strobl dev = sc->dev; 507cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 50872dec079SMarius Strobl part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF, 50972dec079SMarius Strobl M_WAITOK | M_ZERO); 51072dec079SMarius Strobl part->sc = sc; 51172dec079SMarius Strobl part->cnt = cnt; 51272dec079SMarius Strobl part->type = type; 51372dec079SMarius Strobl part->ro = ro; 51472dec079SMarius Strobl snprintf(part->name, sizeof(part->name), name, device_get_unit(dev)); 51572dec079SMarius Strobl 516aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_INIT(part); 517aca38eabSMarius Strobl 518aca38eabSMarius Strobl /* 519aca38eabSMarius Strobl * For the RPMB partition, allow IOCTL access only. 520aca38eabSMarius Strobl * NB: If ever attaching RPMB partitions to disk(9), the re-tuning 521aca38eabSMarius Strobl * implementation and especially its pausing need to be revisited, 522aca38eabSMarius Strobl * because then re-tuning requests may be issued by the IOCTL half 523aca38eabSMarius Strobl * of this driver while re-tuning is already paused by the disk(9) 524aca38eabSMarius Strobl * one and vice versa. 525aca38eabSMarius Strobl */ 52672dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 52772dec079SMarius Strobl make_dev_args_init(&args); 52872dec079SMarius Strobl args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 52972dec079SMarius Strobl args.mda_devsw = &mmcsd_rpmb_cdevsw; 53072dec079SMarius Strobl args.mda_uid = UID_ROOT; 53172dec079SMarius Strobl args.mda_gid = GID_OPERATOR; 53272dec079SMarius Strobl args.mda_mode = 0640; 53372dec079SMarius Strobl args.mda_si_drv1 = part; 53472dec079SMarius Strobl if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) { 53572dec079SMarius Strobl device_printf(dev, "Failed to make RPMB device\n"); 53672dec079SMarius Strobl free(part, M_DEVBUF); 53772dec079SMarius Strobl return; 53872dec079SMarius Strobl } 53972dec079SMarius Strobl } else { 540aca38eabSMarius Strobl MMCSD_DISK_LOCK_INIT(part); 54172dec079SMarius Strobl 54272dec079SMarius Strobl d = part->disk = disk_alloc(); 543a0075e58SWarner Losh d->d_close = mmcsd_close; 544a0075e58SWarner Losh d->d_strategy = mmcsd_strategy; 54572dec079SMarius Strobl d->d_ioctl = mmcsd_ioctl_disk; 546a1fda318SAlexander Motin d->d_dump = mmcsd_dump; 54772dec079SMarius Strobl d->d_getattr = mmcsd_getattr; 54872dec079SMarius Strobl d->d_name = part->name; 54972dec079SMarius Strobl d->d_drv1 = part; 550a0075e58SWarner Losh d->d_sectorsize = mmc_get_sector_size(dev); 551aca38eabSMarius Strobl d->d_maxsize = sc->max_data * d->d_sectorsize; 55272dec079SMarius Strobl d->d_mediasize = media_size; 55379f39c6aSMarius Strobl d->d_stripesize = sc->erase_sector * d->d_sectorsize; 55472dec079SMarius Strobl d->d_unit = cnt; 5553906d42dSAlexander Motin d->d_flags = DISKFLAG_CANDELETE; 556646fd30cSMarius Strobl if ((sc->flags & MMCSD_FLUSH_CACHE) != 0) 557646fd30cSMarius Strobl d->d_flags |= DISKFLAG_CANFLUSHCACHE; 55879f39c6aSMarius Strobl d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize; 55972dec079SMarius Strobl strlcpy(d->d_ident, mmc_get_card_sn_string(dev), 56072dec079SMarius Strobl sizeof(d->d_ident)); 56172dec079SMarius Strobl strlcpy(d->d_descr, mmc_get_card_id_string(dev), 56272dec079SMarius Strobl sizeof(d->d_descr)); 56317160457SAlexander Motin d->d_rotation_rate = DISK_RR_NON_ROTATING; 564f3a4b7f7SIan Lepore 565a0075e58SWarner Losh disk_create(d, DISK_VERSION); 56672dec079SMarius Strobl bioq_init(&part->bio_queue); 5670e1466acSWarner Losh 56872dec079SMarius Strobl part->running = 1; 56972dec079SMarius Strobl kproc_create(&mmcsd_task, part, &part->p, 0, 0, 57072dec079SMarius Strobl "%s%d: mmc/sd card", part->name, cnt); 57172dec079SMarius Strobl } 57272dec079SMarius Strobl 57372dec079SMarius Strobl bytes = mmcsd_pretty_size(media_size, unit); 57472dec079SMarius Strobl if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) { 575cd85acbaSMarius Strobl speed = mmcbr_get_clock(mmcbus); 57672dec079SMarius Strobl printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n", 57772dec079SMarius Strobl part->name, cnt, bytes, unit, mmc_get_card_id_string(dev), 578cd85acbaSMarius Strobl ro ? " (read-only)" : "", device_get_nameunit(mmcbus), 57972dec079SMarius Strobl speed / 1000000, (speed / 100000) % 10, 580aca38eabSMarius Strobl mmcsd_bus_bit_width(dev), sc->max_data); 58172dec079SMarius Strobl } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 58272dec079SMarius Strobl printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes, 58372dec079SMarius Strobl unit, type, ro ? " (read-only)" : "", 58488e07d92SRui Paulo device_get_nameunit(dev)); 58572dec079SMarius Strobl } else { 58672dec079SMarius Strobl enh = false; 58772dec079SMarius Strobl ext = NULL; 58872dec079SMarius Strobl extattr = 0; 58972dec079SMarius Strobl if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 && 59072dec079SMarius Strobl type <= EXT_CSD_PART_CONFIG_ACC_GP3) { 59172dec079SMarius Strobl ext_csd = sc->ext_csd; 59272dec079SMarius Strobl gp = type - EXT_CSD_PART_CONFIG_ACC_GP0; 59372dec079SMarius Strobl if ((ext_csd[EXT_CSD_PART_SUPPORT] & 59472dec079SMarius Strobl EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 59572dec079SMarius Strobl (ext_csd[EXT_CSD_PART_ATTR] & 59672dec079SMarius Strobl (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0) 59772dec079SMarius Strobl enh = true; 59872dec079SMarius Strobl else if ((ext_csd[EXT_CSD_PART_SUPPORT] & 59972dec079SMarius Strobl EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) { 60072dec079SMarius Strobl extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR + 60172dec079SMarius Strobl (gp / 2)] >> (4 * (gp % 2))) & 0xF; 60272dec079SMarius Strobl switch (extattr) { 60372dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_DEFAULT: 60472dec079SMarius Strobl break; 60572dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE: 60672dec079SMarius Strobl ext = "system code"; 60772dec079SMarius Strobl break; 60872dec079SMarius Strobl case EXT_CSD_EXT_PART_ATTR_NPERSISTENT: 60972dec079SMarius Strobl ext = "non-persistent"; 61072dec079SMarius Strobl break; 61172dec079SMarius Strobl default: 61272dec079SMarius Strobl ext = "reserved"; 61372dec079SMarius Strobl break; 61472dec079SMarius Strobl } 61572dec079SMarius Strobl } 61672dec079SMarius Strobl } 61772dec079SMarius Strobl if (ext == NULL) 61872dec079SMarius Strobl printf("%s%d: %ju%sB partion %d%s%s at %s\n", 61972dec079SMarius Strobl part->name, cnt, bytes, unit, type, enh ? 62072dec079SMarius Strobl " enhanced" : "", ro ? " (read-only)" : "", 62172dec079SMarius Strobl device_get_nameunit(dev)); 62272dec079SMarius Strobl else 62372dec079SMarius Strobl printf("%s%d: %ju%sB partion %d extended 0x%x " 62472dec079SMarius Strobl "(%s)%s at %s\n", part->name, cnt, bytes, unit, 62572dec079SMarius Strobl type, extattr, ext, ro ? " (read-only)" : "", 62672dec079SMarius Strobl device_get_nameunit(dev)); 62772dec079SMarius Strobl } 62872dec079SMarius Strobl } 629114b4164SWarner Losh 63072dec079SMarius Strobl static int 63172dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider, 63272dec079SMarius Strobl struct flash_slice *slices, int *nslices) 63372dec079SMarius Strobl { 63472dec079SMarius Strobl char name[MMCSD_PART_NAMELEN]; 63572dec079SMarius Strobl struct mmcsd_softc *sc; 63672dec079SMarius Strobl struct mmcsd_part *part; 63772dec079SMarius Strobl 63872dec079SMarius Strobl *nslices = 0; 63972dec079SMarius Strobl if (slices == NULL) 64072dec079SMarius Strobl return (ENOMEM); 64172dec079SMarius Strobl 64272dec079SMarius Strobl sc = device_get_softc(dev); 64372dec079SMarius Strobl if (sc->enh_size == 0) 64472dec079SMarius Strobl return (ENXIO); 64572dec079SMarius Strobl 64672dec079SMarius Strobl part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT]; 64772dec079SMarius Strobl snprintf(name, sizeof(name), "%s%d", part->disk->d_name, 64872dec079SMarius Strobl part->disk->d_unit); 64972dec079SMarius Strobl if (strcmp(name, provider) != 0) 65072dec079SMarius Strobl return (ENXIO); 65172dec079SMarius Strobl 65272dec079SMarius Strobl *nslices = 1; 65372dec079SMarius Strobl slices[0].base = sc->enh_base; 65472dec079SMarius Strobl slices[0].size = sc->enh_size; 65572dec079SMarius Strobl slices[0].label = MMCSD_LABEL_ENH; 656114b4164SWarner Losh return (0); 657114b4164SWarner Losh } 658114b4164SWarner Losh 659114b4164SWarner Losh static int 660114b4164SWarner Losh mmcsd_detach(device_t dev) 661114b4164SWarner Losh { 6620e1466acSWarner Losh struct mmcsd_softc *sc = device_get_softc(dev); 66372dec079SMarius Strobl struct mmcsd_part *part; 66472dec079SMarius Strobl int i; 6650e1466acSWarner Losh 66672dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 66772dec079SMarius Strobl part = sc->part[i]; 668aca38eabSMarius Strobl if (part != NULL) { 669aca38eabSMarius Strobl if (part->disk != NULL) { 670aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 67172dec079SMarius Strobl part->suspend = 0; 67272dec079SMarius Strobl if (part->running > 0) { 673eb67f31aSAlexander Motin /* kill thread */ 67472dec079SMarius Strobl part->running = 0; 67572dec079SMarius Strobl wakeup(part); 676eb67f31aSAlexander Motin /* wait for thread to finish. */ 67772dec079SMarius Strobl while (part->running != -1) 678aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 679aca38eabSMarius Strobl "mmcsd disk detach", 0); 680eb67f31aSAlexander Motin } 681aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 682aca38eabSMarius Strobl } 683aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 684aca38eabSMarius Strobl while (part->ioctl > 0) 685aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 686aca38eabSMarius Strobl "mmcsd IOCTL detach", 0); 687aca38eabSMarius Strobl part->ioctl = -1; 688aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 68972dec079SMarius Strobl } 69072dec079SMarius Strobl } 6910e1466acSWarner Losh 69272dec079SMarius Strobl if (sc->rpmb_dev != NULL) 69372dec079SMarius Strobl destroy_dev(sc->rpmb_dev); 69472dec079SMarius Strobl 69572dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 69672dec079SMarius Strobl part = sc->part[i]; 69772dec079SMarius Strobl if (part != NULL) { 69872dec079SMarius Strobl if (part->disk != NULL) { 699249b0e85SAlexander Motin /* Flush the request queue. */ 70072dec079SMarius Strobl bioq_flush(&part->bio_queue, NULL, ENXIO); 7010e1466acSWarner Losh /* kill disk */ 70272dec079SMarius Strobl disk_destroy(part->disk); 7030e1466acSWarner Losh 704aca38eabSMarius Strobl MMCSD_DISK_LOCK_DESTROY(part); 70572dec079SMarius Strobl } 706aca38eabSMarius Strobl MMCSD_IOCTL_LOCK_DESTROY(part); 70772dec079SMarius Strobl free(part, M_DEVBUF); 70872dec079SMarius Strobl } 70972dec079SMarius Strobl } 710646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 711646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 712646fd30cSMarius Strobl return (0); 713646fd30cSMarius Strobl } 714646fd30cSMarius Strobl 715646fd30cSMarius Strobl static int 716646fd30cSMarius Strobl mmcsd_shutdown(device_t dev) 717646fd30cSMarius Strobl { 718646fd30cSMarius Strobl struct mmcsd_softc *sc = device_get_softc(dev); 719646fd30cSMarius Strobl 720646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 721646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 722128d0ff2SWarner Losh return (0); 723114b4164SWarner Losh } 724114b4164SWarner Losh 725114b4164SWarner Losh static int 726eb67f31aSAlexander Motin mmcsd_suspend(device_t dev) 727eb67f31aSAlexander Motin { 728eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 72972dec079SMarius Strobl struct mmcsd_part *part; 73072dec079SMarius Strobl int i; 731eb67f31aSAlexander Motin 73272dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 73372dec079SMarius Strobl part = sc->part[i]; 734aca38eabSMarius Strobl if (part != NULL) { 735aca38eabSMarius Strobl if (part->disk != NULL) { 736aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 73772dec079SMarius Strobl part->suspend = 1; 73872dec079SMarius Strobl if (part->running > 0) { 739eb67f31aSAlexander Motin /* kill thread */ 74072dec079SMarius Strobl part->running = 0; 74172dec079SMarius Strobl wakeup(part); 742eb67f31aSAlexander Motin /* wait for thread to finish. */ 74372dec079SMarius Strobl while (part->running != -1) 744aca38eabSMarius Strobl msleep(part, &part->disk_mtx, 0, 745aca38eabSMarius Strobl "mmcsd disk suspension", 0); 746eb67f31aSAlexander Motin } 747aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 748aca38eabSMarius Strobl } 749aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 750aca38eabSMarius Strobl while (part->ioctl > 0) 751aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, 752aca38eabSMarius Strobl "mmcsd IOCTL suspension", 0); 753aca38eabSMarius Strobl part->ioctl = -1; 754aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 75572dec079SMarius Strobl } 75672dec079SMarius Strobl } 757646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 758646fd30cSMarius Strobl device_printf(dev, "failed to flush cache\n"); 759eb67f31aSAlexander Motin return (0); 760eb67f31aSAlexander Motin } 761eb67f31aSAlexander Motin 762eb67f31aSAlexander Motin static int 763eb67f31aSAlexander Motin mmcsd_resume(device_t dev) 764eb67f31aSAlexander Motin { 765eb67f31aSAlexander Motin struct mmcsd_softc *sc = device_get_softc(dev); 76672dec079SMarius Strobl struct mmcsd_part *part; 76772dec079SMarius Strobl int i; 768eb67f31aSAlexander Motin 76972dec079SMarius Strobl for (i = 0; i < MMC_PART_MAX; i++) { 77072dec079SMarius Strobl part = sc->part[i]; 771aca38eabSMarius Strobl if (part != NULL) { 772aca38eabSMarius Strobl if (part->disk != NULL) { 773aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 77472dec079SMarius Strobl part->suspend = 0; 77572dec079SMarius Strobl if (part->running <= 0) { 77672dec079SMarius Strobl part->running = 1; 777aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 778aca38eabSMarius Strobl kproc_create(&mmcsd_task, part, 779aca38eabSMarius Strobl &part->p, 0, 0, "%s%d: mmc/sd card", 780aca38eabSMarius Strobl part->name, part->cnt); 781eb67f31aSAlexander Motin } else 782aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 783aca38eabSMarius Strobl } 784aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 785aca38eabSMarius Strobl part->ioctl = 0; 786aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 78772dec079SMarius Strobl } 78872dec079SMarius Strobl } 789eb67f31aSAlexander Motin return (0); 790eb67f31aSAlexander Motin } 791eb67f31aSAlexander Motin 792eb67f31aSAlexander Motin static int 793646fd30cSMarius Strobl mmcsd_close(struct disk *dp) 794114b4164SWarner Losh { 795646fd30cSMarius Strobl struct mmcsd_softc *sc; 796ae5d8757SMarius Strobl 797646fd30cSMarius Strobl if ((dp->d_flags & DISKFLAG_OPEN) != 0) { 798646fd30cSMarius Strobl sc = ((struct mmcsd_part *)dp->d_drv1)->sc; 799646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) 800646fd30cSMarius Strobl device_printf(sc->dev, "failed to flush cache\n"); 801114b4164SWarner Losh } 802128d0ff2SWarner Losh return (0); 803114b4164SWarner Losh } 804114b4164SWarner Losh 805114b4164SWarner Losh static void 806114b4164SWarner Losh mmcsd_strategy(struct bio *bp) 807114b4164SWarner Losh { 80872dec079SMarius Strobl struct mmcsd_part *part; 809114b4164SWarner Losh 81072dec079SMarius Strobl part = bp->bio_disk->d_drv1; 811aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 81272dec079SMarius Strobl if (part->running > 0 || part->suspend > 0) { 81372dec079SMarius Strobl bioq_disksort(&part->bio_queue, bp); 814aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 81572dec079SMarius Strobl wakeup(part); 816249b0e85SAlexander Motin } else { 817aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 818249b0e85SAlexander Motin biofinish(bp, NULL, ENXIO); 819249b0e85SAlexander Motin } 820114b4164SWarner Losh } 821114b4164SWarner Losh 82272dec079SMarius Strobl static int 82372dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data, 8247217ea7cSMarius Strobl int fflag, struct thread *td) 82572dec079SMarius Strobl { 82672dec079SMarius Strobl 8277217ea7cSMarius Strobl return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag, td)); 82872dec079SMarius Strobl } 82972dec079SMarius Strobl 83072dec079SMarius Strobl static int 83172dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag, 8327217ea7cSMarius Strobl struct thread *td) 83372dec079SMarius Strobl { 83472dec079SMarius Strobl 8357217ea7cSMarius Strobl return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag, td)); 83672dec079SMarius Strobl } 83772dec079SMarius Strobl 83872dec079SMarius Strobl static int 8397217ea7cSMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag, 8407217ea7cSMarius Strobl struct thread *td) 84172dec079SMarius Strobl { 84272dec079SMarius Strobl struct mmc_ioc_cmd *mic; 84372dec079SMarius Strobl struct mmc_ioc_multi_cmd *mimc; 84472dec079SMarius Strobl int i, err; 84572dec079SMarius Strobl u_long cnt, size; 84672dec079SMarius Strobl 84772dec079SMarius Strobl if ((fflag & FREAD) == 0) 84872dec079SMarius Strobl return (EBADF); 84972dec079SMarius Strobl 8507217ea7cSMarius Strobl err = priv_check(td, PRIV_DRIVER); 8517217ea7cSMarius Strobl if (err != 0) 8527217ea7cSMarius Strobl return (err); 8537217ea7cSMarius Strobl 85472dec079SMarius Strobl err = 0; 85572dec079SMarius Strobl switch (cmd) { 85672dec079SMarius Strobl case MMC_IOC_CMD: 85772dec079SMarius Strobl mic = data; 858aca38eabSMarius Strobl err = mmcsd_ioctl_cmd(part, mic, fflag); 85972dec079SMarius Strobl break; 860aca38eabSMarius Strobl case MMC_IOC_MULTI_CMD: 86172dec079SMarius Strobl mimc = data; 86272dec079SMarius Strobl if (mimc->num_of_cmds == 0) 86372dec079SMarius Strobl break; 86472dec079SMarius Strobl if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS) 86572dec079SMarius Strobl return (EINVAL); 86672dec079SMarius Strobl cnt = mimc->num_of_cmds; 86772dec079SMarius Strobl size = sizeof(*mic) * cnt; 86872dec079SMarius Strobl mic = malloc(size, M_TEMP, M_WAITOK); 86972dec079SMarius Strobl err = copyin((const void *)mimc->cmds, mic, size); 870aca38eabSMarius Strobl if (err == 0) { 87172dec079SMarius Strobl for (i = 0; i < cnt; i++) { 87272dec079SMarius Strobl err = mmcsd_ioctl_cmd(part, &mic[i], fflag); 87372dec079SMarius Strobl if (err != 0) 87472dec079SMarius Strobl break; 87572dec079SMarius Strobl } 876aca38eabSMarius Strobl } 87772dec079SMarius Strobl free(mic, M_TEMP); 87872dec079SMarius Strobl break; 87972dec079SMarius Strobl default: 88072dec079SMarius Strobl return (ENOIOCTL); 88172dec079SMarius Strobl } 88272dec079SMarius Strobl return (err); 88372dec079SMarius Strobl } 88472dec079SMarius Strobl 88572dec079SMarius Strobl static int 88672dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag) 88772dec079SMarius Strobl { 88872dec079SMarius Strobl struct mmc_command cmd; 88972dec079SMarius Strobl struct mmc_data data; 89072dec079SMarius Strobl struct mmcsd_softc *sc; 891cd85acbaSMarius Strobl device_t dev, mmcbus; 89272dec079SMarius Strobl void *dp; 89372dec079SMarius Strobl u_long len; 89472dec079SMarius Strobl int err, retries; 89572dec079SMarius Strobl uint32_t status; 89672dec079SMarius Strobl uint16_t rca; 89772dec079SMarius Strobl 89872dec079SMarius Strobl if ((fflag & FWRITE) == 0 && mic->write_flag != 0) 89972dec079SMarius Strobl return (EBADF); 90072dec079SMarius Strobl 90172dec079SMarius Strobl if (part->ro == TRUE && mic->write_flag != 0) 90272dec079SMarius Strobl return (EROFS); 90372dec079SMarius Strobl 904aca38eabSMarius Strobl /* 905aca38eabSMarius Strobl * We don't need to explicitly lock against the disk(9) half of this 906aca38eabSMarius Strobl * driver as MMCBUS_ACQUIRE_BUS() will serialize us. However, it's 907aca38eabSMarius Strobl * necessary to protect against races with detachment and suspension, 908aca38eabSMarius Strobl * especially since it's required to switch away from RPMB partitions 909aca38eabSMarius Strobl * again after an access (see mmcsd_switch_part()). 910aca38eabSMarius Strobl */ 911aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 912aca38eabSMarius Strobl while (part->ioctl != 0) { 913aca38eabSMarius Strobl if (part->ioctl < 0) { 914aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 915aca38eabSMarius Strobl return (ENXIO); 916aca38eabSMarius Strobl } 917aca38eabSMarius Strobl msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0); 918aca38eabSMarius Strobl } 919aca38eabSMarius Strobl part->ioctl = 1; 920aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 921aca38eabSMarius Strobl 92272dec079SMarius Strobl err = 0; 92372dec079SMarius Strobl dp = NULL; 92472dec079SMarius Strobl len = mic->blksz * mic->blocks; 925aca38eabSMarius Strobl if (len > MMC_IOC_MAX_BYTES) { 926aca38eabSMarius Strobl err = EOVERFLOW; 927aca38eabSMarius Strobl goto out; 928aca38eabSMarius Strobl } 92972dec079SMarius Strobl if (len != 0) { 93072dec079SMarius Strobl dp = malloc(len, M_TEMP, M_WAITOK); 93172dec079SMarius Strobl err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len); 93272dec079SMarius Strobl if (err != 0) 93372dec079SMarius Strobl goto out; 93472dec079SMarius Strobl } 93572dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 93672dec079SMarius Strobl memset(&data, 0, sizeof(data)); 93772dec079SMarius Strobl cmd.opcode = mic->opcode; 93872dec079SMarius Strobl cmd.arg = mic->arg; 93972dec079SMarius Strobl cmd.flags = mic->flags; 94072dec079SMarius Strobl if (len != 0) { 94172dec079SMarius Strobl data.len = len; 94272dec079SMarius Strobl data.data = dp; 94372dec079SMarius Strobl data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE : 94472dec079SMarius Strobl MMC_DATA_READ; 94572dec079SMarius Strobl cmd.data = &data; 94672dec079SMarius Strobl } 94772dec079SMarius Strobl sc = part->sc; 94872dec079SMarius Strobl rca = sc->rca; 94972dec079SMarius Strobl if (mic->is_acmd == 0) { 95072dec079SMarius Strobl /* Enforce/patch/restrict RCA-based commands */ 95172dec079SMarius Strobl switch (cmd.opcode) { 95272dec079SMarius Strobl case MMC_SET_RELATIVE_ADDR: 95372dec079SMarius Strobl case MMC_SELECT_CARD: 95472dec079SMarius Strobl err = EPERM; 95572dec079SMarius Strobl goto out; 95672dec079SMarius Strobl case MMC_STOP_TRANSMISSION: 95772dec079SMarius Strobl if ((cmd.arg & 0x1) == 0) 95872dec079SMarius Strobl break; 95972dec079SMarius Strobl /* FALLTHROUGH */ 96072dec079SMarius Strobl case MMC_SLEEP_AWAKE: 96172dec079SMarius Strobl case MMC_SEND_CSD: 96272dec079SMarius Strobl case MMC_SEND_CID: 96372dec079SMarius Strobl case MMC_SEND_STATUS: 96472dec079SMarius Strobl case MMC_GO_INACTIVE_STATE: 96572dec079SMarius Strobl case MMC_FAST_IO: 96672dec079SMarius Strobl case MMC_APP_CMD: 96772dec079SMarius Strobl cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16); 96872dec079SMarius Strobl break; 96972dec079SMarius Strobl default: 97072dec079SMarius Strobl break; 97172dec079SMarius Strobl } 9726c06949fSMarius Strobl /* 9736c06949fSMarius Strobl * No partition switching in userland; it's almost impossible 9746c06949fSMarius Strobl * to recover from that, especially if things go wrong. 9756c06949fSMarius Strobl */ 9766c06949fSMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC && dp != NULL && 9776c06949fSMarius Strobl (((uint8_t *)dp)[EXT_CSD_PART_CONFIG] & 97861ef738dSMarius Strobl EXT_CSD_PART_CONFIG_ACC_MASK) != part->type) { 9796c06949fSMarius Strobl err = EINVAL; 9806c06949fSMarius Strobl goto out; 9816c06949fSMarius Strobl } 98272dec079SMarius Strobl } 98372dec079SMarius Strobl dev = sc->dev; 984cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 985cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 986cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, part->type); 98772dec079SMarius Strobl if (err != MMC_ERR_NONE) 98872dec079SMarius Strobl goto release; 98972dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 99072dec079SMarius Strobl err = mmcsd_set_blockcount(sc, mic->blocks, 99172dec079SMarius Strobl mic->write_flag & (1 << 31)); 99272dec079SMarius Strobl if (err != MMC_ERR_NONE) 993aca38eabSMarius Strobl goto switch_back; 99472dec079SMarius Strobl } 995646fd30cSMarius Strobl if (mic->write_flag != 0) 996646fd30cSMarius Strobl sc->flags |= MMCSD_DIRTY; 99772dec079SMarius Strobl if (mic->is_acmd != 0) 998cd85acbaSMarius Strobl (void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0); 99972dec079SMarius Strobl else 1000cd85acbaSMarius Strobl (void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0); 100172dec079SMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 100272dec079SMarius Strobl /* 100372dec079SMarius Strobl * If the request went to the RPMB partition, try to ensure 10046c06949fSMarius Strobl * that the command actually has completed. 100572dec079SMarius Strobl */ 100672dec079SMarius Strobl retries = MMCSD_CMD_RETRIES; 100772dec079SMarius Strobl do { 1008cd85acbaSMarius Strobl err = mmc_send_status(mmcbus, dev, rca, &status); 100972dec079SMarius Strobl if (err != MMC_ERR_NONE) 101072dec079SMarius Strobl break; 101172dec079SMarius Strobl if (R1_STATUS(status) == 0 && 101272dec079SMarius Strobl R1_CURRENT_STATE(status) != R1_STATE_PRG) 101372dec079SMarius Strobl break; 101472dec079SMarius Strobl DELAY(1000); 101572dec079SMarius Strobl } while (retries-- > 0); 101672dec079SMarius Strobl } 101772dec079SMarius Strobl /* 101872dec079SMarius Strobl * If EXT_CSD was changed, our copy is outdated now. Specifically, 101972dec079SMarius Strobl * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(), 102072dec079SMarius Strobl * so retrieve EXT_CSD again. 102172dec079SMarius Strobl */ 102272dec079SMarius Strobl if (cmd.opcode == MMC_SWITCH_FUNC) { 1023cd85acbaSMarius Strobl err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd); 102472dec079SMarius Strobl if (err != MMC_ERR_NONE) 102572dec079SMarius Strobl goto release; 102672dec079SMarius Strobl } 10276c06949fSMarius Strobl switch_back: 10286c06949fSMarius Strobl if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 10296c06949fSMarius Strobl /* 10306c06949fSMarius Strobl * If the request went to the RPMB partition, always switch 10316c06949fSMarius Strobl * back to the default partition (see mmcsd_switch_part()). 10326c06949fSMarius Strobl */ 10336c06949fSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, rca, 10346c06949fSMarius Strobl EXT_CSD_PART_CONFIG_ACC_DEFAULT); 10356c06949fSMarius Strobl if (err != MMC_ERR_NONE) 10366c06949fSMarius Strobl goto release; 10376c06949fSMarius Strobl } 1038cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 103972dec079SMarius Strobl if (cmd.error != MMC_ERR_NONE) { 104072dec079SMarius Strobl switch (cmd.error) { 104172dec079SMarius Strobl case MMC_ERR_TIMEOUT: 104272dec079SMarius Strobl err = ETIMEDOUT; 104372dec079SMarius Strobl break; 104472dec079SMarius Strobl case MMC_ERR_BADCRC: 104572dec079SMarius Strobl err = EILSEQ; 104672dec079SMarius Strobl break; 104772dec079SMarius Strobl case MMC_ERR_INVALID: 104872dec079SMarius Strobl err = EINVAL; 104972dec079SMarius Strobl break; 105072dec079SMarius Strobl case MMC_ERR_NO_MEMORY: 105172dec079SMarius Strobl err = ENOMEM; 105272dec079SMarius Strobl break; 105372dec079SMarius Strobl default: 105472dec079SMarius Strobl err = EIO; 105572dec079SMarius Strobl break; 105672dec079SMarius Strobl } 105772dec079SMarius Strobl goto out; 105872dec079SMarius Strobl } 105972dec079SMarius Strobl memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t)); 106072dec079SMarius Strobl if (mic->write_flag == 0 && len != 0) { 106172dec079SMarius Strobl err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len); 106272dec079SMarius Strobl if (err != 0) 106372dec079SMarius Strobl goto out; 106472dec079SMarius Strobl } 106572dec079SMarius Strobl goto out; 106672dec079SMarius Strobl 106772dec079SMarius Strobl release: 1068cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 106972dec079SMarius Strobl err = EIO; 107072dec079SMarius Strobl 107172dec079SMarius Strobl out: 1072aca38eabSMarius Strobl MMCSD_IOCTL_LOCK(part); 1073aca38eabSMarius Strobl part->ioctl = 0; 1074aca38eabSMarius Strobl MMCSD_IOCTL_UNLOCK(part); 1075aca38eabSMarius Strobl wakeup(part); 107672dec079SMarius Strobl if (dp != NULL) 107772dec079SMarius Strobl free(dp, M_TEMP); 107872dec079SMarius Strobl return (err); 107972dec079SMarius Strobl } 108072dec079SMarius Strobl 108172dec079SMarius Strobl static int 108272dec079SMarius Strobl mmcsd_getattr(struct bio *bp) 108372dec079SMarius Strobl { 108472dec079SMarius Strobl struct mmcsd_part *part; 108572dec079SMarius Strobl device_t dev; 108672dec079SMarius Strobl 108772dec079SMarius Strobl if (strcmp(bp->bio_attribute, "MMC::device") == 0) { 108872dec079SMarius Strobl if (bp->bio_length != sizeof(dev)) 108972dec079SMarius Strobl return (EFAULT); 109072dec079SMarius Strobl part = bp->bio_disk->d_drv1; 109172dec079SMarius Strobl dev = part->sc->dev; 109272dec079SMarius Strobl bcopy(&dev, bp->bio_data, sizeof(dev)); 109372dec079SMarius Strobl bp->bio_completed = bp->bio_length; 109472dec079SMarius Strobl return (0); 109572dec079SMarius Strobl } 109672dec079SMarius Strobl return (-1); 109772dec079SMarius Strobl } 109872dec079SMarius Strobl 109972dec079SMarius Strobl static int 110072dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable) 110172dec079SMarius Strobl { 110272dec079SMarius Strobl struct mmc_command cmd; 110372dec079SMarius Strobl struct mmc_request req; 110472dec079SMarius Strobl 110572dec079SMarius Strobl memset(&req, 0, sizeof(req)); 110672dec079SMarius Strobl memset(&cmd, 0, sizeof(cmd)); 110772dec079SMarius Strobl cmd.mrq = &req; 110872dec079SMarius Strobl req.cmd = &cmd; 110972dec079SMarius Strobl cmd.opcode = MMC_SET_BLOCK_COUNT; 111072dec079SMarius Strobl cmd.arg = count & 0x0000FFFF; 111172dec079SMarius Strobl if (reliable) 111272dec079SMarius Strobl cmd.arg |= 1 << 31; 111372dec079SMarius Strobl cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1114cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req); 111572dec079SMarius Strobl return (cmd.error); 111672dec079SMarius Strobl } 111772dec079SMarius Strobl 111872dec079SMarius Strobl static int 111972dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part) 112072dec079SMarius Strobl { 112172dec079SMarius Strobl struct mmcsd_softc *sc; 112272dec079SMarius Strobl int err; 112372dec079SMarius Strobl uint8_t value; 112472dec079SMarius Strobl 112572dec079SMarius Strobl sc = device_get_softc(dev); 112672dec079SMarius Strobl 1127aca38eabSMarius Strobl if (sc->mode == mode_sd) 112872dec079SMarius Strobl return (MMC_ERR_NONE); 112972dec079SMarius Strobl 1130aca38eabSMarius Strobl /* 1131aca38eabSMarius Strobl * According to section "6.2.2 Command restrictions" of the eMMC 1132aca38eabSMarius Strobl * specification v5.1, CMD19/CMD21 aren't allowed to be used with 1133aca38eabSMarius Strobl * RPMB partitions. So we pause re-tuning along with triggering 1134aca38eabSMarius Strobl * it up-front to decrease the likelihood of re-tuning becoming 1135aca38eabSMarius Strobl * necessary while accessing an RPMB partition. Consequently, an 1136aca38eabSMarius Strobl * RPMB partition should immediately be switched away from again 1137aca38eabSMarius Strobl * after an access in order to allow for re-tuning to take place 1138aca38eabSMarius Strobl * anew. 1139aca38eabSMarius Strobl */ 1140aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1141cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true); 1142aca38eabSMarius Strobl 1143aca38eabSMarius Strobl if (sc->part_curr == part) 114472dec079SMarius Strobl return (MMC_ERR_NONE); 114572dec079SMarius Strobl 114672dec079SMarius Strobl value = (sc->ext_csd[EXT_CSD_PART_CONFIG] & 114772dec079SMarius Strobl ~EXT_CSD_PART_CONFIG_ACC_MASK) | part; 114872dec079SMarius Strobl /* Jump! */ 114972dec079SMarius Strobl err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL, 115072dec079SMarius Strobl EXT_CSD_PART_CONFIG, value, sc->part_time, true); 1151aca38eabSMarius Strobl if (err != MMC_ERR_NONE) { 1152aca38eabSMarius Strobl if (part == EXT_CSD_PART_CONFIG_ACC_RPMB) 1153cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 115472dec079SMarius Strobl return (err); 1155aca38eabSMarius Strobl } 115672dec079SMarius Strobl 115772dec079SMarius Strobl sc->ext_csd[EXT_CSD_PART_CONFIG] = value; 1158aca38eabSMarius Strobl if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) 1159cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev); 116072dec079SMarius Strobl sc->part_curr = part; 116172dec079SMarius Strobl return (MMC_ERR_NONE); 116272dec079SMarius Strobl } 116372dec079SMarius Strobl 1164bb1ef63fSWarner Losh static const char * 1165bb1ef63fSWarner Losh mmcsd_errmsg(int e) 1166bb1ef63fSWarner Losh { 11671bacf3beSMarius Strobl 1168bb1ef63fSWarner Losh if (e < 0 || e > MMC_ERR_MAX) 1169bb1ef63fSWarner Losh return "Bad error code"; 1170aca38eabSMarius Strobl return (errmsg[e]); 1171bb1ef63fSWarner Losh } 1172bb1ef63fSWarner Losh 11733906d42dSAlexander Motin static daddr_t 117472dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp) 1175114b4164SWarner Losh { 1176114b4164SWarner Losh daddr_t block, end; 1177114b4164SWarner Losh struct mmc_command cmd; 1178114b4164SWarner Losh struct mmc_command stop; 1179114b4164SWarner Losh struct mmc_request req; 1180114b4164SWarner Losh struct mmc_data data; 118172dec079SMarius Strobl struct mmcsd_softc *sc; 1182cd85acbaSMarius Strobl device_t dev, mmcbus; 1183aca38eabSMarius Strobl u_int numblocks, sz; 11841bacf3beSMarius Strobl char *vaddr; 1185114b4164SWarner Losh 118672dec079SMarius Strobl sc = part->sc; 118772dec079SMarius Strobl dev = sc->dev; 1188cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 118972dec079SMarius Strobl 11903906d42dSAlexander Motin block = bp->bio_pblkno; 119172dec079SMarius Strobl sz = part->disk->d_sectorsize; 1192114b4164SWarner Losh end = bp->bio_pblkno + (bp->bio_bcount / sz); 11933906d42dSAlexander Motin while (block < end) { 11941bacf3beSMarius Strobl vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 1195aca38eabSMarius Strobl numblocks = min(end - block, sc->max_data); 1196114b4164SWarner Losh memset(&req, 0, sizeof(req)); 1197114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 1198114b4164SWarner Losh memset(&stop, 0, sizeof(stop)); 1199ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1200a350e540SIan Lepore cmd.mrq = &req; 1201114b4164SWarner Losh req.cmd = &cmd; 1202114b4164SWarner Losh cmd.data = &data; 1203114b4164SWarner Losh if (bp->bio_cmd == BIO_READ) { 12040c0903b1SWarner Losh if (numblocks > 1) 1205114b4164SWarner Losh cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 1206114b4164SWarner Losh else 1207114b4164SWarner Losh cmd.opcode = MMC_READ_SINGLE_BLOCK; 12080c0903b1SWarner Losh } else { 1209646fd30cSMarius Strobl sc->flags |= MMCSD_DIRTY; 12100c0903b1SWarner Losh if (numblocks > 1) 12110c0903b1SWarner Losh cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 12120c0903b1SWarner Losh else 1213114b4164SWarner Losh cmd.opcode = MMC_WRITE_BLOCK; 12140c0903b1SWarner Losh } 1215c18f1e26SAlexander Motin cmd.arg = block; 1216aca38eabSMarius Strobl if (sc->high_cap == 0) 1217c18f1e26SAlexander Motin cmd.arg <<= 9; 1218114b4164SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1219114b4164SWarner Losh data.data = vaddr; 1220114b4164SWarner Losh data.mrq = &req; 12210c0903b1SWarner Losh if (bp->bio_cmd == BIO_READ) 1222114b4164SWarner Losh data.flags = MMC_DATA_READ; 12230c0903b1SWarner Losh else 1224114b4164SWarner Losh data.flags = MMC_DATA_WRITE; 12250c0903b1SWarner Losh data.len = numblocks * sz; 12260c0903b1SWarner Losh if (numblocks > 1) { 12270c0903b1SWarner Losh data.flags |= MMC_DATA_MULTI; 1228114b4164SWarner Losh stop.opcode = MMC_STOP_TRANSMISSION; 1229114b4164SWarner Losh stop.arg = 0; 1230114b4164SWarner Losh stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 1231a350e540SIan Lepore stop.mrq = &req; 12320c0903b1SWarner Losh req.stop = &stop; 12330c0903b1SWarner Losh } 1234cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 1235bb1ef63fSWarner Losh if (req.cmd->error != MMC_ERR_NONE) { 12361bacf3beSMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 12371bacf3beSMarius Strobl LOG_PPS)) 1238bb1ef63fSWarner Losh device_printf(dev, "Error indicated: %d %s\n", 12391bacf3beSMarius Strobl req.cmd->error, 12401bacf3beSMarius Strobl mmcsd_errmsg(req.cmd->error)); 12410c0903b1SWarner Losh break; 1242bb1ef63fSWarner Losh } 12430c0903b1SWarner Losh block += numblocks; 1244114b4164SWarner Losh } 12453906d42dSAlexander Motin return (block); 12463906d42dSAlexander Motin } 12473906d42dSAlexander Motin 12483906d42dSAlexander Motin static daddr_t 124972dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp) 12503906d42dSAlexander Motin { 12513906d42dSAlexander Motin daddr_t block, end, start, stop; 12523906d42dSAlexander Motin struct mmc_command cmd; 12533906d42dSAlexander Motin struct mmc_request req; 125472dec079SMarius Strobl struct mmcsd_softc *sc; 1255cd85acbaSMarius Strobl device_t dev, mmcbus; 1256aca38eabSMarius Strobl u_int erase_sector, sz; 125779f39c6aSMarius Strobl int err; 125879f39c6aSMarius Strobl bool use_trim; 125972dec079SMarius Strobl 126072dec079SMarius Strobl sc = part->sc; 126172dec079SMarius Strobl dev = sc->dev; 1262cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 12633906d42dSAlexander Motin 12643906d42dSAlexander Motin block = bp->bio_pblkno; 126572dec079SMarius Strobl sz = part->disk->d_sectorsize; 12663906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 126779f39c6aSMarius Strobl use_trim = sc->flags & MMCSD_USE_TRIM; 126879f39c6aSMarius Strobl if (use_trim == true) { 126979f39c6aSMarius Strobl start = block; 127079f39c6aSMarius Strobl stop = end; 127179f39c6aSMarius Strobl } else { 127279f39c6aSMarius Strobl /* Coalesce with the remainder of the previous request. */ 127372dec079SMarius Strobl if (block > part->eblock && block <= part->eend) 127472dec079SMarius Strobl block = part->eblock; 127572dec079SMarius Strobl if (end >= part->eblock && end < part->eend) 127672dec079SMarius Strobl end = part->eend; 127779f39c6aSMarius Strobl /* Safely round to the erase sector boundaries. */ 1278aca38eabSMarius Strobl erase_sector = sc->erase_sector; 12793906d42dSAlexander Motin start = block + erase_sector - 1; /* Round up. */ 12803906d42dSAlexander Motin start -= start % erase_sector; 12813906d42dSAlexander Motin stop = end; /* Round down. */ 12823906d42dSAlexander Motin stop -= end % erase_sector; 128379f39c6aSMarius Strobl /* 128479f39c6aSMarius Strobl * We can't erase an area smaller than an erase sector, so 128579f39c6aSMarius Strobl * store it for later. 128679f39c6aSMarius Strobl */ 1287a7cf6274SAlexander Motin if (start >= stop) { 128872dec079SMarius Strobl part->eblock = block; 128972dec079SMarius Strobl part->eend = end; 12903906d42dSAlexander Motin return (end); 1291a7cf6274SAlexander Motin } 129279f39c6aSMarius Strobl } 129379f39c6aSMarius Strobl 129479f39c6aSMarius Strobl if ((sc->flags & MMCSD_INAND_CMD38) != 0) { 129579f39c6aSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 129679f39c6aSMarius Strobl EXT_CSD_INAND_CMD38, use_trim == true ? 129779f39c6aSMarius Strobl EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE, 129879f39c6aSMarius Strobl sc->cmd6_time, true); 129979f39c6aSMarius Strobl if (err != MMC_ERR_NONE) { 130079f39c6aSMarius Strobl device_printf(dev, 130179f39c6aSMarius Strobl "Setting iNAND erase command failed %s\n", 130279f39c6aSMarius Strobl mmcsd_errmsg(err)); 130379f39c6aSMarius Strobl return (block); 130479f39c6aSMarius Strobl } 130579f39c6aSMarius Strobl } 13063906d42dSAlexander Motin 1307aca38eabSMarius Strobl /* 1308aca38eabSMarius Strobl * Pause re-tuning so it won't interfere with the order of erase 1309aca38eabSMarius Strobl * commands. Note that these latter don't use the data lines, so 1310aca38eabSMarius Strobl * re-tuning shouldn't actually become necessary during erase. 1311aca38eabSMarius Strobl */ 1312cd85acbaSMarius Strobl MMCBUS_RETUNE_PAUSE(mmcbus, dev, false); 13133906d42dSAlexander Motin /* Set erase start position. */ 13143906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13153906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 1316a350e540SIan Lepore cmd.mrq = &req; 13173906d42dSAlexander Motin req.cmd = &cmd; 13183906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 13193906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_START; 13203906d42dSAlexander Motin else 13213906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_START; 13223906d42dSAlexander Motin cmd.arg = start; 1323aca38eabSMarius Strobl if (sc->high_cap == 0) 13243906d42dSAlexander Motin cmd.arg <<= 9; 13253906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1326cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13273906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 132879f39c6aSMarius Strobl device_printf(dev, "Setting erase start position failed %s\n", 132979f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1330aca38eabSMarius Strobl block = bp->bio_pblkno; 1331aca38eabSMarius Strobl goto unpause; 13323906d42dSAlexander Motin } 13333906d42dSAlexander Motin /* Set erase stop position. */ 13343906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13353906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 13363906d42dSAlexander Motin req.cmd = &cmd; 13373906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 13383906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_END; 13393906d42dSAlexander Motin else 13403906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_END; 13413906d42dSAlexander Motin cmd.arg = stop; 1342aca38eabSMarius Strobl if (sc->high_cap == 0) 13433906d42dSAlexander Motin cmd.arg <<= 9; 13443906d42dSAlexander Motin cmd.arg--; 13453906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1346cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13473906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 134879f39c6aSMarius Strobl device_printf(dev, "Setting erase stop position failed %s\n", 134979f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1350aca38eabSMarius Strobl block = bp->bio_pblkno; 1351aca38eabSMarius Strobl goto unpause; 13523906d42dSAlexander Motin } 13533906d42dSAlexander Motin /* Erase range. */ 13543906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 13553906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 13563906d42dSAlexander Motin req.cmd = &cmd; 13573906d42dSAlexander Motin cmd.opcode = MMC_ERASE; 135879f39c6aSMarius Strobl cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE; 13593906d42dSAlexander Motin cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 1360cd85acbaSMarius Strobl MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req); 13613906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 136279f39c6aSMarius Strobl device_printf(dev, "Issuing erase command failed %s\n", 136379f39c6aSMarius Strobl mmcsd_errmsg(req.cmd->error)); 1364aca38eabSMarius Strobl block = bp->bio_pblkno; 1365aca38eabSMarius Strobl goto unpause; 13663906d42dSAlexander Motin } 136779f39c6aSMarius Strobl if (use_trim == false) { 136879f39c6aSMarius Strobl /* Store one of the remaining parts for the next call. */ 136972dec079SMarius Strobl if (bp->bio_pblkno >= part->eblock || block == start) { 137072dec079SMarius Strobl part->eblock = stop; /* Predict next forward. */ 137172dec079SMarius Strobl part->eend = end; 1372a7cf6274SAlexander Motin } else { 137372dec079SMarius Strobl part->eblock = block; /* Predict next backward. */ 137472dec079SMarius Strobl part->eend = start; 1375a7cf6274SAlexander Motin } 137679f39c6aSMarius Strobl } 1377aca38eabSMarius Strobl block = end; 1378aca38eabSMarius Strobl unpause: 1379cd85acbaSMarius Strobl MMCBUS_RETUNE_UNPAUSE(mmcbus, dev); 1380aca38eabSMarius Strobl return (block); 13813906d42dSAlexander Motin } 13823906d42dSAlexander Motin 1383a1fda318SAlexander Motin static int 13841bacf3beSMarius Strobl mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 13851bacf3beSMarius Strobl size_t length) 1386a1fda318SAlexander Motin { 1387a1fda318SAlexander Motin struct bio bp; 1388a1fda318SAlexander Motin daddr_t block, end; 138972dec079SMarius Strobl struct disk *disk; 139072dec079SMarius Strobl struct mmcsd_softc *sc; 139172dec079SMarius Strobl struct mmcsd_part *part; 1392cd85acbaSMarius Strobl device_t dev, mmcbus; 139372dec079SMarius Strobl int err; 1394a1fda318SAlexander Motin 139572dec079SMarius Strobl disk = arg; 139672dec079SMarius Strobl part = disk->d_drv1; 139772dec079SMarius Strobl sc = part->sc; 1398646fd30cSMarius Strobl 1399646fd30cSMarius Strobl /* length zero is special and really means flush buffers to media */ 1400646fd30cSMarius Strobl if (length == 0) { 1401646fd30cSMarius Strobl err = mmcsd_flush_cache(sc); 1402646fd30cSMarius Strobl if (err != MMC_ERR_NONE) 1403646fd30cSMarius Strobl return (EIO); 1404646fd30cSMarius Strobl return (0); 1405646fd30cSMarius Strobl } 1406646fd30cSMarius Strobl 140772dec079SMarius Strobl dev = sc->dev; 1408cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 140972dec079SMarius Strobl 1410c55f5707SWarner Losh g_reset_bio(&bp); 1411a1fda318SAlexander Motin bp.bio_disk = disk; 1412a1fda318SAlexander Motin bp.bio_pblkno = offset / disk->d_sectorsize; 1413a1fda318SAlexander Motin bp.bio_bcount = length; 1414a1fda318SAlexander Motin bp.bio_data = virtual; 1415a1fda318SAlexander Motin bp.bio_cmd = BIO_WRITE; 141672dec079SMarius Strobl end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize; 1417cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1418cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 141972dec079SMarius Strobl if (err != MMC_ERR_NONE) { 142072dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) 142172dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 1422cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 142372dec079SMarius Strobl return (EIO); 142472dec079SMarius Strobl } 142572dec079SMarius Strobl block = mmcsd_rw(part, &bp); 1426cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1427a1fda318SAlexander Motin return ((end < block) ? EIO : 0); 1428a1fda318SAlexander Motin } 1429a1fda318SAlexander Motin 14303906d42dSAlexander Motin static void 14313906d42dSAlexander Motin mmcsd_task(void *arg) 14323906d42dSAlexander Motin { 14333906d42dSAlexander Motin daddr_t block, end; 143472dec079SMarius Strobl struct mmcsd_part *part; 143572dec079SMarius Strobl struct mmcsd_softc *sc; 143672dec079SMarius Strobl struct bio *bp; 1437cd85acbaSMarius Strobl device_t dev, mmcbus; 143872dec079SMarius Strobl int err, sz; 143972dec079SMarius Strobl 144072dec079SMarius Strobl part = arg; 144172dec079SMarius Strobl sc = part->sc; 144272dec079SMarius Strobl dev = sc->dev; 1443cd85acbaSMarius Strobl mmcbus = sc->mmcbus; 14443906d42dSAlexander Motin 1445249b0e85SAlexander Motin while (1) { 1446aca38eabSMarius Strobl MMCSD_DISK_LOCK(part); 14473906d42dSAlexander Motin do { 144872dec079SMarius Strobl if (part->running == 0) 1449249b0e85SAlexander Motin goto out; 145072dec079SMarius Strobl bp = bioq_takefirst(&part->bio_queue); 14513906d42dSAlexander Motin if (bp == NULL) 1452aca38eabSMarius Strobl msleep(part, &part->disk_mtx, PRIBIO, 1453aca38eabSMarius Strobl "mmcsd disk jobqueue", 0); 1454249b0e85SAlexander Motin } while (bp == NULL); 1455aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 1456646fd30cSMarius Strobl if (__predict_false(bp->bio_cmd == BIO_FLUSH)) { 1457646fd30cSMarius Strobl if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) { 1458646fd30cSMarius Strobl bp->bio_error = EIO; 1459646fd30cSMarius Strobl bp->bio_flags |= BIO_ERROR; 1460646fd30cSMarius Strobl } 1461646fd30cSMarius Strobl biodone(bp); 1462646fd30cSMarius Strobl continue; 1463646fd30cSMarius Strobl } 146472dec079SMarius Strobl if (bp->bio_cmd != BIO_READ && part->ro) { 14653906d42dSAlexander Motin bp->bio_error = EROFS; 14663906d42dSAlexander Motin bp->bio_resid = bp->bio_bcount; 14673906d42dSAlexander Motin bp->bio_flags |= BIO_ERROR; 14683906d42dSAlexander Motin biodone(bp); 14693906d42dSAlexander Motin continue; 14703906d42dSAlexander Motin } 1471cd85acbaSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 147272dec079SMarius Strobl sz = part->disk->d_sectorsize; 14733906d42dSAlexander Motin block = bp->bio_pblkno; 14743906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 1475cd85acbaSMarius Strobl err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type); 147672dec079SMarius Strobl if (err != MMC_ERR_NONE) { 147772dec079SMarius Strobl if (ppsratecheck(&sc->log_time, &sc->log_count, 147872dec079SMarius Strobl LOG_PPS)) 147972dec079SMarius Strobl device_printf(dev, "Partition switch error\n"); 148072dec079SMarius Strobl goto release; 148172dec079SMarius Strobl } 14823906d42dSAlexander Motin if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 1483a7cf6274SAlexander Motin /* Access to the remaining erase block obsoletes it. */ 148472dec079SMarius Strobl if (block < part->eend && end > part->eblock) 148572dec079SMarius Strobl part->eblock = part->eend = 0; 148672dec079SMarius Strobl block = mmcsd_rw(part, bp); 14873906d42dSAlexander Motin } else if (bp->bio_cmd == BIO_DELETE) { 148872dec079SMarius Strobl block = mmcsd_delete(part, bp); 14893906d42dSAlexander Motin } 149072dec079SMarius Strobl release: 1491cd85acbaSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 14920c0903b1SWarner Losh if (block < end) { 14930c0903b1SWarner Losh bp->bio_error = EIO; 14940c0903b1SWarner Losh bp->bio_resid = (end - block) * sz; 14950c0903b1SWarner Losh bp->bio_flags |= BIO_ERROR; 1496d119f637SMarius Strobl } else { 1497d119f637SMarius Strobl bp->bio_resid = 0; 14980c0903b1SWarner Losh } 1499114b4164SWarner Losh biodone(bp); 1500114b4164SWarner Losh } 1501249b0e85SAlexander Motin out: 15020e1466acSWarner Losh /* tell parent we're done */ 150372dec079SMarius Strobl part->running = -1; 1504aca38eabSMarius Strobl MMCSD_DISK_UNLOCK(part); 150572dec079SMarius Strobl wakeup(part); 15060e1466acSWarner Losh 15073745c395SJulian Elischer kproc_exit(0); 1508114b4164SWarner Losh } 1509114b4164SWarner Losh 1510a0075e58SWarner Losh static int 1511a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev) 1512a0075e58SWarner Losh { 1513ae5d8757SMarius Strobl 1514a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_1) 1515a0075e58SWarner Losh return (1); 1516a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_4) 1517a0075e58SWarner Losh return (4); 1518a0075e58SWarner Losh return (8); 1519a0075e58SWarner Losh } 1520a0075e58SWarner Losh 1521646fd30cSMarius Strobl static int 1522646fd30cSMarius Strobl mmcsd_flush_cache(struct mmcsd_softc *sc) 1523646fd30cSMarius Strobl { 1524646fd30cSMarius Strobl device_t dev, mmcbus; 1525646fd30cSMarius Strobl int err; 1526646fd30cSMarius Strobl 1527646fd30cSMarius Strobl if ((sc->flags & MMCSD_FLUSH_CACHE) == 0) 1528646fd30cSMarius Strobl return (MMC_ERR_NONE); 1529646fd30cSMarius Strobl 1530646fd30cSMarius Strobl dev = sc->dev; 1531646fd30cSMarius Strobl mmcbus = sc->mmcbus; 1532646fd30cSMarius Strobl MMCBUS_ACQUIRE_BUS(mmcbus, dev); 1533646fd30cSMarius Strobl if ((sc->flags & MMCSD_DIRTY) == 0) { 1534646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1535646fd30cSMarius Strobl return (MMC_ERR_NONE); 1536646fd30cSMarius Strobl } 1537646fd30cSMarius Strobl err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL, 1538646fd30cSMarius Strobl EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE_FLUSH, 60 * 1000, true); 1539646fd30cSMarius Strobl if (err == MMC_ERR_NONE) 1540646fd30cSMarius Strobl sc->flags &= ~MMCSD_DIRTY; 1541646fd30cSMarius Strobl MMCBUS_RELEASE_BUS(mmcbus, dev); 1542646fd30cSMarius Strobl return (err); 1543646fd30cSMarius Strobl } 1544646fd30cSMarius Strobl 1545114b4164SWarner Losh static device_method_t mmcsd_methods[] = { 1546114b4164SWarner Losh DEVMETHOD(device_probe, mmcsd_probe), 1547114b4164SWarner Losh DEVMETHOD(device_attach, mmcsd_attach), 1548114b4164SWarner Losh DEVMETHOD(device_detach, mmcsd_detach), 1549646fd30cSMarius Strobl DEVMETHOD(device_shutdown, mmcsd_shutdown), 1550eb67f31aSAlexander Motin DEVMETHOD(device_suspend, mmcsd_suspend), 1551eb67f31aSAlexander Motin DEVMETHOD(device_resume, mmcsd_resume), 15527aa65846SMarius Strobl DEVMETHOD_END 1553114b4164SWarner Losh }; 1554114b4164SWarner Losh 1555114b4164SWarner Losh static driver_t mmcsd_driver = { 1556114b4164SWarner Losh "mmcsd", 1557114b4164SWarner Losh mmcsd_methods, 1558114b4164SWarner Losh sizeof(struct mmcsd_softc), 1559114b4164SWarner Losh }; 1560114b4164SWarner Losh static devclass_t mmcsd_devclass; 1561114b4164SWarner Losh 156272dec079SMarius Strobl static int 156372dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused) 156472dec079SMarius Strobl { 156572dec079SMarius Strobl 156672dec079SMarius Strobl switch (what) { 156772dec079SMarius Strobl case MOD_LOAD: 156872dec079SMarius Strobl flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC, 156972dec079SMarius Strobl TRUE); 157072dec079SMarius Strobl return (0); 157172dec079SMarius Strobl case MOD_UNLOAD: 157272dec079SMarius Strobl flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE); 157372dec079SMarius Strobl return (0); 157472dec079SMarius Strobl } 157572dec079SMarius Strobl return (0); 157672dec079SMarius Strobl } 157772dec079SMarius Strobl 157872dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL); 157972dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0); 158072dec079SMarius Strobl MMC_DEPEND(mmcsd); 1581