Home | History | Annotate | Line # | Download | only in sdmmc
ld_sdmmc.c revision 1.30
      1 /*	$NetBSD: ld_sdmmc.c,v 1.30 2017/07/10 10:35:07 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.30 2017/07/10 10:35:07 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 struct ld_sdmmc_softc;
     69 
     70 struct ld_sdmmc_task {
     71 	struct sdmmc_task task;
     72 
     73 	struct ld_sdmmc_softc *task_sc;
     74 	struct buf *task_bp;
     75 	int task_retries; /* number of xfer retry */
     76 	struct callout task_restart_ch;
     77 };
     78 
     79 struct ld_sdmmc_softc {
     80 	struct ld_softc sc_ld;
     81 	int sc_hwunit;
     82 
     83 	struct sdmmc_function *sc_sf;
     84 #define LD_SDMMC_MAXQUEUECNT 4
     85 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
     86 	pcq_t *sc_freeq;
     87 };
     88 
     89 static int ld_sdmmc_match(device_t, cfdata_t, void *);
     90 static void ld_sdmmc_attach(device_t, device_t, void *);
     91 static int ld_sdmmc_detach(device_t, int);
     92 
     93 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
     94 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
     95 static void ld_sdmmc_restart(void *);
     96 static int ld_sdmmc_discard(struct ld_softc *, off_t, off_t);
     97 
     98 static void ld_sdmmc_doattach(void *);
     99 static void ld_sdmmc_dobio(void *);
    100 
    101 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
    102     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
    103 
    104 
    105 /* ARGSUSED */
    106 static int
    107 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
    108 {
    109 	struct sdmmc_softc *sdmsc = device_private(parent);
    110 
    111 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
    112 		return 1;
    113 	return 0;
    114 }
    115 
    116 /* ARGSUSED */
    117 static void
    118 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
    119 {
    120 	struct ld_sdmmc_softc *sc = device_private(self);
    121 	struct sdmmc_attach_args *sa = aux;
    122 	struct ld_softc *ld = &sc->sc_ld;
    123 	struct ld_sdmmc_task *task;
    124 	struct lwp *lwp;
    125 	int i;
    126 
    127 	ld->sc_dv = self;
    128 
    129 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
    130 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
    131 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
    132 	aprint_naive("\n");
    133 
    134 	const int ntask = __arraycount(sc->sc_task);
    135 	sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
    136 	for (i = 0; i < ntask; i++) {
    137 		task = &sc->sc_task[i];
    138 		task->task_sc = sc;
    139 		callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
    140 		pcq_put(sc->sc_freeq, task);
    141 	}
    142 
    143 	sc->sc_hwunit = 0;	/* always 0? */
    144 	sc->sc_sf = sa->sf;
    145 
    146 	ld->sc_flags = LDF_ENABLED;
    147 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
    148 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
    149 	ld->sc_maxxfer = MAXPHYS;
    150 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
    151 	ld->sc_dump = ld_sdmmc_dump;
    152 	ld->sc_start = ld_sdmmc_start;
    153 	ld->sc_discard = ld_sdmmc_discard;
    154 
    155 	/*
    156 	 * Defer attachment of ld + disk subsystem to a thread.
    157 	 *
    158 	 * This is necessary because wedge autodiscover needs to
    159 	 * open and call into the ld driver, which could deadlock
    160 	 * when the sdmmc driver isn't ready in early bootstrap.
    161 	 *
    162 	 * Don't mark thread as MPSAFE to keep aprint output sane.
    163 	 */
    164 	config_pending_incr(self);
    165 	if (kthread_create(PRI_NONE, 0, NULL,
    166 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
    167 		aprint_error_dev(self, "couldn't create thread\n");
    168 	}
    169 }
    170 
    171 static void
    172 ld_sdmmc_doattach(void *arg)
    173 {
    174 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
    175 	struct ld_softc *ld = &sc->sc_ld;
    176 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
    177 
    178 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    179 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
    180 	if (ssc->sc_transfer_mode != NULL)
    181 		aprint_normal(" %s,", ssc->sc_transfer_mode);
    182 	if ((ssc->sc_busclk / 1000) != 0)
    183 		aprint_normal(" %u.%03u MHz\n",
    184 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
    185 	else
    186 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
    187 	config_pending_decr(ld->sc_dv);
    188 	kthread_exit(0);
    189 }
    190 
    191 static int
    192 ld_sdmmc_detach(device_t dev, int flags)
    193 {
    194 	struct ld_sdmmc_softc *sc = device_private(dev);
    195 	struct ld_softc *ld = &sc->sc_ld;
    196 	int rv, i;
    197 
    198 	if ((rv = ldbegindetach(ld, flags)) != 0)
    199 		return rv;
    200 	ldenddetach(ld);
    201 
    202 	for (i = 0; i < __arraycount(sc->sc_task); i++)
    203 		callout_destroy(&sc->sc_task[i].task_restart_ch);
    204 
    205 	pcq_destroy(sc->sc_freeq);
    206 
    207 	return 0;
    208 }
    209 
    210 static int
    211 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
    212 {
    213 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    214 	struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
    215 
    216 	if (task == NULL)
    217 		return EAGAIN;
    218 
    219 	task->task_bp = bp;
    220 	task->task_retries = 0;
    221 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
    222 
    223 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    224 
    225 	return 0;
    226 }
    227 
    228 static void
    229 ld_sdmmc_restart(void *arg)
    230 {
    231 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    232 	struct ld_sdmmc_softc *sc = task->task_sc;
    233 	struct buf *bp = task->task_bp;
    234 
    235 	bp->b_resid = bp->b_bcount;
    236 
    237 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    238 }
    239 
    240 static void
    241 ld_sdmmc_dobio(void *arg)
    242 {
    243 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    244 	struct ld_sdmmc_softc *sc = task->task_sc;
    245 	struct buf *bp = task->task_bp;
    246 	int error;
    247 
    248 	/*
    249 	 * I/O operation
    250 	 */
    251 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
    252 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
    253 	    bp->b_rawblkno, bp->b_bcount));
    254 
    255 	/* is everything done in terms of blocks? */
    256 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
    257 		/* trying to read or write past end of device */
    258 		aprint_error_dev(sc->sc_ld.sc_dv,
    259 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
    260 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
    261 		bp->b_error = EINVAL;
    262 		bp->b_resid = bp->b_bcount;
    263 
    264 		goto done;
    265 	}
    266 
    267 	if (bp->b_flags & B_READ)
    268 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
    269 		    bp->b_data, bp->b_bcount);
    270 	else
    271 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
    272 		    bp->b_data, bp->b_bcount);
    273 	if (error) {
    274 		if (task->task_retries < LD_SDMMC_IORETRIES) {
    275 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
    276 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    277 
    278 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    279 				dksc->sc_dkdev.dk_label);
    280 			printf(", retrying\n");
    281 			task->task_retries++;
    282 			callout_reset(&task->task_restart_ch, RECOVERYTIME,
    283 			    ld_sdmmc_restart, task);
    284 			return;
    285 		}
    286 		bp->b_error = error;
    287 		bp->b_resid = bp->b_bcount;
    288 	} else {
    289 		bp->b_resid = 0;
    290 	}
    291 
    292 done:
    293 	pcq_put(sc->sc_freeq, task);
    294 
    295 	lddone(&sc->sc_ld, bp);
    296 }
    297 
    298 static int
    299 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    300 {
    301 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    302 
    303 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
    304 	    blkcnt * ld->sc_secsize);
    305 }
    306 
    307 static int
    308 ld_sdmmc_discard(struct ld_softc *ld, off_t pos, off_t len)
    309 {
    310 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    311 
    312 	return sdmmc_mem_discard(sc->sc_sf, pos, len);
    313 }
    314 
    315 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
    316 
    317 #ifdef _MODULE
    318 /*
    319  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    320  * XXX it will be defined in the common-code module
    321  */
    322 #undef  CFDRIVER_DECL
    323 #define CFDRIVER_DECL(name, class, attr)
    324 #include "ioconf.c"
    325 #endif
    326 
    327 static int
    328 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
    329 {
    330 #ifdef _MODULE
    331 	/*
    332 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    333 	 * the cfdrivers are attached already.
    334 	 */
    335 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    336 #endif
    337 	int error = 0;
    338 
    339 #ifdef _MODULE
    340 	switch (cmd) {
    341 	case MODULE_CMD_INIT:
    342 		error = config_init_component(no_cfdriver_vec,
    343 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    344         	break;
    345 	case MODULE_CMD_FINI:
    346 		error = config_fini_component(no_cfdriver_vec,
    347 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    348 		break;
    349 	default:
    350 		error = ENOTTY;
    351 		break;
    352 	}
    353 #endif
    354 
    355 	return error;
    356 }
    357