xref: /freebsd/sys/dev/mmc/mmcsd.c (revision ae5d8757bf0d61914f8bd0f498e2a4538e224d53)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4114b4164SWarner Losh  *
5114b4164SWarner Losh  * Redistribution and use in source and binary forms, with or without
6114b4164SWarner Losh  * modification, are permitted provided that the following conditions
7114b4164SWarner Losh  * are met:
8114b4164SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10114b4164SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12114b4164SWarner Losh  *    documentation and/or other materials provided with the distribution.
13114b4164SWarner Losh  *
14114b4164SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15114b4164SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16114b4164SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17114b4164SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18114b4164SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19114b4164SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20114b4164SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21114b4164SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22114b4164SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23114b4164SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2414eced72SWarner Losh  *
2514eced72SWarner Losh  * Portions of this software may have been developed with reference to
2614eced72SWarner Losh  * the SD Simplified Specification.  The following disclaimer may apply:
2714eced72SWarner Losh  *
2814eced72SWarner Losh  * The following conditions apply to the release of the simplified
2914eced72SWarner Losh  * specification ("Simplified Specification") by the SD Card Association and
3014eced72SWarner Losh  * the SD Group. The Simplified Specification is a subset of the complete SD
3114eced72SWarner Losh  * Specification which is owned by the SD Card Association and the SD
3214eced72SWarner Losh  * Group. This Simplified Specification is provided on a non-confidential
3314eced72SWarner Losh  * basis subject to the disclaimers below. Any implementation of the
3414eced72SWarner Losh  * Simplified Specification may require a license from the SD Card
3514eced72SWarner Losh  * Association, SD Group, SD-3C LLC or other third parties.
3614eced72SWarner Losh  *
3714eced72SWarner Losh  * Disclaimers:
3814eced72SWarner Losh  *
3914eced72SWarner Losh  * The information contained in the Simplified Specification is presented only
4014eced72SWarner Losh  * as a standard specification for SD Cards and SD Host/Ancillary products and
4114eced72SWarner Losh  * is provided "AS-IS" without any representations or warranties of any
4214eced72SWarner Losh  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
4314eced72SWarner Losh  * Card Association for any damages, any infringements of patents or other
4414eced72SWarner Losh  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
4514eced72SWarner Losh  * parties, which may result from its use. No license is granted by
4614eced72SWarner Losh  * implication, estoppel or otherwise under any patent or other rights of the
4714eced72SWarner Losh  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
4814eced72SWarner Losh  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
4914eced72SWarner Losh  * or the SD Card Association to disclose or distribute any technical
5014eced72SWarner Losh  * information, know-how or other confidential information to any third party.
51114b4164SWarner Losh  */
52114b4164SWarner Losh 
53114b4164SWarner Losh #include <sys/cdefs.h>
54114b4164SWarner Losh __FBSDID("$FreeBSD$");
55114b4164SWarner Losh 
56114b4164SWarner Losh #include <sys/param.h>
57114b4164SWarner Losh #include <sys/systm.h>
58114b4164SWarner Losh #include <sys/bio.h>
59114b4164SWarner Losh #include <sys/bus.h>
60114b4164SWarner Losh #include <sys/conf.h>
61114b4164SWarner Losh #include <sys/kernel.h>
62114b4164SWarner Losh #include <sys/kthread.h>
63114b4164SWarner Losh #include <sys/lock.h>
64114b4164SWarner Losh #include <sys/malloc.h>
65114b4164SWarner Losh #include <sys/module.h>
66114b4164SWarner Losh #include <sys/mutex.h>
67114b4164SWarner Losh #include <geom/geom_disk.h>
68114b4164SWarner Losh 
697aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h>
70114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
717aa65846SMarius Strobl #include <dev/mmc/mmcvar.h>
72114b4164SWarner Losh 
73114b4164SWarner Losh #include "mmcbus_if.h"
74114b4164SWarner Losh 
757aa65846SMarius Strobl #if __FreeBSD_version < 800002
767aa65846SMarius Strobl #define	kproc_create	kthread_create
777aa65846SMarius Strobl #define	kproc_exit	kthread_exit
787aa65846SMarius Strobl #endif
797aa65846SMarius Strobl 
80114b4164SWarner Losh struct mmcsd_softc {
81114b4164SWarner Losh 	device_t dev;
82114b4164SWarner Losh 	struct mtx sc_mtx;
83114b4164SWarner Losh 	struct disk *disk;
84114b4164SWarner Losh 	struct proc *p;
85114b4164SWarner Losh 	struct bio_queue_head bio_queue;
86a7cf6274SAlexander Motin 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
870e1466acSWarner Losh 	int running;
88eb67f31aSAlexander Motin 	int suspend;
89114b4164SWarner Losh };
90114b4164SWarner Losh 
91114b4164SWarner Losh /* bus entry points */
92114b4164SWarner Losh static int mmcsd_attach(device_t dev);
93114b4164SWarner Losh static int mmcsd_detach(device_t dev);
94*ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev);
95114b4164SWarner Losh 
96114b4164SWarner Losh /* disk routines */
97114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
98a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
99a1fda318SAlexander Motin 	off_t offset, size_t length);
100*ae5d8757SMarius Strobl static int mmcsd_open(struct disk *dp);
101*ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp);
102114b4164SWarner Losh static void mmcsd_task(void *arg);
103114b4164SWarner Losh 
104a0075e58SWarner Losh static int mmcsd_bus_bit_width(device_t dev);
105*ae5d8757SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
106*ae5d8757SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
107a0075e58SWarner Losh 
108114b4164SWarner Losh #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
109114b4164SWarner Losh #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
110114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \
111114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
112114b4164SWarner Losh 	    "mmcsd", MTX_DEF)
113114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
114114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
115114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
116114b4164SWarner Losh 
117114b4164SWarner Losh static int
118114b4164SWarner Losh mmcsd_probe(device_t dev)
119114b4164SWarner Losh {
120114b4164SWarner Losh 
121c18f1e26SAlexander Motin 	device_quiet(dev);
1220c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
123114b4164SWarner Losh 	return (0);
124114b4164SWarner Losh }
125114b4164SWarner Losh 
126114b4164SWarner Losh static int
127114b4164SWarner Losh mmcsd_attach(device_t dev)
128114b4164SWarner Losh {
129114b4164SWarner Losh 	struct mmcsd_softc *sc;
130a0075e58SWarner Losh 	struct disk *d;
131a0075e58SWarner Losh 	intmax_t mb;
1327aa65846SMarius Strobl 	uint32_t speed;
1337aa65846SMarius Strobl 	uint32_t maxblocks;
134a0075e58SWarner Losh 	char unit;
135114b4164SWarner Losh 
136114b4164SWarner Losh 	sc = device_get_softc(dev);
137114b4164SWarner Losh 	sc->dev = dev;
138114b4164SWarner Losh 	MMCSD_LOCK_INIT(sc);
139114b4164SWarner Losh 
140a0075e58SWarner Losh 	d = sc->disk = disk_alloc();
141a0075e58SWarner Losh 	d->d_open = mmcsd_open;
142a0075e58SWarner Losh 	d->d_close = mmcsd_close;
143a0075e58SWarner Losh 	d->d_strategy = mmcsd_strategy;
144a1fda318SAlexander Motin 	d->d_dump = mmcsd_dump;
145a0075e58SWarner Losh 	d->d_name = "mmcsd";
146a0075e58SWarner Losh 	d->d_drv1 = sc;
1473906d42dSAlexander Motin 	d->d_maxsize = 4*1024*1024;	/* Maximum defined SD card AU size. */
148a0075e58SWarner Losh 	d->d_sectorsize = mmc_get_sector_size(dev);
1491f89a4dcSAlexander Motin 	d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
1502262a519SAlexander Motin 	d->d_stripeoffset = 0;
1512262a519SAlexander Motin 	d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
152a0075e58SWarner Losh 	d->d_unit = device_get_unit(dev);
1533906d42dSAlexander Motin 	d->d_flags = DISKFLAG_CANDELETE;
154a0075e58SWarner Losh 	/*
155a0075e58SWarner Losh 	 * Display in most natural units.  There's no cards < 1MB.
156a0075e58SWarner Losh 	 * The SD standard goes to 2GiB, but the data format supports
157a0075e58SWarner Losh 	 * up to 4GiB and some card makers push it up to this limit.
158a0075e58SWarner Losh 	 * The SDHC standard only goes to 32GiB (the data format in
159a0075e58SWarner Losh 	 * SDHC is good to 2TiB however, which isn't too ugly at
160a0075e58SWarner Losh 	 * 2048GiBm, so we note it in passing here and don't add the
161a0075e58SWarner Losh 	 * code to print TiB).
162a0075e58SWarner Losh 	 */
163a0075e58SWarner Losh 	mb = d->d_mediasize >> 20;	/* 1MiB == 1 << 20 */
164a0075e58SWarner Losh 	unit = 'M';
16558bb6260SAlexander Motin 	if (mb >= 10240) {		/* 1GiB = 1024 MiB */
166a0075e58SWarner Losh 		unit = 'G';
167a0075e58SWarner Losh 		mb /= 1024;
168a0075e58SWarner Losh 	}
1697aa65846SMarius Strobl 	/*
1707aa65846SMarius Strobl 	 * Report the clock speed of the underlying hardware, which might be
1717aa65846SMarius Strobl 	 * different than what the card reports due to hardware limitations.
1727aa65846SMarius Strobl 	 * Report how many blocks the hardware transfers at once, but clip the
1737aa65846SMarius Strobl 	 * number to MAXPHYS since the system won't initiate larger transfers.
1747aa65846SMarius Strobl 	 */
1757aa65846SMarius Strobl 	speed = mmcbr_get_clock(device_get_parent(dev));
1767aa65846SMarius Strobl 	maxblocks = mmc_get_max_data(dev);
1777aa65846SMarius Strobl 	if (maxblocks > MAXPHYS)
1787aa65846SMarius Strobl 		maxblocks = MAXPHYS;
1797aa65846SMarius Strobl 	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
1807aa65846SMarius Strobl 	    mb, unit, mmc_get_card_id_string(dev),
1810c0903b1SWarner Losh 	    mmc_get_read_only(dev) ? " (read-only)" : "",
182a0075e58SWarner Losh 	    device_get_nameunit(device_get_parent(dev)),
1837aa65846SMarius Strobl 	    speed / 1000000, (speed / 100000) % 10,
1847aa65846SMarius Strobl 	    mmcsd_bus_bit_width(dev), maxblocks);
185a0075e58SWarner Losh 	disk_create(d, DISK_VERSION);
186114b4164SWarner Losh 	bioq_init(&sc->bio_queue);
1870e1466acSWarner Losh 
1880e1466acSWarner Losh 	sc->running = 1;
189eb67f31aSAlexander Motin 	sc->suspend = 0;
190a7cf6274SAlexander Motin 	sc->eblock = sc->eend = 0;
1913745c395SJulian Elischer 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
192114b4164SWarner Losh 
193114b4164SWarner Losh 	return (0);
194114b4164SWarner Losh }
195114b4164SWarner Losh 
196114b4164SWarner Losh static int
197114b4164SWarner Losh mmcsd_detach(device_t dev)
198114b4164SWarner Losh {
1990e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
2000e1466acSWarner Losh 
2010e1466acSWarner Losh 	MMCSD_LOCK(sc);
202eb67f31aSAlexander Motin 	sc->suspend = 0;
203eb67f31aSAlexander Motin 	if (sc->running > 0) {
204eb67f31aSAlexander Motin 		/* kill thread */
2050e1466acSWarner Losh 		sc->running = 0;
2060e1466acSWarner Losh 		wakeup(sc);
207eb67f31aSAlexander Motin 		/* wait for thread to finish. */
2080e1466acSWarner Losh 		while (sc->running != -1)
209eb67f31aSAlexander Motin 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
210eb67f31aSAlexander Motin 	}
2110e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
2120e1466acSWarner Losh 
213249b0e85SAlexander Motin 	/* Flush the request queue. */
214249b0e85SAlexander Motin 	bioq_flush(&sc->bio_queue, NULL, ENXIO);
2150e1466acSWarner Losh 	/* kill disk */
2160e1466acSWarner Losh 	disk_destroy(sc->disk);
2170e1466acSWarner Losh 
2180e1466acSWarner Losh 	MMCSD_LOCK_DESTROY(sc);
2190e1466acSWarner Losh 
220128d0ff2SWarner Losh 	return (0);
221114b4164SWarner Losh }
222114b4164SWarner Losh 
223114b4164SWarner Losh static int
224eb67f31aSAlexander Motin mmcsd_suspend(device_t dev)
225eb67f31aSAlexander Motin {
226eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
227eb67f31aSAlexander Motin 
228eb67f31aSAlexander Motin 	MMCSD_LOCK(sc);
229eb67f31aSAlexander Motin 	sc->suspend = 1;
230eb67f31aSAlexander Motin 	if (sc->running > 0) {
231eb67f31aSAlexander Motin 		/* kill thread */
232eb67f31aSAlexander Motin 		sc->running = 0;
233eb67f31aSAlexander Motin 		wakeup(sc);
234eb67f31aSAlexander Motin 		/* wait for thread to finish. */
235eb67f31aSAlexander Motin 		while (sc->running != -1)
236eb67f31aSAlexander Motin 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
237eb67f31aSAlexander Motin 	}
238eb67f31aSAlexander Motin 	MMCSD_UNLOCK(sc);
239eb67f31aSAlexander Motin 	return (0);
240eb67f31aSAlexander Motin }
241eb67f31aSAlexander Motin 
242eb67f31aSAlexander Motin static int
243eb67f31aSAlexander Motin mmcsd_resume(device_t dev)
244eb67f31aSAlexander Motin {
245eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
246eb67f31aSAlexander Motin 
247eb67f31aSAlexander Motin 	MMCSD_LOCK(sc);
248eb67f31aSAlexander Motin 	sc->suspend = 0;
249eb67f31aSAlexander Motin 	if (sc->running <= 0) {
250eb67f31aSAlexander Motin 		sc->running = 1;
251eb67f31aSAlexander Motin 		MMCSD_UNLOCK(sc);
252eb67f31aSAlexander Motin 		kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
253eb67f31aSAlexander Motin 	} else
254eb67f31aSAlexander Motin 		MMCSD_UNLOCK(sc);
255eb67f31aSAlexander Motin 	return (0);
256eb67f31aSAlexander Motin }
257eb67f31aSAlexander Motin 
258eb67f31aSAlexander Motin static int
259114b4164SWarner Losh mmcsd_open(struct disk *dp)
260114b4164SWarner Losh {
261*ae5d8757SMarius Strobl 
262128d0ff2SWarner Losh 	return (0);
263114b4164SWarner Losh }
264114b4164SWarner Losh 
265114b4164SWarner Losh static int
266114b4164SWarner Losh mmcsd_close(struct disk *dp)
267114b4164SWarner Losh {
268*ae5d8757SMarius Strobl 
269128d0ff2SWarner Losh 	return (0);
270114b4164SWarner Losh }
271114b4164SWarner Losh 
272114b4164SWarner Losh static void
273114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
274114b4164SWarner Losh {
275114b4164SWarner Losh 	struct mmcsd_softc *sc;
276114b4164SWarner Losh 
277114b4164SWarner Losh 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
278114b4164SWarner Losh 	MMCSD_LOCK(sc);
279eb67f31aSAlexander Motin 	if (sc->running > 0 || sc->suspend > 0) {
280114b4164SWarner Losh 		bioq_disksort(&sc->bio_queue, bp);
281114b4164SWarner Losh 		MMCSD_UNLOCK(sc);
282eb67f31aSAlexander Motin 		wakeup(sc);
283249b0e85SAlexander Motin 	} else {
284249b0e85SAlexander Motin 		MMCSD_UNLOCK(sc);
285249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
286249b0e85SAlexander Motin 	}
287114b4164SWarner Losh }
288114b4164SWarner Losh 
2893906d42dSAlexander Motin static daddr_t
2903906d42dSAlexander Motin mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
291114b4164SWarner Losh {
292114b4164SWarner Losh 	daddr_t block, end;
293114b4164SWarner Losh 	struct mmc_command cmd;
294114b4164SWarner Losh 	struct mmc_command stop;
295114b4164SWarner Losh 	struct mmc_request req;
296114b4164SWarner Losh 	struct mmc_data data;
2973906d42dSAlexander Motin 	device_t dev = sc->dev;
2983906d42dSAlexander Motin 	int sz = sc->disk->d_sectorsize;
299114b4164SWarner Losh 
3003906d42dSAlexander Motin 	block = bp->bio_pblkno;
301114b4164SWarner Losh 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
3023906d42dSAlexander Motin 	while (block < end) {
3033906d42dSAlexander Motin 		char *vaddr = bp->bio_data +
3043906d42dSAlexander Motin 		    (block - bp->bio_pblkno) * sz;
3053a4a2557SAlexander Motin 		int numblocks = min(end - block, mmc_get_max_data(dev));
306114b4164SWarner Losh 		memset(&req, 0, sizeof(req));
307114b4164SWarner Losh     		memset(&cmd, 0, sizeof(cmd));
308114b4164SWarner Losh 		memset(&stop, 0, sizeof(stop));
309114b4164SWarner Losh 		req.cmd = &cmd;
310114b4164SWarner Losh 		cmd.data = &data;
311114b4164SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
3120c0903b1SWarner Losh 			if (numblocks > 1)
313114b4164SWarner Losh 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
314114b4164SWarner Losh 			else
315114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
3160c0903b1SWarner Losh 		} else {
3170c0903b1SWarner Losh 			if (numblocks > 1)
3180c0903b1SWarner Losh 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
3190c0903b1SWarner Losh 			else
320114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
3210c0903b1SWarner Losh 		}
322c18f1e26SAlexander Motin 		cmd.arg = block;
323c18f1e26SAlexander Motin 		if (!mmc_get_high_cap(dev))
324c18f1e26SAlexander Motin 			cmd.arg <<= 9;
325114b4164SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
326114b4164SWarner Losh 		data.data = vaddr;
327114b4164SWarner Losh 		data.mrq = &req;
3280c0903b1SWarner Losh 		if (bp->bio_cmd == BIO_READ)
329114b4164SWarner Losh 			data.flags = MMC_DATA_READ;
3300c0903b1SWarner Losh 		else
331114b4164SWarner Losh 			data.flags = MMC_DATA_WRITE;
3320c0903b1SWarner Losh 		data.len = numblocks * sz;
3330c0903b1SWarner Losh 		if (numblocks > 1) {
3340c0903b1SWarner Losh 			data.flags |= MMC_DATA_MULTI;
335114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
336114b4164SWarner Losh 			stop.arg = 0;
337114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
3380c0903b1SWarner Losh 			req.stop = &stop;
3390c0903b1SWarner Losh 		}
3400c0903b1SWarner Losh //		printf("Len %d  %lld-%lld flags %#x sz %d\n",
3410c0903b1SWarner Losh //		    (int)data.len, (long long)block, (long long)end, data.flags, sz);
342114b4164SWarner Losh 		MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
343114b4164SWarner Losh 		    &req);
3440c0903b1SWarner Losh 		if (req.cmd->error != MMC_ERR_NONE)
3450c0903b1SWarner Losh 			break;
3460c0903b1SWarner Losh 		block += numblocks;
347114b4164SWarner Losh 	}
3483906d42dSAlexander Motin 	return (block);
3493906d42dSAlexander Motin }
3503906d42dSAlexander Motin 
3513906d42dSAlexander Motin static daddr_t
3523906d42dSAlexander Motin mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
3533906d42dSAlexander Motin {
3543906d42dSAlexander Motin 	daddr_t block, end, start, stop;
3553906d42dSAlexander Motin 	struct mmc_command cmd;
3563906d42dSAlexander Motin 	struct mmc_request req;
3573906d42dSAlexander Motin 	device_t dev = sc->dev;
3583906d42dSAlexander Motin 	int sz = sc->disk->d_sectorsize;
3593906d42dSAlexander Motin 	int erase_sector;
3603906d42dSAlexander Motin 
3613906d42dSAlexander Motin 	block = bp->bio_pblkno;
3623906d42dSAlexander Motin 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
363a7cf6274SAlexander Motin 	/* Coalesce with part remaining from previous request. */
364a7cf6274SAlexander Motin 	if (block > sc->eblock && block <= sc->eend)
365a7cf6274SAlexander Motin 		block = sc->eblock;
366a7cf6274SAlexander Motin 	if (end >= sc->eblock && end < sc->eend)
367a7cf6274SAlexander Motin 		end = sc->eend;
3683906d42dSAlexander Motin 	/* Safe round to the erase sector boundaries. */
3693906d42dSAlexander Motin 	erase_sector = mmc_get_erase_sector(dev);
3703906d42dSAlexander Motin 	start = block + erase_sector - 1;	 /* Round up. */
3713906d42dSAlexander Motin 	start -= start % erase_sector;
3723906d42dSAlexander Motin 	stop = end;				/* Round down. */
3733906d42dSAlexander Motin 	stop -= end % erase_sector;
374a7cf6274SAlexander Motin 	/* We can't erase area smaller then sector, store it for later. */
375a7cf6274SAlexander Motin 	if (start >= stop) {
376a7cf6274SAlexander Motin 		sc->eblock = block;
377a7cf6274SAlexander Motin 		sc->eend = end;
3783906d42dSAlexander Motin 		return (end);
379a7cf6274SAlexander Motin 	}
3803906d42dSAlexander Motin 
3813906d42dSAlexander Motin 	/* Set erase start position. */
3823906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
3833906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
3843906d42dSAlexander Motin 	req.cmd = &cmd;
3853906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
3863906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_START;
3873906d42dSAlexander Motin 	else
3883906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_START;
3893906d42dSAlexander Motin 	cmd.arg = start;
3903906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
3913906d42dSAlexander Motin 		cmd.arg <<= 9;
3923906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3933906d42dSAlexander Motin 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
3943906d42dSAlexander Motin 	    &req);
3953906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
3963906d42dSAlexander Motin 	    printf("erase err1: %d\n", req.cmd->error);
3973906d42dSAlexander Motin 	    return (block);
3983906d42dSAlexander Motin 	}
3993906d42dSAlexander Motin 	/* Set erase stop position. */
4003906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
4013906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
4023906d42dSAlexander Motin 	req.cmd = &cmd;
4033906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
4043906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_END;
4053906d42dSAlexander Motin 	else
4063906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_END;
4073906d42dSAlexander Motin 	cmd.arg = stop;
4083906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
4093906d42dSAlexander Motin 		cmd.arg <<= 9;
4103906d42dSAlexander Motin 	cmd.arg--;
4113906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
4123906d42dSAlexander Motin 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
4133906d42dSAlexander Motin 	    &req);
4143906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
4153906d42dSAlexander Motin 	    printf("erase err2: %d\n", req.cmd->error);
4163906d42dSAlexander Motin 	    return (block);
4173906d42dSAlexander Motin 	}
4183906d42dSAlexander Motin 	/* Erase range. */
4193906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
4203906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
4213906d42dSAlexander Motin 	req.cmd = &cmd;
4223906d42dSAlexander Motin 	cmd.opcode = MMC_ERASE;
4233906d42dSAlexander Motin 	cmd.arg = 0;
4243906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
4253906d42dSAlexander Motin 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
4263906d42dSAlexander Motin 	    &req);
4273906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
4283906d42dSAlexander Motin 	    printf("erase err3 %d\n", req.cmd->error);
4293906d42dSAlexander Motin 	    return (block);
4303906d42dSAlexander Motin 	}
431a7cf6274SAlexander Motin 	/* Store one of remaining parts for the next call. */
432a7cf6274SAlexander Motin 	if (bp->bio_pblkno >= sc->eblock || block == start) {
433a7cf6274SAlexander Motin 		sc->eblock = stop;	/* Predict next forward. */
434a7cf6274SAlexander Motin 		sc->eend = end;
435a7cf6274SAlexander Motin 	} else {
436a7cf6274SAlexander Motin 		sc->eblock = block;	/* Predict next backward. */
437a7cf6274SAlexander Motin 		sc->eend = start;
438a7cf6274SAlexander Motin 	}
4393906d42dSAlexander Motin 	return (end);
4403906d42dSAlexander Motin }
4413906d42dSAlexander Motin 
442a1fda318SAlexander Motin static int
443a1fda318SAlexander Motin mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
444a1fda318SAlexander Motin 	off_t offset, size_t length)
445a1fda318SAlexander Motin {
446a1fda318SAlexander Motin 	struct disk *disk = arg;
447a1fda318SAlexander Motin 	struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
448a1fda318SAlexander Motin 	device_t dev = sc->dev;
449a1fda318SAlexander Motin 	struct bio bp;
450a1fda318SAlexander Motin 	daddr_t block, end;
451a1fda318SAlexander Motin 
452a1fda318SAlexander Motin 	/* length zero is special and really means flush buffers to media */
453a1fda318SAlexander Motin 	if (!length)
454a1fda318SAlexander Motin 		return (0);
455a1fda318SAlexander Motin 
456a1fda318SAlexander Motin 	bzero(&bp, sizeof(struct bio));
457a1fda318SAlexander Motin 	bp.bio_disk = disk;
458a1fda318SAlexander Motin 	bp.bio_pblkno = offset / disk->d_sectorsize;
459a1fda318SAlexander Motin 	bp.bio_bcount = length;
460a1fda318SAlexander Motin 	bp.bio_data = virtual;
461a1fda318SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
462a1fda318SAlexander Motin 	end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
463a1fda318SAlexander Motin 	MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
464a1fda318SAlexander Motin 	block = mmcsd_rw(sc, &bp);
465a1fda318SAlexander Motin 	MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
466a1fda318SAlexander Motin 	return ((end < block) ? EIO : 0);
467a1fda318SAlexander Motin }
468a1fda318SAlexander Motin 
4693906d42dSAlexander Motin static void
4703906d42dSAlexander Motin mmcsd_task(void *arg)
4713906d42dSAlexander Motin {
4723906d42dSAlexander Motin 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
4733906d42dSAlexander Motin 	struct bio *bp;
4743906d42dSAlexander Motin 	int sz;
4753906d42dSAlexander Motin 	daddr_t block, end;
4763906d42dSAlexander Motin 	device_t dev;
4773906d42dSAlexander Motin 
4783906d42dSAlexander Motin 	dev = sc->dev;
479249b0e85SAlexander Motin 	while (1) {
4803906d42dSAlexander Motin 		MMCSD_LOCK(sc);
4813906d42dSAlexander Motin 		do {
482249b0e85SAlexander Motin 			if (sc->running == 0)
483249b0e85SAlexander Motin 				goto out;
484249b0e85SAlexander Motin 			bp = bioq_takefirst(&sc->bio_queue);
4853906d42dSAlexander Motin 			if (bp == NULL)
4863906d42dSAlexander Motin 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
487249b0e85SAlexander Motin 		} while (bp == NULL);
4883906d42dSAlexander Motin 		MMCSD_UNLOCK(sc);
4893906d42dSAlexander Motin 		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
4903906d42dSAlexander Motin 			bp->bio_error = EROFS;
4913906d42dSAlexander Motin 			bp->bio_resid = bp->bio_bcount;
4923906d42dSAlexander Motin 			bp->bio_flags |= BIO_ERROR;
4933906d42dSAlexander Motin 			biodone(bp);
4943906d42dSAlexander Motin 			continue;
4953906d42dSAlexander Motin 		}
4963906d42dSAlexander Motin 		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
4973906d42dSAlexander Motin 		sz = sc->disk->d_sectorsize;
4983906d42dSAlexander Motin 		block = bp->bio_pblkno;
4993906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
5003906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
501a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
502a7cf6274SAlexander Motin 			if (block < sc->eend && end > sc->eblock)
503a7cf6274SAlexander Motin 				sc->eblock = sc->eend = 0;
5043906d42dSAlexander Motin 			block = mmcsd_rw(sc, bp);
5053906d42dSAlexander Motin 		} else if (bp->bio_cmd == BIO_DELETE) {
5063906d42dSAlexander Motin 			block = mmcsd_delete(sc, bp);
5073906d42dSAlexander Motin 		}
508114b4164SWarner Losh 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
5090c0903b1SWarner Losh 		if (block < end) {
5100c0903b1SWarner Losh 			bp->bio_error = EIO;
5110c0903b1SWarner Losh 			bp->bio_resid = (end - block) * sz;
5120c0903b1SWarner Losh 			bp->bio_flags |= BIO_ERROR;
5130c0903b1SWarner Losh 		}
514114b4164SWarner Losh 		biodone(bp);
515114b4164SWarner Losh 	}
516249b0e85SAlexander Motin out:
5170e1466acSWarner Losh 	/* tell parent we're done */
5180e1466acSWarner Losh 	sc->running = -1;
5190e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
520eb67f31aSAlexander Motin 	wakeup(sc);
5210e1466acSWarner Losh 
5223745c395SJulian Elischer 	kproc_exit(0);
523114b4164SWarner Losh }
524114b4164SWarner Losh 
525a0075e58SWarner Losh static int
526a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev)
527a0075e58SWarner Losh {
528*ae5d8757SMarius Strobl 
529a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_1)
530a0075e58SWarner Losh 		return (1);
531a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_4)
532a0075e58SWarner Losh 		return (4);
533a0075e58SWarner Losh 	return (8);
534a0075e58SWarner Losh }
535a0075e58SWarner Losh 
536114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
537114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
538114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
539114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
540eb67f31aSAlexander Motin 	DEVMETHOD(device_suspend, mmcsd_suspend),
541eb67f31aSAlexander Motin 	DEVMETHOD(device_resume, mmcsd_resume),
5427aa65846SMarius Strobl 	DEVMETHOD_END
543114b4164SWarner Losh };
544114b4164SWarner Losh 
545114b4164SWarner Losh static driver_t mmcsd_driver = {
546114b4164SWarner Losh 	"mmcsd",
547114b4164SWarner Losh 	mmcsd_methods,
548114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
549114b4164SWarner Losh };
550114b4164SWarner Losh static devclass_t mmcsd_devclass;
551114b4164SWarner Losh 
5527aa65846SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
553