mmcsd.c revision 322120
1163516Simp/*-
2163516Simp * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3163516Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4318198Smarius * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
5163516Simp *
6163516Simp * Redistribution and use in source and binary forms, with or without
7163516Simp * modification, are permitted provided that the following conditions
8163516Simp * are met:
9163516Simp * 1. Redistributions of source code must retain the above copyright
10163516Simp *    notice, this list of conditions and the following disclaimer.
11163516Simp * 2. Redistributions in binary form must reproduce the above copyright
12163516Simp *    notice, this list of conditions and the following disclaimer in the
13163516Simp *    documentation and/or other materials provided with the distribution.
14163516Simp *
15163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25170002Simp *
26170002Simp * Portions of this software may have been developed with reference to
27170002Simp * the SD Simplified Specification.  The following disclaimer may apply:
28170002Simp *
29170002Simp * The following conditions apply to the release of the simplified
30170002Simp * specification ("Simplified Specification") by the SD Card Association and
31170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD
32170002Simp * Specification which is owned by the SD Card Association and the SD
33170002Simp * Group. This Simplified Specification is provided on a non-confidential
34170002Simp * basis subject to the disclaimers below. Any implementation of the
35170002Simp * Simplified Specification may require a license from the SD Card
36170002Simp * Association, SD Group, SD-3C LLC or other third parties.
37170002Simp *
38170002Simp * Disclaimers:
39170002Simp *
40170002Simp * The information contained in the Simplified Specification is presented only
41170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and
42170002Simp * is provided "AS-IS" without any representations or warranties of any
43170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
44170002Simp * Card Association for any damages, any infringements of patents or other
45170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third
46170002Simp * parties, which may result from its use. No license is granted by
47170002Simp * implication, estoppel or otherwise under any patent or other rights of the
48170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
49170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
50170002Simp * or the SD Card Association to disclose or distribute any technical
51170002Simp * information, know-how or other confidential information to any third party.
52163516Simp */
53163516Simp
54163516Simp#include <sys/cdefs.h>
55163516Simp__FBSDID("$FreeBSD: stable/10/sys/dev/mmc/mmcsd.c 322120 2017-08-06 16:07:34Z marius $");
56163516Simp
57163516Simp#include <sys/param.h>
58163516Simp#include <sys/systm.h>
59163516Simp#include <sys/bio.h>
60163516Simp#include <sys/bus.h>
61163516Simp#include <sys/conf.h>
62318198Smarius#include <sys/fcntl.h>
63318198Smarius#include <sys/ioccom.h>
64163516Simp#include <sys/kernel.h>
65163516Simp#include <sys/kthread.h>
66163516Simp#include <sys/lock.h>
67163516Simp#include <sys/malloc.h>
68163516Simp#include <sys/module.h>
69163516Simp#include <sys/mutex.h>
70318198Smarius#include <sys/slicer.h>
71278687Sian#include <sys/time.h>
72318198Smarius
73318198Smarius#include <geom/geom.h>
74163516Simp#include <geom/geom_disk.h>
75163516Simp
76318198Smarius#include <dev/mmc/bridge.h>
77318198Smarius#include <dev/mmc/mmc_ioctl.h>
78318198Smarius#include <dev/mmc/mmc_subr.h>
79234524Smarius#include <dev/mmc/mmcbrvar.h>
80234524Smarius#include <dev/mmc/mmcreg.h>
81163516Simp#include <dev/mmc/mmcvar.h>
82163516Simp
83163516Simp#include "mmcbus_if.h"
84163516Simp
85234524Smarius#if __FreeBSD_version < 800002
86234524Smarius#define	kproc_create	kthread_create
87234524Smarius#define	kproc_exit	kthread_exit
88234524Smarius#endif
89234524Smarius
90318198Smarius#define	MMCSD_CMD_RETRIES	5
91318198Smarius
92318198Smarius#define	MMCSD_FMT_BOOT		"mmcsd%dboot"
93318198Smarius#define	MMCSD_FMT_GP		"mmcsd%dgp"
94318198Smarius#define	MMCSD_FMT_RPMB		"mmcsd%drpmb"
95318198Smarius#define	MMCSD_LABEL_ENH		"enh"
96318198Smarius
97318198Smarius#define	MMCSD_PART_NAMELEN	(16 + 1)
98318198Smarius
99318198Smariusstruct mmcsd_softc;
100318198Smarius
101318198Smariusstruct mmcsd_part {
102322120Smarius	struct mtx disk_mtx;
103322120Smarius	struct mtx ioctl_mtx;
104318198Smarius	struct mmcsd_softc *sc;
105163516Simp	struct disk *disk;
106163516Simp	struct proc *p;
107163516Simp	struct bio_queue_head bio_queue;
108184034Smav	daddr_t eblock, eend;	/* Range remaining after the last erase. */
109318198Smarius	u_int cnt;
110318198Smarius	u_int type;
111169567Simp	int running;
112185721Smav	int suspend;
113322120Smarius	int ioctl;
114318198Smarius	bool ro;
115318198Smarius	char name[MMCSD_PART_NAMELEN];
116318198Smarius};
117318198Smarius
118318198Smariusstruct mmcsd_softc {
119318198Smarius	device_t dev;
120322120Smarius	device_t mmcbus;
121318198Smarius	struct mmcsd_part *part[MMC_PART_MAX];
122318198Smarius	enum mmc_card_mode mode;
123322120Smarius	u_int max_data;		/* Maximum data size [blocks] */
124322120Smarius	u_int erase_sector;	/* Device native erase sector size [blocks] */
125322120Smarius	uint8_t	high_cap;	/* High Capacity device (block addressed) */
126318198Smarius	uint8_t part_curr;	/* Partition currently switched to */
127318198Smarius	uint8_t ext_csd[MMC_EXTCSD_SIZE];
128318198Smarius	uint16_t rca;
129318198Smarius	uint32_t part_time;	/* Partition switch timeout [us] */
130318198Smarius	off_t enh_base;		/* Enhanced user data area slice base ... */
131318198Smarius	off_t enh_size;		/* ... and size [bytes] */
132278687Sian	int log_count;
133278687Sian	struct timeval log_time;
134318198Smarius	struct cdev *rpmb_dev;
135163516Simp};
136163516Simp
137239607Simpstatic const char *errmsg[] =
138239607Simp{
139239607Simp	"None",
140239607Simp	"Timeout",
141239607Simp	"Bad CRC",
142239607Simp	"Fifo",
143239607Simp	"Failed",
144239607Simp	"Invalid",
145239607Simp	"NO MEMORY"
146239607Simp};
147239607Simp
148278687Sian#define	LOG_PPS		5 /* Log no more than 5 errors per second. */
149278687Sian
150163516Simp/* bus entry points */
151163516Simpstatic int mmcsd_attach(device_t dev);
152163516Simpstatic int mmcsd_detach(device_t dev);
153236491Smariusstatic int mmcsd_probe(device_t dev);
154163516Simp
155163516Simp/* disk routines */
156163516Simpstatic int mmcsd_close(struct disk *dp);
157188725Smavstatic int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
158318198Smarius    off_t offset, size_t length);
159318198Smariusstatic int mmcsd_getattr(struct bio *);
160318198Smariusstatic int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
161318198Smarius    int fflag, struct thread *td);
162236491Smariusstatic int mmcsd_open(struct disk *dp);
163236491Smariusstatic void mmcsd_strategy(struct bio *bp);
164163516Simpstatic void mmcsd_task(void *arg);
165163516Simp
166318198Smarius/* RMPB cdev interface */
167318198Smariusstatic int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
168318198Smarius    int fflag, struct thread *td);
169318198Smarius
170318198Smariusstatic void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
171318198Smarius    const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
172183774Simpstatic int mmcsd_bus_bit_width(device_t dev);
173318198Smariusstatic daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
174318198Smariusstatic int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
175318198Smarius    int fflag);
176318198Smariusstatic int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
177318198Smarius    int fflag);
178318198Smariusstatic uintmax_t mmcsd_pretty_size(off_t size, char *unit);
179318198Smariusstatic daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
180318198Smariusstatic int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
181318198Smariusstatic int mmcsd_slicer(device_t dev, const char *provider,
182318198Smarius    struct flash_slice *slices, int *nslices);
183318198Smariusstatic int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
184318198Smarius    u_int part);
185183774Simp
186322120Smarius#define	MMCSD_DISK_LOCK(_part)		mtx_lock(&(_part)->disk_mtx)
187322120Smarius#define	MMCSD_DISK_UNLOCK(_part)	mtx_unlock(&(_part)->disk_mtx)
188322120Smarius#define	MMCSD_DISK_LOCK_INIT(_part)					\
189322120Smarius	mtx_init(&(_part)->disk_mtx, (_part)->name, "mmcsd disk", MTX_DEF)
190322120Smarius#define	MMCSD_DISK_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->disk_mtx);
191322120Smarius#define	MMCSD_DISK_ASSERT_LOCKED(_part)					\
192322120Smarius	mtx_assert(&(_part)->disk_mtx, MA_OWNED);
193322120Smarius#define	MMCSD_DISK_ASSERT_UNLOCKED(_part)				\
194322120Smarius	mtx_assert(&(_part)->disk_mtx, MA_NOTOWNED);
195163516Simp
196322120Smarius#define	MMCSD_IOCTL_LOCK(_part)		mtx_lock(&(_part)->ioctl_mtx)
197322120Smarius#define	MMCSD_IOCTL_UNLOCK(_part)	mtx_unlock(&(_part)->ioctl_mtx)
198322120Smarius#define	MMCSD_IOCTL_LOCK_INIT(_part)					\
199322120Smarius	mtx_init(&(_part)->ioctl_mtx, (_part)->name, "mmcsd IOCTL", MTX_DEF)
200322120Smarius#define	MMCSD_IOCTL_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->ioctl_mtx);
201322120Smarius#define	MMCSD_IOCTL_ASSERT_LOCKED(_part)				\
202322120Smarius	mtx_assert(&(_part)->ioctl_mtx, MA_OWNED);
203322120Smarius#define	MMCSD_IOCLT_ASSERT_UNLOCKED(_part)				\
204322120Smarius	mtx_assert(&(_part)->ioctl_mtx, MA_NOTOWNED);
205322120Smarius
206163516Simpstatic int
207163516Simpmmcsd_probe(device_t dev)
208163516Simp{
209163516Simp
210183704Smav	device_quiet(dev);
211183480Simp	device_set_desc(dev, "MMC/SD Memory Card");
212163516Simp	return (0);
213163516Simp}
214163516Simp
215163516Simpstatic int
216163516Simpmmcsd_attach(device_t dev)
217163516Simp{
218322120Smarius	device_t mmcbus;
219163516Simp	struct mmcsd_softc *sc;
220318198Smarius	const uint8_t *ext_csd;
221318198Smarius	off_t erase_size, sector_size, size, wp_size;
222318198Smarius	uintmax_t bytes;
223318198Smarius	int err, i;
224318198Smarius	uint8_t rev;
225318198Smarius	bool comp, ro;
226318198Smarius	char unit[2];
227163516Simp
228163516Simp	sc = device_get_softc(dev);
229163516Simp	sc->dev = dev;
230322120Smarius	sc->mmcbus = mmcbus = device_get_parent(dev);
231322120Smarius	sc->mode = mmcbr_get_mode(mmcbus);
232322120Smarius	/*
233322120Smarius	 * Note that in principle with an SDHCI-like re-tuning implementation,
234322120Smarius	 * the maximum data size can change at runtime due to a device removal/
235322120Smarius	 * insertion that results in switches to/from a transfer mode involving
236322120Smarius	 * re-tuning, iff there are multiple devices on a given bus.  Until now
237322120Smarius	 * mmc(4) lacks support for rescanning already attached buses, however,
238322120Smarius	 * and sdhci(4) to date has no support for shared buses in the first
239322120Smarius	 * place either.
240322120Smarius	 */
241322120Smarius	sc->max_data = mmc_get_max_data(dev);
242322120Smarius	sc->erase_sector = mmc_get_erase_sector(dev);
243322120Smarius	sc->high_cap = mmc_get_high_cap(dev);
244318198Smarius	sc->rca = mmc_get_rca(dev);
245163516Simp
246318198Smarius	/* Only MMC >= 4.x devices support EXT_CSD. */
247318198Smarius	if (mmc_get_spec_vers(dev) >= 4) {
248322120Smarius		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
249322120Smarius		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
250322120Smarius		MMCBUS_RELEASE_BUS(mmcbus, dev);
251318198Smarius		if (err != MMC_ERR_NONE)
252318198Smarius			bzero(sc->ext_csd, sizeof(sc->ext_csd));
253318198Smarius	}
254318198Smarius	ext_csd = sc->ext_csd;
255269795Sian
256183774Simp	/*
257318198Smarius	 * Enhanced user data area and general purpose partitions are only
258318198Smarius	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
259318198Smarius	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
260183774Simp	 */
261318198Smarius	rev = ext_csd[EXT_CSD_REV];
262318198Smarius
263318198Smarius	/*
264318198Smarius	 * Ignore user-creatable enhanced user data area and general purpose
265318198Smarius	 * partitions partitions as long as partitioning hasn't been finished.
266318198Smarius	 */
267318198Smarius	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
268318198Smarius
269318198Smarius	/*
270318198Smarius	 * Add enhanced user data area slice, unless it spans the entirety of
271318198Smarius	 * the user data area.  The enhanced area is of a multiple of high
272318198Smarius	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
273318198Smarius	 * 512 KB) and its offset given in either sectors or bytes, depending
274318198Smarius	 * on whether it's a high capacity device or not.
275318198Smarius	 * NB: The slicer and its slices need to be registered before adding
276318198Smarius	 *     the disk for the corresponding user data area as re-tasting is
277318198Smarius	 *     racy.
278318198Smarius	 */
279318198Smarius	sector_size = mmc_get_sector_size(dev);
280318198Smarius	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
281318198Smarius	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
282318198Smarius	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
283318198Smarius	if (rev >= 4 && comp == TRUE && size > 0 &&
284318198Smarius	    (ext_csd[EXT_CSD_PART_SUPPORT] &
285318198Smarius	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
286318198Smarius	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
287318198Smarius		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
288318198Smarius		    MMC_SECTOR_SIZE;
289318198Smarius		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
290318198Smarius		size *= erase_size * wp_size;
291318198Smarius		if (size != mmc_get_media_size(dev) * sector_size) {
292318198Smarius			sc->enh_size = size;
293318198Smarius			sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
294318198Smarius			    (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
295318198Smarius			    (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
296318198Smarius			    (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
297322120Smarius			    (sc->high_cap != 0 ? MMC_SECTOR_SIZE : 1);
298318198Smarius		} else if (bootverbose)
299318198Smarius			device_printf(dev,
300318198Smarius			    "enhanced user data area spans entire device\n");
301183774Simp	}
302318198Smarius
303234524Smarius	/*
304318198Smarius	 * Add default partition.  This may be the only one or the user
305318198Smarius	 * data area in case partitions are supported.
306234524Smarius	 */
307318198Smarius	ro = mmc_get_read_only(dev);
308318198Smarius	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
309318198Smarius	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
310322120Smarius	    sc->erase_sector * sector_size, ro);
311169567Simp
312318198Smarius	if (mmc_get_spec_vers(dev) < 3)
313318198Smarius		return (0);
314163516Simp
315318198Smarius	/* Belatedly announce enhanced user data slice. */
316318198Smarius	if (sc->enh_size != 0) {
317318198Smarius		bytes = mmcsd_pretty_size(size, unit);
318318198Smarius		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
319318198Smarius		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
320318198Smarius		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
321318198Smarius		    device_get_nameunit(dev));
322318198Smarius	}
323318198Smarius
324318198Smarius	/*
325318198Smarius	 * Determine partition switch timeout (provided in units of 10 ms)
326318198Smarius	 * and ensure it's at least 300 ms as some eMMC chips lie.
327318198Smarius	 */
328318198Smarius	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
329318198Smarius	    300 * 1000);
330318198Smarius
331318198Smarius	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
332318198Smarius	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
333322120Smarius	if (size > 0 && (mmcbr_get_caps(mmcbus) & MMC_CAP_BOOT_NOACC) == 0) {
334318198Smarius		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
335318198Smarius		    MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
336318198Smarius		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
337318198Smarius		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
338318198Smarius		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
339318198Smarius		    MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
340318198Smarius		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
341318198Smarius		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
342318198Smarius	}
343318198Smarius
344318198Smarius	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
345318198Smarius	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
346318198Smarius	if (rev >= 5 && size > 0)
347318198Smarius		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
348318198Smarius		    MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
349318198Smarius
350318198Smarius	if (rev <= 3 || comp == FALSE)
351318198Smarius		return (0);
352318198Smarius
353318198Smarius	/*
354318198Smarius	 * Add general purpose partitions, which are of a multiple of high
355318198Smarius	 * capacity write protect groups, too.
356318198Smarius	 */
357318198Smarius	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
358318198Smarius		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
359318198Smarius		    MMC_SECTOR_SIZE;
360318198Smarius		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
361318198Smarius		for (i = 0; i < MMC_PART_GP_MAX; i++) {
362318198Smarius			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
363318198Smarius			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
364318198Smarius			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
365318198Smarius			if (size == 0)
366318198Smarius				continue;
367318198Smarius			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
368318198Smarius			    MMCSD_FMT_GP, i, size * erase_size * wp_size,
369318198Smarius			    erase_size, ro);
370318198Smarius		}
371318198Smarius	}
372163516Simp	return (0);
373163516Simp}
374163516Simp
375318198Smariusstatic uintmax_t
376318198Smariusmmcsd_pretty_size(off_t size, char *unit)
377318198Smarius{
378318198Smarius	uintmax_t bytes;
379318198Smarius	int i;
380318198Smarius
381318198Smarius	/*
382318198Smarius	 * Display in most natural units.  There's no card < 1MB.  However,
383318198Smarius	 * RPMB partitions occasionally are smaller than that, though.  The
384318198Smarius	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
385318198Smarius	 * format supports up to 4 GiB and some card makers push it up to this
386318198Smarius	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
387318198Smarius	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
388318198Smarius	 * so we note it in passing here and don't add the code to print TB).
389318198Smarius	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
390318198Smarius	 * report them like that.  We also round to the nearest unit, since
391318198Smarius	 * many cards are a few percent short, even of the power of 10 size.
392318198Smarius	 */
393318198Smarius	bytes = size;
394318198Smarius	unit[0] = unit[1] = '\0';
395318198Smarius	for (i = 0; i <= 2 && bytes >= 1000; i++) {
396318198Smarius		bytes = (bytes + 1000 / 2 - 1) / 1000;
397318198Smarius		switch (i) {
398318198Smarius		case 0:
399318198Smarius			unit[0] = 'k';
400318198Smarius			break;
401318198Smarius		case 1:
402318198Smarius			unit[0] = 'M';
403318198Smarius			break;
404318198Smarius		case 2:
405318198Smarius			unit[0] = 'G';
406318198Smarius			break;
407318198Smarius		default:
408318198Smarius			break;
409318198Smarius		}
410318198Smarius	}
411318198Smarius	return (bytes);
412318198Smarius}
413318198Smarius
414318198Smariusstatic struct cdevsw mmcsd_rpmb_cdevsw = {
415318198Smarius	.d_version	= D_VERSION,
416318198Smarius	.d_name		= "mmcsdrpmb",
417318198Smarius	.d_ioctl	= mmcsd_ioctl_rpmb
418318198Smarius};
419318198Smarius
420318198Smariusstatic void
421318198Smariusmmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
422318198Smarius    off_t media_size, off_t erase_size, bool ro)
423318198Smarius{
424318198Smarius	struct make_dev_args args;
425322120Smarius	device_t dev, mmcbus;
426318198Smarius	const char *ext;
427318198Smarius	const uint8_t *ext_csd;
428318198Smarius	struct mmcsd_part *part;
429318198Smarius	struct disk *d;
430318198Smarius	uintmax_t bytes;
431318198Smarius	u_int gp;
432318198Smarius	uint32_t speed;
433318198Smarius	uint8_t extattr;
434318198Smarius	bool enh;
435318198Smarius	char unit[2];
436318198Smarius
437318198Smarius	dev = sc->dev;
438322120Smarius	mmcbus = sc->mmcbus;
439318198Smarius	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
440318198Smarius	    M_WAITOK | M_ZERO);
441318198Smarius	part->sc = sc;
442318198Smarius	part->cnt = cnt;
443318198Smarius	part->type = type;
444318198Smarius	part->ro = ro;
445318198Smarius	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
446318198Smarius
447322120Smarius	MMCSD_IOCTL_LOCK_INIT(part);
448322120Smarius
449322120Smarius	/*
450322120Smarius	 * For the RPMB partition, allow IOCTL access only.
451322120Smarius	 * NB: If ever attaching RPMB partitions to disk(9), the re-tuning
452322120Smarius	 *     implementation and especially its pausing need to be revisited,
453322120Smarius	 *     because then re-tuning requests may be issued by the IOCTL half
454322120Smarius	 *     of this driver while re-tuning is already paused by the disk(9)
455322120Smarius	 *     one and vice versa.
456322120Smarius	 */
457318198Smarius	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
458318198Smarius		make_dev_args_init(&args);
459318198Smarius		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
460318198Smarius		args.mda_devsw = &mmcsd_rpmb_cdevsw;
461318198Smarius		args.mda_uid = UID_ROOT;
462318198Smarius		args.mda_gid = GID_OPERATOR;
463318198Smarius		args.mda_mode = 0640;
464318198Smarius		args.mda_si_drv1 = part;
465318198Smarius		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
466318198Smarius			device_printf(dev, "Failed to make RPMB device\n");
467318198Smarius			free(part, M_DEVBUF);
468318198Smarius			return;
469318198Smarius		}
470318198Smarius	} else {
471322120Smarius		MMCSD_DISK_LOCK_INIT(part);
472318198Smarius
473318198Smarius		d = part->disk = disk_alloc();
474318198Smarius		d->d_open = mmcsd_open;
475318198Smarius		d->d_close = mmcsd_close;
476318198Smarius		d->d_strategy = mmcsd_strategy;
477318198Smarius		d->d_ioctl = mmcsd_ioctl_disk;
478318198Smarius		d->d_dump = mmcsd_dump;
479318198Smarius		d->d_getattr = mmcsd_getattr;
480318198Smarius		d->d_name = part->name;
481318198Smarius		d->d_drv1 = part;
482318198Smarius		d->d_sectorsize = mmc_get_sector_size(dev);
483322120Smarius		d->d_maxsize = sc->max_data * d->d_sectorsize;
484318198Smarius		d->d_mediasize = media_size;
485318198Smarius		d->d_stripesize = erase_size;
486318198Smarius		d->d_unit = cnt;
487318198Smarius		d->d_flags = DISKFLAG_CANDELETE;
488318198Smarius		d->d_delmaxsize = erase_size;
489318198Smarius		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
490318198Smarius		    sizeof(d->d_ident));
491318198Smarius		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
492318198Smarius		    sizeof(d->d_descr));
493318198Smarius		d->d_rotation_rate = DISK_RR_NON_ROTATING;
494318198Smarius
495318198Smarius		disk_create(d, DISK_VERSION);
496318198Smarius		bioq_init(&part->bio_queue);
497318198Smarius
498318198Smarius		part->running = 1;
499318198Smarius		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
500318198Smarius		    "%s%d: mmc/sd card", part->name, cnt);
501318198Smarius	}
502318198Smarius
503318198Smarius	bytes = mmcsd_pretty_size(media_size, unit);
504318198Smarius	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
505322120Smarius		speed = mmcbr_get_clock(mmcbus);
506318198Smarius		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
507318198Smarius		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
508322120Smarius		    ro ? " (read-only)" : "", device_get_nameunit(mmcbus),
509318198Smarius		    speed / 1000000, (speed / 100000) % 10,
510322120Smarius		    mmcsd_bus_bit_width(dev), sc->max_data);
511318198Smarius	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
512318198Smarius		printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
513318198Smarius		    unit, type, ro ? " (read-only)" : "",
514318198Smarius		    device_get_nameunit(dev));
515318198Smarius	} else {
516318198Smarius		enh = false;
517318198Smarius		ext = NULL;
518318198Smarius		extattr = 0;
519318198Smarius		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
520318198Smarius		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
521318198Smarius			ext_csd = sc->ext_csd;
522318198Smarius			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
523318198Smarius			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
524318198Smarius			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
525318198Smarius			    (ext_csd[EXT_CSD_PART_ATTR] &
526318198Smarius			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
527318198Smarius				enh = true;
528318198Smarius			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
529318198Smarius			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
530318198Smarius				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
531318198Smarius				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
532318198Smarius				switch (extattr) {
533318198Smarius					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
534318198Smarius						break;
535318198Smarius					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
536318198Smarius						ext = "system code";
537318198Smarius						break;
538318198Smarius					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
539318198Smarius						ext = "non-persistent";
540318198Smarius						break;
541318198Smarius					default:
542318198Smarius						ext = "reserved";
543318198Smarius						break;
544318198Smarius				}
545318198Smarius			}
546318198Smarius		}
547318198Smarius		if (ext == NULL)
548318198Smarius			printf("%s%d: %ju%sB partion %d%s%s at %s\n",
549318198Smarius			    part->name, cnt, bytes, unit, type, enh ?
550318198Smarius			    " enhanced" : "", ro ? " (read-only)" : "",
551318198Smarius			    device_get_nameunit(dev));
552318198Smarius		else
553318198Smarius			printf("%s%d: %ju%sB partion %d extended 0x%x "
554318198Smarius			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
555318198Smarius			    type, extattr, ext, ro ? " (read-only)" : "",
556318198Smarius			    device_get_nameunit(dev));
557318198Smarius	}
558318198Smarius}
559318198Smarius
560163516Simpstatic int
561318198Smariusmmcsd_slicer(device_t dev, const char *provider,
562318198Smarius    struct flash_slice *slices, int *nslices)
563318198Smarius{
564318198Smarius	char name[MMCSD_PART_NAMELEN];
565318198Smarius	struct mmcsd_softc *sc;
566318198Smarius	struct mmcsd_part *part;
567318198Smarius
568318198Smarius	*nslices = 0;
569318198Smarius	if (slices == NULL)
570318198Smarius		return (ENOMEM);
571318198Smarius
572318198Smarius	sc = device_get_softc(dev);
573318198Smarius	if (sc->enh_size == 0)
574318198Smarius		return (ENXIO);
575318198Smarius
576318198Smarius	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
577318198Smarius	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
578318198Smarius	    part->disk->d_unit);
579318198Smarius	if (strcmp(name, provider) != 0)
580318198Smarius		return (ENXIO);
581318198Smarius
582318198Smarius	*nslices = 1;
583318198Smarius	slices[0].base = sc->enh_base;
584318198Smarius	slices[0].size = sc->enh_size;
585318198Smarius	slices[0].label = MMCSD_LABEL_ENH;
586318198Smarius	return (0);
587318198Smarius}
588318198Smarius
589318198Smariusstatic int
590163516Simpmmcsd_detach(device_t dev)
591163516Simp{
592169567Simp	struct mmcsd_softc *sc = device_get_softc(dev);
593318198Smarius	struct mmcsd_part *part;
594318198Smarius	int i;
595169567Simp
596318198Smarius	for (i = 0; i < MMC_PART_MAX; i++) {
597318198Smarius		part = sc->part[i];
598322120Smarius		if (part != NULL) {
599322120Smarius			if (part->disk != NULL) {
600322120Smarius				MMCSD_DISK_LOCK(part);
601322120Smarius				part->suspend = 0;
602322120Smarius				if (part->running > 0) {
603322120Smarius					/* kill thread */
604322120Smarius					part->running = 0;
605322120Smarius					wakeup(part);
606322120Smarius					/* wait for thread to finish. */
607322120Smarius					while (part->running != -1)
608322120Smarius						msleep(part, &part->disk_mtx, 0,
609322120Smarius						    "mmcsd disk detach", 0);
610322120Smarius				}
611322120Smarius				MMCSD_DISK_UNLOCK(part);
612318198Smarius			}
613322120Smarius			MMCSD_IOCTL_LOCK(part);
614322120Smarius			while (part->ioctl > 0)
615322120Smarius				msleep(part, &part->ioctl_mtx, 0,
616322120Smarius				    "mmcsd IOCTL detach", 0);
617322120Smarius			part->ioctl = -1;
618322120Smarius			MMCSD_IOCTL_UNLOCK(part);
619318198Smarius		}
620185721Smav	}
621169567Simp
622318198Smarius	if (sc->rpmb_dev != NULL)
623318198Smarius		destroy_dev(sc->rpmb_dev);
624169567Simp
625318198Smarius	for (i = 0; i < MMC_PART_MAX; i++) {
626318198Smarius		part = sc->part[i];
627318198Smarius		if (part != NULL) {
628318198Smarius			if (part->disk != NULL) {
629318198Smarius				/* Flush the request queue. */
630318198Smarius				bioq_flush(&part->bio_queue, NULL, ENXIO);
631318198Smarius				/* kill disk */
632318198Smarius				disk_destroy(part->disk);
633169567Simp
634322120Smarius				MMCSD_DISK_LOCK_DESTROY(part);
635318198Smarius			}
636322120Smarius			MMCSD_IOCTL_LOCK_DESTROY(part);
637318198Smarius			free(part, M_DEVBUF);
638318198Smarius		}
639318198Smarius	}
640183467Simp	return (0);
641163516Simp}
642163516Simp
643163516Simpstatic int
644185721Smavmmcsd_suspend(device_t dev)
645185721Smav{
646185721Smav	struct mmcsd_softc *sc = device_get_softc(dev);
647318198Smarius	struct mmcsd_part *part;
648318198Smarius	int i;
649185721Smav
650318198Smarius	for (i = 0; i < MMC_PART_MAX; i++) {
651318198Smarius		part = sc->part[i];
652322120Smarius		if (part != NULL) {
653322120Smarius			if (part->disk != NULL) {
654322120Smarius				MMCSD_DISK_LOCK(part);
655322120Smarius				part->suspend = 1;
656322120Smarius				if (part->running > 0) {
657322120Smarius					/* kill thread */
658322120Smarius					part->running = 0;
659322120Smarius					wakeup(part);
660322120Smarius					/* wait for thread to finish. */
661322120Smarius					while (part->running != -1)
662322120Smarius						msleep(part, &part->disk_mtx, 0,
663322120Smarius						    "mmcsd disk suspension", 0);
664322120Smarius				}
665322120Smarius				MMCSD_DISK_UNLOCK(part);
666318198Smarius			}
667322120Smarius			MMCSD_IOCTL_LOCK(part);
668322120Smarius			while (part->ioctl > 0)
669322120Smarius				msleep(part, &part->ioctl_mtx, 0,
670322120Smarius				    "mmcsd IOCTL suspension", 0);
671322120Smarius			part->ioctl = -1;
672322120Smarius			MMCSD_IOCTL_UNLOCK(part);
673318198Smarius		}
674185721Smav	}
675185721Smav	return (0);
676185721Smav}
677185721Smav
678185721Smavstatic int
679185721Smavmmcsd_resume(device_t dev)
680185721Smav{
681185721Smav	struct mmcsd_softc *sc = device_get_softc(dev);
682318198Smarius	struct mmcsd_part *part;
683318198Smarius	int i;
684185721Smav
685318198Smarius	for (i = 0; i < MMC_PART_MAX; i++) {
686318198Smarius		part = sc->part[i];
687322120Smarius		if (part != NULL) {
688322120Smarius			if (part->disk != NULL) {
689322120Smarius				MMCSD_DISK_LOCK(part);
690322120Smarius				part->suspend = 0;
691322120Smarius				if (part->running <= 0) {
692322120Smarius					part->running = 1;
693322120Smarius					MMCSD_DISK_UNLOCK(part);
694322120Smarius					kproc_create(&mmcsd_task, part,
695322120Smarius					    &part->p, 0, 0, "%s%d: mmc/sd card",
696322120Smarius					    part->name, part->cnt);
697322120Smarius				} else
698322120Smarius					MMCSD_DISK_UNLOCK(part);
699322120Smarius			}
700322120Smarius			MMCSD_IOCTL_LOCK(part);
701322120Smarius			part->ioctl = 0;
702322120Smarius			MMCSD_IOCTL_UNLOCK(part);
703318198Smarius		}
704318198Smarius	}
705185721Smav	return (0);
706185721Smav}
707185721Smav
708185721Smavstatic int
709318198Smariusmmcsd_open(struct disk *dp __unused)
710163516Simp{
711236491Smarius
712183467Simp	return (0);
713163516Simp}
714163516Simp
715163516Simpstatic int
716318198Smariusmmcsd_close(struct disk *dp __unused)
717163516Simp{
718236491Smarius
719183467Simp	return (0);
720163516Simp}
721163516Simp
722163516Simpstatic void
723163516Simpmmcsd_strategy(struct bio *bp)
724163516Simp{
725163516Simp	struct mmcsd_softc *sc;
726318198Smarius	struct mmcsd_part *part;
727163516Simp
728318198Smarius	part = bp->bio_disk->d_drv1;
729318198Smarius	sc = part->sc;
730322120Smarius	MMCSD_DISK_LOCK(part);
731318198Smarius	if (part->running > 0 || part->suspend > 0) {
732318198Smarius		bioq_disksort(&part->bio_queue, bp);
733322120Smarius		MMCSD_DISK_UNLOCK(part);
734318198Smarius		wakeup(part);
735185201Smav	} else {
736322120Smarius		MMCSD_DISK_UNLOCK(part);
737185201Smav		biofinish(bp, NULL, ENXIO);
738185201Smav	}
739163516Simp}
740163516Simp
741318198Smariusstatic int
742318198Smariusmmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
743318198Smarius    int fflag, struct thread *td __unused)
744318198Smarius{
745318198Smarius
746318198Smarius	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
747318198Smarius}
748318198Smarius
749318198Smariusstatic int
750318198Smariusmmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
751318198Smarius    struct thread *td __unused)
752318198Smarius{
753318198Smarius
754318198Smarius	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
755318198Smarius}
756318198Smarius
757318198Smariusstatic int
758318198Smariusmmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
759318198Smarius{
760318198Smarius	struct mmc_ioc_cmd *mic;
761318198Smarius	struct mmc_ioc_multi_cmd *mimc;
762318198Smarius	int i, err;
763318198Smarius	u_long cnt, size;
764318198Smarius
765318198Smarius	if ((fflag & FREAD) == 0)
766318198Smarius		return (EBADF);
767318198Smarius
768318198Smarius	err = 0;
769318198Smarius	switch (cmd) {
770318198Smarius	case MMC_IOC_CMD:
771318198Smarius		mic = data;
772322120Smarius		err = mmcsd_ioctl_cmd(part, mic, fflag);
773318198Smarius		break;
774322120Smarius	case MMC_IOC_MULTI_CMD:
775318198Smarius		mimc = data;
776318198Smarius		if (mimc->num_of_cmds == 0)
777318198Smarius			break;
778318198Smarius		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
779318198Smarius			return (EINVAL);
780318198Smarius		cnt = mimc->num_of_cmds;
781318198Smarius		size = sizeof(*mic) * cnt;
782318198Smarius		mic = malloc(size, M_TEMP, M_WAITOK);
783318198Smarius		err = copyin((const void *)mimc->cmds, mic, size);
784322120Smarius		if (err == 0) {
785322120Smarius			for (i = 0; i < cnt; i++) {
786322120Smarius				err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
787322120Smarius				if (err != 0)
788322120Smarius					break;
789322120Smarius			}
790318198Smarius		}
791318198Smarius		free(mic, M_TEMP);
792318198Smarius		break;
793318198Smarius	default:
794318198Smarius		return (ENOIOCTL);
795318198Smarius	}
796318198Smarius	return (err);
797318198Smarius}
798318198Smarius
799318198Smariusstatic int
800318198Smariusmmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
801318198Smarius{
802318198Smarius	struct mmc_command cmd;
803318198Smarius	struct mmc_data data;
804318198Smarius	struct mmcsd_softc *sc;
805322120Smarius	device_t dev, mmcbus;
806318198Smarius	void *dp;
807318198Smarius	u_long len;
808318198Smarius	int err, retries;
809318198Smarius	uint32_t status;
810318198Smarius	uint16_t rca;
811318198Smarius
812318198Smarius	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
813318198Smarius		return (EBADF);
814318198Smarius
815318198Smarius	if (part->ro == TRUE && mic->write_flag != 0)
816318198Smarius		return (EROFS);
817318198Smarius
818322120Smarius	/*
819322120Smarius	 * We don't need to explicitly lock against the disk(9) half of this
820322120Smarius	 * driver as MMCBUS_ACQUIRE_BUS() will serialize us.  However, it's
821322120Smarius	 * necessary to protect against races with detachment and suspension,
822322120Smarius	 * especially since it's required to switch away from RPMB partitions
823322120Smarius	 * again after an access (see mmcsd_switch_part()).
824322120Smarius	 */
825322120Smarius	MMCSD_IOCTL_LOCK(part);
826322120Smarius	while (part->ioctl != 0) {
827322120Smarius		if (part->ioctl < 0) {
828322120Smarius			MMCSD_IOCTL_UNLOCK(part);
829322120Smarius			return (ENXIO);
830322120Smarius		}
831322120Smarius		msleep(part, &part->ioctl_mtx, 0, "mmcsd IOCTL", 0);
832322120Smarius	}
833322120Smarius	part->ioctl = 1;
834322120Smarius	MMCSD_IOCTL_UNLOCK(part);
835322120Smarius
836318198Smarius	err = 0;
837318198Smarius	dp = NULL;
838318198Smarius	len = mic->blksz * mic->blocks;
839322120Smarius	if (len > MMC_IOC_MAX_BYTES) {
840322120Smarius		err = EOVERFLOW;
841322120Smarius		goto out;
842322120Smarius	}
843318198Smarius	if (len != 0) {
844318198Smarius		dp = malloc(len, M_TEMP, M_WAITOK);
845318198Smarius		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
846318198Smarius		if (err != 0)
847318198Smarius			goto out;
848318198Smarius	}
849318198Smarius	memset(&cmd, 0, sizeof(cmd));
850318198Smarius	memset(&data, 0, sizeof(data));
851318198Smarius	cmd.opcode = mic->opcode;
852318198Smarius	cmd.arg = mic->arg;
853318198Smarius	cmd.flags = mic->flags;
854318198Smarius	if (len != 0) {
855318198Smarius		data.len = len;
856318198Smarius		data.data = dp;
857318198Smarius		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
858318198Smarius		    MMC_DATA_READ;
859318198Smarius		cmd.data = &data;
860318198Smarius	}
861318198Smarius	sc = part->sc;
862318198Smarius	rca = sc->rca;
863318198Smarius	if (mic->is_acmd == 0) {
864318198Smarius		/* Enforce/patch/restrict RCA-based commands */
865318198Smarius		switch (cmd.opcode) {
866318198Smarius		case MMC_SET_RELATIVE_ADDR:
867318198Smarius		case MMC_SELECT_CARD:
868318198Smarius			err = EPERM;
869318198Smarius			goto out;
870318198Smarius		case MMC_STOP_TRANSMISSION:
871318198Smarius			if ((cmd.arg & 0x1) == 0)
872318198Smarius				break;
873318198Smarius			/* FALLTHROUGH */
874318198Smarius		case MMC_SLEEP_AWAKE:
875318198Smarius		case MMC_SEND_CSD:
876318198Smarius		case MMC_SEND_CID:
877318198Smarius		case MMC_SEND_STATUS:
878318198Smarius		case MMC_GO_INACTIVE_STATE:
879318198Smarius		case MMC_FAST_IO:
880318198Smarius		case MMC_APP_CMD:
881318198Smarius			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
882318198Smarius			break;
883318198Smarius		default:
884318198Smarius			break;
885318198Smarius		}
886318198Smarius	}
887318198Smarius	dev = sc->dev;
888322120Smarius	mmcbus = sc->mmcbus;
889322120Smarius	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
890322120Smarius	err = mmcsd_switch_part(mmcbus, dev, rca, part->type);
891318198Smarius	if (err != MMC_ERR_NONE)
892318198Smarius		goto release;
893318198Smarius	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
894318198Smarius		err = mmcsd_set_blockcount(sc, mic->blocks,
895318198Smarius		    mic->write_flag & (1 << 31));
896318198Smarius		if (err != MMC_ERR_NONE)
897322120Smarius			goto switch_back;
898318198Smarius	}
899318198Smarius	if (mic->is_acmd != 0)
900322120Smarius		(void)mmc_wait_for_app_cmd(mmcbus, dev, rca, &cmd, 0);
901318198Smarius	else
902322120Smarius		(void)mmc_wait_for_cmd(mmcbus, dev, &cmd, 0);
903318198Smarius	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
904318198Smarius		/*
905318198Smarius		 * If the request went to the RPMB partition, try to ensure
906318198Smarius		 * that the command actually has completed ...
907318198Smarius		 */
908318198Smarius		retries = MMCSD_CMD_RETRIES;
909318198Smarius		do {
910322120Smarius			err = mmc_send_status(mmcbus, dev, rca, &status);
911318198Smarius			if (err != MMC_ERR_NONE)
912318198Smarius				break;
913318198Smarius			if (R1_STATUS(status) == 0 &&
914318198Smarius			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
915318198Smarius				break;
916318198Smarius			DELAY(1000);
917318198Smarius		} while (retries-- > 0);
918318198Smarius
919322120Smariusswitch_back:
920318198Smarius		/* ... and always switch back to the default partition. */
921322120Smarius		err = mmcsd_switch_part(mmcbus, dev, rca,
922318198Smarius		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
923318198Smarius		if (err != MMC_ERR_NONE)
924318198Smarius			goto release;
925318198Smarius	}
926318198Smarius	/*
927318198Smarius	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
928318198Smarius	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
929318198Smarius	 * so retrieve EXT_CSD again.
930318198Smarius	 */
931318198Smarius	if (cmd.opcode == MMC_SWITCH_FUNC) {
932322120Smarius		err = mmc_send_ext_csd(mmcbus, dev, sc->ext_csd);
933318198Smarius		if (err != MMC_ERR_NONE)
934318198Smarius			goto release;
935318198Smarius	}
936322120Smarius	MMCBUS_RELEASE_BUS(mmcbus, dev);
937318198Smarius	if (cmd.error != MMC_ERR_NONE) {
938318198Smarius		switch (cmd.error) {
939318198Smarius		case MMC_ERR_TIMEOUT:
940318198Smarius			err = ETIMEDOUT;
941318198Smarius			break;
942318198Smarius		case MMC_ERR_BADCRC:
943318198Smarius			err = EILSEQ;
944318198Smarius			break;
945318198Smarius		case MMC_ERR_INVALID:
946318198Smarius			err = EINVAL;
947318198Smarius			break;
948318198Smarius		case MMC_ERR_NO_MEMORY:
949318198Smarius			err = ENOMEM;
950318198Smarius			break;
951318198Smarius		default:
952318198Smarius			err = EIO;
953318198Smarius			break;
954318198Smarius		}
955318198Smarius		goto out;
956318198Smarius	}
957318198Smarius	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
958318198Smarius	if (mic->write_flag == 0 && len != 0) {
959318198Smarius		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
960318198Smarius		if (err != 0)
961318198Smarius			goto out;
962318198Smarius	}
963318198Smarius	goto out;
964318198Smarius
965318198Smariusrelease:
966322120Smarius	MMCBUS_RELEASE_BUS(mmcbus, dev);
967318198Smarius	err = EIO;
968318198Smarius
969318198Smariusout:
970322120Smarius	MMCSD_IOCTL_LOCK(part);
971322120Smarius	part->ioctl = 0;
972322120Smarius	MMCSD_IOCTL_UNLOCK(part);
973322120Smarius	wakeup(part);
974318198Smarius	if (dp != NULL)
975318198Smarius		free(dp, M_TEMP);
976318198Smarius	return (err);
977318198Smarius}
978318198Smarius
979318198Smariusstatic int
980318198Smariusmmcsd_getattr(struct bio *bp)
981318198Smarius{
982318198Smarius	struct mmcsd_part *part;
983318198Smarius	device_t dev;
984318198Smarius
985318198Smarius	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
986318198Smarius		if (bp->bio_length != sizeof(dev))
987318198Smarius			return (EFAULT);
988318198Smarius		part = bp->bio_disk->d_drv1;
989318198Smarius		dev = part->sc->dev;
990318198Smarius		bcopy(&dev, bp->bio_data, sizeof(dev));
991318198Smarius		bp->bio_completed = bp->bio_length;
992318198Smarius		return (0);
993318198Smarius	}
994318198Smarius	return (-1);
995318198Smarius}
996318198Smarius
997318198Smariusstatic int
998318198Smariusmmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
999318198Smarius{
1000318198Smarius	struct mmc_command cmd;
1001318198Smarius	struct mmc_request req;
1002318198Smarius
1003318198Smarius	memset(&req, 0, sizeof(req));
1004318198Smarius	memset(&cmd, 0, sizeof(cmd));
1005318198Smarius	cmd.mrq = &req;
1006318198Smarius	req.cmd = &cmd;
1007318198Smarius	cmd.opcode = MMC_SET_BLOCK_COUNT;
1008318198Smarius	cmd.arg = count & 0x0000FFFF;
1009318198Smarius	if (reliable)
1010318198Smarius		cmd.arg |= 1 << 31;
1011318198Smarius	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1012322120Smarius	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbus, sc->dev, &req);
1013318198Smarius	return (cmd.error);
1014318198Smarius}
1015318198Smarius
1016318198Smariusstatic int
1017318198Smariusmmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
1018318198Smarius{
1019318198Smarius	struct mmcsd_softc *sc;
1020318198Smarius	int err;
1021318198Smarius	uint8_t	value;
1022318198Smarius
1023318198Smarius	sc = device_get_softc(dev);
1024318198Smarius
1025322120Smarius	if (sc->mode == mode_sd)
1026318198Smarius		return (MMC_ERR_NONE);
1027318198Smarius
1028322120Smarius	/*
1029322120Smarius	 * According to section "6.2.2 Command restrictions" of the eMMC
1030322120Smarius	 * specification v5.1, CMD19/CMD21 aren't allowed to be used with
1031322120Smarius	 * RPMB partitions.  So we pause re-tuning along with triggering
1032322120Smarius	 * it up-front to decrease the likelihood of re-tuning becoming
1033322120Smarius	 * necessary while accessing an RPMB partition.  Consequently, an
1034322120Smarius	 * RPMB partition should immediately be switched away from again
1035322120Smarius	 * after an access in order to allow for re-tuning to take place
1036322120Smarius	 * anew.
1037322120Smarius	 */
1038322120Smarius	if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1039322120Smarius		MMCBUS_RETUNE_PAUSE(sc->mmcbus, sc->dev, true);
1040322120Smarius
1041322120Smarius	if (sc->part_curr == part)
1042318198Smarius		return (MMC_ERR_NONE);
1043318198Smarius
1044318198Smarius	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
1045318198Smarius	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
1046318198Smarius	/* Jump! */
1047318198Smarius	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
1048318198Smarius	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
1049322120Smarius	if (err != MMC_ERR_NONE) {
1050322120Smarius		if (part == EXT_CSD_PART_CONFIG_ACC_RPMB)
1051322120Smarius			MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
1052318198Smarius		return (err);
1053322120Smarius	}
1054318198Smarius
1055318198Smarius	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
1056322120Smarius	if (sc->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1057322120Smarius		MMCBUS_RETUNE_UNPAUSE(sc->mmcbus, sc->dev);
1058318198Smarius	sc->part_curr = part;
1059318198Smarius	return (MMC_ERR_NONE);
1060318198Smarius}
1061318198Smarius
1062239607Simpstatic const char *
1063239607Simpmmcsd_errmsg(int e)
1064239607Simp{
1065318198Smarius
1066239607Simp	if (e < 0 || e > MMC_ERR_MAX)
1067239607Simp		return "Bad error code";
1068322120Smarius	return (errmsg[e]);
1069239607Simp}
1070239607Simp
1071184033Smavstatic daddr_t
1072318198Smariusmmcsd_rw(struct mmcsd_part *part, struct bio *bp)
1073184033Smav{
1074184033Smav	daddr_t block, end;
1075184033Smav	struct mmc_command cmd;
1076184033Smav	struct mmc_command stop;
1077184033Smav	struct mmc_request req;
1078184033Smav	struct mmc_data data;
1079318198Smarius	struct mmcsd_softc *sc;
1080322120Smarius	device_t dev, mmcbus;
1081322120Smarius	u_int numblocks, sz;
1082318198Smarius	char *vaddr;
1083184033Smav
1084318198Smarius	sc = part->sc;
1085318198Smarius	dev = sc->dev;
1086322120Smarius	mmcbus = sc->mmcbus;
1087318198Smarius
1088184033Smav	block = bp->bio_pblkno;
1089318198Smarius	sz = part->disk->d_sectorsize;
1090184033Smav	end = bp->bio_pblkno + (bp->bio_bcount / sz);
1091184033Smav	while (block < end) {
1092318198Smarius		vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
1093322120Smarius		numblocks = min(end - block, sc->max_data);
1094184033Smav		memset(&req, 0, sizeof(req));
1095318198Smarius		memset(&cmd, 0, sizeof(cmd));
1096184033Smav		memset(&stop, 0, sizeof(stop));
1097254432Sian		memset(&data, 0, sizeof(data));
1098248689Sian		cmd.mrq = &req;
1099184033Smav		req.cmd = &cmd;
1100184033Smav		cmd.data = &data;
1101184033Smav		if (bp->bio_cmd == BIO_READ) {
1102184033Smav			if (numblocks > 1)
1103184033Smav				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1104184033Smav			else
1105184033Smav				cmd.opcode = MMC_READ_SINGLE_BLOCK;
1106184033Smav		} else {
1107184033Smav			if (numblocks > 1)
1108184033Smav				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1109184033Smav			else
1110184033Smav				cmd.opcode = MMC_WRITE_BLOCK;
1111184033Smav		}
1112184033Smav		cmd.arg = block;
1113322120Smarius		if (sc->high_cap == 0)
1114184033Smav			cmd.arg <<= 9;
1115184033Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1116184033Smav		data.data = vaddr;
1117184033Smav		data.mrq = &req;
1118184033Smav		if (bp->bio_cmd == BIO_READ)
1119184033Smav			data.flags = MMC_DATA_READ;
1120184033Smav		else
1121184033Smav			data.flags = MMC_DATA_WRITE;
1122184033Smav		data.len = numblocks * sz;
1123184033Smav		if (numblocks > 1) {
1124184033Smav			data.flags |= MMC_DATA_MULTI;
1125184033Smav			stop.opcode = MMC_STOP_TRANSMISSION;
1126184033Smav			stop.arg = 0;
1127184033Smav			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1128248689Sian			stop.mrq = &req;
1129184033Smav			req.stop = &stop;
1130184033Smav		}
1131322120Smarius		MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1132239607Simp		if (req.cmd->error != MMC_ERR_NONE) {
1133318198Smarius			if (ppsratecheck(&sc->log_time, &sc->log_count,
1134318198Smarius			    LOG_PPS))
1135278687Sian				device_printf(dev, "Error indicated: %d %s\n",
1136318198Smarius				    req.cmd->error,
1137318198Smarius				    mmcsd_errmsg(req.cmd->error));
1138184033Smav			break;
1139239607Simp		}
1140184033Smav		block += numblocks;
1141184033Smav	}
1142184033Smav	return (block);
1143184033Smav}
1144184033Smav
1145184033Smavstatic daddr_t
1146318198Smariusmmcsd_delete(struct mmcsd_part *part, struct bio *bp)
1147184033Smav{
1148184033Smav	daddr_t block, end, start, stop;
1149184033Smav	struct mmc_command cmd;
1150184033Smav	struct mmc_request req;
1151318198Smarius	struct mmcsd_softc *sc;
1152322120Smarius	device_t dev, mmcbus;
1153322120Smarius	u_int erase_sector, sz;
1154184033Smav
1155318198Smarius	sc = part->sc;
1156318198Smarius	dev = sc->dev;
1157322120Smarius	mmcbus = sc->mmcbus;
1158318198Smarius
1159184033Smav	block = bp->bio_pblkno;
1160318198Smarius	sz = part->disk->d_sectorsize;
1161184033Smav	end = bp->bio_pblkno + (bp->bio_bcount / sz);
1162184034Smav	/* Coalesce with part remaining from previous request. */
1163318198Smarius	if (block > part->eblock && block <= part->eend)
1164318198Smarius		block = part->eblock;
1165318198Smarius	if (end >= part->eblock && end < part->eend)
1166318198Smarius		end = part->eend;
1167184033Smav	/* Safe round to the erase sector boundaries. */
1168322120Smarius	erase_sector = sc->erase_sector;
1169184033Smav	start = block + erase_sector - 1;	 /* Round up. */
1170184033Smav	start -= start % erase_sector;
1171184033Smav	stop = end;				/* Round down. */
1172318198Smarius	stop -= end % erase_sector;
1173318198Smarius	/* We can't erase an area smaller than a sector, store it for later. */
1174184034Smav	if (start >= stop) {
1175318198Smarius		part->eblock = block;
1176318198Smarius		part->eend = end;
1177184033Smav		return (end);
1178184034Smav	}
1179184033Smav
1180322120Smarius	/*
1181322120Smarius	 * Pause re-tuning so it won't interfere with the order of erase
1182322120Smarius	 * commands.  Note that these latter don't use the data lines, so
1183322120Smarius	 * re-tuning shouldn't actually become necessary during erase.
1184322120Smarius	 */
1185322120Smarius	MMCBUS_RETUNE_PAUSE(mmcbus, dev, false);
1186184033Smav	/* Set erase start position. */
1187184033Smav	memset(&req, 0, sizeof(req));
1188184033Smav	memset(&cmd, 0, sizeof(cmd));
1189248689Sian	cmd.mrq = &req;
1190184033Smav	req.cmd = &cmd;
1191184033Smav	if (mmc_get_card_type(dev) == mode_sd)
1192184033Smav		cmd.opcode = SD_ERASE_WR_BLK_START;
1193184033Smav	else
1194184033Smav		cmd.opcode = MMC_ERASE_GROUP_START;
1195184033Smav	cmd.arg = start;
1196322120Smarius	if (sc->high_cap == 0)
1197184033Smav		cmd.arg <<= 9;
1198184033Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1199322120Smarius	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1200184033Smav	if (req.cmd->error != MMC_ERR_NONE) {
1201322120Smarius		device_printf(dev, "Setting erase start position failed %d\n",
1202322120Smarius		    req.cmd->error);
1203322120Smarius		block = bp->bio_pblkno;
1204322120Smarius		goto unpause;
1205184033Smav	}
1206184033Smav	/* Set erase stop position. */
1207184033Smav	memset(&req, 0, sizeof(req));
1208184033Smav	memset(&cmd, 0, sizeof(cmd));
1209184033Smav	req.cmd = &cmd;
1210184033Smav	if (mmc_get_card_type(dev) == mode_sd)
1211184033Smav		cmd.opcode = SD_ERASE_WR_BLK_END;
1212184033Smav	else
1213184033Smav		cmd.opcode = MMC_ERASE_GROUP_END;
1214184033Smav	cmd.arg = stop;
1215322120Smarius	if (sc->high_cap == 0)
1216184033Smav		cmd.arg <<= 9;
1217184033Smav	cmd.arg--;
1218184033Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1219322120Smarius	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1220184033Smav	if (req.cmd->error != MMC_ERR_NONE) {
1221322120Smarius		device_printf(dev, "Setting erase stop position failed %d\n",
1222322120Smarius		    req.cmd->error);
1223322120Smarius		block = bp->bio_pblkno;
1224322120Smarius		goto unpause;
1225184033Smav	}
1226184033Smav	/* Erase range. */
1227184033Smav	memset(&req, 0, sizeof(req));
1228184033Smav	memset(&cmd, 0, sizeof(cmd));
1229184033Smav	req.cmd = &cmd;
1230184033Smav	cmd.opcode = MMC_ERASE;
1231184033Smav	cmd.arg = 0;
1232184033Smav	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1233322120Smarius	MMCBUS_WAIT_FOR_REQUEST(mmcbus, dev, &req);
1234184033Smav	if (req.cmd->error != MMC_ERR_NONE) {
1235322120Smarius		device_printf(dev, "erase err3: %d\n", req.cmd->error);
1236322120Smarius		device_printf(dev, "Issuing erase command failed %d\n",
1237322120Smarius		    req.cmd->error);
1238322120Smarius		block = bp->bio_pblkno;
1239322120Smarius		goto unpause;
1240184033Smav	}
1241184034Smav	/* Store one of remaining parts for the next call. */
1242318198Smarius	if (bp->bio_pblkno >= part->eblock || block == start) {
1243318198Smarius		part->eblock = stop;	/* Predict next forward. */
1244318198Smarius		part->eend = end;
1245184034Smav	} else {
1246318198Smarius		part->eblock = block;	/* Predict next backward. */
1247318198Smarius		part->eend = start;
1248184034Smav	}
1249322120Smarius	block = end;
1250322120Smariusunpause:
1251322120Smarius	MMCBUS_RETUNE_UNPAUSE(mmcbus, dev);
1252322120Smarius	return (block);
1253184033Smav}
1254184033Smav
1255188725Smavstatic int
1256318198Smariusmmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1257318198Smarius    size_t length)
1258188725Smav{
1259188725Smav	struct bio bp;
1260188725Smav	daddr_t block, end;
1261318198Smarius	struct disk *disk;
1262318198Smarius	struct mmcsd_softc *sc;
1263318198Smarius	struct mmcsd_part *part;
1264322120Smarius	device_t dev, mmcbus;
1265318198Smarius	int err;
1266188725Smav
1267188725Smav	/* length zero is special and really means flush buffers to media */
1268188725Smav	if (!length)
1269188725Smav		return (0);
1270188725Smav
1271318198Smarius	disk = arg;
1272318198Smarius	part = disk->d_drv1;
1273318198Smarius	sc = part->sc;
1274318198Smarius	dev = sc->dev;
1275322120Smarius	mmcbus = sc->mmcbus;
1276318198Smarius
1277188725Smav	bzero(&bp, sizeof(struct bio));
1278188725Smav	bp.bio_disk = disk;
1279188725Smav	bp.bio_pblkno = offset / disk->d_sectorsize;
1280188725Smav	bp.bio_bcount = length;
1281188725Smav	bp.bio_data = virtual;
1282188725Smav	bp.bio_cmd = BIO_WRITE;
1283318198Smarius	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
1284322120Smarius	MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1285322120Smarius	err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
1286318198Smarius	if (err != MMC_ERR_NONE) {
1287318198Smarius		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
1288318198Smarius			device_printf(dev, "Partition switch error\n");
1289322120Smarius		MMCBUS_RELEASE_BUS(mmcbus, dev);
1290318198Smarius		return (EIO);
1291318198Smarius	}
1292318198Smarius	block = mmcsd_rw(part, &bp);
1293322120Smarius	MMCBUS_RELEASE_BUS(mmcbus, dev);
1294188725Smav	return ((end < block) ? EIO : 0);
1295188725Smav}
1296188725Smav
1297163516Simpstatic void
1298163516Simpmmcsd_task(void *arg)
1299163516Simp{
1300318198Smarius	daddr_t block, end;
1301318198Smarius	struct mmcsd_part *part;
1302318198Smarius	struct mmcsd_softc *sc;
1303163516Simp	struct bio *bp;
1304322120Smarius	device_t dev, mmcbus;
1305318198Smarius	int err, sz;
1306163516Simp
1307318198Smarius	part = arg;
1308318198Smarius	sc = part->sc;
1309318198Smarius	dev = sc->dev;
1310322120Smarius	mmcbus = sc->mmcbus;
1311318198Smarius
1312185201Smav	while (1) {
1313322120Smarius		MMCSD_DISK_LOCK(part);
1314163516Simp		do {
1315318198Smarius			if (part->running == 0)
1316185201Smav				goto out;
1317318198Smarius			bp = bioq_takefirst(&part->bio_queue);
1318163516Simp			if (bp == NULL)
1319322120Smarius				msleep(part, &part->disk_mtx, PRIBIO,
1320322120Smarius				    "mmcsd disk jobqueue", 0);
1321185201Smav		} while (bp == NULL);
1322322120Smarius		MMCSD_DISK_UNLOCK(part);
1323318198Smarius		if (bp->bio_cmd != BIO_READ && part->ro) {
1324183448Simp			bp->bio_error = EROFS;
1325183448Simp			bp->bio_resid = bp->bio_bcount;
1326183448Simp			bp->bio_flags |= BIO_ERROR;
1327183448Simp			biodone(bp);
1328183448Simp			continue;
1329183448Simp		}
1330322120Smarius		MMCBUS_ACQUIRE_BUS(mmcbus, dev);
1331318198Smarius		sz = part->disk->d_sectorsize;
1332184033Smav		block = bp->bio_pblkno;
1333163516Simp		end = bp->bio_pblkno + (bp->bio_bcount / sz);
1334322120Smarius		err = mmcsd_switch_part(mmcbus, dev, sc->rca, part->type);
1335318198Smarius		if (err != MMC_ERR_NONE) {
1336318198Smarius			if (ppsratecheck(&sc->log_time, &sc->log_count,
1337318198Smarius			    LOG_PPS))
1338318198Smarius				device_printf(dev, "Partition switch error\n");
1339318198Smarius			goto release;
1340318198Smarius		}
1341184033Smav		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1342184034Smav			/* Access to the remaining erase block obsoletes it. */
1343318198Smarius			if (block < part->eend && end > part->eblock)
1344318198Smarius				part->eblock = part->eend = 0;
1345318198Smarius			block = mmcsd_rw(part, bp);
1346184033Smav		} else if (bp->bio_cmd == BIO_DELETE) {
1347318198Smarius			block = mmcsd_delete(part, bp);
1348163516Simp		}
1349318198Smariusrelease:
1350322120Smarius		MMCBUS_RELEASE_BUS(mmcbus, dev);
1351183480Simp		if (block < end) {
1352183480Simp			bp->bio_error = EIO;
1353183480Simp			bp->bio_resid = (end - block) * sz;
1354183480Simp			bp->bio_flags |= BIO_ERROR;
1355312400Smarius		} else {
1356312400Smarius			bp->bio_resid = 0;
1357183480Simp		}
1358163516Simp		biodone(bp);
1359163516Simp	}
1360185201Smavout:
1361169567Simp	/* tell parent we're done */
1362318198Smarius	part->running = -1;
1363322120Smarius	MMCSD_DISK_UNLOCK(part);
1364318198Smarius	wakeup(part);
1365169567Simp
1366172836Sjulian	kproc_exit(0);
1367163516Simp}
1368163516Simp
1369183774Simpstatic int
1370183774Simpmmcsd_bus_bit_width(device_t dev)
1371183774Simp{
1372236491Smarius
1373183774Simp	if (mmc_get_bus_width(dev) == bus_width_1)
1374183774Simp		return (1);
1375183774Simp	if (mmc_get_bus_width(dev) == bus_width_4)
1376183774Simp		return (4);
1377183774Simp	return (8);
1378183774Simp}
1379183774Simp
1380163516Simpstatic device_method_t mmcsd_methods[] = {
1381163516Simp	DEVMETHOD(device_probe, mmcsd_probe),
1382163516Simp	DEVMETHOD(device_attach, mmcsd_attach),
1383163516Simp	DEVMETHOD(device_detach, mmcsd_detach),
1384185721Smav	DEVMETHOD(device_suspend, mmcsd_suspend),
1385185721Smav	DEVMETHOD(device_resume, mmcsd_resume),
1386234524Smarius	DEVMETHOD_END
1387163516Simp};
1388163516Simp
1389163516Simpstatic driver_t mmcsd_driver = {
1390163516Simp	"mmcsd",
1391163516Simp	mmcsd_methods,
1392163516Simp	sizeof(struct mmcsd_softc),
1393163516Simp};
1394163516Simpstatic devclass_t mmcsd_devclass;
1395163516Simp
1396318198Smariusstatic int
1397318198Smariusmmcsd_handler(module_t mod __unused, int what, void *arg __unused)
1398318198Smarius{
1399318198Smarius
1400318198Smarius	switch (what) {
1401318198Smarius	case MOD_LOAD:
1402318198Smarius		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
1403318198Smarius		    TRUE);
1404318198Smarius		return (0);
1405318198Smarius	case MOD_UNLOAD:
1406318198Smarius		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
1407318198Smarius		return (0);
1408318198Smarius	}
1409318198Smarius	return (0);
1410318198Smarius}
1411318198Smarius
1412318198SmariusDRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
1413318198SmariusMODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
1414318198SmariusMMC_DEPEND(mmcsd);
1415