xref: /illumos-gate/usr/src/uts/common/io/sdcard/impl/sda_slot.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright 2019 Western Digital Corporation.
25  */
26 
27 /*
28  * SD card slot support.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/cmn_err.h>
33 #include <sys/varargs.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/sdcard/sda_impl.h>
37 
38 
39 /*
40  * Prototypes.
41  */
42 
43 static void sda_slot_insert(void *);
44 static sda_err_t sda_slot_check_response(sda_cmd_t *);
45 static void sda_slot_handle_detect(sda_slot_t *);
46 static void sda_slot_handle_transfer(sda_slot_t *, sda_err_t);
47 static void sda_slot_handle_fault(sda_slot_t *, sda_fault_t);
48 static void sda_slot_abort(sda_slot_t *, sda_err_t);
49 static void sda_slot_halt(sda_slot_t *);
50 static void sda_slot_thread(void *);
51 static void sda_slot_vprintf(sda_slot_t *, int, const char *, va_list);
52 
53 /*
54  * Static Variables.
55  */
56 
57 static struct {
58 	sda_fault_t	fault;
59 	const char	*msg;
60 } sda_slot_faults[] = {
61 	{ SDA_FAULT_TIMEOUT,	"Data transfer timed out" },
62 	{ SDA_FAULT_ACMD12,	"Auto CMD12 failure" },
63 	{ SDA_FAULT_CRC7,	"CRC7 failure on CMD/DAT line" },
64 	{ SDA_FAULT_PROTO,	"SD/MMC protocol signaling error" },
65 	{ SDA_FAULT_INIT,	"Card initialization failure" },
66 	{ SDA_FAULT_HOST,	"Internal host or slot failure" },
67 	{ SDA_FAULT_CURRENT,	"Current overlimit detected" },
68 	{ SDA_FAULT_RESET,	"Failed to reset slot" },
69 	{ SDA_FAULT_NONE,	NULL },	/* sentinel, must be last! */
70 };
71 
72 /*
73  * Internal implementation.
74  */
75 
76 /*
77  * These allow for recursive entry.  This is necessary to facilitate
78  * simpler locking with things like the fault handler, where a caller
79  * might already be "holding" the slot.
80  *
81  * This is modeled in part after ndi_devi_enter and ndi_devi_exit.
82  */
83 void
84 sda_slot_enter(sda_slot_t *slot)
85 {
86 	kt_did_t	self = ddi_get_kt_did();
87 	mutex_enter(&slot->s_lock);
88 	if (slot->s_owner == self) {
89 		slot->s_circular++;
90 	} else {
91 		while ((slot->s_owner != 0) && (slot->s_owner != self)) {
92 			cv_wait(&slot->s_cv, &slot->s_lock);
93 		}
94 		slot->s_owner = self;
95 		slot->s_circular++;
96 	}
97 	mutex_exit(&slot->s_lock);
98 }
99 
100 void
101 sda_slot_exit(sda_slot_t *slot)
102 {
103 	ASSERT(sda_slot_owned(slot));
104 
105 	mutex_enter(&slot->s_lock);
106 	slot->s_circular--;
107 	if (slot->s_circular == 0) {
108 		slot->s_owner = 0;
109 		cv_broadcast(&slot->s_cv);
110 	}
111 	mutex_exit(&slot->s_lock);
112 }
113 
114 boolean_t
115 sda_slot_owned(sda_slot_t *slot)
116 {
117 	return (slot->s_owner == ddi_get_kt_did());
118 }
119 
120 sda_err_t
121 sda_slot_check_response(sda_cmd_t *cmdp)
122 {
123 	uint32_t	errs;
124 	switch (cmdp->sc_rtype & 0xf) {
125 	case R1:
126 		if ((errs = (cmdp->sc_response[0] & R1_ERRS)) != 0) {
127 			if (errs & (R1_WP_VIOLATION | R1_CSD_OVERWRITE)) {
128 				return (SDA_EWPROTECT);
129 			}
130 			if (errs & (R1_ADDRESS_ERROR | R1_BLOCK_LEN_ERROR |
131 			    R1_OUT_OF_RANGE | R1_ERASE_PARAM)) {
132 				return (SDA_EINVAL);
133 			}
134 			return (SDA_EIO);
135 		}
136 		break;
137 	case R5:
138 		if ((errs = (cmdp->sc_response[0] & R5_ERRS)) != 0) {
139 			return (SDA_EIO);
140 		}
141 		break;
142 	}
143 	return (SDA_EOK);
144 }
145 
146 void
147 sda_slot_halt(sda_slot_t *slot)
148 {
149 	sda_slot_enter(slot);
150 	slot->s_ops.so_halt(slot->s_prv);
151 	/* We need to wait 1 msec for power down. */
152 	drv_usecwait(1000);
153 	sda_slot_exit(slot);
154 }
155 
156 void
157 sda_slot_reset(sda_slot_t *slot)
158 {
159 	sda_slot_enter(slot);
160 	if (slot->s_ops.so_reset(slot->s_prv) != 0) {
161 		sda_slot_fault(slot, SDA_FAULT_RESET);
162 	}
163 	sda_slot_exit(slot);
164 }
165 
166 int
167 sda_slot_power_on(sda_slot_t *slot)
168 {
169 	int		rv;
170 	uint32_t	ocr;
171 
172 	sda_slot_enter(slot);
173 
174 	/*
175 	 * Get the voltage supplied by the host.  Note that we expect
176 	 * hosts will include a range of 2.7-3.7 in their supported
177 	 * voltage ranges.  The spec does not allow for hosts that
178 	 * cannot supply a voltage in this range, yet.
179 	 */
180 	if ((rv = sda_getprop(slot, SDA_PROP_OCR, &ocr)) != 0) {
181 		sda_slot_err(slot, "Failed to get host OCR (%d)", rv);
182 		goto done;
183 	}
184 	if ((ocr & OCR_HI_MASK) == 0) {
185 		sda_slot_err(slot, "Host does not support standard voltages.");
186 		rv = ENOTSUP;
187 		goto done;
188 	}
189 
190 	/*
191 	 * We prefer 3.3V, 3.0V, and failing that, just use the
192 	 * maximum that the host supports.  3.3V is preferable,
193 	 * because it is the typical common voltage that just about
194 	 * everything supports.  Otherwise we just pick the highest
195 	 * supported voltage.  This facilitates initial power up.
196 	 */
197 	if (ocr & OCR_32_33V) {
198 		slot->s_cur_ocr = OCR_32_33V;
199 	} else if (ocr & OCR_29_30V) {
200 		slot->s_cur_ocr = OCR_29_30V;
201 	} else {
202 		slot->s_cur_ocr = (1U << (ddi_fls(ocr) - 1));
203 	}
204 
205 	/*
206 	 * Turn on the power.
207 	 */
208 	if ((rv = sda_setprop(slot, SDA_PROP_OCR, slot->s_cur_ocr)) != 0) {
209 		sda_slot_err(slot, "Failed to set OCR %x (%d)",
210 		    slot->s_cur_ocr, rv);
211 		goto done;
212 	}
213 
214 	sda_slot_exit(slot);
215 
216 	/*
217 	 * Wait 250 msec (per spec) for power ramp to complete.
218 	 */
219 	delay(drv_usectohz(250000));
220 	return (0);
221 
222 done:
223 	sda_slot_exit(slot);
224 	return (rv);
225 }
226 
227 void
228 sda_slot_power_off(sda_slot_t *slot)
229 {
230 	sda_slot_enter(slot);
231 	(void) sda_setprop(slot, SDA_PROP_OCR, 0);
232 	/* XXX: FMA: on failure this should cause a fault to be generated */
233 	/* spec requires voltage to stay low for at least 1 msec */
234 	drv_usecwait(1000);
235 	sda_slot_exit(slot);
236 }
237 
238 void
239 sda_slot_insert(void *arg)
240 {
241 	sda_slot_t	*slot = arg;
242 
243 	if (sda_init_card(slot) != SDA_EOK) {
244 		/*
245 		 * Remove power from the slot.  If a more severe fault
246 		 * occurred, then a manual reset with cfgadm will be needed.
247 		 */
248 		sda_slot_err(slot, "Unable to initialize card!");
249 		sda_slot_enter(slot);
250 		sda_slot_power_off(slot);
251 		sda_slot_abort(slot, SDA_ENODEV);
252 		sda_slot_exit(slot);
253 
254 	} else if ((slot->s_flags & SLOTF_MEMORY) == 0) {
255 		/*
256 		 * SDIO: For SDIO, we can write the card's
257 		 * MANFID tuple in CIS to the UUID.  Until we
258 		 * support SDIO, we just suppress creating
259 		 * devinfo nodes.
260 		 */
261 		sda_slot_err(slot, "Non-memory target not supported");
262 	} else {
263 
264 		sda_slot_enter(slot);
265 		if (sda_mem_parse_cid_csd(slot) != DDI_SUCCESS) {
266 			sda_slot_err(slot,
267 			    "Unable to parse card identification");
268 		} else {
269 			slot->s_warn = B_FALSE;
270 			slot->s_ready = B_TRUE;
271 		}
272 		sda_slot_exit(slot);
273 	}
274 
275 	slot->s_stamp = ddi_get_time();
276 	slot->s_intransit = 0;
277 	bd_state_change(slot->s_bdh);
278 }
279 
280 void
281 sda_slot_abort(sda_slot_t *slot, sda_err_t errno)
282 {
283 	sda_cmd_t	*cmdp;
284 
285 	ASSERT(sda_slot_owned(slot));
286 
287 	if ((cmdp = slot->s_xfrp) != NULL) {
288 		slot->s_xfrp = NULL;
289 		sda_cmd_notify(cmdp, 0, errno);
290 		list_insert_tail(&slot->s_abortlist, cmdp);
291 	}
292 	while ((cmdp = list_head(&slot->s_cmdlist)) != NULL) {
293 		list_remove(&slot->s_cmdlist, cmdp);
294 		sda_cmd_notify(cmdp, 0, errno);
295 		list_insert_tail(&slot->s_abortlist, cmdp);
296 	}
297 
298 	sda_slot_wakeup(slot);
299 }
300 
301 void
302 sda_slot_handle_transfer(sda_slot_t *slot, sda_err_t errno)
303 {
304 	sda_cmd_t	*cmdp;
305 
306 	sda_slot_enter(slot);
307 
308 	if ((cmdp = slot->s_xfrp) != NULL) {
309 
310 		slot->s_xfrp = NULL;
311 		slot->s_xfrtmo = 0;
312 		(void) sda_setprop(slot, SDA_PROP_LED, 0);
313 		sda_slot_exit(slot);
314 
315 		sda_slot_wakeup(slot);
316 
317 		sda_cmd_notify(cmdp, SDA_CMDF_DAT, errno);
318 	} else {
319 		sda_slot_exit(slot);
320 	}
321 }
322 
323 void
324 sda_slot_handle_fault(sda_slot_t *slot, sda_fault_t fault)
325 {
326 	const char	*msg;
327 	int		i;
328 
329 	sda_slot_enter(slot);
330 
331 	if ((fault == SDA_FAULT_TIMEOUT) && (slot->s_init)) {
332 		/*
333 		 * Timeouts during initialization are quite normal.
334 		 */
335 		sda_slot_exit(slot);
336 		return;
337 	}
338 
339 	slot->s_failed = B_TRUE;
340 	sda_slot_abort(slot, SDA_EFAULT);
341 
342 	msg = "Unknown fault (%d)";
343 	for (i = 0; sda_slot_faults[i].msg != NULL; i++) {
344 		if (sda_slot_faults[i].fault == fault) {
345 			msg = sda_slot_faults[i].msg;
346 			break;
347 		}
348 	}
349 
350 	/*
351 	 * FMA would be a better choice here.
352 	 */
353 	sda_slot_err(slot, msg, fault);
354 
355 	/*
356 	 * Shut down the slot.  Interaction from userland via cfgadm
357 	 * can revive it.
358 	 *
359 	 * FMA can help here.
360 	 */
361 	sda_slot_halt(slot);
362 
363 	sda_slot_exit(slot);
364 }
365 
366 void
367 sda_slot_handle_detect(sda_slot_t *slot)
368 {
369 	uint32_t	inserted;
370 
371 	sda_slot_enter(slot);
372 
373 	slot->s_stamp = ddi_get_time();
374 	slot->s_intransit = 1;
375 	slot->s_flags = 0;
376 	slot->s_rca = 0;
377 	slot->s_ready = B_FALSE;
378 
379 	sda_getprop(slot, SDA_PROP_INSERTED, &inserted);
380 	slot->s_inserted = (inserted != 0);
381 
382 	if (slot->s_inserted && !slot->s_failed) {
383 		/*
384 		 * We need to initialize the card, so we only support
385 		 * hipri commands for now.
386 		 */
387 		slot->s_init = B_TRUE;
388 		sda_slot_exit(slot);
389 
390 		/*
391 		 * Card insertion occurred.  We have to run this on
392 		 * another task, to avoid deadlock as the task may
393 		 * need to dispatch commands.
394 		 */
395 
396 		(void) ddi_taskq_dispatch(slot->s_hp_tq, sda_slot_insert, slot,
397 		    DDI_SLEEP);
398 	} else {
399 
400 		/*
401 		 * Nuke in-flight commands.
402 		 */
403 		sda_slot_abort(slot, SDA_ENODEV);
404 
405 		/*
406 		 * Restart the slot (incl. power cycle).  This gets the
407 		 * slot to a known good state.
408 		 */
409 		sda_slot_reset(slot);
410 
411 		slot->s_intransit = 0;
412 		sda_slot_exit(slot);
413 
414 		bd_state_change(slot->s_bdh);
415 	}
416 
417 	sda_slot_wakeup(slot);
418 }
419 
420 void
421 sda_slot_transfer(sda_slot_t *slot, sda_err_t errno)
422 {
423 	mutex_enter(&slot->s_evlock);
424 	slot->s_errno = errno;
425 	slot->s_xfrdone = B_TRUE;
426 	cv_broadcast(&slot->s_evcv);
427 	mutex_exit(&slot->s_evlock);
428 }
429 
430 void
431 sda_slot_detect(sda_slot_t *slot)
432 {
433 	mutex_enter(&slot->s_evlock);
434 	slot->s_detect = B_TRUE;
435 	cv_broadcast(&slot->s_evcv);
436 	mutex_exit(&slot->s_evlock);
437 }
438 
439 void
440 sda_slot_fault(sda_slot_t *slot, sda_fault_t fault)
441 {
442 	mutex_enter(&slot->s_evlock);
443 	slot->s_fault = fault;
444 	cv_broadcast(&slot->s_evcv);
445 	mutex_exit(&slot->s_evlock);
446 }
447 
448 void
449 sda_slot_wakeup(sda_slot_t *slot)
450 {
451 	mutex_enter(&slot->s_evlock);
452 	slot->s_wake = B_TRUE;
453 	cv_broadcast(&slot->s_evcv);
454 	mutex_exit(&slot->s_evlock);
455 }
456 
457 void
458 sda_slot_init(sda_slot_t *slot)
459 {
460 	mutex_init(&slot->s_lock, NULL, MUTEX_DRIVER, NULL);
461 	cv_init(&slot->s_cv, NULL, CV_DRIVER, NULL);
462 	mutex_init(&slot->s_evlock, NULL, MUTEX_DRIVER, NULL);
463 	cv_init(&slot->s_evcv, NULL, CV_DRIVER, NULL);
464 
465 	sda_cmd_list_init(&slot->s_cmdlist);
466 	sda_cmd_list_init(&slot->s_abortlist);
467 }
468 
469 void
470 sda_slot_fini(sda_slot_t *slot)
471 {
472 	sda_cmd_list_fini(&slot->s_cmdlist);
473 	sda_cmd_list_fini(&slot->s_abortlist);
474 	mutex_destroy(&slot->s_lock);
475 	mutex_destroy(&slot->s_evlock);
476 	cv_destroy(&slot->s_cv);
477 	cv_destroy(&slot->s_evcv);
478 }
479 
480 static bd_ops_t sda_bd_ops = {
481 	BD_OPS_CURRENT_VERSION,
482 	sda_mem_bd_driveinfo,
483 	sda_mem_bd_mediainfo,
484 	NULL,			/* devid_init */
485 	NULL,			/* sync_cache */
486 	sda_mem_bd_read,
487 	sda_mem_bd_write,
488 };
489 
490 void
491 sda_slot_attach(sda_slot_t *slot)
492 {
493 	sda_host_t	*h = slot->s_hostp;
494 	char		name[16];
495 	uint32_t	cap;
496 
497 	/*
498 	 * We have two taskqs.  The first taskq is used for
499 	 * card initialization.
500 	 *
501 	 * The second is used for the main processing loop.
502 	 *
503 	 * The reason for a separate taskq is that initialization
504 	 * needs to acquire locks which may be held by the slot
505 	 * thread, or by device driver context... use of the separate
506 	 * taskq breaks the deadlock.  Additionally, the
507 	 * initialization task may need to sleep quite a while during
508 	 * card initialization.
509 	 */
510 
511 	slot->s_bdh = bd_alloc_handle(slot, &sda_bd_ops, h->h_dma, KM_SLEEP);
512 	ASSERT(slot->s_bdh);
513 
514 	sda_slot_enter(slot);
515 
516 	(void) snprintf(name, sizeof (name), "slot_%d_hp_tq",
517 	    slot->s_slot_num);
518 	slot->s_hp_tq = ddi_taskq_create(h->h_dip, name, 1,
519 	    TASKQ_DEFAULTPRI, 0);
520 	if (slot->s_hp_tq == NULL) {
521 		/* Generally, this failure should never occur */
522 		sda_slot_err(slot, "Unable to create hotplug slot taskq");
523 		sda_slot_exit(slot);
524 		bd_free_handle(slot->s_bdh);
525 		slot->s_bdh = NULL;
526 		return;
527 	}
528 
529 	/* create the main processing thread */
530 	(void) snprintf(name, sizeof (name), "slot_%d_main_tq",
531 	    slot->s_slot_num);
532 	slot->s_main_tq = ddi_taskq_create(h->h_dip, name, 1,
533 	    TASKQ_DEFAULTPRI, 0);
534 	if (slot->s_main_tq == NULL) {
535 		/* Generally, this failure should never occur */
536 		sda_slot_err(slot, "Unable to create main slot taskq");
537 		sda_slot_exit(slot);
538 		bd_free_handle(slot->s_bdh);
539 		slot->s_bdh = NULL;
540 		return;
541 	}
542 	(void) ddi_taskq_dispatch(slot->s_main_tq, sda_slot_thread, slot,
543 	    DDI_SLEEP);
544 
545 	/*
546 	 * Determine slot capabilities.
547 	 */
548 	slot->s_caps = 0;
549 
550 	if ((sda_getprop(slot, SDA_PROP_CAP_NOPIO, &cap) == 0) && (cap != 0)) {
551 		slot->s_caps |= SLOT_CAP_NOPIO;
552 	}
553 	if ((sda_getprop(slot, SDA_PROP_CAP_4BITS, &cap) == 0) && (cap != 0)) {
554 		slot->s_caps |= SLOT_CAP_4BITS;
555 	}
556 	if ((sda_getprop(slot, SDA_PROP_CAP_HISPEED, &cap) == 0) &&
557 	    (cap != 0)) {
558 		slot->s_caps |= SLOT_CAP_HISPEED;
559 	}
560 
561 	/* make sure that the host is started up */
562 	if (slot->s_ops.so_reset(slot->s_prv) != 0) {
563 		sda_slot_fault(slot, SDA_FAULT_RESET);
564 	}
565 
566 	sda_slot_exit(slot);
567 
568 	(void) bd_attach_handle(h->h_dip, slot->s_bdh);
569 }
570 
571 void
572 sda_slot_detach(sda_slot_t *slot)
573 {
574 	/*
575 	 * Shut down the thread.
576 	 */
577 	(void) bd_detach_handle(slot->s_bdh);
578 
579 	mutex_enter(&slot->s_evlock);
580 	slot->s_detach = B_TRUE;
581 	cv_broadcast(&slot->s_evcv);
582 	mutex_exit(&slot->s_evlock);
583 
584 	/*
585 	 * Nuke the taskqs. We do this after stopping the background
586 	 * thread to avoid deadlock.
587 	 */
588 	if (slot->s_main_tq)
589 		ddi_taskq_destroy(slot->s_main_tq);
590 	if (slot->s_hp_tq)
591 		ddi_taskq_destroy(slot->s_hp_tq);
592 
593 	bd_free_handle(slot->s_bdh);
594 }
595 
596 void
597 sda_slot_suspend(sda_slot_t *slot)
598 {
599 	mutex_enter(&slot->s_evlock);
600 	slot->s_suspend = B_TRUE;
601 	cv_broadcast(&slot->s_evcv);
602 	mutex_exit(&slot->s_evlock);
603 	ddi_taskq_wait(slot->s_main_tq);
604 }
605 
606 void
607 sda_slot_resume(sda_slot_t *slot)
608 {
609 	mutex_enter(&slot->s_evlock);
610 	slot->s_suspend = B_FALSE;
611 	/*
612 	 * A card change event may have occurred, and in any case we need
613 	 * to reinitialize the card.
614 	 */
615 	slot->s_detect = B_TRUE;
616 	mutex_exit(&slot->s_evlock);
617 
618 	/* Start up a new instance of the main processing task. */
619 	(void) ddi_taskq_dispatch(slot->s_main_tq, sda_slot_thread, slot,
620 	    DDI_SLEEP);
621 }
622 
623 void
624 sda_slot_thread(void *arg)
625 {
626 	sda_slot_t	*slot = arg;
627 
628 	for (;;) {
629 		sda_cmd_t	*cmdp;
630 		boolean_t	datline;
631 		sda_err_t	rv;
632 
633 		mutex_enter(&slot->s_evlock);
634 
635 		/*
636 		 * Process any abort list first.
637 		 */
638 		if ((cmdp = list_head(&slot->s_abortlist)) != NULL) {
639 			list_remove(&slot->s_abortlist, cmdp);
640 			mutex_exit(&slot->s_evlock);
641 			/*
642 			 * EOK used here, to avoid clobbering previous
643 			 * error code.
644 			 */
645 			sda_cmd_notify(cmdp, SDA_CMDF_BUSY | SDA_CMDF_DAT,
646 			    SDA_EOK);
647 			continue;
648 		}
649 
650 		if (slot->s_detach) {
651 			/* Parent is detaching the slot, bail out. */
652 			break;
653 		}
654 
655 		if ((slot->s_suspend) && (slot->s_xfrp == NULL)) {
656 			/*
657 			 * Host wants to suspend, but don't do it if
658 			 * we have a transfer outstanding.
659 			 */
660 			break;
661 		}
662 
663 		if (slot->s_detect) {
664 			slot->s_detect = B_FALSE;
665 			mutex_exit(&slot->s_evlock);
666 
667 			sda_slot_handle_detect(slot);
668 			continue;
669 		}
670 
671 		if (slot->s_xfrdone) {
672 			sda_err_t	errno;
673 
674 			errno = slot->s_errno;
675 			slot->s_errno = SDA_EOK;
676 			slot->s_xfrdone = B_FALSE;
677 			mutex_exit(&slot->s_evlock);
678 
679 			sda_slot_handle_transfer(slot, errno);
680 			continue;
681 		}
682 
683 		if (slot->s_fault != SDA_FAULT_NONE) {
684 			sda_fault_t	fault;
685 
686 			fault = slot->s_fault;
687 			slot->s_fault = SDA_FAULT_NONE;
688 			mutex_exit(&slot->s_evlock);
689 
690 			sda_slot_handle_fault(slot, fault);
691 			continue;
692 		}
693 
694 		if ((slot->s_xfrp != NULL) && (gethrtime() > slot->s_xfrtmo)) {
695 			/*
696 			 * The device stalled processing the data request.
697 			 * At this point, we really have no choice but to
698 			 * nuke the request, and flag a fault.
699 			 */
700 			mutex_exit(&slot->s_evlock);
701 			sda_slot_handle_transfer(slot, SDA_ETIME);
702 			sda_slot_fault(slot, SDA_FAULT_TIMEOUT);
703 			continue;
704 		}
705 
706 		/*
707 		 * If the slot has suspended, then we can't process
708 		 * any new commands yet.
709 		 */
710 		if ((slot->s_suspend) || (!slot->s_wake)) {
711 
712 			/*
713 			 * We use a timed wait if we are waiting for a
714 			 * data transfer to complete.  Otherwise we
715 			 * avoid the timed wait to avoid waking CPU
716 			 * (power savings.)
717 			 */
718 
719 			if ((slot->s_xfrp != NULL) || (slot->s_reap)) {
720 				/* Wait 3 sec (reap attempts). */
721 				(void) cv_reltimedwait(&slot->s_evcv,
722 				    &slot->s_evlock, drv_usectohz(3000000),
723 				    TR_CLOCK_TICK);
724 			} else {
725 				(void) cv_wait(&slot->s_evcv, &slot->s_evlock);
726 			}
727 
728 			mutex_exit(&slot->s_evlock);
729 			continue;
730 		}
731 
732 		slot->s_wake = B_FALSE;
733 
734 		mutex_exit(&slot->s_evlock);
735 
736 		/*
737 		 * We're awake now, so look for work to do.  First
738 		 * acquire access to the slot.
739 		 */
740 		sda_slot_enter(slot);
741 
742 
743 		/*
744 		 * If no more commands to process, go back to sleep.
745 		 */
746 		if ((cmdp = list_head(&slot->s_cmdlist)) == NULL) {
747 			sda_slot_exit(slot);
748 			continue;
749 		}
750 
751 		/*
752 		 * If the current command is not an initialization
753 		 * command, but we are initializing, go back to sleep.
754 		 * (This happens potentially during a card reset or
755 		 * suspend/resume cycle, where the card has not been
756 		 * removed, but a reset is in progress.)
757 		 */
758 		if (slot->s_init && !(cmdp->sc_flags & SDA_CMDF_INIT)) {
759 			sda_slot_exit(slot);
760 			continue;
761 		}
762 
763 		datline = ((cmdp->sc_flags & SDA_CMDF_DAT) != 0);
764 
765 		if (datline) {
766 			/*
767 			 * If the current command has a data phase
768 			 * while a transfer is in progress, then go
769 			 * back to sleep.
770 			 */
771 			if (slot->s_xfrp != NULL) {
772 				sda_slot_exit(slot);
773 				continue;
774 			}
775 
776 			/*
777 			 * Note that APP_CMD doesn't have a data phase,
778 			 * although the associated ACMD might.
779 			 */
780 			if (cmdp->sc_index != CMD_APP_CMD) {
781 				slot->s_xfrp = cmdp;
782 				/*
783 				 * All commands should complete in
784 				 * less than 5 seconds.  The worst
785 				 * case is actually somewhere around 4
786 				 * seconds, but that is when the clock
787 				 * is only 100 kHz.
788 				 */
789 				slot->s_xfrtmo = gethrtime() +
790 				    5000000000ULL;
791 				(void) sda_setprop(slot, SDA_PROP_LED, 1);
792 			}
793 		}
794 
795 		/*
796 		 * We're committed to dispatching this command now,
797 		 * so remove it from the list.
798 		 */
799 		list_remove(&slot->s_cmdlist, cmdp);
800 
801 		/*
802 		 * There could be more commands after this one, so we
803 		 * mark ourself so we stay awake for another cycle.
804 		 */
805 		sda_slot_wakeup(slot);
806 
807 		/*
808 		 * Submit the command.  Note that we are holding the
809 		 * slot lock here, so it is critical that the caller
810 		 * *not* call back up into the framework.  The caller
811 		 * must break context.  But doing it this way prevents
812 		 * a critical race on card removal.
813 		 *
814 		 * Note that we don't resubmit memory to the device if
815 		 * it isn't flagged as ready (e.g. if the wrong device
816 		 * was inserted!)
817 		 */
818 		if ((!slot->s_ready) && (cmdp->sc_flags & SDA_CMDF_MEM)) {
819 			rv = SDA_ENODEV;
820 		} else {
821 			rv = slot->s_ops.so_cmd(slot->s_prv, cmdp);
822 		}
823 		if (rv == SDA_EOK)
824 			rv = sda_slot_check_response(cmdp);
825 
826 		if (rv == SDA_EOK) {
827 			/*
828 			 * If APP_CMD completed properly, then
829 			 * resubmit with ACMD index.  Note wake was
830 			 * already set above.
831 			 */
832 			if (cmdp->sc_index == CMD_APP_CMD) {
833 				if ((cmdp->sc_response[0] & R1_APP_CMD) == 0) {
834 					sda_slot_log(slot, "APP_CMD not set!");
835 				}
836 				sda_cmd_resubmit_acmd(slot, cmdp);
837 				sda_slot_exit(slot);
838 
839 				continue;
840 			}
841 
842 		} else if (datline) {
843 			/*
844 			 * If an error occurred and we were expecting
845 			 * a transfer phase, we have to clean up.
846 			 */
847 			(void) sda_setprop(slot, SDA_PROP_LED, 0);
848 			slot->s_xfrp = NULL;
849 			slot->s_xfrtmo = 0;
850 
851 			/*
852 			 * And notify any waiter.
853 			 */
854 			sda_slot_exit(slot);
855 			sda_cmd_notify(cmdp, SDA_CMDF_BUSY | SDA_CMDF_DAT, rv);
856 			continue;
857 		}
858 
859 		/*
860 		 * Wake any waiter.
861 		 */
862 		sda_slot_exit(slot);
863 		sda_cmd_notify(cmdp, SDA_CMDF_BUSY, rv);
864 	}
865 
866 	mutex_exit(&slot->s_evlock);
867 }
868 
869 void
870 sda_slot_vprintf(sda_slot_t *s, int level, const char *fmt, va_list ap)
871 {
872 	char		msgbuf[256];
873 	const char	*pfx, *sfx;
874 
875 	if (level == CE_CONT) {
876 		pfx = "!";
877 		sfx = "\n";
878 	} else {
879 		pfx = sfx = "";
880 	}
881 
882 	if (s != NULL) {
883 		dev_info_t	*dip = s->s_hostp->h_dip;
884 
885 		(void) snprintf(msgbuf, sizeof (msgbuf),
886 		    "%s%s%d: slot %d: %s%s", pfx,
887 		    ddi_driver_name(dip), ddi_get_instance(dip),
888 		    s->s_slot_num, fmt, sfx);
889 	} else {
890 		(void) snprintf(msgbuf, sizeof (msgbuf), "%ssda: %s%s",
891 		    pfx, fmt, sfx);
892 	}
893 	vcmn_err(level, msgbuf, ap);
894 }
895 
896 void
897 sda_slot_err(sda_slot_t *s, const char *fmt, ...)
898 {
899 	va_list	ap;
900 
901 	va_start(ap, fmt);
902 	sda_slot_vprintf(s, CE_WARN, fmt, ap);
903 	va_end(ap);
904 }
905 
906 void
907 sda_slot_log(sda_slot_t *s, const char *fmt, ...)
908 {
909 	va_list	ap;
910 
911 	va_start(ap, fmt);
912 	sda_slot_vprintf(s, CE_CONT, fmt, ap);
913 	va_end(ap);
914 }
915