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