xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 17160457b41ff26b4384d8f82ed176a2844eb272)
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>
675e3dbf8bSIan Lepore #include <sys/time.h>
68c55f5707SWarner Losh #include <geom/geom.h>
69114b4164SWarner Losh #include <geom/geom_disk.h>
70114b4164SWarner Losh 
717aa65846SMarius Strobl #include <dev/mmc/mmcbrvar.h>
72114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
737aa65846SMarius Strobl #include <dev/mmc/mmcvar.h>
74114b4164SWarner Losh 
75114b4164SWarner Losh #include "mmcbus_if.h"
76114b4164SWarner Losh 
777aa65846SMarius Strobl #if __FreeBSD_version < 800002
787aa65846SMarius Strobl #define	kproc_create	kthread_create
797aa65846SMarius Strobl #define	kproc_exit	kthread_exit
807aa65846SMarius Strobl #endif
817aa65846SMarius Strobl 
82114b4164SWarner Losh struct mmcsd_softc {
83114b4164SWarner Losh 	device_t dev;
84114b4164SWarner Losh 	struct mtx sc_mtx;
85114b4164SWarner Losh 	struct disk *disk;
86114b4164SWarner Losh 	struct proc *p;
87114b4164SWarner Losh 	struct bio_queue_head bio_queue;
88a7cf6274SAlexander Motin 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
890e1466acSWarner Losh 	int running;
90eb67f31aSAlexander Motin 	int suspend;
915e3dbf8bSIan Lepore 	int log_count;
925e3dbf8bSIan Lepore 	struct timeval log_time;
93114b4164SWarner Losh };
94114b4164SWarner Losh 
95bb1ef63fSWarner Losh static const char *errmsg[] =
96bb1ef63fSWarner Losh {
97bb1ef63fSWarner Losh 	"None",
98bb1ef63fSWarner Losh 	"Timeout",
99bb1ef63fSWarner Losh 	"Bad CRC",
100bb1ef63fSWarner Losh 	"Fifo",
101bb1ef63fSWarner Losh 	"Failed",
102bb1ef63fSWarner Losh 	"Invalid",
103bb1ef63fSWarner Losh 	"NO MEMORY"
104bb1ef63fSWarner Losh };
105bb1ef63fSWarner Losh 
1065e3dbf8bSIan Lepore #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
1075e3dbf8bSIan Lepore 
108114b4164SWarner Losh /* bus entry points */
109114b4164SWarner Losh static int mmcsd_attach(device_t dev);
110114b4164SWarner Losh static int mmcsd_detach(device_t dev);
111ae5d8757SMarius Strobl static int mmcsd_probe(device_t dev);
112114b4164SWarner Losh 
113114b4164SWarner Losh /* disk routines */
114114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
115a1fda318SAlexander Motin static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
116a1fda318SAlexander Motin 	off_t offset, size_t length);
117ae5d8757SMarius Strobl static int mmcsd_open(struct disk *dp);
118ae5d8757SMarius Strobl static void mmcsd_strategy(struct bio *bp);
119114b4164SWarner Losh static void mmcsd_task(void *arg);
120114b4164SWarner Losh 
121a0075e58SWarner Losh static int mmcsd_bus_bit_width(device_t dev);
122ae5d8757SMarius Strobl static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
123ae5d8757SMarius Strobl static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
124a0075e58SWarner Losh 
125114b4164SWarner Losh #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
126114b4164SWarner Losh #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
127114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \
128114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
129114b4164SWarner Losh 	    "mmcsd", MTX_DEF)
130114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
131114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
132114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
133114b4164SWarner Losh 
134114b4164SWarner Losh static int
135114b4164SWarner Losh mmcsd_probe(device_t dev)
136114b4164SWarner Losh {
137114b4164SWarner Losh 
138c18f1e26SAlexander Motin 	device_quiet(dev);
1390c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
140114b4164SWarner Losh 	return (0);
141114b4164SWarner Losh }
142114b4164SWarner Losh 
143114b4164SWarner Losh static int
144114b4164SWarner Losh mmcsd_attach(device_t dev)
145114b4164SWarner Losh {
146114b4164SWarner Losh 	struct mmcsd_softc *sc;
147a0075e58SWarner Losh 	struct disk *d;
148a0075e58SWarner Losh 	intmax_t mb;
1497aa65846SMarius Strobl 	uint32_t speed;
1507aa65846SMarius Strobl 	uint32_t maxblocks;
151a0075e58SWarner Losh 	char unit;
152114b4164SWarner Losh 
153114b4164SWarner Losh 	sc = device_get_softc(dev);
154114b4164SWarner Losh 	sc->dev = dev;
155114b4164SWarner Losh 	MMCSD_LOCK_INIT(sc);
156114b4164SWarner Losh 
157a0075e58SWarner Losh 	d = sc->disk = disk_alloc();
158a0075e58SWarner Losh 	d->d_open = mmcsd_open;
159a0075e58SWarner Losh 	d->d_close = mmcsd_close;
160a0075e58SWarner Losh 	d->d_strategy = mmcsd_strategy;
161a1fda318SAlexander Motin 	d->d_dump = mmcsd_dump;
162a0075e58SWarner Losh 	d->d_name = "mmcsd";
163a0075e58SWarner Losh 	d->d_drv1 = sc;
164a0075e58SWarner Losh 	d->d_sectorsize = mmc_get_sector_size(dev);
165a5928b20SWarner Losh 	d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
1661f89a4dcSAlexander Motin 	d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
1672262a519SAlexander Motin 	d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
168a0075e58SWarner Losh 	d->d_unit = device_get_unit(dev);
1693906d42dSAlexander Motin 	d->d_flags = DISKFLAG_CANDELETE;
170ca4a0f7bSMarcel Moolenaar 	d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize;
171f3a4b7f7SIan Lepore 	strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident));
172f3a4b7f7SIan Lepore 	strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr));
173*17160457SAlexander Motin 	d->d_rotation_rate = DISK_RR_NON_ROTATING;
174f3a4b7f7SIan Lepore 
175a0075e58SWarner Losh 	/*
1768fa3a540SWarner Losh 	 * Display in most natural units.  There's no cards < 1MB.  The SD
1778fa3a540SWarner Losh 	 * standard goes to 2GiB due to its reliance on FAT, but the data
1788fa3a540SWarner Losh 	 * format supports up to 4GiB and some card makers push it up to this
1798fa3a540SWarner Losh 	 * limit.  The SDHC standard only goes to 32GiB due to FAT32, but the
1808fa3a540SWarner Losh 	 * data format supports up to 2TiB however. 2048GB isn't too ugly, so
1818fa3a540SWarner Losh 	 * we note it in passing here and don't add the code to print
1828fa3a540SWarner Losh 	 * TB). Since these cards are sold in terms of MB and GB not MiB and
183c96e8c32SWarner Losh 	 * GiB, report them like that. We also round to the nearest unit, since
184c96e8c32SWarner Losh 	 * many cards are a few percent short, even of the power of 10 size.
185a0075e58SWarner Losh 	 */
186c96e8c32SWarner Losh 	mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000;
187a0075e58SWarner Losh 	unit = 'M';
1888fa3a540SWarner Losh 	if (mb >= 1000) {
189a0075e58SWarner Losh 		unit = 'G';
190c96e8c32SWarner Losh 		mb = (mb + 1000 / 2 - 1) / 1000;
191a0075e58SWarner Losh 	}
1927aa65846SMarius Strobl 	/*
1937aa65846SMarius Strobl 	 * Report the clock speed of the underlying hardware, which might be
1947aa65846SMarius Strobl 	 * different than what the card reports due to hardware limitations.
195bb1ef63fSWarner Losh 	 * Report how many blocks the hardware transfers at once.
1967aa65846SMarius Strobl 	 */
1977aa65846SMarius Strobl 	speed = mmcbr_get_clock(device_get_parent(dev));
1987aa65846SMarius Strobl 	maxblocks = mmc_get_max_data(dev);
1997aa65846SMarius Strobl 	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
200f3a4b7f7SIan Lepore 	    mb, unit, d->d_descr,
2010c0903b1SWarner Losh 	    mmc_get_read_only(dev) ? " (read-only)" : "",
202a0075e58SWarner Losh 	    device_get_nameunit(device_get_parent(dev)),
2037aa65846SMarius Strobl 	    speed / 1000000, (speed / 100000) % 10,
2047aa65846SMarius Strobl 	    mmcsd_bus_bit_width(dev), maxblocks);
205a0075e58SWarner Losh 	disk_create(d, DISK_VERSION);
206114b4164SWarner Losh 	bioq_init(&sc->bio_queue);
2070e1466acSWarner Losh 
2080e1466acSWarner Losh 	sc->running = 1;
209eb67f31aSAlexander Motin 	sc->suspend = 0;
210a7cf6274SAlexander Motin 	sc->eblock = sc->eend = 0;
21188e07d92SRui Paulo 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
21288e07d92SRui Paulo 	    device_get_nameunit(dev));
213114b4164SWarner Losh 
214114b4164SWarner Losh 	return (0);
215114b4164SWarner Losh }
216114b4164SWarner Losh 
217114b4164SWarner Losh static int
218114b4164SWarner Losh mmcsd_detach(device_t dev)
219114b4164SWarner Losh {
2200e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
2210e1466acSWarner Losh 
2220e1466acSWarner Losh 	MMCSD_LOCK(sc);
223eb67f31aSAlexander Motin 	sc->suspend = 0;
224eb67f31aSAlexander Motin 	if (sc->running > 0) {
225eb67f31aSAlexander Motin 		/* kill thread */
2260e1466acSWarner Losh 		sc->running = 0;
2270e1466acSWarner Losh 		wakeup(sc);
228eb67f31aSAlexander Motin 		/* wait for thread to finish. */
2290e1466acSWarner Losh 		while (sc->running != -1)
230eb67f31aSAlexander Motin 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
231eb67f31aSAlexander Motin 	}
2320e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
2330e1466acSWarner Losh 
234249b0e85SAlexander Motin 	/* Flush the request queue. */
235249b0e85SAlexander Motin 	bioq_flush(&sc->bio_queue, NULL, ENXIO);
2360e1466acSWarner Losh 	/* kill disk */
2370e1466acSWarner Losh 	disk_destroy(sc->disk);
2380e1466acSWarner Losh 
2390e1466acSWarner Losh 	MMCSD_LOCK_DESTROY(sc);
2400e1466acSWarner Losh 
241128d0ff2SWarner Losh 	return (0);
242114b4164SWarner Losh }
243114b4164SWarner Losh 
244114b4164SWarner Losh static int
245eb67f31aSAlexander Motin mmcsd_suspend(device_t dev)
246eb67f31aSAlexander Motin {
247eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
248eb67f31aSAlexander Motin 
249eb67f31aSAlexander Motin 	MMCSD_LOCK(sc);
250eb67f31aSAlexander Motin 	sc->suspend = 1;
251eb67f31aSAlexander Motin 	if (sc->running > 0) {
252eb67f31aSAlexander Motin 		/* kill thread */
253eb67f31aSAlexander Motin 		sc->running = 0;
254eb67f31aSAlexander Motin 		wakeup(sc);
255eb67f31aSAlexander Motin 		/* wait for thread to finish. */
256eb67f31aSAlexander Motin 		while (sc->running != -1)
257eb67f31aSAlexander Motin 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
258eb67f31aSAlexander Motin 	}
259eb67f31aSAlexander Motin 	MMCSD_UNLOCK(sc);
260eb67f31aSAlexander Motin 	return (0);
261eb67f31aSAlexander Motin }
262eb67f31aSAlexander Motin 
263eb67f31aSAlexander Motin static int
264eb67f31aSAlexander Motin mmcsd_resume(device_t dev)
265eb67f31aSAlexander Motin {
266eb67f31aSAlexander Motin 	struct mmcsd_softc *sc = device_get_softc(dev);
267eb67f31aSAlexander Motin 
268eb67f31aSAlexander Motin 	MMCSD_LOCK(sc);
269eb67f31aSAlexander Motin 	sc->suspend = 0;
270eb67f31aSAlexander Motin 	if (sc->running <= 0) {
271eb67f31aSAlexander Motin 		sc->running = 1;
272eb67f31aSAlexander Motin 		MMCSD_UNLOCK(sc);
27388e07d92SRui Paulo 		kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
27488e07d92SRui Paulo 		    device_get_nameunit(dev));
275eb67f31aSAlexander Motin 	} else
276eb67f31aSAlexander Motin 		MMCSD_UNLOCK(sc);
277eb67f31aSAlexander Motin 	return (0);
278eb67f31aSAlexander Motin }
279eb67f31aSAlexander Motin 
280eb67f31aSAlexander Motin static int
281114b4164SWarner Losh mmcsd_open(struct disk *dp)
282114b4164SWarner Losh {
283ae5d8757SMarius Strobl 
284128d0ff2SWarner Losh 	return (0);
285114b4164SWarner Losh }
286114b4164SWarner Losh 
287114b4164SWarner Losh static int
288114b4164SWarner Losh mmcsd_close(struct disk *dp)
289114b4164SWarner Losh {
290ae5d8757SMarius Strobl 
291128d0ff2SWarner Losh 	return (0);
292114b4164SWarner Losh }
293114b4164SWarner Losh 
294114b4164SWarner Losh static void
295114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
296114b4164SWarner Losh {
297114b4164SWarner Losh 	struct mmcsd_softc *sc;
298114b4164SWarner Losh 
299114b4164SWarner Losh 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
300114b4164SWarner Losh 	MMCSD_LOCK(sc);
301eb67f31aSAlexander Motin 	if (sc->running > 0 || sc->suspend > 0) {
302114b4164SWarner Losh 		bioq_disksort(&sc->bio_queue, bp);
303114b4164SWarner Losh 		MMCSD_UNLOCK(sc);
304eb67f31aSAlexander Motin 		wakeup(sc);
305249b0e85SAlexander Motin 	} else {
306249b0e85SAlexander Motin 		MMCSD_UNLOCK(sc);
307249b0e85SAlexander Motin 		biofinish(bp, NULL, ENXIO);
308249b0e85SAlexander Motin 	}
309114b4164SWarner Losh }
310114b4164SWarner Losh 
311bb1ef63fSWarner Losh static const char *
312bb1ef63fSWarner Losh mmcsd_errmsg(int e)
313bb1ef63fSWarner Losh {
314bb1ef63fSWarner Losh 	if (e < 0 || e > MMC_ERR_MAX)
315bb1ef63fSWarner Losh 		return "Bad error code";
316bb1ef63fSWarner Losh 	return errmsg[e];
317bb1ef63fSWarner Losh }
318bb1ef63fSWarner Losh 
3193906d42dSAlexander Motin static daddr_t
3203906d42dSAlexander Motin mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
321114b4164SWarner Losh {
322114b4164SWarner Losh 	daddr_t block, end;
323114b4164SWarner Losh 	struct mmc_command cmd;
324114b4164SWarner Losh 	struct mmc_command stop;
325114b4164SWarner Losh 	struct mmc_request req;
326114b4164SWarner Losh 	struct mmc_data data;
3273906d42dSAlexander Motin 	device_t dev = sc->dev;
3283906d42dSAlexander Motin 	int sz = sc->disk->d_sectorsize;
3298fa3a540SWarner Losh 	device_t mmcbr = device_get_parent(dev);
330114b4164SWarner Losh 
3313906d42dSAlexander Motin 	block = bp->bio_pblkno;
332114b4164SWarner Losh 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
3333906d42dSAlexander Motin 	while (block < end) {
3343906d42dSAlexander Motin 		char *vaddr = bp->bio_data +
3353906d42dSAlexander Motin 		    (block - bp->bio_pblkno) * sz;
3363a4a2557SAlexander Motin 		int numblocks = min(end - block, mmc_get_max_data(dev));
337114b4164SWarner Losh 		memset(&req, 0, sizeof(req));
338114b4164SWarner Losh     		memset(&cmd, 0, sizeof(cmd));
339114b4164SWarner Losh 		memset(&stop, 0, sizeof(stop));
340ed7142a7SIan Lepore 		memset(&data, 0, sizeof(data));
341a350e540SIan Lepore 		cmd.mrq = &req;
342114b4164SWarner Losh 		req.cmd = &cmd;
343114b4164SWarner Losh 		cmd.data = &data;
344114b4164SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
3450c0903b1SWarner Losh 			if (numblocks > 1)
346114b4164SWarner Losh 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
347114b4164SWarner Losh 			else
348114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
3490c0903b1SWarner Losh 		} else {
3500c0903b1SWarner Losh 			if (numblocks > 1)
3510c0903b1SWarner Losh 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
3520c0903b1SWarner Losh 			else
353114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
3540c0903b1SWarner Losh 		}
355c18f1e26SAlexander Motin 		cmd.arg = block;
356c18f1e26SAlexander Motin 		if (!mmc_get_high_cap(dev))
357c18f1e26SAlexander Motin 			cmd.arg <<= 9;
358114b4164SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
359114b4164SWarner Losh 		data.data = vaddr;
360114b4164SWarner Losh 		data.mrq = &req;
3610c0903b1SWarner Losh 		if (bp->bio_cmd == BIO_READ)
362114b4164SWarner Losh 			data.flags = MMC_DATA_READ;
3630c0903b1SWarner Losh 		else
364114b4164SWarner Losh 			data.flags = MMC_DATA_WRITE;
3650c0903b1SWarner Losh 		data.len = numblocks * sz;
3660c0903b1SWarner Losh 		if (numblocks > 1) {
3670c0903b1SWarner Losh 			data.flags |= MMC_DATA_MULTI;
368114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
369114b4164SWarner Losh 			stop.arg = 0;
370114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
371a350e540SIan Lepore 			stop.mrq = &req;
3720c0903b1SWarner Losh 			req.stop = &stop;
3730c0903b1SWarner Losh 		}
3748fa3a540SWarner Losh 		MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
375bb1ef63fSWarner Losh 		if (req.cmd->error != MMC_ERR_NONE) {
3765e3dbf8bSIan Lepore 			if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
377bb1ef63fSWarner Losh 				device_printf(dev, "Error indicated: %d %s\n",
378bb1ef63fSWarner Losh 				    req.cmd->error, mmcsd_errmsg(req.cmd->error));
3795e3dbf8bSIan Lepore 			}
3800c0903b1SWarner Losh 			break;
381bb1ef63fSWarner Losh 		}
3820c0903b1SWarner Losh 		block += numblocks;
383114b4164SWarner Losh 	}
3843906d42dSAlexander Motin 	return (block);
3853906d42dSAlexander Motin }
3863906d42dSAlexander Motin 
3873906d42dSAlexander Motin static daddr_t
3883906d42dSAlexander Motin mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
3893906d42dSAlexander Motin {
3903906d42dSAlexander Motin 	daddr_t block, end, start, stop;
3913906d42dSAlexander Motin 	struct mmc_command cmd;
3923906d42dSAlexander Motin 	struct mmc_request req;
3933906d42dSAlexander Motin 	device_t dev = sc->dev;
3943906d42dSAlexander Motin 	int sz = sc->disk->d_sectorsize;
3953906d42dSAlexander Motin 	int erase_sector;
3968fa3a540SWarner Losh 	device_t mmcbr = device_get_parent(dev);
3973906d42dSAlexander Motin 
3983906d42dSAlexander Motin 	block = bp->bio_pblkno;
3993906d42dSAlexander Motin 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
400a7cf6274SAlexander Motin 	/* Coalesce with part remaining from previous request. */
401a7cf6274SAlexander Motin 	if (block > sc->eblock && block <= sc->eend)
402a7cf6274SAlexander Motin 		block = sc->eblock;
403a7cf6274SAlexander Motin 	if (end >= sc->eblock && end < sc->eend)
404a7cf6274SAlexander Motin 		end = sc->eend;
4053906d42dSAlexander Motin 	/* Safe round to the erase sector boundaries. */
4063906d42dSAlexander Motin 	erase_sector = mmc_get_erase_sector(dev);
4073906d42dSAlexander Motin 	start = block + erase_sector - 1;	 /* Round up. */
4083906d42dSAlexander Motin 	start -= start % erase_sector;
4093906d42dSAlexander Motin 	stop = end;				/* Round down. */
4103906d42dSAlexander Motin 	stop -= end % erase_sector;
411a7cf6274SAlexander Motin 	/* We can't erase area smaller then sector, store it for later. */
412a7cf6274SAlexander Motin 	if (start >= stop) {
413a7cf6274SAlexander Motin 		sc->eblock = block;
414a7cf6274SAlexander Motin 		sc->eend = end;
4153906d42dSAlexander Motin 		return (end);
416a7cf6274SAlexander Motin 	}
4173906d42dSAlexander Motin 
4183906d42dSAlexander Motin 	/* Set erase start position. */
4193906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
4203906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
421a350e540SIan Lepore 	cmd.mrq = &req;
4223906d42dSAlexander Motin 	req.cmd = &cmd;
4233906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
4243906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_START;
4253906d42dSAlexander Motin 	else
4263906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_START;
4273906d42dSAlexander Motin 	cmd.arg = start;
4283906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
4293906d42dSAlexander Motin 		cmd.arg <<= 9;
4303906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
4318fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
4323906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
4333906d42dSAlexander Motin 	    printf("erase err1: %d\n", req.cmd->error);
4343906d42dSAlexander Motin 	    return (block);
4353906d42dSAlexander Motin 	}
4363906d42dSAlexander Motin 	/* Set erase stop position. */
4373906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
4383906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
4393906d42dSAlexander Motin 	req.cmd = &cmd;
4403906d42dSAlexander Motin 	if (mmc_get_card_type(dev) == mode_sd)
4413906d42dSAlexander Motin 		cmd.opcode = SD_ERASE_WR_BLK_END;
4423906d42dSAlexander Motin 	else
4433906d42dSAlexander Motin 		cmd.opcode = MMC_ERASE_GROUP_END;
4443906d42dSAlexander Motin 	cmd.arg = stop;
4453906d42dSAlexander Motin 	if (!mmc_get_high_cap(dev))
4463906d42dSAlexander Motin 		cmd.arg <<= 9;
4473906d42dSAlexander Motin 	cmd.arg--;
4483906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
4498fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
4503906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
4513906d42dSAlexander Motin 	    printf("erase err2: %d\n", req.cmd->error);
4523906d42dSAlexander Motin 	    return (block);
4533906d42dSAlexander Motin 	}
4543906d42dSAlexander Motin 	/* Erase range. */
4553906d42dSAlexander Motin 	memset(&req, 0, sizeof(req));
4563906d42dSAlexander Motin 	memset(&cmd, 0, sizeof(cmd));
4573906d42dSAlexander Motin 	req.cmd = &cmd;
4583906d42dSAlexander Motin 	cmd.opcode = MMC_ERASE;
4593906d42dSAlexander Motin 	cmd.arg = 0;
4603906d42dSAlexander Motin 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
4618fa3a540SWarner Losh 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
4623906d42dSAlexander Motin 	if (req.cmd->error != MMC_ERR_NONE) {
4633906d42dSAlexander Motin 	    printf("erase err3 %d\n", req.cmd->error);
4643906d42dSAlexander Motin 	    return (block);
4653906d42dSAlexander Motin 	}
466a7cf6274SAlexander Motin 	/* Store one of remaining parts for the next call. */
467a7cf6274SAlexander Motin 	if (bp->bio_pblkno >= sc->eblock || block == start) {
468a7cf6274SAlexander Motin 		sc->eblock = stop;	/* Predict next forward. */
469a7cf6274SAlexander Motin 		sc->eend = end;
470a7cf6274SAlexander Motin 	} else {
471a7cf6274SAlexander Motin 		sc->eblock = block;	/* Predict next backward. */
472a7cf6274SAlexander Motin 		sc->eend = start;
473a7cf6274SAlexander Motin 	}
4743906d42dSAlexander Motin 	return (end);
4753906d42dSAlexander Motin }
4763906d42dSAlexander Motin 
477a1fda318SAlexander Motin static int
478a1fda318SAlexander Motin mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
479a1fda318SAlexander Motin 	off_t offset, size_t length)
480a1fda318SAlexander Motin {
481a1fda318SAlexander Motin 	struct disk *disk = arg;
482a1fda318SAlexander Motin 	struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
483a1fda318SAlexander Motin 	device_t dev = sc->dev;
484a1fda318SAlexander Motin 	struct bio bp;
485a1fda318SAlexander Motin 	daddr_t block, end;
4868fa3a540SWarner Losh 	device_t mmcbr = device_get_parent(dev);
487a1fda318SAlexander Motin 
488a1fda318SAlexander Motin 	/* length zero is special and really means flush buffers to media */
489a1fda318SAlexander Motin 	if (!length)
490a1fda318SAlexander Motin 		return (0);
491a1fda318SAlexander Motin 
492c55f5707SWarner Losh 	g_reset_bio(&bp);
493a1fda318SAlexander Motin 	bp.bio_disk = disk;
494a1fda318SAlexander Motin 	bp.bio_pblkno = offset / disk->d_sectorsize;
495a1fda318SAlexander Motin 	bp.bio_bcount = length;
496a1fda318SAlexander Motin 	bp.bio_data = virtual;
497a1fda318SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
498a1fda318SAlexander Motin 	end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
4998fa3a540SWarner Losh 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
500a1fda318SAlexander Motin 	block = mmcsd_rw(sc, &bp);
5018fa3a540SWarner Losh 	MMCBUS_RELEASE_BUS(mmcbr, dev);
502a1fda318SAlexander Motin 	return ((end < block) ? EIO : 0);
503a1fda318SAlexander Motin }
504a1fda318SAlexander Motin 
5053906d42dSAlexander Motin static void
5063906d42dSAlexander Motin mmcsd_task(void *arg)
5073906d42dSAlexander Motin {
5083906d42dSAlexander Motin 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
5093906d42dSAlexander Motin 	struct bio *bp;
5103906d42dSAlexander Motin 	int sz;
5113906d42dSAlexander Motin 	daddr_t block, end;
5128fa3a540SWarner Losh 	device_t dev = sc->dev;
5138fa3a540SWarner Losh 	device_t mmcbr = device_get_parent(sc->dev);
5143906d42dSAlexander Motin 
515249b0e85SAlexander Motin 	while (1) {
5163906d42dSAlexander Motin 		MMCSD_LOCK(sc);
5173906d42dSAlexander Motin 		do {
518249b0e85SAlexander Motin 			if (sc->running == 0)
519249b0e85SAlexander Motin 				goto out;
520249b0e85SAlexander Motin 			bp = bioq_takefirst(&sc->bio_queue);
5213906d42dSAlexander Motin 			if (bp == NULL)
5223906d42dSAlexander Motin 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
523249b0e85SAlexander Motin 		} while (bp == NULL);
5243906d42dSAlexander Motin 		MMCSD_UNLOCK(sc);
5253906d42dSAlexander Motin 		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
5263906d42dSAlexander Motin 			bp->bio_error = EROFS;
5273906d42dSAlexander Motin 			bp->bio_resid = bp->bio_bcount;
5283906d42dSAlexander Motin 			bp->bio_flags |= BIO_ERROR;
5293906d42dSAlexander Motin 			biodone(bp);
5303906d42dSAlexander Motin 			continue;
5313906d42dSAlexander Motin 		}
5328fa3a540SWarner Losh 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
5333906d42dSAlexander Motin 		sz = sc->disk->d_sectorsize;
5343906d42dSAlexander Motin 		block = bp->bio_pblkno;
5353906d42dSAlexander Motin 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
5363906d42dSAlexander Motin 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
537a7cf6274SAlexander Motin 			/* Access to the remaining erase block obsoletes it. */
538a7cf6274SAlexander Motin 			if (block < sc->eend && end > sc->eblock)
539a7cf6274SAlexander Motin 				sc->eblock = sc->eend = 0;
5403906d42dSAlexander Motin 			block = mmcsd_rw(sc, bp);
5413906d42dSAlexander Motin 		} else if (bp->bio_cmd == BIO_DELETE) {
5423906d42dSAlexander Motin 			block = mmcsd_delete(sc, bp);
5433906d42dSAlexander Motin 		}
5448fa3a540SWarner Losh 		MMCBUS_RELEASE_BUS(mmcbr, dev);
5450c0903b1SWarner Losh 		if (block < end) {
5460c0903b1SWarner Losh 			bp->bio_error = EIO;
5470c0903b1SWarner Losh 			bp->bio_resid = (end - block) * sz;
5480c0903b1SWarner Losh 			bp->bio_flags |= BIO_ERROR;
549d119f637SMarius Strobl 		} else {
550d119f637SMarius Strobl 			bp->bio_resid = 0;
5510c0903b1SWarner Losh 		}
552114b4164SWarner Losh 		biodone(bp);
553114b4164SWarner Losh 	}
554249b0e85SAlexander Motin out:
5550e1466acSWarner Losh 	/* tell parent we're done */
5560e1466acSWarner Losh 	sc->running = -1;
5570e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
558eb67f31aSAlexander Motin 	wakeup(sc);
5590e1466acSWarner Losh 
5603745c395SJulian Elischer 	kproc_exit(0);
561114b4164SWarner Losh }
562114b4164SWarner Losh 
563a0075e58SWarner Losh static int
564a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev)
565a0075e58SWarner Losh {
566ae5d8757SMarius Strobl 
567a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_1)
568a0075e58SWarner Losh 		return (1);
569a0075e58SWarner Losh 	if (mmc_get_bus_width(dev) == bus_width_4)
570a0075e58SWarner Losh 		return (4);
571a0075e58SWarner Losh 	return (8);
572a0075e58SWarner Losh }
573a0075e58SWarner Losh 
574114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
575114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
576114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
577114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
578eb67f31aSAlexander Motin 	DEVMETHOD(device_suspend, mmcsd_suspend),
579eb67f31aSAlexander Motin 	DEVMETHOD(device_resume, mmcsd_resume),
5807aa65846SMarius Strobl 	DEVMETHOD_END
581114b4164SWarner Losh };
582114b4164SWarner Losh 
583114b4164SWarner Losh static driver_t mmcsd_driver = {
584114b4164SWarner Losh 	"mmcsd",
585114b4164SWarner Losh 	mmcsd_methods,
586114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
587114b4164SWarner Losh };
588114b4164SWarner Losh static devclass_t mmcsd_devclass;
589114b4164SWarner Losh 
5907aa65846SMarius Strobl DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
591