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