xref: /freebsd/sys/dev/mmc/mmcsd.c (revision fdafd315)
1114b4164SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
57114b4164SWarner Losh #include <sys/systm.h>
58114b4164SWarner Losh #include <sys/bio.h>
59114b4164SWarner Losh #include <sys/bus.h>
60114b4164SWarner Losh #include <sys/conf.h>
61602a05b0SMarius Strobl #include <sys/endian.h>
6272dec079SMarius Strobl #include <sys/fcntl.h>
6372dec079SMarius Strobl #include <sys/ioccom.h>
64114b4164SWarner Losh #include <sys/kernel.h>
65114b4164SWarner Losh #include <sys/kthread.h>
66114b4164SWarner Losh #include <sys/lock.h>
67114b4164SWarner Losh #include <sys/malloc.h>
68114b4164SWarner Losh #include <sys/module.h>
69114b4164SWarner Losh #include <sys/mutex.h>
707217ea7cSMarius Strobl #include <sys/priv.h>
7172dec079SMarius Strobl #include <sys/slicer.h>
72646fd30cSMarius Strobl #include <sys/sysctl.h>
735e3dbf8bSIan Lepore #include <sys/time.h>
7472dec079SMarius Strobl 
75c55f5707SWarner Losh #include <geom/geom.h>
76114b4164SWarner Losh #include <geom/geom_disk.h>
77114b4164SWarner Losh 
7872dec079SMarius Strobl #include <dev/mmc/bridge.h>
7972dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h>
8072dec079SMarius Strobl #include <dev/mmc/mmc_subr.h>
817aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h>
82114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
837aa65846SMarius Strobl #include <dev/mmc/mmcvar.h>
84114b4164SWarner Losh 
85114b4164SWarner Losh #include "mmcbus_if.h"
86114b4164SWarner Losh 
8772dec079SMarius Strobl #define	MMCSD_CMD_RETRIES	5
8872dec079SMarius Strobl 
8972dec079SMarius Strobl #define	MMCSD_FMT_BOOT		"mmcsd%dboot"
9072dec079SMarius Strobl #define	MMCSD_FMT_GP		"mmcsd%dgp"
9172dec079SMarius Strobl #define	MMCSD_FMT_RPMB		"mmcsd%drpmb"
9272dec079SMarius Strobl #define	MMCSD_LABEL_ENH		"enh"
9372dec079SMarius Strobl 
9472dec079SMarius Strobl #define	MMCSD_PART_NAMELEN	(16 + 1)
9572dec079SMarius Strobl 
9672dec079SMarius Strobl struct mmcsd_softc;
9772dec079SMarius Strobl 
9872dec079SMarius Strobl struct mmcsd_part {
99aca38eabSMarius Strobl 	struct mtx disk_mtx;
100aca38eabSMarius Strobl 	struct mtx ioctl_mtx;
10172dec079SMarius Strobl 	struct mmcsd_softc *sc;
102114b4164SWarner Losh 	struct disk *disk;
103114b4164SWarner Losh 	struct proc *p;
104114b4164SWarner Losh 	struct bio_queue_head bio_queue;
105a7cf6274SAlexander Motin 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
10672dec079SMarius Strobl 	u_int cnt;
10772dec079SMarius Strobl 	u_int type;
1080e1466acSWarner Losh 	int running;
109eb67f31aSAlexander Motin 	int suspend;
110aca38eabSMarius Strobl 	int ioctl;
11172dec079SMarius Strobl 	bool ro;
11272dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
11372dec079SMarius Strobl };
11472dec079SMarius Strobl 
11572dec079SMarius Strobl struct mmcsd_softc {
11672dec079SMarius Strobl 	device_t dev;
117cd85acbaSMarius Strobl 	device_t mmcbus;
11872dec079SMarius Strobl 	struct mmcsd_part *part[MMC_PART_MAX];
11972dec079SMarius Strobl 	enum mmc_card_mode mode;
120aca38eabSMarius Strobl 	u_int max_data;		/* Maximum data size [blocks] */
121aca38eabSMarius Strobl 	u_int erase_sector;	/* Device native erase sector size [blocks] */
122aca38eabSMarius Strobl 	uint8_t	high_cap;	/* High Capacity device (block addressed) */
12372dec079SMarius Strobl 	uint8_t part_curr;	/* Partition currently switched to */
12472dec079SMarius Strobl 	uint8_t ext_csd[MMC_EXTCSD_SIZE];
12572dec079SMarius Strobl 	uint16_t rca;
12679f39c6aSMarius Strobl 	uint32_t flags;
12779f39c6aSMarius Strobl #define	MMCSD_INAND_CMD38	0x0001
12879f39c6aSMarius Strobl #define	MMCSD_USE_TRIM		0x0002
129646fd30cSMarius Strobl #define	MMCSD_FLUSH_CACHE	0x0004
130646fd30cSMarius Strobl #define	MMCSD_DIRTY		0x0008
13179f39c6aSMarius Strobl 	uint32_t cmd6_time;	/* Generic switch timeout [us] */
13272dec079SMarius Strobl 	uint32_t part_time;	/* Partition switch timeout [us] */
13372dec079SMarius Strobl 	off_t enh_base;		/* Enhanced user data area slice base ... */
13472dec079SMarius Strobl 	off_t enh_size;		/* ... and size [bytes] */
1355e3dbf8bSIan Lepore 	int log_count;
1365e3dbf8bSIan Lepore 	struct timeval log_time;
13772dec079SMarius Strobl 	struct cdev *rpmb_dev;
138114b4164SWarner Losh };
139114b4164SWarner Losh 
140bb1ef63fSWarner Losh static const char *errmsg[] =
141bb1ef63fSWarner Losh {
142bb1ef63fSWarner Losh 	"None",
143bb1ef63fSWarner Losh 	"Timeout",
144bb1ef63fSWarner Losh 	"Bad CRC",
145bb1ef63fSWarner Losh 	"Fifo",
146bb1ef63fSWarner Losh 	"Failed",
147bb1ef63fSWarner Losh 	"Invalid",
148bb1ef63fSWarner Losh 	"NO MEMORY"
149bb1ef63fSWarner Losh };
150bb1ef63fSWarner Losh 
1517029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, mmcsd, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1527029da5cSPawel Biernacki     "mmcsd driver");
153646fd30cSMarius Strobl 
154646fd30cSMarius Strobl static int mmcsd_cache = 1;
155646fd30cSMarius Strobl SYSCTL_INT(_hw_mmcsd, OID_AUTO, cache, CTLFLAG_RDTUN, &mmcsd_cache, 0,
156646fd30cSMarius Strobl     "Device R/W cache enabled if present");
157646fd30cSMarius Strobl 
1585e3dbf8bSIan Lepore #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
1595e3dbf8bSIan Lepore 
160114b4164SWarner Losh /* bus entry points */
161114b4164SWarner Losh static int mmcsd_attach(device_t dev);
162114b4164SWarner Losh static int mmcsd_detach(device_t dev);
163ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev);
164646fd30cSMarius Strobl static int mmcsd_shutdown(device_t dev);
165114b4164SWarner Losh 
166114b4164SWarner Losh /* disk routines */
167114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
168489ba222SMitchell Horne static int mmcsd_dump(void *arg, void *virtual, off_t offset, size_t length);
16972dec079SMarius Strobl static int mmcsd_getattr(struct bio *);
17072dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
17172dec079SMarius Strobl     int fflag, struct thread *td);
172ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp);
173114b4164SWarner Losh static void mmcsd_task(void *arg);
174114b4164SWarner Losh 
17572dec079SMarius Strobl /* RMPB cdev interface */
17672dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
17772dec079SMarius Strobl     int fflag, struct thread *td);
178a0075e58SWarner Losh 
17972dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
18079f39c6aSMarius Strobl     const char *name, u_int cnt, off_t media_size, bool ro);
18172dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev);
18272dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
18379f39c6aSMarius Strobl static const char *mmcsd_errmsg(int e);
184646fd30cSMarius Strobl static int mmcsd_flush_cache(struct mmcsd_softc *sc);
18572dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
1867217ea7cSMarius Strobl     int fflag, struct thread *td);
18772dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
18872dec079SMarius Strobl     int fflag);
18972dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
19072dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
19172dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
19272dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider,
19372dec079SMarius Strobl     struct flash_slice *slices, int *nslices);
19472dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
19572dec079SMarius Strobl     u_int part);
19672dec079SMarius Strobl 
197aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK(_part)		mtx_lock(&(_part)->disk_mtx)
198aca38eabSMarius Strobl #define	MMCSD_DISK_UNLOCK(_part)	mtx_unlock(&(_part)->disk_mtx)
199aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK_INIT(_part)					\
200aca38eabSMarius Strobl 	mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF)
201aca38eabSMarius Strobl #define	MMCSD_DISK_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->disk_mtx);
202aca38eabSMarius Strobl #define	MMCSD_DISK_ASSERT_LOCKED(_part)					\
203aca38eabSMarius Strobl 	mtx_assert(&(_part)->disk_mtx, MA_OWNED);
204aca38eabSMarius Strobl #define	MMCSD_DISK_ASSERT_UNLOCKED(_part)				\
205aca38eabSMarius Strobl 	mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED);
206aca38eabSMarius Strobl 
207aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK(_part)		mtx_lock(&(_part)->ioctl_mtx)
208aca38eabSMarius Strobl #define	MMCSD_IOCTL_UNLOCK(_part)	mtx_unlock(&(_part)->ioctl_mtx)
209aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK_INIT(_part)					\
210aca38eabSMarius Strobl 	mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF)
211aca38eabSMarius Strobl #define	MMCSD_IOCTL_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->ioctl_mtx);
212aca38eabSMarius Strobl #define	MMCSD_IOCTL_ASSERT_LOCKED(_part)				\
213aca38eabSMarius Strobl 	mtx_assert(&(_part)->ioctl_mtx, MA_OWNED);
214aca38eabSMarius Strobl #define	MMCSD_IOCLT_ASSERT_UNLOCKED(_part)				\
215aca38eabSMarius Strobl 	mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED);
216114b4164SWarner Losh 
217114b4164SWarner Losh static int
mmcsd_probe(device_t dev)218114b4164SWarner Losh mmcsd_probe(device_t dev)
219114b4164SWarner Losh {
220114b4164SWarner Losh 
221c18f1e26SAlexander Motin 	device_quiet(dev);
2220c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
223114b4164SWarner Losh 	return (0);
224114b4164SWarner Losh }
225114b4164SWarner Losh 
226114b4164SWarner Losh static int
mmcsd_attach(device_t dev)227114b4164SWarner Losh mmcsd_attach(device_t dev)
228114b4164SWarner Losh {
229cd85acbaSMarius Strobl 	device_t mmcbus;
230114b4164SWarner Losh 	struct mmcsd_softc *sc;
23172dec079SMarius Strobl 	const uint8_t *ext_csd;
23272dec079SMarius Strobl 	off_t erase_size, sector_size, size, wp_size;
23372dec079SMarius Strobl 	uintmax_t bytes;
23472dec079SMarius Strobl 	int err, i;
23579f39c6aSMarius Strobl 	uint32_t quirks;
23672dec079SMarius Strobl 	uint8_t rev;
23772dec079SMarius Strobl 	bool comp, ro;
23872dec079SMarius Strobl 	char unit[2];
239114b4164SWarner Losh 
240114b4164SWarner Losh 	sc = device_get_softc(dev);
241114b4164SWarner Losh 	sc->dev = dev;
242cd85acbaSMarius Strobl 	sc->mmcbus = mmcbus = device_get_parent(dev);
243608226d5SMarius Strobl 	sc->mode = mmc_get_card_type(dev);
244aca38eabSMarius Strobl 	/*
245aca38eabSMarius Strobl 	 * Note that in principle with an SDHCI-like re-tuning implementation,
246aca38eabSMarius Strobl 	 * the maximum data size can change at runtime due to a device removal/
247aca38eabSMarius Strobl 	 * insertion that results in switches to/from a transfer mode involving
248aca38eabSMarius Strobl 	 * re-tuning, iff there are multiple devices on a given bus.  Until now
249aca38eabSMarius Strobl 	 * mmc(4) lacks support for rescanning already attached buses, however,
25021794534SMarius Strobl 	 * and sdhci(4) to date has no support for shared buses in the first
251aca38eabSMarius Strobl 	 * place either.
252aca38eabSMarius Strobl 	 */
253aca38eabSMarius Strobl 	sc->max_data = mmc_get_max_data(dev);
254aca38eabSMarius Strobl 	sc->high_cap = mmc_get_high_cap(dev);
25572dec079SMarius Strobl 	sc->rca = mmc_get_rca(dev);
25679f39c6aSMarius Strobl 	sc->cmd6_time = mmc_get_cmd6_timeout(dev);
25779f39c6aSMarius Strobl 	quirks = mmc_get_quirks(dev);
258114b4164SWarner Losh 
25972dec079SMarius Strobl 	/* Only MMC >= 4.x devices support EXT_CSD. */
26072dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) >= 4) {
261cd85acbaSMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
262cd85acbaSMarius Strobl 		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
263cd85acbaSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
26479f39c6aSMarius Strobl 		if (err != MMC_ERR_NONE) {
26579f39c6aSMarius Strobl 			device_printf(dev, "Error reading EXT_CSD %s\n",
26679f39c6aSMarius Strobl 			    mmcsd_errmsg(err));
26779f39c6aSMarius Strobl 			return (ENXIO);
26879f39c6aSMarius Strobl 		}
26972dec079SMarius Strobl 	}
27072dec079SMarius Strobl 	ext_csd = sc->ext_csd;
27172dec079SMarius Strobl 
27279f39c6aSMarius Strobl 	if ((quirks & MMC_QUIRK_INAND_CMD38) != 0) {
27379f39c6aSMarius Strobl 		if (mmc_get_spec_vers(dev) < 4) {
27479f39c6aSMarius Strobl 			device_printf(dev,
27579f39c6aSMarius Strobl 			    "MMC_QUIRK_INAND_CMD38 set but no EXT_CSD\n");
27679f39c6aSMarius Strobl 			return (EINVAL);
27779f39c6aSMarius Strobl 		}
27879f39c6aSMarius Strobl 		sc->flags |= MMCSD_INAND_CMD38;
27979f39c6aSMarius Strobl 	}
28079f39c6aSMarius Strobl 
28179f39c6aSMarius Strobl 	/*
28279f39c6aSMarius Strobl 	 * EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN denotes support for both
28379f39c6aSMarius Strobl 	 * insecure and secure TRIM.
28479f39c6aSMarius Strobl 	 */
28579f39c6aSMarius Strobl 	if ((ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] &
28679f39c6aSMarius Strobl 	    EXT_CSD_SEC_FEATURE_SUPPORT_GB_CL_EN) != 0 &&
28779f39c6aSMarius Strobl 	    (quirks & MMC_QUIRK_BROKEN_TRIM) == 0) {
28879f39c6aSMarius Strobl 		if (bootverbose)
28979f39c6aSMarius Strobl 			device_printf(dev, "taking advantage of TRIM\n");
29079f39c6aSMarius Strobl 		sc->flags |= MMCSD_USE_TRIM;
29179f39c6aSMarius Strobl 		sc->erase_sector = 1;
29279f39c6aSMarius Strobl 	} else
29379f39c6aSMarius Strobl 		sc->erase_sector = mmc_get_erase_sector(dev);
29479f39c6aSMarius Strobl 
29572dec079SMarius Strobl 	/*
29672dec079SMarius Strobl 	 * Enhanced user data area and general purpose partitions are only
29772dec079SMarius Strobl 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
29872dec079SMarius Strobl 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
29972dec079SMarius Strobl 	 */
30072dec079SMarius Strobl 	rev = ext_csd[EXT_CSD_REV];
30172dec079SMarius Strobl 
30272dec079SMarius Strobl 	/*
303646fd30cSMarius Strobl 	 * With revision 1.5 (MMC v4.5, EXT_CSD_REV == 6) and later, take
304646fd30cSMarius Strobl 	 * advantage of the device R/W cache if present and useage is not
305646fd30cSMarius Strobl 	 * disabled.
306646fd30cSMarius Strobl 	 */
307646fd30cSMarius Strobl 	if (rev >= 6 && mmcsd_cache != 0) {
308602a05b0SMarius Strobl 		size = le32dec(&ext_csd[EXT_CSD_CACHE_SIZE]);
309646fd30cSMarius Strobl 		if (bootverbose)
310646fd30cSMarius Strobl 			device_printf(dev, "cache size %juKB\n", size);
311646fd30cSMarius Strobl 		if (size > 0) {
312646fd30cSMarius Strobl 			MMCBUS_ACQUIRE_BUS(mmcbus, dev);
313646fd30cSMarius Strobl 			err = mmc_switch(mmcbus, dev, sc->rca,
314646fd30cSMarius Strobl 			    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CACHE_CTRL,
315646fd30cSMarius Strobl 			    EXT_CSD_CACHE_CTRL_CACHE_EN, sc->cmd6_time, true);
316646fd30cSMarius Strobl 			MMCBUS_RELEASE_BUS(mmcbus, dev);
317646fd30cSMarius Strobl 			if (err != MMC_ERR_NONE)
318646fd30cSMarius Strobl 				device_printf(dev, "failed to enable cache\n");
319646fd30cSMarius Strobl 			else
320646fd30cSMarius Strobl 				sc->flags |= MMCSD_FLUSH_CACHE;
321646fd30cSMarius Strobl 		}
322646fd30cSMarius Strobl 	}
323646fd30cSMarius Strobl 
324646fd30cSMarius Strobl 	/*
32572dec079SMarius Strobl 	 * Ignore user-creatable enhanced user data area and general purpose
32672dec079SMarius Strobl 	 * partitions partitions as long as partitioning hasn't been finished.
32772dec079SMarius Strobl 	 */
32872dec079SMarius Strobl 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
32972dec079SMarius Strobl 
33072dec079SMarius Strobl 	/*
33172dec079SMarius Strobl 	 * Add enhanced user data area slice, unless it spans the entirety of
33272dec079SMarius Strobl 	 * the user data area.  The enhanced area is of a multiple of high
33372dec079SMarius Strobl 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
33472dec079SMarius Strobl 	 * 512 KB) and its offset given in either sectors or bytes, depending
33572dec079SMarius Strobl 	 * on whether it's a high capacity device or not.
33672dec079SMarius Strobl 	 * NB: The slicer and its slices need to be registered before adding
33772dec079SMarius Strobl 	 *     the disk for the corresponding user data area as re-tasting is
33872dec079SMarius Strobl 	 *     racy.
33972dec079SMarius Strobl 	 */
34072dec079SMarius Strobl 	sector_size = mmc_get_sector_size(dev);
34172dec079SMarius Strobl 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
34272dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
34372dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
34472dec079SMarius Strobl 	if (rev >= 4 && comp == TRUE && size > 0 &&
34572dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
34672dec079SMarius Strobl 	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
34772dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
34872dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
34972dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
35072dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
35172dec079SMarius Strobl 		size *= erase_size * wp_size;
35272dec079SMarius Strobl 		if (size != mmc_get_media_size(dev) * sector_size) {
35372dec079SMarius Strobl 			sc->enh_size = size;
354602a05b0SMarius Strobl 			sc->enh_base =
355602a05b0SMarius Strobl 			    le32dec(&ext_csd[EXT_CSD_ENH_START_ADDR]) *
356da979d44SMarius Strobl 			    (sc->high_cap == 0 ? MMC_SECTOR_SIZE : 1);
35772dec079SMarius Strobl 		} else if (bootverbose)
35872dec079SMarius Strobl 			device_printf(dev,
35972dec079SMarius Strobl 			    "enhanced user data area spans entire device\n");
36072dec079SMarius Strobl 	}
36172dec079SMarius Strobl 
36272dec079SMarius Strobl 	/*
36372dec079SMarius Strobl 	 * Add default partition.  This may be the only one or the user
36472dec079SMarius Strobl 	 * data area in case partitions are supported.
36572dec079SMarius Strobl 	 */
36672dec079SMarius Strobl 	ro = mmc_get_read_only(dev);
36772dec079SMarius Strobl 	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
36879f39c6aSMarius Strobl 	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size, ro);
36972dec079SMarius Strobl 
37072dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) < 3)
37172dec079SMarius Strobl 		return (0);
37272dec079SMarius Strobl 
37372dec079SMarius Strobl 	/* Belatedly announce enhanced user data slice. */
37472dec079SMarius Strobl 	if (sc->enh_size != 0) {
37572dec079SMarius Strobl 		bytes = mmcsd_pretty_size(size, unit);
37672dec079SMarius Strobl 		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
37772dec079SMarius Strobl 		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
37872dec079SMarius Strobl 		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
37972dec079SMarius Strobl 		    device_get_nameunit(dev));
38072dec079SMarius Strobl 	}
38172dec079SMarius Strobl 
38272dec079SMarius Strobl 	/*
38372dec079SMarius Strobl 	 * Determine partition switch timeout (provided in units of 10 ms)
38472dec079SMarius Strobl 	 * and ensure it's at least 300 ms as some eMMC chips lie.
38572dec079SMarius Strobl 	 */
38672dec079SMarius Strobl 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
38772dec079SMarius Strobl 	    300 * 1000);
38872dec079SMarius Strobl 
38972dec079SMarius Strobl 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
39072dec079SMarius Strobl 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
391cd85acbaSMarius Strobl 	if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) {
39272dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
39379f39c6aSMarius Strobl 		    MMCSD_FMT_BOOT, 0, size,
39472dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
39572dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
39672dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
39779f39c6aSMarius Strobl 		    MMCSD_FMT_BOOT, 1, size,
39872dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
39972dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
40072dec079SMarius Strobl 	}
40172dec079SMarius Strobl 
40272dec079SMarius Strobl 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
40372dec079SMarius Strobl 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
40472dec079SMarius Strobl 	if (rev >= 5 && size > 0)
40572dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
40679f39c6aSMarius Strobl 		    MMCSD_FMT_RPMB, 0, size, ro);
40772dec079SMarius Strobl 
40872dec079SMarius Strobl 	if (rev <= 3 || comp == FALSE)
40972dec079SMarius Strobl 		return (0);
41072dec079SMarius Strobl 
41172dec079SMarius Strobl 	/*
41272dec079SMarius Strobl 	 * Add general purpose partitions, which are of a multiple of high
41372dec079SMarius Strobl 	 * capacity write protect groups, too.
41472dec079SMarius Strobl 	 */
41572dec079SMarius Strobl 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
41672dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
41772dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
41872dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
41972dec079SMarius Strobl 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
42072dec079SMarius Strobl 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
42172dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
42272dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
42372dec079SMarius Strobl 			if (size == 0)
42472dec079SMarius Strobl 				continue;
42572dec079SMarius Strobl 			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
42679f39c6aSMarius Strobl 			    MMCSD_FMT_GP, i, size * erase_size * wp_size, ro);
42772dec079SMarius Strobl 		}
42872dec079SMarius Strobl 	}
42972dec079SMarius Strobl 	return (0);
43072dec079SMarius Strobl }
43172dec079SMarius Strobl 
43272dec079SMarius Strobl static uintmax_t
mmcsd_pretty_size(off_t size,char * unit)43372dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit)
43472dec079SMarius Strobl {
43572dec079SMarius Strobl 	uintmax_t bytes;
43672dec079SMarius Strobl 	int i;
43772dec079SMarius Strobl 
43872dec079SMarius Strobl 	/*
43972dec079SMarius Strobl 	 * Display in most natural units.  There's no card < 1MB.  However,
44072dec079SMarius Strobl 	 * RPMB partitions occasionally are smaller than that, though.  The
44172dec079SMarius Strobl 	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
44272dec079SMarius Strobl 	 * format supports up to 4 GiB and some card makers push it up to this
44372dec079SMarius Strobl 	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
44472dec079SMarius Strobl 	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
44572dec079SMarius Strobl 	 * so we note it in passing here and don't add the code to print TB).
44672dec079SMarius Strobl 	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
44772dec079SMarius Strobl 	 * report them like that.  We also round to the nearest unit, since
44872dec079SMarius Strobl 	 * many cards are a few percent short, even of the power of 10 size.
44972dec079SMarius Strobl 	 */
45072dec079SMarius Strobl 	bytes = size;
45172dec079SMarius Strobl 	unit[0] = unit[1] = '\0';
45272dec079SMarius Strobl 	for (i = 0; i <= 2 && bytes >= 1000; i++) {
45372dec079SMarius Strobl 		bytes = (bytes + 1000 / 2 - 1) / 1000;
45472dec079SMarius Strobl 		switch (i) {
45572dec079SMarius Strobl 		case 0:
45672dec079SMarius Strobl 			unit[0] = 'k';
45772dec079SMarius Strobl 			break;
45872dec079SMarius Strobl 		case 1:
45972dec079SMarius Strobl 			unit[0] = 'M';
46072dec079SMarius Strobl 			break;
46172dec079SMarius Strobl 		case 2:
46272dec079SMarius Strobl 			unit[0] = 'G';
46372dec079SMarius Strobl 			break;
46472dec079SMarius Strobl 		default:
46572dec079SMarius Strobl 			break;
46672dec079SMarius Strobl 		}
46772dec079SMarius Strobl 	}
46872dec079SMarius Strobl 	return (bytes);
46972dec079SMarius Strobl }
47072dec079SMarius Strobl 
47172dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = {
47272dec079SMarius Strobl 	.d_version	= D_VERSION,
47372dec079SMarius Strobl 	.d_name		= "mmcsdrpmb",
47472dec079SMarius Strobl 	.d_ioctl	= mmcsd_ioctl_rpmb
47572dec079SMarius Strobl };
47672dec079SMarius Strobl 
47772dec079SMarius Strobl static void
mmcsd_add_part(struct mmcsd_softc * sc,u_int type,const char * name,u_int cnt,off_t media_size,bool ro)47872dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
47979f39c6aSMarius Strobl     off_t media_size, bool ro)
48072dec079SMarius Strobl {
48172dec079SMarius Strobl 	struct make_dev_args args;
482cd85acbaSMarius Strobl 	device_t dev, mmcbus;
48372dec079SMarius Strobl 	const char *ext;
48472dec079SMarius Strobl 	const uint8_t *ext_csd;
48572dec079SMarius Strobl 	struct mmcsd_part *part;
48672dec079SMarius Strobl 	struct disk *d;
48772dec079SMarius Strobl 	uintmax_t bytes;
48872dec079SMarius Strobl 	u_int gp;
48972dec079SMarius Strobl 	uint32_t speed;
49072dec079SMarius Strobl 	uint8_t extattr;
49172dec079SMarius Strobl 	bool enh;
49272dec079SMarius Strobl 	char unit[2];
49372dec079SMarius Strobl 
49472dec079SMarius Strobl 	dev = sc->dev;
495cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
49672dec079SMarius Strobl 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
49772dec079SMarius Strobl 	    M_WAITOK | M_ZERO);
49872dec079SMarius Strobl 	part->sc = sc;
49972dec079SMarius Strobl 	part->cnt = cnt;
50072dec079SMarius Strobl 	part->type = type;
50172dec079SMarius Strobl 	part->ro = ro;
50272dec079SMarius Strobl 	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
50372dec079SMarius Strobl 
504aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK_INIT(part);
505aca38eabSMarius Strobl 
506aca38eabSMarius Strobl 	/*
507aca38eabSMarius Strobl 	 * For the RPMB partition, allow IOCTL access only.
508aca38eabSMarius Strobl 	 * NB: If ever attaching RPMB partitions to disk(9), the re-tuning
509aca38eabSMarius Strobl 	 *     implementation and especially its pausing need to be revisited,
510aca38eabSMarius Strobl 	 *     because then re-tuning requests may be issued by the IOCTL half
511aca38eabSMarius Strobl 	 *     of this driver while re-tuning is already paused by the disk(9)
512aca38eabSMarius Strobl 	 *     one and vice versa.
513aca38eabSMarius Strobl 	 */
51472dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
51572dec079SMarius Strobl 		make_dev_args_init(&args);
51672dec079SMarius Strobl 		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
51772dec079SMarius Strobl 		args.mda_devsw = &mmcsd_rpmb_cdevsw;
51872dec079SMarius Strobl 		args.mda_uid = UID_ROOT;
51972dec079SMarius Strobl 		args.mda_gid = GID_OPERATOR;
52072dec079SMarius Strobl 		args.mda_mode = 0640;
52172dec079SMarius Strobl 		args.mda_si_drv1 = part;
52272dec079SMarius Strobl 		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
52372dec079SMarius Strobl 			device_printf(dev, "Failed to make RPMB device\n");
52472dec079SMarius Strobl 			free(part, M_DEVBUF);
52572dec079SMarius Strobl 			return;
52672dec079SMarius Strobl 		}
52772dec079SMarius Strobl 	} else {
528aca38eabSMarius Strobl 		MMCSD_DISK_LOCK_INIT(part);
52972dec079SMarius Strobl 
53072dec079SMarius Strobl 		d = part->disk = disk_alloc();
531a0075e58SWarner Losh 		d->d_close = mmcsd_close;
532a0075e58SWarner Losh 		d->d_strategy = mmcsd_strategy;
53372dec079SMarius Strobl 		d->d_ioctl = mmcsd_ioctl_disk;
534a1fda318SAlexander Motin 		d->d_dump = mmcsd_dump;
53572dec079SMarius Strobl 		d->d_getattr = mmcsd_getattr;
53672dec079SMarius Strobl 		d->d_name = part->name;
53772dec079SMarius Strobl 		d->d_drv1 = part;
538a0075e58SWarner Losh 		d->d_sectorsize = mmc_get_sector_size(dev);
539aca38eabSMarius Strobl 		d->d_maxsize = sc->max_data * d->d_sectorsize;
54072dec079SMarius Strobl 		d->d_mediasize = media_size;
54179f39c6aSMarius Strobl 		d->d_stripesize = sc->erase_sector * d->d_sectorsize;
54272dec079SMarius Strobl 		d->d_unit = cnt;
5433906d42dSAlexander Motin 		d->d_flags = DISKFLAG_CANDELETE;
544646fd30cSMarius Strobl 		if ((sc->flags & MMCSD_FLUSH_CACHE) != 0)
545646fd30cSMarius Strobl 			d->d_flags |= DISKFLAG_CANFLUSHCACHE;
54679f39c6aSMarius Strobl 		d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize;
54772dec079SMarius Strobl 		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
54872dec079SMarius Strobl 		    sizeof(d->d_ident));
54972dec079SMarius Strobl 		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
55072dec079SMarius Strobl 		    sizeof(d->d_descr));
55117160457SAlexander Motin 		d->d_rotation_rate = DISK_RR_NON_ROTATING;
552f3a4b7f7SIan Lepore 
553a0075e58SWarner Losh 		disk_create(d, DISK_VERSION);
55472dec079SMarius Strobl 		bioq_init(&part->bio_queue);
5550e1466acSWarner Losh 
55672dec079SMarius Strobl 		part->running = 1;
55772dec079SMarius Strobl 		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
55872dec079SMarius Strobl 		    "%s%d: mmc/sd card", part->name, cnt);
55972dec079SMarius Strobl 	}
56072dec079SMarius Strobl 
56172dec079SMarius Strobl 	bytes = mmcsd_pretty_size(media_size, unit);
56272dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
563cd85acbaSMarius Strobl 		speed = mmcbr_get_clock(mmcbus);
56472dec079SMarius Strobl 		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
56572dec079SMarius Strobl 		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
566cd85acbaSMarius Strobl 		    ro ? " (read-only)" : "", device_get_nameunit(mmcbus),
56772dec079SMarius Strobl 		    speed / 1000000, (speed / 100000) % 10,
568aca38eabSMarius Strobl 		    mmcsd_bus_bit_width(dev), sc->max_data);
56972dec079SMarius Strobl 	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
57039a289c3SIan Lepore 		printf("%s: %ju%sB partition %d%s at %s\n", part->name, bytes,
57172dec079SMarius Strobl 		    unit, type, ro ? " (read-only)" : "",
57288e07d92SRui Paulo 		    device_get_nameunit(dev));
57372dec079SMarius Strobl 	} else {
57472dec079SMarius Strobl 		enh = false;
57572dec079SMarius Strobl 		ext = NULL;
57672dec079SMarius Strobl 		extattr = 0;
57772dec079SMarius Strobl 		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
57872dec079SMarius Strobl 		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
57972dec079SMarius Strobl 			ext_csd = sc->ext_csd;
58072dec079SMarius Strobl 			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
58172dec079SMarius Strobl 			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
58272dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
58372dec079SMarius Strobl 			    (ext_csd[EXT_CSD_PART_ATTR] &
58472dec079SMarius Strobl 			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
58572dec079SMarius Strobl 				enh = true;
58672dec079SMarius Strobl 			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
58772dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
58872dec079SMarius Strobl 				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
58972dec079SMarius Strobl 				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
59072dec079SMarius Strobl 				switch (extattr) {
59172dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
59272dec079SMarius Strobl 						break;
59372dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
59472dec079SMarius Strobl 						ext = "system code";
59572dec079SMarius Strobl 						break;
59672dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
59772dec079SMarius Strobl 						ext = "non-persistent";
59872dec079SMarius Strobl 						break;
59972dec079SMarius Strobl 					default:
60072dec079SMarius Strobl 						ext = "reserved";
60172dec079SMarius Strobl 						break;
60272dec079SMarius Strobl 				}
60372dec079SMarius Strobl 			}
60472dec079SMarius Strobl 		}
60572dec079SMarius Strobl 		if (ext == NULL)
60639a289c3SIan Lepore 			printf("%s%d: %ju%sB partition %d%s%s at %s\n",
60772dec079SMarius Strobl 			    part->name, cnt, bytes, unit, type, enh ?
60872dec079SMarius Strobl 			    " enhanced" : "", ro ? " (read-only)" : "",
60972dec079SMarius Strobl 			    device_get_nameunit(dev));
61072dec079SMarius Strobl 		else
61139a289c3SIan Lepore 			printf("%s%d: %ju%sB partition %d extended 0x%x "
61272dec079SMarius Strobl 			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
61372dec079SMarius Strobl 			    type, extattr, ext, ro ? " (read-only)" : "",
61472dec079SMarius Strobl 			    device_get_nameunit(dev));
61572dec079SMarius Strobl 	}
61672dec079SMarius Strobl }
617114b4164SWarner Losh 
61872dec079SMarius Strobl static int
mmcsd_slicer(device_t dev,const char * provider,struct flash_slice * slices,int * nslices)61972dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider,
62072dec079SMarius Strobl     struct flash_slice *slices, int *nslices)
62172dec079SMarius Strobl {
62272dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
62372dec079SMarius Strobl 	struct mmcsd_softc *sc;
62472dec079SMarius Strobl 	struct mmcsd_part *part;
62572dec079SMarius Strobl 
62672dec079SMarius Strobl 	*nslices = 0;
62772dec079SMarius Strobl 	if (slices == NULL)
62872dec079SMarius Strobl 		return (ENOMEM);
62972dec079SMarius Strobl 
63072dec079SMarius Strobl 	sc = device_get_softc(dev);
63172dec079SMarius Strobl 	if (sc->enh_size == 0)
63272dec079SMarius Strobl 		return (ENXIO);
63372dec079SMarius Strobl 
63472dec079SMarius Strobl 	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
63572dec079SMarius Strobl 	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
63672dec079SMarius Strobl 	    part->disk->d_unit);
63772dec079SMarius Strobl 	if (strcmp(name, provider) != 0)
63872dec079SMarius Strobl 		return (ENXIO);
63972dec079SMarius Strobl 
64072dec079SMarius Strobl 	*nslices = 1;
64172dec079SMarius Strobl 	slices[0].base = sc->enh_base;
64272dec079SMarius Strobl 	slices[0].size = sc->enh_size;
64372dec079SMarius Strobl 	slices[0].label = MMCSD_LABEL_ENH;
644114b4164SWarner Losh 	return (0);
645114b4164SWarner Losh }
646114b4164SWarner Losh 
647114b4164SWarner Losh static int
mmcsd_detach(device_t dev)648114b4164SWarner Losh mmcsd_detach(device_t dev)
649114b4164SWarner Losh {
6500e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
65172dec079SMarius Strobl 	struct mmcsd_part *part;
65272dec079SMarius Strobl 	int i;
6530e1466acSWarner Losh 
65472dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
65572dec079SMarius Strobl 		part = sc->part[i];
656aca38eabSMarius Strobl 		if (part != NULL) {
657aca38eabSMarius Strobl 			if (part->disk != NULL) {
658aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
65972dec079SMarius Strobl 				part->suspend = 0;
66072dec079SMarius Strobl 				if (part->running > 0) {
661eb67f31aSAlexander Motin 					/* kill thread */
66272dec079SMarius Strobl 					part->running = 0;
66372dec079SMarius Strobl 					wakeup(part);
664eb67f31aSAlexander Motin 					/* wait for thread to finish. */
66572dec079SMarius Strobl 					while (part->running != -1)
666aca38eabSMarius Strobl 						msleep(part, &part->disk_mtx, 0,
667aca38eabSMarius Strobl 						    "mmcsd disk detach", 0);
668eb67f31aSAlexander Motin 				}
669aca38eabSMarius Strobl 				MMCSD_DISK_UNLOCK(part);
670aca38eabSMarius Strobl 			}
671aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
672aca38eabSMarius Strobl 			while (part->ioctl > 0)
673aca38eabSMarius Strobl 				msleep(part, &part->ioctl_mtx, 0,
674aca38eabSMarius Strobl 				    "mmcsd IOCTL detach", 0);
675aca38eabSMarius Strobl 			part->ioctl = -1;
676aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
67772dec079SMarius Strobl 		}
67872dec079SMarius Strobl 	}
6790e1466acSWarner Losh 
68072dec079SMarius Strobl 	if (sc->rpmb_dev != NULL)
68172dec079SMarius Strobl 		destroy_dev(sc->rpmb_dev);
68272dec079SMarius Strobl 
68372dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
68472dec079SMarius Strobl 		part = sc->part[i];
68572dec079SMarius Strobl 		if (part != NULL) {
68672dec079SMarius Strobl 			if (part->disk != NULL) {
687249b0e85SAlexander Motin 				/* Flush the request queue. */
68872dec079SMarius Strobl 				bioq_flush(&part->bio_queue, NULL, ENXIO);
6890e1466acSWarner Losh 				/* kill disk */
69072dec079SMarius Strobl 				disk_destroy(part->disk);
6910e1466acSWarner Losh 
692aca38eabSMarius Strobl 				MMCSD_DISK_LOCK_DESTROY(part);
69372dec079SMarius Strobl 			}
694aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK_DESTROY(part);
69572dec079SMarius Strobl 			free(part, M_DEVBUF);
69672dec079SMarius Strobl 		}
69772dec079SMarius Strobl 	}
698646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
699646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
700646fd30cSMarius Strobl 	return (0);
701646fd30cSMarius Strobl }
702646fd30cSMarius Strobl 
703646fd30cSMarius Strobl static int
mmcsd_shutdown(device_t dev)704646fd30cSMarius Strobl mmcsd_shutdown(device_t dev)
705646fd30cSMarius Strobl {
706646fd30cSMarius Strobl 	struct mmcsd_softc *sc = device_get_softc(dev);
707646fd30cSMarius Strobl 
708646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
709646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
710128d0ff2SWarner Losh 	return (0);
711114b4164SWarner Losh }
712114b4164SWarner Losh 
713114b4164SWarner Losh static int
mmcsd_suspend(device_t dev)714eb67f31aSAlexander Motin mmcsd_suspend(device_t dev)
715eb67f31aSAlexander Motin {
716eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
71772dec079SMarius Strobl 	struct mmcsd_part *part;
71872dec079SMarius Strobl 	int i;
719eb67f31aSAlexander Motin 
72072dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
72172dec079SMarius Strobl 		part = sc->part[i];
722aca38eabSMarius Strobl 		if (part != NULL) {
723aca38eabSMarius Strobl 			if (part->disk != NULL) {
724aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
72572dec079SMarius Strobl 				part->suspend = 1;
72672dec079SMarius Strobl 				if (part->running > 0) {
727eb67f31aSAlexander Motin 					/* kill thread */
72872dec079SMarius Strobl 					part->running = 0;
72972dec079SMarius Strobl 					wakeup(part);
730eb67f31aSAlexander Motin 					/* wait for thread to finish. */
73172dec079SMarius Strobl 					while (part->running != -1)
732aca38eabSMarius Strobl 						msleep(part, &part->disk_mtx, 0,
733aca38eabSMarius Strobl 						    "mmcsd disk suspension", 0);
734eb67f31aSAlexander Motin 				}
735aca38eabSMarius Strobl 				MMCSD_DISK_UNLOCK(part);
736aca38eabSMarius Strobl 			}
737aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
738aca38eabSMarius Strobl 			while (part->ioctl > 0)
739aca38eabSMarius Strobl 				msleep(part, &part->ioctl_mtx, 0,
740aca38eabSMarius Strobl 				    "mmcsd IOCTL suspension", 0);
741aca38eabSMarius Strobl 			part->ioctl = -1;
742aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
74372dec079SMarius Strobl 		}
74472dec079SMarius Strobl 	}
745646fd30cSMarius Strobl 	if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
746646fd30cSMarius Strobl 		device_printf(dev, "failed to flush cache\n");
747eb67f31aSAlexander Motin 	return (0);
748eb67f31aSAlexander Motin }
749eb67f31aSAlexander Motin 
750eb67f31aSAlexander Motin static int
mmcsd_resume(device_t dev)751eb67f31aSAlexander Motin mmcsd_resume(device_t dev)
752eb67f31aSAlexander Motin {
753eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
75472dec079SMarius Strobl 	struct mmcsd_part *part;
75572dec079SMarius Strobl 	int i;
756eb67f31aSAlexander Motin 
75772dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
75872dec079SMarius Strobl 		part = sc->part[i];
759aca38eabSMarius Strobl 		if (part != NULL) {
760aca38eabSMarius Strobl 			if (part->disk != NULL) {
761aca38eabSMarius Strobl 				MMCSD_DISK_LOCK(part);
76272dec079SMarius Strobl 				part->suspend = 0;
76372dec079SMarius Strobl 				if (part->running <= 0) {
76472dec079SMarius Strobl 					part->running = 1;
765aca38eabSMarius Strobl 					MMCSD_DISK_UNLOCK(part);
766aca38eabSMarius Strobl 					kproc_create(&mmcsd_task, part,
767aca38eabSMarius Strobl 					    &part->p, 0, 0, "%s%d: mmc/sd card",
768aca38eabSMarius Strobl 					    part->name, part->cnt);
769eb67f31aSAlexander Motin 				} else
770aca38eabSMarius Strobl 					MMCSD_DISK_UNLOCK(part);
771aca38eabSMarius Strobl 			}
772aca38eabSMarius Strobl 			MMCSD_IOCTL_LOCK(part);
773aca38eabSMarius Strobl 			part->ioctl = 0;
774aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
77572dec079SMarius Strobl 		}
77672dec079SMarius Strobl 	}
777eb67f31aSAlexander Motin 	return (0);
778eb67f31aSAlexander Motin }
779eb67f31aSAlexander Motin 
780eb67f31aSAlexander Motin static int
mmcsd_close(struct disk * dp)781646fd30cSMarius Strobl mmcsd_close(struct disk *dp)
782114b4164SWarner Losh {
783646fd30cSMarius Strobl 	struct mmcsd_softc *sc;
784ae5d8757SMarius Strobl 
785646fd30cSMarius Strobl 	if ((dp->d_flags & DISKFLAG_OPEN) != 0) {
786646fd30cSMarius Strobl 		sc = ((struct mmcsd_part *)dp->d_drv1)->sc;
787646fd30cSMarius Strobl 		if (mmcsd_flush_cache(sc) != MMC_ERR_NONE)
788646fd30cSMarius Strobl 			device_printf(sc->dev, "failed to flush cache\n");
789114b4164SWarner Losh 	}
790128d0ff2SWarner Losh 	return (0);
791114b4164SWarner Losh }
792114b4164SWarner Losh 
793114b4164SWarner Losh static void
mmcsd_strategy(struct bio * bp)794114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
795114b4164SWarner Losh {
79672dec079SMarius Strobl 	struct mmcsd_part *part;
797114b4164SWarner Losh 
79872dec079SMarius Strobl 	part = bp->bio_disk->d_drv1;
799aca38eabSMarius Strobl 	MMCSD_DISK_LOCK(part);
80072dec079SMarius Strobl 	if (part->running > 0 || part->suspend > 0) {
80172dec079SMarius Strobl 		bioq_disksort(&part->bio_queue, bp);
802aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
80372dec079SMarius Strobl 		wakeup(part);
804249b0e85SAlexander Motin 	} else {
805aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
806249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
807249b0e85SAlexander Motin 	}
808114b4164SWarner Losh }
809114b4164SWarner Losh 
81072dec079SMarius Strobl static int
mmcsd_ioctl_rpmb(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)81172dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
8127217ea7cSMarius Strobl     int fflag, struct thread *td)
81372dec079SMarius Strobl {
81472dec079SMarius Strobl 
8157217ea7cSMarius Strobl 	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag, td));
81672dec079SMarius Strobl }
81772dec079SMarius Strobl 
81872dec079SMarius Strobl static int
mmcsd_ioctl_disk(struct disk * disk,u_long cmd,void * data,int fflag,struct thread * td)81972dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
8207217ea7cSMarius Strobl     struct thread *td)
82172dec079SMarius Strobl {
82272dec079SMarius Strobl 
8237217ea7cSMarius Strobl 	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag, td));
82472dec079SMarius Strobl }
82572dec079SMarius Strobl 
82672dec079SMarius Strobl static int
mmcsd_ioctl(struct mmcsd_part * part,u_long cmd,void * data,int fflag,struct thread * td)8277217ea7cSMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag,
8287217ea7cSMarius Strobl     struct thread *td)
82972dec079SMarius Strobl {
83072dec079SMarius Strobl 	struct mmc_ioc_cmd *mic;
83172dec079SMarius Strobl 	struct mmc_ioc_multi_cmd *mimc;
83272dec079SMarius Strobl 	int i, err;
83372dec079SMarius Strobl 	u_long cnt, size;
83472dec079SMarius Strobl 
83572dec079SMarius Strobl 	if ((fflag & FREAD) == 0)
83672dec079SMarius Strobl 		return (EBADF);
83772dec079SMarius Strobl 
8387217ea7cSMarius Strobl 	err = priv_check(td, PRIV_DRIVER);
8397217ea7cSMarius Strobl 	if (err != 0)
8407217ea7cSMarius Strobl 		return (err);
8417217ea7cSMarius Strobl 
84272dec079SMarius Strobl 	err = 0;
84372dec079SMarius Strobl 	switch (cmd) {
84472dec079SMarius Strobl 	case MMC_IOC_CMD:
84572dec079SMarius Strobl 		mic = data;
846aca38eabSMarius Strobl 		err = mmcsd_ioctl_cmd(part, mic, fflag);
84772dec079SMarius Strobl 		break;
848aca38eabSMarius Strobl 	case MMC_IOC_MULTI_CMD:
84972dec079SMarius Strobl 		mimc = data;
85072dec079SMarius Strobl 		if (mimc->num_of_cmds == 0)
85172dec079SMarius Strobl 			break;
85272dec079SMarius Strobl 		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
85372dec079SMarius Strobl 			return (EINVAL);
85472dec079SMarius Strobl 		cnt = mimc->num_of_cmds;
85572dec079SMarius Strobl 		size = sizeof(*mic) * cnt;
85672dec079SMarius Strobl 		mic = malloc(size, M_TEMP, M_WAITOK);
85772dec079SMarius Strobl 		err = copyin((const void *)mimc->cmds, mic, size);
858aca38eabSMarius Strobl 		if (err == 0) {
85972dec079SMarius Strobl 			for (i = 0; i < cnt; i++) {
86072dec079SMarius Strobl 				err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
86172dec079SMarius Strobl 				if (err != 0)
86272dec079SMarius Strobl 					break;
86372dec079SMarius Strobl 			}
864aca38eabSMarius Strobl 		}
86572dec079SMarius Strobl 		free(mic, M_TEMP);
86672dec079SMarius Strobl 		break;
86772dec079SMarius Strobl 	default:
86872dec079SMarius Strobl 		return (ENOIOCTL);
86972dec079SMarius Strobl 	}
87072dec079SMarius Strobl 	return (err);
87172dec079SMarius Strobl }
87272dec079SMarius Strobl 
87372dec079SMarius Strobl static int
mmcsd_ioctl_cmd(struct mmcsd_part * part,struct mmc_ioc_cmd * mic,int fflag)87472dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
87572dec079SMarius Strobl {
87672dec079SMarius Strobl 	struct mmc_command cmd;
87772dec079SMarius Strobl 	struct mmc_data data;
87872dec079SMarius Strobl 	struct mmcsd_softc *sc;
879cd85acbaSMarius Strobl 	device_t dev, mmcbus;
88072dec079SMarius Strobl 	void *dp;
88172dec079SMarius Strobl 	u_long len;
88272dec079SMarius Strobl 	int err, retries;
88372dec079SMarius Strobl 	uint32_t status;
88472dec079SMarius Strobl 	uint16_t rca;
88572dec079SMarius Strobl 
88672dec079SMarius Strobl 	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
88772dec079SMarius Strobl 		return (EBADF);
88872dec079SMarius Strobl 
88972dec079SMarius Strobl 	if (part->ro == TRUE && mic->write_flag != 0)
89072dec079SMarius Strobl 		return (EROFS);
89172dec079SMarius Strobl 
892aca38eabSMarius Strobl 	/*
893aca38eabSMarius Strobl 	 * We don't need to explicitly lock against the disk(9) half of this
894aca38eabSMarius Strobl 	 * driver as MMCBUS_ACQUIRE_BUS() will serialize us.  However, it's
895aca38eabSMarius Strobl 	 * necessary to protect against races with detachment and suspension,
896aca38eabSMarius Strobl 	 * especially since it's required to switch away from RPMB partitions
897aca38eabSMarius Strobl 	 * again after an access (see mmcsd_switch_part()).
898aca38eabSMarius Strobl 	 */
899aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK(part);
900aca38eabSMarius Strobl 	while (part->ioctl != 0) {
901aca38eabSMarius Strobl 		if (part->ioctl < 0) {
902aca38eabSMarius Strobl 			MMCSD_IOCTL_UNLOCK(part);
903aca38eabSMarius Strobl 			return (ENXIO);
904aca38eabSMarius Strobl 		}
905aca38eabSMarius Strobl 		msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0);
906aca38eabSMarius Strobl 	}
907aca38eabSMarius Strobl 	part->ioctl = 1;
908aca38eabSMarius Strobl 	MMCSD_IOCTL_UNLOCK(part);
909aca38eabSMarius Strobl 
91072dec079SMarius Strobl 	err = 0;
91172dec079SMarius Strobl 	dp = NULL;
91272dec079SMarius Strobl 	len = mic->blksz * mic->blocks;
913aca38eabSMarius Strobl 	if (len > MMC_IOC_MAX_BYTES) {
914aca38eabSMarius Strobl 		err = EOVERFLOW;
915aca38eabSMarius Strobl 		goto out;
916aca38eabSMarius Strobl 	}
91772dec079SMarius Strobl 	if (len != 0) {
91872dec079SMarius Strobl 		dp = malloc(len, M_TEMP, M_WAITOK);
91972dec079SMarius Strobl 		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
92072dec079SMarius Strobl 		if (err != 0)
92172dec079SMarius Strobl 			goto out;
92272dec079SMarius Strobl 	}
92372dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
92472dec079SMarius Strobl 	memset(&data, 0, sizeof(data));
92572dec079SMarius Strobl 	cmd.opcode = mic->opcode;
92672dec079SMarius Strobl 	cmd.arg = mic->arg;
92772dec079SMarius Strobl 	cmd.flags = mic->flags;
92872dec079SMarius Strobl 	if (len != 0) {
92972dec079SMarius Strobl 		data.len = len;
93072dec079SMarius Strobl 		data.data = dp;
93172dec079SMarius Strobl 		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
93272dec079SMarius Strobl 		    MMC_DATA_READ;
93372dec079SMarius Strobl 		cmd.data = &data;
93472dec079SMarius Strobl 	}
93572dec079SMarius Strobl 	sc = part->sc;
93672dec079SMarius Strobl 	rca = sc->rca;
93772dec079SMarius Strobl 	if (mic->is_acmd == 0) {
93872dec079SMarius Strobl 		/* Enforce/patch/restrict RCA-based commands */
93972dec079SMarius Strobl 		switch (cmd.opcode) {
94072dec079SMarius Strobl 		case MMC_SET_RELATIVE_ADDR:
94172dec079SMarius Strobl 		case MMC_SELECT_CARD:
94272dec079SMarius Strobl 			err = EPERM;
94372dec079SMarius Strobl 			goto out;
94472dec079SMarius Strobl 		case MMC_STOP_TRANSMISSION:
94572dec079SMarius Strobl 			if ((cmd.arg & 0x1) == 0)
94672dec079SMarius Strobl 				break;
94772dec079SMarius Strobl 			/* FALLTHROUGH */
94872dec079SMarius Strobl 		case MMC_SLEEP_AWAKE:
94972dec079SMarius Strobl 		case MMC_SEND_CSD:
95072dec079SMarius Strobl 		case MMC_SEND_CID:
95172dec079SMarius Strobl 		case MMC_SEND_STATUS:
95272dec079SMarius Strobl 		case MMC_GO_INACTIVE_STATE:
95372dec079SMarius Strobl 		case MMC_FAST_IO:
95472dec079SMarius Strobl 		case MMC_APP_CMD:
95572dec079SMarius Strobl 			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
95672dec079SMarius Strobl 			break;
95772dec079SMarius Strobl 		default:
95872dec079SMarius Strobl 			break;
95972dec079SMarius Strobl 		}
9606c06949fSMarius Strobl 		/*
9616c06949fSMarius Strobl 		 * No partition switching in userland; it's almost impossible
9626c06949fSMarius Strobl 		 * to recover from that, especially if things go wrong.
9636c06949fSMarius Strobl 		 */
9646c06949fSMarius Strobl 		if (cmd.opcode == MMC_SWITCH_FUNC && dp != NULL &&
9656c06949fSMarius Strobl 		    (((uint8_t *)dp)[EXT_CSD_PART_CONFIG] &
96661ef738dSMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_MASK) != part->type) {
9676c06949fSMarius Strobl 			err = EINVAL;
9686c06949fSMarius Strobl 			goto out;
9696c06949fSMarius Strobl 		}
97072dec079SMarius Strobl 	}
97172dec079SMarius Strobl 	dev = sc->dev;
972cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
973cd85acbaSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
974cd85acbaSMarius Strobl 	err = mmcsd_switch_part(mmcbus, dev, rca, part->type);
97572dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
97672dec079SMarius Strobl 		goto release;
97772dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
97872dec079SMarius Strobl 		err = mmcsd_set_blockcount(sc, mic->blocks,
97972dec079SMarius Strobl 		    mic->write_flag & (1 << 31));
98072dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
981aca38eabSMarius Strobl 			goto switch_back;
98272dec079SMarius Strobl 	}
983646fd30cSMarius Strobl 	if (mic->write_flag != 0)
984646fd30cSMarius Strobl 		sc->flags |= MMCSD_DIRTY;
98572dec079SMarius Strobl 	if (mic->is_acmd != 0)
986cd85acbaSMarius Strobl 		(void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0);
98772dec079SMarius Strobl 	else
988cd85acbaSMarius Strobl 		(void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0);
98972dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
99072dec079SMarius Strobl 		/*
99172dec079SMarius Strobl 		 * If the request went to the RPMB partition, try to ensure
9926c06949fSMarius Strobl 		 * that the command actually has completed.
99372dec079SMarius Strobl 		 */
99472dec079SMarius Strobl 		retries = MMCSD_CMD_RETRIES;
99572dec079SMarius Strobl 		do {
996cd85acbaSMarius Strobl 			err = mmc_send_status(mmcbus, dev, rca, &status);
99772dec079SMarius Strobl 			if (err != MMC_ERR_NONE)
99872dec079SMarius Strobl 				break;
99972dec079SMarius Strobl 			if (R1_STATUS(status) == 0 &&
100072dec079SMarius Strobl 			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
100172dec079SMarius Strobl 				break;
100272dec079SMarius Strobl 			DELAY(1000);
100372dec079SMarius Strobl 		} while (retries-- > 0);
100472dec079SMarius Strobl 	}
100572dec079SMarius Strobl 	/*
100672dec079SMarius Strobl 	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
100772dec079SMarius Strobl 	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
100872dec079SMarius Strobl 	 * so retrieve EXT_CSD again.
100972dec079SMarius Strobl 	 */
101072dec079SMarius Strobl 	if (cmd.opcode == MMC_SWITCH_FUNC) {
1011cd85acbaSMarius Strobl 		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
101272dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
101372dec079SMarius Strobl 			goto release;
101472dec079SMarius Strobl 	}
10156c06949fSMarius Strobl switch_back:
10166c06949fSMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
10176c06949fSMarius Strobl 		/*
10186c06949fSMarius Strobl 		 * If the request went to the RPMB partition, always switch
10196c06949fSMarius Strobl 		 * back to the default partition (see mmcsd_switch_part()).
10206c06949fSMarius Strobl 		 */
10216c06949fSMarius Strobl 		err = mmcsd_switch_part(mmcbus, dev, rca,
10226c06949fSMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
10236c06949fSMarius Strobl 		if (err != MMC_ERR_NONE)
10246c06949fSMarius Strobl 			goto release;
10256c06949fSMarius Strobl 	}
1026cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
102772dec079SMarius Strobl 	if (cmd.error != MMC_ERR_NONE) {
102872dec079SMarius Strobl 		switch (cmd.error) {
102972dec079SMarius Strobl 		case MMC_ERR_TIMEOUT:
103072dec079SMarius Strobl 			err = ETIMEDOUT;
103172dec079SMarius Strobl 			break;
103272dec079SMarius Strobl 		case MMC_ERR_BADCRC:
103372dec079SMarius Strobl 			err = EILSEQ;
103472dec079SMarius Strobl 			break;
103572dec079SMarius Strobl 		case MMC_ERR_INVALID:
103672dec079SMarius Strobl 			err = EINVAL;
103772dec079SMarius Strobl 			break;
103872dec079SMarius Strobl 		case MMC_ERR_NO_MEMORY:
103972dec079SMarius Strobl 			err = ENOMEM;
104072dec079SMarius Strobl 			break;
104172dec079SMarius Strobl 		default:
104272dec079SMarius Strobl 			err = EIO;
104372dec079SMarius Strobl 			break;
104472dec079SMarius Strobl 		}
104572dec079SMarius Strobl 		goto out;
104672dec079SMarius Strobl 	}
104772dec079SMarius Strobl 	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
104872dec079SMarius Strobl 	if (mic->write_flag == 0 && len != 0) {
104972dec079SMarius Strobl 		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
105072dec079SMarius Strobl 		if (err != 0)
105172dec079SMarius Strobl 			goto out;
105272dec079SMarius Strobl 	}
105372dec079SMarius Strobl 	goto out;
105472dec079SMarius Strobl 
105572dec079SMarius Strobl release:
1056cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
105772dec079SMarius Strobl 	err = EIO;
105872dec079SMarius Strobl 
105972dec079SMarius Strobl out:
1060aca38eabSMarius Strobl 	MMCSD_IOCTL_LOCK(part);
1061aca38eabSMarius Strobl 	part->ioctl = 0;
1062aca38eabSMarius Strobl 	MMCSD_IOCTL_UNLOCK(part);
1063aca38eabSMarius Strobl 	wakeup(part);
106472dec079SMarius Strobl 	if (dp != NULL)
106572dec079SMarius Strobl 		free(dp, M_TEMP);
106672dec079SMarius Strobl 	return (err);
106772dec079SMarius Strobl }
106872dec079SMarius Strobl 
106972dec079SMarius Strobl static int
mmcsd_getattr(struct bio * bp)107072dec079SMarius Strobl mmcsd_getattr(struct bio *bp)
107172dec079SMarius Strobl {
107272dec079SMarius Strobl 	struct mmcsd_part *part;
107372dec079SMarius Strobl 	device_t dev;
107472dec079SMarius Strobl 
107572dec079SMarius Strobl 	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
107672dec079SMarius Strobl 		if (bp->bio_length != sizeof(dev))
107772dec079SMarius Strobl 			return (EFAULT);
107872dec079SMarius Strobl 		part = bp->bio_disk->d_drv1;
107972dec079SMarius Strobl 		dev = part->sc->dev;
108072dec079SMarius Strobl 		bcopy(&dev, bp->bio_data, sizeof(dev));
108172dec079SMarius Strobl 		bp->bio_completed = bp->bio_length;
108272dec079SMarius Strobl 		return (0);
108372dec079SMarius Strobl 	}
108472dec079SMarius Strobl 	return (-1);
108572dec079SMarius Strobl }
108672dec079SMarius Strobl 
108772dec079SMarius Strobl static int
mmcsd_set_blockcount(struct mmcsd_softc * sc,u_int count,bool reliable)108872dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
108972dec079SMarius Strobl {
109072dec079SMarius Strobl 	struct mmc_command cmd;
109172dec079SMarius Strobl 	struct mmc_request req;
109272dec079SMarius Strobl 
109372dec079SMarius Strobl 	memset(&req, 0, sizeof(req));
109472dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
109572dec079SMarius Strobl 	cmd.mrq = &req;
109672dec079SMarius Strobl 	req.cmd = &cmd;
109772dec079SMarius Strobl 	cmd.opcode = MMC_SET_BLOCK_COUNT;
109872dec079SMarius Strobl 	cmd.arg = count & 0x0000FFFF;
109972dec079SMarius Strobl 	if (reliable)
110072dec079SMarius Strobl 		cmd.arg |= 1 << 31;
110172dec079SMarius Strobl 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1102cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req);
110372dec079SMarius Strobl 	return (cmd.error);
110472dec079SMarius Strobl }
110572dec079SMarius Strobl 
110672dec079SMarius Strobl static int
mmcsd_switch_part(device_t bus,device_t dev,uint16_t rca,u_int part)110772dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
110872dec079SMarius Strobl {
110972dec079SMarius Strobl 	struct mmcsd_softc *sc;
111072dec079SMarius Strobl 	int err;
111172dec079SMarius Strobl 	uint8_t	value;
111272dec079SMarius Strobl 
111372dec079SMarius Strobl 	sc = device_get_softc(dev);
111472dec079SMarius Strobl 
1115aca38eabSMarius Strobl 	if (sc->mode == mode_sd)
111672dec079SMarius Strobl 		return (MMC_ERR_NONE);
111772dec079SMarius Strobl 
1118aca38eabSMarius Strobl 	/*
1119aca38eabSMarius Strobl 	 * According to section "6.2.2 Command restrictions" of the eMMC
1120aca38eabSMarius Strobl 	 * specification v5.1, CMD19/CMD21 aren't allowed to be used with
1121aca38eabSMarius Strobl 	 * RPMB partitions.  So we pause re-tuning along with triggering
1122aca38eabSMarius Strobl 	 * it up-front to decrease the likelihood of re-tuning becoming
1123aca38eabSMarius Strobl 	 * necessary while accessing an RPMB partition.  Consequently, an
1124aca38eabSMarius Strobl 	 * RPMB partition should immediately be switched away from again
1125aca38eabSMarius Strobl 	 * after an access in order to allow for re-tuning to take place
1126aca38eabSMarius Strobl 	 * anew.
1127aca38eabSMarius Strobl 	 */
1128aca38eabSMarius Strobl 	if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1129cd85acbaSMarius Strobl 		MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true);
1130aca38eabSMarius Strobl 
1131aca38eabSMarius Strobl 	if (sc->part_curr == part)
113272dec079SMarius Strobl 		return (MMC_ERR_NONE);
113372dec079SMarius Strobl 
113472dec079SMarius Strobl 	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
113572dec079SMarius Strobl 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
113672dec079SMarius Strobl 	/* Jump! */
113772dec079SMarius Strobl 	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
113872dec079SMarius Strobl 	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
1139aca38eabSMarius Strobl 	if (err != MMC_ERR_NONE) {
1140aca38eabSMarius Strobl 		if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1141cd85acbaSMarius Strobl 			MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
114272dec079SMarius Strobl 		return (err);
1143aca38eabSMarius Strobl 	}
114472dec079SMarius Strobl 
114572dec079SMarius Strobl 	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
1146aca38eabSMarius Strobl 	if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1147cd85acbaSMarius Strobl 		MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
114872dec079SMarius Strobl 	sc->part_curr = part;
114972dec079SMarius Strobl 	return (MMC_ERR_NONE);
115072dec079SMarius Strobl }
115172dec079SMarius Strobl 
1152bb1ef63fSWarner Losh static const char *
mmcsd_errmsg(int e)1153bb1ef63fSWarner Losh mmcsd_errmsg(int e)
1154bb1ef63fSWarner Losh {
11551bacf3beSMarius Strobl 
1156bb1ef63fSWarner Losh 	if (e < 0 || e > MMC_ERR_MAX)
1157bb1ef63fSWarner Losh 		return "Bad error code";
1158aca38eabSMarius Strobl 	return (errmsg[e]);
1159bb1ef63fSWarner Losh }
1160bb1ef63fSWarner Losh 
11613906d42dSAlexander Motin static daddr_t
mmcsd_rw(struct mmcsd_part * part,struct bio * bp)116272dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
1163114b4164SWarner Losh {
1164114b4164SWarner Losh 	daddr_t block, end;
1165114b4164SWarner Losh 	struct mmc_command cmd;
1166114b4164SWarner Losh 	struct mmc_command stop;
1167114b4164SWarner Losh 	struct mmc_request req;
1168114b4164SWarner Losh 	struct mmc_data data;
116972dec079SMarius Strobl 	struct mmcsd_softc *sc;
1170cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1171aca38eabSMarius Strobl 	u_int numblocks, sz;
11721bacf3beSMarius Strobl 	char *vaddr;
1173114b4164SWarner Losh 
117472dec079SMarius Strobl 	sc = part->sc;
117572dec079SMarius Strobl 	dev = sc->dev;
1176cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
117772dec079SMarius Strobl 
11783906d42dSAlexander Motin 	block = bp->bio_pblkno;
117972dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
1180114b4164SWarner Losh 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
11813906d42dSAlexander Motin 	while (block < end) {
11821bacf3beSMarius Strobl 		vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
1183aca38eabSMarius Strobl 		numblocks = min(end - block, sc->max_data);
1184114b4164SWarner Losh 		memset(&req, 0, sizeof(req));
1185114b4164SWarner Losh 		memset(&cmd, 0, sizeof(cmd));
1186114b4164SWarner Losh 		memset(&stop, 0, sizeof(stop));
1187ed7142a7SIan Lepore 		memset(&data, 0, sizeof(data));
1188a350e540SIan Lepore 		cmd.mrq = &req;
1189114b4164SWarner Losh 		req.cmd = &cmd;
1190114b4164SWarner Losh 		cmd.data = &data;
1191114b4164SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
11920c0903b1SWarner Losh 			if (numblocks > 1)
1193114b4164SWarner Losh 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1194114b4164SWarner Losh 			else
1195114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
11960c0903b1SWarner Losh 		} else {
1197646fd30cSMarius Strobl 			sc->flags |= MMCSD_DIRTY;
11980c0903b1SWarner Losh 			if (numblocks > 1)
11990c0903b1SWarner Losh 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
12000c0903b1SWarner Losh 			else
1201114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
12020c0903b1SWarner Losh 		}
1203c18f1e26SAlexander Motin 		cmd.arg = block;
1204aca38eabSMarius Strobl 		if (sc->high_cap == 0)
1205c18f1e26SAlexander Motin 			cmd.arg <<= 9;
1206114b4164SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1207114b4164SWarner Losh 		data.data = vaddr;
1208114b4164SWarner Losh 		data.mrq = &req;
12090c0903b1SWarner Losh 		if (bp->bio_cmd == BIO_READ)
1210114b4164SWarner Losh 			data.flags = MMC_DATA_READ;
12110c0903b1SWarner Losh 		else
1212114b4164SWarner Losh 			data.flags = MMC_DATA_WRITE;
12130c0903b1SWarner Losh 		data.len = numblocks * sz;
12140c0903b1SWarner Losh 		if (numblocks > 1) {
12150c0903b1SWarner Losh 			data.flags |= MMC_DATA_MULTI;
1216114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
1217114b4164SWarner Losh 			stop.arg = 0;
1218114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1219a350e540SIan Lepore 			stop.mrq = &req;
12200c0903b1SWarner Losh 			req.stop = &stop;
12210c0903b1SWarner Losh 		}
1222cd85acbaSMarius Strobl 		MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1223bb1ef63fSWarner Losh 		if (req.cmd->error != MMC_ERR_NONE) {
12241bacf3beSMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
12251bacf3beSMarius Strobl 			    LOG_PPS))
1226bb1ef63fSWarner Losh 				device_printf(dev, "Error indicated: %d %s\n",
12271bacf3beSMarius Strobl 				    req.cmd->error,
12281bacf3beSMarius Strobl 				    mmcsd_errmsg(req.cmd->error));
12290c0903b1SWarner Losh 			break;
1230bb1ef63fSWarner Losh 		}
12310c0903b1SWarner Losh 		block += numblocks;
1232114b4164SWarner Losh 	}
12333906d42dSAlexander Motin 	return (block);
12343906d42dSAlexander Motin }
12353906d42dSAlexander Motin 
12363906d42dSAlexander Motin static daddr_t
mmcsd_delete(struct mmcsd_part * part,struct bio * bp)123772dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
12383906d42dSAlexander Motin {
12393906d42dSAlexander Motin 	daddr_t block, end, start, stop;
12403906d42dSAlexander Motin 	struct mmc_command cmd;
12413906d42dSAlexander Motin 	struct mmc_request req;
124272dec079SMarius Strobl 	struct mmcsd_softc *sc;
1243cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1244aca38eabSMarius Strobl 	u_int erase_sector, sz;
124579f39c6aSMarius Strobl 	int err;
124679f39c6aSMarius Strobl 	bool use_trim;
124772dec079SMarius Strobl 
124872dec079SMarius Strobl 	sc = part->sc;
124972dec079SMarius Strobl 	dev = sc->dev;
1250cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
12513906d42dSAlexander Motin 
12523906d42dSAlexander Motin 	block = bp->bio_pblkno;
125372dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
12543906d42dSAlexander Motin 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
125579f39c6aSMarius Strobl 	use_trim = sc->flags & MMCSD_USE_TRIM;
125679f39c6aSMarius Strobl 	if (use_trim == true) {
125779f39c6aSMarius Strobl 		start = block;
125879f39c6aSMarius Strobl 		stop = end;
125979f39c6aSMarius Strobl 	} else {
126079f39c6aSMarius Strobl 		/* Coalesce with the remainder of the previous request. */
126172dec079SMarius Strobl 		if (block > part->eblock && block <= part->eend)
126272dec079SMarius Strobl 			block = part->eblock;
126372dec079SMarius Strobl 		if (end >= part->eblock && end < part->eend)
126472dec079SMarius Strobl 			end = part->eend;
126579f39c6aSMarius Strobl 		/* Safely round to the erase sector boundaries. */
1266aca38eabSMarius Strobl 		erase_sector = sc->erase_sector;
12673906d42dSAlexander Motin 		start = block + erase_sector - 1;	 /* Round up. */
12683906d42dSAlexander Motin 		start -= start % erase_sector;
12693906d42dSAlexander Motin 		stop = end;				/* Round down. */
12703906d42dSAlexander Motin 		stop -= end % erase_sector;
127179f39c6aSMarius Strobl 		/*
127279f39c6aSMarius Strobl 		 * We can't erase an area smaller than an erase sector, so
127379f39c6aSMarius Strobl 		 * store it for later.
127479f39c6aSMarius Strobl 		 */
1275a7cf6274SAlexander Motin 		if (start >= stop) {
127672dec079SMarius Strobl 			part->eblock = block;
127772dec079SMarius Strobl 			part->eend = end;
12783906d42dSAlexander Motin 			return (end);
1279a7cf6274SAlexander Motin 		}
128079f39c6aSMarius Strobl 	}
128179f39c6aSMarius Strobl 
128279f39c6aSMarius Strobl 	if ((sc->flags & MMCSD_INAND_CMD38) != 0) {
128379f39c6aSMarius Strobl 		err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL,
128479f39c6aSMarius Strobl 		    EXT_CSD_INAND_CMD38, use_trim == true ?
128579f39c6aSMarius Strobl 		    EXT_CSD_INAND_CMD38_TRIM : EXT_CSD_INAND_CMD38_ERASE,
128679f39c6aSMarius Strobl 		    sc->cmd6_time, true);
128779f39c6aSMarius Strobl 		if (err != MMC_ERR_NONE) {
128879f39c6aSMarius Strobl 			device_printf(dev,
128979f39c6aSMarius Strobl 			    "Setting iNAND erase command failed %s\n",
129079f39c6aSMarius Strobl 			    mmcsd_errmsg(err));
129179f39c6aSMarius Strobl 			return (block);
129279f39c6aSMarius Strobl 		}
129379f39c6aSMarius Strobl 	}
12943906d42dSAlexander Motin 
1295aca38eabSMarius Strobl 	/*
1296aca38eabSMarius Strobl 	 * Pause re-tuning so it won't interfere with the order of erase
1297aca38eabSMarius Strobl 	 * commands.  Note that these latter don't use the data lines, so
1298aca38eabSMarius Strobl 	 * re-tuning shouldn't actually become necessary during erase.
1299aca38eabSMarius Strobl 	 */
1300cd85acbaSMarius Strobl 	MMCBUS_RETUNE_PAUSE(mmcbus, dev, false);
13013906d42dSAlexander Motin 	/* Set erase start position. */
13023906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13033906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
1304a350e540SIan Lepore 	cmd.mrq = &req;
13053906d42dSAlexander Motin 	req.cmd = &cmd;
1306608226d5SMarius Strobl 	if (sc->mode == mode_sd)
13073906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_START;
13083906d42dSAlexander Motin 	else
13093906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_START;
13103906d42dSAlexander Motin 	cmd.arg = start;
1311aca38eabSMarius Strobl 	if (sc->high_cap == 0)
13123906d42dSAlexander Motin 		cmd.arg <<= 9;
13133906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1314cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13153906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
131679f39c6aSMarius Strobl 		device_printf(dev, "Setting erase start position failed %s\n",
131779f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1318aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1319aca38eabSMarius Strobl 		goto unpause;
13203906d42dSAlexander Motin 	}
13213906d42dSAlexander Motin 	/* Set erase stop position. */
13223906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13233906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
13243906d42dSAlexander Motin 	req.cmd = &cmd;
1325608226d5SMarius Strobl 	if (sc->mode == mode_sd)
13263906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_END;
13273906d42dSAlexander Motin 	else
13283906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_END;
13293906d42dSAlexander Motin 	cmd.arg = stop;
1330aca38eabSMarius Strobl 	if (sc->high_cap == 0)
13313906d42dSAlexander Motin 		cmd.arg <<= 9;
13323906d42dSAlexander Motin 	cmd.arg--;
13333906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1334cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13353906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
133679f39c6aSMarius Strobl 		device_printf(dev, "Setting erase stop position failed %s\n",
133779f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1338aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1339aca38eabSMarius Strobl 		goto unpause;
13403906d42dSAlexander Motin 	}
13413906d42dSAlexander Motin 	/* Erase range. */
13423906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
13433906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
13443906d42dSAlexander Motin 	req.cmd = &cmd;
13453906d42dSAlexander Motin 	cmd.opcode = MMC_ERASE;
134679f39c6aSMarius Strobl 	cmd.arg = use_trim == true ? MMC_ERASE_TRIM : MMC_ERASE_ERASE;
13473906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1348cd85acbaSMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
13493906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
135079f39c6aSMarius Strobl 		device_printf(dev, "Issuing erase command failed %s\n",
135179f39c6aSMarius Strobl 		    mmcsd_errmsg(req.cmd->error));
1352aca38eabSMarius Strobl 		block = bp->bio_pblkno;
1353aca38eabSMarius Strobl 		goto unpause;
13543906d42dSAlexander Motin 	}
135579f39c6aSMarius Strobl 	if (use_trim == false) {
135679f39c6aSMarius Strobl 		/* Store one of the remaining parts for the next call. */
135772dec079SMarius Strobl 		if (bp->bio_pblkno >= part->eblock || block == start) {
135872dec079SMarius Strobl 			part->eblock = stop;	/* Predict next forward. */
135972dec079SMarius Strobl 			part->eend = end;
1360a7cf6274SAlexander Motin 		} else {
136172dec079SMarius Strobl 			part->eblock = block;	/* Predict next backward. */
136272dec079SMarius Strobl 			part->eend = start;
1363a7cf6274SAlexander Motin 		}
136479f39c6aSMarius Strobl 	}
1365aca38eabSMarius Strobl 	block = end;
1366aca38eabSMarius Strobl unpause:
1367cd85acbaSMarius Strobl 	MMCBUS_RETUNE_UNPAUSE(mmcbus, dev);
1368aca38eabSMarius Strobl 	return (block);
13693906d42dSAlexander Motin }
13703906d42dSAlexander Motin 
1371a1fda318SAlexander Motin static int
mmcsd_dump(void * arg,void * virtual,off_t offset,size_t length)1372489ba222SMitchell Horne mmcsd_dump(void *arg, void *virtual, off_t offset, size_t length)
1373a1fda318SAlexander Motin {
1374a1fda318SAlexander Motin 	struct bio bp;
1375a1fda318SAlexander Motin 	daddr_t block, end;
137672dec079SMarius Strobl 	struct disk *disk;
137772dec079SMarius Strobl 	struct mmcsd_softc *sc;
137872dec079SMarius Strobl 	struct mmcsd_part *part;
1379cd85acbaSMarius Strobl 	device_t dev, mmcbus;
138072dec079SMarius Strobl 	int err;
1381a1fda318SAlexander Motin 
138272dec079SMarius Strobl 	disk = arg;
138372dec079SMarius Strobl 	part = disk->d_drv1;
138472dec079SMarius Strobl 	sc = part->sc;
1385646fd30cSMarius Strobl 
1386646fd30cSMarius Strobl 	/* length zero is special and really means flush buffers to media */
1387646fd30cSMarius Strobl 	if (length == 0) {
1388646fd30cSMarius Strobl 		err = mmcsd_flush_cache(sc);
1389646fd30cSMarius Strobl 		if (err != MMC_ERR_NONE)
1390646fd30cSMarius Strobl 			return (EIO);
1391646fd30cSMarius Strobl 		return (0);
1392646fd30cSMarius Strobl 	}
1393646fd30cSMarius Strobl 
139472dec079SMarius Strobl 	dev = sc->dev;
1395cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
139672dec079SMarius Strobl 
1397c55f5707SWarner Losh 	g_reset_bio(&bp);
1398a1fda318SAlexander Motin 	bp.bio_disk = disk;
1399a1fda318SAlexander Motin 	bp.bio_pblkno = offset / disk->d_sectorsize;
1400a1fda318SAlexander Motin 	bp.bio_bcount = length;
1401a1fda318SAlexander Motin 	bp.bio_data = virtual;
1402a1fda318SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
140372dec079SMarius Strobl 	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
1404cd85acbaSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1405cd85acbaSMarius Strobl 	err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
140672dec079SMarius Strobl 	if (err != MMC_ERR_NONE) {
140772dec079SMarius Strobl 		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
140872dec079SMarius Strobl 			device_printf(dev, "Partition switch error\n");
1409cd85acbaSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
141072dec079SMarius Strobl 		return (EIO);
141172dec079SMarius Strobl 	}
141272dec079SMarius Strobl 	block = mmcsd_rw(part, &bp);
1413cd85acbaSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
1414a1fda318SAlexander Motin 	return ((end < block) ? EIO : 0);
1415a1fda318SAlexander Motin }
1416a1fda318SAlexander Motin 
14173906d42dSAlexander Motin static void
mmcsd_task(void * arg)14183906d42dSAlexander Motin mmcsd_task(void *arg)
14193906d42dSAlexander Motin {
14203906d42dSAlexander Motin 	daddr_t block, end;
142172dec079SMarius Strobl 	struct mmcsd_part *part;
142272dec079SMarius Strobl 	struct mmcsd_softc *sc;
142372dec079SMarius Strobl 	struct bio *bp;
1424cd85acbaSMarius Strobl 	device_t dev, mmcbus;
1425d176b803SScott Long 	int bio_error, err, sz;
142672dec079SMarius Strobl 
142772dec079SMarius Strobl 	part = arg;
142872dec079SMarius Strobl 	sc = part->sc;
142972dec079SMarius Strobl 	dev = sc->dev;
1430cd85acbaSMarius Strobl 	mmcbus = sc->mmcbus;
14313906d42dSAlexander Motin 
1432249b0e85SAlexander Motin 	while (1) {
14333b433ed7SMarius Strobl 		bio_error = 0;
1434aca38eabSMarius Strobl 		MMCSD_DISK_LOCK(part);
14353906d42dSAlexander Motin 		do {
143672dec079SMarius Strobl 			if (part->running == 0)
1437249b0e85SAlexander Motin 				goto out;
143872dec079SMarius Strobl 			bp = bioq_takefirst(&part->bio_queue);
14393906d42dSAlexander Motin 			if (bp == NULL)
1440aca38eabSMarius Strobl 				msleep(part, &part->disk_mtx, PRIBIO,
1441aca38eabSMarius Strobl 				    "mmcsd disk jobqueue", 0);
1442249b0e85SAlexander Motin 		} while (bp == NULL);
1443aca38eabSMarius Strobl 		MMCSD_DISK_UNLOCK(part);
1444646fd30cSMarius Strobl 		if (__predict_false(bp->bio_cmd == BIO_FLUSH)) {
1445646fd30cSMarius Strobl 			if (mmcsd_flush_cache(sc) != MMC_ERR_NONE) {
1446646fd30cSMarius Strobl 				bp->bio_error = EIO;
1447646fd30cSMarius Strobl 				bp->bio_flags |= BIO_ERROR;
1448646fd30cSMarius Strobl 			}
1449646fd30cSMarius Strobl 			biodone(bp);
1450646fd30cSMarius Strobl 			continue;
1451646fd30cSMarius Strobl 		}
145272dec079SMarius Strobl 		if (bp->bio_cmd != BIO_READ && part->ro) {
14533906d42dSAlexander Motin 			bp->bio_error = EROFS;
14543906d42dSAlexander Motin 			bp->bio_resid = bp->bio_bcount;
14553906d42dSAlexander Motin 			bp->bio_flags |= BIO_ERROR;
14563906d42dSAlexander Motin 			biodone(bp);
14573906d42dSAlexander Motin 			continue;
14583906d42dSAlexander Motin 		}
1459cd85acbaSMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
146072dec079SMarius Strobl 		sz = part->disk->d_sectorsize;
14613906d42dSAlexander Motin 		block = bp->bio_pblkno;
14623906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
1463cd85acbaSMarius Strobl 		err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
146472dec079SMarius Strobl 		if (err != MMC_ERR_NONE) {
146572dec079SMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
146672dec079SMarius Strobl 			    LOG_PPS))
146772dec079SMarius Strobl 				device_printf(dev, "Partition switch error\n");
146872dec079SMarius Strobl 			goto release;
146972dec079SMarius Strobl 		}
14703906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1471a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
147272dec079SMarius Strobl 			if (block < part->eend && end > part->eblock)
147372dec079SMarius Strobl 				part->eblock = part->eend = 0;
147472dec079SMarius Strobl 			block = mmcsd_rw(part, bp);
14753b433ed7SMarius Strobl 		} else if (bp->bio_cmd == BIO_DELETE)
147672dec079SMarius Strobl 			block = mmcsd_delete(part, bp);
14773b433ed7SMarius Strobl 		else
1478d176b803SScott Long 			bio_error = EOPNOTSUPP;
147972dec079SMarius Strobl release:
1480cd85acbaSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
14810c0903b1SWarner Losh 		if (block < end) {
1482d176b803SScott Long 			bp->bio_error = (bio_error == 0) ? EIO : bio_error;
14830c0903b1SWarner Losh 			bp->bio_resid = (end - block) * sz;
14840c0903b1SWarner Losh 			bp->bio_flags |= BIO_ERROR;
14853b433ed7SMarius Strobl 		} else
1486d119f637SMarius Strobl 			bp->bio_resid = 0;
1487114b4164SWarner Losh 		biodone(bp);
1488114b4164SWarner Losh 	}
1489249b0e85SAlexander Motin out:
14900e1466acSWarner Losh 	/* tell parent we're done */
149172dec079SMarius Strobl 	part->running = -1;
1492aca38eabSMarius Strobl 	MMCSD_DISK_UNLOCK(part);
149372dec079SMarius Strobl 	wakeup(part);
14940e1466acSWarner Losh 
14953745c395SJulian Elischer 	kproc_exit(0);
1496114b4164SWarner Losh }
1497114b4164SWarner Losh 
1498a0075e58SWarner Losh static int
mmcsd_bus_bit_width(device_t dev)1499a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev)
1500a0075e58SWarner Losh {
1501ae5d8757SMarius Strobl 
1502a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_1)
1503a0075e58SWarner Losh 		return (1);
1504a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_4)
1505a0075e58SWarner Losh 		return (4);
1506a0075e58SWarner Losh 	return (8);
1507a0075e58SWarner Losh }
1508a0075e58SWarner Losh 
1509646fd30cSMarius Strobl static int
mmcsd_flush_cache(struct mmcsd_softc * sc)1510646fd30cSMarius Strobl mmcsd_flush_cache(struct mmcsd_softc *sc)
1511646fd30cSMarius Strobl {
1512646fd30cSMarius Strobl 	device_t dev, mmcbus;
1513646fd30cSMarius Strobl 	int err;
1514646fd30cSMarius Strobl 
1515646fd30cSMarius Strobl 	if ((sc->flags & MMCSD_FLUSH_CACHE) == 0)
1516646fd30cSMarius Strobl 		return (MMC_ERR_NONE);
1517646fd30cSMarius Strobl 
1518646fd30cSMarius Strobl 	dev = sc->dev;
1519646fd30cSMarius Strobl 	mmcbus = sc->mmcbus;
1520646fd30cSMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1521646fd30cSMarius Strobl 	if ((sc->flags & MMCSD_DIRTY) == 0) {
1522646fd30cSMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbus, dev);
1523646fd30cSMarius Strobl 		return (MMC_ERR_NONE);
1524646fd30cSMarius Strobl 	}
1525646fd30cSMarius Strobl 	err = mmc_switch(mmcbus, dev, sc->rca, EXT_CSD_CMD_SET_NORMAL,
1526646fd30cSMarius Strobl 	    EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE_FLUSH, 60 * 1000, true);
1527646fd30cSMarius Strobl 	if (err == MMC_ERR_NONE)
1528646fd30cSMarius Strobl 		sc->flags &= ~MMCSD_DIRTY;
1529646fd30cSMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbus, dev);
1530646fd30cSMarius Strobl 	return (err);
1531646fd30cSMarius Strobl }
1532646fd30cSMarius Strobl 
1533114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
1534114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
1535114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
1536114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
1537646fd30cSMarius Strobl 	DEVMETHOD(device_shutdown, mmcsd_shutdown),
1538eb67f31aSAlexander Motin 	DEVMETHOD(device_suspend, mmcsd_suspend),
1539eb67f31aSAlexander Motin 	DEVMETHOD(device_resume, mmcsd_resume),
15407aa65846SMarius Strobl 	DEVMETHOD_END
1541114b4164SWarner Losh };
1542114b4164SWarner Losh 
1543114b4164SWarner Losh static driver_t mmcsd_driver = {
1544114b4164SWarner Losh 	"mmcsd",
1545114b4164SWarner Losh 	mmcsd_methods,
1546114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
1547114b4164SWarner Losh };
1548114b4164SWarner Losh 
154972dec079SMarius Strobl static int
mmcsd_handler(module_t mod __unused,int what,void * arg __unused)155072dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
155172dec079SMarius Strobl {
155272dec079SMarius Strobl 
155372dec079SMarius Strobl 	switch (what) {
155472dec079SMarius Strobl 	case MOD_LOAD:
155572dec079SMarius Strobl 		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
155672dec079SMarius Strobl 		    TRUE);
155772dec079SMarius Strobl 		return (0);
155872dec079SMarius Strobl 	case MOD_UNLOAD:
155972dec079SMarius Strobl 		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
156072dec079SMarius Strobl 		return (0);
156172dec079SMarius Strobl 	}
156272dec079SMarius Strobl 	return (0);
156372dec079SMarius Strobl }
156472dec079SMarius Strobl 
1565b58c5abfSJohn Baldwin DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_handler, NULL);
156672dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
156772dec079SMarius Strobl MMC_DEPEND(mmcsd);
1568