xref: /freebsd/sys/cam/mmc/mmc_xpt.c (revision cddbc3b40812213ff00041f79174cac0be360a2a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/endian.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/time.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
43 #include <sys/sbuf.h>
44 
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/condvar.h>
49 
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
60 
61 #include <cam/mmc/mmc.h>
62 #include <cam/mmc/mmc_bus.h>
63 
64 #include <machine/stdarg.h>	/* for xpt_print below */
65 #include <machine/_inttypes.h>  /* for PRIu64 */
66 #include "opt_cam.h"
67 
68 FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");
69 
70 static struct cam_ed * mmc_alloc_device(struct cam_eb *bus,
71     struct cam_et *target, lun_id_t lun_id);
72 static void mmc_dev_async(u_int32_t async_code, struct cam_eb *bus,
73     struct cam_et *target, struct cam_ed *device, void *async_arg);
74 static void	 mmc_action(union ccb *start_ccb);
75 static void	 mmc_dev_advinfo(union ccb *start_ccb);
76 static void	 mmc_announce_periph(struct cam_periph *periph);
77 static void	 mmc_scan_lun(struct cam_periph *periph,
78     struct cam_path *path, cam_flags flags, union ccb *ccb);
79 
80 /* mmcprobe methods */
81 static cam_status mmcprobe_register(struct cam_periph *periph, void *arg);
82 static void	 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb);
83 static void	 mmcprobe_cleanup(struct cam_periph *periph);
84 static void	 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb);
85 
86 static void mmc_proto_announce(struct cam_ed *device);
87 static void mmc_proto_denounce(struct cam_ed *device);
88 static void mmc_proto_debug_out(union ccb *ccb);
89 
90 typedef enum {
91 	PROBE_RESET,
92 	PROBE_IDENTIFY,
93         PROBE_SDIO_RESET,
94 	PROBE_SEND_IF_COND,
95         PROBE_SDIO_INIT,
96         PROBE_MMC_INIT,
97 	PROBE_SEND_APP_OP_COND,
98         PROBE_GET_CID,
99         PROBE_GET_CSD,
100         PROBE_SEND_RELATIVE_ADDR,
101 	PROBE_MMC_SET_RELATIVE_ADDR,
102 	PROBE_SELECT_CARD,
103 	PROBE_DONE,
104 	PROBE_INVALID
105 } probe_action;
106 
107 static char *probe_action_text[] = {
108 	"PROBE_RESET",
109 	"PROBE_IDENTIFY",
110         "PROBE_SDIO_RESET",
111 	"PROBE_SEND_IF_COND",
112         "PROBE_SDIO_INIT",
113         "PROBE_MMC_INIT",
114 	"PROBE_SEND_APP_OP_COND",
115         "PROBE_GET_CID",
116         "PROBE_GET_CSD",
117         "PROBE_SEND_RELATIVE_ADDR",
118 	"PROBE_MMC_SET_RELATIVE_ADDR",
119         "PROBE_SELECT_CARD",
120 	"PROBE_DONE",
121 	"PROBE_INVALID"
122 };
123 
124 #define PROBE_SET_ACTION(softc, newaction)	\
125 do {									\
126 	char **text;							\
127 	text = probe_action_text;					\
128 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
129 	    ("Probe %s to %s\n", text[(softc)->action],			\
130 	    text[(newaction)]));					\
131 	(softc)->action = (newaction);					\
132 } while(0)
133 
134 static struct xpt_xport_ops mmc_xport_ops = {
135 	.alloc_device = mmc_alloc_device,
136 	.action = mmc_action,
137 	.async = mmc_dev_async,
138 	.announce = mmc_announce_periph,
139 };
140 
141 #define MMC_XPT_XPORT(x, X)				\
142 	static struct xpt_xport mmc_xport_ ## x = {	\
143 		.xport = XPORT_ ## X,			\
144 		.name = #x,				\
145 		.ops = &mmc_xport_ops,			\
146 	};						\
147 	CAM_XPT_XPORT(mmc_xport_ ## x);
148 
149 MMC_XPT_XPORT(mmc, MMCSD);
150 
151 static struct xpt_proto_ops mmc_proto_ops = {
152 	.announce = mmc_proto_announce,
153 	.denounce = mmc_proto_denounce,
154 	.debug_out = mmc_proto_debug_out,
155 };
156 
157 static struct xpt_proto mmc_proto = {
158 	.proto = PROTO_MMCSD,
159 	.name = "mmcsd",
160 	.ops = &mmc_proto_ops,
161 };
162 CAM_XPT_PROTO(mmc_proto);
163 
164 typedef struct {
165 	probe_action	action;
166 	int             restart;
167 	union ccb	saved_ccb;
168 	uint32_t	flags;
169 #define PROBE_FLAG_ACMD_SENT	0x1 /* CMD55 is sent, card expects ACMD */
170 	uint8_t         acmd41_count; /* how many times ACMD41 has been issued */
171 	struct cam_periph *periph;
172 } mmcprobe_softc;
173 
174 /* XPort functions -- an interface to CAM at periph side */
175 
176 static struct cam_ed *
177 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
178 {
179 	struct cam_ed *device;
180 
181 	printf("mmc_alloc_device()\n");
182 	device = xpt_alloc_device(bus, target, lun_id);
183 	if (device == NULL)
184 		return (NULL);
185 
186 	device->quirk = NULL;
187 	device->mintags = 0;
188 	device->maxtags = 0;
189 	bzero(&device->inq_data, sizeof(device->inq_data));
190 	device->inq_flags = 0;
191 	device->queue_flags = 0;
192 	device->serial_num = NULL;
193 	device->serial_num_len = 0;
194 	return (device);
195 }
196 
197 static void
198 mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
199 	      struct cam_ed *device, void *async_arg)
200 {
201 
202 	printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
203 	       async_code,
204 	       bus->path_id,
205 	       target->target_id,
206 	       device->lun_id);
207 	/*
208 	 * We only need to handle events for real devices.
209 	 */
210 	if (target->target_id == CAM_TARGET_WILDCARD
211             || device->lun_id == CAM_LUN_WILDCARD)
212 		return;
213 
214         if (async_code == AC_LOST_DEVICE) {
215                 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
216                         printf("AC_LOST_DEVICE -> set to unconfigured\n");
217                         device->flags |= CAM_DEV_UNCONFIGURED;
218                         xpt_release_device(device);
219                 } else {
220                         printf("AC_LOST_DEVICE on unconfigured device\n");
221                 }
222         } else if (async_code == AC_FOUND_DEVICE) {
223                 printf("Got AC_FOUND_DEVICE -- whatever...\n");
224         } else if (async_code == AC_PATH_REGISTERED) {
225                 printf("Got AC_PATH_REGISTERED -- whatever...\n");
226         } else if (async_code == AC_PATH_DEREGISTERED ) {
227                         printf("Got AC_PATH_DEREGISTERED -- whatever...\n");
228 	} else if (async_code == AC_UNIT_ATTENTION) {
229 		printf("Got interrupt generated by the card and ignored it\n");
230 	} else
231 		panic("Unknown async code\n");
232 }
233 
234 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
235 static void
236 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
237 	     cam_flags flags, union ccb *request_ccb)
238 {
239 	struct ccb_pathinq cpi;
240 	cam_status status;
241 	struct cam_periph *old_periph;
242 	int lock;
243 
244 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
245 
246 	xpt_path_inq(&cpi, path);
247 
248 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
249 		if (request_ccb != NULL) {
250 			request_ccb->ccb_h.status = cpi.ccb_h.status;
251 			xpt_done(request_ccb);
252 		}
253 		return;
254 	}
255 
256 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
257 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
258 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
259 		xpt_done(request_ccb);
260 		return;
261 	}
262 
263 	lock = (xpt_path_owned(path) == 0);
264 	if (lock)
265 		xpt_path_lock(path);
266 
267 	if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
268 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
269 //			mmcprobe_softc *softc;
270 //			softc = (mmcprobe_softc *)old_periph->softc;
271 //                      Not sure if we need request ccb queue for mmc
272 //			TAILQ_INSERT_TAIL(&softc->request_ccbs,
273 //				&request_ccb->ccb_h, periph_links.tqe);
274 //			softc->restart = 1;
275                         CAM_DEBUG(path, CAM_DEBUG_INFO,
276                                   ("Got scan request, but mmcprobe already exists\n"));
277 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
278                         xpt_done(request_ccb);
279 		} else {
280 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
281 			xpt_done(request_ccb);
282 		}
283 	} else {
284 		xpt_print(path, " Set up the mmcprobe device...\n");
285 
286                 status = cam_periph_alloc(mmcprobe_register, NULL,
287 					  mmcprobe_cleanup,
288 					  mmcprobe_start,
289 					  "mmcprobe",
290 					  CAM_PERIPH_BIO,
291 					  path, NULL, 0,
292 					  request_ccb);
293                 if (status != CAM_REQ_CMP) {
294 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
295                                   "returned an error, can't continue probe\n");
296 		}
297 		request_ccb->ccb_h.status = status;
298 		xpt_done(request_ccb);
299 	}
300 
301 	if (lock)
302 		xpt_path_unlock(path);
303 }
304 
305 static void
306 mmc_action(union ccb *start_ccb)
307 {
308 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
309 		  ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
310 		   xpt_action_name(start_ccb->ccb_h.func_code)));
311 	switch (start_ccb->ccb_h.func_code) {
312 
313 	case XPT_SCAN_BUS:
314                 /* FALLTHROUGH */
315 	case XPT_SCAN_TGT:
316                 /* FALLTHROUGH */
317 	case XPT_SCAN_LUN:
318 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
319 			  ("XPT_SCAN_{BUS,TGT,LUN}\n"));
320 		mmc_scan_lun(start_ccb->ccb_h.path->periph,
321 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
322 			     start_ccb);
323 		break;
324 
325 	case XPT_DEV_ADVINFO:
326 	{
327 		mmc_dev_advinfo(start_ccb);
328 		break;
329 	}
330 
331 	default:
332 		xpt_action_default(start_ccb);
333 		break;
334 	}
335 }
336 
337 static void
338 mmc_dev_advinfo(union ccb *start_ccb)
339 {
340 	struct cam_ed *device;
341 	struct ccb_dev_advinfo *cdai;
342 	off_t amt;
343 
344 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
345 	device = start_ccb->ccb_h.path->device;
346 	cdai = &start_ccb->cdai;
347 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
348 		  ("%s: request %x\n", __func__, cdai->buftype));
349 
350         /* We don't support writing any data */
351         if (cdai->flags & CDAI_FLAG_STORE)
352                 panic("Attempt to store data?!");
353 
354 	switch(cdai->buftype) {
355 	case CDAI_TYPE_SCSI_DEVID:
356 		cdai->provsiz = device->device_id_len;
357 		if (device->device_id_len == 0)
358 			break;
359 		amt = MIN(cdai->provsiz, cdai->bufsiz);
360 		memcpy(cdai->buf, device->device_id, amt);
361 		break;
362 	case CDAI_TYPE_SERIAL_NUM:
363 		cdai->provsiz = device->serial_num_len;
364 		if (device->serial_num_len == 0)
365 			break;
366 		amt = MIN(cdai->provsiz, cdai->bufsiz);
367 		memcpy(cdai->buf, device->serial_num, amt);
368 		break;
369         case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
370                 cdai->provsiz = 0;
371                 break;
372 	case CDAI_TYPE_MMC_PARAMS:
373 		cdai->provsiz = sizeof(struct mmc_params);
374 		amt = MIN(cdai->provsiz, cdai->bufsiz);
375 		memcpy(cdai->buf, &device->mmc_ident_data, amt);
376 		break;
377 	default:
378                 panic("Unknown buftype");
379 		return;
380 	}
381 	start_ccb->ccb_h.status = CAM_REQ_CMP;
382 }
383 
384 static void
385 mmc_announce_periph(struct cam_periph *periph)
386 {
387 	struct	ccb_pathinq cpi;
388 	struct	ccb_trans_settings cts;
389 	struct	cam_path *path = periph->path;
390 
391 	cam_periph_assert(periph, MA_OWNED);
392 
393 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
394 		  ("mmc_announce_periph: called\n"));
395 
396 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
397 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
398 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
399 	xpt_action((union ccb*)&cts);
400 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
401 		return;
402 	xpt_path_inq(&cpi, periph->path);
403 	printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
404 }
405 
406 /* This func is called per attached device :-( */
407 void
408 mmc_print_ident(struct mmc_params *ident_data)
409 {
410         printf("Relative addr: %08x\n", ident_data->card_rca);
411         printf("Card features: <");
412         if (ident_data->card_features & CARD_FEATURE_MMC)
413                 printf("MMC ");
414         if (ident_data->card_features & CARD_FEATURE_MEMORY)
415                 printf("Memory ");
416         if (ident_data->card_features & CARD_FEATURE_SDHC)
417                 printf("High-Capacity ");
418         if (ident_data->card_features & CARD_FEATURE_SD20)
419                 printf("SD2.0-Conditions ");
420         if (ident_data->card_features & CARD_FEATURE_SDIO)
421                 printf("SDIO ");
422         printf(">\n");
423 
424         if (ident_data->card_features & CARD_FEATURE_MEMORY)
425                 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
426 
427         if (ident_data->card_features & CARD_FEATURE_SDIO) {
428                 printf("Card IO OCR: %08x\n", ident_data->io_ocr);
429                 printf("Number of funcitions: %u\n", ident_data->sdio_func_count);
430         }
431 }
432 
433 static void
434 mmc_proto_announce(struct cam_ed *device)
435 {
436 	mmc_print_ident(&device->mmc_ident_data);
437 }
438 
439 static void
440 mmc_proto_denounce(struct cam_ed *device)
441 {
442 	mmc_print_ident(&device->mmc_ident_data);
443 }
444 
445 static void
446 mmc_proto_debug_out(union ccb *ccb)
447 {
448 	if (ccb->ccb_h.func_code != XPT_MMC_IO)
449 		return;
450 
451 	CAM_DEBUG(ccb->ccb_h.path,
452 	    CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
453 }
454 
455 static periph_init_t probe_periph_init;
456 
457 static struct periph_driver probe_driver =
458 {
459 	probe_periph_init, "mmcprobe",
460 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
461 	CAM_PERIPH_DRV_EARLY
462 };
463 
464 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
465 
466 #define	CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
467 
468 static void
469 probe_periph_init()
470 {
471 }
472 
473 static cam_status
474 mmcprobe_register(struct cam_periph *periph, void *arg)
475 {
476 	mmcprobe_softc *softc;
477 	union ccb *request_ccb;	/* CCB representing the probe request */
478 	int status;
479 
480 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
481 
482 	request_ccb = (union ccb *)arg;
483 	if (request_ccb == NULL) {
484 		printf("mmcprobe_register: no probe CCB, "
485 		       "can't register device\n");
486 		return(CAM_REQ_CMP_ERR);
487 	}
488 
489 	softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
490 
491 	if (softc == NULL) {
492 		printf("proberegister: Unable to probe new device. "
493 		       "Unable to allocate softc\n");
494 		return(CAM_REQ_CMP_ERR);
495 	}
496 
497 	softc->flags = 0;
498 	softc->acmd41_count = 0;
499 	periph->softc = softc;
500 	softc->periph = periph;
501 	softc->action = PROBE_INVALID;
502         softc->restart = 0;
503 	status = cam_periph_acquire(periph);
504 
505         memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
506 	if (status != 0) {
507 		printf("proberegister: cam_periph_acquire failed (status=%d)\n",
508 			status);
509 		return (CAM_REQ_CMP_ERR);
510 	}
511 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
512 
513 	if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
514 		PROBE_SET_ACTION(softc, PROBE_RESET);
515 	else
516 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
517 
518 	/* This will kick the ball */
519 	xpt_schedule(periph, CAM_PRIORITY_XPT);
520 
521 	return(CAM_REQ_CMP);
522 }
523 
524 static int
525 mmc_highest_voltage(uint32_t ocr)
526 {
527 	int i;
528 
529 	for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
530 	    i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
531 		if (ocr & (1 << i))
532 			return (i);
533 	return (-1);
534 }
535 
536 static inline void
537 init_standard_ccb(union ccb *ccb, uint32_t cmd)
538 {
539 	ccb->ccb_h.func_code = cmd;
540 	ccb->ccb_h.flags = CAM_DIR_OUT;
541 	ccb->ccb_h.retry_count = 0;
542 	ccb->ccb_h.timeout = 15 * 1000;
543 	ccb->ccb_h.cbfcnp = mmcprobe_done;
544 }
545 
546 static void
547 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
548 {
549 	mmcprobe_softc *softc;
550 	struct cam_path *path;
551 	struct ccb_mmcio *mmcio;
552 	struct mtx *p_mtx = cam_periph_mtx(periph);
553 	struct ccb_trans_settings_mmc *cts;
554 
555 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
556 	softc = (mmcprobe_softc *)periph->softc;
557 	path = start_ccb->ccb_h.path;
558 	mmcio = &start_ccb->mmcio;
559 	cts = &start_ccb->cts.proto_specific.mmc;
560 	struct mmc_params *mmcp = &path->device->mmc_ident_data;
561 
562 	memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
563 
564 	if (softc->restart) {
565 		softc->restart = 0;
566 		if (path->device->flags & CAM_DEV_UNCONFIGURED)
567 			softc->action = PROBE_RESET;
568 		else
569 			softc->action = PROBE_IDENTIFY;
570 
571 	}
572 
573 	/* Here is the place where the identify fun begins */
574 	switch (softc->action) {
575 	case PROBE_RESET:
576 		/* FALLTHROUGH */
577 	case PROBE_IDENTIFY:
578 		xpt_path_inq(&start_ccb->cpi, periph->path);
579 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
580 		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
581 		xpt_action(start_ccb);
582 		if (cts->ios.power_mode != power_off) {
583 			init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
584 			cts->ios.power_mode = power_off;
585 			cts->ios_valid = MMC_PM;
586 			xpt_action(start_ccb);
587 			mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
588 		}
589 		/* mmc_power_up */
590 		/* Get the host OCR */
591 		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
592 		xpt_action(start_ccb);
593 
594 		uint32_t hv = mmc_highest_voltage(cts->host_ocr);
595 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
596 		cts->ios.vdd = hv;
597 		cts->ios.bus_mode = opendrain;
598 		cts->ios.chip_select = cs_dontcare;
599 		cts->ios.power_mode = power_up;
600 		cts->ios.bus_width = bus_width_1;
601 		cts->ios.clock = 0;
602 		cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
603 			MMC_CS | MMC_BW | MMC_CLK;
604 		xpt_action(start_ccb);
605 		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
606 
607 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
608 		cts->ios.power_mode = power_on;
609 		cts->ios.clock = CARD_ID_FREQUENCY;
610 		cts->ios.timing = bus_timing_normal;
611 		cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
612 		xpt_action(start_ccb);
613 		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
614 		/* End for mmc_power_on */
615 
616 		/* Begin mmc_idle_cards() */
617 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
618 		cts->ios.chip_select = cs_high;
619 		cts->ios_valid = MMC_CS;
620 		xpt_action(start_ccb);
621 		mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
622 
623 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
624 		init_standard_ccb(start_ccb, XPT_MMC_IO);
625 		mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
626 		mmcio->cmd.arg = 0;
627 		mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
628 		mmcio->cmd.data = NULL;
629 		mmcio->stop.opcode = 0;
630 
631 		/* XXX Reset I/O portion as well */
632 		break;
633 	case PROBE_SDIO_RESET:
634 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
635 			  ("Start with PROBE_SDIO_RESET\n"));
636 		uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
637 			| SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
638 		cam_fill_mmcio(&start_ccb->mmcio,
639 			       /*retries*/ 0,
640 			       /*cbfcnp*/ mmcprobe_done,
641 			       /*flags*/ CAM_DIR_NONE,
642 			       /*mmc_opcode*/ SD_IO_RW_DIRECT,
643 			       /*mmc_arg*/ mmc_arg,
644 			       /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
645 			       /*mmc_data*/ NULL,
646 			       /*timeout*/ 1000);
647 		break;
648 	case PROBE_SEND_IF_COND:
649 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
650 			  ("Start with PROBE_SEND_IF_COND\n"));
651 		init_standard_ccb(start_ccb, XPT_MMC_IO);
652 		mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
653 		mmcio->cmd.arg = (1 << 8) + 0xAA;
654 		mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
655 		mmcio->stop.opcode = 0;
656 		break;
657 
658 	case PROBE_SDIO_INIT:
659 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
660 			  ("Start with PROBE_SDIO_INIT\n"));
661 		init_standard_ccb(start_ccb, XPT_MMC_IO);
662 		mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
663 		mmcio->cmd.arg = mmcp->io_ocr;
664 		mmcio->cmd.flags = MMC_RSP_R4;
665 		mmcio->stop.opcode = 0;
666 		break;
667 
668 	case PROBE_MMC_INIT:
669 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
670 			  ("Start with PROBE_MMC_INIT\n"));
671 		init_standard_ccb(start_ccb, XPT_MMC_IO);
672 		mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
673 		mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
674 		mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
675 		mmcio->stop.opcode = 0;
676 		break;
677 
678 	case PROBE_SEND_APP_OP_COND:
679 		init_standard_ccb(start_ccb, XPT_MMC_IO);
680 		if (softc->flags & PROBE_FLAG_ACMD_SENT) {
681 			mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
682 			/*
683 			 * We set CCS bit because we do support SDHC cards.
684 			 * XXX: Don't set CCS if no response to CMD8.
685 			 */
686 			uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
687 			if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
688 				cmd_arg |= MMC_OCR_S18R;
689 			mmcio->cmd.arg = cmd_arg;
690 			mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
691 			softc->acmd41_count++;
692 		} else {
693 			mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
694 			mmcio->cmd.arg = 0; /* rca << 16 */
695 			mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
696 		}
697 		mmcio->stop.opcode = 0;
698 		break;
699 
700 	case PROBE_GET_CID: /* XXX move to mmc_da */
701 		init_standard_ccb(start_ccb, XPT_MMC_IO);
702 		mmcio->cmd.opcode = MMC_ALL_SEND_CID;
703 		mmcio->cmd.arg = 0;
704 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
705 		mmcio->stop.opcode = 0;
706 		break;
707 	case PROBE_SEND_RELATIVE_ADDR:
708 		init_standard_ccb(start_ccb, XPT_MMC_IO);
709 		mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
710 		mmcio->cmd.arg = 0;
711 		mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
712 		mmcio->stop.opcode = 0;
713 		break;
714 	case PROBE_MMC_SET_RELATIVE_ADDR:
715 		init_standard_ccb(start_ccb, XPT_MMC_IO);
716 		mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
717 		mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
718 		mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
719 		mmcio->stop.opcode = 0;
720 		break;
721 	case PROBE_SELECT_CARD:
722 		init_standard_ccb(start_ccb, XPT_MMC_IO);
723 		mmcio->cmd.opcode = MMC_SELECT_CARD;
724 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
725 		mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
726 		mmcio->stop.opcode = 0;
727 		break;
728 	case PROBE_GET_CSD: /* XXX move to mmc_da */
729 		init_standard_ccb(start_ccb, XPT_MMC_IO);
730 		mmcio->cmd.opcode = MMC_SEND_CSD;
731 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
732 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
733 		mmcio->stop.opcode = 0;
734 		break;
735 	case PROBE_DONE:
736 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
737 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
738 		cts->ios.bus_mode = pushpull;
739 		cts->ios_valid = MMC_BM;
740 		xpt_action(start_ccb);
741 		return;
742 		/* NOTREACHED */
743 		break;
744 	case PROBE_INVALID:
745 		break;
746 	default:
747 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
748 		panic("default: case in mmc_probe_start()");
749 	}
750 
751 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
752 	xpt_action(start_ccb);
753 }
754 
755 static void mmcprobe_cleanup(struct cam_periph *periph)
756 {
757 	free(periph->softc, M_CAMXPT);
758 }
759 
760 static void
761 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
762 {
763 	mmcprobe_softc *softc;
764 	struct cam_path *path;
765 
766 	int err;
767 	struct ccb_mmcio *mmcio;
768 	u_int32_t  priority;
769 
770 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
771 	softc = (mmcprobe_softc *)periph->softc;
772 	path = done_ccb->ccb_h.path;
773 	priority = done_ccb->ccb_h.pinfo.priority;
774 
775 	switch (softc->action) {
776 	case PROBE_RESET:
777 		/* FALLTHROUGH */
778 	case PROBE_IDENTIFY:
779 	{
780 		printf("Starting completion of PROBE_RESET\n");
781 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
782 		mmcio = &done_ccb->mmcio;
783 		err = mmcio->cmd.error;
784 
785 		if (err != MMC_ERR_NONE) {
786 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
787 				  ("GO_IDLE_STATE failed with error %d\n",
788 				   err));
789 
790 			/* There was a device there, but now it's gone... */
791 			if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
792 				xpt_async(AC_LOST_DEVICE, path, NULL);
793 			}
794 			PROBE_SET_ACTION(softc, PROBE_INVALID);
795 			break;
796 		}
797 		path->device->protocol = PROTO_MMCSD;
798 		PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
799 		break;
800 	}
801 	case PROBE_SEND_IF_COND:
802 	{
803 		mmcio = &done_ccb->mmcio;
804 		err = mmcio->cmd.error;
805 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
806 
807 		if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
808 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
809 				  ("IF_COND: error %d, pattern %08x\n",
810 				   err, mmcio->cmd.resp[0]));
811 		} else {
812 			mmcp->card_features |= CARD_FEATURE_SD20;
813 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
814 				  ("SD 2.0 interface conditions: OK\n"));
815 
816 		}
817                 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
818 		break;
819 	}
820         case PROBE_SDIO_RESET:
821         {
822 		mmcio = &done_ccb->mmcio;
823 		err = mmcio->cmd.error;
824 
825                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
826                           ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
827                            err, mmcio->cmd.resp[0]));
828                 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
829                 break;
830         }
831         case PROBE_SDIO_INIT:
832         {
833 		mmcio = &done_ccb->mmcio;
834 		err = mmcio->cmd.error;
835                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
836 
837                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
838                           ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
839                            err, mmcio->cmd.resp[0],
840                            mmcio->cmd.resp[1],
841                            mmcio->cmd.resp[2],
842                            mmcio->cmd.resp[3]));
843 
844                 /*
845                  * Error here means that this card is not SDIO,
846                  * so proceed with memory init as if nothing has happened
847                  */
848 		if (err != MMC_ERR_NONE) {
849                         PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
850                         break;
851 		}
852                 mmcp->card_features |= CARD_FEATURE_SDIO;
853                 uint32_t ioifcond = mmcio->cmd.resp[0];
854                 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
855 
856                 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
857                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
858                           ("SDIO card: %d functions\n", mmcp->sdio_func_count));
859                 if (io_ocr == 0) {
860                     CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
861                               ("SDIO OCR invalid?!\n"));
862                     break; /* Retry */
863                 }
864 
865                 if (io_ocr != 0 && mmcp->io_ocr == 0) {
866                         mmcp->io_ocr = io_ocr;
867                         break; /* Retry, this time with non-0 OCR */
868                 }
869                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
870                           ("SDIO OCR: %08x\n", mmcp->io_ocr));
871 
872                 if (ioifcond & R4_IO_MEM_PRESENT) {
873                         /* Combo card -- proceed to memory initialization */
874                         PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
875                 } else {
876                         /* No memory portion -- get RCA and select card */
877                         PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
878                 }
879                 break;
880         }
881         case PROBE_MMC_INIT:
882         {
883 		mmcio = &done_ccb->mmcio;
884 		err = mmcio->cmd.error;
885                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
886 
887 		if (err != MMC_ERR_NONE) {
888 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
889 				  ("MMC_INIT: error %d, resp %08x\n",
890 				   err, mmcio->cmd.resp[0]));
891 			PROBE_SET_ACTION(softc, PROBE_INVALID);
892                         break;
893                 }
894                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
895                           ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
896 
897                 if (mmcp->card_ocr == 0) {
898                         /* We haven't sent the OCR to the card yet -- do it */
899                         mmcp->card_ocr = mmcio->cmd.resp[0];
900                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
901                                   ("-> sending OCR to card\n"));
902                         break;
903                 }
904 
905                 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
906                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
907                                   ("Card is still powering up\n"));
908                         break;
909                 }
910 
911                 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
912                 PROBE_SET_ACTION(softc, PROBE_GET_CID);
913                 break;
914         }
915 	case PROBE_SEND_APP_OP_COND:
916 	{
917 		mmcio = &done_ccb->mmcio;
918 		err = mmcio->cmd.error;
919 
920 		if (err != MMC_ERR_NONE) {
921 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
922 				  ("APP_OP_COND: error %d, resp %08x\n",
923 				   err, mmcio->cmd.resp[0]));
924 			PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
925                         break;
926                 }
927 
928                 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
929                         /* Don't change the state */
930                         softc->flags |= PROBE_FLAG_ACMD_SENT;
931                         break;
932                 }
933 
934                 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
935                 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
936                     (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
937                         struct mmc_params *mmcp = &path->device->mmc_ident_data;
938                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
939                                   ("Card OCR: %08x\n",  mmcio->cmd.resp[0]));
940                         if (mmcp->card_ocr == 0) {
941                                 mmcp->card_ocr = mmcio->cmd.resp[0];
942                                 /* Now when we know OCR that we want -- send it to card */
943                                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
944                                           ("-> sending OCR to card\n"));
945                         } else {
946                                 /* We already know the OCR and despite of that we
947                                  * are processing the answer to ACMD41 -> move on
948                                  */
949                                 PROBE_SET_ACTION(softc, PROBE_GET_CID);
950                         }
951                         /* Getting an answer to ACMD41 means the card has memory */
952                         mmcp->card_features |= CARD_FEATURE_MEMORY;
953 
954                         /* Standard capacity vs High Capacity memory card */
955                         if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
956                                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
957                                           ("Card is SDHC\n"));
958                                 mmcp->card_features |= CARD_FEATURE_SDHC;
959                         }
960 
961 			/* Whether the card supports 1.8V signaling */
962 			if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
963 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
964 					  ("Card supports 1.8V signaling\n"));
965 				mmcp->card_features |= CARD_FEATURE_18V;
966 			}
967 		} else {
968 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
969 				  ("Card not ready: %08x\n",  mmcio->cmd.resp[0]));
970 			/* Send CMD55+ACMD41 once again  */
971 			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
972 		}
973 
974                 break;
975 	}
976         case PROBE_GET_CID: /* XXX move to mmc_da */
977         {
978 		mmcio = &done_ccb->mmcio;
979 		err = mmcio->cmd.error;
980 
981 		if (err != MMC_ERR_NONE) {
982 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
983 				  ("PROBE_GET_CID: error %d\n", err));
984 			PROBE_SET_ACTION(softc, PROBE_INVALID);
985                         break;
986                 }
987 
988                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
989                 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
990                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
991                           ("CID %08x%08x%08x%08x\n",
992                            mmcp->card_cid[0],
993                            mmcp->card_cid[1],
994                            mmcp->card_cid[2],
995                            mmcp->card_cid[3]));
996 		if (mmcp->card_features & CARD_FEATURE_MMC)
997 			PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
998 		else
999 			PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1000                 break;
1001         }
1002         case PROBE_SEND_RELATIVE_ADDR: {
1003 		mmcio = &done_ccb->mmcio;
1004 		err = mmcio->cmd.error;
1005                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1006                 uint16_t rca = mmcio->cmd.resp[0] >> 16;
1007                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1008                           ("Card published RCA: %u\n", rca));
1009                 path->device->mmc_ident_data.card_rca = rca;
1010 		if (err != MMC_ERR_NONE) {
1011 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1012 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1013 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1014                         break;
1015                 }
1016 
1017                 /* If memory is present, get CSD, otherwise select card */
1018                 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1019                         PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1020                 else
1021                         PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1022 		break;
1023         }
1024 	case PROBE_MMC_SET_RELATIVE_ADDR:
1025 		mmcio = &done_ccb->mmcio;
1026 		err = mmcio->cmd.error;
1027 		if (err != MMC_ERR_NONE) {
1028 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1029 			    ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1030 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1031 			break;
1032 		}
1033 		path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1034 		PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1035 		break;
1036         case PROBE_GET_CSD: {
1037 		mmcio = &done_ccb->mmcio;
1038 		err = mmcio->cmd.error;
1039 
1040 		if (err != MMC_ERR_NONE) {
1041 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1042 				  ("PROBE_GET_CSD: error %d\n", err));
1043 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1044                         break;
1045                 }
1046 
1047                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1048                 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1049                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1050                           ("CSD %08x%08x%08x%08x\n",
1051                            mmcp->card_csd[0],
1052                            mmcp->card_csd[1],
1053                            mmcp->card_csd[2],
1054                            mmcp->card_csd[3]));
1055                 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1056                 break;
1057         }
1058         case PROBE_SELECT_CARD: {
1059 		mmcio = &done_ccb->mmcio;
1060 		err = mmcio->cmd.error;
1061 		if (err != MMC_ERR_NONE) {
1062 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1063 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1064 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1065                         break;
1066                 }
1067 
1068 		PROBE_SET_ACTION(softc, PROBE_DONE);
1069                 break;
1070         }
1071 	default:
1072 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1073 			  ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1074 		panic("default: case in mmc_probe_done()");
1075 	}
1076 
1077         if (softc->action == PROBE_INVALID &&
1078             (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1079                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1080 			  ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1081                 //xpt_async(AC_LOST_DEVICE, path, NULL);
1082         }
1083 
1084 	xpt_release_ccb(done_ccb);
1085         if (softc->action != PROBE_INVALID)
1086                 xpt_schedule(periph, priority);
1087 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1088 	int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1089         printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1090 
1091 	if (softc->action == PROBE_DONE) {
1092                 /* Notify the system that the device is found! */
1093 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1094 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1095 			xpt_acquire_device(path->device);
1096 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1097 			xpt_action(done_ccb);
1098 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1099 		}
1100 	}
1101         if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1102                 cam_periph_invalidate(periph);
1103                 cam_periph_release_locked(periph);
1104         }
1105 }
1106