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