xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 3b433ed76500f1dde444bed736f9f36695cb93a0)
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 
1597029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, mmcsd, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1607029da5cSPawel Biernacki     "mmcsd driver");
161646fd30cSMarius Strobl 
162646fd30cSMarius Strobl static int mmcsd_cache = 1;
163646fd30cSMarius Strobl SYSCTL_INT(_hw_mmcsd, OID_AUTO, cache, CTLFLAG_RDTUN, &mmcsd_cache, 0,
164646fd30cSMarius Strobl     "Device R/W cache enabled if present");
165646fd30cSMarius Strobl 
1665e3dbf8bSIan Lepore #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
1675e3dbf8bSIan Lepore 
168114b4164SWarner Losh /* bus entry points */
169114b4164SWarner Losh static int mmcsd_attach(device_t dev);
170114b4164SWarner Losh static int mmcsd_detach(device_t dev);
171ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev);
172646fd30cSMarius Strobl static int mmcsd_shutdown(device_t dev);
173114b4164SWarner Losh 
174114b4164SWarner Losh /* disk routines */
175114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
176a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
177a1fda318SAlexander Motin     off_t offset, size_t length);
17872dec079SMarius Strobl static int mmcsd_getattr(struct bio *);
17972dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
18072dec079SMarius Strobl     int fflag, struct thread *td);
181ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp);
182114b4164SWarner Losh static void mmcsd_task(void *arg);
183114b4164SWarner Losh 
18472dec079SMarius Strobl /* RMPB cdev interface */
18572dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
18672dec079SMarius Strobl     int fflag, struct thread *td);
187a0075e58SWarner Losh 
18872dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
18979f39c6aSMarius Strobl     const char *name, u_int cnt, off_t media_size, bool ro);
19072dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev);
19172dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
19279f39c6aSMarius Strobl static const char *mmcsd_errmsg(int e);
193646fd30cSMarius Strobl static int mmcsd_flush_cache(struct mmcsd_softc *sc);
19472dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
1957217ea7cSMarius Strobl     int fflag, struct thread *td);
19672dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
19772dec079SMarius Strobl     int fflag);
19872dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
19972dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
20072dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
20172dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider,
20272dec079SMarius Strobl     struct flash_slice *slices, int *nslices);
20372dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
20472dec079SMarius Strobl     u_int part);
20572dec079SMarius Strobl 
206aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK(_part)		mtx_lock(&(_part)->disk_mtx)
207aca38eabSMarius Strobl #define	MMCSD_DISK_UNLOCK(_part)	mtx_unlock(&(_part)->disk_mtx)
208aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK_INIT(_part)					\
209aca38eabSMarius Strobl 	mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF)
210aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->disk_mtx);
211aca38eabSMarius Strobl #define	MMCSD_DISK_ASSERT_LOCKED(_part)					\
212aca38eabSMarius Strobl 	mtx_assert(&(_part)->disk_mtx, MA_OWNED);
213aca38eabSMarius Strobl #define	MMCSD_DISK_ASSERT_UNLOCKED(_part)				\
214aca38eabSMarius Strobl 	mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED);
215aca38eabSMarius Strobl 
216aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK(_part)		mtx_lock(&(_part)->ioctl_mtx)
217aca38eabSMarius Strobl #define	MMCSD_IOCTL_UNLOCK(_part)	mtx_unlock(&(_part)->ioctl_mtx)
218aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK_INIT(_part)					\
219aca38eabSMarius Strobl 	mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF)
220aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->ioctl_mtx);
221aca38eabSMarius Strobl #define	MMCSD_IOCTL_ASSERT_LOCKED(_part)				\
222aca38eabSMarius Strobl 	mtx_assert(&(_part)->ioctl_mtx, MA_OWNED);
223aca38eabSMarius Strobl #define	MMCSD_IOCLT_ASSERT_UNLOCKED(_part)				\
224aca38eabSMarius Strobl 	mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED);
225114b4164SWarner Losh 
226114b4164SWarner Losh static int
227114b4164SWarner Losh mmcsd_probe(device_t dev)
228114b4164SWarner Losh {
229114b4164SWarner Losh 
230c18f1e26SAlexander Motin 	device_quiet(dev);
2310c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
232114b4164SWarner Losh 	return (0);
233114b4164SWarner Losh }
234114b4164SWarner Losh 
235114b4164SWarner Losh static int
236114b4164SWarner Losh mmcsd_attach(device_t dev)
237114b4164SWarner Losh {
238cd85acbaSMarius Strobl 	device_t mmcbus;
239114b4164SWarner Losh 	struct mmcsd_softc *sc;
24072dec079SMarius Strobl 	const uint8_t *ext_csd;
24172dec079SMarius Strobl 	off_t erase_size, sector_size, size, wp_size;
24272dec079SMarius Strobl 	uintmax_t bytes;
24372dec079SMarius Strobl 	int err, i;
24479f39c6aSMarius Strobl 	uint32_t quirks;
24572dec079SMarius Strobl 	uint8_t rev;
24672dec079SMarius Strobl 	bool comp, ro;
24772dec079SMarius Strobl 	char unit[2];
248114b4164SWarner Losh 
249114b4164SWarner Losh 	sc = device_get_softc(dev);
250114b4164SWarner Losh 	sc->dev = dev;
251cd85acbaSMarius Strobl 	sc->mmcbus = mmcbus = device_get_parent(dev);
252608226d5SMarius Strobl 	sc->mode = mmc_get_card_type(dev);
253aca38eabSMarius Strobl 	/*
254aca38eabSMarius Strobl 	 * Note that in principle with an SDHCI-like re-tuning implementation,
255aca38eabSMarius Strobl 	 * the maximum data size can change at runtime due to a device removal/
256aca38eabSMarius Strobl 	 * insertion that results in switches to/from a transfer mode involving
257aca38eabSMarius Strobl 	 * re-tuning, iff there are multiple devices on a given bus.  Until now
258aca38eabSMarius Strobl 	 * mmc(4) lacks support for rescanning already attached buses, however,
25921794534SMarius Strobl 	 * and sdhci(4) to date has no support for shared buses in the first
260aca38eabSMarius Strobl 	 * place either.
261aca38eabSMarius Strobl 	 */
262aca38eabSMarius Strobl 	sc->max_data = mmc_get_max_data(dev);
263aca38eabSMarius Strobl 	sc->high_cap = mmc_get_high_cap(dev);
26472dec079SMarius Strobl 	sc->rca = mmc_get_rca(dev);
26579f39c6aSMarius Strobl 	sc->cmd6_time = mmc_get_cmd6_timeout(dev);
26679f39c6aSMarius Strobl 	quirks = mmc_get_quirks(dev);
267114b4164SWarner Losh 
26872dec079SMarius Strobl 	/* Only MMC >= 4.x devices support EXT_CSD. */
26972dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) >= 4) {
270cd85acbaSMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
271cd85acbaSMarius Strobl 		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
272cd85acbaSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
27379f39c6aSMarius Strobl 		if (err != MMC_ERR_NONE) {
27479f39c6aSMarius Strobl 			device_printf(dev, "Error reading EXT_CSD %s\n",
27579f39c6aSMarius Strobl 			    mmcsd_errmsg(err));
27679f39c6aSMarius Strobl 			return (ENXIO);
27779f39c6aSMarius Strobl 		}
27872dec079SMarius Strobl 	}
27972dec079SMarius Strobl 	ext_csd = sc->ext_csd;
28072dec079SMarius Strobl 
28179f39c6aSMarius Strobl 	if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) {
28279f39c6aSMarius Strobl 		if (mmc_get_spec_vers(dev) < 4) {
28379f39c6aSMarius Strobl 			device_printf(dev,
28479f39c6aSMarius Strobl 			    "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n");
28579f39c6aSMarius Strobl 			return (EINVAL);
28679f39c6aSMarius Strobl 		}
28779f39c6aSMarius Strobl 		sc->flags |= MMCSD_INAND_CMD38;
28879f39c6aSMarius Strobl 	}
28979f39c6aSMarius Strobl 
29079f39c6aSMarius Strobl 	/*
29179f39c6aSMarius Strobl 	 * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both
29279f39c6aSMarius Strobl 	 * insecure and secure TRIM.
29379f39c6aSMarius Strobl 	 */
29479f39c6aSMarius Strobl 	if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] &
29579f39c6aSMarius Strobl 	    EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 &&
29679f39c6aSMarius Strobl 	    (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) {
29779f39c6aSMarius Strobl 		if (bootverbose)
29879f39c6aSMarius Strobl 			device_printf(dev, "taking advantage of TRIM\n");
29979f39c6aSMarius Strobl 		sc->flags |= MMCSD_USE_TRIM;
30079f39c6aSMarius Strobl 		sc->erase_sector = 1;
30179f39c6aSMarius Strobl 	} else
30279f39c6aSMarius Strobl 		sc->erase_sector = mmc_get_erase_sector(dev);
30379f39c6aSMarius Strobl 
30472dec079SMarius Strobl 	/*
30572dec079SMarius Strobl 	 * Enhanced user data area and general purpose partitions are only
30672dec079SMarius Strobl 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
30772dec079SMarius Strobl 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
30872dec079SMarius Strobl 	 */
30972dec079SMarius Strobl 	rev = ext_csd[EXT_CSD_REV];
31072dec079SMarius Strobl 
31172dec079SMarius Strobl 	/*
312646fd30cSMarius Strobl 	 * With revision 1.5 (MMC v4.5, EXT_CSD_REV == 6) and later, take
313646fd30cSMarius Strobl 	 * advantage of the device R/W cache if present and useage is not
314646fd30cSMarius Strobl 	 * disabled.
315646fd30cSMarius Strobl 	 */
316646fd30cSMarius Strobl 	if (rev >= 6 && mmcsd_cache != 0) {
317602a05b0SMarius Strobl 		size = le32dec(&ext_csd[EXT_CSD_CACHE_SIZE]);
318646fd30cSMarius Strobl 		if (bootverbose)
319646fd30cSMarius Strobl 			device_printf(dev, "cache size %juKB\n", size);
320646fd30cSMarius Strobl 		if (size > 0) {
321646fd30cSMarius Strobl 			MMCBUS_ACQUIRE_BUS(mmcbus, dev);
322646fd30cSMarius Strobl 			err = mmc_switch(mmcbus, dev, sc->rca,
323646fd30cSMarius Strobl 			    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CACHE_CTRL,
324646fd30cSMarius Strobl 			    EXT_CSD_CACHE_CTRL_CACHE_EN, sc->cmd6_time, true);
325646fd30cSMarius Strobl 			MMCBUS_RELEASE_BUS(mmcbus, dev);
326646fd30cSMarius Strobl 			if (err != MMC_ERR_NONE)
327646fd30cSMarius Strobl 				device_printf(dev, "failed to enable cache\n");
328646fd30cSMarius Strobl 			else
329646fd30cSMarius Strobl 				sc->flags |= MMCSD_FLUSH_CACHE;
330646fd30cSMarius Strobl 		}
331646fd30cSMarius Strobl 	}
332646fd30cSMarius Strobl 
333646fd30cSMarius Strobl 	/*
33472dec079SMarius Strobl 	 * Ignore user-creatable enhanced user data area and general purpose
33572dec079SMarius Strobl 	 * partitions partitions as long as partitioning hasn't been finished.
33672dec079SMarius Strobl 	 */
33772dec079SMarius Strobl 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
33872dec079SMarius Strobl 
33972dec079SMarius Strobl 	/*
34072dec079SMarius Strobl 	 * Add enhanced user data area slice, unless it spans the entirety of
34172dec079SMarius Strobl 	 * the user data area.  The enhanced area is of a multiple of high
34272dec079SMarius Strobl 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
34372dec079SMarius Strobl 	 * 512 KB) and its offset given in either sectors or bytes, depending
34472dec079SMarius Strobl 	 * on whether it's a high capacity device or not.
34572dec079SMarius Strobl 	 * NB: The slicer and its slices need to be registered before adding
34672dec079SMarius Strobl 	 *     the disk for the corresponding user data area as re-tasting is
34772dec079SMarius Strobl 	 *     racy.
34872dec079SMarius Strobl 	 */
34972dec079SMarius Strobl 	sector_size = mmc_get_sector_size(dev);
35072dec079SMarius Strobl 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
35172dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
35272dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
35372dec079SMarius Strobl 	if (rev >= 4 && comp == TRUE && size > 0 &&
35472dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
35572dec079SMarius Strobl 	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
35672dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
35772dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
35872dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
35972dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
36072dec079SMarius Strobl 		size *= erase_size * wp_size;
36172dec079SMarius Strobl 		if (size != mmc_get_media_size(dev) * sector_size) {
36272dec079SMarius Strobl 			sc->enh_size = size;
363602a05b0SMarius Strobl 			sc->enh_base =
364602a05b0SMarius Strobl 			    le32dec(&ext_csd[EXT_CSD_ENH_START_ADDR]) *
365da979d44SMarius Strobl 			    (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1);
36672dec079SMarius Strobl 		} else if (bootverbose)
36772dec079SMarius Strobl 			device_printf(dev,
36872dec079SMarius Strobl 			    "enhanced user data area spans entire device\n");
36972dec079SMarius Strobl 	}
37072dec079SMarius Strobl 
37172dec079SMarius Strobl 	/*
37272dec079SMarius Strobl 	 * Add default partition.  This may be the only one or the user
37372dec079SMarius Strobl 	 * data area in case partitions are supported.
37472dec079SMarius Strobl 	 */
37572dec079SMarius Strobl 	ro = mmc_get_read_only(dev);
37672dec079SMarius Strobl 	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
37779f39c6aSMarius Strobl 	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro);
37872dec079SMarius Strobl 
37972dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) < 3)
38072dec079SMarius Strobl 		return (0);
38172dec079SMarius Strobl 
38272dec079SMarius Strobl 	/* Belatedly announce enhanced user data slice. */
38372dec079SMarius Strobl 	if (sc->enh_size != 0) {
38472dec079SMarius Strobl 		bytes = mmcsd_pretty_size(size, unit);
38572dec079SMarius Strobl 		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
38672dec079SMarius Strobl 		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
38772dec079SMarius Strobl 		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
38872dec079SMarius Strobl 		    device_get_nameunit(dev));
38972dec079SMarius Strobl 	}
39072dec079SMarius Strobl 
39172dec079SMarius Strobl 	/*
39272dec079SMarius Strobl 	 * Determine partition switch timeout (provided in units of 10 ms)
39372dec079SMarius Strobl 	 * and ensure it's at least 300 ms as some eMMC chips lie.
39472dec079SMarius Strobl 	 */
39572dec079SMarius Strobl 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
39672dec079SMarius Strobl 	    300 * 1000);
39772dec079SMarius Strobl 
39872dec079SMarius Strobl 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
39972dec079SMarius Strobl 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
400cd85acbaSMarius Strobl 	if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) {
40172dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
40279f39c6aSMarius Strobl 		    MMCSD_FMT_BOOT, 0, size,
40372dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
40472dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
40572dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
40679f39c6aSMarius Strobl 		    MMCSD_FMT_BOOT, 1, size,
40772dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
40872dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
40972dec079SMarius Strobl 	}
41072dec079SMarius Strobl 
41172dec079SMarius Strobl 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
41272dec079SMarius Strobl 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
41372dec079SMarius Strobl 	if (rev >= 5 && size > 0)
41472dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
41579f39c6aSMarius Strobl 		    MMCSD_FMT_RPMB, 0, size, ro);
41672dec079SMarius Strobl 
41772dec079SMarius Strobl 	if (rev <= 3 || comp == FALSE)
41872dec079SMarius Strobl 		return (0);
41972dec079SMarius Strobl 
42072dec079SMarius Strobl 	/*
42172dec079SMarius Strobl 	 * Add general purpose partitions, which are of a multiple of high
42272dec079SMarius Strobl 	 * capacity write protect groups, too.
42372dec079SMarius Strobl 	 */
42472dec079SMarius Strobl 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
42572dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
42672dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
42772dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
42872dec079SMarius Strobl 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
42972dec079SMarius Strobl 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
43072dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
43172dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
43272dec079SMarius Strobl 			if (size == 0)
43372dec079SMarius Strobl 				continue;
43472dec079SMarius Strobl 			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
43579f39c6aSMarius Strobl 			    MMCSD_FMT_GP, i, size * erase_size * wp_size, ro);
43672dec079SMarius Strobl 		}
43772dec079SMarius Strobl 	}
43872dec079SMarius Strobl 	return (0);
43972dec079SMarius Strobl }
44072dec079SMarius Strobl 
44172dec079SMarius Strobl static uintmax_t
44272dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit)
44372dec079SMarius Strobl {
44472dec079SMarius Strobl 	uintmax_t bytes;
44572dec079SMarius Strobl 	int i;
44672dec079SMarius Strobl 
44772dec079SMarius Strobl 	/*
44872dec079SMarius Strobl 	 * Display in most natural units.  There's no card < 1MB.  However,
44972dec079SMarius Strobl 	 * RPMB partitions occasionally are smaller than that, though.  The
45072dec079SMarius Strobl 	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
45172dec079SMarius Strobl 	 * format supports up to 4 GiB and some card makers push it up to this
45272dec079SMarius Strobl 	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
45372dec079SMarius Strobl 	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
45472dec079SMarius Strobl 	 * so we note it in passing here and don't add the code to print TB).
45572dec079SMarius Strobl 	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
45672dec079SMarius Strobl 	 * report them like that.  We also round to the nearest unit, since
45772dec079SMarius Strobl 	 * many cards are a few percent short, even of the power of 10 size.
45872dec079SMarius Strobl 	 */
45972dec079SMarius Strobl 	bytes = size;
46072dec079SMarius Strobl 	unit[0] = unit[1] = '\0';
46172dec079SMarius Strobl 	for (i = 0; i <= 2 && bytes >= 1000; i++) {
46272dec079SMarius Strobl 		bytes = (bytes + 1000 / 2 - 1) / 1000;
46372dec079SMarius Strobl 		switch (i) {
46472dec079SMarius Strobl 		case 0:
46572dec079SMarius Strobl 			unit[0] = 'k';
46672dec079SMarius Strobl 			break;
46772dec079SMarius Strobl 		case 1:
46872dec079SMarius Strobl 			unit[0] = 'M';
46972dec079SMarius Strobl 			break;
47072dec079SMarius Strobl 		case 2:
47172dec079SMarius Strobl 			unit[0] = 'G';
47272dec079SMarius Strobl 			break;
47372dec079SMarius Strobl 		default:
47472dec079SMarius Strobl 			break;
47572dec079SMarius Strobl 		}
47672dec079SMarius Strobl 	}
47772dec079SMarius Strobl 	return (bytes);
47872dec079SMarius Strobl }
47972dec079SMarius Strobl 
48072dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = {
48172dec079SMarius Strobl 	.d_version	= D_VERSION,
48272dec079SMarius Strobl 	.d_name		= "mmcsdrpmb",
48372dec079SMarius Strobl 	.d_ioctl	= mmcsd_ioctl_rpmb
48472dec079SMarius Strobl };
48572dec079SMarius Strobl 
48672dec079SMarius Strobl static void
48772dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
48879f39c6aSMarius Strobl     off_t media_size, bool ro)
48972dec079SMarius Strobl {
49072dec079SMarius Strobl 	struct make_dev_args args;
491cd85acbaSMarius Strobl 	device_t dev, mmcbus;
49272dec079SMarius Strobl 	const char *ext;
49372dec079SMarius Strobl 	const uint8_t *ext_csd;
49472dec079SMarius Strobl 	struct mmcsd_part *part;
49572dec079SMarius Strobl 	struct disk *d;
49672dec079SMarius Strobl 	uintmax_t bytes;
49772dec079SMarius Strobl 	u_int gp;
49872dec079SMarius Strobl 	uint32_t speed;
49972dec079SMarius Strobl 	uint8_t extattr;
50072dec079SMarius Strobl 	bool enh;
50172dec079SMarius Strobl 	char unit[2];
50272dec079SMarius Strobl 
50372dec079SMarius Strobl 	dev = sc->dev;
504cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
50572dec079SMarius Strobl 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
50672dec079SMarius Strobl 	    M_WAITOK | M_ZERO);
50772dec079SMarius Strobl 	part->sc = sc;
50872dec079SMarius Strobl 	part->cnt = cnt;
50972dec079SMarius Strobl 	part->type = type;
51072dec079SMarius Strobl 	part->ro = ro;
51172dec079SMarius Strobl 	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
51272dec079SMarius Strobl 
513aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK_INIT(part);
514aca38eabSMarius Strobl 
515aca38eabSMarius Strobl 	/*
516aca38eabSMarius Strobl 	 * For the RPMB partition, allow IOCTL access only.
517aca38eabSMarius Strobl 	 * NB: If ever attaching RPMB partitions to disk(9), the re-tuning
518aca38eabSMarius Strobl 	 *     implementation and especially its pausing need to be revisited,
519aca38eabSMarius Strobl 	 *     because then re-tuning requests may be issued by the IOCTL half
520aca38eabSMarius Strobl 	 *     of this driver while re-tuning is already paused by the disk(9)
521aca38eabSMarius Strobl 	 *     one and vice versa.
522aca38eabSMarius Strobl 	 */
52372dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
52472dec079SMarius Strobl 		make_dev_args_init(&args);
52572dec079SMarius Strobl 		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
52672dec079SMarius Strobl 		args.mda_devsw = &mmcsd_rpmb_cdevsw;
52772dec079SMarius Strobl 		args.mda_uid = UID_ROOT;
52872dec079SMarius Strobl 		args.mda_gid = GID_OPERATOR;
52972dec079SMarius Strobl 		args.mda_mode = 0640;
53072dec079SMarius Strobl 		args.mda_si_drv1 = part;
53172dec079SMarius Strobl 		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
53272dec079SMarius Strobl 			device_printf(dev, "Failed to make RPMB device\n");
53372dec079SMarius Strobl 			free(part, M_DEVBUF);
53472dec079SMarius Strobl 			return;
53572dec079SMarius Strobl 		}
53672dec079SMarius Strobl 	} else {
537aca38eabSMarius Strobl 		MMCSD_DISK_LOCK_INIT(part);
53872dec079SMarius Strobl 
53972dec079SMarius Strobl 		d = part->disk = disk_alloc();
540a0075e58SWarner Losh 		d->d_close = mmcsd_close;
541a0075e58SWarner Losh 		d->d_strategy = mmcsd_strategy;
54272dec079SMarius Strobl 		d->d_ioctl = mmcsd_ioctl_disk;
543a1fda318SAlexander Motin 		d->d_dump = mmcsd_dump;
54472dec079SMarius Strobl 		d->d_getattr = mmcsd_getattr;
54572dec079SMarius Strobl 		d->d_name = part->name;
54672dec079SMarius Strobl 		d->d_drv1 = part;
547a0075e58SWarner Losh 		d->d_sectorsize = mmc_get_sector_size(dev);
548aca38eabSMarius Strobl 		d->d_maxsize = sc->max_data * d->d_sectorsize;
54972dec079SMarius Strobl 		d->d_mediasize = media_size;
55079f39c6aSMarius Strobl 		d->d_stripesize = sc->erase_sector * d->d_sectorsize;
55172dec079SMarius Strobl 		d->d_unit = cnt;
5523906d42dSAlexander Motin 		d->d_flags = DISKFLAG_CANDELETE;
553646fd30cSMarius Strobl 		if ((sc->flags & MMCSD_FLUSH_CACHE) != 0)
554646fd30cSMarius Strobl 			d->d_flags |= DISKFLAG_CANFLUSHCACHE;
55579f39c6aSMarius Strobl 		d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize;
55672dec079SMarius Strobl 		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
55772dec079SMarius Strobl 		    sizeof(d->d_ident));
55872dec079SMarius Strobl 		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
55972dec079SMarius Strobl 		    sizeof(d->d_descr));
56017160457SAlexander Motin 		d->d_rotation_rate = DISK_RR_NON_ROTATING;
561f3a4b7f7SIan Lepore 
562a0075e58SWarner Losh 		disk_create(d, DISK_VERSION);
56372dec079SMarius Strobl 		bioq_init(&part->bio_queue);
5640e1466acSWarner Losh 
56572dec079SMarius Strobl 		part->running = 1;
56672dec079SMarius Strobl 		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
56772dec079SMarius Strobl 		    "%s%d: mmc/sd card", part->name, cnt);
56872dec079SMarius Strobl 	}
56972dec079SMarius Strobl 
57072dec079SMarius Strobl 	bytes = mmcsd_pretty_size(media_size, unit);
57172dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
572cd85acbaSMarius Strobl 		speed = mmcbr_get_clock(mmcbus);
57372dec079SMarius Strobl 		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
57472dec079SMarius Strobl 		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
575cd85acbaSMarius Strobl 		    ro ? " (read-only)" : "", device_get_nameunit(mmcbus),
57672dec079SMarius Strobl 		    speed / 1000000, (speed / 100000) % 10,
577aca38eabSMarius Strobl 		    mmcsd_bus_bit_width(dev), sc->max_data);
57872dec079SMarius Strobl 	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
57939a289c3SIan Lepore 		printf("%s: %ju%sB partition %d%s at %s\n", part->name, bytes,
58072dec079SMarius Strobl 		    unit, type, ro ? " (read-only)" : "",
58188e07d92SRui Paulo 		    device_get_nameunit(dev));
58272dec079SMarius Strobl 	} else {
58372dec079SMarius Strobl 		enh = false;
58472dec079SMarius Strobl 		ext = NULL;
58572dec079SMarius Strobl 		extattr = 0;
58672dec079SMarius Strobl 		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
58772dec079SMarius Strobl 		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
58872dec079SMarius Strobl 			ext_csd = sc->ext_csd;
58972dec079SMarius Strobl 			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
59072dec079SMarius Strobl 			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
59172dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
59272dec079SMarius Strobl 			    (ext_csd[EXT_CSD_PART_ATTR] &
59372dec079SMarius Strobl 			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
59472dec079SMarius Strobl 				enh = true;
59572dec079SMarius Strobl 			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
59672dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
59772dec079SMarius Strobl 				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
59872dec079SMarius Strobl 				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
59972dec079SMarius Strobl 				switch (extattr) {
60072dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
60172dec079SMarius Strobl 						break;
60272dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
60372dec079SMarius Strobl 						ext = "system code";
60472dec079SMarius Strobl 						break;
60572dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
60672dec079SMarius Strobl 						ext = "non-persistent";
60772dec079SMarius Strobl 						break;
60872dec079SMarius Strobl 					default:
60972dec079SMarius Strobl 						ext = "reserved";
61072dec079SMarius Strobl 						break;
61172dec079SMarius Strobl 				}
61272dec079SMarius Strobl 			}
61372dec079SMarius Strobl 		}
61472dec079SMarius Strobl 		if (ext == NULL)
61539a289c3SIan Lepore 			printf("%s%d: %ju%sB partition %d%s%s at %s\n",
61672dec079SMarius Strobl 			    part->name, cnt, bytes, unit, type, enh ?
61772dec079SMarius Strobl 			    " enhanced" : "", ro ? " (read-only)" : "",
61872dec079SMarius Strobl 			    device_get_nameunit(dev));
61972dec079SMarius Strobl 		else
62039a289c3SIan Lepore 			printf("%s%d: %ju%sB partition %d extended 0x%x "
62172dec079SMarius Strobl 			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
62272dec079SMarius Strobl 			    type, extattr, ext, ro ? " (read-only)" : "",
62372dec079SMarius Strobl 			    device_get_nameunit(dev));
62472dec079SMarius Strobl 	}
62572dec079SMarius Strobl }
626114b4164SWarner Losh 
62772dec079SMarius Strobl static int
62872dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider,
62972dec079SMarius Strobl     struct flash_slice *slices, int *nslices)
63072dec079SMarius Strobl {
63172dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
63272dec079SMarius Strobl 	struct mmcsd_softc *sc;
63372dec079SMarius Strobl 	struct mmcsd_part *part;
63472dec079SMarius Strobl 
63572dec079SMarius Strobl 	*nslices = 0;
63672dec079SMarius Strobl 	if (slices == NULL)
63772dec079SMarius Strobl 		return (ENOMEM);
63872dec079SMarius Strobl 
63972dec079SMarius Strobl 	sc = device_get_softc(dev);
64072dec079SMarius Strobl 	if (sc->enh_size == 0)
64172dec079SMarius Strobl 		return (ENXIO);
64272dec079SMarius Strobl 
64372dec079SMarius Strobl 	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
64472dec079SMarius Strobl 	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
64572dec079SMarius Strobl 	    part->disk->d_unit);
64672dec079SMarius Strobl 	if (strcmp(name, provider) != 0)
64772dec079SMarius Strobl 		return (ENXIO);
64872dec079SMarius Strobl 
64972dec079SMarius Strobl 	*nslices = 1;
65072dec079SMarius Strobl 	slices[0].base = sc->enh_base;
65172dec079SMarius Strobl 	slices[0].size = sc->enh_size;
65272dec079SMarius Strobl 	slices[0].label = MMCSD_LABEL_ENH;
653114b4164SWarner Losh 	return (0);
654114b4164SWarner Losh }
655114b4164SWarner Losh 
656114b4164SWarner Losh static int
657114b4164SWarner Losh mmcsd_detach(device_t dev)
658114b4164SWarner Losh {
6590e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
66072dec079SMarius Strobl 	struct mmcsd_part *part;
66172dec079SMarius Strobl 	int i;
6620e1466acSWarner Losh 
66372dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
66472dec079SMarius Strobl 		part = sc->part[i];
665aca38eabSMarius Strobl 		if (part != NULL) {
666aca38eabSMarius Strobl 			if (part->disk != NULL) {
667aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
66872dec079SMarius Strobl 				part->suspend = 0;
66972dec079SMarius Strobl 				if (part->running > 0) {
670eb67f31aSAlexander Motin 					/* kill thread */
67172dec079SMarius Strobl 					part->running = 0;
67272dec079SMarius Strobl 					wakeup(part);
673eb67f31aSAlexander Motin 					/* wait for thread to finish. */
67472dec079SMarius Strobl 					while (part->running != -1)
675aca38eabSMarius Strobl 						msleep(part, &part->disk_mtx, 0,
676aca38eabSMarius Strobl 						    "mmcsd disk detach", 0);
677eb67f31aSAlexander Motin 				}
678aca38eabSMarius Strobl 				MMCSD_DISK_UNLOCK(part);
679aca38eabSMarius Strobl 			}
680aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
681aca38eabSMarius Strobl 			while (part->ioctl > 0)
682aca38eabSMarius Strobl 				msleep(part, &part->ioctl_mtx, 0,
683aca38eabSMarius Strobl 				    "mmcsd IOCTL detach", 0);
684aca38eabSMarius Strobl 			part->ioctl = -1;
685aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
68672dec079SMarius Strobl 		}
68772dec079SMarius Strobl 	}
6880e1466acSWarner Losh 
68972dec079SMarius Strobl 	if (sc->rpmb_dev != NULL)
69072dec079SMarius Strobl 		destroy_dev(sc->rpmb_dev);
69172dec079SMarius Strobl 
69272dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
69372dec079SMarius Strobl 		part = sc->part[i];
69472dec079SMarius Strobl 		if (part != NULL) {
69572dec079SMarius Strobl 			if (part->disk != NULL) {
696249b0e85SAlexander Motin 				/* Flush the request queue. */
69772dec079SMarius Strobl 				bioq_flush(&part->bio_queue, NULL, ENXIO);
6980e1466acSWarner Losh 				/* kill disk */
69972dec079SMarius Strobl 				disk_destroy(part->disk);
7000e1466acSWarner Losh 
701aca38eabSMarius Strobl 				MMCSD_DISK_LOCK_DESTROY(part);
70272dec079SMarius Strobl 			}
703aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK_DESTROY(part);
70472dec079SMarius Strobl 			free(part, M_DEVBUF);
70572dec079SMarius Strobl 		}
70672dec079SMarius Strobl 	}
707646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
708646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
709646fd30cSMarius Strobl 	return (0);
710646fd30cSMarius Strobl }
711646fd30cSMarius Strobl 
712646fd30cSMarius Strobl static int
713646fd30cSMarius Strobl mmcsd_shutdown(device_t dev)
714646fd30cSMarius Strobl {
715646fd30cSMarius Strobl 	struct mmcsd_softc *sc = device_get_softc(dev);
716646fd30cSMarius Strobl 
717646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
718646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
719128d0ff2SWarner Losh 	return (0);
720114b4164SWarner Losh }
721114b4164SWarner Losh 
722114b4164SWarner Losh static int
723eb67f31aSAlexander Motin mmcsd_suspend(device_t dev)
724eb67f31aSAlexander Motin {
725eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
72672dec079SMarius Strobl 	struct mmcsd_part *part;
72772dec079SMarius Strobl 	int i;
728eb67f31aSAlexander Motin 
72972dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
73072dec079SMarius Strobl 		part = sc->part[i];
731aca38eabSMarius Strobl 		if (part != NULL) {
732aca38eabSMarius Strobl 			if (part->disk != NULL) {
733aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
73472dec079SMarius Strobl 				part->suspend = 1;
73572dec079SMarius Strobl 				if (part->running > 0) {
736eb67f31aSAlexander Motin 					/* kill thread */
73772dec079SMarius Strobl 					part->running = 0;
73872dec079SMarius Strobl 					wakeup(part);
739eb67f31aSAlexander Motin 					/* wait for thread to finish. */
74072dec079SMarius Strobl 					while (part->running != -1)
741aca38eabSMarius Strobl 						msleep(part, &part->disk_mtx, 0,
742aca38eabSMarius Strobl 						    "mmcsd disk suspension", 0);
743eb67f31aSAlexander Motin 				}
744aca38eabSMarius Strobl 				MMCSD_DISK_UNLOCK(part);
745aca38eabSMarius Strobl 			}
746aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
747aca38eabSMarius Strobl 			while (part->ioctl > 0)
748aca38eabSMarius Strobl 				msleep(part, &part->ioctl_mtx, 0,
749aca38eabSMarius Strobl 				    "mmcsd IOCTL suspension", 0);
750aca38eabSMarius Strobl 			part->ioctl = -1;
751aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
75272dec079SMarius Strobl 		}
75372dec079SMarius Strobl 	}
754646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
755646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
756eb67f31aSAlexander Motin 	return (0);
757eb67f31aSAlexander Motin }
758eb67f31aSAlexander Motin 
759eb67f31aSAlexander Motin static int
760eb67f31aSAlexander Motin mmcsd_resume(device_t dev)
761eb67f31aSAlexander Motin {
762eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
76372dec079SMarius Strobl 	struct mmcsd_part *part;
76472dec079SMarius Strobl 	int i;
765eb67f31aSAlexander Motin 
76672dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
76772dec079SMarius Strobl 		part = sc->part[i];
768aca38eabSMarius Strobl 		if (part != NULL) {
769aca38eabSMarius Strobl 			if (part->disk != NULL) {
770aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
77172dec079SMarius Strobl 				part->suspend = 0;
77272dec079SMarius Strobl 				if (part->running <= 0) {
77372dec079SMarius Strobl 					part->running = 1;
774aca38eabSMarius Strobl 					MMCSD_DISK_UNLOCK(part);
775aca38eabSMarius Strobl 					kproc_create(&mmcsd_task, part,
776aca38eabSMarius Strobl 					    &part->p, 0, 0, "%s%d: mmc/sd card",
777aca38eabSMarius Strobl 					    part->name, part->cnt);
778eb67f31aSAlexander Motin 				} else
779aca38eabSMarius Strobl 					MMCSD_DISK_UNLOCK(part);
780aca38eabSMarius Strobl 			}
781aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
782aca38eabSMarius Strobl 			part->ioctl = 0;
783aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
78472dec079SMarius Strobl 		}
78572dec079SMarius Strobl 	}
786eb67f31aSAlexander Motin 	return (0);
787eb67f31aSAlexander Motin }
788eb67f31aSAlexander Motin 
789eb67f31aSAlexander Motin static int
790646fd30cSMarius Strobl mmcsd_close(struct disk *dp)
791114b4164SWarner Losh {
792646fd30cSMarius Strobl 	struct mmcsd_softc *sc;
793ae5d8757SMarius Strobl 
794646fd30cSMarius Strobl 	if ((dp->d_flags & DISKFLAG_OPEN) != 0) {
795646fd30cSMarius Strobl 		sc = ((struct mmcsd_part *)dp->d_drv1)->sc;
796646fd30cSMarius Strobl 		if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
797646fd30cSMarius Strobl 			device_printf(sc->dev, "failed to flush cache\n");
798114b4164SWarner Losh 	}
799128d0ff2SWarner Losh 	return (0);
800114b4164SWarner Losh }
801114b4164SWarner Losh 
802114b4164SWarner Losh static void
803114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
804114b4164SWarner Losh {
80572dec079SMarius Strobl 	struct mmcsd_part *part;
806114b4164SWarner Losh 
80772dec079SMarius Strobl 	part = bp->bio_disk->d_drv1;
808aca38eabSMarius Strobl 	MMCSD_DISK_LOCK(part);
80972dec079SMarius Strobl 	if (part->running > 0 || part->suspend > 0) {
81072dec079SMarius Strobl 		bioq_disksort(&part->bio_queue, bp);
811aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
81272dec079SMarius Strobl 		wakeup(part);
813249b0e85SAlexander Motin 	} else {
814aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
815249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
816249b0e85SAlexander Motin 	}
817114b4164SWarner Losh }
818114b4164SWarner Losh 
81972dec079SMarius Strobl static int
82072dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
8217217ea7cSMarius Strobl     int fflag, struct thread *td)
82272dec079SMarius Strobl {
82372dec079SMarius Strobl 
8247217ea7cSMarius Strobl 	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag, td));
82572dec079SMarius Strobl }
82672dec079SMarius Strobl 
82772dec079SMarius Strobl static int
82872dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
8297217ea7cSMarius Strobl     struct thread *td)
83072dec079SMarius Strobl {
83172dec079SMarius Strobl 
8327217ea7cSMarius Strobl 	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag, td));
83372dec079SMarius Strobl }
83472dec079SMarius Strobl 
83572dec079SMarius Strobl static int
8367217ea7cSMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag,
8377217ea7cSMarius Strobl     struct thread *td)
83872dec079SMarius Strobl {
83972dec079SMarius Strobl 	struct mmc_ioc_cmd *mic;
84072dec079SMarius Strobl 	struct mmc_ioc_multi_cmd *mimc;
84172dec079SMarius Strobl 	int i, err;
84272dec079SMarius Strobl 	u_long cnt, size;
84372dec079SMarius Strobl 
84472dec079SMarius Strobl 	if ((fflag & FREAD) == 0)
84572dec079SMarius Strobl 		return (EBADF);
84672dec079SMarius Strobl 
8477217ea7cSMarius Strobl 	err = priv_check(td, PRIV_DRIVER);
8487217ea7cSMarius Strobl 	if (err != 0)
8497217ea7cSMarius Strobl 		return (err);
8507217ea7cSMarius Strobl 
85172dec079SMarius Strobl 	err = 0;
85272dec079SMarius Strobl 	switch (cmd) {
85372dec079SMarius Strobl 	case MMC_IOC_CMD:
85472dec079SMarius Strobl 		mic = data;
855aca38eabSMarius Strobl 		err = mmcsd_ioctl_cmd(part, mic, fflag);
85672dec079SMarius Strobl 		break;
857aca38eabSMarius Strobl 	case MMC_IOC_MULTI_CMD:
85872dec079SMarius Strobl 		mimc = data;
85972dec079SMarius Strobl 		if (mimc->num_of_cmds == 0)
86072dec079SMarius Strobl 			break;
86172dec079SMarius Strobl 		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
86272dec079SMarius Strobl 			return (EINVAL);
86372dec079SMarius Strobl 		cnt = mimc->num_of_cmds;
86472dec079SMarius Strobl 		size = sizeof(*mic) * cnt;
86572dec079SMarius Strobl 		mic = malloc(size, M_TEMP, M_WAITOK);
86672dec079SMarius Strobl 		err = copyin((const void *)mimc->cmds, mic, size);
867aca38eabSMarius Strobl 		if (err == 0) {
86872dec079SMarius Strobl 			for (i = 0; i < cnt; i++) {
86972dec079SMarius Strobl 				err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
87072dec079SMarius Strobl 				if (err != 0)
87172dec079SMarius Strobl 					break;
87272dec079SMarius Strobl 			}
873aca38eabSMarius Strobl 		}
87472dec079SMarius Strobl 		free(mic, M_TEMP);
87572dec079SMarius Strobl 		break;
87672dec079SMarius Strobl 	default:
87772dec079SMarius Strobl 		return (ENOIOCTL);
87872dec079SMarius Strobl 	}
87972dec079SMarius Strobl 	return (err);
88072dec079SMarius Strobl }
88172dec079SMarius Strobl 
88272dec079SMarius Strobl static int
88372dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
88472dec079SMarius Strobl {
88572dec079SMarius Strobl 	struct mmc_command cmd;
88672dec079SMarius Strobl 	struct mmc_data data;
88772dec079SMarius Strobl 	struct mmcsd_softc *sc;
888cd85acbaSMarius Strobl 	device_t dev, mmcbus;
88972dec079SMarius Strobl 	void *dp;
89072dec079SMarius Strobl 	u_long len;
89172dec079SMarius Strobl 	int err, retries;
89272dec079SMarius Strobl 	uint32_t status;
89372dec079SMarius Strobl 	uint16_t rca;
89472dec079SMarius Strobl 
89572dec079SMarius Strobl 	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
89672dec079SMarius Strobl 		return (EBADF);
89772dec079SMarius Strobl 
89872dec079SMarius Strobl 	if (part->ro == TRUE && mic->write_flag != 0)
89972dec079SMarius Strobl 		return (EROFS);
90072dec079SMarius Strobl 
901aca38eabSMarius Strobl 	/*
902aca38eabSMarius Strobl 	 * We don't need to explicitly lock against the disk(9) half of this
903aca38eabSMarius Strobl 	 * driver as MMCBUS_ACQUIRE_BUS() will serialize us.  However, it's
904aca38eabSMarius Strobl 	 * necessary to protect against races with detachment and suspension,
905aca38eabSMarius Strobl 	 * especially since it's required to switch away from RPMB partitions
906aca38eabSMarius Strobl 	 * again after an access (see mmcsd_switch_part()).
907aca38eabSMarius Strobl 	 */
908aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK(part);
909aca38eabSMarius Strobl 	while (part->ioctl != 0) {
910aca38eabSMarius Strobl 		if (part->ioctl < 0) {
911aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
912aca38eabSMarius Strobl 			return (ENXIO);
913aca38eabSMarius Strobl 		}
914aca38eabSMarius Strobl 		msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0);
915aca38eabSMarius Strobl 	}
916aca38eabSMarius Strobl 	part->ioctl = 1;
917aca38eabSMarius Strobl 	MMCSD_IOCTL_UNLOCK(part);
918aca38eabSMarius Strobl 
91972dec079SMarius Strobl 	err = 0;
92072dec079SMarius Strobl 	dp = NULL;
92172dec079SMarius Strobl 	len = mic->blksz * mic->blocks;
922aca38eabSMarius Strobl 	if (len > MMC_IOC_MAX_BYTES) {
923aca38eabSMarius Strobl 		err = EOVERFLOW;
924aca38eabSMarius Strobl 		goto out;
925aca38eabSMarius Strobl 	}
92672dec079SMarius Strobl 	if (len != 0) {
92772dec079SMarius Strobl 		dp = malloc(len, M_TEMP, M_WAITOK);
92872dec079SMarius Strobl 		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
92972dec079SMarius Strobl 		if (err != 0)
93072dec079SMarius Strobl 			goto out;
93172dec079SMarius Strobl 	}
93272dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
93372dec079SMarius Strobl 	memset(&data, 0, sizeof(data));
93472dec079SMarius Strobl 	cmd.opcode = mic->opcode;
93572dec079SMarius Strobl 	cmd.arg = mic->arg;
93672dec079SMarius Strobl 	cmd.flags = mic->flags;
93772dec079SMarius Strobl 	if (len != 0) {
93872dec079SMarius Strobl 		data.len = len;
93972dec079SMarius Strobl 		data.data = dp;
94072dec079SMarius Strobl 		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
94172dec079SMarius Strobl 		    MMC_DATA_READ;
94272dec079SMarius Strobl 		cmd.data = &data;
94372dec079SMarius Strobl 	}
94472dec079SMarius Strobl 	sc = part->sc;
94572dec079SMarius Strobl 	rca = sc->rca;
94672dec079SMarius Strobl 	if (mic->is_acmd == 0) {
94772dec079SMarius Strobl 		/* Enforce/patch/restrict RCA-based commands */
94872dec079SMarius Strobl 		switch (cmd.opcode) {
94972dec079SMarius Strobl 		case MMC_SET_RELATIVE_ADDR:
95072dec079SMarius Strobl 		case MMC_SELECT_CARD:
95172dec079SMarius Strobl 			err = EPERM;
95272dec079SMarius Strobl 			goto out;
95372dec079SMarius Strobl 		case MMC_STOP_TRANSMISSION:
95472dec079SMarius Strobl 			if ((cmd.arg & 0x1) == 0)
95572dec079SMarius Strobl 				break;
95672dec079SMarius Strobl 			/* FALLTHROUGH */
95772dec079SMarius Strobl 		case MMC_SLEEP_AWAKE:
95872dec079SMarius Strobl 		case MMC_SEND_CSD:
95972dec079SMarius Strobl 		case MMC_SEND_CID:
96072dec079SMarius Strobl 		case MMC_SEND_STATUS:
96172dec079SMarius Strobl 		case MMC_GO_INACTIVE_STATE:
96272dec079SMarius Strobl 		case MMC_FAST_IO:
96372dec079SMarius Strobl 		case MMC_APP_CMD:
96472dec079SMarius Strobl 			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
96572dec079SMarius Strobl 			break;
96672dec079SMarius Strobl 		default:
96772dec079SMarius Strobl 			break;
96872dec079SMarius Strobl 		}
9696c06949fSMarius Strobl 		/*
9706c06949fSMarius Strobl 		 * No partition switching in userland; it's almost impossible
9716c06949fSMarius Strobl 		 * to recover from that, especially if things go wrong.
9726c06949fSMarius Strobl 		 */
9736c06949fSMarius Strobl 		if (cmd.opcode == MMC_SWITCH_FUNC && dp != NULL &&
9746c06949fSMarius Strobl 		    (((uint8_t *)dp)[EXT_CSD_PART_CONFIG] &
97561ef738dSMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_MASK) != part->type) {
9766c06949fSMarius Strobl 			err = EINVAL;
9776c06949fSMarius Strobl 			goto out;
9786c06949fSMarius Strobl 		}
97972dec079SMarius Strobl 	}
98072dec079SMarius Strobl 	dev = sc->dev;
981cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
982cd85acbaSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
983cd85acbaSMarius Strobl 	err = mmcsd_switch_part(mmcbus, dev, rca, part->type);
98472dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
98572dec079SMarius Strobl 		goto release;
98672dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
98772dec079SMarius Strobl 		err = mmcsd_set_blockcount(sc, mic->blocks,
98872dec079SMarius Strobl 		    mic->write_flag & (1 << 31));
98972dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
990aca38eabSMarius Strobl 			goto switch_back;
99172dec079SMarius Strobl 	}
992646fd30cSMarius Strobl 	if (mic->write_flag != 0)
993646fd30cSMarius Strobl 		sc->flags |= MMCSD_DIRTY;
99472dec079SMarius Strobl 	if (mic->is_acmd != 0)
995cd85acbaSMarius Strobl 		(void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0);
99672dec079SMarius Strobl 	else
997cd85acbaSMarius Strobl 		(void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0);
99872dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
99972dec079SMarius Strobl 		/*
100072dec079SMarius Strobl 		 * If the request went to the RPMB partition, try to ensure
10016c06949fSMarius Strobl 		 * that the command actually has completed.
100272dec079SMarius Strobl 		 */
100372dec079SMarius Strobl 		retries = MMCSD_CMD_RETRIES;
100472dec079SMarius Strobl 		do {
1005cd85acbaSMarius Strobl 			err = mmc_send_status(mmcbus, dev, rca, &status);
100672dec079SMarius Strobl 			if (err != MMC_ERR_NONE)
100772dec079SMarius Strobl 				break;
100872dec079SMarius Strobl 			if (R1_STATUS(status) == 0 &&
100972dec079SMarius Strobl 			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
101072dec079SMarius Strobl 				break;
101172dec079SMarius Strobl 			DELAY(1000);
101272dec079SMarius Strobl 		} while (retries-- > 0);
101372dec079SMarius Strobl 	}
101472dec079SMarius Strobl 	/*
101572dec079SMarius Strobl 	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
101672dec079SMarius Strobl 	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
101772dec079SMarius Strobl 	 * so retrieve EXT_CSD again.
101872dec079SMarius Strobl 	 */
101972dec079SMarius Strobl 	if (cmd.opcode == MMC_SWITCH_FUNC) {
1020cd85acbaSMarius Strobl 		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
102172dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
102272dec079SMarius Strobl 			goto release;
102372dec079SMarius Strobl 	}
10246c06949fSMarius Strobl switch_back:
10256c06949fSMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
10266c06949fSMarius Strobl 		/*
10276c06949fSMarius Strobl 		 * If the request went to the RPMB partition, always switch
10286c06949fSMarius Strobl 		 * back to the default partition (see mmcsd_switch_part()).
10296c06949fSMarius Strobl 		 */
10306c06949fSMarius Strobl 		err = mmcsd_switch_part(mmcbus, dev, rca,
10316c06949fSMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
10326c06949fSMarius Strobl 		if (err != MMC_ERR_NONE)
10336c06949fSMarius Strobl 			goto release;
10346c06949fSMarius Strobl 	}
1035cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
103672dec079SMarius Strobl 	if (cmd.error != MMC_ERR_NONE) {
103772dec079SMarius Strobl 		switch (cmd.error) {
103872dec079SMarius Strobl 		case MMC_ERR_TIMEOUT:
103972dec079SMarius Strobl 			err = ETIMEDOUT;
104072dec079SMarius Strobl 			break;
104172dec079SMarius Strobl 		case MMC_ERR_BADCRC:
104272dec079SMarius Strobl 			err = EILSEQ;
104372dec079SMarius Strobl 			break;
104472dec079SMarius Strobl 		case MMC_ERR_INVALID:
104572dec079SMarius Strobl 			err = EINVAL;
104672dec079SMarius Strobl 			break;
104772dec079SMarius Strobl 		case MMC_ERR_NO_MEMORY:
104872dec079SMarius Strobl 			err = ENOMEM;
104972dec079SMarius Strobl 			break;
105072dec079SMarius Strobl 		default:
105172dec079SMarius Strobl 			err = EIO;
105272dec079SMarius Strobl 			break;
105372dec079SMarius Strobl 		}
105472dec079SMarius Strobl 		goto out;
105572dec079SMarius Strobl 	}
105672dec079SMarius Strobl 	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
105772dec079SMarius Strobl 	if (mic->write_flag == 0 && len != 0) {
105872dec079SMarius Strobl 		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
105972dec079SMarius Strobl 		if (err != 0)
106072dec079SMarius Strobl 			goto out;
106172dec079SMarius Strobl 	}
106272dec079SMarius Strobl 	goto out;
106372dec079SMarius Strobl 
106472dec079SMarius Strobl release:
1065cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
106672dec079SMarius Strobl 	err = EIO;
106772dec079SMarius Strobl 
106872dec079SMarius Strobl out:
1069aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK(part);
1070aca38eabSMarius Strobl 	part->ioctl = 0;
1071aca38eabSMarius Strobl 	MMCSD_IOCTL_UNLOCK(part);
1072aca38eabSMarius Strobl 	wakeup(part);
107372dec079SMarius Strobl 	if (dp != NULL)
107472dec079SMarius Strobl 		free(dp, M_TEMP);
107572dec079SMarius Strobl 	return (err);
107672dec079SMarius Strobl }
107772dec079SMarius Strobl 
107872dec079SMarius Strobl static int
107972dec079SMarius Strobl mmcsd_getattr(struct bio *bp)
108072dec079SMarius Strobl {
108172dec079SMarius Strobl 	struct mmcsd_part *part;
108272dec079SMarius Strobl 	device_t dev;
108372dec079SMarius Strobl 
108472dec079SMarius Strobl 	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
108572dec079SMarius Strobl 		if (bp->bio_length != sizeof(dev))
108672dec079SMarius Strobl 			return (EFAULT);
108772dec079SMarius Strobl 		part = bp->bio_disk->d_drv1;
108872dec079SMarius Strobl 		dev = part->sc->dev;
108972dec079SMarius Strobl 		bcopy(&dev, bp->bio_data, sizeof(dev));
109072dec079SMarius Strobl 		bp->bio_completed = bp->bio_length;
109172dec079SMarius Strobl 		return (0);
109272dec079SMarius Strobl 	}
109372dec079SMarius Strobl 	return (-1);
109472dec079SMarius Strobl }
109572dec079SMarius Strobl 
109672dec079SMarius Strobl static int
109772dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
109872dec079SMarius Strobl {
109972dec079SMarius Strobl 	struct mmc_command cmd;
110072dec079SMarius Strobl 	struct mmc_request req;
110172dec079SMarius Strobl 
110272dec079SMarius Strobl 	memset(&req, 0, sizeof(req));
110372dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
110472dec079SMarius Strobl 	cmd.mrq = &req;
110572dec079SMarius Strobl 	req.cmd = &cmd;
110672dec079SMarius Strobl 	cmd.opcode = MMC_SET_BLOCK_COUNT;
110772dec079SMarius Strobl 	cmd.arg = count & 0x0000FFFF;
110872dec079SMarius Strobl 	if (reliable)
110972dec079SMarius Strobl 		cmd.arg |= 1 << 31;
111072dec079SMarius Strobl 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1111cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req);
111272dec079SMarius Strobl 	return (cmd.error);
111372dec079SMarius Strobl }
111472dec079SMarius Strobl 
111572dec079SMarius Strobl static int
111672dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
111772dec079SMarius Strobl {
111872dec079SMarius Strobl 	struct mmcsd_softc *sc;
111972dec079SMarius Strobl 	int err;
112072dec079SMarius Strobl 	uint8_t	value;
112172dec079SMarius Strobl 
112272dec079SMarius Strobl 	sc = device_get_softc(dev);
112372dec079SMarius Strobl 
1124aca38eabSMarius Strobl 	if (sc->mode == mode_sd)
112572dec079SMarius Strobl 		return (MMC_ERR_NONE);
112672dec079SMarius Strobl 
1127aca38eabSMarius Strobl 	/*
1128aca38eabSMarius Strobl 	 * According to section "6.2.2 Command restrictions" of the eMMC
1129aca38eabSMarius Strobl 	 * specification v5.1, CMD19/CMD21 aren't allowed to be used with
1130aca38eabSMarius Strobl 	 * RPMB partitions.  So we pause re-tuning along with triggering
1131aca38eabSMarius Strobl 	 * it up-front to decrease the likelihood of re-tuning becoming
1132aca38eabSMarius Strobl 	 * necessary while accessing an RPMB partition.  Consequently, an
1133aca38eabSMarius Strobl 	 * RPMB partition should immediately be switched away from again
1134aca38eabSMarius Strobl 	 * after an access in order to allow for re-tuning to take place
1135aca38eabSMarius Strobl 	 * anew.
1136aca38eabSMarius Strobl 	 */
1137aca38eabSMarius Strobl 	if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1138cd85acbaSMarius Strobl 		MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true);
1139aca38eabSMarius Strobl 
1140aca38eabSMarius Strobl 	if (sc->part_curr == part)
114172dec079SMarius Strobl 		return (MMC_ERR_NONE);
114272dec079SMarius Strobl 
114372dec079SMarius Strobl 	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
114472dec079SMarius Strobl 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
114572dec079SMarius Strobl 	/* Jump! */
114672dec079SMarius Strobl 	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
114772dec079SMarius Strobl 	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
1148aca38eabSMarius Strobl 	if (err != MMC_ERR_NONE) {
1149aca38eabSMarius Strobl 		if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1150cd85acbaSMarius Strobl 			MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
115172dec079SMarius Strobl 		return (err);
1152aca38eabSMarius Strobl 	}
115372dec079SMarius Strobl 
115472dec079SMarius Strobl 	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
1155aca38eabSMarius Strobl 	if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1156cd85acbaSMarius Strobl 		MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
115772dec079SMarius Strobl 	sc->part_curr = part;
115872dec079SMarius Strobl 	return (MMC_ERR_NONE);
115972dec079SMarius Strobl }
116072dec079SMarius Strobl 
1161bb1ef63fSWarner Losh static const char *
1162bb1ef63fSWarner Losh mmcsd_errmsg(int e)
1163bb1ef63fSWarner Losh {
11641bacf3beSMarius Strobl 
1165bb1ef63fSWarner Losh 	if (e < 0 || e > MMC_ERR_MAX)
1166bb1ef63fSWarner Losh 		return "Bad error code";
1167aca38eabSMarius Strobl 	return (errmsg[e]);
1168bb1ef63fSWarner Losh }
1169bb1ef63fSWarner Losh 
11703906d42dSAlexander Motin static daddr_t
117172dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
1172114b4164SWarner Losh {
1173114b4164SWarner Losh 	daddr_t block, end;
1174114b4164SWarner Losh 	struct mmc_command cmd;
1175114b4164SWarner Losh 	struct mmc_command stop;
1176114b4164SWarner Losh 	struct mmc_request req;
1177114b4164SWarner Losh 	struct mmc_data data;
117872dec079SMarius Strobl 	struct mmcsd_softc *sc;
1179cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1180aca38eabSMarius Strobl 	u_int numblocks, sz;
11811bacf3beSMarius Strobl 	char *vaddr;
1182114b4164SWarner Losh 
118372dec079SMarius Strobl 	sc = part->sc;
118472dec079SMarius Strobl 	dev = sc->dev;
1185cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
118672dec079SMarius Strobl 
11873906d42dSAlexander Motin 	block = bp->bio_pblkno;
118872dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
1189114b4164SWarner Losh 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
11903906d42dSAlexander Motin 	while (block < end) {
11911bacf3beSMarius Strobl 		vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
1192aca38eabSMarius Strobl 		numblocks = min(end - block, sc->max_data);
1193114b4164SWarner Losh 		memset(&req, 0, sizeof(req));
1194114b4164SWarner Losh 		memset(&cmd, 0, sizeof(cmd));
1195114b4164SWarner Losh 		memset(&stop, 0, sizeof(stop));
1196ed7142a7SIan Lepore 		memset(&data, 0, sizeof(data));
1197a350e540SIan Lepore 		cmd.mrq = &req;
1198114b4164SWarner Losh 		req.cmd = &cmd;
1199114b4164SWarner Losh 		cmd.data = &data;
1200114b4164SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
12010c0903b1SWarner Losh 			if (numblocks > 1)
1202114b4164SWarner Losh 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1203114b4164SWarner Losh 			else
1204114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
12050c0903b1SWarner Losh 		} else {
1206646fd30cSMarius Strobl 			sc->flags |= MMCSD_DIRTY;
12070c0903b1SWarner Losh 			if (numblocks > 1)
12080c0903b1SWarner Losh 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
12090c0903b1SWarner Losh 			else
1210114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
12110c0903b1SWarner Losh 		}
1212c18f1e26SAlexander Motin 		cmd.arg = block;
1213aca38eabSMarius Strobl 		if (sc->high_cap == 0)
1214c18f1e26SAlexander Motin 			cmd.arg <<= 9;
1215114b4164SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1216114b4164SWarner Losh 		data.data = vaddr;
1217114b4164SWarner Losh 		data.mrq = &req;
12180c0903b1SWarner Losh 		if (bp->bio_cmd == BIO_READ)
1219114b4164SWarner Losh 			data.flags = MMC_DATA_READ;
12200c0903b1SWarner Losh 		else
1221114b4164SWarner Losh 			data.flags = MMC_DATA_WRITE;
12220c0903b1SWarner Losh 		data.len = numblocks * sz;
12230c0903b1SWarner Losh 		if (numblocks > 1) {
12240c0903b1SWarner Losh 			data.flags |= MMC_DATA_MULTI;
1225114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
1226114b4164SWarner Losh 			stop.arg = 0;
1227114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1228a350e540SIan Lepore 			stop.mrq = &req;
12290c0903b1SWarner Losh 			req.stop = &stop;
12300c0903b1SWarner Losh 		}
1231cd85acbaSMarius Strobl 		MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1232bb1ef63fSWarner Losh 		if (req.cmd->error != MMC_ERR_NONE) {
12331bacf3beSMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
12341bacf3beSMarius Strobl 			    LOG_PPS))
1235bb1ef63fSWarner Losh 				device_printf(dev, "Error indicated: %d %s\n",
12361bacf3beSMarius Strobl 				    req.cmd->error,
12371bacf3beSMarius Strobl 				    mmcsd_errmsg(req.cmd->error));
12380c0903b1SWarner Losh 			break;
1239bb1ef63fSWarner Losh 		}
12400c0903b1SWarner Losh 		block += numblocks;
1241114b4164SWarner Losh 	}
12423906d42dSAlexander Motin 	return (block);
12433906d42dSAlexander Motin }
12443906d42dSAlexander Motin 
12453906d42dSAlexander Motin static daddr_t
124672dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
12473906d42dSAlexander Motin {
12483906d42dSAlexander Motin 	daddr_t block, end, start, stop;
12493906d42dSAlexander Motin 	struct mmc_command cmd;
12503906d42dSAlexander Motin 	struct mmc_request req;
125172dec079SMarius Strobl 	struct mmcsd_softc *sc;
1252cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1253aca38eabSMarius Strobl 	u_int erase_sector, sz;
125479f39c6aSMarius Strobl 	int err;
125579f39c6aSMarius Strobl 	bool use_trim;
125672dec079SMarius Strobl 
125772dec079SMarius Strobl 	sc = part->sc;
125872dec079SMarius Strobl 	dev = sc->dev;
1259cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
12603906d42dSAlexander Motin 
12613906d42dSAlexander Motin 	block = bp->bio_pblkno;
126272dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
12633906d42dSAlexander Motin 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
126479f39c6aSMarius Strobl 	use_trim = sc->flags & MMCSD_USE_TRIM;
126579f39c6aSMarius Strobl 	if (use_trim == true) {
126679f39c6aSMarius Strobl 		start = block;
126779f39c6aSMarius Strobl 		stop = end;
126879f39c6aSMarius Strobl 	} else {
126979f39c6aSMarius Strobl 		/* Coalesce with the remainder of the previous request. */
127072dec079SMarius Strobl 		if (block > part->eblock && block <= part->eend)
127172dec079SMarius Strobl 			block = part->eblock;
127272dec079SMarius Strobl 		if (end >= part->eblock && end < part->eend)
127372dec079SMarius Strobl 			end = part->eend;
127479f39c6aSMarius Strobl 		/* Safely round to the erase sector boundaries. */
1275aca38eabSMarius Strobl 		erase_sector = sc->erase_sector;
12763906d42dSAlexander Motin 		start = block + erase_sector - 1;	 /* Round up. */
12773906d42dSAlexander Motin 		start -= start % erase_sector;
12783906d42dSAlexander Motin 		stop = end;				/* Round down. */
12793906d42dSAlexander Motin 		stop -= end % erase_sector;
128079f39c6aSMarius Strobl 		/*
128179f39c6aSMarius Strobl 		 * We can't erase an area smaller than an erase sector, so
128279f39c6aSMarius Strobl 		 * store it for later.
128379f39c6aSMarius Strobl 		 */
1284a7cf6274SAlexander Motin 		if (start >= stop) {
128572dec079SMarius Strobl 			part->eblock = block;
128672dec079SMarius Strobl 			part->eend = end;
12873906d42dSAlexander Motin 			return (end);
1288a7cf6274SAlexander Motin 		}
128979f39c6aSMarius Strobl 	}
129079f39c6aSMarius Strobl 
129179f39c6aSMarius Strobl 	if ((sc->flags & MMCSD_INAND_CMD38) != 0) {
129279f39c6aSMarius Strobl 		err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL,
129379f39c6aSMarius Strobl 		    EXT_CSD_INAND_CMD38, use_trim == true ?
129479f39c6aSMarius Strobl 		    EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE,
129579f39c6aSMarius Strobl 		    sc->cmd6_time, true);
129679f39c6aSMarius Strobl 		if (err != MMC_ERR_NONE) {
129779f39c6aSMarius Strobl 			device_printf(dev,
129879f39c6aSMarius Strobl 			    "Setting iNAND erase command failed %s\n",
129979f39c6aSMarius Strobl 			    mmcsd_errmsg(err));
130079f39c6aSMarius Strobl 			return (block);
130179f39c6aSMarius Strobl 		}
130279f39c6aSMarius Strobl 	}
13033906d42dSAlexander Motin 
1304aca38eabSMarius Strobl 	/*
1305aca38eabSMarius Strobl 	 * Pause re-tuning so it won't interfere with the order of erase
1306aca38eabSMarius Strobl 	 * commands.  Note that these latter don't use the data lines, so
1307aca38eabSMarius Strobl 	 * re-tuning shouldn't actually become necessary during erase.
1308aca38eabSMarius Strobl 	 */
1309cd85acbaSMarius Strobl 	MMCBUS_RETUNE_PAUSE(mmcbus, dev, false);
13103906d42dSAlexander Motin 	/* Set erase start position. */
13113906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13123906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
1313a350e540SIan Lepore 	cmd.mrq = &req;
13143906d42dSAlexander Motin 	req.cmd = &cmd;
1315608226d5SMarius Strobl 	if (sc->mode == mode_sd)
13163906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_START;
13173906d42dSAlexander Motin 	else
13183906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_START;
13193906d42dSAlexander Motin 	cmd.arg = start;
1320aca38eabSMarius Strobl 	if (sc->high_cap == 0)
13213906d42dSAlexander Motin 		cmd.arg <<= 9;
13223906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1323cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13243906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
132579f39c6aSMarius Strobl 		device_printf(dev, "Setting erase start position failed %s\n",
132679f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1327aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1328aca38eabSMarius Strobl 		goto unpause;
13293906d42dSAlexander Motin 	}
13303906d42dSAlexander Motin 	/* Set erase stop position. */
13313906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13323906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
13333906d42dSAlexander Motin 	req.cmd = &cmd;
1334608226d5SMarius Strobl 	if (sc->mode == mode_sd)
13353906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_END;
13363906d42dSAlexander Motin 	else
13373906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_END;
13383906d42dSAlexander Motin 	cmd.arg = stop;
1339aca38eabSMarius Strobl 	if (sc->high_cap == 0)
13403906d42dSAlexander Motin 		cmd.arg <<= 9;
13413906d42dSAlexander Motin 	cmd.arg--;
13423906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1343cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13443906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
134579f39c6aSMarius Strobl 		device_printf(dev, "Setting erase stop position failed %s\n",
134679f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1347aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1348aca38eabSMarius Strobl 		goto unpause;
13493906d42dSAlexander Motin 	}
13503906d42dSAlexander Motin 	/* Erase range. */
13513906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13523906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
13533906d42dSAlexander Motin 	req.cmd = &cmd;
13543906d42dSAlexander Motin 	cmd.opcode = MMC_ERASE;
135579f39c6aSMarius Strobl 	cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE;
13563906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1357cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13583906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
135979f39c6aSMarius Strobl 		device_printf(dev, "Issuing erase command failed %s\n",
136079f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1361aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1362aca38eabSMarius Strobl 		goto unpause;
13633906d42dSAlexander Motin 	}
136479f39c6aSMarius Strobl 	if (use_trim == false) {
136579f39c6aSMarius Strobl 		/* Store one of the remaining parts for the next call. */
136672dec079SMarius Strobl 		if (bp->bio_pblkno >= part->eblock || block == start) {
136772dec079SMarius Strobl 			part->eblock = stop;	/* Predict next forward. */
136872dec079SMarius Strobl 			part->eend = end;
1369a7cf6274SAlexander Motin 		} else {
137072dec079SMarius Strobl 			part->eblock = block;	/* Predict next backward. */
137172dec079SMarius Strobl 			part->eend = start;
1372a7cf6274SAlexander Motin 		}
137379f39c6aSMarius Strobl 	}
1374aca38eabSMarius Strobl 	block = end;
1375aca38eabSMarius Strobl unpause:
1376cd85acbaSMarius Strobl 	MMCBUS_RETUNE_UNPAUSE(mmcbus, dev);
1377aca38eabSMarius Strobl 	return (block);
13783906d42dSAlexander Motin }
13793906d42dSAlexander Motin 
1380a1fda318SAlexander Motin static int
13811bacf3beSMarius Strobl mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
13821bacf3beSMarius Strobl     size_t length)
1383a1fda318SAlexander Motin {
1384a1fda318SAlexander Motin 	struct bio bp;
1385a1fda318SAlexander Motin 	daddr_t block, end;
138672dec079SMarius Strobl 	struct disk *disk;
138772dec079SMarius Strobl 	struct mmcsd_softc *sc;
138872dec079SMarius Strobl 	struct mmcsd_part *part;
1389cd85acbaSMarius Strobl 	device_t dev, mmcbus;
139072dec079SMarius Strobl 	int err;
1391a1fda318SAlexander Motin 
139272dec079SMarius Strobl 	disk = arg;
139372dec079SMarius Strobl 	part = disk->d_drv1;
139472dec079SMarius Strobl 	sc = part->sc;
1395646fd30cSMarius Strobl 
1396646fd30cSMarius Strobl 	/* length zero is special and really means flush buffers to media */
1397646fd30cSMarius Strobl 	if (length == 0) {
1398646fd30cSMarius Strobl 		err = mmcsd_flush_cache(sc);
1399646fd30cSMarius Strobl 		if (err != MMC_ERR_NONE)
1400646fd30cSMarius Strobl 			return (EIO);
1401646fd30cSMarius Strobl 		return (0);
1402646fd30cSMarius Strobl 	}
1403646fd30cSMarius Strobl 
140472dec079SMarius Strobl 	dev = sc->dev;
1405cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
140672dec079SMarius Strobl 
1407c55f5707SWarner Losh 	g_reset_bio(&bp);
1408a1fda318SAlexander Motin 	bp.bio_disk = disk;
1409a1fda318SAlexander Motin 	bp.bio_pblkno = offset / disk->d_sectorsize;
1410a1fda318SAlexander Motin 	bp.bio_bcount = length;
1411a1fda318SAlexander Motin 	bp.bio_data = virtual;
1412a1fda318SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
141372dec079SMarius Strobl 	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
1414cd85acbaSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1415cd85acbaSMarius Strobl 	err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
141672dec079SMarius Strobl 	if (err != MMC_ERR_NONE) {
141772dec079SMarius Strobl 		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
141872dec079SMarius Strobl 			device_printf(dev, "Partition switch error\n");
1419cd85acbaSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
142072dec079SMarius Strobl 		return (EIO);
142172dec079SMarius Strobl 	}
142272dec079SMarius Strobl 	block = mmcsd_rw(part, &bp);
1423cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
1424a1fda318SAlexander Motin 	return ((end < block) ? EIO : 0);
1425a1fda318SAlexander Motin }
1426a1fda318SAlexander Motin 
14273906d42dSAlexander Motin static void
14283906d42dSAlexander Motin mmcsd_task(void *arg)
14293906d42dSAlexander Motin {
14303906d42dSAlexander Motin 	daddr_t block, end;
143172dec079SMarius Strobl 	struct mmcsd_part *part;
143272dec079SMarius Strobl 	struct mmcsd_softc *sc;
143372dec079SMarius Strobl 	struct bio *bp;
1434cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1435d176b803SScott Long 	int bio_error, err, sz;
143672dec079SMarius Strobl 
143772dec079SMarius Strobl 	part = arg;
143872dec079SMarius Strobl 	sc = part->sc;
143972dec079SMarius Strobl 	dev = sc->dev;
1440cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
14413906d42dSAlexander Motin 
1442249b0e85SAlexander Motin 	while (1) {
1443*3b433ed7SMarius Strobl 		bio_error = 0;
1444aca38eabSMarius Strobl 		MMCSD_DISK_LOCK(part);
14453906d42dSAlexander Motin 		do {
144672dec079SMarius Strobl 			if (part->running == 0)
1447249b0e85SAlexander Motin 				goto out;
144872dec079SMarius Strobl 			bp = bioq_takefirst(&part->bio_queue);
14493906d42dSAlexander Motin 			if (bp == NULL)
1450aca38eabSMarius Strobl 				msleep(part, &part->disk_mtx, PRIBIO,
1451aca38eabSMarius Strobl 				    "mmcsd disk jobqueue", 0);
1452249b0e85SAlexander Motin 		} while (bp == NULL);
1453aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
1454646fd30cSMarius Strobl 		if (__predict_false(bp->bio_cmd == BIO_FLUSH)) {
1455646fd30cSMarius Strobl 			if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) {
1456646fd30cSMarius Strobl 				bp->bio_error = EIO;
1457646fd30cSMarius Strobl 				bp->bio_flags |= BIO_ERROR;
1458646fd30cSMarius Strobl 			}
1459646fd30cSMarius Strobl 			biodone(bp);
1460646fd30cSMarius Strobl 			continue;
1461646fd30cSMarius Strobl 		}
146272dec079SMarius Strobl 		if (bp->bio_cmd != BIO_READ && part->ro) {
14633906d42dSAlexander Motin 			bp->bio_error = EROFS;
14643906d42dSAlexander Motin 			bp->bio_resid = bp->bio_bcount;
14653906d42dSAlexander Motin 			bp->bio_flags |= BIO_ERROR;
14663906d42dSAlexander Motin 			biodone(bp);
14673906d42dSAlexander Motin 			continue;
14683906d42dSAlexander Motin 		}
1469cd85acbaSMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
147072dec079SMarius Strobl 		sz = part->disk->d_sectorsize;
14713906d42dSAlexander Motin 		block = bp->bio_pblkno;
14723906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
1473cd85acbaSMarius Strobl 		err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
147472dec079SMarius Strobl 		if (err != MMC_ERR_NONE) {
147572dec079SMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
147672dec079SMarius Strobl 			    LOG_PPS))
147772dec079SMarius Strobl 				device_printf(dev, "Partition switch error\n");
147872dec079SMarius Strobl 			goto release;
147972dec079SMarius Strobl 		}
14803906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1481a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
148272dec079SMarius Strobl 			if (block < part->eend && end > part->eblock)
148372dec079SMarius Strobl 				part->eblock = part->eend = 0;
148472dec079SMarius Strobl 			block = mmcsd_rw(part, bp);
1485*3b433ed7SMarius Strobl 		} else if (bp->bio_cmd == BIO_DELETE)
148672dec079SMarius Strobl 			block = mmcsd_delete(part, bp);
1487*3b433ed7SMarius Strobl 		else
1488d176b803SScott Long 			bio_error = EOPNOTSUPP;
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;
1495*3b433ed7SMarius Strobl 		} else
1496d119f637SMarius Strobl 			bp->bio_resid = 0;
1497114b4164SWarner Losh 		biodone(bp);
1498114b4164SWarner Losh 	}
1499249b0e85SAlexander Motin out:
15000e1466acSWarner Losh 	/* tell parent we're done */
150172dec079SMarius Strobl 	part->running = -1;
1502aca38eabSMarius Strobl 	MMCSD_DISK_UNLOCK(part);
150372dec079SMarius Strobl 	wakeup(part);
15040e1466acSWarner Losh 
15053745c395SJulian Elischer 	kproc_exit(0);
1506114b4164SWarner Losh }
1507114b4164SWarner Losh 
1508a0075e58SWarner Losh static int
1509a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev)
1510a0075e58SWarner Losh {
1511ae5d8757SMarius Strobl 
1512a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_1)
1513a0075e58SWarner Losh 		return (1);
1514a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_4)
1515a0075e58SWarner Losh 		return (4);
1516a0075e58SWarner Losh 	return (8);
1517a0075e58SWarner Losh }
1518a0075e58SWarner Losh 
1519646fd30cSMarius Strobl static int
1520646fd30cSMarius Strobl mmcsd_flush_cache(struct mmcsd_softc *sc)
1521646fd30cSMarius Strobl {
1522646fd30cSMarius Strobl 	device_t dev, mmcbus;
1523646fd30cSMarius Strobl 	int err;
1524646fd30cSMarius Strobl 
1525646fd30cSMarius Strobl 	if ((sc->flags & MMCSD_FLUSH_CACHE) == 0)
1526646fd30cSMarius Strobl 		return (MMC_ERR_NONE);
1527646fd30cSMarius Strobl 
1528646fd30cSMarius Strobl 	dev = sc->dev;
1529646fd30cSMarius Strobl 	mmcbus = sc->mmcbus;
1530646fd30cSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1531646fd30cSMarius Strobl 	if ((sc->flags & MMCSD_DIRTY) == 0) {
1532646fd30cSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
1533646fd30cSMarius Strobl 		return (MMC_ERR_NONE);
1534646fd30cSMarius Strobl 	}
1535646fd30cSMarius Strobl 	err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL,
1536646fd30cSMarius Strobl 	    EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE_FLUSH, 60 * 1000, true);
1537646fd30cSMarius Strobl 	if (err == MMC_ERR_NONE)
1538646fd30cSMarius Strobl 		sc->flags &= ~MMCSD_DIRTY;
1539646fd30cSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
1540646fd30cSMarius Strobl 	return (err);
1541646fd30cSMarius Strobl }
1542646fd30cSMarius Strobl 
1543114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
1544114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
1545114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
1546114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
1547646fd30cSMarius Strobl 	DEVMETHOD(device_shutdown, mmcsd_shutdown),
1548eb67f31aSAlexander Motin 	DEVMETHOD(device_suspend, mmcsd_suspend),
1549eb67f31aSAlexander Motin 	DEVMETHOD(device_resume, mmcsd_resume),
15507aa65846SMarius Strobl 	DEVMETHOD_END
1551114b4164SWarner Losh };
1552114b4164SWarner Losh 
1553114b4164SWarner Losh static driver_t mmcsd_driver = {
1554114b4164SWarner Losh 	"mmcsd",
1555114b4164SWarner Losh 	mmcsd_methods,
1556114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
1557114b4164SWarner Losh };
1558114b4164SWarner Losh static devclass_t mmcsd_devclass;
1559114b4164SWarner Losh 
156072dec079SMarius Strobl static int
156172dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
156272dec079SMarius Strobl {
156372dec079SMarius Strobl 
156472dec079SMarius Strobl 	switch (what) {
156572dec079SMarius Strobl 	case MOD_LOAD:
156672dec079SMarius Strobl 		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
156772dec079SMarius Strobl 		    TRUE);
156872dec079SMarius Strobl 		return (0);
156972dec079SMarius Strobl 	case MOD_UNLOAD:
157072dec079SMarius Strobl 		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
157172dec079SMarius Strobl 		return (0);
157272dec079SMarius Strobl 	}
157372dec079SMarius Strobl 	return (0);
157472dec079SMarius Strobl }
157572dec079SMarius Strobl 
157672dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
157772dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
157872dec079SMarius Strobl MMC_DEPEND(mmcsd);
1579