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