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