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