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