xref: /freebsd/sys/cam/mmc/mmc_xpt.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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 	KASSERT(sim->sim_dev != NULL, ("mmccam_start_discovery(%s): sim_dev is not initialized,"
410 	    " has cam_sim_alloc_dev() been used?", cam_sim_name(sim)));
411 	pathid = cam_sim_path(sim);
412 	ccb = xpt_alloc_ccb();
413 
414 	/*
415 	 * We create a rescan request for BUS:0:0, since the card
416 	 * will be at lun 0.
417 	 */
418 	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
419 		/* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
420 		xpt_free_ccb(ccb);
421 		return;
422 	}
423 	xpt_rescan(ccb);
424 }
425 
426 /* This func is called per attached device :-( */
427 static void
428 mmc_print_ident(struct mmc_params *ident_data, struct sbuf *sb)
429 {
430 	bool space = false;
431 
432 	sbuf_printf(sb, "Relative addr: %08x\n", ident_data->card_rca);
433 	sbuf_printf(sb, "Card features: <");
434 	if (ident_data->card_features & CARD_FEATURE_MMC) {
435 		sbuf_printf(sb, "MMC");
436 		space = true;
437 	}
438 	if (ident_data->card_features & CARD_FEATURE_MEMORY) {
439 		sbuf_printf(sb, "%sMemory", space ? " " : "");
440 		space = true;
441 	}
442 	if (ident_data->card_features & CARD_FEATURE_SDHC) {
443 		sbuf_printf(sb, "%sHigh-Capacity", space ? " " : "");
444 		space = true;
445 	}
446 	if (ident_data->card_features & CARD_FEATURE_SD20) {
447 		sbuf_printf(sb, "%sSD2.0-Conditions", space ? " " : "");
448 		space = true;
449 	}
450 	if (ident_data->card_features & CARD_FEATURE_SDIO) {
451 		sbuf_printf(sb, "%sSDIO", space ? " " : "");
452 		space = true;
453 	}
454 	if (ident_data->card_features & CARD_FEATURE_18V) {
455 		sbuf_printf(sb, "%s1.8-Signaling", space ? " " : "");
456 	}
457 	sbuf_printf(sb, ">\n");
458 
459 	if (ident_data->card_features & CARD_FEATURE_MEMORY)
460 		sbuf_printf(sb, "Card memory OCR: %08x\n",
461 		    ident_data->card_ocr);
462 
463 	if (ident_data->card_features & CARD_FEATURE_SDIO) {
464 		sbuf_printf(sb, "Card IO OCR: %08x\n", ident_data->io_ocr);
465 		sbuf_printf(sb, "Number of functions: %u\n",
466 		    ident_data->sdio_func_count);
467 	}
468 
469 	sbuf_finish(sb);
470 	printf("%s", sbuf_data(sb));
471 	sbuf_clear(sb);
472 }
473 
474 static void
475 mmc_proto_announce(struct cam_ed *device)
476 {
477 	struct sbuf	sb;
478 	char		buffer[256];
479 
480 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
481 	mmc_print_ident(&device->mmc_ident_data, &sb);
482 	sbuf_finish(&sb);
483 	sbuf_putbuf(&sb);
484 }
485 
486 static void
487 mmc_proto_denounce(struct cam_ed *device)
488 {
489 
490 	mmc_proto_announce(device);
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 ccb_trans_settings_mmc *cts;
601 
602 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
603 	softc = (mmcprobe_softc *)periph->softc;
604 	path = start_ccb->ccb_h.path;
605 	mmcio = &start_ccb->mmcio;
606 	cts = &start_ccb->cts.proto_specific.mmc;
607 	struct mmc_params *mmcp = &path->device->mmc_ident_data;
608 
609 	memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
610 
611 	if (softc->restart) {
612 		softc->restart = 0;
613 		if (path->device->flags & CAM_DEV_UNCONFIGURED)
614 			softc->action = PROBE_RESET;
615 		else
616 			softc->action = PROBE_IDENTIFY;
617 	}
618 
619 	/* Here is the place where the identify fun begins */
620 	switch (softc->action) {
621 	case PROBE_RESET:
622 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
623 		/* FALLTHROUGH */
624 	case PROBE_IDENTIFY:
625 		xpt_path_inq(&start_ccb->cpi, periph->path);
626 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_IDENTIFY\n"));
627 		init_standard_ccb(start_ccb, XPT_MMC_GET_TRAN_SETTINGS);
628 		break;
629 
630 	case PROBE_POWER_OFF:
631 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("power off the card\n"));
632 		init_standard_ccb(start_ccb, XPT_MMC_SET_TRAN_SETTINGS);
633 		cts->ios.power_mode = power_off;
634 		cts->ios_valid = MMC_PM;
635 		break;
636 
637 	case PROBE_GET_HOST_OCR:
638 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("get the host ocr\n"));
639 		init_standard_ccb(start_ccb, XPT_MMC_GET_TRAN_SETTINGS);
640 		break;
641 
642 	case PROBE_RESET_BUS:
643 	{
644 		uint32_t host_caps = cts->host_caps;
645 		if (host_caps & MMC_CAP_SIGNALING_180)
646 			softc->flags |= PROBE_FLAG_HOST_CAN_DO_18V;
647 		uint32_t hv = mmc_highest_voltage(softc->host_ocr);
648 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("reseting the bus\n"));
649 		init_standard_ccb(start_ccb, XPT_MMC_SET_TRAN_SETTINGS);
650 		cts->ios.vdd = hv;
651 		cts->ios.bus_mode = opendrain;
652 		cts->ios.chip_select = cs_dontcare;
653 		cts->ios.power_mode = power_up;
654 		cts->ios.bus_width = bus_width_1;
655 		cts->ios.clock = 0;
656 		cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
657 			MMC_CS | MMC_BW | MMC_CLK;
658 		break;
659 	}
660 
661 	case PROBE_SET_ID_FREQ:
662 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("setting the ID freq\n"));
663 		init_standard_ccb(start_ccb, XPT_MMC_SET_TRAN_SETTINGS);
664 		cts->ios.power_mode = power_on;
665 		cts->ios.clock = CARD_ID_FREQUENCY;
666 		cts->ios.timing = bus_timing_normal;
667 		cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
668 		break;
669 
670 	case PROBE_SET_CS:
671 		/* Begin mmc_idle_cards() */
672 		init_standard_ccb(start_ccb, XPT_MMC_SET_TRAN_SETTINGS);
673 		cts->ios.chip_select = cs_high;
674 		cts->ios_valid = MMC_CS;
675 		break;
676 
677 	case PROBE_GO_IDLE_STATE:
678 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
679 		init_standard_ccb(start_ccb, XPT_MMC_IO);
680 		mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
681 		mmcio->cmd.arg = 0;
682 		mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
683 		mmcio->cmd.data = NULL;
684 		mmcio->stop.opcode = 0;
685 
686 		/* XXX Reset I/O portion as well */
687 		break;
688 
689 	case PROBE_SDIO_RESET:
690 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
691 			  ("Start with PROBE_SDIO_RESET\n"));
692 		uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
693 			| SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
694 		cam_fill_mmcio(&start_ccb->mmcio,
695 			       /*retries*/ 0,
696 			       /*cbfcnp*/ mmcprobe_done,
697 			       /*flags*/ CAM_DIR_NONE,
698 			       /*mmc_opcode*/ SD_IO_RW_DIRECT,
699 			       /*mmc_arg*/ mmc_arg,
700 			       /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
701 			       /*mmc_data*/ NULL,
702 			       /*timeout*/ 1000);
703 		break;
704 	case PROBE_SEND_IF_COND:
705 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
706 			  ("Start with PROBE_SEND_IF_COND\n"));
707 		init_standard_ccb(start_ccb, XPT_MMC_IO);
708 		mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
709 		mmcio->cmd.arg = (1 << 8) + 0xAA;
710 		mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
711 		mmcio->stop.opcode = 0;
712 		break;
713 
714 	case PROBE_SDIO_INIT:
715 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
716 			  ("Start with PROBE_SDIO_INIT\n"));
717 		init_standard_ccb(start_ccb, XPT_MMC_IO);
718 		mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
719 		mmcio->cmd.arg = mmcp->io_ocr;
720 		mmcio->cmd.flags = MMC_RSP_R4;
721 		mmcio->stop.opcode = 0;
722 		break;
723 
724 	case PROBE_MMC_INIT:
725 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
726 			  ("Start with PROBE_MMC_INIT\n"));
727 		init_standard_ccb(start_ccb, XPT_MMC_IO);
728 		mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
729 		mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
730 		mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
731 		mmcio->stop.opcode = 0;
732 		break;
733 
734 	case PROBE_SEND_APP_OP_COND:
735 		init_standard_ccb(start_ccb, XPT_MMC_IO);
736 		if (softc->flags & PROBE_FLAG_ACMD_SENT) {
737 			mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
738 			/*
739 			 * We set CCS bit because we do support SDHC cards.
740 			 * XXX: Don't set CCS if no response to CMD8.
741 			 */
742 			uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
743 			if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
744 				cmd_arg |= MMC_OCR_S18R;
745 			mmcio->cmd.arg = cmd_arg;
746 			mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
747 			softc->acmd41_count++;
748 		} else {
749 			mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
750 			mmcio->cmd.arg = 0; /* rca << 16 */
751 			mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
752 		}
753 		mmcio->stop.opcode = 0;
754 		break;
755 
756 	case PROBE_GET_CID: /* XXX move to mmc_da */
757 		init_standard_ccb(start_ccb, XPT_MMC_IO);
758 		mmcio->cmd.opcode = MMC_ALL_SEND_CID;
759 		mmcio->cmd.arg = 0;
760 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
761 		mmcio->stop.opcode = 0;
762 		break;
763 	case PROBE_SEND_RELATIVE_ADDR:
764 		init_standard_ccb(start_ccb, XPT_MMC_IO);
765 		mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
766 		mmcio->cmd.arg = 0;
767 		mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
768 		mmcio->stop.opcode = 0;
769 		break;
770 	case PROBE_MMC_SET_RELATIVE_ADDR:
771 		init_standard_ccb(start_ccb, XPT_MMC_IO);
772 		mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
773 		mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
774 		mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
775 		mmcio->stop.opcode = 0;
776 		break;
777 	case PROBE_SELECT_CARD:
778 		init_standard_ccb(start_ccb, XPT_MMC_IO);
779 		mmcio->cmd.opcode = MMC_SELECT_CARD;
780 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
781 		mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
782 		mmcio->stop.opcode = 0;
783 		break;
784 	case PROBE_GET_CSD: /* XXX move to mmc_da */
785 		init_standard_ccb(start_ccb, XPT_MMC_IO);
786 		mmcio->cmd.opcode = MMC_SEND_CSD;
787 		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
788 		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
789 		mmcio->stop.opcode = 0;
790 		break;
791 	case PROBE_DONE:
792 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
793 		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
794 		cts->ios.bus_mode = pushpull;
795 		cts->ios_valid = MMC_BM;
796 		xpt_action(start_ccb);
797 		return;
798 		/* NOTREACHED */
799 		break;
800 	case PROBE_INVALID:
801 		break;
802 	default:
803 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
804 		panic("default: case in mmc_probe_start()");
805 	}
806 
807 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
808 	xpt_action(start_ccb);
809 }
810 
811 static void mmcprobe_cleanup(struct cam_periph *periph)
812 {
813 	free(periph->softc, M_CAMXPT);
814 }
815 
816 static void
817 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
818 {
819 	mmcprobe_softc *softc;
820 	struct cam_path *path;
821 
822 	int err;
823 	struct ccb_mmcio *mmcio;
824 	u_int32_t  priority;
825 
826 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
827 	softc = (mmcprobe_softc *)periph->softc;
828 	path = done_ccb->ccb_h.path;
829 	priority = done_ccb->ccb_h.pinfo.priority;
830 
831 	switch (softc->action) {
832 	case PROBE_RESET:
833 		/* FALLTHROUGH */
834 	case PROBE_IDENTIFY:
835 	{
836 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
837 		PROBE_SET_ACTION(softc, PROBE_POWER_OFF);
838 		break;
839 	}
840 	case PROBE_POWER_OFF:
841 	{
842 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_POWER_OFF\n"));
843 		PROBE_SET_ACTION(softc, PROBE_GET_HOST_OCR);
844 		break;
845 	}
846 	case PROBE_GET_HOST_OCR:
847 	{
848 		struct ccb_trans_settings_mmc *cts;
849 		cts = &done_ccb->cts.proto_specific.mmc;
850 		softc->host_ocr = cts->host_ocr;
851 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_GET_HOST_OCR (Got OCR=%x\n", softc->host_ocr));
852 		PROBE_SET_ACTION(softc, PROBE_RESET_BUS);
853 		break;
854 	}
855 	case PROBE_RESET_BUS:
856 	{
857 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET_BUS\n"));
858 		PROBE_SET_ACTION(softc, PROBE_SET_ID_FREQ);
859 		break;
860 	}
861 	case PROBE_SET_ID_FREQ:
862 	{
863 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_SET_ID_FREQ\n"));
864 		PROBE_SET_ACTION(softc, PROBE_SET_CS);
865 		break;
866 	}
867 	case PROBE_SET_CS:
868 	{
869 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_SET_CS\n"));
870 		PROBE_SET_ACTION(softc, PROBE_GO_IDLE_STATE);
871 		break;
872 	}
873 	case PROBE_GO_IDLE_STATE:
874 	{
875 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_GO_IDLE_STATE\n"));
876 		mmcio = &done_ccb->mmcio;
877 		err = mmcio->cmd.error;
878 
879 		if (err != MMC_ERR_NONE) {
880 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
881 				  ("GO_IDLE_STATE failed with error %d\n",
882 				   err));
883 
884 			/* There was a device there, but now it's gone... */
885 			if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
886 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
887 				  ("Device lost!\n"));
888 
889 				xpt_async(AC_LOST_DEVICE, path, NULL);
890 			}
891 			PROBE_SET_ACTION(softc, PROBE_INVALID);
892 			break;
893 		}
894 		path->device->protocol = PROTO_MMCSD;
895 		PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
896 		break;
897 	}
898 	case PROBE_SEND_IF_COND:
899 	{
900 		mmcio = &done_ccb->mmcio;
901 		err = mmcio->cmd.error;
902 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
903 
904 		if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
905 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
906 				  ("IF_COND: error %d, pattern %08x\n",
907 				   err, mmcio->cmd.resp[0]));
908 		} else {
909 			mmcp->card_features |= CARD_FEATURE_SD20;
910 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
911 				  ("SD 2.0 interface conditions: OK\n"));
912 		}
913                 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
914 		break;
915 	}
916 	case PROBE_SDIO_RESET:
917 	{
918 		mmcio = &done_ccb->mmcio;
919 		err = mmcio->cmd.error;
920 
921 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
922 		    ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
923 		    err, mmcio->cmd.resp[0]));
924 		PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
925 		break;
926 	}
927 	case PROBE_SDIO_INIT:
928 	{
929 		mmcio = &done_ccb->mmcio;
930 		err = mmcio->cmd.error;
931 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
932 
933 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
934 		    ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
935 		    err, mmcio->cmd.resp[0],
936 		    mmcio->cmd.resp[1],
937 		    mmcio->cmd.resp[2],
938 		    mmcio->cmd.resp[3]));
939 
940 		/*
941 		 * Error here means that this card is not SDIO,
942 		 * so proceed with memory init as if nothing has happened
943 		 */
944 		if (err != MMC_ERR_NONE) {
945 			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
946 			break;
947 		}
948 		mmcp->card_features |= CARD_FEATURE_SDIO;
949 		uint32_t ioifcond = mmcio->cmd.resp[0];
950 		uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
951 
952 		mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
953 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
954 		    ("SDIO card: %d functions\n", mmcp->sdio_func_count));
955 		if (io_ocr == 0) {
956 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
957 			  ("SDIO OCR invalid, retrying\n"));
958 			break; /* Retry */
959 		}
960 
961 		if (io_ocr != 0 && mmcp->io_ocr == 0) {
962 			mmcp->io_ocr = io_ocr;
963 			break; /* Retry, this time with non-0 OCR */
964 		}
965 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
966 		    ("SDIO OCR: %08x\n", mmcp->io_ocr));
967 
968 		if (ioifcond & R4_IO_MEM_PRESENT) {
969 			/* Combo card -- proceed to memory initialization */
970 			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
971 		} else {
972 			/* No memory portion -- get RCA and select card */
973 			PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
974 		}
975 		break;
976 	}
977 	case PROBE_MMC_INIT:
978 	{
979 		mmcio = &done_ccb->mmcio;
980 		err = mmcio->cmd.error;
981 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
982 
983 		if (err != MMC_ERR_NONE) {
984 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
985 				  ("MMC_INIT: error %d, resp %08x\n",
986 				   err, mmcio->cmd.resp[0]));
987 			PROBE_SET_ACTION(softc, PROBE_INVALID);
988 			break;
989 		}
990 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
991 		    ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
992 
993 		if (mmcp->card_ocr == 0) {
994 			/* We haven't sent the OCR to the card yet -- do it */
995 			mmcp->card_ocr = mmcio->cmd.resp[0];
996 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
997 			    ("-> sending OCR to card\n"));
998 			break;
999 		}
1000 
1001 		if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
1002 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1003 			    ("Card is still powering up\n"));
1004 			break;
1005 		}
1006 
1007 		mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
1008 		PROBE_SET_ACTION(softc, PROBE_GET_CID);
1009 		break;
1010 	}
1011 	case PROBE_SEND_APP_OP_COND:
1012 	{
1013 		mmcio = &done_ccb->mmcio;
1014 		err = mmcio->cmd.error;
1015 
1016 		if (err != MMC_ERR_NONE) {
1017 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1018 				  ("APP_OP_COND: error %d, resp %08x\n",
1019 				   err, mmcio->cmd.resp[0]));
1020 			PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
1021 			break;
1022 		}
1023 
1024 		if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
1025 			/* Don't change the state */
1026 			softc->flags |= PROBE_FLAG_ACMD_SENT;
1027 			break;
1028 		}
1029 
1030 		softc->flags &= ~PROBE_FLAG_ACMD_SENT;
1031 		if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
1032 		    (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
1033 			struct mmc_params *mmcp = &path->device->mmc_ident_data;
1034 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1035 			    ("Card OCR: %08x\n",  mmcio->cmd.resp[0]));
1036 			if (mmcp->card_ocr == 0) {
1037 				mmcp->card_ocr = mmcio->cmd.resp[0];
1038 				/* Now when we know OCR that we want -- send it to card */
1039 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1040 				    ("-> sending OCR to card\n"));
1041 			} else {
1042 				/* We already know the OCR and despite of that we
1043 				 * are processing the answer to ACMD41 -> move on
1044 				 */
1045 				PROBE_SET_ACTION(softc, PROBE_GET_CID);
1046 			}
1047 			/* Getting an answer to ACMD41 means the card has memory */
1048 			mmcp->card_features |= CARD_FEATURE_MEMORY;
1049 
1050 			/* Standard capacity vs High Capacity memory card */
1051 			if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
1052 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1053 				    ("Card is SDHC\n"));
1054 				mmcp->card_features |= CARD_FEATURE_SDHC;
1055 			}
1056 
1057 			/* Whether the card supports 1.8V signaling */
1058 			if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
1059 				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1060 					  ("Card supports 1.8V signaling\n"));
1061 				mmcp->card_features |= CARD_FEATURE_18V;
1062 				if (softc->flags & PROBE_FLAG_HOST_CAN_DO_18V) {
1063 					CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1064 						  ("Host supports 1.8V signaling. Switch voltage!\n"));
1065 					done_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1066 					done_ccb->ccb_h.flags = CAM_DIR_NONE;
1067 					done_ccb->ccb_h.retry_count = 0;
1068 					done_ccb->ccb_h.timeout = 100;
1069 					done_ccb->ccb_h.cbfcnp = NULL;
1070 					done_ccb->cts.proto_specific.mmc.ios.vccq = vccq_180;
1071 					done_ccb->cts.proto_specific.mmc.ios_valid = MMC_VCCQ;
1072 					xpt_action(done_ccb);
1073 				}
1074 			}
1075 		} else {
1076 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1077 				  ("Card not ready: %08x\n",  mmcio->cmd.resp[0]));
1078 			/* Send CMD55+ACMD41 once again  */
1079 			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
1080 		}
1081 
1082 		break;
1083 	}
1084 	case PROBE_GET_CID: /* XXX move to mmc_da */
1085 	{
1086 		mmcio = &done_ccb->mmcio;
1087 		err = mmcio->cmd.error;
1088 
1089 		if (err != MMC_ERR_NONE) {
1090 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1091 				  ("PROBE_GET_CID: error %d\n", err));
1092 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1093 			break;
1094 		}
1095 
1096 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
1097 		memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1098 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1099 		    ("CID %08x%08x%08x%08x\n",
1100 		      mmcp->card_cid[0],
1101 		      mmcp->card_cid[1],
1102 		      mmcp->card_cid[2],
1103 		      mmcp->card_cid[3]));
1104 		if (mmcp->card_features & CARD_FEATURE_MMC)
1105 			PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
1106 		else
1107 			PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1108 		break;
1109 	}
1110 	case PROBE_SEND_RELATIVE_ADDR: {
1111 		mmcio = &done_ccb->mmcio;
1112 		err = mmcio->cmd.error;
1113 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
1114 		uint16_t rca = mmcio->cmd.resp[0] >> 16;
1115 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1116 		    ("Card published RCA: %u\n", rca));
1117 		path->device->mmc_ident_data.card_rca = rca;
1118 		if (err != MMC_ERR_NONE) {
1119 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1120 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1121 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1122 			break;
1123 		}
1124 
1125 		/* If memory is present, get CSD, otherwise select card */
1126 		if (mmcp->card_features & CARD_FEATURE_MEMORY)
1127 			PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1128 		else
1129 			PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1130 		break;
1131 	}
1132 	case PROBE_MMC_SET_RELATIVE_ADDR:
1133 		mmcio = &done_ccb->mmcio;
1134 		err = mmcio->cmd.error;
1135 		if (err != MMC_ERR_NONE) {
1136 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1137 			    ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1138 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1139 			break;
1140 		}
1141 		path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1142 		PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1143 		break;
1144 	case PROBE_GET_CSD: {
1145 		mmcio = &done_ccb->mmcio;
1146 		err = mmcio->cmd.error;
1147 
1148 		if (err != MMC_ERR_NONE) {
1149 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1150 				  ("PROBE_GET_CSD: error %d\n", err));
1151 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1152 			break;
1153 		}
1154 
1155 		struct mmc_params *mmcp = &path->device->mmc_ident_data;
1156 		memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1157 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1158 		    ("CSD %08x%08x%08x%08x\n",
1159 		      mmcp->card_csd[0],
1160 		      mmcp->card_csd[1],
1161 		      mmcp->card_csd[2],
1162 		      mmcp->card_csd[3]));
1163 		PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1164 		break;
1165 	}
1166 	case PROBE_SELECT_CARD: {
1167 		mmcio = &done_ccb->mmcio;
1168 		err = mmcio->cmd.error;
1169 		if (err != MMC_ERR_NONE) {
1170 			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1171 				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1172 			PROBE_SET_ACTION(softc, PROBE_INVALID);
1173 			break;
1174 		}
1175 
1176 		PROBE_SET_ACTION(softc, PROBE_DONE);
1177 		break;
1178 	}
1179 	default:
1180 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1181 			  ("mmcprobe_done: invalid action state 0x%x\n", softc->action));
1182 		panic("default: case in mmc_probe_done()");
1183 	}
1184 
1185 	if (softc->action == PROBE_INVALID &&
1186 	  (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1187 		xpt_async(AC_LOST_DEVICE, path, NULL);
1188 	}
1189 
1190 	if (softc->action != PROBE_INVALID)
1191 		xpt_schedule(periph, priority);
1192 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1193 	int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1194 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1195 	  ("mmcprobe_done: remaining freeze count %d\n", frozen));
1196 
1197 	if (softc->action == PROBE_DONE) {
1198                 /* Notify the system that the device is found! */
1199 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1200 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1201 			xpt_acquire_device(path->device);
1202 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1203 			xpt_action(done_ccb);
1204 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1205 		}
1206 	}
1207 	xpt_release_ccb(done_ccb);
1208 	if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1209 		cam_periph_invalidate(periph);
1210 		cam_periph_release_locked(periph);
1211 	}
1212 }
1213 
1214 void
1215 mmc_path_inq(struct ccb_pathinq *cpi, const char *hba,
1216     const struct cam_sim *sim, size_t maxio)
1217 {
1218 
1219 	cpi->version_num = 1;
1220 	cpi->hba_inquiry = 0;
1221 	cpi->target_sprt = 0;
1222 	cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
1223 	cpi->hba_eng_cnt = 0;
1224 	cpi->max_target = 0;
1225 	cpi->max_lun = 0;
1226 	cpi->initiator_id = 1;
1227 	cpi->maxio = maxio;
1228 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1229 	strncpy(cpi->hba_vid, hba, HBA_IDLEN);
1230 	strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1231 	cpi->unit_number = cam_sim_unit(sim);
1232 	cpi->bus_id = cam_sim_bus(sim);
1233 	cpi->protocol = PROTO_MMCSD;
1234 	cpi->protocol_version = SCSI_REV_0;
1235 	cpi->transport = XPORT_MMCSD;
1236 	cpi->transport_version = 1;
1237 
1238 	cpi->base_transfer_speed = 100; /* XXX WTF? */
1239 
1240 	cpi->ccb_h.status = CAM_REQ_CMP;
1241 }
1242