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