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