mmcsd.c revision 312399
1/*-
2 * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification.  The following disclaimer may apply:
27 *
28 * The following conditions apply to the release of the simplified
29 * specification ("Simplified Specification") by the SD Card Association and
30 * the SD Group. The Simplified Specification is a subset of the complete SD
31 * Specification which is owned by the SD Card Association and the SD
32 * Group. This Simplified Specification is provided on a non-confidential
33 * basis subject to the disclaimers below. Any implementation of the
34 * Simplified Specification may require a license from the SD Card
35 * Association, SD Group, SD-3C LLC or other third parties.
36 *
37 * Disclaimers:
38 *
39 * The information contained in the Simplified Specification is presented only
40 * as a standard specification for SD Cards and SD Host/Ancillary products and
41 * is provided "AS-IS" without any representations or warranties of any
42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43 * Card Association for any damages, any infringements of patents or other
44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45 * parties, which may result from its use. No license is granted by
46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
51 */
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: stable/11/sys/dev/mmc/mmcsd.c 312399 2017-01-18 23:25:46Z marius $");
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/bio.h>
59#include <sys/bus.h>
60#include <sys/conf.h>
61#include <sys/kernel.h>
62#include <sys/kthread.h>
63#include <sys/lock.h>
64#include <sys/malloc.h>
65#include <sys/module.h>
66#include <sys/mutex.h>
67#include <sys/time.h>
68#include <geom/geom.h>
69#include <geom/geom_disk.h>
70
71#include <dev/mmc/mmcbrvar.h>
72#include <dev/mmc/mmcreg.h>
73#include <dev/mmc/mmcvar.h>
74
75#include "mmcbus_if.h"
76
77#if __FreeBSD_version < 800002
78#define	kproc_create	kthread_create
79#define	kproc_exit	kthread_exit
80#endif
81
82struct mmcsd_softc {
83	device_t dev;
84	struct mtx sc_mtx;
85	struct disk *disk;
86	struct proc *p;
87	struct bio_queue_head bio_queue;
88	daddr_t eblock, eend;	/* Range remaining after the last erase. */
89	int running;
90	int suspend;
91	int log_count;
92	struct timeval log_time;
93};
94
95static const char *errmsg[] =
96{
97	"None",
98	"Timeout",
99	"Bad CRC",
100	"Fifo",
101	"Failed",
102	"Invalid",
103	"NO MEMORY"
104};
105
106#define	LOG_PPS		5 /* Log no more than 5 errors per second. */
107
108/* bus entry points */
109static int mmcsd_attach(device_t dev);
110static int mmcsd_detach(device_t dev);
111static int mmcsd_probe(device_t dev);
112
113/* disk routines */
114static int mmcsd_close(struct disk *dp);
115static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
116	off_t offset, size_t length);
117static int mmcsd_open(struct disk *dp);
118static void mmcsd_strategy(struct bio *bp);
119static void mmcsd_task(void *arg);
120
121static int mmcsd_bus_bit_width(device_t dev);
122static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
123static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
124
125#define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
126#define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
127#define MMCSD_LOCK_INIT(_sc) \
128	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
129	    "mmcsd", MTX_DEF)
130#define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
131#define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
132#define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
133
134static int
135mmcsd_probe(device_t dev)
136{
137
138	device_quiet(dev);
139	device_set_desc(dev, "MMC/SD Memory Card");
140	return (0);
141}
142
143static int
144mmcsd_attach(device_t dev)
145{
146	struct mmcsd_softc *sc;
147	struct disk *d;
148	intmax_t mb;
149	uint32_t speed;
150	uint32_t maxblocks;
151	char unit;
152
153	sc = device_get_softc(dev);
154	sc->dev = dev;
155	MMCSD_LOCK_INIT(sc);
156
157	d = sc->disk = disk_alloc();
158	d->d_open = mmcsd_open;
159	d->d_close = mmcsd_close;
160	d->d_strategy = mmcsd_strategy;
161	d->d_dump = mmcsd_dump;
162	d->d_name = "mmcsd";
163	d->d_drv1 = sc;
164	d->d_sectorsize = mmc_get_sector_size(dev);
165	d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
166	d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
167	d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
168	d->d_unit = device_get_unit(dev);
169	d->d_flags = DISKFLAG_CANDELETE;
170	d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize;
171	strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident));
172	strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr));
173
174	/*
175	 * Display in most natural units.  There's no cards < 1MB.  The SD
176	 * standard goes to 2GiB due to its reliance on FAT, but the data
177	 * format supports up to 4GiB and some card makers push it up to this
178	 * limit.  The SDHC standard only goes to 32GiB due to FAT32, but the
179	 * data format supports up to 2TiB however. 2048GB isn't too ugly, so
180	 * we note it in passing here and don't add the code to print
181	 * TB). Since these cards are sold in terms of MB and GB not MiB and
182	 * GiB, report them like that. We also round to the nearest unit, since
183	 * many cards are a few percent short, even of the power of 10 size.
184	 */
185	mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000;
186	unit = 'M';
187	if (mb >= 1000) {
188		unit = 'G';
189		mb = (mb + 1000 / 2 - 1) / 1000;
190	}
191	/*
192	 * Report the clock speed of the underlying hardware, which might be
193	 * different than what the card reports due to hardware limitations.
194	 * Report how many blocks the hardware transfers at once.
195	 */
196	speed = mmcbr_get_clock(device_get_parent(dev));
197	maxblocks = mmc_get_max_data(dev);
198	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
199	    mb, unit, d->d_descr,
200	    mmc_get_read_only(dev) ? " (read-only)" : "",
201	    device_get_nameunit(device_get_parent(dev)),
202	    speed / 1000000, (speed / 100000) % 10,
203	    mmcsd_bus_bit_width(dev), maxblocks);
204	disk_create(d, DISK_VERSION);
205	bioq_init(&sc->bio_queue);
206
207	sc->running = 1;
208	sc->suspend = 0;
209	sc->eblock = sc->eend = 0;
210	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
211	    device_get_nameunit(dev));
212
213	return (0);
214}
215
216static int
217mmcsd_detach(device_t dev)
218{
219	struct mmcsd_softc *sc = device_get_softc(dev);
220
221	MMCSD_LOCK(sc);
222	sc->suspend = 0;
223	if (sc->running > 0) {
224		/* kill thread */
225		sc->running = 0;
226		wakeup(sc);
227		/* wait for thread to finish. */
228		while (sc->running != -1)
229			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
230	}
231	MMCSD_UNLOCK(sc);
232
233	/* Flush the request queue. */
234	bioq_flush(&sc->bio_queue, NULL, ENXIO);
235	/* kill disk */
236	disk_destroy(sc->disk);
237
238	MMCSD_LOCK_DESTROY(sc);
239
240	return (0);
241}
242
243static int
244mmcsd_suspend(device_t dev)
245{
246	struct mmcsd_softc *sc = device_get_softc(dev);
247
248	MMCSD_LOCK(sc);
249	sc->suspend = 1;
250	if (sc->running > 0) {
251		/* kill thread */
252		sc->running = 0;
253		wakeup(sc);
254		/* wait for thread to finish. */
255		while (sc->running != -1)
256			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
257	}
258	MMCSD_UNLOCK(sc);
259	return (0);
260}
261
262static int
263mmcsd_resume(device_t dev)
264{
265	struct mmcsd_softc *sc = device_get_softc(dev);
266
267	MMCSD_LOCK(sc);
268	sc->suspend = 0;
269	if (sc->running <= 0) {
270		sc->running = 1;
271		MMCSD_UNLOCK(sc);
272		kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
273		    device_get_nameunit(dev));
274	} else
275		MMCSD_UNLOCK(sc);
276	return (0);
277}
278
279static int
280mmcsd_open(struct disk *dp)
281{
282
283	return (0);
284}
285
286static int
287mmcsd_close(struct disk *dp)
288{
289
290	return (0);
291}
292
293static void
294mmcsd_strategy(struct bio *bp)
295{
296	struct mmcsd_softc *sc;
297
298	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
299	MMCSD_LOCK(sc);
300	if (sc->running > 0 || sc->suspend > 0) {
301		bioq_disksort(&sc->bio_queue, bp);
302		MMCSD_UNLOCK(sc);
303		wakeup(sc);
304	} else {
305		MMCSD_UNLOCK(sc);
306		biofinish(bp, NULL, ENXIO);
307	}
308}
309
310static const char *
311mmcsd_errmsg(int e)
312{
313	if (e < 0 || e > MMC_ERR_MAX)
314		return "Bad error code";
315	return errmsg[e];
316}
317
318static daddr_t
319mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
320{
321	daddr_t block, end;
322	struct mmc_command cmd;
323	struct mmc_command stop;
324	struct mmc_request req;
325	struct mmc_data data;
326	device_t dev = sc->dev;
327	int sz = sc->disk->d_sectorsize;
328	device_t mmcbr = device_get_parent(dev);
329
330	block = bp->bio_pblkno;
331	end = bp->bio_pblkno + (bp->bio_bcount / sz);
332	while (block < end) {
333		char *vaddr = bp->bio_data +
334		    (block - bp->bio_pblkno) * sz;
335		int numblocks = min(end - block, mmc_get_max_data(dev));
336		memset(&req, 0, sizeof(req));
337    		memset(&cmd, 0, sizeof(cmd));
338		memset(&stop, 0, sizeof(stop));
339		memset(&data, 0, sizeof(data));
340		cmd.mrq = &req;
341		req.cmd = &cmd;
342		cmd.data = &data;
343		if (bp->bio_cmd == BIO_READ) {
344			if (numblocks > 1)
345				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
346			else
347				cmd.opcode = MMC_READ_SINGLE_BLOCK;
348		} else {
349			if (numblocks > 1)
350				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
351			else
352				cmd.opcode = MMC_WRITE_BLOCK;
353		}
354		cmd.arg = block;
355		if (!mmc_get_high_cap(dev))
356			cmd.arg <<= 9;
357		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
358		data.data = vaddr;
359		data.mrq = &req;
360		if (bp->bio_cmd == BIO_READ)
361			data.flags = MMC_DATA_READ;
362		else
363			data.flags = MMC_DATA_WRITE;
364		data.len = numblocks * sz;
365		if (numblocks > 1) {
366			data.flags |= MMC_DATA_MULTI;
367			stop.opcode = MMC_STOP_TRANSMISSION;
368			stop.arg = 0;
369			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
370			stop.mrq = &req;
371			req.stop = &stop;
372		}
373		MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
374		if (req.cmd->error != MMC_ERR_NONE) {
375			if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
376				device_printf(dev, "Error indicated: %d %s\n",
377				    req.cmd->error, mmcsd_errmsg(req.cmd->error));
378			}
379			break;
380		}
381		block += numblocks;
382	}
383	return (block);
384}
385
386static daddr_t
387mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
388{
389	daddr_t block, end, start, stop;
390	struct mmc_command cmd;
391	struct mmc_request req;
392	device_t dev = sc->dev;
393	int sz = sc->disk->d_sectorsize;
394	int erase_sector;
395	device_t mmcbr = device_get_parent(dev);
396
397	block = bp->bio_pblkno;
398	end = bp->bio_pblkno + (bp->bio_bcount / sz);
399	/* Coalesce with part remaining from previous request. */
400	if (block > sc->eblock && block <= sc->eend)
401		block = sc->eblock;
402	if (end >= sc->eblock && end < sc->eend)
403		end = sc->eend;
404	/* Safe round to the erase sector boundaries. */
405	erase_sector = mmc_get_erase_sector(dev);
406	start = block + erase_sector - 1;	 /* Round up. */
407	start -= start % erase_sector;
408	stop = end;				/* Round down. */
409	stop -= end % erase_sector;
410	/* We can't erase area smaller then sector, store it for later. */
411	if (start >= stop) {
412		sc->eblock = block;
413		sc->eend = end;
414		return (end);
415	}
416
417	/* Set erase start position. */
418	memset(&req, 0, sizeof(req));
419	memset(&cmd, 0, sizeof(cmd));
420	cmd.mrq = &req;
421	req.cmd = &cmd;
422	if (mmc_get_card_type(dev) == mode_sd)
423		cmd.opcode = SD_ERASE_WR_BLK_START;
424	else
425		cmd.opcode = MMC_ERASE_GROUP_START;
426	cmd.arg = start;
427	if (!mmc_get_high_cap(dev))
428		cmd.arg <<= 9;
429	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
430	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
431	if (req.cmd->error != MMC_ERR_NONE) {
432	    printf("erase err1: %d\n", req.cmd->error);
433	    return (block);
434	}
435	/* Set erase stop position. */
436	memset(&req, 0, sizeof(req));
437	memset(&cmd, 0, sizeof(cmd));
438	req.cmd = &cmd;
439	if (mmc_get_card_type(dev) == mode_sd)
440		cmd.opcode = SD_ERASE_WR_BLK_END;
441	else
442		cmd.opcode = MMC_ERASE_GROUP_END;
443	cmd.arg = stop;
444	if (!mmc_get_high_cap(dev))
445		cmd.arg <<= 9;
446	cmd.arg--;
447	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
448	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
449	if (req.cmd->error != MMC_ERR_NONE) {
450	    printf("erase err2: %d\n", req.cmd->error);
451	    return (block);
452	}
453	/* Erase range. */
454	memset(&req, 0, sizeof(req));
455	memset(&cmd, 0, sizeof(cmd));
456	req.cmd = &cmd;
457	cmd.opcode = MMC_ERASE;
458	cmd.arg = 0;
459	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
460	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
461	if (req.cmd->error != MMC_ERR_NONE) {
462	    printf("erase err3 %d\n", req.cmd->error);
463	    return (block);
464	}
465	/* Store one of remaining parts for the next call. */
466	if (bp->bio_pblkno >= sc->eblock || block == start) {
467		sc->eblock = stop;	/* Predict next forward. */
468		sc->eend = end;
469	} else {
470		sc->eblock = block;	/* Predict next backward. */
471		sc->eend = start;
472	}
473	return (end);
474}
475
476static int
477mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
478	off_t offset, size_t length)
479{
480	struct disk *disk = arg;
481	struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
482	device_t dev = sc->dev;
483	struct bio bp;
484	daddr_t block, end;
485	device_t mmcbr = device_get_parent(dev);
486
487	/* length zero is special and really means flush buffers to media */
488	if (!length)
489		return (0);
490
491	g_reset_bio(&bp);
492	bp.bio_disk = disk;
493	bp.bio_pblkno = offset / disk->d_sectorsize;
494	bp.bio_bcount = length;
495	bp.bio_data = virtual;
496	bp.bio_cmd = BIO_WRITE;
497	end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
498	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
499	block = mmcsd_rw(sc, &bp);
500	MMCBUS_RELEASE_BUS(mmcbr, dev);
501	return ((end < block) ? EIO : 0);
502}
503
504static void
505mmcsd_task(void *arg)
506{
507	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
508	struct bio *bp;
509	int sz;
510	daddr_t block, end;
511	device_t dev = sc->dev;
512	device_t mmcbr = device_get_parent(sc->dev);
513
514	while (1) {
515		MMCSD_LOCK(sc);
516		do {
517			if (sc->running == 0)
518				goto out;
519			bp = bioq_takefirst(&sc->bio_queue);
520			if (bp == NULL)
521				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
522		} while (bp == NULL);
523		MMCSD_UNLOCK(sc);
524		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
525			bp->bio_error = EROFS;
526			bp->bio_resid = bp->bio_bcount;
527			bp->bio_flags |= BIO_ERROR;
528			biodone(bp);
529			continue;
530		}
531		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
532		sz = sc->disk->d_sectorsize;
533		block = bp->bio_pblkno;
534		end = bp->bio_pblkno + (bp->bio_bcount / sz);
535		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
536			/* Access to the remaining erase block obsoletes it. */
537			if (block < sc->eend && end > sc->eblock)
538				sc->eblock = sc->eend = 0;
539			block = mmcsd_rw(sc, bp);
540		} else if (bp->bio_cmd == BIO_DELETE) {
541			block = mmcsd_delete(sc, bp);
542		}
543		MMCBUS_RELEASE_BUS(mmcbr, dev);
544		if (block < end) {
545			bp->bio_error = EIO;
546			bp->bio_resid = (end - block) * sz;
547			bp->bio_flags |= BIO_ERROR;
548		} else {
549			bp->bio_resid = 0;
550		}
551		biodone(bp);
552	}
553out:
554	/* tell parent we're done */
555	sc->running = -1;
556	MMCSD_UNLOCK(sc);
557	wakeup(sc);
558
559	kproc_exit(0);
560}
561
562static int
563mmcsd_bus_bit_width(device_t dev)
564{
565
566	if (mmc_get_bus_width(dev) == bus_width_1)
567		return (1);
568	if (mmc_get_bus_width(dev) == bus_width_4)
569		return (4);
570	return (8);
571}
572
573static device_method_t mmcsd_methods[] = {
574	DEVMETHOD(device_probe, mmcsd_probe),
575	DEVMETHOD(device_attach, mmcsd_attach),
576	DEVMETHOD(device_detach, mmcsd_detach),
577	DEVMETHOD(device_suspend, mmcsd_suspend),
578	DEVMETHOD(device_resume, mmcsd_resume),
579	DEVMETHOD_END
580};
581
582static driver_t mmcsd_driver = {
583	"mmcsd",
584	mmcsd_methods,
585	sizeof(struct mmcsd_softc),
586};
587static devclass_t mmcsd_devclass;
588
589DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
590