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