xref: /freebsd/sys/cam/mmc/mmc_xpt.c (revision 78adacd4eab39a3508bd8c65f0aba94fc6b907ce)
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 #define PROBE_FLAG_HOST_CAN_DO_18V   0x2 /* Host can do 1.8V signaling */
171 	uint8_t         acmd41_count; /* how many times ACMD41 has been issued */
172 	struct cam_periph *periph;
173 } mmcprobe_softc;
174 
175 /* XPort functions -- an interface to CAM at periph side */
176 
177 static struct cam_ed *
178 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
179 {
180 	struct cam_ed *device;
181 
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 	/*
203 	 * We only need to handle events for real devices.
204 	 */
205 	if (target->target_id == CAM_TARGET_WILDCARD
206             || device->lun_id == CAM_LUN_WILDCARD)
207 		return;
208 
209 	if (async_code == AC_LOST_DEVICE &&
210 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
211 		device->flags |= CAM_DEV_UNCONFIGURED;
212 		xpt_release_device(device);
213 	}
214 }
215 
216 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
217 static void
218 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
219 	     cam_flags flags, union ccb *request_ccb)
220 {
221 	struct ccb_pathinq cpi;
222 	cam_status status;
223 	struct cam_periph *old_periph;
224 	int lock;
225 
226 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
227 
228 	xpt_path_inq(&cpi, path);
229 
230 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
231 		if (request_ccb != NULL) {
232 			request_ccb->ccb_h.status = cpi.ccb_h.status;
233 			xpt_done(request_ccb);
234 		}
235 		return;
236 	}
237 
238 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
239 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
240 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
241 		xpt_done(request_ccb);
242 		return;
243 	}
244 
245 	lock = (xpt_path_owned(path) == 0);
246 	if (lock)
247 		xpt_path_lock(path);
248 
249 	if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
250 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
251 //			mmcprobe_softc *softc;
252 //			softc = (mmcprobe_softc *)old_periph->softc;
253 //                      Not sure if we need request ccb queue for mmc
254 //			TAILQ_INSERT_TAIL(&softc->request_ccbs,
255 //				&request_ccb->ccb_h, periph_links.tqe);
256 //			softc->restart = 1;
257                         CAM_DEBUG(path, CAM_DEBUG_INFO,
258                                   ("Got scan request, but mmcprobe already exists\n"));
259 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
260                         xpt_done(request_ccb);
261 		} else {
262 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
263 			xpt_done(request_ccb);
264 		}
265 	} else {
266 		CAM_DEBUG(path, CAM_DEBUG_INFO,
267 		    (" Set up the mmcprobe device...\n"));
268 
269                 status = cam_periph_alloc(mmcprobe_register, NULL,
270 					  mmcprobe_cleanup,
271 					  mmcprobe_start,
272 					  "mmcprobe",
273 					  CAM_PERIPH_BIO,
274 					  path, NULL, 0,
275 					  request_ccb);
276                 if (status != CAM_REQ_CMP) {
277 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
278                                   "returned an error, can't continue probe\n");
279 		}
280 		request_ccb->ccb_h.status = status;
281 		xpt_done(request_ccb);
282 	}
283 
284 	if (lock)
285 		xpt_path_unlock(path);
286 }
287 
288 static void
289 mmc_action(union ccb *start_ccb)
290 {
291 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
292 		  ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
293 		   xpt_action_name(start_ccb->ccb_h.func_code)));
294 	switch (start_ccb->ccb_h.func_code) {
295 	case XPT_SCAN_BUS:
296                 /* FALLTHROUGH */
297 	case XPT_SCAN_TGT:
298                 /* FALLTHROUGH */
299 	case XPT_SCAN_LUN:
300 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
301 			  ("XPT_SCAN_{BUS,TGT,LUN}\n"));
302 		mmc_scan_lun(start_ccb->ccb_h.path->periph,
303 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
304 			     start_ccb);
305 		break;
306 
307 	case XPT_DEV_ADVINFO:
308 	{
309 		mmc_dev_advinfo(start_ccb);
310 		break;
311 	}
312 
313 	default:
314 		xpt_action_default(start_ccb);
315 		break;
316 	}
317 }
318 
319 static void
320 mmc_dev_advinfo(union ccb *start_ccb)
321 {
322 	struct cam_ed *device;
323 	struct ccb_dev_advinfo *cdai;
324 	off_t amt;
325 
326 	xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
327 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
328 	device = start_ccb->ccb_h.path->device;
329 	cdai = &start_ccb->cdai;
330 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
331 		  ("%s: request %x\n", __func__, cdai->buftype));
332 
333         /* We don't support writing any data */
334         if (cdai->flags & CDAI_FLAG_STORE)
335                 panic("Attempt to store data?!");
336 
337 	switch(cdai->buftype) {
338 	case CDAI_TYPE_SCSI_DEVID:
339 		cdai->provsiz = device->device_id_len;
340 		if (device->device_id_len == 0)
341 			break;
342 		amt = MIN(cdai->provsiz, cdai->bufsiz);
343 		memcpy(cdai->buf, device->device_id, amt);
344 		break;
345 	case CDAI_TYPE_SERIAL_NUM:
346 		cdai->provsiz = device->serial_num_len;
347 		if (device->serial_num_len == 0)
348 			break;
349 		amt = MIN(cdai->provsiz, cdai->bufsiz);
350 		memcpy(cdai->buf, device->serial_num, amt);
351 		break;
352         case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
353                 cdai->provsiz = 0;
354                 break;
355 	case CDAI_TYPE_MMC_PARAMS:
356 		cdai->provsiz = sizeof(struct mmc_params);
357 		amt = MIN(cdai->provsiz, cdai->bufsiz);
358 		memcpy(cdai->buf, &device->mmc_ident_data, amt);
359 		break;
360 	default:
361                 panic("Unknown buftype");
362 		return;
363 	}
364 	start_ccb->ccb_h.status = CAM_REQ_CMP;
365 }
366 
367 static void
368 mmc_announce_periph(struct cam_periph *periph)
369 {
370 	struct	ccb_pathinq cpi;
371 	struct	ccb_trans_settings cts;
372 	struct	cam_path *path = periph->path;
373 
374 	cam_periph_assert(periph, MA_OWNED);
375 
376 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmc_announce_periph"));
377 
378 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
379 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
380 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
381 	xpt_action((union ccb*)&cts);
382 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
383 		return;
384 	xpt_path_inq(&cpi, periph->path);
385 	CAM_DEBUG(path, CAM_DEBUG_INFO,
386 	    ("XPT info: CLK %04d, ...\n", cts.proto_specific.mmc.ios.clock));
387 }
388 
389 void
390 mmccam_start_discovery(struct cam_sim *sim)
391 {
392 	union ccb *ccb;
393 	uint32_t pathid;
394 
395 	KASSERT(sim->sim_dev != NULL, ("mmccam_start_discovery(%s): sim_dev is not initialized,"
396 	    " has cam_sim_alloc_dev() been used?", cam_sim_name(sim)));
397 	pathid = cam_sim_path(sim);
398 	ccb = xpt_alloc_ccb();
399 
400 	/*
401 	 * We create a rescan request for BUS:0:0, since the card
402 	 * will be at lun 0.
403 	 */
404 	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
405 		/* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
406 		xpt_free_ccb(ccb);
407 		return;
408 	}
409 	xpt_rescan(ccb);
410 }
411 
412 /* This func is called per attached device :-( */
413 static void
414 mmc_print_ident(struct mmc_params *ident_data, struct sbuf *sb)
415 {
416 	bool space = false;
417 
418 	sbuf_printf(sb, "Relative addr: %08x\n", ident_data->card_rca);
419 	sbuf_printf(sb, "Card features: <");
420 	if (ident_data->card_features & CARD_FEATURE_MMC) {
421 		sbuf_printf(sb, "MMC");
422 		space = true;
423 	}
424 	if (ident_data->card_features & CARD_FEATURE_MEMORY) {
425 		sbuf_printf(sb, "%sMemory", space ? " " : "");
426 		space = true;
427 	}
428 	if (ident_data->card_features & CARD_FEATURE_SDHC) {
429 		sbuf_printf(sb, "%sHigh-Capacity", space ? " " : "");
430 		space = true;
431 	}
432 	if (ident_data->card_features & CARD_FEATURE_SD20) {
433 		sbuf_printf(sb, "%sSD2.0-Conditions", space ? " " : "");
434 		space = true;
435 	}
436 	if (ident_data->card_features & CARD_FEATURE_SDIO) {
437 		sbuf_printf(sb, "%sSDIO", space ? " " : "");
438 		space = true;
439 	}
440 	if (ident_data->card_features & CARD_FEATURE_18V) {
441 		sbuf_printf(sb, "%s1.8-Signaling", space ? " " : "");
442 	}
443 	sbuf_printf(sb, ">\n");
444 
445 	if (ident_data->card_features & CARD_FEATURE_MEMORY)
446 		sbuf_printf(sb, "Card memory OCR: %08x\n",
447 		    ident_data->card_ocr);
448 
449 	if (ident_data->card_features & CARD_FEATURE_SDIO) {
450 		sbuf_printf(sb, "Card IO OCR: %08x\n", ident_data->io_ocr);
451 		sbuf_printf(sb, "Number of functions: %u\n",
452 		    ident_data->sdio_func_count);
453 	}
454 
455 	sbuf_finish(sb);
456 	printf("%s", sbuf_data(sb));
457 	sbuf_clear(sb);
458 }
459 
460 static void
461 mmc_proto_announce(struct cam_ed *device)
462 {
463 	struct sbuf	sb;
464 	char		buffer[256];
465 
466 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
467 	mmc_print_ident(&device->mmc_ident_data, &sb);
468 	sbuf_finish(&sb);
469 	sbuf_putbuf(&sb);
470 }
471 
472 static void
473 mmc_proto_denounce(struct cam_ed *device)
474 {
475 
476 	mmc_proto_announce(device);
477 }
478 
479 static void
480 mmc_proto_debug_out(union ccb *ccb)
481 {
482 	if (ccb->ccb_h.func_code != XPT_MMC_IO)
483 		return;
484 
485 	CAM_DEBUG(ccb->ccb_h.path,
486 	    CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
487 }
488 
489 static periph_init_t probe_periph_init;
490 
491 static struct periph_driver probe_driver =
492 {
493 	probe_periph_init, "mmcprobe",
494 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
495 	CAM_PERIPH_DRV_EARLY
496 };
497 
498 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
499 
500 #define	CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
501 
502 static void
503 probe_periph_init(void)
504 {
505 }
506 
507 static cam_status
508 mmcprobe_register(struct cam_periph *periph, void *arg)
509 {
510 	mmcprobe_softc *softc;
511 	union ccb *request_ccb;	/* CCB representing the probe request */
512 	int status;
513 
514 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
515 
516 	request_ccb = (union ccb *)arg;
517 	if (request_ccb == NULL) {
518 		printf("mmcprobe_register: no probe CCB, "
519 		       "can't register device\n");
520 		return(CAM_REQ_CMP_ERR);
521 	}
522 
523 	softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
524 
525 	if (softc == NULL) {
526 		printf("proberegister: Unable to probe new device. "
527 		       "Unable to allocate softc\n");
528 		return(CAM_REQ_CMP_ERR);
529 	}
530 
531 	softc->flags = 0;
532 	softc->acmd41_count = 0;
533 	periph->softc = softc;
534 	softc->periph = periph;
535 	softc->action = PROBE_INVALID;
536         softc->restart = 0;
537 	status = cam_periph_acquire(periph);
538 
539         memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
540 	if (status != 0) {
541 		printf("proberegister: cam_periph_acquire failed (status=%d)\n",
542 			status);
543 		return (CAM_REQ_CMP_ERR);
544 	}
545 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
546 
547 	if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
548 		PROBE_SET_ACTION(softc, PROBE_RESET);
549 	else
550 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
551 
552 	/* This will kick the ball */
553 	xpt_schedule(periph, CAM_PRIORITY_XPT);
554 
555 	return(CAM_REQ_CMP);
556 }
557 
558 static int
559 mmc_highest_voltage(uint32_t ocr)
560 {
561 	int i;
562 
563 	for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
564 	    i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
565 		if (ocr & (1 << i))
566 			return (i);
567 	return (-1);
568 }
569 
570 static inline void
571 init_standard_ccb(union ccb *ccb, uint32_t cmd)
572 {
573 	ccb->ccb_h.func_code = cmd;
574 	ccb->ccb_h.flags = CAM_DIR_OUT;
575 	ccb->ccb_h.retry_count = 0;
576 	ccb->ccb_h.timeout = 15 * 1000;
577 	ccb->ccb_h.cbfcnp = mmcprobe_done;
578 }
579 
580 static void
581 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
582 {
583 	mmcprobe_softc *softc;
584 	struct cam_path *path;
585 	struct ccb_mmcio *mmcio;
586 	struct mtx *p_mtx = cam_periph_mtx(periph);
587 	struct ccb_trans_settings_mmc *cts;
588 
589 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
590 	softc = (mmcprobe_softc *)periph->softc;
591 	path = start_ccb->ccb_h.path;
592 	mmcio = &start_ccb->mmcio;
593 	cts = &start_ccb->cts.proto_specific.mmc;
594 	struct mmc_params *mmcp = &path->device->mmc_ident_data;
595 
596 	memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
597 
598 	if (softc->restart) {
599 		softc->restart = 0;
600 		if (path->device->flags & CAM_DEV_UNCONFIGURED)
601 			softc->action = PROBE_RESET;
602 		else
603 			softc->action = PROBE_IDENTIFY;
604 	}
605 
606 	/* Here is the place where the identify fun begins */
607 	switch (softc->action) {
608 	case PROBE_RESET:
609 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
610 		/* FALLTHROUGH */
611 	case PROBE_IDENTIFY:
612 		xpt_path_inq(&start_ccb->cpi, periph->path);
613 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_IDENTIFY\n"));
614 		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
615 		xpt_action(start_ccb);
616 		if (cts->ios.power_mode != power_off) {
617 			init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
618 			cts->ios.power_mode = power_off;
619 			cts->ios_valid = MMC_PM;
620 			xpt_action(start_ccb);
621 			mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
622 		}
623 		/* mmc_power_up */
624 		/* Get the host OCR */
625 		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
626 		xpt_action(start_ccb);
627 
628 		uint32_t host_caps = cts->host_caps;
629 		if (host_caps & MMC_CAP_SIGNALING_180)
630 			softc->flags |= PROBE_FLAG_HOST_CAN_DO_18V;
631 		uint32_t hv = mmc_highest_voltage(cts->host_ocr);
632 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
633 		cts->ios.vdd = hv;
634 		cts->ios.bus_mode = opendrain;
635 		cts->ios.chip_select = cs_dontcare;
636 		cts->ios.power_mode = power_up;
637 		cts->ios.bus_width = bus_width_1;
638 		cts->ios.clock = 0;
639 		cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
640 			MMC_CS | MMC_BW | MMC_CLK;
641 		xpt_action(start_ccb);
642 		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
643 
644 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
645 		cts->ios.power_mode = power_on;
646 		cts->ios.clock = CARD_ID_FREQUENCY;
647 		cts->ios.timing = bus_timing_normal;
648 		cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
649 		xpt_action(start_ccb);
650 		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
651 		/* End for mmc_power_on */
652 
653 		/* Begin mmc_idle_cards() */
654 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
655 		cts->ios.chip_select = cs_high;
656 		cts->ios_valid = MMC_CS;
657 		xpt_action(start_ccb);
658 		mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
659 
660 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
661 		init_standard_ccb(start_ccb, XPT_MMC_IO);
662 		mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
663 		mmcio->cmd.arg = 0;
664 		mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
665 		mmcio->cmd.data = NULL;
666 		mmcio->stop.opcode = 0;
667 
668 		/* XXX Reset I/O portion as well */
669 		break;
670 	case PROBE_SDIO_RESET:
671 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
672 			  ("Start with PROBE_SDIO_RESET\n"));
673 		uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
674 			| SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
675 		cam_fill_mmcio(&start_ccb->mmcio,
676 			       /*retries*/ 0,
677 			       /*cbfcnp*/ mmcprobe_done,
678 			       /*flags*/ CAM_DIR_NONE,
679 			       /*mmc_opcode*/ SD_IO_RW_DIRECT,
680 			       /*mmc_arg*/ mmc_arg,
681 			       /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
682 			       /*mmc_data*/ NULL,
683 			       /*timeout*/ 1000);
684 		break;
685 	case PROBE_SEND_IF_COND:
686 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
687 			  ("Start with PROBE_SEND_IF_COND\n"));
688 		init_standard_ccb(start_ccb, XPT_MMC_IO);
689 		mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
690 		mmcio->cmd.arg = (1 << 8) + 0xAA;
691 		mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
692 		mmcio->stop.opcode = 0;
693 		break;
694 
695 	case PROBE_SDIO_INIT:
696 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
697 			  ("Start with PROBE_SDIO_INIT\n"));
698 		init_standard_ccb(start_ccb, XPT_MMC_IO);
699 		mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
700 		mmcio->cmd.arg = mmcp->io_ocr;
701 		mmcio->cmd.flags = MMC_RSP_R4;
702 		mmcio->stop.opcode = 0;
703 		break;
704 
705 	case PROBE_MMC_INIT:
706 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
707 			  ("Start with PROBE_MMC_INIT\n"));
708 		init_standard_ccb(start_ccb, XPT_MMC_IO);
709 		mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
710 		mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
711 		mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
712 		mmcio->stop.opcode = 0;
713 		break;
714 
715 	case PROBE_SEND_APP_OP_COND:
716 		init_standard_ccb(start_ccb, XPT_MMC_IO);
717 		if (softc->flags & PROBE_FLAG_ACMD_SENT) {
718 			mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
719 			/*
720 			 * We set CCS bit because we do support SDHC cards.
721 			 * XXX: Don't set CCS if no response to CMD8.
722 			 */
723 			uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
724 			if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
725 				cmd_arg |= MMC_OCR_S18R;
726 			mmcio->cmd.arg = cmd_arg;
727 			mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
728 			softc->acmd41_count++;
729 		} else {
730 			mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
731 			mmcio->cmd.arg = 0; /* rca << 16 */
732 			mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
733 		}
734 		mmcio->stop.opcode = 0;
735 		break;
736 
737 	case PROBE_GET_CID: /* XXX move to mmc_da */
738 		init_standard_ccb(start_ccb, XPT_MMC_IO);
739 		mmcio->cmd.opcode = MMC_ALL_SEND_CID;
740 		mmcio->cmd.arg = 0;
741 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
742 		mmcio->stop.opcode = 0;
743 		break;
744 	case PROBE_SEND_RELATIVE_ADDR:
745 		init_standard_ccb(start_ccb, XPT_MMC_IO);
746 		mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
747 		mmcio->cmd.arg = 0;
748 		mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
749 		mmcio->stop.opcode = 0;
750 		break;
751 	case PROBE_MMC_SET_RELATIVE_ADDR:
752 		init_standard_ccb(start_ccb, XPT_MMC_IO);
753 		mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
754 		mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
755 		mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
756 		mmcio->stop.opcode = 0;
757 		break;
758 	case PROBE_SELECT_CARD:
759 		init_standard_ccb(start_ccb, XPT_MMC_IO);
760 		mmcio->cmd.opcode = MMC_SELECT_CARD;
761 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
762 		mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
763 		mmcio->stop.opcode = 0;
764 		break;
765 	case PROBE_GET_CSD: /* XXX move to mmc_da */
766 		init_standard_ccb(start_ccb, XPT_MMC_IO);
767 		mmcio->cmd.opcode = MMC_SEND_CSD;
768 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
769 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
770 		mmcio->stop.opcode = 0;
771 		break;
772 	case PROBE_DONE:
773 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
774 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
775 		cts->ios.bus_mode = pushpull;
776 		cts->ios_valid = MMC_BM;
777 		xpt_action(start_ccb);
778 		return;
779 		/* NOTREACHED */
780 		break;
781 	case PROBE_INVALID:
782 		break;
783 	default:
784 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
785 		panic("default: case in mmc_probe_start()");
786 	}
787 
788 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
789 	xpt_action(start_ccb);
790 }
791 
792 static void mmcprobe_cleanup(struct cam_periph *periph)
793 {
794 	free(periph->softc, M_CAMXPT);
795 }
796 
797 static void
798 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
799 {
800 	mmcprobe_softc *softc;
801 	struct cam_path *path;
802 
803 	int err;
804 	struct ccb_mmcio *mmcio;
805 	u_int32_t  priority;
806 
807 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mmcprobe_done\n"));
808 	softc = (mmcprobe_softc *)periph->softc;
809 	path = done_ccb->ccb_h.path;
810 	priority = done_ccb->ccb_h.pinfo.priority;
811 
812 	switch (softc->action) {
813 	case PROBE_RESET:
814 		/* FALLTHROUGH */
815 	case PROBE_IDENTIFY:
816 	{
817 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
818 		mmcio = &done_ccb->mmcio;
819 		err = mmcio->cmd.error;
820 
821 		if (err != MMC_ERR_NONE) {
822 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
823 				  ("GO_IDLE_STATE failed with error %d\n",
824 				   err));
825 
826 			/* There was a device there, but now it's gone... */
827 			if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
828 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
829 				  ("Device lost!\n"));
830 
831 				xpt_async(AC_LOST_DEVICE, path, NULL);
832 			}
833 			PROBE_SET_ACTION(softc, PROBE_INVALID);
834 			break;
835 		}
836 		path->device->protocol = PROTO_MMCSD;
837 		PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
838 		break;
839 	}
840 	case PROBE_SEND_IF_COND:
841 	{
842 		mmcio = &done_ccb->mmcio;
843 		err = mmcio->cmd.error;
844 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
845 
846 		if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
847 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
848 				  ("IF_COND: error %d, pattern %08x\n",
849 				   err, mmcio->cmd.resp[0]));
850 		} else {
851 			mmcp->card_features |= CARD_FEATURE_SD20;
852 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
853 				  ("SD 2.0 interface conditions: OK\n"));
854 		}
855                 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
856 		break;
857 	}
858         case PROBE_SDIO_RESET:
859         {
860 		mmcio = &done_ccb->mmcio;
861 		err = mmcio->cmd.error;
862 
863                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
864                           ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
865                            err, mmcio->cmd.resp[0]));
866                 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
867                 break;
868         }
869         case PROBE_SDIO_INIT:
870         {
871 		mmcio = &done_ccb->mmcio;
872 		err = mmcio->cmd.error;
873                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
874 
875                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
876                           ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
877                            err, mmcio->cmd.resp[0],
878                            mmcio->cmd.resp[1],
879                            mmcio->cmd.resp[2],
880                            mmcio->cmd.resp[3]));
881 
882                 /*
883                  * Error here means that this card is not SDIO,
884                  * so proceed with memory init as if nothing has happened
885                  */
886 		if (err != MMC_ERR_NONE) {
887                         PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
888                         break;
889 		}
890                 mmcp->card_features |= CARD_FEATURE_SDIO;
891                 uint32_t ioifcond = mmcio->cmd.resp[0];
892                 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
893 
894                 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
895                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
896                           ("SDIO card: %d functions\n", mmcp->sdio_func_count));
897                 if (io_ocr == 0) {
898                     CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
899                               ("SDIO OCR invalid, retrying\n"));
900                     break; /* Retry */
901                 }
902 
903                 if (io_ocr != 0 && mmcp->io_ocr == 0) {
904                         mmcp->io_ocr = io_ocr;
905                         break; /* Retry, this time with non-0 OCR */
906                 }
907                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
908                           ("SDIO OCR: %08x\n", mmcp->io_ocr));
909 
910                 if (ioifcond & R4_IO_MEM_PRESENT) {
911                         /* Combo card -- proceed to memory initialization */
912                         PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
913                 } else {
914                         /* No memory portion -- get RCA and select card */
915                         PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
916                 }
917                 break;
918         }
919         case PROBE_MMC_INIT:
920         {
921 		mmcio = &done_ccb->mmcio;
922 		err = mmcio->cmd.error;
923                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
924 
925 		if (err != MMC_ERR_NONE) {
926 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
927 				  ("MMC_INIT: error %d, resp %08x\n",
928 				   err, mmcio->cmd.resp[0]));
929 			PROBE_SET_ACTION(softc, PROBE_INVALID);
930                         break;
931                 }
932                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
933                           ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
934 
935                 if (mmcp->card_ocr == 0) {
936                         /* We haven't sent the OCR to the card yet -- do it */
937                         mmcp->card_ocr = mmcio->cmd.resp[0];
938                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
939                                   ("-> sending OCR to card\n"));
940                         break;
941                 }
942 
943                 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
944                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
945                                   ("Card is still powering up\n"));
946                         break;
947                 }
948 
949                 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
950                 PROBE_SET_ACTION(softc, PROBE_GET_CID);
951                 break;
952         }
953 	case PROBE_SEND_APP_OP_COND:
954 	{
955 		mmcio = &done_ccb->mmcio;
956 		err = mmcio->cmd.error;
957 
958 		if (err != MMC_ERR_NONE) {
959 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
960 				  ("APP_OP_COND: error %d, resp %08x\n",
961 				   err, mmcio->cmd.resp[0]));
962 			PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
963                         break;
964                 }
965 
966                 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
967                         /* Don't change the state */
968                         softc->flags |= PROBE_FLAG_ACMD_SENT;
969                         break;
970                 }
971 
972                 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
973                 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
974                     (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
975                         struct mmc_params *mmcp = &path->device->mmc_ident_data;
976                         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
977                                   ("Card OCR: %08x\n",  mmcio->cmd.resp[0]));
978                         if (mmcp->card_ocr == 0) {
979                                 mmcp->card_ocr = mmcio->cmd.resp[0];
980                                 /* Now when we know OCR that we want -- send it to card */
981                                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
982                                           ("-> sending OCR to card\n"));
983                         } else {
984                                 /* We already know the OCR and despite of that we
985                                  * are processing the answer to ACMD41 -> move on
986                                  */
987                                 PROBE_SET_ACTION(softc, PROBE_GET_CID);
988                         }
989                         /* Getting an answer to ACMD41 means the card has memory */
990                         mmcp->card_features |= CARD_FEATURE_MEMORY;
991 
992                         /* Standard capacity vs High Capacity memory card */
993                         if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
994                                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
995                                           ("Card is SDHC\n"));
996                                 mmcp->card_features |= CARD_FEATURE_SDHC;
997                         }
998 
999 			/* Whether the card supports 1.8V signaling */
1000 			if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
1001 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1002 					  ("Card supports 1.8V signaling\n"));
1003 				mmcp->card_features |= CARD_FEATURE_18V;
1004 				if (softc->flags & PROBE_FLAG_HOST_CAN_DO_18V) {
1005 					CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1006 						  ("Host supports 1.8V signaling. Switch voltage!\n"));
1007 					done_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1008 					done_ccb->ccb_h.flags = CAM_DIR_NONE;
1009 					done_ccb->ccb_h.retry_count = 0;
1010 					done_ccb->ccb_h.timeout = 100;
1011 					done_ccb->ccb_h.cbfcnp = NULL;
1012 					done_ccb->cts.proto_specific.mmc.ios.vccq = vccq_180;
1013 					done_ccb->cts.proto_specific.mmc.ios_valid = MMC_VCCQ;
1014 					xpt_action(done_ccb);
1015 				}
1016 			}
1017 		} else {
1018 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1019 				  ("Card not ready: %08x\n",  mmcio->cmd.resp[0]));
1020 			/* Send CMD55+ACMD41 once again  */
1021 			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
1022 		}
1023 
1024                 break;
1025 	}
1026         case PROBE_GET_CID: /* XXX move to mmc_da */
1027         {
1028 		mmcio = &done_ccb->mmcio;
1029 		err = mmcio->cmd.error;
1030 
1031 		if (err != MMC_ERR_NONE) {
1032 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1033 				  ("PROBE_GET_CID: error %d\n", err));
1034 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1035                         break;
1036                 }
1037 
1038                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1039                 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1040                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1041                           ("CID %08x%08x%08x%08x\n",
1042                            mmcp->card_cid[0],
1043                            mmcp->card_cid[1],
1044                            mmcp->card_cid[2],
1045                            mmcp->card_cid[3]));
1046 		if (mmcp->card_features & CARD_FEATURE_MMC)
1047 			PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
1048 		else
1049 			PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1050                 break;
1051         }
1052         case PROBE_SEND_RELATIVE_ADDR: {
1053 		mmcio = &done_ccb->mmcio;
1054 		err = mmcio->cmd.error;
1055                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1056                 uint16_t rca = mmcio->cmd.resp[0] >> 16;
1057                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1058                           ("Card published RCA: %u\n", rca));
1059                 path->device->mmc_ident_data.card_rca = rca;
1060 		if (err != MMC_ERR_NONE) {
1061 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1062 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1063 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1064                         break;
1065                 }
1066 
1067                 /* If memory is present, get CSD, otherwise select card */
1068                 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1069                         PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1070                 else
1071                         PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1072 		break;
1073         }
1074 	case PROBE_MMC_SET_RELATIVE_ADDR:
1075 		mmcio = &done_ccb->mmcio;
1076 		err = mmcio->cmd.error;
1077 		if (err != MMC_ERR_NONE) {
1078 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1079 			    ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1080 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1081 			break;
1082 		}
1083 		path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1084 		PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1085 		break;
1086         case PROBE_GET_CSD: {
1087 		mmcio = &done_ccb->mmcio;
1088 		err = mmcio->cmd.error;
1089 
1090 		if (err != MMC_ERR_NONE) {
1091 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1092 				  ("PROBE_GET_CSD: error %d\n", err));
1093 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1094                         break;
1095                 }
1096 
1097                 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1098                 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1099                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1100                           ("CSD %08x%08x%08x%08x\n",
1101                            mmcp->card_csd[0],
1102                            mmcp->card_csd[1],
1103                            mmcp->card_csd[2],
1104                            mmcp->card_csd[3]));
1105                 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1106                 break;
1107         }
1108         case PROBE_SELECT_CARD: {
1109 		mmcio = &done_ccb->mmcio;
1110 		err = mmcio->cmd.error;
1111 		if (err != MMC_ERR_NONE) {
1112 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1113 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1114 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1115                         break;
1116                 }
1117 
1118 		PROBE_SET_ACTION(softc, PROBE_DONE);
1119                 break;
1120         }
1121 	default:
1122 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1123 			  ("mmcprobe_done: invalid action state 0x%x\n", softc->action));
1124 		panic("default: case in mmc_probe_done()");
1125 	}
1126 
1127 	if (softc->action == PROBE_INVALID &&
1128 	  (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1129 		xpt_async(AC_LOST_DEVICE, path, NULL);
1130 	}
1131 
1132 	if (softc->action != PROBE_INVALID)
1133 		xpt_schedule(periph, priority);
1134 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1135 	int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1136 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1137 	  ("mmcprobe_done: remaining freeze count %d\n", frozen));
1138 
1139 	if (softc->action == PROBE_DONE) {
1140                 /* Notify the system that the device is found! */
1141 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1142 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1143 			xpt_acquire_device(path->device);
1144 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1145 			xpt_action(done_ccb);
1146 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1147 		}
1148 	}
1149 	xpt_release_ccb(done_ccb);
1150 	if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1151 		cam_periph_invalidate(periph);
1152 		cam_periph_release_locked(periph);
1153 	}
1154 }
1155 
1156 void
1157 mmc_path_inq(struct ccb_pathinq *cpi, const char *hba,
1158     const struct cam_sim *sim, size_t maxio)
1159 {
1160 
1161 	cpi->version_num = 1;
1162 	cpi->hba_inquiry = 0;
1163 	cpi->target_sprt = 0;
1164 	cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
1165 	cpi->hba_eng_cnt = 0;
1166 	cpi->max_target = 0;
1167 	cpi->max_lun = 0;
1168 	cpi->initiator_id = 1;
1169 	cpi->maxio = maxio;
1170 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1171 	strncpy(cpi->hba_vid, hba, HBA_IDLEN);
1172 	strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1173 	cpi->unit_number = cam_sim_unit(sim);
1174 	cpi->bus_id = cam_sim_bus(sim);
1175 	cpi->protocol = PROTO_MMCSD;
1176 	cpi->protocol_version = SCSI_REV_0;
1177 	cpi->transport = XPORT_MMCSD;
1178 	cpi->transport_version = 1;
1179 
1180 	cpi->base_transfer_speed = 100; /* XXX WTF? */
1181 
1182 	cpi->ccb_h.status = CAM_REQ_CMP;
1183 }
1184