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