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