Home | History | Annotate | Line # | Download | only in sdmmc
ld_sdmmc.c revision 1.34
      1 /*	$NetBSD: ld_sdmmc.c,v 1.34 2017/08/20 15:58:43 mlelstv 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.34 2017/08/20 15:58:43 mlelstv 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 
     83 struct ld_sdmmc_softc {
     84 	struct ld_softc sc_ld;
     85 	int sc_hwunit;
     86 
     87 	struct sdmmc_function *sc_sf;
     88 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
     89 	pcq_t *sc_freeq;
     90 
     91 	struct evcnt sc_ev_discard;	/* discard counter */
     92 	struct evcnt sc_ev_discarderr;	/* discard error counter */
     93 	struct evcnt sc_ev_discardbusy;	/* discard busy counter */
     94 };
     95 
     96 static int ld_sdmmc_match(device_t, cfdata_t, void *);
     97 static void ld_sdmmc_attach(device_t, device_t, void *);
     98 static int ld_sdmmc_detach(device_t, int);
     99 
    100 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
    101 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
    102 static void ld_sdmmc_restart(void *);
    103 static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
    104 static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
    105 
    106 static void ld_sdmmc_doattach(void *);
    107 static void ld_sdmmc_dobio(void *);
    108 static void ld_sdmmc_dodiscard(void *);
    109 
    110 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
    111     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
    112 
    113 
    114 /* ARGSUSED */
    115 static int
    116 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
    117 {
    118 	struct sdmmc_softc *sdmsc = device_private(parent);
    119 
    120 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
    121 		return 1;
    122 	return 0;
    123 }
    124 
    125 /* ARGSUSED */
    126 static void
    127 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
    128 {
    129 	struct ld_sdmmc_softc *sc = device_private(self);
    130 	struct sdmmc_attach_args *sa = aux;
    131 	struct ld_softc *ld = &sc->sc_ld;
    132 	struct ld_sdmmc_task *task;
    133 	struct lwp *lwp;
    134 	int i;
    135 
    136 	ld->sc_dv = self;
    137 
    138 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
    139 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
    140 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
    141 	aprint_naive("\n");
    142 
    143 	evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
    144 	    NULL, device_xname(self), "sdmmc discard count");
    145 	evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
    146 	    NULL, device_xname(self), "sdmmc discard errors");
    147 	evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
    148 	    NULL, device_xname(self), "sdmmc discard busy");
    149 
    150 	const int ntask = __arraycount(sc->sc_task);
    151 	sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
    152 	for (i = 0; i < ntask; i++) {
    153 		task = &sc->sc_task[i];
    154 		task->task_sc = sc;
    155 		callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
    156 		pcq_put(sc->sc_freeq, task);
    157 	}
    158 
    159 	sc->sc_hwunit = 0;	/* always 0? */
    160 	sc->sc_sf = sa->sf;
    161 
    162 	ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
    163 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
    164 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
    165 	ld->sc_maxxfer = MAXPHYS;
    166 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
    167 	ld->sc_dump = ld_sdmmc_dump;
    168 	ld->sc_start = ld_sdmmc_start;
    169 	ld->sc_discard = ld_sdmmc_discard;
    170 	ld->sc_ioctl = ld_sdmmc_ioctl;
    171 
    172 	/*
    173 	 * Defer attachment of ld + disk subsystem to a thread.
    174 	 *
    175 	 * This is necessary because wedge autodiscover needs to
    176 	 * open and call into the ld driver, which could deadlock
    177 	 * when the sdmmc driver isn't ready in early bootstrap.
    178 	 *
    179 	 * Don't mark thread as MPSAFE to keep aprint output sane.
    180 	 */
    181 	config_pending_incr(self);
    182 	if (kthread_create(PRI_NONE, 0, NULL,
    183 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
    184 		aprint_error_dev(self, "couldn't create thread\n");
    185 	}
    186 }
    187 
    188 static void
    189 ld_sdmmc_doattach(void *arg)
    190 {
    191 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
    192 	struct ld_softc *ld = &sc->sc_ld;
    193 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
    194 	const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
    195 	char buf[sizeof("9999 KB")];
    196 
    197 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    198 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
    199 	if (ssc->sc_transfer_mode != NULL)
    200 		aprint_normal(" %s,", ssc->sc_transfer_mode);
    201 	if (cache_size > 0) {
    202 		format_bytes(buf, sizeof(buf), cache_size);
    203 		aprint_normal(" %s cache%s,", buf,
    204 		    ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
    205 		    " (disabled)");
    206 	}
    207 	if ((ssc->sc_busclk / 1000) != 0)
    208 		aprint_normal(" %u.%03u MHz\n",
    209 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
    210 	else
    211 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
    212 	config_pending_decr(ld->sc_dv);
    213 	kthread_exit(0);
    214 }
    215 
    216 static int
    217 ld_sdmmc_detach(device_t dev, int flags)
    218 {
    219 	struct ld_sdmmc_softc *sc = device_private(dev);
    220 	struct ld_softc *ld = &sc->sc_ld;
    221 	int rv, i;
    222 
    223 	if ((rv = ldbegindetach(ld, flags)) != 0)
    224 		return rv;
    225 	ldenddetach(ld);
    226 
    227 	for (i = 0; i < __arraycount(sc->sc_task); i++)
    228 		callout_destroy(&sc->sc_task[i].task_restart_ch);
    229 
    230 	pcq_destroy(sc->sc_freeq);
    231 	evcnt_detach(&sc->sc_ev_discard);
    232 	evcnt_detach(&sc->sc_ev_discarderr);
    233 	evcnt_detach(&sc->sc_ev_discardbusy);
    234 
    235 	return 0;
    236 }
    237 
    238 static int
    239 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
    240 {
    241 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    242 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    243 
    244 	if (task == NULL)
    245 		return EAGAIN;
    246 
    247 	task->task_bp = bp;
    248 	task->task_retries = 0;
    249 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
    250 
    251 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    252 
    253 	return 0;
    254 }
    255 
    256 static void
    257 ld_sdmmc_restart(void *arg)
    258 {
    259 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    260 	struct ld_sdmmc_softc *sc = task->task_sc;
    261 	struct buf *bp = task->task_bp;
    262 
    263 	bp->b_resid = bp->b_bcount;
    264 
    265 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    266 }
    267 
    268 static void
    269 ld_sdmmc_dobio(void *arg)
    270 {
    271 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    272 	struct ld_sdmmc_softc *sc = task->task_sc;
    273 	struct buf *bp = task->task_bp;
    274 	int error;
    275 
    276 	/*
    277 	 * I/O operation
    278 	 */
    279 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
    280 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
    281 	    bp->b_rawblkno, bp->b_bcount));
    282 
    283 	/* is everything done in terms of blocks? */
    284 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
    285 		/* trying to read or write past end of device */
    286 		aprint_error_dev(sc->sc_ld.sc_dv,
    287 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
    288 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
    289 		bp->b_error = EINVAL;
    290 		bp->b_resid = bp->b_bcount;
    291 
    292 		goto done;
    293 	}
    294 
    295 	if (bp->b_flags & B_READ)
    296 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
    297 		    bp->b_data, bp->b_bcount);
    298 	else
    299 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
    300 		    bp->b_data, bp->b_bcount);
    301 	if (error) {
    302 		if (task->task_retries < LD_SDMMC_IORETRIES) {
    303 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
    304 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    305 
    306 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    307 				dksc->sc_dkdev.dk_label);
    308 			printf(", retrying\n");
    309 			task->task_retries++;
    310 			callout_reset(&task->task_restart_ch, RECOVERYTIME,
    311 			    ld_sdmmc_restart, task);
    312 			return;
    313 		}
    314 		bp->b_error = error;
    315 		bp->b_resid = bp->b_bcount;
    316 	} else {
    317 		bp->b_resid = 0;
    318 	}
    319 
    320 done:
    321 	pcq_put(sc->sc_freeq, task);
    322 
    323 	lddone(&sc->sc_ld, bp);
    324 }
    325 
    326 static int
    327 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    328 {
    329 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    330 
    331 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
    332 	    blkcnt * ld->sc_secsize);
    333 }
    334 
    335 static void
    336 ld_sdmmc_dodiscard(void *arg)
    337 {
    338 	struct ld_sdmmc_task *task = arg;
    339 	struct ld_sdmmc_softc *sc = task->task_sc;
    340 	struct buf *bp = task->task_bp;
    341 	uint32_t sblkno, nblks;
    342 	int error;
    343 
    344 	/* first and last block to erase */
    345 	sblkno = bp->b_rawblkno;
    346 	nblks  = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
    347 
    348 	/* An error from discard is non-fatal */
    349 	error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
    350 	if (error != 0)
    351 		sc->sc_ev_discarderr.ev_count++;
    352 	else
    353 		sc->sc_ev_discard.ev_count++;
    354 	pcq_put(sc->sc_freeq, task);
    355 
    356 	if (error)
    357 		bp->b_error = error;
    358 
    359 	lddiscardend(&sc->sc_ld, bp);
    360 }
    361 
    362 static int
    363 ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
    364 {
    365 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    366 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    367 
    368 	if (task == NULL) {
    369 		sc->sc_ev_discardbusy.ev_count++;
    370 		return 0;
    371 	}
    372 
    373 	task->task_bp = bp;
    374 
    375 	sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
    376 
    377 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    378 
    379 	return 0;
    380 }
    381 
    382 static int
    383 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
    384     bool poll)
    385 {
    386 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    387 
    388 	switch (cmd) {
    389 	case DIOCCACHESYNC:
    390 		return sdmmc_mem_flush_cache(sc->sc_sf, poll);
    391 	default:
    392 		return EPASSTHROUGH;
    393 	}
    394 }
    395 
    396 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
    397 
    398 #ifdef _MODULE
    399 /*
    400  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    401  * XXX it will be defined in the common-code module
    402  */
    403 #undef  CFDRIVER_DECL
    404 #define CFDRIVER_DECL(name, class, attr)
    405 #include "ioconf.c"
    406 #endif
    407 
    408 static int
    409 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
    410 {
    411 #ifdef _MODULE
    412 	/*
    413 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    414 	 * the cfdrivers are attached already.
    415 	 */
    416 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    417 #endif
    418 	int error = 0;
    419 
    420 #ifdef _MODULE
    421 	switch (cmd) {
    422 	case MODULE_CMD_INIT:
    423 		error = config_init_component(no_cfdriver_vec,
    424 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    425         	break;
    426 	case MODULE_CMD_FINI:
    427 		error = config_fini_component(no_cfdriver_vec,
    428 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    429 		break;
    430 	default:
    431 		error = ENOTTY;
    432 		break;
    433 	}
    434 #endif
    435 
    436 	return error;
    437 }
    438