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