xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 72dec079)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
472dec079SMarius Strobl  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
5114b4164SWarner Losh  *
6114b4164SWarner Losh  * Redistribution and use in source and binary forms, with or without
7114b4164SWarner Losh  * modification, are permitted provided that the following conditions
8114b4164SWarner Losh  * are met:
9114b4164SWarner Losh  * 1. Redistributions of source code must retain the above copyright
10114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer.
11114b4164SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13114b4164SWarner Losh  *    documentation and/or other materials provided with the distribution.
14114b4164SWarner Losh  *
15114b4164SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16114b4164SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17114b4164SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18114b4164SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19114b4164SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20114b4164SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21114b4164SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22114b4164SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23114b4164SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24114b4164SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2514eced72SWarner Losh  *
2614eced72SWarner Losh  * Portions of this software may have been developed with reference to
2714eced72SWarner Losh  * the SD Simplified Specification.  The following disclaimer may apply:
2814eced72SWarner Losh  *
2914eced72SWarner Losh  * The following conditions apply to the release of the simplified
3014eced72SWarner Losh  * specification ("Simplified Specification") by the SD Card Association and
3114eced72SWarner Losh  * the SD Group. The Simplified Specification is a subset of the complete SD
3214eced72SWarner Losh  * Specification which is owned by the SD Card Association and the SD
3314eced72SWarner Losh  * Group. This Simplified Specification is provided on a non-confidential
3414eced72SWarner Losh  * basis subject to the disclaimers below. Any implementation of the
3514eced72SWarner Losh  * Simplified Specification may require a license from the SD Card
3614eced72SWarner Losh  * Association, SD Group, SD-3C LLC or other third parties.
3714eced72SWarner Losh  *
3814eced72SWarner Losh  * Disclaimers:
3914eced72SWarner Losh  *
4014eced72SWarner Losh  * The information contained in the Simplified Specification is presented only
4114eced72SWarner Losh  * as a standard specification for SD Cards and SD Host/Ancillary products and
4214eced72SWarner Losh  * is provided "AS-IS" without any representations or warranties of any
4314eced72SWarner Losh  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
4414eced72SWarner Losh  * Card Association for any damages, any infringements of patents or other
4514eced72SWarner Losh  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
4614eced72SWarner Losh  * parties, which may result from its use. No license is granted by
4714eced72SWarner Losh  * implication, estoppel or otherwise under any patent or other rights of the
4814eced72SWarner Losh  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
4914eced72SWarner Losh  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
5014eced72SWarner Losh  * or the SD Card Association to disclose or distribute any technical
5114eced72SWarner Losh  * information, know-how or other confidential information to any third party.
52114b4164SWarner Losh  */
53114b4164SWarner Losh 
54114b4164SWarner Losh #include <sys/cdefs.h>
55114b4164SWarner Losh __FBSDID("$FreeBSD$");
56114b4164SWarner Losh 
57114b4164SWarner Losh #include <sys/param.h>
58114b4164SWarner Losh #include <sys/systm.h>
59114b4164SWarner Losh #include <sys/bio.h>
60114b4164SWarner Losh #include <sys/bus.h>
61114b4164SWarner Losh #include <sys/conf.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>
7072dec079SMarius Strobl #include <sys/slicer.h>
715e3dbf8bSIan Lepore #include <sys/time.h>
7272dec079SMarius Strobl 
73c55f5707SWarner Losh #include <geom/geom.h>
74114b4164SWarner Losh #include <geom/geom_disk.h>
75114b4164SWarner Losh 
7672dec079SMarius Strobl #include <dev/mmc/bridge.h>
7772dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h>
7872dec079SMarius Strobl #include <dev/mmc/mmc_subr.h>
797aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h>
80114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
817aa65846SMarius Strobl #include <dev/mmc/mmcvar.h>
82114b4164SWarner Losh 
83114b4164SWarner Losh #include "mmcbus_if.h"
84114b4164SWarner Losh 
857aa65846SMarius Strobl #if __FreeBSD_version < 800002
867aa65846SMarius Strobl #define	kproc_create	kthread_create
877aa65846SMarius Strobl #define	kproc_exit	kthread_exit
887aa65846SMarius Strobl #endif
897aa65846SMarius Strobl 
9072dec079SMarius Strobl #define	MMCSD_CMD_RETRIES	5
9172dec079SMarius Strobl 
9272dec079SMarius Strobl #define	MMCSD_FMT_BOOT		"mmcsd%dboot"
9372dec079SMarius Strobl #define	MMCSD_FMT_GP		"mmcsd%dgp"
9472dec079SMarius Strobl #define	MMCSD_FMT_RPMB		"mmcsd%drpmb"
9572dec079SMarius Strobl #define	MMCSD_LABEL_ENH		"enh"
9672dec079SMarius Strobl 
9772dec079SMarius Strobl #define	MMCSD_PART_NAMELEN	(16 + 1)
9872dec079SMarius Strobl 
9972dec079SMarius Strobl struct mmcsd_softc;
10072dec079SMarius Strobl 
10172dec079SMarius Strobl struct mmcsd_part {
10272dec079SMarius Strobl 	struct mtx part_mtx;
10372dec079SMarius Strobl 	struct mmcsd_softc *sc;
104114b4164SWarner Losh 	struct disk *disk;
105114b4164SWarner Losh 	struct proc *p;
106114b4164SWarner Losh 	struct bio_queue_head bio_queue;
107a7cf6274SAlexander Motin 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
10872dec079SMarius Strobl 	u_int cnt;
10972dec079SMarius Strobl 	u_int type;
1100e1466acSWarner Losh 	int running;
111eb67f31aSAlexander Motin 	int suspend;
11272dec079SMarius Strobl 	bool ro;
11372dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
11472dec079SMarius Strobl };
11572dec079SMarius Strobl 
11672dec079SMarius Strobl struct mmcsd_softc {
11772dec079SMarius Strobl 	device_t dev;
11872dec079SMarius Strobl 	device_t mmcbr;
11972dec079SMarius Strobl 	struct mmcsd_part *part[MMC_PART_MAX];
12072dec079SMarius Strobl 	enum mmc_card_mode mode;
12172dec079SMarius Strobl 	uint8_t part_curr;	/* Partition currently switched to */
12272dec079SMarius Strobl 	uint8_t ext_csd[MMC_EXTCSD_SIZE];
12372dec079SMarius Strobl 	uint16_t rca;
12472dec079SMarius Strobl 	uint32_t part_time;	/* Partition switch timeout [us] */
12572dec079SMarius Strobl 	off_t enh_base;		/* Enhanced user data area slice base ... */
12672dec079SMarius Strobl 	off_t enh_size;		/* ... and size [bytes] */
1275e3dbf8bSIan Lepore 	int log_count;
1285e3dbf8bSIan Lepore 	struct timeval log_time;
12972dec079SMarius Strobl 	struct cdev *rpmb_dev;
130114b4164SWarner Losh };
131114b4164SWarner Losh 
132bb1ef63fSWarner Losh static const char *errmsg[] =
133bb1ef63fSWarner Losh {
134bb1ef63fSWarner Losh 	"None",
135bb1ef63fSWarner Losh 	"Timeout",
136bb1ef63fSWarner Losh 	"Bad CRC",
137bb1ef63fSWarner Losh 	"Fifo",
138bb1ef63fSWarner Losh 	"Failed",
139bb1ef63fSWarner Losh 	"Invalid",
140bb1ef63fSWarner Losh 	"NO MEMORY"
141bb1ef63fSWarner Losh };
142bb1ef63fSWarner Losh 
1435e3dbf8bSIan Lepore #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
1445e3dbf8bSIan Lepore 
145114b4164SWarner Losh /* bus entry points */
146114b4164SWarner Losh static int mmcsd_attach(device_t dev);
147114b4164SWarner Losh static int mmcsd_detach(device_t dev);
148ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev);
149114b4164SWarner Losh 
150114b4164SWarner Losh /* disk routines */
151114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
152a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
153a1fda318SAlexander Motin     off_t offset, size_t length);
15472dec079SMarius Strobl static int mmcsd_getattr(struct bio *);
15572dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
15672dec079SMarius Strobl     int fflag, struct thread *td);
157ae5d8757SMarius Strobl static int mmcsd_open(struct disk *dp);
158ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp);
159114b4164SWarner Losh static void mmcsd_task(void *arg);
160114b4164SWarner Losh 
16172dec079SMarius Strobl /* RMPB cdev interface */
16272dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
16372dec079SMarius Strobl     int fflag, struct thread *td);
164a0075e58SWarner Losh 
16572dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
16672dec079SMarius Strobl     const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
16772dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev);
16872dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
16972dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
17072dec079SMarius Strobl     int fflag);
17172dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
17272dec079SMarius Strobl     int fflag);
17372dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
17472dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
17572dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
17672dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider,
17772dec079SMarius Strobl     struct flash_slice *slices, int *nslices);
17872dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
17972dec079SMarius Strobl     u_int part);
18072dec079SMarius Strobl 
18172dec079SMarius Strobl #define	MMCSD_PART_LOCK(_part)		mtx_lock(&(_part)->part_mtx)
18272dec079SMarius Strobl #define	MMCSD_PART_UNLOCK(_part)	mtx_unlock(&(_part)->part_mtx)
18372dec079SMarius Strobl #define	MMCSD_PART_LOCK_INIT(_part)					\
18472dec079SMarius Strobl 	mtx_init(&(_part)->part_mtx, (_part)->name, "mmcsd part", MTX_DEF)
18572dec079SMarius Strobl #define	MMCSD_PART_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->part_mtx);
18672dec079SMarius Strobl #define	MMCSD_PART_ASSERT_LOCKED(_part)					\
18772dec079SMarius Strobl 	mtx_assert(&(_part)->part_mtx, MA_OWNED);
18872dec079SMarius Strobl #define	MMCSD_PART_ASSERT_UNLOCKED(_part)				\
18972dec079SMarius Strobl 	mtx_assert(&(_part)->part_mtx, MA_NOTOWNED);
190114b4164SWarner Losh 
191114b4164SWarner Losh static int
192114b4164SWarner Losh mmcsd_probe(device_t dev)
193114b4164SWarner Losh {
194114b4164SWarner Losh 
195c18f1e26SAlexander Motin 	device_quiet(dev);
1960c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
197114b4164SWarner Losh 	return (0);
198114b4164SWarner Losh }
199114b4164SWarner Losh 
200114b4164SWarner Losh static int
201114b4164SWarner Losh mmcsd_attach(device_t dev)
202114b4164SWarner Losh {
20372dec079SMarius Strobl 	device_t mmcbr;
204114b4164SWarner Losh 	struct mmcsd_softc *sc;
20572dec079SMarius Strobl 	const uint8_t *ext_csd;
20672dec079SMarius Strobl 	off_t erase_size, sector_size, size, wp_size;
20772dec079SMarius Strobl 	uintmax_t bytes;
20872dec079SMarius Strobl 	int err, i;
20972dec079SMarius Strobl 	uint8_t rev;
21072dec079SMarius Strobl 	bool comp, ro;
21172dec079SMarius Strobl 	char unit[2];
212114b4164SWarner Losh 
213114b4164SWarner Losh 	sc = device_get_softc(dev);
214114b4164SWarner Losh 	sc->dev = dev;
21572dec079SMarius Strobl 	sc->mmcbr = mmcbr = device_get_parent(dev);
21672dec079SMarius Strobl 	sc->mode = mmcbr_get_mode(mmcbr);
21772dec079SMarius Strobl 	sc->rca = mmc_get_rca(dev);
218114b4164SWarner Losh 
21972dec079SMarius Strobl 	/* Only MMC >= 4.x devices support EXT_CSD. */
22072dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) >= 4) {
22172dec079SMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
22272dec079SMarius Strobl 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
22372dec079SMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbr, dev);
22472dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
22572dec079SMarius Strobl 			bzero(sc->ext_csd, sizeof(sc->ext_csd));
22672dec079SMarius Strobl 	}
22772dec079SMarius Strobl 	ext_csd = sc->ext_csd;
22872dec079SMarius Strobl 
22972dec079SMarius Strobl 	/*
23072dec079SMarius Strobl 	 * Enhanced user data area and general purpose partitions are only
23172dec079SMarius Strobl 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
23272dec079SMarius Strobl 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
23372dec079SMarius Strobl 	 */
23472dec079SMarius Strobl 	rev = ext_csd[EXT_CSD_REV];
23572dec079SMarius Strobl 
23672dec079SMarius Strobl 	/*
23772dec079SMarius Strobl 	 * Ignore user-creatable enhanced user data area and general purpose
23872dec079SMarius Strobl 	 * partitions partitions as long as partitioning hasn't been finished.
23972dec079SMarius Strobl 	 */
24072dec079SMarius Strobl 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
24172dec079SMarius Strobl 
24272dec079SMarius Strobl 	/*
24372dec079SMarius Strobl 	 * Add enhanced user data area slice, unless it spans the entirety of
24472dec079SMarius Strobl 	 * the user data area.  The enhanced area is of a multiple of high
24572dec079SMarius Strobl 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
24672dec079SMarius Strobl 	 * 512 KB) and its offset given in either sectors or bytes, depending
24772dec079SMarius Strobl 	 * on whether it's a high capacity device or not.
24872dec079SMarius Strobl 	 * NB: The slicer and its slices need to be registered before adding
24972dec079SMarius Strobl 	 *     the disk for the corresponding user data area as re-tasting is
25072dec079SMarius Strobl 	 *     racy.
25172dec079SMarius Strobl 	 */
25272dec079SMarius Strobl 	sector_size = mmc_get_sector_size(dev);
25372dec079SMarius Strobl 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
25472dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
25572dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
25672dec079SMarius Strobl 	if (rev >= 4 && comp == TRUE && size > 0 &&
25772dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
25872dec079SMarius Strobl 	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
25972dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
26072dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
26172dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
26272dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
26372dec079SMarius Strobl 		size *= erase_size * wp_size;
26472dec079SMarius Strobl 		if (size != mmc_get_media_size(dev) * sector_size) {
26572dec079SMarius Strobl 			sc->enh_size = size;
26672dec079SMarius Strobl 			sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
26772dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
26872dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
26972dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
27072dec079SMarius Strobl 			    (mmc_get_high_cap(dev) ? MMC_SECTOR_SIZE : 1);
27172dec079SMarius Strobl 		} else if (bootverbose)
27272dec079SMarius Strobl 			device_printf(dev,
27372dec079SMarius Strobl 			    "enhanced user data area spans entire device\n");
27472dec079SMarius Strobl 	}
27572dec079SMarius Strobl 
27672dec079SMarius Strobl 	/*
27772dec079SMarius Strobl 	 * Add default partition.  This may be the only one or the user
27872dec079SMarius Strobl 	 * data area in case partitions are supported.
27972dec079SMarius Strobl 	 */
28072dec079SMarius Strobl 	ro = mmc_get_read_only(dev);
28172dec079SMarius Strobl 	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
28272dec079SMarius Strobl 	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
28372dec079SMarius Strobl 	    mmc_get_erase_sector(dev) * sector_size, ro);
28472dec079SMarius Strobl 
28572dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) < 3)
28672dec079SMarius Strobl 		return (0);
28772dec079SMarius Strobl 
28872dec079SMarius Strobl 	/* Belatedly announce enhanced user data slice. */
28972dec079SMarius Strobl 	if (sc->enh_size != 0) {
29072dec079SMarius Strobl 		bytes = mmcsd_pretty_size(size, unit);
29172dec079SMarius Strobl 		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
29272dec079SMarius Strobl 		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
29372dec079SMarius Strobl 		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
29472dec079SMarius Strobl 		    device_get_nameunit(dev));
29572dec079SMarius Strobl 	}
29672dec079SMarius Strobl 
29772dec079SMarius Strobl 	/*
29872dec079SMarius Strobl 	 * Determine partition switch timeout (provided in units of 10 ms)
29972dec079SMarius Strobl 	 * and ensure it's at least 300 ms as some eMMC chips lie.
30072dec079SMarius Strobl 	 */
30172dec079SMarius Strobl 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
30272dec079SMarius Strobl 	    300 * 1000);
30372dec079SMarius Strobl 
30472dec079SMarius Strobl 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
30572dec079SMarius Strobl 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
30672dec079SMarius Strobl 	if (size > 0 && (mmcbr_get_caps(mmcbr) & MMC_CAP_BOOT_NOACC) == 0) {
30772dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
30872dec079SMarius Strobl 		    MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
30972dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
31072dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
31172dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
31272dec079SMarius Strobl 		    MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
31372dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
31472dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
31572dec079SMarius Strobl 	}
31672dec079SMarius Strobl 
31772dec079SMarius Strobl 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
31872dec079SMarius Strobl 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
31972dec079SMarius Strobl 	if (rev >= 5 && size > 0)
32072dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
32172dec079SMarius Strobl 		    MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
32272dec079SMarius Strobl 
32372dec079SMarius Strobl 	if (rev <= 3 || comp == FALSE)
32472dec079SMarius Strobl 		return (0);
32572dec079SMarius Strobl 
32672dec079SMarius Strobl 	/*
32772dec079SMarius Strobl 	 * Add general purpose partitions, which are of a multiple of high
32872dec079SMarius Strobl 	 * capacity write protect groups, too.
32972dec079SMarius Strobl 	 */
33072dec079SMarius Strobl 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
33172dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
33272dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
33372dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
33472dec079SMarius Strobl 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
33572dec079SMarius Strobl 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
33672dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
33772dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
33872dec079SMarius Strobl 			if (size == 0)
33972dec079SMarius Strobl 				continue;
34072dec079SMarius Strobl 			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
34172dec079SMarius Strobl 			    MMCSD_FMT_GP, i, size * erase_size * wp_size,
34272dec079SMarius Strobl 			    erase_size, ro);
34372dec079SMarius Strobl 		}
34472dec079SMarius Strobl 	}
34572dec079SMarius Strobl 	return (0);
34672dec079SMarius Strobl }
34772dec079SMarius Strobl 
34872dec079SMarius Strobl static uintmax_t
34972dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit)
35072dec079SMarius Strobl {
35172dec079SMarius Strobl 	uintmax_t bytes;
35272dec079SMarius Strobl 	int i;
35372dec079SMarius Strobl 
35472dec079SMarius Strobl 	/*
35572dec079SMarius Strobl 	 * Display in most natural units.  There's no card < 1MB.  However,
35672dec079SMarius Strobl 	 * RPMB partitions occasionally are smaller than that, though.  The
35772dec079SMarius Strobl 	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
35872dec079SMarius Strobl 	 * format supports up to 4 GiB and some card makers push it up to this
35972dec079SMarius Strobl 	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
36072dec079SMarius Strobl 	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
36172dec079SMarius Strobl 	 * so we note it in passing here and don't add the code to print TB).
36272dec079SMarius Strobl 	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
36372dec079SMarius Strobl 	 * report them like that.  We also round to the nearest unit, since
36472dec079SMarius Strobl 	 * many cards are a few percent short, even of the power of 10 size.
36572dec079SMarius Strobl 	 */
36672dec079SMarius Strobl 	bytes = size;
36772dec079SMarius Strobl 	unit[0] = unit[1] = '\0';
36872dec079SMarius Strobl 	for (i = 0; i <= 2 && bytes >= 1000; i++) {
36972dec079SMarius Strobl 		bytes = (bytes + 1000 / 2 - 1) / 1000;
37072dec079SMarius Strobl 		switch (i) {
37172dec079SMarius Strobl 		case 0:
37272dec079SMarius Strobl 			unit[0] = 'k';
37372dec079SMarius Strobl 			break;
37472dec079SMarius Strobl 		case 1:
37572dec079SMarius Strobl 			unit[0] = 'M';
37672dec079SMarius Strobl 			break;
37772dec079SMarius Strobl 		case 2:
37872dec079SMarius Strobl 			unit[0] = 'G';
37972dec079SMarius Strobl 			break;
38072dec079SMarius Strobl 		default:
38172dec079SMarius Strobl 			break;
38272dec079SMarius Strobl 		}
38372dec079SMarius Strobl 	}
38472dec079SMarius Strobl 	return (bytes);
38572dec079SMarius Strobl }
38672dec079SMarius Strobl 
38772dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = {
38872dec079SMarius Strobl 	.d_version	= D_VERSION,
38972dec079SMarius Strobl 	.d_name		= "mmcsdrpmb",
39072dec079SMarius Strobl 	.d_ioctl	= mmcsd_ioctl_rpmb
39172dec079SMarius Strobl };
39272dec079SMarius Strobl 
39372dec079SMarius Strobl static void
39472dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
39572dec079SMarius Strobl     off_t media_size, off_t erase_size, bool ro)
39672dec079SMarius Strobl {
39772dec079SMarius Strobl 	struct make_dev_args args;
39872dec079SMarius Strobl 	device_t dev, mmcbr;
39972dec079SMarius Strobl 	const char *ext;
40072dec079SMarius Strobl 	const uint8_t *ext_csd;
40172dec079SMarius Strobl 	struct mmcsd_part *part;
40272dec079SMarius Strobl 	struct disk *d;
40372dec079SMarius Strobl 	uintmax_t bytes;
40472dec079SMarius Strobl 	u_int gp;
40572dec079SMarius Strobl 	uint32_t speed;
40672dec079SMarius Strobl 	uint8_t extattr;
40772dec079SMarius Strobl 	bool enh;
40872dec079SMarius Strobl 	char unit[2];
40972dec079SMarius Strobl 
41072dec079SMarius Strobl 	dev = sc->dev;
41172dec079SMarius Strobl 	mmcbr = sc->mmcbr;
41272dec079SMarius Strobl 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
41372dec079SMarius Strobl 	    M_WAITOK | M_ZERO);
41472dec079SMarius Strobl 	part->sc = sc;
41572dec079SMarius Strobl 	part->cnt = cnt;
41672dec079SMarius Strobl 	part->type = type;
41772dec079SMarius Strobl 	part->ro = ro;
41872dec079SMarius Strobl 	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
41972dec079SMarius Strobl 
42072dec079SMarius Strobl 	/* For the RPMB partition, allow IOCTL access only. */
42172dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
42272dec079SMarius Strobl 		make_dev_args_init(&args);
42372dec079SMarius Strobl 		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
42472dec079SMarius Strobl 		args.mda_devsw = &mmcsd_rpmb_cdevsw;
42572dec079SMarius Strobl 		args.mda_uid = UID_ROOT;
42672dec079SMarius Strobl 		args.mda_gid = GID_OPERATOR;
42772dec079SMarius Strobl 		args.mda_mode = 0640;
42872dec079SMarius Strobl 		args.mda_si_drv1 = part;
42972dec079SMarius Strobl 		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
43072dec079SMarius Strobl 			device_printf(dev, "Failed to make RPMB device\n");
43172dec079SMarius Strobl 			free(part, M_DEVBUF);
43272dec079SMarius Strobl 			return;
43372dec079SMarius Strobl 		}
43472dec079SMarius Strobl 	} else {
43572dec079SMarius Strobl 		MMCSD_PART_LOCK_INIT(part);
43672dec079SMarius Strobl 
43772dec079SMarius Strobl 		d = part->disk = disk_alloc();
438a0075e58SWarner Losh 		d->d_open = mmcsd_open;
439a0075e58SWarner Losh 		d->d_close = mmcsd_close;
440a0075e58SWarner Losh 		d->d_strategy = mmcsd_strategy;
44172dec079SMarius Strobl 		d->d_ioctl = mmcsd_ioctl_disk;
442a1fda318SAlexander Motin 		d->d_dump = mmcsd_dump;
44372dec079SMarius Strobl 		d->d_getattr = mmcsd_getattr;
44472dec079SMarius Strobl 		d->d_name = part->name;
44572dec079SMarius Strobl 		d->d_drv1 = part;
446a0075e58SWarner Losh 		d->d_sectorsize = mmc_get_sector_size(dev);
447a5928b20SWarner Losh 		d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
44872dec079SMarius Strobl 		d->d_mediasize = media_size;
44972dec079SMarius Strobl 		d->d_stripesize = erase_size;
45072dec079SMarius Strobl 		d->d_unit = cnt;
4513906d42dSAlexander Motin 		d->d_flags = DISKFLAG_CANDELETE;
45272dec079SMarius Strobl 		d->d_delmaxsize = erase_size;
45372dec079SMarius Strobl 		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
45472dec079SMarius Strobl 		    sizeof(d->d_ident));
45572dec079SMarius Strobl 		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
45672dec079SMarius Strobl 		    sizeof(d->d_descr));
45717160457SAlexander Motin 		d->d_rotation_rate = DISK_RR_NON_ROTATING;
458f3a4b7f7SIan Lepore 
459a0075e58SWarner Losh 		disk_create(d, DISK_VERSION);
46072dec079SMarius Strobl 		bioq_init(&part->bio_queue);
4610e1466acSWarner Losh 
46272dec079SMarius Strobl 		part->running = 1;
46372dec079SMarius Strobl 		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
46472dec079SMarius Strobl 		    "%s%d: mmc/sd card", part->name, cnt);
46572dec079SMarius Strobl 	}
46672dec079SMarius Strobl 
46772dec079SMarius Strobl 	bytes = mmcsd_pretty_size(media_size, unit);
46872dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
46972dec079SMarius Strobl 		speed = mmcbr_get_clock(mmcbr);
47072dec079SMarius Strobl 		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
47172dec079SMarius Strobl 		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
47272dec079SMarius Strobl 		    ro ? " (read-only)" : "", device_get_nameunit(mmcbr),
47372dec079SMarius Strobl 		    speed / 1000000, (speed / 100000) % 10,
47472dec079SMarius Strobl 		    mmcsd_bus_bit_width(dev), mmc_get_max_data(dev));
47572dec079SMarius Strobl 	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
47672dec079SMarius Strobl 		printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
47772dec079SMarius Strobl 		    unit, type, ro ? " (read-only)" : "",
47888e07d92SRui Paulo 		    device_get_nameunit(dev));
47972dec079SMarius Strobl 	} else {
48072dec079SMarius Strobl 		enh = false;
48172dec079SMarius Strobl 		ext = NULL;
48272dec079SMarius Strobl 		extattr = 0;
48372dec079SMarius Strobl 		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
48472dec079SMarius Strobl 		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
48572dec079SMarius Strobl 			ext_csd = sc->ext_csd;
48672dec079SMarius Strobl 			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
48772dec079SMarius Strobl 			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
48872dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
48972dec079SMarius Strobl 			    (ext_csd[EXT_CSD_PART_ATTR] &
49072dec079SMarius Strobl 			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
49172dec079SMarius Strobl 				enh = true;
49272dec079SMarius Strobl 			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
49372dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
49472dec079SMarius Strobl 				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
49572dec079SMarius Strobl 				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
49672dec079SMarius Strobl 				switch (extattr) {
49772dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
49872dec079SMarius Strobl 						break;
49972dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
50072dec079SMarius Strobl 						ext = "system code";
50172dec079SMarius Strobl 						break;
50272dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
50372dec079SMarius Strobl 						ext = "non-persistent";
50472dec079SMarius Strobl 						break;
50572dec079SMarius Strobl 					default:
50672dec079SMarius Strobl 						ext = "reserved";
50772dec079SMarius Strobl 						break;
50872dec079SMarius Strobl 				}
50972dec079SMarius Strobl 			}
51072dec079SMarius Strobl 		}
51172dec079SMarius Strobl 		if (ext == NULL)
51272dec079SMarius Strobl 			printf("%s%d: %ju%sB partion %d%s%s at %s\n",
51372dec079SMarius Strobl 			    part->name, cnt, bytes, unit, type, enh ?
51472dec079SMarius Strobl 			    " enhanced" : "", ro ? " (read-only)" : "",
51572dec079SMarius Strobl 			    device_get_nameunit(dev));
51672dec079SMarius Strobl 		else
51772dec079SMarius Strobl 			printf("%s%d: %ju%sB partion %d extended 0x%x "
51872dec079SMarius Strobl 			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
51972dec079SMarius Strobl 			    type, extattr, ext, ro ? " (read-only)" : "",
52072dec079SMarius Strobl 			    device_get_nameunit(dev));
52172dec079SMarius Strobl 	}
52272dec079SMarius Strobl }
523114b4164SWarner Losh 
52472dec079SMarius Strobl static int
52572dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider,
52672dec079SMarius Strobl     struct flash_slice *slices, int *nslices)
52772dec079SMarius Strobl {
52872dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
52972dec079SMarius Strobl 	struct mmcsd_softc *sc;
53072dec079SMarius Strobl 	struct mmcsd_part *part;
53172dec079SMarius Strobl 
53272dec079SMarius Strobl 	*nslices = 0;
53372dec079SMarius Strobl 	if (slices == NULL)
53472dec079SMarius Strobl 		return (ENOMEM);
53572dec079SMarius Strobl 
53672dec079SMarius Strobl 	sc = device_get_softc(dev);
53772dec079SMarius Strobl 	if (sc->enh_size == 0)
53872dec079SMarius Strobl 		return (ENXIO);
53972dec079SMarius Strobl 
54072dec079SMarius Strobl 	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
54172dec079SMarius Strobl 	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
54272dec079SMarius Strobl 	    part->disk->d_unit);
54372dec079SMarius Strobl 	if (strcmp(name, provider) != 0)
54472dec079SMarius Strobl 		return (ENXIO);
54572dec079SMarius Strobl 
54672dec079SMarius Strobl 	*nslices = 1;
54772dec079SMarius Strobl 	slices[0].base = sc->enh_base;
54872dec079SMarius Strobl 	slices[0].size = sc->enh_size;
54972dec079SMarius Strobl 	slices[0].label = MMCSD_LABEL_ENH;
550114b4164SWarner Losh 	return (0);
551114b4164SWarner Losh }
552114b4164SWarner Losh 
553114b4164SWarner Losh static int
554114b4164SWarner Losh mmcsd_detach(device_t dev)
555114b4164SWarner Losh {
5560e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
55772dec079SMarius Strobl 	struct mmcsd_part *part;
55872dec079SMarius Strobl 	int i;
5590e1466acSWarner Losh 
56072dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
56172dec079SMarius Strobl 		part = sc->part[i];
56272dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
56372dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
56472dec079SMarius Strobl 			part->suspend = 0;
56572dec079SMarius Strobl 			if (part->running > 0) {
566eb67f31aSAlexander Motin 				/* kill thread */
56772dec079SMarius Strobl 				part->running = 0;
56872dec079SMarius Strobl 				wakeup(part);
569eb67f31aSAlexander Motin 				/* wait for thread to finish. */
57072dec079SMarius Strobl 				while (part->running != -1)
57172dec079SMarius Strobl 					msleep(part, &part->part_mtx, 0,
57272dec079SMarius Strobl 					    "detach", 0);
573eb67f31aSAlexander Motin 			}
57472dec079SMarius Strobl 			MMCSD_PART_UNLOCK(part);
57572dec079SMarius Strobl 		}
57672dec079SMarius Strobl 	}
5770e1466acSWarner Losh 
57872dec079SMarius Strobl 	if (sc->rpmb_dev != NULL)
57972dec079SMarius Strobl 		destroy_dev(sc->rpmb_dev);
58072dec079SMarius Strobl 
58172dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
58272dec079SMarius Strobl 		part = sc->part[i];
58372dec079SMarius Strobl 		if (part != NULL) {
58472dec079SMarius Strobl 			if (part->disk != NULL) {
585249b0e85SAlexander Motin 				/* Flush the request queue. */
58672dec079SMarius Strobl 				bioq_flush(&part->bio_queue, NULL, ENXIO);
5870e1466acSWarner Losh 				/* kill disk */
58872dec079SMarius Strobl 				disk_destroy(part->disk);
5890e1466acSWarner Losh 
59072dec079SMarius Strobl 				MMCSD_PART_LOCK_DESTROY(part);
59172dec079SMarius Strobl 			}
59272dec079SMarius Strobl 			free(part, M_DEVBUF);
59372dec079SMarius Strobl 		}
59472dec079SMarius Strobl 	}
595128d0ff2SWarner Losh 	return (0);
596114b4164SWarner Losh }
597114b4164SWarner Losh 
598114b4164SWarner Losh static int
599eb67f31aSAlexander Motin mmcsd_suspend(device_t dev)
600eb67f31aSAlexander Motin {
601eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
60272dec079SMarius Strobl 	struct mmcsd_part *part;
60372dec079SMarius Strobl 	int i;
604eb67f31aSAlexander Motin 
60572dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
60672dec079SMarius Strobl 		part = sc->part[i];
60772dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
60872dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
60972dec079SMarius Strobl 			part->suspend = 1;
61072dec079SMarius Strobl 			if (part->running > 0) {
611eb67f31aSAlexander Motin 				/* kill thread */
61272dec079SMarius Strobl 				part->running = 0;
61372dec079SMarius Strobl 				wakeup(part);
614eb67f31aSAlexander Motin 				/* wait for thread to finish. */
61572dec079SMarius Strobl 				while (part->running != -1)
61672dec079SMarius Strobl 					msleep(part, &part->part_mtx, 0,
61772dec079SMarius Strobl 					    "detach", 0);
618eb67f31aSAlexander Motin 			}
61972dec079SMarius Strobl 			MMCSD_PART_UNLOCK(part);
62072dec079SMarius Strobl 		}
62172dec079SMarius Strobl 	}
622eb67f31aSAlexander Motin 	return (0);
623eb67f31aSAlexander Motin }
624eb67f31aSAlexander Motin 
625eb67f31aSAlexander Motin static int
626eb67f31aSAlexander Motin mmcsd_resume(device_t dev)
627eb67f31aSAlexander Motin {
628eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
62972dec079SMarius Strobl 	struct mmcsd_part *part;
63072dec079SMarius Strobl 	int i;
631eb67f31aSAlexander Motin 
63272dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
63372dec079SMarius Strobl 		part = sc->part[i];
63472dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
63572dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
63672dec079SMarius Strobl 			part->suspend = 0;
63772dec079SMarius Strobl 			if (part->running <= 0) {
63872dec079SMarius Strobl 				part->running = 1;
63972dec079SMarius Strobl 				kproc_create(&mmcsd_task, part, &part->p, 0, 0,
64072dec079SMarius Strobl 				    "%s%d: mmc/sd card", part->name, part->cnt);
64172dec079SMarius Strobl 				MMCSD_PART_UNLOCK(part);
642eb67f31aSAlexander Motin 			} else
64372dec079SMarius Strobl 				MMCSD_PART_UNLOCK(part);
64472dec079SMarius Strobl 		}
64572dec079SMarius Strobl 	}
646eb67f31aSAlexander Motin 	return (0);
647eb67f31aSAlexander Motin }
648eb67f31aSAlexander Motin 
649eb67f31aSAlexander Motin static int
650b440e965SMarius Strobl mmcsd_open(struct disk *dp __unused)
651114b4164SWarner Losh {
652ae5d8757SMarius Strobl 
653128d0ff2SWarner Losh 	return (0);
654114b4164SWarner Losh }
655114b4164SWarner Losh 
656114b4164SWarner Losh static int
657b440e965SMarius Strobl mmcsd_close(struct disk *dp __unused)
658114b4164SWarner Losh {
659ae5d8757SMarius Strobl 
660128d0ff2SWarner Losh 	return (0);
661114b4164SWarner Losh }
662114b4164SWarner Losh 
663114b4164SWarner Losh static void
664114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
665114b4164SWarner Losh {
666114b4164SWarner Losh 	struct mmcsd_softc *sc;
66772dec079SMarius Strobl 	struct mmcsd_part *part;
668114b4164SWarner Losh 
66972dec079SMarius Strobl 	part = bp->bio_disk->d_drv1;
67072dec079SMarius Strobl 	sc = part->sc;
67172dec079SMarius Strobl 	MMCSD_PART_LOCK(part);
67272dec079SMarius Strobl 	if (part->running > 0 || part->suspend > 0) {
67372dec079SMarius Strobl 		bioq_disksort(&part->bio_queue, bp);
67472dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
67572dec079SMarius Strobl 		wakeup(part);
676249b0e85SAlexander Motin 	} else {
67772dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
678249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
679249b0e85SAlexander Motin 	}
680114b4164SWarner Losh }
681114b4164SWarner Losh 
68272dec079SMarius Strobl static int
68372dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
68472dec079SMarius Strobl     int fflag, struct thread *td __unused)
68572dec079SMarius Strobl {
68672dec079SMarius Strobl 
68772dec079SMarius Strobl 	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
68872dec079SMarius Strobl }
68972dec079SMarius Strobl 
69072dec079SMarius Strobl static int
69172dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
69272dec079SMarius Strobl     struct thread *td __unused)
69372dec079SMarius Strobl {
69472dec079SMarius Strobl 
69572dec079SMarius Strobl 	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
69672dec079SMarius Strobl }
69772dec079SMarius Strobl 
69872dec079SMarius Strobl static int
69972dec079SMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
70072dec079SMarius Strobl {
70172dec079SMarius Strobl 	struct mmc_ioc_cmd *mic;
70272dec079SMarius Strobl 	struct mmc_ioc_multi_cmd *mimc;
70372dec079SMarius Strobl 	int i, err;
70472dec079SMarius Strobl 	u_long cnt, size;
70572dec079SMarius Strobl 
70672dec079SMarius Strobl 	if ((fflag & FREAD) == 0)
70772dec079SMarius Strobl 		return (EBADF);
70872dec079SMarius Strobl 
70972dec079SMarius Strobl 	err = 0;
71072dec079SMarius Strobl 	switch (cmd) {
71172dec079SMarius Strobl 	case MMC_IOC_CMD:
71272dec079SMarius Strobl 		mic = data;
71372dec079SMarius Strobl 		err = mmcsd_ioctl_cmd(part, data, fflag);
71472dec079SMarius Strobl 		break;
71572dec079SMarius Strobl 	case MMC_IOC_CMD_MULTI:
71672dec079SMarius Strobl 		mimc = data;
71772dec079SMarius Strobl 		if (mimc->num_of_cmds == 0)
71872dec079SMarius Strobl 			break;
71972dec079SMarius Strobl 		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
72072dec079SMarius Strobl 			return (EINVAL);
72172dec079SMarius Strobl 		cnt = mimc->num_of_cmds;
72272dec079SMarius Strobl 		size = sizeof(*mic) * cnt;
72372dec079SMarius Strobl 		mic = malloc(size, M_TEMP, M_WAITOK);
72472dec079SMarius Strobl 		err = copyin((const void *)mimc->cmds, mic, size);
72572dec079SMarius Strobl 		if (err != 0)
72672dec079SMarius Strobl 			break;
72772dec079SMarius Strobl 		for (i = 0; i < cnt; i++) {
72872dec079SMarius Strobl 			err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
72972dec079SMarius Strobl 			if (err != 0)
73072dec079SMarius Strobl 				break;
73172dec079SMarius Strobl 		}
73272dec079SMarius Strobl 		free(mic, M_TEMP);
73372dec079SMarius Strobl 		break;
73472dec079SMarius Strobl 	default:
73572dec079SMarius Strobl 		return (ENOIOCTL);
73672dec079SMarius Strobl 	}
73772dec079SMarius Strobl 	return (err);
73872dec079SMarius Strobl }
73972dec079SMarius Strobl 
74072dec079SMarius Strobl static int
74172dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
74272dec079SMarius Strobl {
74372dec079SMarius Strobl 	struct mmc_command cmd;
74472dec079SMarius Strobl 	struct mmc_data data;
74572dec079SMarius Strobl 	struct mmcsd_softc *sc;
74672dec079SMarius Strobl 	device_t dev, mmcbr;
74772dec079SMarius Strobl 	void *dp;
74872dec079SMarius Strobl 	u_long len;
74972dec079SMarius Strobl 	int err, retries;
75072dec079SMarius Strobl 	uint32_t status;
75172dec079SMarius Strobl 	uint16_t rca;
75272dec079SMarius Strobl 
75372dec079SMarius Strobl 	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
75472dec079SMarius Strobl 		return (EBADF);
75572dec079SMarius Strobl 
75672dec079SMarius Strobl 	if (part->ro == TRUE && mic->write_flag != 0)
75772dec079SMarius Strobl 		return (EROFS);
75872dec079SMarius Strobl 
75972dec079SMarius Strobl 	err = 0;
76072dec079SMarius Strobl 	dp = NULL;
76172dec079SMarius Strobl 	len = mic->blksz * mic->blocks;
76272dec079SMarius Strobl 	if (len > MMC_IOC_MAX_BYTES)
76372dec079SMarius Strobl 		return (EOVERFLOW);
76472dec079SMarius Strobl 	if (len != 0) {
76572dec079SMarius Strobl 		dp = malloc(len, M_TEMP, M_WAITOK);
76672dec079SMarius Strobl 		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
76772dec079SMarius Strobl 		if (err != 0)
76872dec079SMarius Strobl 			goto out;
76972dec079SMarius Strobl 	}
77072dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
77172dec079SMarius Strobl 	memset(&data, 0, sizeof(data));
77272dec079SMarius Strobl 	cmd.opcode = mic->opcode;
77372dec079SMarius Strobl 	cmd.arg = mic->arg;
77472dec079SMarius Strobl 	cmd.flags = mic->flags;
77572dec079SMarius Strobl 	if (len != 0) {
77672dec079SMarius Strobl 		data.len = len;
77772dec079SMarius Strobl 		data.data = dp;
77872dec079SMarius Strobl 		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
77972dec079SMarius Strobl 		    MMC_DATA_READ;
78072dec079SMarius Strobl 		cmd.data = &data;
78172dec079SMarius Strobl 	}
78272dec079SMarius Strobl 	sc = part->sc;
78372dec079SMarius Strobl 	rca = sc->rca;
78472dec079SMarius Strobl 	if (mic->is_acmd == 0) {
78572dec079SMarius Strobl 		/* Enforce/patch/restrict RCA-based commands */
78672dec079SMarius Strobl 		switch (cmd.opcode) {
78772dec079SMarius Strobl 		case MMC_SET_RELATIVE_ADDR:
78872dec079SMarius Strobl 		case MMC_SELECT_CARD:
78972dec079SMarius Strobl 			err = EPERM;
79072dec079SMarius Strobl 			goto out;
79172dec079SMarius Strobl 		case MMC_STOP_TRANSMISSION:
79272dec079SMarius Strobl 			if ((cmd.arg & 0x1) == 0)
79372dec079SMarius Strobl 				break;
79472dec079SMarius Strobl 			/* FALLTHROUGH */
79572dec079SMarius Strobl 		case MMC_SLEEP_AWAKE:
79672dec079SMarius Strobl 		case MMC_SEND_CSD:
79772dec079SMarius Strobl 		case MMC_SEND_CID:
79872dec079SMarius Strobl 		case MMC_SEND_STATUS:
79972dec079SMarius Strobl 		case MMC_GO_INACTIVE_STATE:
80072dec079SMarius Strobl 		case MMC_FAST_IO:
80172dec079SMarius Strobl 		case MMC_APP_CMD:
80272dec079SMarius Strobl 			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
80372dec079SMarius Strobl 			break;
80472dec079SMarius Strobl 		default:
80572dec079SMarius Strobl 			break;
80672dec079SMarius Strobl 		}
80772dec079SMarius Strobl 	}
80872dec079SMarius Strobl 	dev = sc->dev;
80972dec079SMarius Strobl 	mmcbr = sc->mmcbr;
81072dec079SMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
81172dec079SMarius Strobl 	err = mmcsd_switch_part(mmcbr, dev, rca, part->type);
81272dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
81372dec079SMarius Strobl 		goto release;
81472dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
81572dec079SMarius Strobl 		err = mmcsd_set_blockcount(sc, mic->blocks,
81672dec079SMarius Strobl 		    mic->write_flag & (1 << 31));
81772dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
81872dec079SMarius Strobl 			goto release;
81972dec079SMarius Strobl 	}
82072dec079SMarius Strobl 	if (mic->is_acmd != 0)
82172dec079SMarius Strobl 		(void)mmc_wait_for_app_cmd(mmcbr, dev, rca, &cmd, 0);
82272dec079SMarius Strobl 	else
82372dec079SMarius Strobl 		(void)mmc_wait_for_cmd(mmcbr, dev, &cmd, 0);
82472dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
82572dec079SMarius Strobl 		/*
82672dec079SMarius Strobl 		 * If the request went to the RPMB partition, try to ensure
82772dec079SMarius Strobl 		 * that the command actually has completed ...
82872dec079SMarius Strobl 		 */
82972dec079SMarius Strobl 		retries = MMCSD_CMD_RETRIES;
83072dec079SMarius Strobl 		do {
83172dec079SMarius Strobl 			err = mmc_send_status(mmcbr, dev, rca, &status);
83272dec079SMarius Strobl 			if (err != MMC_ERR_NONE)
83372dec079SMarius Strobl 				break;
83472dec079SMarius Strobl 			if (R1_STATUS(status) == 0 &&
83572dec079SMarius Strobl 			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
83672dec079SMarius Strobl 				break;
83772dec079SMarius Strobl 			DELAY(1000);
83872dec079SMarius Strobl 		} while (retries-- > 0);
83972dec079SMarius Strobl 
84072dec079SMarius Strobl 		/* ... and always switch back to the default partition. */
84172dec079SMarius Strobl 		err = mmcsd_switch_part(mmcbr, dev, rca,
84272dec079SMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
84372dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
84472dec079SMarius Strobl 			goto release;
84572dec079SMarius Strobl 	}
84672dec079SMarius Strobl 	/*
84772dec079SMarius Strobl 	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
84872dec079SMarius Strobl 	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
84972dec079SMarius Strobl 	 * so retrieve EXT_CSD again.
85072dec079SMarius Strobl 	 */
85172dec079SMarius Strobl 	if (cmd.opcode == MMC_SWITCH_FUNC) {
85272dec079SMarius Strobl 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
85372dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
85472dec079SMarius Strobl 			goto release;
85572dec079SMarius Strobl 	}
85672dec079SMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbr, dev);
85772dec079SMarius Strobl 	if (cmd.error != MMC_ERR_NONE) {
85872dec079SMarius Strobl 		switch (cmd.error) {
85972dec079SMarius Strobl 		case MMC_ERR_TIMEOUT:
86072dec079SMarius Strobl 			err = ETIMEDOUT;
86172dec079SMarius Strobl 			break;
86272dec079SMarius Strobl 		case MMC_ERR_BADCRC:
86372dec079SMarius Strobl 			err = EILSEQ;
86472dec079SMarius Strobl 			break;
86572dec079SMarius Strobl 		case MMC_ERR_INVALID:
86672dec079SMarius Strobl 			err = EINVAL;
86772dec079SMarius Strobl 			break;
86872dec079SMarius Strobl 		case MMC_ERR_NO_MEMORY:
86972dec079SMarius Strobl 			err = ENOMEM;
87072dec079SMarius Strobl 			break;
87172dec079SMarius Strobl 		default:
87272dec079SMarius Strobl 			err = EIO;
87372dec079SMarius Strobl 			break;
87472dec079SMarius Strobl 		}
87572dec079SMarius Strobl 		goto out;
87672dec079SMarius Strobl 	}
87772dec079SMarius Strobl 	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
87872dec079SMarius Strobl 	if (mic->write_flag == 0 && len != 0) {
87972dec079SMarius Strobl 		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
88072dec079SMarius Strobl 		if (err != 0)
88172dec079SMarius Strobl 			goto out;
88272dec079SMarius Strobl 	}
88372dec079SMarius Strobl 	goto out;
88472dec079SMarius Strobl 
88572dec079SMarius Strobl release:
88672dec079SMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbr, dev);
88772dec079SMarius Strobl 	err = EIO;
88872dec079SMarius Strobl 
88972dec079SMarius Strobl out:
89072dec079SMarius Strobl 	if (dp != NULL)
89172dec079SMarius Strobl 		free(dp, M_TEMP);
89272dec079SMarius Strobl 	return (err);
89372dec079SMarius Strobl }
89472dec079SMarius Strobl 
89572dec079SMarius Strobl static int
89672dec079SMarius Strobl mmcsd_getattr(struct bio *bp)
89772dec079SMarius Strobl {
89872dec079SMarius Strobl 	struct mmcsd_part *part;
89972dec079SMarius Strobl 	device_t dev;
90072dec079SMarius Strobl 
90172dec079SMarius Strobl 	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
90272dec079SMarius Strobl 		if (bp->bio_length != sizeof(dev))
90372dec079SMarius Strobl 			return (EFAULT);
90472dec079SMarius Strobl 		part = bp->bio_disk->d_drv1;
90572dec079SMarius Strobl 		dev = part->sc->dev;
90672dec079SMarius Strobl 		bcopy(&dev, bp->bio_data, sizeof(dev));
90772dec079SMarius Strobl 		bp->bio_completed = bp->bio_length;
90872dec079SMarius Strobl 		return (0);
90972dec079SMarius Strobl 	}
91072dec079SMarius Strobl 	return (-1);
91172dec079SMarius Strobl }
91272dec079SMarius Strobl 
91372dec079SMarius Strobl static int
91472dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
91572dec079SMarius Strobl {
91672dec079SMarius Strobl 	struct mmc_command cmd;
91772dec079SMarius Strobl 	struct mmc_request req;
91872dec079SMarius Strobl 
91972dec079SMarius Strobl 	memset(&req, 0, sizeof(req));
92072dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
92172dec079SMarius Strobl 	cmd.mrq = &req;
92272dec079SMarius Strobl 	req.cmd = &cmd;
92372dec079SMarius Strobl 	cmd.opcode = MMC_SET_BLOCK_COUNT;
92472dec079SMarius Strobl 	cmd.arg = count & 0x0000FFFF;
92572dec079SMarius Strobl 	if (reliable)
92672dec079SMarius Strobl 		cmd.arg |= 1 << 31;
92772dec079SMarius Strobl 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
92872dec079SMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbr, sc->dev, &req);
92972dec079SMarius Strobl 	return (cmd.error);
93072dec079SMarius Strobl }
93172dec079SMarius Strobl 
93272dec079SMarius Strobl static int
93372dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
93472dec079SMarius Strobl {
93572dec079SMarius Strobl 	struct mmcsd_softc *sc;
93672dec079SMarius Strobl 	int err;
93772dec079SMarius Strobl 	uint8_t	value;
93872dec079SMarius Strobl 
93972dec079SMarius Strobl 	sc = device_get_softc(dev);
94072dec079SMarius Strobl 
94172dec079SMarius Strobl 	if (sc->part_curr == part)
94272dec079SMarius Strobl 		return (MMC_ERR_NONE);
94372dec079SMarius Strobl 
94472dec079SMarius Strobl 	if (sc->mode == mode_sd)
94572dec079SMarius Strobl 		return (MMC_ERR_NONE);
94672dec079SMarius Strobl 
94772dec079SMarius Strobl 	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
94872dec079SMarius Strobl 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
94972dec079SMarius Strobl 	/* Jump! */
95072dec079SMarius Strobl 	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
95172dec079SMarius Strobl 	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
95272dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
95372dec079SMarius Strobl 		return (err);
95472dec079SMarius Strobl 
95572dec079SMarius Strobl 	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
95672dec079SMarius Strobl 	sc->part_curr = part;
95772dec079SMarius Strobl 	return (MMC_ERR_NONE);
95872dec079SMarius Strobl }
95972dec079SMarius Strobl 
960bb1ef63fSWarner Losh static const char *
961bb1ef63fSWarner Losh mmcsd_errmsg(int e)
962bb1ef63fSWarner Losh {
9631bacf3beSMarius Strobl 
964bb1ef63fSWarner Losh 	if (e < 0 || e > MMC_ERR_MAX)
965bb1ef63fSWarner Losh 		return "Bad error code";
966bb1ef63fSWarner Losh 	return errmsg[e];
967bb1ef63fSWarner Losh }
968bb1ef63fSWarner Losh 
9693906d42dSAlexander Motin static daddr_t
97072dec079SMarius Strobl mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
971114b4164SWarner Losh {
972114b4164SWarner Losh 	daddr_t block, end;
973114b4164SWarner Losh 	struct mmc_command cmd;
974114b4164SWarner Losh 	struct mmc_command stop;
975114b4164SWarner Losh 	struct mmc_request req;
976114b4164SWarner Losh 	struct mmc_data data;
97772dec079SMarius Strobl 	struct mmcsd_softc *sc;
97872dec079SMarius Strobl 	device_t dev, mmcbr;
9791bacf3beSMarius Strobl 	int numblocks, sz;
9801bacf3beSMarius Strobl 	char *vaddr;
981114b4164SWarner Losh 
98272dec079SMarius Strobl 	sc = part->sc;
98372dec079SMarius Strobl 	dev = sc->dev;
98472dec079SMarius Strobl 	mmcbr = sc->mmcbr;
98572dec079SMarius Strobl 
9863906d42dSAlexander Motin 	block = bp->bio_pblkno;
98772dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
988114b4164SWarner Losh 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
9893906d42dSAlexander Motin 	while (block < end) {
9901bacf3beSMarius Strobl 		vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
9911bacf3beSMarius Strobl 		numblocks = min(end - block, mmc_get_max_data(dev));
992114b4164SWarner Losh 		memset(&req, 0, sizeof(req));
993114b4164SWarner Losh 		memset(&cmd, 0, sizeof(cmd));
994114b4164SWarner Losh 		memset(&stop, 0, sizeof(stop));
995ed7142a7SIan Lepore 		memset(&data, 0, sizeof(data));
996a350e540SIan Lepore 		cmd.mrq = &req;
997114b4164SWarner Losh 		req.cmd = &cmd;
998114b4164SWarner Losh 		cmd.data = &data;
999114b4164SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
10000c0903b1SWarner Losh 			if (numblocks > 1)
1001114b4164SWarner Losh 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1002114b4164SWarner Losh 			else
1003114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
10040c0903b1SWarner Losh 		} else {
10050c0903b1SWarner Losh 			if (numblocks > 1)
10060c0903b1SWarner Losh 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
10070c0903b1SWarner Losh 			else
1008114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
10090c0903b1SWarner Losh 		}
1010c18f1e26SAlexander Motin 		cmd.arg = block;
1011c18f1e26SAlexander Motin 		if (!mmc_get_high_cap(dev))
1012c18f1e26SAlexander Motin 			cmd.arg <<= 9;
1013114b4164SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1014114b4164SWarner Losh 		data.data = vaddr;
1015114b4164SWarner Losh 		data.mrq = &req;
10160c0903b1SWarner Losh 		if (bp->bio_cmd == BIO_READ)
1017114b4164SWarner Losh 			data.flags = MMC_DATA_READ;
10180c0903b1SWarner Losh 		else
1019114b4164SWarner Losh 			data.flags = MMC_DATA_WRITE;
10200c0903b1SWarner Losh 		data.len = numblocks * sz;
10210c0903b1SWarner Losh 		if (numblocks > 1) {
10220c0903b1SWarner Losh 			data.flags |= MMC_DATA_MULTI;
1023114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
1024114b4164SWarner Losh 			stop.arg = 0;
1025114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1026a350e540SIan Lepore 			stop.mrq = &req;
10270c0903b1SWarner Losh 			req.stop = &stop;
10280c0903b1SWarner Losh 		}
10298fa3a540SWarner Losh 		MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1030bb1ef63fSWarner Losh 		if (req.cmd->error != MMC_ERR_NONE) {
10311bacf3beSMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
10321bacf3beSMarius Strobl 			    LOG_PPS))
1033bb1ef63fSWarner Losh 				device_printf(dev, "Error indicated: %d %s\n",
10341bacf3beSMarius Strobl 				    req.cmd->error,
10351bacf3beSMarius Strobl 				    mmcsd_errmsg(req.cmd->error));
10360c0903b1SWarner Losh 			break;
1037bb1ef63fSWarner Losh 		}
10380c0903b1SWarner Losh 		block += numblocks;
1039114b4164SWarner Losh 	}
10403906d42dSAlexander Motin 	return (block);
10413906d42dSAlexander Motin }
10423906d42dSAlexander Motin 
10433906d42dSAlexander Motin static daddr_t
104472dec079SMarius Strobl mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
10453906d42dSAlexander Motin {
10463906d42dSAlexander Motin 	daddr_t block, end, start, stop;
10473906d42dSAlexander Motin 	struct mmc_command cmd;
10483906d42dSAlexander Motin 	struct mmc_request req;
104972dec079SMarius Strobl 	struct mmcsd_softc *sc;
105072dec079SMarius Strobl 	device_t dev, mmcbr;
105172dec079SMarius Strobl 	int erase_sector, sz;
105272dec079SMarius Strobl 
105372dec079SMarius Strobl 	sc = part->sc;
105472dec079SMarius Strobl 	dev = sc->dev;
105572dec079SMarius Strobl 	mmcbr = sc->mmcbr;
10563906d42dSAlexander Motin 
10573906d42dSAlexander Motin 	block = bp->bio_pblkno;
105872dec079SMarius Strobl 	sz = part->disk->d_sectorsize;
10593906d42dSAlexander Motin 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
1060a7cf6274SAlexander Motin 	/* Coalesce with part remaining from previous request. */
106172dec079SMarius Strobl 	if (block > part->eblock && block <= part->eend)
106272dec079SMarius Strobl 		block = part->eblock;
106372dec079SMarius Strobl 	if (end >= part->eblock && end < part->eend)
106472dec079SMarius Strobl 		end = part->eend;
10653906d42dSAlexander Motin 	/* Safe round to the erase sector boundaries. */
10663906d42dSAlexander Motin 	erase_sector = mmc_get_erase_sector(dev);
10673906d42dSAlexander Motin 	start = block + erase_sector - 1;	 /* Round up. */
10683906d42dSAlexander Motin 	start -= start % erase_sector;
10693906d42dSAlexander Motin 	stop = end;				/* Round down. */
10703906d42dSAlexander Motin 	stop -= end % erase_sector;
107172dec079SMarius Strobl 	/* We can't erase an area smaller than a sector, store it for later. */
1072a7cf6274SAlexander Motin 	if (start >= stop) {
107372dec079SMarius Strobl 		part->eblock = block;
107472dec079SMarius Strobl 		part->eend = end;
10753906d42dSAlexander Motin 		return (end);
1076a7cf6274SAlexander Motin 	}
10773906d42dSAlexander Motin 
10783906d42dSAlexander Motin 	/* Set erase start position. */
10793906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
10803906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
1081a350e540SIan Lepore 	cmd.mrq = &req;
10823906d42dSAlexander Motin 	req.cmd = &cmd;
10833906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
10843906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_START;
10853906d42dSAlexander Motin 	else
10863906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_START;
10873906d42dSAlexander Motin 	cmd.arg = start;
10883906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
10893906d42dSAlexander Motin 		cmd.arg <<= 9;
10903906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
10918fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
10923906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
10933906d42dSAlexander Motin 	    printf("erase err1: %d\n", req.cmd->error);
10943906d42dSAlexander Motin 	    return (block);
10953906d42dSAlexander Motin 	}
10963906d42dSAlexander Motin 	/* Set erase stop position. */
10973906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
10983906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
10993906d42dSAlexander Motin 	req.cmd = &cmd;
11003906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
11013906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_END;
11023906d42dSAlexander Motin 	else
11033906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_END;
11043906d42dSAlexander Motin 	cmd.arg = stop;
11053906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
11063906d42dSAlexander Motin 		cmd.arg <<= 9;
11073906d42dSAlexander Motin 	cmd.arg--;
11083906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
11098fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
11103906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
11113906d42dSAlexander Motin 	    printf("erase err2: %d\n", req.cmd->error);
11123906d42dSAlexander Motin 	    return (block);
11133906d42dSAlexander Motin 	}
11143906d42dSAlexander Motin 	/* Erase range. */
11153906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
11163906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
11173906d42dSAlexander Motin 	req.cmd = &cmd;
11183906d42dSAlexander Motin 	cmd.opcode = MMC_ERASE;
11193906d42dSAlexander Motin 	cmd.arg = 0;
11203906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
11218fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
11223906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
11233906d42dSAlexander Motin 	    printf("erase err3 %d\n", req.cmd->error);
11243906d42dSAlexander Motin 	    return (block);
11253906d42dSAlexander Motin 	}
1126a7cf6274SAlexander Motin 	/* Store one of remaining parts for the next call. */
112772dec079SMarius Strobl 	if (bp->bio_pblkno >= part->eblock || block == start) {
112872dec079SMarius Strobl 		part->eblock = stop;	/* Predict next forward. */
112972dec079SMarius Strobl 		part->eend = end;
1130a7cf6274SAlexander Motin 	} else {
113172dec079SMarius Strobl 		part->eblock = block;	/* Predict next backward. */
113272dec079SMarius Strobl 		part->eend = start;
1133a7cf6274SAlexander Motin 	}
11343906d42dSAlexander Motin 	return (end);
11353906d42dSAlexander Motin }
11363906d42dSAlexander Motin 
1137a1fda318SAlexander Motin static int
11381bacf3beSMarius Strobl mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
11391bacf3beSMarius Strobl     size_t length)
1140a1fda318SAlexander Motin {
1141a1fda318SAlexander Motin 	struct bio bp;
1142a1fda318SAlexander Motin 	daddr_t block, end;
114372dec079SMarius Strobl 	struct disk *disk;
114472dec079SMarius Strobl 	struct mmcsd_softc *sc;
114572dec079SMarius Strobl 	struct mmcsd_part *part;
114672dec079SMarius Strobl 	device_t dev, mmcbr;
114772dec079SMarius Strobl 	int err;
1148a1fda318SAlexander Motin 
1149a1fda318SAlexander Motin 	/* length zero is special and really means flush buffers to media */
1150a1fda318SAlexander Motin 	if (!length)
1151a1fda318SAlexander Motin 		return (0);
1152a1fda318SAlexander Motin 
115372dec079SMarius Strobl 	disk = arg;
115472dec079SMarius Strobl 	part = disk->d_drv1;
115572dec079SMarius Strobl 	sc = part->sc;
115672dec079SMarius Strobl 	dev = sc->dev;
115772dec079SMarius Strobl 	mmcbr = sc->mmcbr;
115872dec079SMarius Strobl 
1159c55f5707SWarner Losh 	g_reset_bio(&bp);
1160a1fda318SAlexander Motin 	bp.bio_disk = disk;
1161a1fda318SAlexander Motin 	bp.bio_pblkno = offset / disk->d_sectorsize;
1162a1fda318SAlexander Motin 	bp.bio_bcount = length;
1163a1fda318SAlexander Motin 	bp.bio_data = virtual;
1164a1fda318SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
116572dec079SMarius Strobl 	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
11668fa3a540SWarner Losh 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
116772dec079SMarius Strobl 	err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
116872dec079SMarius Strobl 	if (err != MMC_ERR_NONE) {
116972dec079SMarius Strobl 		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
117072dec079SMarius Strobl 			device_printf(dev, "Partition switch error\n");
117172dec079SMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbr, dev);
117272dec079SMarius Strobl 		return (EIO);
117372dec079SMarius Strobl 	}
117472dec079SMarius Strobl 	block = mmcsd_rw(part, &bp);
11758fa3a540SWarner Losh 	MMCBUS_RELEASE_BUS(mmcbr, dev);
1176a1fda318SAlexander Motin 	return ((end < block) ? EIO : 0);
1177a1fda318SAlexander Motin }
1178a1fda318SAlexander Motin 
11793906d42dSAlexander Motin static void
11803906d42dSAlexander Motin mmcsd_task(void *arg)
11813906d42dSAlexander Motin {
11823906d42dSAlexander Motin 	daddr_t block, end;
118372dec079SMarius Strobl 	struct mmcsd_part *part;
118472dec079SMarius Strobl 	struct mmcsd_softc *sc;
118572dec079SMarius Strobl 	struct bio *bp;
118672dec079SMarius Strobl 	device_t dev, mmcbr;
118772dec079SMarius Strobl 	int err, sz;
118872dec079SMarius Strobl 
118972dec079SMarius Strobl 	part = arg;
119072dec079SMarius Strobl 	sc = part->sc;
119172dec079SMarius Strobl 	dev = sc->dev;
119272dec079SMarius Strobl 	mmcbr = sc->mmcbr;
11933906d42dSAlexander Motin 
1194249b0e85SAlexander Motin 	while (1) {
119572dec079SMarius Strobl 		MMCSD_PART_LOCK(part);
11963906d42dSAlexander Motin 		do {
119772dec079SMarius Strobl 			if (part->running == 0)
1198249b0e85SAlexander Motin 				goto out;
119972dec079SMarius Strobl 			bp = bioq_takefirst(&part->bio_queue);
12003906d42dSAlexander Motin 			if (bp == NULL)
120172dec079SMarius Strobl 				msleep(part, &part->part_mtx, PRIBIO,
120272dec079SMarius Strobl 				    "jobqueue", 0);
1203249b0e85SAlexander Motin 		} while (bp == NULL);
120472dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
120572dec079SMarius Strobl 		if (bp->bio_cmd != BIO_READ && part->ro) {
12063906d42dSAlexander Motin 			bp->bio_error = EROFS;
12073906d42dSAlexander Motin 			bp->bio_resid = bp->bio_bcount;
12083906d42dSAlexander Motin 			bp->bio_flags |= BIO_ERROR;
12093906d42dSAlexander Motin 			biodone(bp);
12103906d42dSAlexander Motin 			continue;
12113906d42dSAlexander Motin 		}
12128fa3a540SWarner Losh 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
121372dec079SMarius Strobl 		sz = part->disk->d_sectorsize;
12143906d42dSAlexander Motin 		block = bp->bio_pblkno;
12153906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
121672dec079SMarius Strobl 		err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
121772dec079SMarius Strobl 		if (err != MMC_ERR_NONE) {
121872dec079SMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
121972dec079SMarius Strobl 			    LOG_PPS))
122072dec079SMarius Strobl 				device_printf(dev, "Partition switch error\n");
122172dec079SMarius Strobl 			goto release;
122272dec079SMarius Strobl 		}
12233906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1224a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
122572dec079SMarius Strobl 			if (block < part->eend && end > part->eblock)
122672dec079SMarius Strobl 				part->eblock = part->eend = 0;
122772dec079SMarius Strobl 			block = mmcsd_rw(part, bp);
12283906d42dSAlexander Motin 		} else if (bp->bio_cmd == BIO_DELETE) {
122972dec079SMarius Strobl 			block = mmcsd_delete(part, bp);
12303906d42dSAlexander Motin 		}
123172dec079SMarius Strobl release:
12328fa3a540SWarner Losh 		MMCBUS_RELEASE_BUS(mmcbr, dev);
12330c0903b1SWarner Losh 		if (block < end) {
12340c0903b1SWarner Losh 			bp->bio_error = EIO;
12350c0903b1SWarner Losh 			bp->bio_resid = (end - block) * sz;
12360c0903b1SWarner Losh 			bp->bio_flags |= BIO_ERROR;
1237d119f637SMarius Strobl 		} else {
1238d119f637SMarius Strobl 			bp->bio_resid = 0;
12390c0903b1SWarner Losh 		}
1240114b4164SWarner Losh 		biodone(bp);
1241114b4164SWarner Losh 	}
1242249b0e85SAlexander Motin out:
12430e1466acSWarner Losh 	/* tell parent we're done */
124472dec079SMarius Strobl 	part->running = -1;
124572dec079SMarius Strobl 	MMCSD_PART_UNLOCK(part);
124672dec079SMarius Strobl 	wakeup(part);
12470e1466acSWarner Losh 
12483745c395SJulian Elischer 	kproc_exit(0);
1249114b4164SWarner Losh }
1250114b4164SWarner Losh 
1251a0075e58SWarner Losh static int
1252a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev)
1253a0075e58SWarner Losh {
1254ae5d8757SMarius Strobl 
1255a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_1)
1256a0075e58SWarner Losh 		return (1);
1257a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_4)
1258a0075e58SWarner Losh 		return (4);
1259a0075e58SWarner Losh 	return (8);
1260a0075e58SWarner Losh }
1261a0075e58SWarner Losh 
1262114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
1263114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
1264114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
1265114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
1266eb67f31aSAlexander Motin 	DEVMETHOD(device_suspend, mmcsd_suspend),
1267eb67f31aSAlexander Motin 	DEVMETHOD(device_resume, mmcsd_resume),
12687aa65846SMarius Strobl 	DEVMETHOD_END
1269114b4164SWarner Losh };
1270114b4164SWarner Losh 
1271114b4164SWarner Losh static driver_t mmcsd_driver = {
1272114b4164SWarner Losh 	"mmcsd",
1273114b4164SWarner Losh 	mmcsd_methods,
1274114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
1275114b4164SWarner Losh };
1276114b4164SWarner Losh static devclass_t mmcsd_devclass;
1277114b4164SWarner Losh 
127872dec079SMarius Strobl static int
127972dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
128072dec079SMarius Strobl {
128172dec079SMarius Strobl 
128272dec079SMarius Strobl 	switch (what) {
128372dec079SMarius Strobl 	case MOD_LOAD:
128472dec079SMarius Strobl 		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
128572dec079SMarius Strobl 		    TRUE);
128672dec079SMarius Strobl 		return (0);
128772dec079SMarius Strobl 	case MOD_UNLOAD:
128872dec079SMarius Strobl 		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
128972dec079SMarius Strobl 		return (0);
129072dec079SMarius Strobl 	}
129172dec079SMarius Strobl 	return (0);
129272dec079SMarius Strobl }
129372dec079SMarius Strobl 
129472dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
129572dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
129672dec079SMarius Strobl MMC_DEPEND(mmcsd);
1297