xref: /netbsd-src/sys/dev/sdmmc/ld_sdmmc.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ld_sdmmc.c,v 1.41 2020/08/02 01:17:56 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.41 2020/08/02 01:17:56 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 	int i;
251 
252 	ld->sc_dv = self;
253 
254 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
255 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
256 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
257 	aprint_naive("\n");
258 
259 	sc->sc_typename = kmem_asprintf("0x%02x:0x%04x:%s",
260 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm);
261 
262 	evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
263 	    NULL, device_xname(self), "sdmmc discard count");
264 	evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
265 	    NULL, device_xname(self), "sdmmc discard errors");
266 	evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
267 	    NULL, device_xname(self), "sdmmc discard busy");
268 
269 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SDMMC);
270 	cv_init(&sc->sc_cv, "ldsdmmc");
271 	TAILQ_INIT(&sc->sc_freeq);
272 	TAILQ_INIT(&sc->sc_xferq);
273 	sc->sc_dying = false;
274 
275 	const int ntask = __arraycount(sc->sc_task);
276 	for (i = 0; i < ntask; i++) {
277 		task = &sc->sc_task[i];
278 		task->task_sc = sc;
279 		callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
280 		TAILQ_INSERT_TAIL(&sc->sc_freeq, task, task_entry);
281 	}
282 
283 	sc->sc_hwunit = 0;	/* always 0? */
284 	sc->sc_sf = sa->sf;
285 
286 	ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
287 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
288 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
289 	ld->sc_maxxfer = MAXPHYS;
290 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
291 	ld->sc_dump = ld_sdmmc_dump;
292 	ld->sc_start = ld_sdmmc_start;
293 	ld->sc_discard = ld_sdmmc_discard;
294 	ld->sc_ioctl = ld_sdmmc_ioctl;
295 	ld->sc_typename = sc->sc_typename;
296 
297 	/*
298 	 * Defer attachment of ld + disk subsystem to a thread.
299 	 *
300 	 * This is necessary because wedge autodiscover needs to
301 	 * open and call into the ld driver, which could deadlock
302 	 * when the sdmmc driver isn't ready in early bootstrap.
303 	 *
304 	 * Don't mark thread as MPSAFE to keep aprint output sane.
305 	 */
306 	config_pending_incr(self);
307 	if (kthread_create(PRI_NONE, 0, NULL,
308 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
309 		aprint_error_dev(self, "couldn't create thread\n");
310 	}
311 }
312 
313 static void
314 ld_sdmmc_doattach(void *arg)
315 {
316 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
317 	struct ld_softc *ld = &sc->sc_ld;
318 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
319 	const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
320 	char buf[sizeof("9999 KB")];
321 
322 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
323 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
324 	if (ssc->sc_transfer_mode != NULL)
325 		aprint_normal(" %s,", ssc->sc_transfer_mode);
326 	if (cache_size > 0) {
327 		format_bytes(buf, sizeof(buf), cache_size);
328 		aprint_normal(" %s cache%s,", buf,
329 		    ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
330 		    " (disabled)");
331 	}
332 	if ((ssc->sc_busclk / 1000) != 0)
333 		aprint_normal(" %u.%03u MHz\n",
334 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
335 	else
336 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
337 	config_pending_decr(ld->sc_dv);
338 	kthread_exit(0);
339 }
340 
341 static int
342 ld_sdmmc_detach(device_t dev, int flags)
343 {
344 	struct ld_sdmmc_softc *sc = device_private(dev);
345 	struct ld_softc *ld = &sc->sc_ld;
346 	struct ld_sdmmc_task *task;
347 	int error, i;
348 
349 	/*
350 	 * Block new xfers, or fail if the disk is still open and the
351 	 * detach isn't forced.  After this point, we are committed to
352 	 * detaching.
353 	 */
354 	error = ldbegindetach(ld, flags);
355 	if (error)
356 		return error;
357 
358 	/*
359 	 * Abort all pending tasks, and wait for all pending waiters to
360 	 * notice that we're gone.
361 	 */
362 	mutex_enter(&sc->sc_lock);
363 	sc->sc_dying = true;
364 	while ((task = TAILQ_FIRST(&sc->sc_xferq)) != NULL)
365 		ld_sdmmc_task_cancel(sc, task);
366 	while (sc->sc_busy)
367 		cv_wait(&sc->sc_cv, &sc->sc_lock);
368 	mutex_exit(&sc->sc_lock);
369 
370 	/* Done!  Destroy the disk.  */
371 	ldenddetach(ld);
372 
373 	KASSERT(TAILQ_EMPTY(&sc->sc_xferq));
374 
375 	for (i = 0; i < __arraycount(sc->sc_task); i++)
376 		callout_destroy(&sc->sc_task[i].task_restart_ch);
377 
378 	cv_destroy(&sc->sc_cv);
379 	mutex_destroy(&sc->sc_lock);
380 
381 	evcnt_detach(&sc->sc_ev_discard);
382 	evcnt_detach(&sc->sc_ev_discarderr);
383 	evcnt_detach(&sc->sc_ev_discardbusy);
384 	kmem_free(sc->sc_typename, strlen(sc->sc_typename) + 1);
385 
386 	return 0;
387 }
388 
389 static int
390 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
391 {
392 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
393 	struct ld_sdmmc_task *task;
394 	int error;
395 
396 	mutex_enter(&sc->sc_lock);
397 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
398 		error = EAGAIN;
399 		goto out;
400 	}
401 
402 	task->task_bp = bp;
403 	task->task_retries = 0;
404 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
405 
406 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
407 
408 	/* Success!  The xfer is now queued.  */
409 	error = 0;
410 
411 out:	mutex_exit(&sc->sc_lock);
412 	return error;
413 }
414 
415 static void
416 ld_sdmmc_restart(void *arg)
417 {
418 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
419 	struct ld_sdmmc_softc *sc = task->task_sc;
420 	struct buf *bp = task->task_bp;
421 
422 	bp->b_resid = bp->b_bcount;
423 
424 	mutex_enter(&sc->sc_lock);
425 	callout_ack(&task->task_restart_ch);
426 	if (!sc->sc_dying)
427 		sdmmc_add_task(sc->sc_sf->sc, &task->task);
428 	mutex_exit(&sc->sc_lock);
429 }
430 
431 static void
432 ld_sdmmc_dobio(void *arg)
433 {
434 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
435 	struct ld_sdmmc_softc *sc = task->task_sc;
436 	struct buf *bp = task->task_bp;
437 	int error;
438 
439 	/*
440 	 * I/O operation
441 	 */
442 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
443 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
444 	    bp->b_rawblkno, bp->b_bcount));
445 
446 	/* is everything done in terms of blocks? */
447 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
448 		/* trying to read or write past end of device */
449 		aprint_error_dev(sc->sc_ld.sc_dv,
450 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
451 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
452 		bp->b_error = EINVAL;
453 		bp->b_resid = bp->b_bcount;
454 
455 		goto done;
456 	}
457 
458 	if (bp->b_flags & B_READ)
459 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
460 		    bp->b_data, bp->b_bcount);
461 	else
462 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
463 		    bp->b_data, bp->b_bcount);
464 	if (error) {
465 		if (task->task_retries < LD_SDMMC_IORETRIES) {
466 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
467 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
468 
469 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
470 				dksc->sc_dkdev.dk_label);
471 			printf(", retrying\n");
472 			task->task_retries++;
473 			mutex_enter(&sc->sc_lock);
474 			if (sc->sc_dying) {
475 				bp->b_resid = bp->b_bcount;
476 				bp->b_error = error;
477 				goto done_locked;
478 			} else {
479 				callout_reset(&task->task_restart_ch,
480 				    RECOVERYTIME, ld_sdmmc_restart, task);
481 			}
482 			mutex_exit(&sc->sc_lock);
483 			return;
484 		}
485 		bp->b_error = error;
486 		bp->b_resid = bp->b_bcount;
487 	} else {
488 		bp->b_resid = 0;
489 	}
490 
491 done:
492 	/* Dissociate the task from the I/O xfer and release it.  */
493 	mutex_enter(&sc->sc_lock);
494 done_locked:
495 	ld_sdmmc_task_put(sc, task);
496 	mutex_exit(&sc->sc_lock);
497 
498 	lddone(&sc->sc_ld, bp);
499 }
500 
501 static int
502 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
503 {
504 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
505 
506 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
507 	    blkcnt * ld->sc_secsize);
508 }
509 
510 static void
511 ld_sdmmc_dodiscard(void *arg)
512 {
513 	struct ld_sdmmc_task *task = arg;
514 	struct ld_sdmmc_softc *sc = task->task_sc;
515 	struct buf *bp = task->task_bp;
516 	uint32_t sblkno, nblks;
517 	int error;
518 
519 	/* first and last block to erase */
520 	sblkno = bp->b_rawblkno;
521 	nblks  = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
522 
523 	/* An error from discard is non-fatal */
524 	error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
525 
526 	/* Count error or success and release the task.  */
527 	mutex_enter(&sc->sc_lock);
528 	if (error)
529 		sc->sc_ev_discarderr.ev_count++;
530 	else
531 		sc->sc_ev_discard.ev_count++;
532 	ld_sdmmc_task_put(sc, task);
533 	mutex_exit(&sc->sc_lock);
534 
535 	/* Record the error and notify the xfer of completion.  */
536 	if (error)
537 		bp->b_error = error;
538 	lddiscardend(&sc->sc_ld, bp);
539 }
540 
541 static int
542 ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
543 {
544 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
545 	struct ld_sdmmc_task *task;
546 	int error;
547 
548 	mutex_enter(&sc->sc_lock);
549 
550 	/* Acquire a free task, or drop the request altogether.  */
551 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
552 		sc->sc_ev_discardbusy.ev_count++;
553 		error = EBUSY;
554 		goto out;
555 	}
556 
557 	/* Set up the task and schedule it.  */
558 	task->task_bp = bp;
559 	sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
560 
561 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
562 
563 	/* Success!  The request is queued.  */
564 	error = 0;
565 
566 out:	mutex_exit(&sc->sc_lock);
567 	return error;
568 }
569 
570 static void
571 ld_sdmmc_docachesync(void *arg)
572 {
573 	struct ld_sdmmc_task *task = arg;
574 	struct ld_sdmmc_softc *sc = task->task_sc;
575 	int error;
576 
577 	/* Flush the cache.  */
578 	error = sdmmc_mem_flush_cache(sc->sc_sf, task->task_poll);
579 
580 	mutex_enter(&sc->sc_lock);
581 
582 	/* Notify the other thread that we're done; pass on the error.  */
583 	*task->task_errorp = error;
584 	cv_broadcast(&sc->sc_cv);
585 
586 	/* Release the task.  */
587 	ld_sdmmc_task_put(sc, task);
588 
589 	mutex_exit(&sc->sc_lock);
590 }
591 
592 static int
593 ld_sdmmc_cachesync(struct ld_softc *ld, bool poll)
594 {
595 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
596 	struct ld_sdmmc_task *task;
597 	int error = -1;
598 
599 	mutex_enter(&sc->sc_lock);
600 
601 	/* Acquire a free task, or fail with EBUSY.  */
602 	if ((task = ld_sdmmc_task_get(sc)) == NULL) {
603 		sc->sc_ev_cachesyncbusy.ev_count++;
604 		error = EBUSY;
605 		goto out;
606 	}
607 
608 	/* Set up the task and schedule it.  */
609 	task->task_poll = poll;
610 	task->task_errorp = &error;
611 	sdmmc_init_task(&task->task, ld_sdmmc_docachesync, task);
612 
613 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
614 
615 	/*
616 	 * Wait for the task to complete.  If the device is yanked,
617 	 * detach will notify us.  Keep the busy count up until we're
618 	 * done waiting so that the softc doesn't go away until we're
619 	 * done.
620 	 */
621 	sc->sc_busy++;
622 	KASSERT(sc->sc_busy <= LD_SDMMC_MAXTASKCNT);
623 	while (error == -1)
624 		cv_wait(&sc->sc_cv, &sc->sc_lock);
625 	if (--sc->sc_busy == 0)
626 		cv_broadcast(&sc->sc_cv);
627 
628 out:	mutex_exit(&sc->sc_lock);
629 	return error;
630 }
631 
632 static int
633 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
634     bool poll)
635 {
636 
637 	switch (cmd) {
638 	case DIOCCACHESYNC:
639 		return ld_sdmmc_cachesync(ld, poll);
640 	default:
641 		return EPASSTHROUGH;
642 	}
643 }
644 
645 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
646 
647 #ifdef _MODULE
648 /*
649  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
650  * XXX it will be defined in the common-code module
651  */
652 #undef  CFDRIVER_DECL
653 #define CFDRIVER_DECL(name, class, attr)
654 #include "ioconf.c"
655 #endif
656 
657 static int
658 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
659 {
660 #ifdef _MODULE
661 	/*
662 	 * We ignore the cfdriver_vec[] that ioconf provides, since
663 	 * the cfdrivers are attached already.
664 	 */
665 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
666 #endif
667 	int error = 0;
668 
669 #ifdef _MODULE
670 	switch (cmd) {
671 	case MODULE_CMD_INIT:
672 		error = config_init_component(no_cfdriver_vec,
673 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
674 		break;
675 	case MODULE_CMD_FINI:
676 		error = config_fini_component(no_cfdriver_vec,
677 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
678 		break;
679 	default:
680 		error = ENOTTY;
681 		break;
682 	}
683 #endif
684 
685 	return error;
686 }
687