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