xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 72dec0792a09bb5f03d341642657bd6115d99c9e)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4*72dec079SMarius 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>
62*72dec079SMarius Strobl #include <sys/fcntl.h>
63*72dec079SMarius 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>
70*72dec079SMarius Strobl #include <sys/slicer.h>
715e3dbf8bSIan Lepore #include <sys/time.h>
72*72dec079SMarius Strobl 
73c55f5707SWarner Losh #include <geom/geom.h>
74114b4164SWarner Losh #include <geom/geom_disk.h>
75114b4164SWarner Losh 
76*72dec079SMarius Strobl #include <dev/mmc/bridge.h>
77*72dec079SMarius Strobl #include <dev/mmc/mmc_ioctl.h>
78*72dec079SMarius 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 
90*72dec079SMarius Strobl #define	MMCSD_CMD_RETRIES	5
91*72dec079SMarius Strobl 
92*72dec079SMarius Strobl #define	MMCSD_FMT_BOOT		"mmcsd%dboot"
93*72dec079SMarius Strobl #define	MMCSD_FMT_GP		"mmcsd%dgp"
94*72dec079SMarius Strobl #define	MMCSD_FMT_RPMB		"mmcsd%drpmb"
95*72dec079SMarius Strobl #define	MMCSD_LABEL_ENH		"enh"
96*72dec079SMarius Strobl 
97*72dec079SMarius Strobl #define	MMCSD_PART_NAMELEN	(16 + 1)
98*72dec079SMarius Strobl 
99*72dec079SMarius Strobl struct mmcsd_softc;
100*72dec079SMarius Strobl 
101*72dec079SMarius Strobl struct mmcsd_part {
102*72dec079SMarius Strobl 	struct mtx part_mtx;
103*72dec079SMarius 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. */
108*72dec079SMarius Strobl 	u_int cnt;
109*72dec079SMarius Strobl 	u_int type;
1100e1466acSWarner Losh 	int running;
111eb67f31aSAlexander Motin 	int suspend;
112*72dec079SMarius Strobl 	bool ro;
113*72dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
114*72dec079SMarius Strobl };
115*72dec079SMarius Strobl 
116*72dec079SMarius Strobl struct mmcsd_softc {
117*72dec079SMarius Strobl 	device_t dev;
118*72dec079SMarius Strobl 	device_t mmcbr;
119*72dec079SMarius Strobl 	struct mmcsd_part *part[MMC_PART_MAX];
120*72dec079SMarius Strobl 	enum mmc_card_mode mode;
121*72dec079SMarius Strobl 	uint8_t part_curr;	/* Partition currently switched to */
122*72dec079SMarius Strobl 	uint8_t ext_csd[MMC_EXTCSD_SIZE];
123*72dec079SMarius Strobl 	uint16_t rca;
124*72dec079SMarius Strobl 	uint32_t part_time;	/* Partition switch timeout [us] */
125*72dec079SMarius Strobl 	off_t enh_base;		/* Enhanced user data area slice base ... */
126*72dec079SMarius Strobl 	off_t enh_size;		/* ... and size [bytes] */
1275e3dbf8bSIan Lepore 	int log_count;
1285e3dbf8bSIan Lepore 	struct timeval log_time;
129*72dec079SMarius 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);
154*72dec079SMarius Strobl static int mmcsd_getattr(struct bio *);
155*72dec079SMarius Strobl static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
156*72dec079SMarius 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 
161*72dec079SMarius Strobl /* RMPB cdev interface */
162*72dec079SMarius Strobl static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
163*72dec079SMarius Strobl     int fflag, struct thread *td);
164a0075e58SWarner Losh 
165*72dec079SMarius Strobl static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
166*72dec079SMarius Strobl     const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
167*72dec079SMarius Strobl static int mmcsd_bus_bit_width(device_t dev);
168*72dec079SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
169*72dec079SMarius Strobl static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
170*72dec079SMarius Strobl     int fflag);
171*72dec079SMarius Strobl static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
172*72dec079SMarius Strobl     int fflag);
173*72dec079SMarius Strobl static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
174*72dec079SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
175*72dec079SMarius Strobl static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
176*72dec079SMarius Strobl static int mmcsd_slicer(device_t dev, const char *provider,
177*72dec079SMarius Strobl     struct flash_slice *slices, int *nslices);
178*72dec079SMarius Strobl static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
179*72dec079SMarius Strobl     u_int part);
180*72dec079SMarius Strobl 
181*72dec079SMarius Strobl #define	MMCSD_PART_LOCK(_part)		mtx_lock(&(_part)->part_mtx)
182*72dec079SMarius Strobl #define	MMCSD_PART_UNLOCK(_part)	mtx_unlock(&(_part)->part_mtx)
183*72dec079SMarius Strobl #define	MMCSD_PART_LOCK_INIT(_part)					\
184*72dec079SMarius Strobl 	mtx_init(&(_part)->part_mtx, (_part)->name, "mmcsd part", MTX_DEF)
185*72dec079SMarius Strobl #define	MMCSD_PART_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->part_mtx);
186*72dec079SMarius Strobl #define	MMCSD_PART_ASSERT_LOCKED(_part)					\
187*72dec079SMarius Strobl 	mtx_assert(&(_part)->part_mtx, MA_OWNED);
188*72dec079SMarius Strobl #define	MMCSD_PART_ASSERT_UNLOCKED(_part)				\
189*72dec079SMarius 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 {
203*72dec079SMarius Strobl 	device_t mmcbr;
204114b4164SWarner Losh 	struct mmcsd_softc *sc;
205*72dec079SMarius Strobl 	const uint8_t *ext_csd;
206*72dec079SMarius Strobl 	off_t erase_size, sector_size, size, wp_size;
207*72dec079SMarius Strobl 	uintmax_t bytes;
208*72dec079SMarius Strobl 	int err, i;
209*72dec079SMarius Strobl 	uint8_t rev;
210*72dec079SMarius Strobl 	bool comp, ro;
211*72dec079SMarius Strobl 	char unit[2];
212114b4164SWarner Losh 
213114b4164SWarner Losh 	sc = device_get_softc(dev);
214114b4164SWarner Losh 	sc->dev = dev;
215*72dec079SMarius Strobl 	sc->mmcbr = mmcbr = device_get_parent(dev);
216*72dec079SMarius Strobl 	sc->mode = mmcbr_get_mode(mmcbr);
217*72dec079SMarius Strobl 	sc->rca = mmc_get_rca(dev);
218114b4164SWarner Losh 
219*72dec079SMarius Strobl 	/* Only MMC >= 4.x devices support EXT_CSD. */
220*72dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) >= 4) {
221*72dec079SMarius Strobl 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
222*72dec079SMarius Strobl 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
223*72dec079SMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbr, dev);
224*72dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
225*72dec079SMarius Strobl 			bzero(sc->ext_csd, sizeof(sc->ext_csd));
226*72dec079SMarius Strobl 	}
227*72dec079SMarius Strobl 	ext_csd = sc->ext_csd;
228*72dec079SMarius Strobl 
229*72dec079SMarius Strobl 	/*
230*72dec079SMarius Strobl 	 * Enhanced user data area and general purpose partitions are only
231*72dec079SMarius Strobl 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
232*72dec079SMarius Strobl 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
233*72dec079SMarius Strobl 	 */
234*72dec079SMarius Strobl 	rev = ext_csd[EXT_CSD_REV];
235*72dec079SMarius Strobl 
236*72dec079SMarius Strobl 	/*
237*72dec079SMarius Strobl 	 * Ignore user-creatable enhanced user data area and general purpose
238*72dec079SMarius Strobl 	 * partitions partitions as long as partitioning hasn't been finished.
239*72dec079SMarius Strobl 	 */
240*72dec079SMarius Strobl 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
241*72dec079SMarius Strobl 
242*72dec079SMarius Strobl 	/*
243*72dec079SMarius Strobl 	 * Add enhanced user data area slice, unless it spans the entirety of
244*72dec079SMarius Strobl 	 * the user data area.  The enhanced area is of a multiple of high
245*72dec079SMarius Strobl 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
246*72dec079SMarius Strobl 	 * 512 KB) and its offset given in either sectors or bytes, depending
247*72dec079SMarius Strobl 	 * on whether it's a high capacity device or not.
248*72dec079SMarius Strobl 	 * NB: The slicer and its slices need to be registered before adding
249*72dec079SMarius Strobl 	 *     the disk for the corresponding user data area as re-tasting is
250*72dec079SMarius Strobl 	 *     racy.
251*72dec079SMarius Strobl 	 */
252*72dec079SMarius Strobl 	sector_size = mmc_get_sector_size(dev);
253*72dec079SMarius Strobl 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
254*72dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
255*72dec079SMarius Strobl 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
256*72dec079SMarius Strobl 	if (rev >= 4 && comp == TRUE && size > 0 &&
257*72dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
258*72dec079SMarius Strobl 	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
259*72dec079SMarius Strobl 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
260*72dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
261*72dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
262*72dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
263*72dec079SMarius Strobl 		size *= erase_size * wp_size;
264*72dec079SMarius Strobl 		if (size != mmc_get_media_size(dev) * sector_size) {
265*72dec079SMarius Strobl 			sc->enh_size = size;
266*72dec079SMarius Strobl 			sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
267*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
268*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
269*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
270*72dec079SMarius Strobl 			    (mmc_get_high_cap(dev) ? MMC_SECTOR_SIZE : 1);
271*72dec079SMarius Strobl 		} else if (bootverbose)
272*72dec079SMarius Strobl 			device_printf(dev,
273*72dec079SMarius Strobl 			    "enhanced user data area spans entire device\n");
274*72dec079SMarius Strobl 	}
275*72dec079SMarius Strobl 
276*72dec079SMarius Strobl 	/*
277*72dec079SMarius Strobl 	 * Add default partition.  This may be the only one or the user
278*72dec079SMarius Strobl 	 * data area in case partitions are supported.
279*72dec079SMarius Strobl 	 */
280*72dec079SMarius Strobl 	ro = mmc_get_read_only(dev);
281*72dec079SMarius Strobl 	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
282*72dec079SMarius Strobl 	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
283*72dec079SMarius Strobl 	    mmc_get_erase_sector(dev) * sector_size, ro);
284*72dec079SMarius Strobl 
285*72dec079SMarius Strobl 	if (mmc_get_spec_vers(dev) < 3)
286*72dec079SMarius Strobl 		return (0);
287*72dec079SMarius Strobl 
288*72dec079SMarius Strobl 	/* Belatedly announce enhanced user data slice. */
289*72dec079SMarius Strobl 	if (sc->enh_size != 0) {
290*72dec079SMarius Strobl 		bytes = mmcsd_pretty_size(size, unit);
291*72dec079SMarius Strobl 		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
292*72dec079SMarius Strobl 		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
293*72dec079SMarius Strobl 		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
294*72dec079SMarius Strobl 		    device_get_nameunit(dev));
295*72dec079SMarius Strobl 	}
296*72dec079SMarius Strobl 
297*72dec079SMarius Strobl 	/*
298*72dec079SMarius Strobl 	 * Determine partition switch timeout (provided in units of 10 ms)
299*72dec079SMarius Strobl 	 * and ensure it's at least 300 ms as some eMMC chips lie.
300*72dec079SMarius Strobl 	 */
301*72dec079SMarius Strobl 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
302*72dec079SMarius Strobl 	    300 * 1000);
303*72dec079SMarius Strobl 
304*72dec079SMarius Strobl 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
305*72dec079SMarius Strobl 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
306*72dec079SMarius Strobl 	if (size > 0 && (mmcbr_get_caps(mmcbr) & MMC_CAP_BOOT_NOACC) == 0) {
307*72dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
308*72dec079SMarius Strobl 		    MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
309*72dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
310*72dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
311*72dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
312*72dec079SMarius Strobl 		    MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
313*72dec079SMarius Strobl 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
314*72dec079SMarius Strobl 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
315*72dec079SMarius Strobl 	}
316*72dec079SMarius Strobl 
317*72dec079SMarius Strobl 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
318*72dec079SMarius Strobl 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
319*72dec079SMarius Strobl 	if (rev >= 5 && size > 0)
320*72dec079SMarius Strobl 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
321*72dec079SMarius Strobl 		    MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
322*72dec079SMarius Strobl 
323*72dec079SMarius Strobl 	if (rev <= 3 || comp == FALSE)
324*72dec079SMarius Strobl 		return (0);
325*72dec079SMarius Strobl 
326*72dec079SMarius Strobl 	/*
327*72dec079SMarius Strobl 	 * Add general purpose partitions, which are of a multiple of high
328*72dec079SMarius Strobl 	 * capacity write protect groups, too.
329*72dec079SMarius Strobl 	 */
330*72dec079SMarius Strobl 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
331*72dec079SMarius Strobl 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
332*72dec079SMarius Strobl 		    MMC_SECTOR_SIZE;
333*72dec079SMarius Strobl 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
334*72dec079SMarius Strobl 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
335*72dec079SMarius Strobl 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
336*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
337*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
338*72dec079SMarius Strobl 			if (size == 0)
339*72dec079SMarius Strobl 				continue;
340*72dec079SMarius Strobl 			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
341*72dec079SMarius Strobl 			    MMCSD_FMT_GP, i, size * erase_size * wp_size,
342*72dec079SMarius Strobl 			    erase_size, ro);
343*72dec079SMarius Strobl 		}
344*72dec079SMarius Strobl 	}
345*72dec079SMarius Strobl 	return (0);
346*72dec079SMarius Strobl }
347*72dec079SMarius Strobl 
348*72dec079SMarius Strobl static uintmax_t
349*72dec079SMarius Strobl mmcsd_pretty_size(off_t size, char *unit)
350*72dec079SMarius Strobl {
351*72dec079SMarius Strobl 	uintmax_t bytes;
352*72dec079SMarius Strobl 	int i;
353*72dec079SMarius Strobl 
354*72dec079SMarius Strobl 	/*
355*72dec079SMarius Strobl 	 * Display in most natural units.  There's no card < 1MB.  However,
356*72dec079SMarius Strobl 	 * RPMB partitions occasionally are smaller than that, though.  The
357*72dec079SMarius Strobl 	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
358*72dec079SMarius Strobl 	 * format supports up to 4 GiB and some card makers push it up to this
359*72dec079SMarius Strobl 	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
360*72dec079SMarius Strobl 	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
361*72dec079SMarius Strobl 	 * so we note it in passing here and don't add the code to print TB).
362*72dec079SMarius Strobl 	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
363*72dec079SMarius Strobl 	 * report them like that.  We also round to the nearest unit, since
364*72dec079SMarius Strobl 	 * many cards are a few percent short, even of the power of 10 size.
365*72dec079SMarius Strobl 	 */
366*72dec079SMarius Strobl 	bytes = size;
367*72dec079SMarius Strobl 	unit[0] = unit[1] = '\0';
368*72dec079SMarius Strobl 	for (i = 0; i <= 2 && bytes >= 1000; i++) {
369*72dec079SMarius Strobl 		bytes = (bytes + 1000 / 2 - 1) / 1000;
370*72dec079SMarius Strobl 		switch (i) {
371*72dec079SMarius Strobl 		case 0:
372*72dec079SMarius Strobl 			unit[0] = 'k';
373*72dec079SMarius Strobl 			break;
374*72dec079SMarius Strobl 		case 1:
375*72dec079SMarius Strobl 			unit[0] = 'M';
376*72dec079SMarius Strobl 			break;
377*72dec079SMarius Strobl 		case 2:
378*72dec079SMarius Strobl 			unit[0] = 'G';
379*72dec079SMarius Strobl 			break;
380*72dec079SMarius Strobl 		default:
381*72dec079SMarius Strobl 			break;
382*72dec079SMarius Strobl 		}
383*72dec079SMarius Strobl 	}
384*72dec079SMarius Strobl 	return (bytes);
385*72dec079SMarius Strobl }
386*72dec079SMarius Strobl 
387*72dec079SMarius Strobl static struct cdevsw mmcsd_rpmb_cdevsw = {
388*72dec079SMarius Strobl 	.d_version	= D_VERSION,
389*72dec079SMarius Strobl 	.d_name		= "mmcsdrpmb",
390*72dec079SMarius Strobl 	.d_ioctl	= mmcsd_ioctl_rpmb
391*72dec079SMarius Strobl };
392*72dec079SMarius Strobl 
393*72dec079SMarius Strobl static void
394*72dec079SMarius Strobl mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
395*72dec079SMarius Strobl     off_t media_size, off_t erase_size, bool ro)
396*72dec079SMarius Strobl {
397*72dec079SMarius Strobl 	struct make_dev_args args;
398*72dec079SMarius Strobl 	device_t dev, mmcbr;
399*72dec079SMarius Strobl 	const char *ext;
400*72dec079SMarius Strobl 	const uint8_t *ext_csd;
401*72dec079SMarius Strobl 	struct mmcsd_part *part;
402*72dec079SMarius Strobl 	struct disk *d;
403*72dec079SMarius Strobl 	uintmax_t bytes;
404*72dec079SMarius Strobl 	u_int gp;
405*72dec079SMarius Strobl 	uint32_t speed;
406*72dec079SMarius Strobl 	uint8_t extattr;
407*72dec079SMarius Strobl 	bool enh;
408*72dec079SMarius Strobl 	char unit[2];
409*72dec079SMarius Strobl 
410*72dec079SMarius Strobl 	dev = sc->dev;
411*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
412*72dec079SMarius Strobl 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
413*72dec079SMarius Strobl 	    M_WAITOK | M_ZERO);
414*72dec079SMarius Strobl 	part->sc = sc;
415*72dec079SMarius Strobl 	part->cnt = cnt;
416*72dec079SMarius Strobl 	part->type = type;
417*72dec079SMarius Strobl 	part->ro = ro;
418*72dec079SMarius Strobl 	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
419*72dec079SMarius Strobl 
420*72dec079SMarius Strobl 	/* For the RPMB partition, allow IOCTL access only. */
421*72dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
422*72dec079SMarius Strobl 		make_dev_args_init(&args);
423*72dec079SMarius Strobl 		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
424*72dec079SMarius Strobl 		args.mda_devsw = &mmcsd_rpmb_cdevsw;
425*72dec079SMarius Strobl 		args.mda_uid = UID_ROOT;
426*72dec079SMarius Strobl 		args.mda_gid = GID_OPERATOR;
427*72dec079SMarius Strobl 		args.mda_mode = 0640;
428*72dec079SMarius Strobl 		args.mda_si_drv1 = part;
429*72dec079SMarius Strobl 		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
430*72dec079SMarius Strobl 			device_printf(dev, "Failed to make RPMB device\n");
431*72dec079SMarius Strobl 			free(part, M_DEVBUF);
432*72dec079SMarius Strobl 			return;
433*72dec079SMarius Strobl 		}
434*72dec079SMarius Strobl 	} else {
435*72dec079SMarius Strobl 		MMCSD_PART_LOCK_INIT(part);
436*72dec079SMarius Strobl 
437*72dec079SMarius 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;
441*72dec079SMarius Strobl 		d->d_ioctl = mmcsd_ioctl_disk;
442a1fda318SAlexander Motin 		d->d_dump = mmcsd_dump;
443*72dec079SMarius Strobl 		d->d_getattr = mmcsd_getattr;
444*72dec079SMarius Strobl 		d->d_name = part->name;
445*72dec079SMarius 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;
448*72dec079SMarius Strobl 		d->d_mediasize = media_size;
449*72dec079SMarius Strobl 		d->d_stripesize = erase_size;
450*72dec079SMarius Strobl 		d->d_unit = cnt;
4513906d42dSAlexander Motin 		d->d_flags = DISKFLAG_CANDELETE;
452*72dec079SMarius Strobl 		d->d_delmaxsize = erase_size;
453*72dec079SMarius Strobl 		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
454*72dec079SMarius Strobl 		    sizeof(d->d_ident));
455*72dec079SMarius Strobl 		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
456*72dec079SMarius Strobl 		    sizeof(d->d_descr));
45717160457SAlexander Motin 		d->d_rotation_rate = DISK_RR_NON_ROTATING;
458f3a4b7f7SIan Lepore 
459a0075e58SWarner Losh 		disk_create(d, DISK_VERSION);
460*72dec079SMarius Strobl 		bioq_init(&part->bio_queue);
4610e1466acSWarner Losh 
462*72dec079SMarius Strobl 		part->running = 1;
463*72dec079SMarius Strobl 		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
464*72dec079SMarius Strobl 		    "%s%d: mmc/sd card", part->name, cnt);
465*72dec079SMarius Strobl 	}
466*72dec079SMarius Strobl 
467*72dec079SMarius Strobl 	bytes = mmcsd_pretty_size(media_size, unit);
468*72dec079SMarius Strobl 	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
469*72dec079SMarius Strobl 		speed = mmcbr_get_clock(mmcbr);
470*72dec079SMarius Strobl 		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
471*72dec079SMarius Strobl 		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
472*72dec079SMarius Strobl 		    ro ? " (read-only)" : "", device_get_nameunit(mmcbr),
473*72dec079SMarius Strobl 		    speed / 1000000, (speed / 100000) % 10,
474*72dec079SMarius Strobl 		    mmcsd_bus_bit_width(dev), mmc_get_max_data(dev));
475*72dec079SMarius Strobl 	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
476*72dec079SMarius Strobl 		printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
477*72dec079SMarius Strobl 		    unit, type, ro ? " (read-only)" : "",
47888e07d92SRui Paulo 		    device_get_nameunit(dev));
479*72dec079SMarius Strobl 	} else {
480*72dec079SMarius Strobl 		enh = false;
481*72dec079SMarius Strobl 		ext = NULL;
482*72dec079SMarius Strobl 		extattr = 0;
483*72dec079SMarius Strobl 		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
484*72dec079SMarius Strobl 		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
485*72dec079SMarius Strobl 			ext_csd = sc->ext_csd;
486*72dec079SMarius Strobl 			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
487*72dec079SMarius Strobl 			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
488*72dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
489*72dec079SMarius Strobl 			    (ext_csd[EXT_CSD_PART_ATTR] &
490*72dec079SMarius Strobl 			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
491*72dec079SMarius Strobl 				enh = true;
492*72dec079SMarius Strobl 			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
493*72dec079SMarius Strobl 			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
494*72dec079SMarius Strobl 				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
495*72dec079SMarius Strobl 				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
496*72dec079SMarius Strobl 				switch (extattr) {
497*72dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
498*72dec079SMarius Strobl 						break;
499*72dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
500*72dec079SMarius Strobl 						ext = "system code";
501*72dec079SMarius Strobl 						break;
502*72dec079SMarius Strobl 					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
503*72dec079SMarius Strobl 						ext = "non-persistent";
504*72dec079SMarius Strobl 						break;
505*72dec079SMarius Strobl 					default:
506*72dec079SMarius Strobl 						ext = "reserved";
507*72dec079SMarius Strobl 						break;
508*72dec079SMarius Strobl 				}
509*72dec079SMarius Strobl 			}
510*72dec079SMarius Strobl 		}
511*72dec079SMarius Strobl 		if (ext == NULL)
512*72dec079SMarius Strobl 			printf("%s%d: %ju%sB partion %d%s%s at %s\n",
513*72dec079SMarius Strobl 			    part->name, cnt, bytes, unit, type, enh ?
514*72dec079SMarius Strobl 			    " enhanced" : "", ro ? " (read-only)" : "",
515*72dec079SMarius Strobl 			    device_get_nameunit(dev));
516*72dec079SMarius Strobl 		else
517*72dec079SMarius Strobl 			printf("%s%d: %ju%sB partion %d extended 0x%x "
518*72dec079SMarius Strobl 			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
519*72dec079SMarius Strobl 			    type, extattr, ext, ro ? " (read-only)" : "",
520*72dec079SMarius Strobl 			    device_get_nameunit(dev));
521*72dec079SMarius Strobl 	}
522*72dec079SMarius Strobl }
523114b4164SWarner Losh 
524*72dec079SMarius Strobl static int
525*72dec079SMarius Strobl mmcsd_slicer(device_t dev, const char *provider,
526*72dec079SMarius Strobl     struct flash_slice *slices, int *nslices)
527*72dec079SMarius Strobl {
528*72dec079SMarius Strobl 	char name[MMCSD_PART_NAMELEN];
529*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
530*72dec079SMarius Strobl 	struct mmcsd_part *part;
531*72dec079SMarius Strobl 
532*72dec079SMarius Strobl 	*nslices = 0;
533*72dec079SMarius Strobl 	if (slices == NULL)
534*72dec079SMarius Strobl 		return (ENOMEM);
535*72dec079SMarius Strobl 
536*72dec079SMarius Strobl 	sc = device_get_softc(dev);
537*72dec079SMarius Strobl 	if (sc->enh_size == 0)
538*72dec079SMarius Strobl 		return (ENXIO);
539*72dec079SMarius Strobl 
540*72dec079SMarius Strobl 	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
541*72dec079SMarius Strobl 	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
542*72dec079SMarius Strobl 	    part->disk->d_unit);
543*72dec079SMarius Strobl 	if (strcmp(name, provider) != 0)
544*72dec079SMarius Strobl 		return (ENXIO);
545*72dec079SMarius Strobl 
546*72dec079SMarius Strobl 	*nslices = 1;
547*72dec079SMarius Strobl 	slices[0].base = sc->enh_base;
548*72dec079SMarius Strobl 	slices[0].size = sc->enh_size;
549*72dec079SMarius 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);
557*72dec079SMarius Strobl 	struct mmcsd_part *part;
558*72dec079SMarius Strobl 	int i;
5590e1466acSWarner Losh 
560*72dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
561*72dec079SMarius Strobl 		part = sc->part[i];
562*72dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
563*72dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
564*72dec079SMarius Strobl 			part->suspend = 0;
565*72dec079SMarius Strobl 			if (part->running > 0) {
566eb67f31aSAlexander Motin 				/* kill thread */
567*72dec079SMarius Strobl 				part->running = 0;
568*72dec079SMarius Strobl 				wakeup(part);
569eb67f31aSAlexander Motin 				/* wait for thread to finish. */
570*72dec079SMarius Strobl 				while (part->running != -1)
571*72dec079SMarius Strobl 					msleep(part, &part->part_mtx, 0,
572*72dec079SMarius Strobl 					    "detach", 0);
573eb67f31aSAlexander Motin 			}
574*72dec079SMarius Strobl 			MMCSD_PART_UNLOCK(part);
575*72dec079SMarius Strobl 		}
576*72dec079SMarius Strobl 	}
5770e1466acSWarner Losh 
578*72dec079SMarius Strobl 	if (sc->rpmb_dev != NULL)
579*72dec079SMarius Strobl 		destroy_dev(sc->rpmb_dev);
580*72dec079SMarius Strobl 
581*72dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
582*72dec079SMarius Strobl 		part = sc->part[i];
583*72dec079SMarius Strobl 		if (part != NULL) {
584*72dec079SMarius Strobl 			if (part->disk != NULL) {
585249b0e85SAlexander Motin 				/* Flush the request queue. */
586*72dec079SMarius Strobl 				bioq_flush(&part->bio_queue, NULL, ENXIO);
5870e1466acSWarner Losh 				/* kill disk */
588*72dec079SMarius Strobl 				disk_destroy(part->disk);
5890e1466acSWarner Losh 
590*72dec079SMarius Strobl 				MMCSD_PART_LOCK_DESTROY(part);
591*72dec079SMarius Strobl 			}
592*72dec079SMarius Strobl 			free(part, M_DEVBUF);
593*72dec079SMarius Strobl 		}
594*72dec079SMarius 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);
602*72dec079SMarius Strobl 	struct mmcsd_part *part;
603*72dec079SMarius Strobl 	int i;
604eb67f31aSAlexander Motin 
605*72dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
606*72dec079SMarius Strobl 		part = sc->part[i];
607*72dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
608*72dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
609*72dec079SMarius Strobl 			part->suspend = 1;
610*72dec079SMarius Strobl 			if (part->running > 0) {
611eb67f31aSAlexander Motin 				/* kill thread */
612*72dec079SMarius Strobl 				part->running = 0;
613*72dec079SMarius Strobl 				wakeup(part);
614eb67f31aSAlexander Motin 				/* wait for thread to finish. */
615*72dec079SMarius Strobl 				while (part->running != -1)
616*72dec079SMarius Strobl 					msleep(part, &part->part_mtx, 0,
617*72dec079SMarius Strobl 					    "detach", 0);
618eb67f31aSAlexander Motin 			}
619*72dec079SMarius Strobl 			MMCSD_PART_UNLOCK(part);
620*72dec079SMarius Strobl 		}
621*72dec079SMarius 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);
629*72dec079SMarius Strobl 	struct mmcsd_part *part;
630*72dec079SMarius Strobl 	int i;
631eb67f31aSAlexander Motin 
632*72dec079SMarius Strobl 	for (i = 0; i < MMC_PART_MAX; i++) {
633*72dec079SMarius Strobl 		part = sc->part[i];
634*72dec079SMarius Strobl 		if (part != NULL && part->disk != NULL) {
635*72dec079SMarius Strobl 			MMCSD_PART_LOCK(part);
636*72dec079SMarius Strobl 			part->suspend = 0;
637*72dec079SMarius Strobl 			if (part->running <= 0) {
638*72dec079SMarius Strobl 				part->running = 1;
639*72dec079SMarius Strobl 				kproc_create(&mmcsd_task, part, &part->p, 0, 0,
640*72dec079SMarius Strobl 				    "%s%d: mmc/sd card", part->name, part->cnt);
641*72dec079SMarius Strobl 				MMCSD_PART_UNLOCK(part);
642eb67f31aSAlexander Motin 			} else
643*72dec079SMarius Strobl 				MMCSD_PART_UNLOCK(part);
644*72dec079SMarius Strobl 		}
645*72dec079SMarius 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;
667*72dec079SMarius Strobl 	struct mmcsd_part *part;
668114b4164SWarner Losh 
669*72dec079SMarius Strobl 	part = bp->bio_disk->d_drv1;
670*72dec079SMarius Strobl 	sc = part->sc;
671*72dec079SMarius Strobl 	MMCSD_PART_LOCK(part);
672*72dec079SMarius Strobl 	if (part->running > 0 || part->suspend > 0) {
673*72dec079SMarius Strobl 		bioq_disksort(&part->bio_queue, bp);
674*72dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
675*72dec079SMarius Strobl 		wakeup(part);
676249b0e85SAlexander Motin 	} else {
677*72dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
678249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
679249b0e85SAlexander Motin 	}
680114b4164SWarner Losh }
681114b4164SWarner Losh 
682*72dec079SMarius Strobl static int
683*72dec079SMarius Strobl mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
684*72dec079SMarius Strobl     int fflag, struct thread *td __unused)
685*72dec079SMarius Strobl {
686*72dec079SMarius Strobl 
687*72dec079SMarius Strobl 	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
688*72dec079SMarius Strobl }
689*72dec079SMarius Strobl 
690*72dec079SMarius Strobl static int
691*72dec079SMarius Strobl mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
692*72dec079SMarius Strobl     struct thread *td __unused)
693*72dec079SMarius Strobl {
694*72dec079SMarius Strobl 
695*72dec079SMarius Strobl 	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
696*72dec079SMarius Strobl }
697*72dec079SMarius Strobl 
698*72dec079SMarius Strobl static int
699*72dec079SMarius Strobl mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
700*72dec079SMarius Strobl {
701*72dec079SMarius Strobl 	struct mmc_ioc_cmd *mic;
702*72dec079SMarius Strobl 	struct mmc_ioc_multi_cmd *mimc;
703*72dec079SMarius Strobl 	int i, err;
704*72dec079SMarius Strobl 	u_long cnt, size;
705*72dec079SMarius Strobl 
706*72dec079SMarius Strobl 	if ((fflag & FREAD) == 0)
707*72dec079SMarius Strobl 		return (EBADF);
708*72dec079SMarius Strobl 
709*72dec079SMarius Strobl 	err = 0;
710*72dec079SMarius Strobl 	switch (cmd) {
711*72dec079SMarius Strobl 	case MMC_IOC_CMD:
712*72dec079SMarius Strobl 		mic = data;
713*72dec079SMarius Strobl 		err = mmcsd_ioctl_cmd(part, data, fflag);
714*72dec079SMarius Strobl 		break;
715*72dec079SMarius Strobl 	case MMC_IOC_CMD_MULTI:
716*72dec079SMarius Strobl 		mimc = data;
717*72dec079SMarius Strobl 		if (mimc->num_of_cmds == 0)
718*72dec079SMarius Strobl 			break;
719*72dec079SMarius Strobl 		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
720*72dec079SMarius Strobl 			return (EINVAL);
721*72dec079SMarius Strobl 		cnt = mimc->num_of_cmds;
722*72dec079SMarius Strobl 		size = sizeof(*mic) * cnt;
723*72dec079SMarius Strobl 		mic = malloc(size, M_TEMP, M_WAITOK);
724*72dec079SMarius Strobl 		err = copyin((const void *)mimc->cmds, mic, size);
725*72dec079SMarius Strobl 		if (err != 0)
726*72dec079SMarius Strobl 			break;
727*72dec079SMarius Strobl 		for (i = 0; i < cnt; i++) {
728*72dec079SMarius Strobl 			err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
729*72dec079SMarius Strobl 			if (err != 0)
730*72dec079SMarius Strobl 				break;
731*72dec079SMarius Strobl 		}
732*72dec079SMarius Strobl 		free(mic, M_TEMP);
733*72dec079SMarius Strobl 		break;
734*72dec079SMarius Strobl 	default:
735*72dec079SMarius Strobl 		return (ENOIOCTL);
736*72dec079SMarius Strobl 	}
737*72dec079SMarius Strobl 	return (err);
738*72dec079SMarius Strobl }
739*72dec079SMarius Strobl 
740*72dec079SMarius Strobl static int
741*72dec079SMarius Strobl mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
742*72dec079SMarius Strobl {
743*72dec079SMarius Strobl 	struct mmc_command cmd;
744*72dec079SMarius Strobl 	struct mmc_data data;
745*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
746*72dec079SMarius Strobl 	device_t dev, mmcbr;
747*72dec079SMarius Strobl 	void *dp;
748*72dec079SMarius Strobl 	u_long len;
749*72dec079SMarius Strobl 	int err, retries;
750*72dec079SMarius Strobl 	uint32_t status;
751*72dec079SMarius Strobl 	uint16_t rca;
752*72dec079SMarius Strobl 
753*72dec079SMarius Strobl 	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
754*72dec079SMarius Strobl 		return (EBADF);
755*72dec079SMarius Strobl 
756*72dec079SMarius Strobl 	if (part->ro == TRUE && mic->write_flag != 0)
757*72dec079SMarius Strobl 		return (EROFS);
758*72dec079SMarius Strobl 
759*72dec079SMarius Strobl 	err = 0;
760*72dec079SMarius Strobl 	dp = NULL;
761*72dec079SMarius Strobl 	len = mic->blksz * mic->blocks;
762*72dec079SMarius Strobl 	if (len > MMC_IOC_MAX_BYTES)
763*72dec079SMarius Strobl 		return (EOVERFLOW);
764*72dec079SMarius Strobl 	if (len != 0) {
765*72dec079SMarius Strobl 		dp = malloc(len, M_TEMP, M_WAITOK);
766*72dec079SMarius Strobl 		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
767*72dec079SMarius Strobl 		if (err != 0)
768*72dec079SMarius Strobl 			goto out;
769*72dec079SMarius Strobl 	}
770*72dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
771*72dec079SMarius Strobl 	memset(&data, 0, sizeof(data));
772*72dec079SMarius Strobl 	cmd.opcode = mic->opcode;
773*72dec079SMarius Strobl 	cmd.arg = mic->arg;
774*72dec079SMarius Strobl 	cmd.flags = mic->flags;
775*72dec079SMarius Strobl 	if (len != 0) {
776*72dec079SMarius Strobl 		data.len = len;
777*72dec079SMarius Strobl 		data.data = dp;
778*72dec079SMarius Strobl 		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
779*72dec079SMarius Strobl 		    MMC_DATA_READ;
780*72dec079SMarius Strobl 		cmd.data = &data;
781*72dec079SMarius Strobl 	}
782*72dec079SMarius Strobl 	sc = part->sc;
783*72dec079SMarius Strobl 	rca = sc->rca;
784*72dec079SMarius Strobl 	if (mic->is_acmd == 0) {
785*72dec079SMarius Strobl 		/* Enforce/patch/restrict RCA-based commands */
786*72dec079SMarius Strobl 		switch (cmd.opcode) {
787*72dec079SMarius Strobl 		case MMC_SET_RELATIVE_ADDR:
788*72dec079SMarius Strobl 		case MMC_SELECT_CARD:
789*72dec079SMarius Strobl 			err = EPERM;
790*72dec079SMarius Strobl 			goto out;
791*72dec079SMarius Strobl 		case MMC_STOP_TRANSMISSION:
792*72dec079SMarius Strobl 			if ((cmd.arg & 0x1) == 0)
793*72dec079SMarius Strobl 				break;
794*72dec079SMarius Strobl 			/* FALLTHROUGH */
795*72dec079SMarius Strobl 		case MMC_SLEEP_AWAKE:
796*72dec079SMarius Strobl 		case MMC_SEND_CSD:
797*72dec079SMarius Strobl 		case MMC_SEND_CID:
798*72dec079SMarius Strobl 		case MMC_SEND_STATUS:
799*72dec079SMarius Strobl 		case MMC_GO_INACTIVE_STATE:
800*72dec079SMarius Strobl 		case MMC_FAST_IO:
801*72dec079SMarius Strobl 		case MMC_APP_CMD:
802*72dec079SMarius Strobl 			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
803*72dec079SMarius Strobl 			break;
804*72dec079SMarius Strobl 		default:
805*72dec079SMarius Strobl 			break;
806*72dec079SMarius Strobl 		}
807*72dec079SMarius Strobl 	}
808*72dec079SMarius Strobl 	dev = sc->dev;
809*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
810*72dec079SMarius Strobl 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
811*72dec079SMarius Strobl 	err = mmcsd_switch_part(mmcbr, dev, rca, part->type);
812*72dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
813*72dec079SMarius Strobl 		goto release;
814*72dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
815*72dec079SMarius Strobl 		err = mmcsd_set_blockcount(sc, mic->blocks,
816*72dec079SMarius Strobl 		    mic->write_flag & (1 << 31));
817*72dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
818*72dec079SMarius Strobl 			goto release;
819*72dec079SMarius Strobl 	}
820*72dec079SMarius Strobl 	if (mic->is_acmd != 0)
821*72dec079SMarius Strobl 		(void)mmc_wait_for_app_cmd(mmcbr, dev, rca, &cmd, 0);
822*72dec079SMarius Strobl 	else
823*72dec079SMarius Strobl 		(void)mmc_wait_for_cmd(mmcbr, dev, &cmd, 0);
824*72dec079SMarius Strobl 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
825*72dec079SMarius Strobl 		/*
826*72dec079SMarius Strobl 		 * If the request went to the RPMB partition, try to ensure
827*72dec079SMarius Strobl 		 * that the command actually has completed ...
828*72dec079SMarius Strobl 		 */
829*72dec079SMarius Strobl 		retries = MMCSD_CMD_RETRIES;
830*72dec079SMarius Strobl 		do {
831*72dec079SMarius Strobl 			err = mmc_send_status(mmcbr, dev, rca, &status);
832*72dec079SMarius Strobl 			if (err != MMC_ERR_NONE)
833*72dec079SMarius Strobl 				break;
834*72dec079SMarius Strobl 			if (R1_STATUS(status) == 0 &&
835*72dec079SMarius Strobl 			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
836*72dec079SMarius Strobl 				break;
837*72dec079SMarius Strobl 			DELAY(1000);
838*72dec079SMarius Strobl 		} while (retries-- > 0);
839*72dec079SMarius Strobl 
840*72dec079SMarius Strobl 		/* ... and always switch back to the default partition. */
841*72dec079SMarius Strobl 		err = mmcsd_switch_part(mmcbr, dev, rca,
842*72dec079SMarius Strobl 		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
843*72dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
844*72dec079SMarius Strobl 			goto release;
845*72dec079SMarius Strobl 	}
846*72dec079SMarius Strobl 	/*
847*72dec079SMarius Strobl 	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
848*72dec079SMarius Strobl 	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
849*72dec079SMarius Strobl 	 * so retrieve EXT_CSD again.
850*72dec079SMarius Strobl 	 */
851*72dec079SMarius Strobl 	if (cmd.opcode == MMC_SWITCH_FUNC) {
852*72dec079SMarius Strobl 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
853*72dec079SMarius Strobl 		if (err != MMC_ERR_NONE)
854*72dec079SMarius Strobl 			goto release;
855*72dec079SMarius Strobl 	}
856*72dec079SMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbr, dev);
857*72dec079SMarius Strobl 	if (cmd.error != MMC_ERR_NONE) {
858*72dec079SMarius Strobl 		switch (cmd.error) {
859*72dec079SMarius Strobl 		case MMC_ERR_TIMEOUT:
860*72dec079SMarius Strobl 			err = ETIMEDOUT;
861*72dec079SMarius Strobl 			break;
862*72dec079SMarius Strobl 		case MMC_ERR_BADCRC:
863*72dec079SMarius Strobl 			err = EILSEQ;
864*72dec079SMarius Strobl 			break;
865*72dec079SMarius Strobl 		case MMC_ERR_INVALID:
866*72dec079SMarius Strobl 			err = EINVAL;
867*72dec079SMarius Strobl 			break;
868*72dec079SMarius Strobl 		case MMC_ERR_NO_MEMORY:
869*72dec079SMarius Strobl 			err = ENOMEM;
870*72dec079SMarius Strobl 			break;
871*72dec079SMarius Strobl 		default:
872*72dec079SMarius Strobl 			err = EIO;
873*72dec079SMarius Strobl 			break;
874*72dec079SMarius Strobl 		}
875*72dec079SMarius Strobl 		goto out;
876*72dec079SMarius Strobl 	}
877*72dec079SMarius Strobl 	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
878*72dec079SMarius Strobl 	if (mic->write_flag == 0 && len != 0) {
879*72dec079SMarius Strobl 		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
880*72dec079SMarius Strobl 		if (err != 0)
881*72dec079SMarius Strobl 			goto out;
882*72dec079SMarius Strobl 	}
883*72dec079SMarius Strobl 	goto out;
884*72dec079SMarius Strobl 
885*72dec079SMarius Strobl release:
886*72dec079SMarius Strobl 	MMCBUS_RELEASE_BUS(mmcbr, dev);
887*72dec079SMarius Strobl 	err = EIO;
888*72dec079SMarius Strobl 
889*72dec079SMarius Strobl out:
890*72dec079SMarius Strobl 	if (dp != NULL)
891*72dec079SMarius Strobl 		free(dp, M_TEMP);
892*72dec079SMarius Strobl 	return (err);
893*72dec079SMarius Strobl }
894*72dec079SMarius Strobl 
895*72dec079SMarius Strobl static int
896*72dec079SMarius Strobl mmcsd_getattr(struct bio *bp)
897*72dec079SMarius Strobl {
898*72dec079SMarius Strobl 	struct mmcsd_part *part;
899*72dec079SMarius Strobl 	device_t dev;
900*72dec079SMarius Strobl 
901*72dec079SMarius Strobl 	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
902*72dec079SMarius Strobl 		if (bp->bio_length != sizeof(dev))
903*72dec079SMarius Strobl 			return (EFAULT);
904*72dec079SMarius Strobl 		part = bp->bio_disk->d_drv1;
905*72dec079SMarius Strobl 		dev = part->sc->dev;
906*72dec079SMarius Strobl 		bcopy(&dev, bp->bio_data, sizeof(dev));
907*72dec079SMarius Strobl 		bp->bio_completed = bp->bio_length;
908*72dec079SMarius Strobl 		return (0);
909*72dec079SMarius Strobl 	}
910*72dec079SMarius Strobl 	return (-1);
911*72dec079SMarius Strobl }
912*72dec079SMarius Strobl 
913*72dec079SMarius Strobl static int
914*72dec079SMarius Strobl mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
915*72dec079SMarius Strobl {
916*72dec079SMarius Strobl 	struct mmc_command cmd;
917*72dec079SMarius Strobl 	struct mmc_request req;
918*72dec079SMarius Strobl 
919*72dec079SMarius Strobl 	memset(&req, 0, sizeof(req));
920*72dec079SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
921*72dec079SMarius Strobl 	cmd.mrq = &req;
922*72dec079SMarius Strobl 	req.cmd = &cmd;
923*72dec079SMarius Strobl 	cmd.opcode = MMC_SET_BLOCK_COUNT;
924*72dec079SMarius Strobl 	cmd.arg = count & 0x0000FFFF;
925*72dec079SMarius Strobl 	if (reliable)
926*72dec079SMarius Strobl 		cmd.arg |= 1 << 31;
927*72dec079SMarius Strobl 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
928*72dec079SMarius Strobl 	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbr, sc->dev, &req);
929*72dec079SMarius Strobl 	return (cmd.error);
930*72dec079SMarius Strobl }
931*72dec079SMarius Strobl 
932*72dec079SMarius Strobl static int
933*72dec079SMarius Strobl mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
934*72dec079SMarius Strobl {
935*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
936*72dec079SMarius Strobl 	int err;
937*72dec079SMarius Strobl 	uint8_t	value;
938*72dec079SMarius Strobl 
939*72dec079SMarius Strobl 	sc = device_get_softc(dev);
940*72dec079SMarius Strobl 
941*72dec079SMarius Strobl 	if (sc->part_curr == part)
942*72dec079SMarius Strobl 		return (MMC_ERR_NONE);
943*72dec079SMarius Strobl 
944*72dec079SMarius Strobl 	if (sc->mode == mode_sd)
945*72dec079SMarius Strobl 		return (MMC_ERR_NONE);
946*72dec079SMarius Strobl 
947*72dec079SMarius Strobl 	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
948*72dec079SMarius Strobl 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
949*72dec079SMarius Strobl 	/* Jump! */
950*72dec079SMarius Strobl 	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
951*72dec079SMarius Strobl 	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
952*72dec079SMarius Strobl 	if (err != MMC_ERR_NONE)
953*72dec079SMarius Strobl 		return (err);
954*72dec079SMarius Strobl 
955*72dec079SMarius Strobl 	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
956*72dec079SMarius Strobl 	sc->part_curr = part;
957*72dec079SMarius Strobl 	return (MMC_ERR_NONE);
958*72dec079SMarius Strobl }
959*72dec079SMarius 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
970*72dec079SMarius 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;
977*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
978*72dec079SMarius Strobl 	device_t dev, mmcbr;
9791bacf3beSMarius Strobl 	int numblocks, sz;
9801bacf3beSMarius Strobl 	char *vaddr;
981114b4164SWarner Losh 
982*72dec079SMarius Strobl 	sc = part->sc;
983*72dec079SMarius Strobl 	dev = sc->dev;
984*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
985*72dec079SMarius Strobl 
9863906d42dSAlexander Motin 	block = bp->bio_pblkno;
987*72dec079SMarius 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
1044*72dec079SMarius 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;
1049*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
1050*72dec079SMarius Strobl 	device_t dev, mmcbr;
1051*72dec079SMarius Strobl 	int erase_sector, sz;
1052*72dec079SMarius Strobl 
1053*72dec079SMarius Strobl 	sc = part->sc;
1054*72dec079SMarius Strobl 	dev = sc->dev;
1055*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
10563906d42dSAlexander Motin 
10573906d42dSAlexander Motin 	block = bp->bio_pblkno;
1058*72dec079SMarius 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. */
1061*72dec079SMarius Strobl 	if (block > part->eblock && block <= part->eend)
1062*72dec079SMarius Strobl 		block = part->eblock;
1063*72dec079SMarius Strobl 	if (end >= part->eblock && end < part->eend)
1064*72dec079SMarius 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;
1071*72dec079SMarius Strobl 	/* We can't erase an area smaller than a sector, store it for later. */
1072a7cf6274SAlexander Motin 	if (start >= stop) {
1073*72dec079SMarius Strobl 		part->eblock = block;
1074*72dec079SMarius 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. */
1127*72dec079SMarius Strobl 	if (bp->bio_pblkno >= part->eblock || block == start) {
1128*72dec079SMarius Strobl 		part->eblock = stop;	/* Predict next forward. */
1129*72dec079SMarius Strobl 		part->eend = end;
1130a7cf6274SAlexander Motin 	} else {
1131*72dec079SMarius Strobl 		part->eblock = block;	/* Predict next backward. */
1132*72dec079SMarius 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;
1143*72dec079SMarius Strobl 	struct disk *disk;
1144*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
1145*72dec079SMarius Strobl 	struct mmcsd_part *part;
1146*72dec079SMarius Strobl 	device_t dev, mmcbr;
1147*72dec079SMarius 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 
1153*72dec079SMarius Strobl 	disk = arg;
1154*72dec079SMarius Strobl 	part = disk->d_drv1;
1155*72dec079SMarius Strobl 	sc = part->sc;
1156*72dec079SMarius Strobl 	dev = sc->dev;
1157*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
1158*72dec079SMarius 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;
1165*72dec079SMarius Strobl 	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
11668fa3a540SWarner Losh 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1167*72dec079SMarius Strobl 	err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1168*72dec079SMarius Strobl 	if (err != MMC_ERR_NONE) {
1169*72dec079SMarius Strobl 		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
1170*72dec079SMarius Strobl 			device_printf(dev, "Partition switch error\n");
1171*72dec079SMarius Strobl 		MMCBUS_RELEASE_BUS(mmcbr, dev);
1172*72dec079SMarius Strobl 		return (EIO);
1173*72dec079SMarius Strobl 	}
1174*72dec079SMarius 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;
1183*72dec079SMarius Strobl 	struct mmcsd_part *part;
1184*72dec079SMarius Strobl 	struct mmcsd_softc *sc;
1185*72dec079SMarius Strobl 	struct bio *bp;
1186*72dec079SMarius Strobl 	device_t dev, mmcbr;
1187*72dec079SMarius Strobl 	int err, sz;
1188*72dec079SMarius Strobl 
1189*72dec079SMarius Strobl 	part = arg;
1190*72dec079SMarius Strobl 	sc = part->sc;
1191*72dec079SMarius Strobl 	dev = sc->dev;
1192*72dec079SMarius Strobl 	mmcbr = sc->mmcbr;
11933906d42dSAlexander Motin 
1194249b0e85SAlexander Motin 	while (1) {
1195*72dec079SMarius Strobl 		MMCSD_PART_LOCK(part);
11963906d42dSAlexander Motin 		do {
1197*72dec079SMarius Strobl 			if (part->running == 0)
1198249b0e85SAlexander Motin 				goto out;
1199*72dec079SMarius Strobl 			bp = bioq_takefirst(&part->bio_queue);
12003906d42dSAlexander Motin 			if (bp == NULL)
1201*72dec079SMarius Strobl 				msleep(part, &part->part_mtx, PRIBIO,
1202*72dec079SMarius Strobl 				    "jobqueue", 0);
1203249b0e85SAlexander Motin 		} while (bp == NULL);
1204*72dec079SMarius Strobl 		MMCSD_PART_UNLOCK(part);
1205*72dec079SMarius 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);
1213*72dec079SMarius Strobl 		sz = part->disk->d_sectorsize;
12143906d42dSAlexander Motin 		block = bp->bio_pblkno;
12153906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
1216*72dec079SMarius Strobl 		err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1217*72dec079SMarius Strobl 		if (err != MMC_ERR_NONE) {
1218*72dec079SMarius Strobl 			if (ppsratecheck(&sc->log_time, &sc->log_count,
1219*72dec079SMarius Strobl 			    LOG_PPS))
1220*72dec079SMarius Strobl 				device_printf(dev, "Partition switch error\n");
1221*72dec079SMarius Strobl 			goto release;
1222*72dec079SMarius Strobl 		}
12233906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1224a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
1225*72dec079SMarius Strobl 			if (block < part->eend && end > part->eblock)
1226*72dec079SMarius Strobl 				part->eblock = part->eend = 0;
1227*72dec079SMarius Strobl 			block = mmcsd_rw(part, bp);
12283906d42dSAlexander Motin 		} else if (bp->bio_cmd == BIO_DELETE) {
1229*72dec079SMarius Strobl 			block = mmcsd_delete(part, bp);
12303906d42dSAlexander Motin 		}
1231*72dec079SMarius 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 */
1244*72dec079SMarius Strobl 	part->running = -1;
1245*72dec079SMarius Strobl 	MMCSD_PART_UNLOCK(part);
1246*72dec079SMarius 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 
1278*72dec079SMarius Strobl static int
1279*72dec079SMarius Strobl mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
1280*72dec079SMarius Strobl {
1281*72dec079SMarius Strobl 
1282*72dec079SMarius Strobl 	switch (what) {
1283*72dec079SMarius Strobl 	case MOD_LOAD:
1284*72dec079SMarius Strobl 		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
1285*72dec079SMarius Strobl 		    TRUE);
1286*72dec079SMarius Strobl 		return (0);
1287*72dec079SMarius Strobl 	case MOD_UNLOAD:
1288*72dec079SMarius Strobl 		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
1289*72dec079SMarius Strobl 		return (0);
1290*72dec079SMarius Strobl 	}
1291*72dec079SMarius Strobl 	return (0);
1292*72dec079SMarius Strobl }
1293*72dec079SMarius Strobl 
1294*72dec079SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
1295*72dec079SMarius Strobl MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
1296*72dec079SMarius Strobl MMC_DEPEND(mmcsd);
1297