xref: /illumos-gate/usr/src/uts/common/io/sdcard/impl/sda_slot.c (revision 7ffba875a0c7cf118aef7a2c9bfd00c3935e230a)
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 2010 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 /*
27  * SD card slot support.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/cmn_err.h>
32 #include <sys/varargs.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/sdcard/sda_impl.h>
36 
37 
38 /*
39  * Prototypes.
40  */
41 
42 static void sda_slot_insert(void *);
43 static sda_err_t sda_slot_check_response(sda_cmd_t *);
44 static void sda_slot_handle_detect(sda_slot_t *);
45 static void sda_slot_handle_transfer(sda_slot_t *, sda_err_t);
46 static void sda_slot_handle_fault(sda_slot_t *, sda_fault_t);
47 static void sda_slot_abort(sda_slot_t *, sda_err_t);
48 static void sda_slot_halt(sda_slot_t *);
49 static void sda_slot_thread(void *);
50 static void sda_slot_vprintf(sda_slot_t *, int, const char *, va_list);
51 
52 /*
53  * Static Variables.
54  */
55 
56 static struct {
57 	sda_fault_t	fault;
58 	const char	*msg;
59 } sda_slot_faults[] = {
60 	{ SDA_FAULT_TIMEOUT,	"Data transfer timed out" },
61 	{ SDA_FAULT_ACMD12,	"Auto CMD12 failure" },
62 	{ SDA_FAULT_CRC7,	"CRC7 failure on CMD/DAT line" },
63 	{ SDA_FAULT_PROTO,	"SD/MMC protocol signaling error" },
64 	{ SDA_FAULT_INIT,	"Card initialization failure" },
65 	{ SDA_FAULT_HOST,	"Internal host or slot failure" },
66 	{ SDA_FAULT_CURRENT,	"Current overlimit detected" },
67 	{ SDA_FAULT_RESET,	"Failed to reset slot" },
68 	{ SDA_FAULT_NONE,	NULL },	/* sentinel, must be last! */
69 };
70 
71 /*
72  * Internal implementation.
73  */
74 
75 /*
76  * These allow for recursive entry.  This is necessary to facilitate
77  * simpler locking with things like the fault handler, where a caller
78  * might already be "holding" the slot.
79  *
80  * This is modeled in part after ndi_devi_enter and ndi_devi_exit.
81  */
82 void
83 sda_slot_enter(sda_slot_t *slot)
84 {
85 	kt_did_t	self = ddi_get_kt_did();
86 	mutex_enter(&slot->s_lock);
87 	if (slot->s_owner == self) {
88 		slot->s_circular++;
89 	} else {
90 		while ((slot->s_owner != 0) && (slot->s_owner != self)) {
91 			cv_wait(&slot->s_cv, &slot->s_lock);
92 		}
93 		slot->s_owner = self;
94 		slot->s_circular++;
95 	}
96 	mutex_exit(&slot->s_lock);
97 }
98 
99 void
100 sda_slot_exit(sda_slot_t *slot)
101 {
102 	ASSERT(sda_slot_owned(slot));
103 
104 	mutex_enter(&slot->s_lock);
105 	slot->s_circular--;
106 	if (slot->s_circular == 0) {
107 		slot->s_owner = 0;
108 		cv_broadcast(&slot->s_cv);
109 	}
110 	mutex_exit(&slot->s_lock);
111 }
112 
113 boolean_t
114 sda_slot_owned(sda_slot_t *slot)
115 {
116 	return (slot->s_owner == ddi_get_kt_did());
117 }
118 
119 sda_err_t
120 sda_slot_check_response(sda_cmd_t *cmdp)
121 {
122 	uint32_t	errs;
123 	switch (cmdp->sc_rtype & 0xf) {
124 	case R1:
125 		if ((errs = (cmdp->sc_response[0] & R1_ERRS)) != 0) {
126 			if (errs & (R1_WP_VIOLATION | R1_CSD_OVERWRITE)) {
127 				return (SDA_EWPROTECT);
128 			}
129 			if (errs & (R1_ADDRESS_ERROR | R1_BLOCK_LEN_ERROR |
130 			    R1_OUT_OF_RANGE | R1_ERASE_PARAM)) {
131 				return (SDA_EINVAL);
132 			}
133 			return (SDA_EIO);
134 		}
135 		break;
136 	case R5:
137 		if ((errs = (cmdp->sc_response[0] & R5_ERRS)) != 0) {
138 			return (SDA_EIO);
139 		}
140 		break;
141 	}
142 	return (SDA_EOK);
143 }
144 
145 void
146 sda_slot_halt(sda_slot_t *slot)
147 {
148 	sda_slot_enter(slot);
149 	slot->s_ops.so_halt(slot->s_prv);
150 	/* We need to wait 1 msec for power down. */
151 	drv_usecwait(1000);
152 	sda_slot_exit(slot);
153 }
154 
155 void
156 sda_slot_reset(sda_slot_t *slot)
157 {
158 	sda_slot_enter(slot);
159 	if (slot->s_ops.so_reset(slot->s_prv) != 0) {
160 		sda_slot_fault(slot, SDA_FAULT_RESET);
161 	}
162 	sda_slot_exit(slot);
163 }
164 
165 int
166 sda_slot_power_on(sda_slot_t *slot)
167 {
168 	int		rv;
169 	uint32_t	ocr;
170 
171 	sda_slot_enter(slot);
172 
173 	/*
174 	 * Get the voltage supplied by the host.  Note that we expect
175 	 * hosts will include a range of 2.7-3.7 in their supported
176 	 * voltage ranges.  The spec does not allow for hosts that
177 	 * cannot supply a voltage in this range, yet.
178 	 */
179 	if ((rv = sda_getprop(slot, SDA_PROP_OCR, &ocr)) != 0) {
180 		sda_slot_err(slot, "Failed to get host OCR (%d)", rv);
181 		goto done;
182 	}
183 	if ((ocr & OCR_HI_MASK) == 0) {
184 		sda_slot_err(slot, "Host does not support standard voltages.");
185 		rv = ENOTSUP;
186 		goto done;
187 	}
188 
189 	/*
190 	 * We prefer 3.3V, 3.0V, and failing that, just use the
191 	 * maximum that the host supports.  3.3V is preferable,
192 	 * because it is the typical common voltage that just about
193 	 * everything supports.  Otherwise we just pick the highest
194 	 * supported voltage.  This facilitates initial power up.
195 	 */
196 	if (ocr & OCR_32_33V) {
197 		slot->s_cur_ocr = OCR_32_33V;
198 	} else if (ocr & OCR_29_30V) {
199 		slot->s_cur_ocr = OCR_29_30V;
200 	} else {
201 		slot->s_cur_ocr = (1U << (ddi_fls(ocr) - 1));
202 	}
203 
204 	/*
205 	 * Turn on the power.
206 	 */
207 	if ((rv = sda_setprop(slot, SDA_PROP_OCR, slot->s_cur_ocr)) != 0) {
208 		sda_slot_err(slot, "Failed to set OCR %x (%d)",
209 		    slot->s_cur_ocr, rv);
210 		goto done;
211 	}
212 
213 	sda_slot_exit(slot);
214 
215 	/*
216 	 * Wait 250 msec (per spec) for power ramp to complete.
217 	 */
218 	delay(drv_usectohz(250000));
219 	return (0);
220 
221 done:
222 	sda_slot_exit(slot);
223 	return (rv);
224 }
225 
226 void
227 sda_slot_power_off(sda_slot_t *slot)
228 {
229 	sda_slot_enter(slot);
230 	(void) sda_setprop(slot, SDA_PROP_OCR, 0);
231 	/* XXX: FMA: on failure this should cause a fault to be generated */
232 	/* spec requires voltage to stay low for at least 1 msec */
233 	drv_usecwait(1000);
234 	sda_slot_exit(slot);
235 }
236 
237 void
238 sda_slot_insert(void *arg)
239 {
240 	sda_slot_t	*slot = arg;
241 
242 	if (sda_init_card(slot) != SDA_EOK) {
243 		/*
244 		 * Remove power from the slot.  If a more severe fault
245 		 * occurred, then a manual reset with cfgadm will be needed.
246 		 */
247 		sda_slot_err(slot, "Unable to initialize card!");
248 		sda_slot_enter(slot);
249 		sda_slot_power_off(slot);
250 		sda_slot_abort(slot, SDA_ENODEV);
251 		sda_slot_exit(slot);
252 
253 	} else if ((slot->s_flags & SLOTF_MEMORY) == 0) {
254 		/*
255 		 * SDIO: For SDIO, we can write the card's
256 		 * MANFID tuple in CIS to the UUID.  Until we
257 		 * support SDIO, we just suppress creating
258 		 * devinfo nodes.
259 		 */
260 		sda_slot_err(slot, "Non-memory target not supported");
261 	} else {
262 
263 		sda_slot_enter(slot);
264 		if (sda_mem_parse_cid_csd(slot) != DDI_SUCCESS) {
265 			sda_slot_err(slot,
266 			    "Unable to parse card identification");
267 		} else {
268 			slot->s_warn = B_FALSE;
269 			slot->s_ready = B_TRUE;
270 		}
271 		sda_slot_exit(slot);
272 	}
273 
274 	slot->s_stamp = ddi_get_time();
275 	slot->s_intransit = 0;
276 	bd_state_change(slot->s_bdh);
277 }
278 
279 void
280 sda_slot_abort(sda_slot_t *slot, sda_err_t errno)
281 {
282 	sda_cmd_t	*cmdp;
283 
284 	ASSERT(sda_slot_owned(slot));
285 
286 	if ((cmdp = slot->s_xfrp) != NULL) {
287 		slot->s_xfrp = NULL;
288 		sda_cmd_notify(cmdp, 0, errno);
289 		list_insert_tail(&slot->s_abortlist, cmdp);
290 	}
291 	while ((cmdp = list_head(&slot->s_cmdlist)) != NULL) {
292 		list_remove(&slot->s_cmdlist, cmdp);
293 		sda_cmd_notify(cmdp, 0, errno);
294 		list_insert_tail(&slot->s_abortlist, cmdp);
295 	}
296 
297 	sda_slot_wakeup(slot);
298 }
299 
300 void
301 sda_slot_handle_transfer(sda_slot_t *slot, sda_err_t errno)
302 {
303 	sda_cmd_t	*cmdp;
304 
305 	sda_slot_enter(slot);
306 
307 	if ((cmdp = slot->s_xfrp) != NULL) {
308 
309 		slot->s_xfrp = NULL;
310 		slot->s_xfrtmo = 0;
311 		(void) sda_setprop(slot, SDA_PROP_LED, 0);
312 		sda_slot_exit(slot);
313 
314 		sda_slot_wakeup(slot);
315 
316 		sda_cmd_notify(cmdp, SDA_CMDF_DAT, errno);
317 	} else {
318 		sda_slot_exit(slot);
319 	}
320 }
321 
322 void
323 sda_slot_handle_fault(sda_slot_t *slot, sda_fault_t fault)
324 {
325 	const char	*msg;
326 	int		i;
327 
328 	sda_slot_enter(slot);
329 
330 	if ((fault == SDA_FAULT_TIMEOUT) && (slot->s_init)) {
331 		/*
332 		 * Timeouts during initialization are quite normal.
333 		 */
334 		sda_slot_exit(slot);
335 		return;
336 	}
337 
338 	slot->s_failed = B_TRUE;
339 	sda_slot_abort(slot, SDA_EFAULT);
340 
341 	msg = "Unknown fault (%d)";
342 	for (i = 0; sda_slot_faults[i].msg != NULL; i++) {
343 		if (sda_slot_faults[i].fault == fault) {
344 			msg = sda_slot_faults[i].msg;
345 			break;
346 		}
347 	}
348 
349 	/*
350 	 * FMA would be a better choice here.
351 	 */
352 	sda_slot_err(slot, msg, fault);
353 
354 	/*
355 	 * Shut down the slot.  Interaction from userland via cfgadm
356 	 * can revive it.
357 	 *
358 	 * FMA can help here.
359 	 */
360 	sda_slot_halt(slot);
361 
362 	sda_slot_exit(slot);
363 }
364 
365 void
366 sda_slot_handle_detect(sda_slot_t *slot)
367 {
368 	uint32_t	inserted;
369 
370 	sda_slot_enter(slot);
371 
372 	slot->s_stamp = ddi_get_time();
373 	slot->s_intransit = 1;
374 	slot->s_flags = 0;
375 	slot->s_rca = 0;
376 	slot->s_ready = B_FALSE;
377 
378 	sda_getprop(slot, SDA_PROP_INSERTED, &inserted);
379 	slot->s_inserted = (inserted != 0);
380 
381 	if (slot->s_inserted && !slot->s_failed) {
382 		/*
383 		 * We need to initialize the card, so we only support
384 		 * hipri commands for now.
385 		 */
386 		slot->s_init = B_TRUE;
387 		sda_slot_exit(slot);
388 
389 		/*
390 		 * Card insertion occurred.  We have to run this on
391 		 * another task, to avoid deadlock as the task may
392 		 * need to dispatch commands.
393 		 */
394 
395 		(void) ddi_taskq_dispatch(slot->s_hp_tq, sda_slot_insert, slot,
396 		    DDI_SLEEP);
397 	} else {
398 
399 		/*
400 		 * Nuke in-flight commands.
401 		 */
402 		sda_slot_abort(slot, SDA_ENODEV);
403 
404 		/*
405 		 * Restart the slot (incl. power cycle).  This gets the
406 		 * slot to a known good state.
407 		 */
408 		sda_slot_reset(slot);
409 
410 		slot->s_intransit = 0;
411 		sda_slot_exit(slot);
412 
413 		bd_state_change(slot->s_bdh);
414 	}
415 
416 	sda_slot_wakeup(slot);
417 }
418 
419 void
420 sda_slot_transfer(sda_slot_t *slot, sda_err_t errno)
421 {
422 	mutex_enter(&slot->s_evlock);
423 	slot->s_errno = errno;
424 	slot->s_xfrdone = B_TRUE;
425 	cv_broadcast(&slot->s_evcv);
426 	mutex_exit(&slot->s_evlock);
427 }
428 
429 void
430 sda_slot_detect(sda_slot_t *slot)
431 {
432 	mutex_enter(&slot->s_evlock);
433 	slot->s_detect = B_TRUE;
434 	cv_broadcast(&slot->s_evcv);
435 	mutex_exit(&slot->s_evlock);
436 }
437 
438 void
439 sda_slot_fault(sda_slot_t *slot, sda_fault_t fault)
440 {
441 	mutex_enter(&slot->s_evlock);
442 	slot->s_fault = fault;
443 	cv_broadcast(&slot->s_evcv);
444 	mutex_exit(&slot->s_evlock);
445 }
446 
447 void
448 sda_slot_wakeup(sda_slot_t *slot)
449 {
450 	mutex_enter(&slot->s_evlock);
451 	slot->s_wake = B_TRUE;
452 	cv_broadcast(&slot->s_evcv);
453 	mutex_exit(&slot->s_evlock);
454 }
455 
456 void
457 sda_slot_init(sda_slot_t *slot)
458 {
459 	mutex_init(&slot->s_lock, NULL, MUTEX_DRIVER, NULL);
460 	cv_init(&slot->s_cv, NULL, CV_DRIVER, NULL);
461 	mutex_init(&slot->s_evlock, NULL, MUTEX_DRIVER, NULL);
462 	cv_init(&slot->s_evcv, NULL, CV_DRIVER, NULL);
463 
464 	sda_cmd_list_init(&slot->s_cmdlist);
465 	sda_cmd_list_init(&slot->s_abortlist);
466 }
467 
468 void
469 sda_slot_fini(sda_slot_t *slot)
470 {
471 	sda_cmd_list_fini(&slot->s_cmdlist);
472 	sda_cmd_list_fini(&slot->s_abortlist);
473 	mutex_destroy(&slot->s_lock);
474 	mutex_destroy(&slot->s_evlock);
475 	cv_destroy(&slot->s_cv);
476 	cv_destroy(&slot->s_evcv);
477 }
478 
479 static bd_ops_t sda_bd_ops = {
480 	BD_OPS_VERSION_0,
481 	sda_mem_bd_driveinfo,
482 	sda_mem_bd_mediainfo,
483 	NULL,			/* devid_init */
484 	NULL,			/* sync_cache */
485 	sda_mem_bd_read,
486 	sda_mem_bd_write,
487 	NULL			/* dump */
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