xref: /netbsd-src/sys/dev/sdmmc/ld_sdmmc.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: ld_sdmmc.c,v 1.43 2024/01/23 23:13:05 riastradh 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.43 2024/01/23 23:13:05 riastradh Exp $");
32 
33 #ifdef _KERNEL_OPT
34 #include "opt_sdmmc.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 
40 #include <sys/buf.h>
41 #include <sys/bufq.h>
42 #include <sys/bus.h>
43 #include <sys/device.h>
44 #include <sys/disk.h>
45 #include <sys/disklabel.h>
46 #include <sys/dkio.h>
47 #include <sys/endian.h>
48 #include <sys/kernel.h>
49 #include <sys/kmem.h>
50 #include <sys/kthread.h>
51 #include <sys/module.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54 
55 #include <dev/ldvar.h>
56 
57 #include <dev/sdmmc/sdmmcvar.h>
58 
59 #include "ioconf.h"
60 
61 #ifdef LD_SDMMC_DEBUG
62 #define DPRINTF(s)	printf s
63 #else
64 #define DPRINTF(s)	__nothing
65 #endif
66 
67 #define	LD_SDMMC_IORETRIES	5	/* number of retries before giving up */
68 #define	RECOVERYTIME		hz/2	/* time to wait before retrying a cmd */
69 
70 #define	LD_SDMMC_MAXQUEUECNT	4	/* number of queued bio requests */
71 #define	LD_SDMMC_MAXTASKCNT	8	/* number of tasks in task pool */
72 
73 struct ld_sdmmc_softc;
74 
75 struct ld_sdmmc_task {
76 	struct sdmmc_task task;
77 	struct ld_sdmmc_softc *task_sc;
78 
79 	struct buf *task_bp;
80 	int task_retries; /* number of xfer retry */
81 	struct callout task_restart_ch;
82 
83 	bool task_poll;
84 	int *task_errorp;
85 
86 	TAILQ_ENTRY(ld_sdmmc_task) task_entry;
87 };
88 
89 struct ld_sdmmc_softc {
90 	struct ld_softc sc_ld;
91 	int sc_hwunit;
92 	char *sc_typename;
93 	struct sdmmc_function *sc_sf;
94 
95 	kmutex_t sc_lock;
96 	kcondvar_t sc_cv;
97 	TAILQ_HEAD(, ld_sdmmc_task) sc_freeq;
98 	TAILQ_HEAD(, ld_sdmmc_task) sc_xferq;
99 	unsigned sc_busy;
100 	bool sc_dying;
101 
102 	struct evcnt sc_ev_discard;	/* discard counter */
103 	struct evcnt sc_ev_discarderr;	/* discard error counter */
104 	struct evcnt sc_ev_discardbusy;	/* discard busy counter */
105 	struct evcnt sc_ev_cachesyncbusy; /* cache sync busy counter */
106 
107 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
108 };
109 
110 static int ld_sdmmc_match(device_t, cfdata_t, void *);
111 static void ld_sdmmc_attach(device_t, device_t, void *);
112 static int ld_sdmmc_detach(device_t, int);
113 
114 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
115 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
116 static void ld_sdmmc_restart(void *);
117 static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
118 static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
119 
120 static void ld_sdmmc_doattach(void *);
121 static void ld_sdmmc_dobio(void *);
122 static void ld_sdmmc_dodiscard(void *);
123 
124 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
125     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
126 
127 static struct ld_sdmmc_task *
128 ld_sdmmc_task_get(struct ld_sdmmc_softc *sc)
129 {
130 	struct ld_sdmmc_task *task;
131 
132 	KASSERT(mutex_owned(&sc->sc_lock));
133 
134 	if (sc->sc_dying || (task = TAILQ_FIRST(&sc->sc_freeq)) == NULL)
135 		return NULL;
136 	TAILQ_REMOVE(&sc->sc_freeq, task, task_entry);
137 	TAILQ_INSERT_TAIL(&sc->sc_xferq, task, task_entry);
138 	KASSERT(task->task_bp == NULL);
139 	KASSERT(task->task_errorp == NULL);
140 
141 	return task;
142 }
143 
144 static void
145 ld_sdmmc_task_put(struct ld_sdmmc_softc *sc, struct ld_sdmmc_task *task)
146 {
147 
148 	KASSERT(mutex_owned(&sc->sc_lock));
149 
150 	TAILQ_REMOVE(&sc->sc_xferq, task, task_entry);
151 	TAILQ_INSERT_TAIL(&sc->sc_freeq, task, task_entry);
152 	task->task_bp = NULL;
153 	task->task_errorp = NULL;
154 }
155 
156 static void
157 ld_sdmmc_task_cancel(struct ld_sdmmc_softc *sc, struct ld_sdmmc_task *task)
158 {
159 	struct buf *bp;
160 	int *errorp;
161 
162 	KASSERT(mutex_owned(&sc->sc_lock));
163 	KASSERT(sc->sc_dying);
164 
165 	/*
166 	 * Either the callout or the task may be pending, but not both.
167 	 * First, determine whether the callout is pending.
168 	 */
169 	if (callout_pending(&task->task_restart_ch) ||
170 	    callout_invoking(&task->task_restart_ch)) {
171 		/*
172 		 * The callout either is pending, or just started but
173 		 * is waiting for us to release the lock.  At this
174 		 * point, it will notice sc->sc_dying and give up, so
175 		 * just wait for it to complete and then we will
176 		 * release everything.
177 		 */
178 		callout_halt(&task->task_restart_ch, &sc->sc_lock);
179 	} else {
180 		/*
181 		 * If the callout is running, it has just scheduled, so
182 		 * after we wait for the callout to finish running, the
183 		 * task is either pending or running.  If the task is
184 		 * already running, it will notice sc->sc_dying and
185 		 * give up; otherwise we have to release everything.
186 		 */
187 		callout_halt(&task->task_restart_ch, &sc->sc_lock);
188 		if (!sdmmc_del_task(sc->sc_sf->sc, &task->task, &sc->sc_lock))
189 			return; /* task already started, let it clean up */
190 	}
191 
192 	/*
193 	 * It is our responsibility to clean up.  Move it from xferq
194 	 * back to freeq and make sure to notify anyone waiting that
195 	 * it's finished.
196 	 */
197 	bp = task->task_bp;
198 	errorp = task->task_errorp;
199 	ld_sdmmc_task_put(sc, task);
200 
201 	/*
202 	 * If the task was for an asynchronous I/O xfer, fail the I/O
203 	 * xfer, with the softc lock dropped since this is a callback
204 	 * into arbitrary other subsystems.
205 	 */
206 	if (bp) {
207 		mutex_exit(&sc->sc_lock);
208 		/*
209 		 * XXX We assume that the same sequence works for bio
210 		 * and discard -- that lddiscardend is just the same as
211 		 * setting bp->b_resid = bp->b_bcount in the event of
212 		 * error and then calling lddone.
213 		 */
214 		bp->b_error = ENXIO;
215 		bp->b_resid = bp->b_bcount;
216 		lddone(&sc->sc_ld, bp);
217 		mutex_enter(&sc->sc_lock);
218 	}
219 
220 	/*
221 	 * If the task was for a synchronous operation (cachesync),
222 	 * then just set the error indicator and wake up the waiter.
223 	 */
224 	if (errorp) {
225 		*errorp = ENXIO;
226 		cv_broadcast(&sc->sc_cv);
227 	}
228 }
229 
230 /* ARGSUSED */
231 static int
232 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
233 {
234 	struct sdmmc_softc *sdmsc = device_private(parent);
235 
236 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
237 		return 1;
238 	return 0;
239 }
240 
241 /* ARGSUSED */
242 static void
243 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
244 {
245 	struct ld_sdmmc_softc *sc = device_private(self);
246 	struct sdmmc_attach_args *sa = aux;
247 	struct ld_softc *ld = &sc->sc_ld;
248 	struct ld_sdmmc_task *task;
249 	struct lwp *lwp;
250 	const char *cardtype;
251 	int i;
252 
253 	ld->sc_dv = self;
254 
255 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
256 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
257 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
258 	aprint_naive("\n");
259 
260 	if (ISSET(sa->sf->sc->sc_flags, SMF_SD_MODE)) {
261 		cardtype = "SD card";
262 	} else {
263 		cardtype = "MMC";
264 	}
265 	sc->sc_typename = kmem_asprintf("%s 0x%02x:0x%04x:%s",
266 	    cardtype, sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm);
267 
268 	evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
269 	    NULL, device_xname(self), "sdmmc discard count");
270 	evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
271 	    NULL, device_xname(self), "sdmmc discard errors");
272 	evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
273 	    NULL, device_xname(self), "sdmmc discard busy");
274 
275 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SDMMC);
276 	cv_init(&sc->sc_cv, "ldsdmmc");
277 	TAILQ_INIT(&sc->sc_freeq);
278 	TAILQ_INIT(&sc->sc_xferq);
279 	sc->sc_dying = false;
280 
281 	const int ntask = __arraycount(sc->sc_task);
282 	for (i = 0; i < ntask; i++) {
283 		task = &sc->sc_task[i];
284 		task->task_sc = sc;
285 		callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
286 		TAILQ_INSERT_TAIL(&sc->sc_freeq, task, task_entry);
287 	}
288 
289 	sc->sc_hwunit = 0;	/* always 0? */
290 	sc->sc_sf = sa->sf;
291 
292 	ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
293 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
294 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
295 	ld->sc_maxxfer = MAXPHYS;
296 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
297 	ld->sc_dump = ld_sdmmc_dump;
298 	ld->sc_start = ld_sdmmc_start;
299 	ld->sc_discard = ld_sdmmc_discard;
300 	ld->sc_ioctl = ld_sdmmc_ioctl;
301 	ld->sc_typename = sc->sc_typename;
302 
303 	/*
304 	 * Defer attachment of ld + disk subsystem to a thread.
305 	 *
306 	 * This is necessary because wedge autodiscover needs to
307 	 * open and call into the ld driver, which could deadlock
308 	 * when the sdmmc driver isn't ready in early bootstrap.
309 	 *
310 	 * Don't mark thread as MPSAFE to keep aprint output sane.
311 	 */
312 	config_pending_incr(self);
313 	if (kthread_create(PRI_NONE, 0, NULL,
314 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
315 		aprint_error_dev(self, "couldn't create thread\n");
316 	}
317 }
318 
319 static void
320 ld_sdmmc_doattach(void *arg)
321 {
322 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
323 	struct ld_softc *ld = &sc->sc_ld;
324 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
325 	const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
326 	char buf[sizeof("9999 KB")];
327 
328 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
329 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
330 	if (ssc->sc_transfer_mode != NULL)
331 		aprint_normal(" %s,", ssc->sc_transfer_mode);
332 	if (cache_size > 0) {
333 		format_bytes(buf, sizeof(buf), cache_size);
334 		aprint_normal(" %s cache%s,", buf,
335 		    ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
336 		    " (disabled)");
337 	}
338 	if ((ssc->sc_busclk / 1000) != 0)
339 		aprint_normal(" %u.%03u MHz\n",
340 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
341 	else
342 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
343 	config_pending_decr(ld->sc_dv);
344 	kthread_exit(0);
345 }
346 
347 static int
348 ld_sdmmc_detach(device_t dev, int flags)
349 {
350 	struct ld_sdmmc_softc *sc = device_private(dev);
351 	struct ld_softc *ld = &sc->sc_ld;
352 	struct ld_sdmmc_task *task;
353 	int error, i;
354 
355 	/*
356 	 * Block new xfers, or fail if the disk is still open and the
357 	 * detach isn't forced.  After this point, we are committed to
358 	 * detaching.
359 	 */
360 	error = ldbegindetach(ld, flags);
361 	if (error)
362 		return error;
363 
364 	/*
365 	 * Abort all pending tasks, and wait for all pending waiters to
366 	 * notice that we're gone.
367 	 */
368 	mutex_enter(&sc->sc_lock);
369 	sc->sc_dying = true;
370 	while ((task = TAILQ_FIRST(&sc->sc_xferq)) != NULL)
371 		ld_sdmmc_task_cancel(sc, task);
372 	while (sc->sc_busy)
373 		cv_wait(&sc->sc_cv, &sc->sc_lock);
374 	mutex_exit(&sc->sc_lock);
375 
376 	/* Done!  Destroy the disk.  */
377 	ldenddetach(ld);
378 
379 	KASSERT(TAILQ_EMPTY(&sc->sc_xferq));
380 
381 	for (i = 0; i < __arraycount(sc->sc_task); i++)
382 		callout_destroy(&sc->sc_task[i].task_restart_ch);
383 
384 	cv_destroy(&sc->sc_cv);
385 	mutex_destroy(&sc->sc_lock);
386 
387 	evcnt_detach(&sc->sc_ev_discard);
388 	evcnt_detach(&sc->sc_ev_discarderr);
389 	evcnt_detach(&sc->sc_ev_discardbusy);
390 	kmem_free(sc->sc_typename, strlen(sc->sc_typename) + 1);
391 
392 	return 0;
393 }
394 
395 static int
396 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
397 {
398 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
399 	struct ld_sdmmc_task *task;
400 	int error;
401 
402 	mutex_enter(&sc->sc_lock);
403 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
404 		error = EAGAIN;
405 		goto out;
406 	}
407 
408 	task->task_bp = bp;
409 	task->task_retries = 0;
410 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
411 
412 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
413 
414 	/* Success!  The xfer is now queued.  */
415 	error = 0;
416 
417 out:	mutex_exit(&sc->sc_lock);
418 	return error;
419 }
420 
421 static void
422 ld_sdmmc_restart(void *arg)
423 {
424 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
425 	struct ld_sdmmc_softc *sc = task->task_sc;
426 	struct buf *bp = task->task_bp;
427 
428 	bp->b_resid = bp->b_bcount;
429 
430 	mutex_enter(&sc->sc_lock);
431 	callout_ack(&task->task_restart_ch);
432 	if (!sc->sc_dying)
433 		sdmmc_add_task(sc->sc_sf->sc, &task->task);
434 	mutex_exit(&sc->sc_lock);
435 }
436 
437 static void
438 ld_sdmmc_dobio(void *arg)
439 {
440 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
441 	struct ld_sdmmc_softc *sc = task->task_sc;
442 	struct buf *bp = task->task_bp;
443 	int error;
444 
445 	/*
446 	 * I/O operation
447 	 */
448 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
449 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
450 	    bp->b_rawblkno, bp->b_bcount));
451 
452 	/* is everything done in terms of blocks? */
453 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
454 		/* trying to read or write past end of device */
455 		aprint_error_dev(sc->sc_ld.sc_dv,
456 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
457 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
458 		bp->b_error = EINVAL;
459 		bp->b_resid = bp->b_bcount;
460 
461 		goto done;
462 	}
463 
464 	if (bp->b_flags & B_READ)
465 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
466 		    bp->b_data, bp->b_bcount);
467 	else
468 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
469 		    bp->b_data, bp->b_bcount);
470 	if (error) {
471 		if (task->task_retries < LD_SDMMC_IORETRIES) {
472 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
473 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
474 
475 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
476 				dksc->sc_dkdev.dk_label);
477 			printf(", retrying\n");
478 			task->task_retries++;
479 			mutex_enter(&sc->sc_lock);
480 			if (sc->sc_dying) {
481 				bp->b_resid = bp->b_bcount;
482 				bp->b_error = error;
483 				goto done_locked;
484 			} else {
485 				callout_reset(&task->task_restart_ch,
486 				    RECOVERYTIME, ld_sdmmc_restart, task);
487 			}
488 			mutex_exit(&sc->sc_lock);
489 			return;
490 		}
491 		bp->b_error = error;
492 		bp->b_resid = bp->b_bcount;
493 	} else {
494 		bp->b_resid = 0;
495 	}
496 
497 done:
498 	/* Dissociate the task from the I/O xfer and release it.  */
499 	mutex_enter(&sc->sc_lock);
500 done_locked:
501 	ld_sdmmc_task_put(sc, task);
502 	mutex_exit(&sc->sc_lock);
503 
504 	lddone(&sc->sc_ld, bp);
505 }
506 
507 static int
508 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
509 {
510 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
511 
512 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
513 	    blkcnt * ld->sc_secsize);
514 }
515 
516 static void
517 ld_sdmmc_dodiscard(void *arg)
518 {
519 	struct ld_sdmmc_task *task = arg;
520 	struct ld_sdmmc_softc *sc = task->task_sc;
521 	struct buf *bp = task->task_bp;
522 	uint32_t sblkno, nblks;
523 	int error;
524 
525 	/* first and last block to erase */
526 	sblkno = bp->b_rawblkno;
527 	nblks  = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
528 
529 	/* An error from discard is non-fatal */
530 	error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
531 
532 	/* Count error or success and release the task.  */
533 	mutex_enter(&sc->sc_lock);
534 	if (error)
535 		sc->sc_ev_discarderr.ev_count++;
536 	else
537 		sc->sc_ev_discard.ev_count++;
538 	ld_sdmmc_task_put(sc, task);
539 	mutex_exit(&sc->sc_lock);
540 
541 	/* Record the error and notify the xfer of completion.  */
542 	if (error)
543 		bp->b_error = error;
544 	lddiscardend(&sc->sc_ld, bp);
545 }
546 
547 static int
548 ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
549 {
550 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
551 	struct ld_sdmmc_task *task;
552 	int error;
553 
554 	mutex_enter(&sc->sc_lock);
555 
556 	/* Acquire a free task, or drop the request altogether.  */
557 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
558 		sc->sc_ev_discardbusy.ev_count++;
559 		error = EBUSY;
560 		goto out;
561 	}
562 
563 	/* Set up the task and schedule it.  */
564 	task->task_bp = bp;
565 	sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
566 
567 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
568 
569 	/* Success!  The request is queued.  */
570 	error = 0;
571 
572 out:	mutex_exit(&sc->sc_lock);
573 	return error;
574 }
575 
576 static void
577 ld_sdmmc_docachesync(void *arg)
578 {
579 	struct ld_sdmmc_task *task = arg;
580 	struct ld_sdmmc_softc *sc = task->task_sc;
581 	int error;
582 
583 	/* Flush the cache.  */
584 	error = sdmmc_mem_flush_cache(sc->sc_sf, task->task_poll);
585 
586 	mutex_enter(&sc->sc_lock);
587 
588 	/* Notify the other thread that we're done; pass on the error.  */
589 	*task->task_errorp = error;
590 	cv_broadcast(&sc->sc_cv);
591 
592 	/* Release the task.  */
593 	ld_sdmmc_task_put(sc, task);
594 
595 	mutex_exit(&sc->sc_lock);
596 }
597 
598 static int
599 ld_sdmmc_cachesync(struct ld_softc *ld, bool poll)
600 {
601 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
602 	struct sdmmc_softc *sdmmc = device_private(device_parent(ld->sc_dv));
603 	struct ld_sdmmc_task *task;
604 	int error = -1;
605 
606 	/*
607 	 * If we come here through the sdmmc discovery task, we can't
608 	 * wait for a new task because the new task can't even begin
609 	 * until the sdmmc discovery task has completed.
610 	 *
611 	 * XXX This is wrong, because there may already be queued I/O
612 	 * tasks ahead of us.  Fixing this properly requires doing
613 	 * discovery in a separate thread.  But this should avoid the
614 	 * deadlock of PR kern/57870 (https://gnats.NetBSD.org/57870)
615 	 * until we do split that up.
616 	 */
617 	if (curlwp == sdmmc->sc_tskq_lwp)
618 		return sdmmc_mem_flush_cache(sc->sc_sf, poll);
619 
620 	mutex_enter(&sc->sc_lock);
621 
622 	/* Acquire a free task, or fail with EBUSY.  */
623 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
624 		sc->sc_ev_cachesyncbusy.ev_count++;
625 		error = EBUSY;
626 		goto out;
627 	}
628 
629 	/* Set up the task and schedule it.  */
630 	task->task_poll = poll;
631 	task->task_errorp = &error;
632 	sdmmc_init_task(&task->task, ld_sdmmc_docachesync, task);
633 
634 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
635 
636 	/*
637 	 * Wait for the task to complete.  If the device is yanked,
638 	 * detach will notify us.  Keep the busy count up until we're
639 	 * done waiting so that the softc doesn't go away until we're
640 	 * done.
641 	 */
642 	sc->sc_busy++;
643 	KASSERT(sc->sc_busy <= LD_SDMMC_MAXTASKCNT);
644 	while (error == -1)
645 		cv_wait(&sc->sc_cv, &sc->sc_lock);
646 	if (--sc->sc_busy == 0)
647 		cv_broadcast(&sc->sc_cv);
648 
649 out:	mutex_exit(&sc->sc_lock);
650 	return error;
651 }
652 
653 static int
654 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
655     bool poll)
656 {
657 
658 	switch (cmd) {
659 	case DIOCCACHESYNC:
660 		return ld_sdmmc_cachesync(ld, poll);
661 	default:
662 		return EPASSTHROUGH;
663 	}
664 }
665 
666 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
667 
668 #ifdef _MODULE
669 /*
670  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
671  * XXX it will be defined in the common-code module
672  */
673 #undef  CFDRIVER_DECL
674 #define CFDRIVER_DECL(name, class, attr)
675 #include "ioconf.c"
676 #endif
677 
678 static int
679 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
680 {
681 #ifdef _MODULE
682 	/*
683 	 * We ignore the cfdriver_vec[] that ioconf provides, since
684 	 * the cfdrivers are attached already.
685 	 */
686 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
687 #endif
688 	int error = 0;
689 
690 #ifdef _MODULE
691 	switch (cmd) {
692 	case MODULE_CMD_INIT:
693 		error = config_init_component(no_cfdriver_vec,
694 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
695 		break;
696 	case MODULE_CMD_FINI:
697 		error = config_fini_component(no_cfdriver_vec,
698 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
699 		break;
700 	default:
701 		error = ENOTTY;
702 		break;
703 	}
704 #endif
705 
706 	return error;
707 }
708