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