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