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