xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Portions of this software may have been developed with reference to
27  * the SD Simplified Specification.  The following disclaimer may apply:
28  *
29  * The following conditions apply to the release of the simplified
30  * specification ("Simplified Specification") by the SD Card Association and
31  * the SD Group. The Simplified Specification is a subset of the complete SD
32  * Specification which is owned by the SD Card Association and the SD
33  * Group. This Simplified Specification is provided on a non-confidential
34  * basis subject to the disclaimers below. Any implementation of the
35  * Simplified Specification may require a license from the SD Card
36  * Association, SD Group, SD-3C LLC or other third parties.
37  *
38  * Disclaimers:
39  *
40  * The information contained in the Simplified Specification is presented only
41  * as a standard specification for SD Cards and SD Host/Ancillary products and
42  * is provided "AS-IS" without any representations or warranties of any
43  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
44  * Card Association for any damages, any infringements of patents or other
45  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
46  * parties, which may result from its use. No license is granted by
47  * implication, estoppel or otherwise under any patent or other rights of the
48  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
49  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
50  * or the SD Card Association to disclose or distribute any technical
51  * information, know-how or other confidential information to any third party.
52  */
53 
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/bio.h>
60 #include <sys/bus.h>
61 #include <sys/conf.h>
62 #include <sys/fcntl.h>
63 #include <sys/ioccom.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
66 #include <sys/lock.h>
67 #include <sys/malloc.h>
68 #include <sys/module.h>
69 #include <sys/mutex.h>
70 #include <sys/slicer.h>
71 #include <sys/time.h>
72 
73 #include <geom/geom.h>
74 #include <geom/geom_disk.h>
75 
76 #include <dev/mmc/bridge.h>
77 #include <dev/mmc/mmc_ioctl.h>
78 #include <dev/mmc/mmc_subr.h>
79 #include <dev/mmc/mmcbrvar.h>
80 #include <dev/mmc/mmcreg.h>
81 #include <dev/mmc/mmcvar.h>
82 
83 #include "mmcbus_if.h"
84 
85 #if __FreeBSD_version < 800002
86 #define	kproc_create	kthread_create
87 #define	kproc_exit	kthread_exit
88 #endif
89 
90 #define	MMCSD_CMD_RETRIES	5
91 
92 #define	MMCSD_FMT_BOOT		"mmcsd%dboot"
93 #define	MMCSD_FMT_GP		"mmcsd%dgp"
94 #define	MMCSD_FMT_RPMB		"mmcsd%drpmb"
95 #define	MMCSD_LABEL_ENH		"enh"
96 
97 #define	MMCSD_PART_NAMELEN	(16 + 1)
98 
99 struct mmcsd_softc;
100 
101 struct mmcsd_part {
102 	struct mtx part_mtx;
103 	struct mmcsd_softc *sc;
104 	struct disk *disk;
105 	struct proc *p;
106 	struct bio_queue_head bio_queue;
107 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
108 	u_int cnt;
109 	u_int type;
110 	int running;
111 	int suspend;
112 	bool ro;
113 	char name[MMCSD_PART_NAMELEN];
114 };
115 
116 struct mmcsd_softc {
117 	device_t dev;
118 	device_t mmcbr;
119 	struct mmcsd_part *part[MMC_PART_MAX];
120 	enum mmc_card_mode mode;
121 	uint8_t part_curr;	/* Partition currently switched to */
122 	uint8_t ext_csd[MMC_EXTCSD_SIZE];
123 	uint16_t rca;
124 	uint32_t part_time;	/* Partition switch timeout [us] */
125 	off_t enh_base;		/* Enhanced user data area slice base ... */
126 	off_t enh_size;		/* ... and size [bytes] */
127 	int log_count;
128 	struct timeval log_time;
129 	struct cdev *rpmb_dev;
130 };
131 
132 static const char *errmsg[] =
133 {
134 	"None",
135 	"Timeout",
136 	"Bad CRC",
137 	"Fifo",
138 	"Failed",
139 	"Invalid",
140 	"NO MEMORY"
141 };
142 
143 #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
144 
145 /* bus entry points */
146 static int mmcsd_attach(device_t dev);
147 static int mmcsd_detach(device_t dev);
148 static int mmcsd_probe(device_t dev);
149 
150 /* disk routines */
151 static int mmcsd_close(struct disk *dp);
152 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
153     off_t offset, size_t length);
154 static int mmcsd_getattr(struct bio *);
155 static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
156     int fflag, struct thread *td);
157 static int mmcsd_open(struct disk *dp);
158 static void mmcsd_strategy(struct bio *bp);
159 static void mmcsd_task(void *arg);
160 
161 /* RMPB cdev interface */
162 static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
163     int fflag, struct thread *td);
164 
165 static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
166     const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
167 static int mmcsd_bus_bit_width(device_t dev);
168 static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
169 static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
170     int fflag);
171 static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
172     int fflag);
173 static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
174 static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
175 static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
176 static int mmcsd_slicer(device_t dev, const char *provider,
177     struct flash_slice *slices, int *nslices);
178 static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
179     u_int part);
180 
181 #define	MMCSD_PART_LOCK(_part)		mtx_lock(&(_part)->part_mtx)
182 #define	MMCSD_PART_UNLOCK(_part)	mtx_unlock(&(_part)->part_mtx)
183 #define	MMCSD_PART_LOCK_INIT(_part)					\
184 	mtx_init(&(_part)->part_mtx, (_part)->name, "mmcsd part", MTX_DEF)
185 #define	MMCSD_PART_LOCK_DESTROY(_part)	mtx_destroy(&(_part)->part_mtx);
186 #define	MMCSD_PART_ASSERT_LOCKED(_part)					\
187 	mtx_assert(&(_part)->part_mtx, MA_OWNED);
188 #define	MMCSD_PART_ASSERT_UNLOCKED(_part)				\
189 	mtx_assert(&(_part)->part_mtx, MA_NOTOWNED);
190 
191 static int
192 mmcsd_probe(device_t dev)
193 {
194 
195 	device_quiet(dev);
196 	device_set_desc(dev, "MMC/SD Memory Card");
197 	return (0);
198 }
199 
200 static int
201 mmcsd_attach(device_t dev)
202 {
203 	device_t mmcbr;
204 	struct mmcsd_softc *sc;
205 	const uint8_t *ext_csd;
206 	off_t erase_size, sector_size, size, wp_size;
207 	uintmax_t bytes;
208 	int err, i;
209 	uint8_t rev;
210 	bool comp, ro;
211 	char unit[2];
212 
213 	sc = device_get_softc(dev);
214 	sc->dev = dev;
215 	sc->mmcbr = mmcbr = device_get_parent(dev);
216 	sc->mode = mmcbr_get_mode(mmcbr);
217 	sc->rca = mmc_get_rca(dev);
218 
219 	/* Only MMC >= 4.x devices support EXT_CSD. */
220 	if (mmc_get_spec_vers(dev) >= 4) {
221 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
222 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
223 		MMCBUS_RELEASE_BUS(mmcbr, dev);
224 		if (err != MMC_ERR_NONE)
225 			bzero(sc->ext_csd, sizeof(sc->ext_csd));
226 	}
227 	ext_csd = sc->ext_csd;
228 
229 	/*
230 	 * Enhanced user data area and general purpose partitions are only
231 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
232 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
233 	 */
234 	rev = ext_csd[EXT_CSD_REV];
235 
236 	/*
237 	 * Ignore user-creatable enhanced user data area and general purpose
238 	 * partitions partitions as long as partitioning hasn't been finished.
239 	 */
240 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
241 
242 	/*
243 	 * Add enhanced user data area slice, unless it spans the entirety of
244 	 * the user data area.  The enhanced area is of a multiple of high
245 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
246 	 * 512 KB) and its offset given in either sectors or bytes, depending
247 	 * on whether it's a high capacity device or not.
248 	 * NB: The slicer and its slices need to be registered before adding
249 	 *     the disk for the corresponding user data area as re-tasting is
250 	 *     racy.
251 	 */
252 	sector_size = mmc_get_sector_size(dev);
253 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
254 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
255 	    (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
256 	if (rev >= 4 && comp == TRUE && size > 0 &&
257 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
258 	    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
259 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
260 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
261 		    MMC_SECTOR_SIZE;
262 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
263 		size *= erase_size * wp_size;
264 		if (size != mmc_get_media_size(dev) * sector_size) {
265 			sc->enh_size = size;
266 			sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
267 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
268 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
269 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
270 			    (mmc_get_high_cap(dev) ? MMC_SECTOR_SIZE : 1);
271 		} else if (bootverbose)
272 			device_printf(dev,
273 			    "enhanced user data area spans entire device\n");
274 	}
275 
276 	/*
277 	 * Add default partition.  This may be the only one or the user
278 	 * data area in case partitions are supported.
279 	 */
280 	ro = mmc_get_read_only(dev);
281 	mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
282 	    device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
283 	    mmc_get_erase_sector(dev) * sector_size, ro);
284 
285 	if (mmc_get_spec_vers(dev) < 3)
286 		return (0);
287 
288 	/* Belatedly announce enhanced user data slice. */
289 	if (sc->enh_size != 0) {
290 		bytes = mmcsd_pretty_size(size, unit);
291 		printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
292 		    "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
293 		    MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
294 		    device_get_nameunit(dev));
295 	}
296 
297 	/*
298 	 * Determine partition switch timeout (provided in units of 10 ms)
299 	 * and ensure it's at least 300 ms as some eMMC chips lie.
300 	 */
301 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
302 	    300 * 1000);
303 
304 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
305 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
306 	if (size > 0 && (mmcbr_get_caps(mmcbr) & MMC_CAP_BOOT_NOACC) == 0) {
307 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
308 		    MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
309 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
310 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
311 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
312 		    MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
313 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
314 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
315 	}
316 
317 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
318 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
319 	if (rev >= 5 && size > 0)
320 		mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
321 		    MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
322 
323 	if (rev <= 3 || comp == FALSE)
324 		return (0);
325 
326 	/*
327 	 * Add general purpose partitions, which are of a multiple of high
328 	 * capacity write protect groups, too.
329 	 */
330 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
331 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
332 		    MMC_SECTOR_SIZE;
333 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
334 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
335 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
336 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
337 			    (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
338 			if (size == 0)
339 				continue;
340 			mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
341 			    MMCSD_FMT_GP, i, size * erase_size * wp_size,
342 			    erase_size, ro);
343 		}
344 	}
345 	return (0);
346 }
347 
348 static uintmax_t
349 mmcsd_pretty_size(off_t size, char *unit)
350 {
351 	uintmax_t bytes;
352 	int i;
353 
354 	/*
355 	 * Display in most natural units.  There's no card < 1MB.  However,
356 	 * RPMB partitions occasionally are smaller than that, though.  The
357 	 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
358 	 * format supports up to 4 GiB and some card makers push it up to this
359 	 * limit.  The SDHC standard only goes to 32 GiB due to FAT32, but the
360 	 * data format supports up to 2 TiB however.  2048 GB isn't too ugly,
361 	 * so we note it in passing here and don't add the code to print TB).
362 	 * Since these cards are sold in terms of MB and GB not MiB and GiB,
363 	 * report them like that.  We also round to the nearest unit, since
364 	 * many cards are a few percent short, even of the power of 10 size.
365 	 */
366 	bytes = size;
367 	unit[0] = unit[1] = '\0';
368 	for (i = 0; i <= 2 && bytes >= 1000; i++) {
369 		bytes = (bytes + 1000 / 2 - 1) / 1000;
370 		switch (i) {
371 		case 0:
372 			unit[0] = 'k';
373 			break;
374 		case 1:
375 			unit[0] = 'M';
376 			break;
377 		case 2:
378 			unit[0] = 'G';
379 			break;
380 		default:
381 			break;
382 		}
383 	}
384 	return (bytes);
385 }
386 
387 static struct cdevsw mmcsd_rpmb_cdevsw = {
388 	.d_version	= D_VERSION,
389 	.d_name		= "mmcsdrpmb",
390 	.d_ioctl	= mmcsd_ioctl_rpmb
391 };
392 
393 static void
394 mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
395     off_t media_size, off_t erase_size, bool ro)
396 {
397 	struct make_dev_args args;
398 	device_t dev, mmcbr;
399 	const char *ext;
400 	const uint8_t *ext_csd;
401 	struct mmcsd_part *part;
402 	struct disk *d;
403 	uintmax_t bytes;
404 	u_int gp;
405 	uint32_t speed;
406 	uint8_t extattr;
407 	bool enh;
408 	char unit[2];
409 
410 	dev = sc->dev;
411 	mmcbr = sc->mmcbr;
412 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
413 	    M_WAITOK | M_ZERO);
414 	part->sc = sc;
415 	part->cnt = cnt;
416 	part->type = type;
417 	part->ro = ro;
418 	snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
419 
420 	/* For the RPMB partition, allow IOCTL access only. */
421 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
422 		make_dev_args_init(&args);
423 		args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
424 		args.mda_devsw = &mmcsd_rpmb_cdevsw;
425 		args.mda_uid = UID_ROOT;
426 		args.mda_gid = GID_OPERATOR;
427 		args.mda_mode = 0640;
428 		args.mda_si_drv1 = part;
429 		if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
430 			device_printf(dev, "Failed to make RPMB device\n");
431 			free(part, M_DEVBUF);
432 			return;
433 		}
434 	} else {
435 		MMCSD_PART_LOCK_INIT(part);
436 
437 		d = part->disk = disk_alloc();
438 		d->d_open = mmcsd_open;
439 		d->d_close = mmcsd_close;
440 		d->d_strategy = mmcsd_strategy;
441 		d->d_ioctl = mmcsd_ioctl_disk;
442 		d->d_dump = mmcsd_dump;
443 		d->d_getattr = mmcsd_getattr;
444 		d->d_name = part->name;
445 		d->d_drv1 = part;
446 		d->d_sectorsize = mmc_get_sector_size(dev);
447 		d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
448 		d->d_mediasize = media_size;
449 		d->d_stripesize = erase_size;
450 		d->d_unit = cnt;
451 		d->d_flags = DISKFLAG_CANDELETE;
452 		d->d_delmaxsize = erase_size;
453 		strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
454 		    sizeof(d->d_ident));
455 		strlcpy(d->d_descr, mmc_get_card_id_string(dev),
456 		    sizeof(d->d_descr));
457 		d->d_rotation_rate = DISK_RR_NON_ROTATING;
458 
459 		disk_create(d, DISK_VERSION);
460 		bioq_init(&part->bio_queue);
461 
462 		part->running = 1;
463 		kproc_create(&mmcsd_task, part, &part->p, 0, 0,
464 		    "%s%d: mmc/sd card", part->name, cnt);
465 	}
466 
467 	bytes = mmcsd_pretty_size(media_size, unit);
468 	if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
469 		speed = mmcbr_get_clock(mmcbr);
470 		printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
471 		    part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
472 		    ro ? " (read-only)" : "", device_get_nameunit(mmcbr),
473 		    speed / 1000000, (speed / 100000) % 10,
474 		    mmcsd_bus_bit_width(dev), mmc_get_max_data(dev));
475 	} else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
476 		printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
477 		    unit, type, ro ? " (read-only)" : "",
478 		    device_get_nameunit(dev));
479 	} else {
480 		enh = false;
481 		ext = NULL;
482 		extattr = 0;
483 		if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
484 		    type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
485 			ext_csd = sc->ext_csd;
486 			gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
487 			if ((ext_csd[EXT_CSD_PART_SUPPORT] &
488 			    EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
489 			    (ext_csd[EXT_CSD_PART_ATTR] &
490 			    (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
491 				enh = true;
492 			else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
493 			    EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
494 				extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
495 				    (gp / 2)] >> (4 * (gp % 2))) & 0xF;
496 				switch (extattr) {
497 					case EXT_CSD_EXT_PART_ATTR_DEFAULT:
498 						break;
499 					case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
500 						ext = "system code";
501 						break;
502 					case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
503 						ext = "non-persistent";
504 						break;
505 					default:
506 						ext = "reserved";
507 						break;
508 				}
509 			}
510 		}
511 		if (ext == NULL)
512 			printf("%s%d: %ju%sB partion %d%s%s at %s\n",
513 			    part->name, cnt, bytes, unit, type, enh ?
514 			    " enhanced" : "", ro ? " (read-only)" : "",
515 			    device_get_nameunit(dev));
516 		else
517 			printf("%s%d: %ju%sB partion %d extended 0x%x "
518 			    "(%s)%s at %s\n", part->name, cnt, bytes, unit,
519 			    type, extattr, ext, ro ? " (read-only)" : "",
520 			    device_get_nameunit(dev));
521 	}
522 }
523 
524 static int
525 mmcsd_slicer(device_t dev, const char *provider,
526     struct flash_slice *slices, int *nslices)
527 {
528 	char name[MMCSD_PART_NAMELEN];
529 	struct mmcsd_softc *sc;
530 	struct mmcsd_part *part;
531 
532 	*nslices = 0;
533 	if (slices == NULL)
534 		return (ENOMEM);
535 
536 	sc = device_get_softc(dev);
537 	if (sc->enh_size == 0)
538 		return (ENXIO);
539 
540 	part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
541 	snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
542 	    part->disk->d_unit);
543 	if (strcmp(name, provider) != 0)
544 		return (ENXIO);
545 
546 	*nslices = 1;
547 	slices[0].base = sc->enh_base;
548 	slices[0].size = sc->enh_size;
549 	slices[0].label = MMCSD_LABEL_ENH;
550 	return (0);
551 }
552 
553 static int
554 mmcsd_detach(device_t dev)
555 {
556 	struct mmcsd_softc *sc = device_get_softc(dev);
557 	struct mmcsd_part *part;
558 	int i;
559 
560 	for (i = 0; i < MMC_PART_MAX; i++) {
561 		part = sc->part[i];
562 		if (part != NULL && part->disk != NULL) {
563 			MMCSD_PART_LOCK(part);
564 			part->suspend = 0;
565 			if (part->running > 0) {
566 				/* kill thread */
567 				part->running = 0;
568 				wakeup(part);
569 				/* wait for thread to finish. */
570 				while (part->running != -1)
571 					msleep(part, &part->part_mtx, 0,
572 					    "detach", 0);
573 			}
574 			MMCSD_PART_UNLOCK(part);
575 		}
576 	}
577 
578 	if (sc->rpmb_dev != NULL)
579 		destroy_dev(sc->rpmb_dev);
580 
581 	for (i = 0; i < MMC_PART_MAX; i++) {
582 		part = sc->part[i];
583 		if (part != NULL) {
584 			if (part->disk != NULL) {
585 				/* Flush the request queue. */
586 				bioq_flush(&part->bio_queue, NULL, ENXIO);
587 				/* kill disk */
588 				disk_destroy(part->disk);
589 
590 				MMCSD_PART_LOCK_DESTROY(part);
591 			}
592 			free(part, M_DEVBUF);
593 		}
594 	}
595 	return (0);
596 }
597 
598 static int
599 mmcsd_suspend(device_t dev)
600 {
601 	struct mmcsd_softc *sc = device_get_softc(dev);
602 	struct mmcsd_part *part;
603 	int i;
604 
605 	for (i = 0; i < MMC_PART_MAX; i++) {
606 		part = sc->part[i];
607 		if (part != NULL && part->disk != NULL) {
608 			MMCSD_PART_LOCK(part);
609 			part->suspend = 1;
610 			if (part->running > 0) {
611 				/* kill thread */
612 				part->running = 0;
613 				wakeup(part);
614 				/* wait for thread to finish. */
615 				while (part->running != -1)
616 					msleep(part, &part->part_mtx, 0,
617 					    "detach", 0);
618 			}
619 			MMCSD_PART_UNLOCK(part);
620 		}
621 	}
622 	return (0);
623 }
624 
625 static int
626 mmcsd_resume(device_t dev)
627 {
628 	struct mmcsd_softc *sc = device_get_softc(dev);
629 	struct mmcsd_part *part;
630 	int i;
631 
632 	for (i = 0; i < MMC_PART_MAX; i++) {
633 		part = sc->part[i];
634 		if (part != NULL && part->disk != NULL) {
635 			MMCSD_PART_LOCK(part);
636 			part->suspend = 0;
637 			if (part->running <= 0) {
638 				part->running = 1;
639 				kproc_create(&mmcsd_task, part, &part->p, 0, 0,
640 				    "%s%d: mmc/sd card", part->name, part->cnt);
641 				MMCSD_PART_UNLOCK(part);
642 			} else
643 				MMCSD_PART_UNLOCK(part);
644 		}
645 	}
646 	return (0);
647 }
648 
649 static int
650 mmcsd_open(struct disk *dp __unused)
651 {
652 
653 	return (0);
654 }
655 
656 static int
657 mmcsd_close(struct disk *dp __unused)
658 {
659 
660 	return (0);
661 }
662 
663 static void
664 mmcsd_strategy(struct bio *bp)
665 {
666 	struct mmcsd_softc *sc;
667 	struct mmcsd_part *part;
668 
669 	part = bp->bio_disk->d_drv1;
670 	sc = part->sc;
671 	MMCSD_PART_LOCK(part);
672 	if (part->running > 0 || part->suspend > 0) {
673 		bioq_disksort(&part->bio_queue, bp);
674 		MMCSD_PART_UNLOCK(part);
675 		wakeup(part);
676 	} else {
677 		MMCSD_PART_UNLOCK(part);
678 		biofinish(bp, NULL, ENXIO);
679 	}
680 }
681 
682 static int
683 mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
684     int fflag, struct thread *td __unused)
685 {
686 
687 	return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
688 }
689 
690 static int
691 mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
692     struct thread *td __unused)
693 {
694 
695 	return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
696 }
697 
698 static int
699 mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
700 {
701 	struct mmc_ioc_cmd *mic;
702 	struct mmc_ioc_multi_cmd *mimc;
703 	int i, err;
704 	u_long cnt, size;
705 
706 	if ((fflag & FREAD) == 0)
707 		return (EBADF);
708 
709 	err = 0;
710 	switch (cmd) {
711 	case MMC_IOC_CMD:
712 		mic = data;
713 		err = mmcsd_ioctl_cmd(part, data, fflag);
714 		break;
715 	case MMC_IOC_CMD_MULTI:
716 		mimc = data;
717 		if (mimc->num_of_cmds == 0)
718 			break;
719 		if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
720 			return (EINVAL);
721 		cnt = mimc->num_of_cmds;
722 		size = sizeof(*mic) * cnt;
723 		mic = malloc(size, M_TEMP, M_WAITOK);
724 		err = copyin((const void *)mimc->cmds, mic, size);
725 		if (err != 0)
726 			break;
727 		for (i = 0; i < cnt; i++) {
728 			err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
729 			if (err != 0)
730 				break;
731 		}
732 		free(mic, M_TEMP);
733 		break;
734 	default:
735 		return (ENOIOCTL);
736 	}
737 	return (err);
738 }
739 
740 static int
741 mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
742 {
743 	struct mmc_command cmd;
744 	struct mmc_data data;
745 	struct mmcsd_softc *sc;
746 	device_t dev, mmcbr;
747 	void *dp;
748 	u_long len;
749 	int err, retries;
750 	uint32_t status;
751 	uint16_t rca;
752 
753 	if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
754 		return (EBADF);
755 
756 	if (part->ro == TRUE && mic->write_flag != 0)
757 		return (EROFS);
758 
759 	err = 0;
760 	dp = NULL;
761 	len = mic->blksz * mic->blocks;
762 	if (len > MMC_IOC_MAX_BYTES)
763 		return (EOVERFLOW);
764 	if (len != 0) {
765 		dp = malloc(len, M_TEMP, M_WAITOK);
766 		err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
767 		if (err != 0)
768 			goto out;
769 	}
770 	memset(&cmd, 0, sizeof(cmd));
771 	memset(&data, 0, sizeof(data));
772 	cmd.opcode = mic->opcode;
773 	cmd.arg = mic->arg;
774 	cmd.flags = mic->flags;
775 	if (len != 0) {
776 		data.len = len;
777 		data.data = dp;
778 		data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
779 		    MMC_DATA_READ;
780 		cmd.data = &data;
781 	}
782 	sc = part->sc;
783 	rca = sc->rca;
784 	if (mic->is_acmd == 0) {
785 		/* Enforce/patch/restrict RCA-based commands */
786 		switch (cmd.opcode) {
787 		case MMC_SET_RELATIVE_ADDR:
788 		case MMC_SELECT_CARD:
789 			err = EPERM;
790 			goto out;
791 		case MMC_STOP_TRANSMISSION:
792 			if ((cmd.arg & 0x1) == 0)
793 				break;
794 			/* FALLTHROUGH */
795 		case MMC_SLEEP_AWAKE:
796 		case MMC_SEND_CSD:
797 		case MMC_SEND_CID:
798 		case MMC_SEND_STATUS:
799 		case MMC_GO_INACTIVE_STATE:
800 		case MMC_FAST_IO:
801 		case MMC_APP_CMD:
802 			cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
803 			break;
804 		default:
805 			break;
806 		}
807 	}
808 	dev = sc->dev;
809 	mmcbr = sc->mmcbr;
810 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
811 	err = mmcsd_switch_part(mmcbr, dev, rca, part->type);
812 	if (err != MMC_ERR_NONE)
813 		goto release;
814 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
815 		err = mmcsd_set_blockcount(sc, mic->blocks,
816 		    mic->write_flag & (1 << 31));
817 		if (err != MMC_ERR_NONE)
818 			goto release;
819 	}
820 	if (mic->is_acmd != 0)
821 		(void)mmc_wait_for_app_cmd(mmcbr, dev, rca, &cmd, 0);
822 	else
823 		(void)mmc_wait_for_cmd(mmcbr, dev, &cmd, 0);
824 	if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
825 		/*
826 		 * If the request went to the RPMB partition, try to ensure
827 		 * that the command actually has completed ...
828 		 */
829 		retries = MMCSD_CMD_RETRIES;
830 		do {
831 			err = mmc_send_status(mmcbr, dev, rca, &status);
832 			if (err != MMC_ERR_NONE)
833 				break;
834 			if (R1_STATUS(status) == 0 &&
835 			    R1_CURRENT_STATE(status) != R1_STATE_PRG)
836 				break;
837 			DELAY(1000);
838 		} while (retries-- > 0);
839 
840 		/* ... and always switch back to the default partition. */
841 		err = mmcsd_switch_part(mmcbr, dev, rca,
842 		    EXT_CSD_PART_CONFIG_ACC_DEFAULT);
843 		if (err != MMC_ERR_NONE)
844 			goto release;
845 	}
846 	/*
847 	 * If EXT_CSD was changed, our copy is outdated now.  Specifically,
848 	 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
849 	 * so retrieve EXT_CSD again.
850 	 */
851 	if (cmd.opcode == MMC_SWITCH_FUNC) {
852 		err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
853 		if (err != MMC_ERR_NONE)
854 			goto release;
855 	}
856 	MMCBUS_RELEASE_BUS(mmcbr, dev);
857 	if (cmd.error != MMC_ERR_NONE) {
858 		switch (cmd.error) {
859 		case MMC_ERR_TIMEOUT:
860 			err = ETIMEDOUT;
861 			break;
862 		case MMC_ERR_BADCRC:
863 			err = EILSEQ;
864 			break;
865 		case MMC_ERR_INVALID:
866 			err = EINVAL;
867 			break;
868 		case MMC_ERR_NO_MEMORY:
869 			err = ENOMEM;
870 			break;
871 		default:
872 			err = EIO;
873 			break;
874 		}
875 		goto out;
876 	}
877 	memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
878 	if (mic->write_flag == 0 && len != 0) {
879 		err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
880 		if (err != 0)
881 			goto out;
882 	}
883 	goto out;
884 
885 release:
886 	MMCBUS_RELEASE_BUS(mmcbr, dev);
887 	err = EIO;
888 
889 out:
890 	if (dp != NULL)
891 		free(dp, M_TEMP);
892 	return (err);
893 }
894 
895 static int
896 mmcsd_getattr(struct bio *bp)
897 {
898 	struct mmcsd_part *part;
899 	device_t dev;
900 
901 	if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
902 		if (bp->bio_length != sizeof(dev))
903 			return (EFAULT);
904 		part = bp->bio_disk->d_drv1;
905 		dev = part->sc->dev;
906 		bcopy(&dev, bp->bio_data, sizeof(dev));
907 		bp->bio_completed = bp->bio_length;
908 		return (0);
909 	}
910 	return (-1);
911 }
912 
913 static int
914 mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
915 {
916 	struct mmc_command cmd;
917 	struct mmc_request req;
918 
919 	memset(&req, 0, sizeof(req));
920 	memset(&cmd, 0, sizeof(cmd));
921 	cmd.mrq = &req;
922 	req.cmd = &cmd;
923 	cmd.opcode = MMC_SET_BLOCK_COUNT;
924 	cmd.arg = count & 0x0000FFFF;
925 	if (reliable)
926 		cmd.arg |= 1 << 31;
927 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
928 	MMCBUS_WAIT_FOR_REQUEST(sc->mmcbr, sc->dev, &req);
929 	return (cmd.error);
930 }
931 
932 static int
933 mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
934 {
935 	struct mmcsd_softc *sc;
936 	int err;
937 	uint8_t	value;
938 
939 	sc = device_get_softc(dev);
940 
941 	if (sc->part_curr == part)
942 		return (MMC_ERR_NONE);
943 
944 	if (sc->mode == mode_sd)
945 		return (MMC_ERR_NONE);
946 
947 	value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
948 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
949 	/* Jump! */
950 	err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
951 	    EXT_CSD_PART_CONFIG, value, sc->part_time, true);
952 	if (err != MMC_ERR_NONE)
953 		return (err);
954 
955 	sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
956 	sc->part_curr = part;
957 	return (MMC_ERR_NONE);
958 }
959 
960 static const char *
961 mmcsd_errmsg(int e)
962 {
963 
964 	if (e < 0 || e > MMC_ERR_MAX)
965 		return "Bad error code";
966 	return errmsg[e];
967 }
968 
969 static daddr_t
970 mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
971 {
972 	daddr_t block, end;
973 	struct mmc_command cmd;
974 	struct mmc_command stop;
975 	struct mmc_request req;
976 	struct mmc_data data;
977 	struct mmcsd_softc *sc;
978 	device_t dev, mmcbr;
979 	int numblocks, sz;
980 	char *vaddr;
981 
982 	sc = part->sc;
983 	dev = sc->dev;
984 	mmcbr = sc->mmcbr;
985 
986 	block = bp->bio_pblkno;
987 	sz = part->disk->d_sectorsize;
988 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
989 	while (block < end) {
990 		vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
991 		numblocks = min(end - block, mmc_get_max_data(dev));
992 		memset(&req, 0, sizeof(req));
993 		memset(&cmd, 0, sizeof(cmd));
994 		memset(&stop, 0, sizeof(stop));
995 		memset(&data, 0, sizeof(data));
996 		cmd.mrq = &req;
997 		req.cmd = &cmd;
998 		cmd.data = &data;
999 		if (bp->bio_cmd == BIO_READ) {
1000 			if (numblocks > 1)
1001 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
1002 			else
1003 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
1004 		} else {
1005 			if (numblocks > 1)
1006 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1007 			else
1008 				cmd.opcode = MMC_WRITE_BLOCK;
1009 		}
1010 		cmd.arg = block;
1011 		if (!mmc_get_high_cap(dev))
1012 			cmd.arg <<= 9;
1013 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1014 		data.data = vaddr;
1015 		data.mrq = &req;
1016 		if (bp->bio_cmd == BIO_READ)
1017 			data.flags = MMC_DATA_READ;
1018 		else
1019 			data.flags = MMC_DATA_WRITE;
1020 		data.len = numblocks * sz;
1021 		if (numblocks > 1) {
1022 			data.flags |= MMC_DATA_MULTI;
1023 			stop.opcode = MMC_STOP_TRANSMISSION;
1024 			stop.arg = 0;
1025 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1026 			stop.mrq = &req;
1027 			req.stop = &stop;
1028 		}
1029 		MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1030 		if (req.cmd->error != MMC_ERR_NONE) {
1031 			if (ppsratecheck(&sc->log_time, &sc->log_count,
1032 			    LOG_PPS))
1033 				device_printf(dev, "Error indicated: %d %s\n",
1034 				    req.cmd->error,
1035 				    mmcsd_errmsg(req.cmd->error));
1036 			break;
1037 		}
1038 		block += numblocks;
1039 	}
1040 	return (block);
1041 }
1042 
1043 static daddr_t
1044 mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
1045 {
1046 	daddr_t block, end, start, stop;
1047 	struct mmc_command cmd;
1048 	struct mmc_request req;
1049 	struct mmcsd_softc *sc;
1050 	device_t dev, mmcbr;
1051 	int erase_sector, sz;
1052 
1053 	sc = part->sc;
1054 	dev = sc->dev;
1055 	mmcbr = sc->mmcbr;
1056 
1057 	block = bp->bio_pblkno;
1058 	sz = part->disk->d_sectorsize;
1059 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
1060 	/* Coalesce with part remaining from previous request. */
1061 	if (block > part->eblock && block <= part->eend)
1062 		block = part->eblock;
1063 	if (end >= part->eblock && end < part->eend)
1064 		end = part->eend;
1065 	/* Safe round to the erase sector boundaries. */
1066 	erase_sector = mmc_get_erase_sector(dev);
1067 	start = block + erase_sector - 1;	 /* Round up. */
1068 	start -= start % erase_sector;
1069 	stop = end;				/* Round down. */
1070 	stop -= end % erase_sector;
1071 	/* We can't erase an area smaller than a sector, store it for later. */
1072 	if (start >= stop) {
1073 		part->eblock = block;
1074 		part->eend = end;
1075 		return (end);
1076 	}
1077 
1078 	/* Set erase start position. */
1079 	memset(&req, 0, sizeof(req));
1080 	memset(&cmd, 0, sizeof(cmd));
1081 	cmd.mrq = &req;
1082 	req.cmd = &cmd;
1083 	if (mmc_get_card_type(dev) == mode_sd)
1084 		cmd.opcode = SD_ERASE_WR_BLK_START;
1085 	else
1086 		cmd.opcode = MMC_ERASE_GROUP_START;
1087 	cmd.arg = start;
1088 	if (!mmc_get_high_cap(dev))
1089 		cmd.arg <<= 9;
1090 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1091 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1092 	if (req.cmd->error != MMC_ERR_NONE) {
1093 	    printf("erase err1: %d\n", req.cmd->error);
1094 	    return (block);
1095 	}
1096 	/* Set erase stop position. */
1097 	memset(&req, 0, sizeof(req));
1098 	memset(&cmd, 0, sizeof(cmd));
1099 	req.cmd = &cmd;
1100 	if (mmc_get_card_type(dev) == mode_sd)
1101 		cmd.opcode = SD_ERASE_WR_BLK_END;
1102 	else
1103 		cmd.opcode = MMC_ERASE_GROUP_END;
1104 	cmd.arg = stop;
1105 	if (!mmc_get_high_cap(dev))
1106 		cmd.arg <<= 9;
1107 	cmd.arg--;
1108 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1109 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1110 	if (req.cmd->error != MMC_ERR_NONE) {
1111 	    printf("erase err2: %d\n", req.cmd->error);
1112 	    return (block);
1113 	}
1114 	/* Erase range. */
1115 	memset(&req, 0, sizeof(req));
1116 	memset(&cmd, 0, sizeof(cmd));
1117 	req.cmd = &cmd;
1118 	cmd.opcode = MMC_ERASE;
1119 	cmd.arg = 0;
1120 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1121 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1122 	if (req.cmd->error != MMC_ERR_NONE) {
1123 	    printf("erase err3 %d\n", req.cmd->error);
1124 	    return (block);
1125 	}
1126 	/* Store one of remaining parts for the next call. */
1127 	if (bp->bio_pblkno >= part->eblock || block == start) {
1128 		part->eblock = stop;	/* Predict next forward. */
1129 		part->eend = end;
1130 	} else {
1131 		part->eblock = block;	/* Predict next backward. */
1132 		part->eend = start;
1133 	}
1134 	return (end);
1135 }
1136 
1137 static int
1138 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1139     size_t length)
1140 {
1141 	struct bio bp;
1142 	daddr_t block, end;
1143 	struct disk *disk;
1144 	struct mmcsd_softc *sc;
1145 	struct mmcsd_part *part;
1146 	device_t dev, mmcbr;
1147 	int err;
1148 
1149 	/* length zero is special and really means flush buffers to media */
1150 	if (!length)
1151 		return (0);
1152 
1153 	disk = arg;
1154 	part = disk->d_drv1;
1155 	sc = part->sc;
1156 	dev = sc->dev;
1157 	mmcbr = sc->mmcbr;
1158 
1159 	g_reset_bio(&bp);
1160 	bp.bio_disk = disk;
1161 	bp.bio_pblkno = offset / disk->d_sectorsize;
1162 	bp.bio_bcount = length;
1163 	bp.bio_data = virtual;
1164 	bp.bio_cmd = BIO_WRITE;
1165 	end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
1166 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1167 	err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1168 	if (err != MMC_ERR_NONE) {
1169 		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
1170 			device_printf(dev, "Partition switch error\n");
1171 		MMCBUS_RELEASE_BUS(mmcbr, dev);
1172 		return (EIO);
1173 	}
1174 	block = mmcsd_rw(part, &bp);
1175 	MMCBUS_RELEASE_BUS(mmcbr, dev);
1176 	return ((end < block) ? EIO : 0);
1177 }
1178 
1179 static void
1180 mmcsd_task(void *arg)
1181 {
1182 	daddr_t block, end;
1183 	struct mmcsd_part *part;
1184 	struct mmcsd_softc *sc;
1185 	struct bio *bp;
1186 	device_t dev, mmcbr;
1187 	int err, sz;
1188 
1189 	part = arg;
1190 	sc = part->sc;
1191 	dev = sc->dev;
1192 	mmcbr = sc->mmcbr;
1193 
1194 	while (1) {
1195 		MMCSD_PART_LOCK(part);
1196 		do {
1197 			if (part->running == 0)
1198 				goto out;
1199 			bp = bioq_takefirst(&part->bio_queue);
1200 			if (bp == NULL)
1201 				msleep(part, &part->part_mtx, PRIBIO,
1202 				    "jobqueue", 0);
1203 		} while (bp == NULL);
1204 		MMCSD_PART_UNLOCK(part);
1205 		if (bp->bio_cmd != BIO_READ && part->ro) {
1206 			bp->bio_error = EROFS;
1207 			bp->bio_resid = bp->bio_bcount;
1208 			bp->bio_flags |= BIO_ERROR;
1209 			biodone(bp);
1210 			continue;
1211 		}
1212 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1213 		sz = part->disk->d_sectorsize;
1214 		block = bp->bio_pblkno;
1215 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
1216 		err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1217 		if (err != MMC_ERR_NONE) {
1218 			if (ppsratecheck(&sc->log_time, &sc->log_count,
1219 			    LOG_PPS))
1220 				device_printf(dev, "Partition switch error\n");
1221 			goto release;
1222 		}
1223 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1224 			/* Access to the remaining erase block obsoletes it. */
1225 			if (block < part->eend && end > part->eblock)
1226 				part->eblock = part->eend = 0;
1227 			block = mmcsd_rw(part, bp);
1228 		} else if (bp->bio_cmd == BIO_DELETE) {
1229 			block = mmcsd_delete(part, bp);
1230 		}
1231 release:
1232 		MMCBUS_RELEASE_BUS(mmcbr, dev);
1233 		if (block < end) {
1234 			bp->bio_error = EIO;
1235 			bp->bio_resid = (end - block) * sz;
1236 			bp->bio_flags |= BIO_ERROR;
1237 		} else {
1238 			bp->bio_resid = 0;
1239 		}
1240 		biodone(bp);
1241 	}
1242 out:
1243 	/* tell parent we're done */
1244 	part->running = -1;
1245 	MMCSD_PART_UNLOCK(part);
1246 	wakeup(part);
1247 
1248 	kproc_exit(0);
1249 }
1250 
1251 static int
1252 mmcsd_bus_bit_width(device_t dev)
1253 {
1254 
1255 	if (mmc_get_bus_width(dev) == bus_width_1)
1256 		return (1);
1257 	if (mmc_get_bus_width(dev) == bus_width_4)
1258 		return (4);
1259 	return (8);
1260 }
1261 
1262 static device_method_t mmcsd_methods[] = {
1263 	DEVMETHOD(device_probe, mmcsd_probe),
1264 	DEVMETHOD(device_attach, mmcsd_attach),
1265 	DEVMETHOD(device_detach, mmcsd_detach),
1266 	DEVMETHOD(device_suspend, mmcsd_suspend),
1267 	DEVMETHOD(device_resume, mmcsd_resume),
1268 	DEVMETHOD_END
1269 };
1270 
1271 static driver_t mmcsd_driver = {
1272 	"mmcsd",
1273 	mmcsd_methods,
1274 	sizeof(struct mmcsd_softc),
1275 };
1276 static devclass_t mmcsd_devclass;
1277 
1278 static int
1279 mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
1280 {
1281 
1282 	switch (what) {
1283 	case MOD_LOAD:
1284 		flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
1285 		    TRUE);
1286 		return (0);
1287 	case MOD_UNLOAD:
1288 		flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
1289 		return (0);
1290 	}
1291 	return (0);
1292 }
1293 
1294 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
1295 MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
1296 MMC_DEPEND(mmcsd);
1297