Home | History | Annotate | Line # | Download | only in sdmmc
ld_sdmmc.c revision 1.35
      1 /*	$NetBSD: ld_sdmmc.c,v 1.35 2018/11/09 14:39:19 jmcneill Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 KIYOHARA Takashi
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.35 2018/11/09 14:39:19 jmcneill Exp $");
     32 
     33 #ifdef _KERNEL_OPT
     34 #include "opt_sdmmc.h"
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/buf.h>
     42 #include <sys/bufq.h>
     43 #include <sys/bus.h>
     44 #include <sys/endian.h>
     45 #include <sys/dkio.h>
     46 #include <sys/disk.h>
     47 #include <sys/disklabel.h>
     48 #include <sys/kthread.h>
     49 #include <sys/syslog.h>
     50 #include <sys/module.h>
     51 #include <sys/pcq.h>
     52 
     53 #include <dev/ldvar.h>
     54 
     55 #include <dev/sdmmc/sdmmcvar.h>
     56 
     57 #include "ioconf.h"
     58 
     59 #ifdef LD_SDMMC_DEBUG
     60 #define DPRINTF(s)	printf s
     61 #else
     62 #define DPRINTF(s)	/**/
     63 #endif
     64 
     65 #define	LD_SDMMC_IORETRIES	5	/* number of retries before giving up */
     66 #define	RECOVERYTIME		hz/2	/* time to wait before retrying a cmd */
     67 
     68 #define	LD_SDMMC_MAXQUEUECNT	4	/* number of queued bio requests */
     69 #define	LD_SDMMC_MAXTASKCNT	8	/* number of tasks in task pool */
     70 
     71 struct ld_sdmmc_softc;
     72 
     73 struct ld_sdmmc_task {
     74 	struct sdmmc_task task;
     75 
     76 	struct ld_sdmmc_softc *task_sc;
     77 
     78 	struct buf *task_bp;
     79 	int task_retries; /* number of xfer retry */
     80 	struct callout task_restart_ch;
     81 
     82 	kmutex_t task_lock;
     83 	kcondvar_t task_cv;
     84 
     85 	uintptr_t task_data;
     86 };
     87 
     88 struct ld_sdmmc_softc {
     89 	struct ld_softc sc_ld;
     90 	int sc_hwunit;
     91 
     92 	struct sdmmc_function *sc_sf;
     93 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
     94 	pcq_t *sc_freeq;
     95 
     96 	struct evcnt sc_ev_discard;	/* discard counter */
     97 	struct evcnt sc_ev_discarderr;	/* discard error counter */
     98 	struct evcnt sc_ev_discardbusy;	/* discard busy counter */
     99 	struct evcnt sc_ev_cachesyncbusy; /* cache sync busy counter */
    100 };
    101 
    102 static int ld_sdmmc_match(device_t, cfdata_t, void *);
    103 static void ld_sdmmc_attach(device_t, device_t, void *);
    104 static int ld_sdmmc_detach(device_t, int);
    105 
    106 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
    107 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
    108 static void ld_sdmmc_restart(void *);
    109 static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
    110 static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
    111 
    112 static void ld_sdmmc_doattach(void *);
    113 static void ld_sdmmc_dobio(void *);
    114 static void ld_sdmmc_dodiscard(void *);
    115 
    116 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
    117     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
    118 
    119 
    120 /* ARGSUSED */
    121 static int
    122 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
    123 {
    124 	struct sdmmc_softc *sdmsc = device_private(parent);
    125 
    126 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
    127 		return 1;
    128 	return 0;
    129 }
    130 
    131 /* ARGSUSED */
    132 static void
    133 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
    134 {
    135 	struct ld_sdmmc_softc *sc = device_private(self);
    136 	struct sdmmc_attach_args *sa = aux;
    137 	struct ld_softc *ld = &sc->sc_ld;
    138 	struct ld_sdmmc_task *task;
    139 	struct lwp *lwp;
    140 	int i;
    141 
    142 	ld->sc_dv = self;
    143 
    144 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
    145 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
    146 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
    147 	aprint_naive("\n");
    148 
    149 	evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
    150 	    NULL, device_xname(self), "sdmmc discard count");
    151 	evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
    152 	    NULL, device_xname(self), "sdmmc discard errors");
    153 	evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
    154 	    NULL, device_xname(self), "sdmmc discard busy");
    155 
    156 	const int ntask = __arraycount(sc->sc_task);
    157 	sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
    158 	for (i = 0; i < ntask; i++) {
    159 		task = &sc->sc_task[i];
    160 		task->task_sc = sc;
    161 		callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
    162 		mutex_init(&task->task_lock, MUTEX_DEFAULT, IPL_NONE);
    163 		cv_init(&task->task_cv, "ldsdmmctask");
    164 		pcq_put(sc->sc_freeq, task);
    165 	}
    166 
    167 	sc->sc_hwunit = 0;	/* always 0? */
    168 	sc->sc_sf = sa->sf;
    169 
    170 	ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
    171 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
    172 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
    173 	ld->sc_maxxfer = MAXPHYS;
    174 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
    175 	ld->sc_dump = ld_sdmmc_dump;
    176 	ld->sc_start = ld_sdmmc_start;
    177 	ld->sc_discard = ld_sdmmc_discard;
    178 	ld->sc_ioctl = ld_sdmmc_ioctl;
    179 
    180 	/*
    181 	 * Defer attachment of ld + disk subsystem to a thread.
    182 	 *
    183 	 * This is necessary because wedge autodiscover needs to
    184 	 * open and call into the ld driver, which could deadlock
    185 	 * when the sdmmc driver isn't ready in early bootstrap.
    186 	 *
    187 	 * Don't mark thread as MPSAFE to keep aprint output sane.
    188 	 */
    189 	config_pending_incr(self);
    190 	if (kthread_create(PRI_NONE, 0, NULL,
    191 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
    192 		aprint_error_dev(self, "couldn't create thread\n");
    193 	}
    194 }
    195 
    196 static void
    197 ld_sdmmc_doattach(void *arg)
    198 {
    199 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
    200 	struct ld_softc *ld = &sc->sc_ld;
    201 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
    202 	const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
    203 	char buf[sizeof("9999 KB")];
    204 
    205 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    206 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
    207 	if (ssc->sc_transfer_mode != NULL)
    208 		aprint_normal(" %s,", ssc->sc_transfer_mode);
    209 	if (cache_size > 0) {
    210 		format_bytes(buf, sizeof(buf), cache_size);
    211 		aprint_normal(" %s cache%s,", buf,
    212 		    ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
    213 		    " (disabled)");
    214 	}
    215 	if ((ssc->sc_busclk / 1000) != 0)
    216 		aprint_normal(" %u.%03u MHz\n",
    217 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
    218 	else
    219 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
    220 	config_pending_decr(ld->sc_dv);
    221 	kthread_exit(0);
    222 }
    223 
    224 static int
    225 ld_sdmmc_detach(device_t dev, int flags)
    226 {
    227 	struct ld_sdmmc_softc *sc = device_private(dev);
    228 	struct ld_softc *ld = &sc->sc_ld;
    229 	int rv, i;
    230 
    231 	if ((rv = ldbegindetach(ld, flags)) != 0)
    232 		return rv;
    233 	ldenddetach(ld);
    234 
    235 	for (i = 0; i < __arraycount(sc->sc_task); i++) {
    236 		callout_destroy(&sc->sc_task[i].task_restart_ch);
    237 		mutex_destroy(&sc->sc_task[i].task_lock);
    238 		cv_destroy(&sc->sc_task[i].task_cv);
    239 	}
    240 
    241 	pcq_destroy(sc->sc_freeq);
    242 	evcnt_detach(&sc->sc_ev_discard);
    243 	evcnt_detach(&sc->sc_ev_discarderr);
    244 	evcnt_detach(&sc->sc_ev_discardbusy);
    245 
    246 	return 0;
    247 }
    248 
    249 static int
    250 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
    251 {
    252 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    253 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    254 
    255 	if (task == NULL)
    256 		return EAGAIN;
    257 
    258 	task->task_bp = bp;
    259 	task->task_retries = 0;
    260 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
    261 
    262 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    263 
    264 	return 0;
    265 }
    266 
    267 static void
    268 ld_sdmmc_restart(void *arg)
    269 {
    270 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    271 	struct ld_sdmmc_softc *sc = task->task_sc;
    272 	struct buf *bp = task->task_bp;
    273 
    274 	bp->b_resid = bp->b_bcount;
    275 
    276 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    277 }
    278 
    279 static void
    280 ld_sdmmc_dobio(void *arg)
    281 {
    282 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    283 	struct ld_sdmmc_softc *sc = task->task_sc;
    284 	struct buf *bp = task->task_bp;
    285 	int error;
    286 
    287 	/*
    288 	 * I/O operation
    289 	 */
    290 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
    291 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
    292 	    bp->b_rawblkno, bp->b_bcount));
    293 
    294 	/* is everything done in terms of blocks? */
    295 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
    296 		/* trying to read or write past end of device */
    297 		aprint_error_dev(sc->sc_ld.sc_dv,
    298 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
    299 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
    300 		bp->b_error = EINVAL;
    301 		bp->b_resid = bp->b_bcount;
    302 
    303 		goto done;
    304 	}
    305 
    306 	if (bp->b_flags & B_READ)
    307 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
    308 		    bp->b_data, bp->b_bcount);
    309 	else
    310 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
    311 		    bp->b_data, bp->b_bcount);
    312 	if (error) {
    313 		if (task->task_retries < LD_SDMMC_IORETRIES) {
    314 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
    315 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    316 
    317 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    318 				dksc->sc_dkdev.dk_label);
    319 			printf(", retrying\n");
    320 			task->task_retries++;
    321 			callout_reset(&task->task_restart_ch, RECOVERYTIME,
    322 			    ld_sdmmc_restart, task);
    323 			return;
    324 		}
    325 		bp->b_error = error;
    326 		bp->b_resid = bp->b_bcount;
    327 	} else {
    328 		bp->b_resid = 0;
    329 	}
    330 
    331 done:
    332 	pcq_put(sc->sc_freeq, task);
    333 
    334 	lddone(&sc->sc_ld, bp);
    335 }
    336 
    337 static int
    338 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    339 {
    340 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    341 
    342 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
    343 	    blkcnt * ld->sc_secsize);
    344 }
    345 
    346 static void
    347 ld_sdmmc_dodiscard(void *arg)
    348 {
    349 	struct ld_sdmmc_task *task = arg;
    350 	struct ld_sdmmc_softc *sc = task->task_sc;
    351 	struct buf *bp = task->task_bp;
    352 	uint32_t sblkno, nblks;
    353 	int error;
    354 
    355 	/* first and last block to erase */
    356 	sblkno = bp->b_rawblkno;
    357 	nblks  = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
    358 
    359 	/* An error from discard is non-fatal */
    360 	error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
    361 	if (error != 0)
    362 		sc->sc_ev_discarderr.ev_count++;
    363 	else
    364 		sc->sc_ev_discard.ev_count++;
    365 	pcq_put(sc->sc_freeq, task);
    366 
    367 	if (error)
    368 		bp->b_error = error;
    369 
    370 	lddiscardend(&sc->sc_ld, bp);
    371 }
    372 
    373 static int
    374 ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
    375 {
    376 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    377 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    378 
    379 	if (task == NULL) {
    380 		sc->sc_ev_discardbusy.ev_count++;
    381 		return 0;
    382 	}
    383 
    384 	task->task_bp = bp;
    385 
    386 	sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
    387 
    388 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    389 
    390 	return 0;
    391 }
    392 
    393 static void
    394 ld_sdmmc_docachesync(void *arg)
    395 {
    396 	struct ld_sdmmc_task *task = arg;
    397 	struct ld_sdmmc_softc *sc = task->task_sc;
    398 	const bool poll = (bool)task->task_data;
    399 
    400 	task->task_data = sdmmc_mem_flush_cache(sc->sc_sf, poll);
    401 
    402 	mutex_enter(&task->task_lock);
    403 	cv_signal(&task->task_cv);
    404 	mutex_exit(&task->task_lock);
    405 }
    406 
    407 static int
    408 ld_sdmmc_cachesync(struct ld_softc *ld, bool poll)
    409 {
    410 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    411 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    412 	int error = 0;
    413 
    414 	if (task == NULL) {
    415 		sc->sc_ev_cachesyncbusy.ev_count++;
    416 		return EBUSY;
    417 	}
    418 
    419 	sdmmc_init_task(&task->task, ld_sdmmc_docachesync, task);
    420 	task->task_data = poll;
    421 
    422 	mutex_enter(&task->task_lock);
    423 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    424 	error = cv_wait_sig(&task->task_cv, &task->task_lock);
    425 	mutex_exit(&task->task_lock);
    426 
    427 	if (error == 0)
    428 		error = (int)task->task_data;
    429 
    430 	pcq_put(sc->sc_freeq, task);
    431 
    432 	return error;
    433 }
    434 
    435 static int
    436 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
    437     bool poll)
    438 {
    439 
    440 	switch (cmd) {
    441 	case DIOCCACHESYNC:
    442 		return ld_sdmmc_cachesync(ld, poll);
    443 	default:
    444 		return EPASSTHROUGH;
    445 	}
    446 }
    447 
    448 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
    449 
    450 #ifdef _MODULE
    451 /*
    452  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    453  * XXX it will be defined in the common-code module
    454  */
    455 #undef  CFDRIVER_DECL
    456 #define CFDRIVER_DECL(name, class, attr)
    457 #include "ioconf.c"
    458 #endif
    459 
    460 static int
    461 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
    462 {
    463 #ifdef _MODULE
    464 	/*
    465 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    466 	 * the cfdrivers are attached already.
    467 	 */
    468 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    469 #endif
    470 	int error = 0;
    471 
    472 #ifdef _MODULE
    473 	switch (cmd) {
    474 	case MODULE_CMD_INIT:
    475 		error = config_init_component(no_cfdriver_vec,
    476 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    477         	break;
    478 	case MODULE_CMD_FINI:
    479 		error = config_fini_component(no_cfdriver_vec,
    480 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    481 		break;
    482 	default:
    483 		error = ENOTTY;
    484 		break;
    485 	}
    486 #endif
    487 
    488 	return error;
    489 }
    490