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 case CDAI_TYPE_MMC_PARAMS: 371 cdai->provsiz = sizeof(struct mmc_params); 372 amt = MIN(cdai->provsiz, cdai->bufsiz); 373 memcpy(cdai->buf, &device->mmc_ident_data, amt); 374 break; 375 default: 376 panic("Unknown buftype"); 377 return; 378 } 379 start_ccb->ccb_h.status = CAM_REQ_CMP; 380 } 381 382 static void 383 mmc_announce_periph(struct cam_periph *periph) 384 { 385 struct ccb_pathinq cpi; 386 struct ccb_trans_settings cts; 387 struct cam_path *path = periph->path; 388 389 cam_periph_assert(periph, MA_OWNED); 390 391 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 392 ("mmc_announce_periph: called\n")); 393 394 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 395 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 396 cts.type = CTS_TYPE_CURRENT_SETTINGS; 397 xpt_action((union ccb*)&cts); 398 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 399 return; 400 xpt_path_inq(&cpi, periph->path); 401 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock); 402 } 403 404 /* This func is called per attached device :-( */ 405 void 406 mmc_print_ident(struct mmc_params *ident_data) 407 { 408 printf("Relative addr: %08x\n", ident_data->card_rca); 409 printf("Card features: <"); 410 if (ident_data->card_features & CARD_FEATURE_MMC) 411 printf("MMC "); 412 if (ident_data->card_features & CARD_FEATURE_MEMORY) 413 printf("Memory "); 414 if (ident_data->card_features & CARD_FEATURE_SDHC) 415 printf("High-Capacity "); 416 if (ident_data->card_features & CARD_FEATURE_SD20) 417 printf("SD2.0-Conditions "); 418 if (ident_data->card_features & CARD_FEATURE_SDIO) 419 printf("SDIO "); 420 printf(">\n"); 421 422 if (ident_data->card_features & CARD_FEATURE_MEMORY) 423 printf("Card memory OCR: %08x\n", ident_data->card_ocr); 424 425 if (ident_data->card_features & CARD_FEATURE_SDIO) { 426 printf("Card IO OCR: %08x\n", ident_data->io_ocr); 427 printf("Number of funcitions: %u\n", ident_data->sdio_func_count); 428 } 429 } 430 431 static void 432 mmc_proto_announce(struct cam_ed *device) 433 { 434 mmc_print_ident(&device->mmc_ident_data); 435 } 436 437 static void 438 mmc_proto_denounce(struct cam_ed *device) 439 { 440 mmc_print_ident(&device->mmc_ident_data); 441 } 442 443 static void 444 mmc_proto_debug_out(union ccb *ccb) 445 { 446 if (ccb->ccb_h.func_code != XPT_MMC_IO) 447 return; 448 449 CAM_DEBUG(ccb->ccb_h.path, 450 CAM_DEBUG_CDB,("mmc_proto_debug_out\n")); 451 } 452 453 static periph_init_t probe_periph_init; 454 455 static struct periph_driver probe_driver = 456 { 457 probe_periph_init, "mmcprobe", 458 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0, 459 CAM_PERIPH_DRV_EARLY 460 }; 461 462 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver); 463 464 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */ 465 466 static void 467 probe_periph_init() 468 { 469 } 470 471 static cam_status 472 mmcprobe_register(struct cam_periph *periph, void *arg) 473 { 474 mmcprobe_softc *softc; 475 union ccb *request_ccb; /* CCB representing the probe request */ 476 int status; 477 478 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n")); 479 480 request_ccb = (union ccb *)arg; 481 if (request_ccb == NULL) { 482 printf("mmcprobe_register: no probe CCB, " 483 "can't register device\n"); 484 return(CAM_REQ_CMP_ERR); 485 } 486 487 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT); 488 489 if (softc == NULL) { 490 printf("proberegister: Unable to probe new device. " 491 "Unable to allocate softc\n"); 492 return(CAM_REQ_CMP_ERR); 493 } 494 495 softc->flags = 0; 496 softc->acmd41_count = 0; 497 periph->softc = softc; 498 softc->periph = periph; 499 softc->action = PROBE_INVALID; 500 softc->restart = 0; 501 status = cam_periph_acquire(periph); 502 503 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params)); 504 if (status != 0) { 505 printf("proberegister: cam_periph_acquire failed (status=%d)\n", 506 status); 507 return (CAM_REQ_CMP_ERR); 508 } 509 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n")); 510 511 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) 512 PROBE_SET_ACTION(softc, PROBE_RESET); 513 else 514 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 515 516 /* This will kick the ball */ 517 xpt_schedule(periph, CAM_PRIORITY_XPT); 518 519 return(CAM_REQ_CMP); 520 } 521 522 static int 523 mmc_highest_voltage(uint32_t ocr) 524 { 525 int i; 526 527 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 528 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 529 if (ocr & (1 << i)) 530 return (i); 531 return (-1); 532 } 533 534 static inline void 535 init_standard_ccb(union ccb *ccb, uint32_t cmd) 536 { 537 ccb->ccb_h.func_code = cmd; 538 ccb->ccb_h.flags = CAM_DIR_OUT; 539 ccb->ccb_h.retry_count = 0; 540 ccb->ccb_h.timeout = 15 * 1000; 541 ccb->ccb_h.cbfcnp = mmcprobe_done; 542 } 543 544 static void 545 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb) 546 { 547 mmcprobe_softc *softc; 548 struct cam_path *path; 549 struct ccb_mmcio *mmcio; 550 struct mtx *p_mtx = cam_periph_mtx(periph); 551 struct ccb_trans_settings_mmc *cts; 552 553 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n")); 554 softc = (mmcprobe_softc *)periph->softc; 555 path = start_ccb->ccb_h.path; 556 mmcio = &start_ccb->mmcio; 557 cts = &start_ccb->cts.proto_specific.mmc; 558 struct mmc_params *mmcp = &path->device->mmc_ident_data; 559 560 memset(&mmcio->cmd, 0, sizeof(struct mmc_command)); 561 562 if (softc->restart) { 563 softc->restart = 0; 564 if (path->device->flags & CAM_DEV_UNCONFIGURED) 565 softc->action = PROBE_RESET; 566 else 567 softc->action = PROBE_IDENTIFY; 568 569 } 570 571 /* Here is the place where the identify fun begins */ 572 switch (softc->action) { 573 case PROBE_RESET: 574 /* FALLTHROUGH */ 575 case PROBE_IDENTIFY: 576 xpt_path_inq(&start_ccb->cpi, periph->path); 577 578 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n")); 579 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS); 580 cts->ios.power_mode = power_off; 581 cts->ios_valid = MMC_PM; 582 xpt_action(start_ccb); 583 mtx_sleep(periph, p_mtx, 0, "mmcios", 100); 584 585 /* mmc_power_up */ 586 /* Get the host OCR */ 587 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS); 588 xpt_action(start_ccb); 589 590 uint32_t hv = mmc_highest_voltage(cts->host_ocr); 591 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS); 592 cts->ios.vdd = hv; 593 cts->ios.bus_mode = opendrain; 594 cts->ios.chip_select = cs_dontcare; 595 cts->ios.power_mode = power_up; 596 cts->ios.bus_width = bus_width_1; 597 cts->ios.clock = 0; 598 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM | 599 MMC_CS | MMC_BW | MMC_CLK; 600 xpt_action(start_ccb); 601 mtx_sleep(periph, p_mtx, 0, "mmcios", 100); 602 603 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS); 604 cts->ios.power_mode = power_on; 605 cts->ios.clock = CARD_ID_FREQUENCY; 606 cts->ios.timing = bus_timing_normal; 607 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT; 608 xpt_action(start_ccb); 609 mtx_sleep(periph, p_mtx, 0, "mmcios", 100); 610 /* End for mmc_power_on */ 611 612 /* Begin mmc_idle_cards() */ 613 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS); 614 cts->ios.chip_select = cs_high; 615 cts->ios_valid = MMC_CS; 616 xpt_action(start_ccb); 617 mtx_sleep(periph, p_mtx, 0, "mmcios", 1); 618 619 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n")); 620 init_standard_ccb(start_ccb, XPT_MMC_IO); 621 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */ 622 mmcio->cmd.arg = 0; 623 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 624 mmcio->cmd.data = NULL; 625 mmcio->stop.opcode = 0; 626 627 /* XXX Reset I/O portion as well */ 628 break; 629 case PROBE_SDIO_RESET: 630 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, 631 ("Start with PROBE_SDIO_RESET\n")); 632 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL) 633 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW; 634 cam_fill_mmcio(&start_ccb->mmcio, 635 /*retries*/ 0, 636 /*cbfcnp*/ mmcprobe_done, 637 /*flags*/ CAM_DIR_NONE, 638 /*mmc_opcode*/ SD_IO_RW_DIRECT, 639 /*mmc_arg*/ mmc_arg, 640 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC, 641 /*mmc_data*/ NULL, 642 /*timeout*/ 1000); 643 break; 644 case PROBE_SEND_IF_COND: 645 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, 646 ("Start with PROBE_SEND_IF_COND\n")); 647 init_standard_ccb(start_ccb, XPT_MMC_IO); 648 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */ 649 mmcio->cmd.arg = (1 << 8) + 0xAA; 650 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 651 mmcio->stop.opcode = 0; 652 break; 653 654 case PROBE_SDIO_INIT: 655 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, 656 ("Start with PROBE_SDIO_INIT\n")); 657 init_standard_ccb(start_ccb, XPT_MMC_IO); 658 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */ 659 mmcio->cmd.arg = mmcp->io_ocr; 660 mmcio->cmd.flags = MMC_RSP_R4; 661 mmcio->stop.opcode = 0; 662 break; 663 664 case PROBE_MMC_INIT: 665 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, 666 ("Start with PROBE_MMC_INIT\n")); 667 init_standard_ccb(start_ccb, XPT_MMC_IO); 668 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */ 669 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */; 670 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 671 mmcio->stop.opcode = 0; 672 break; 673 674 case PROBE_SEND_APP_OP_COND: 675 init_standard_ccb(start_ccb, XPT_MMC_IO); 676 if (softc->flags & PROBE_FLAG_ACMD_SENT) { 677 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */ 678 /* 679 * We set CCS bit because we do support SDHC cards. 680 * XXX: Don't set CCS if no response to CMD8. 681 */ 682 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */ 683 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 ) 684 cmd_arg |= MMC_OCR_S18R; 685 mmcio->cmd.arg = cmd_arg; 686 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 687 softc->acmd41_count++; 688 } else { 689 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */ 690 mmcio->cmd.arg = 0; /* rca << 16 */ 691 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 692 } 693 mmcio->stop.opcode = 0; 694 break; 695 696 case PROBE_GET_CID: /* XXX move to mmc_da */ 697 init_standard_ccb(start_ccb, XPT_MMC_IO); 698 mmcio->cmd.opcode = MMC_ALL_SEND_CID; 699 mmcio->cmd.arg = 0; 700 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 701 mmcio->stop.opcode = 0; 702 break; 703 704 case PROBE_SEND_RELATIVE_ADDR: 705 init_standard_ccb(start_ccb, XPT_MMC_IO); 706 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR; 707 mmcio->cmd.arg = 0; 708 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 709 mmcio->stop.opcode = 0; 710 break; 711 case PROBE_SELECT_CARD: 712 init_standard_ccb(start_ccb, XPT_MMC_IO); 713 mmcio->cmd.opcode = MMC_SELECT_CARD; 714 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16; 715 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 716 mmcio->stop.opcode = 0; 717 break; 718 case PROBE_GET_CSD: /* XXX move to mmc_da */ 719 init_standard_ccb(start_ccb, XPT_MMC_IO); 720 mmcio->cmd.opcode = MMC_SEND_CSD; 721 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16; 722 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 723 mmcio->stop.opcode = 0; 724 break; 725 case PROBE_DONE: 726 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n")); 727 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS); 728 cts->ios.bus_mode = pushpull; 729 cts->ios_valid = MMC_BM; 730 xpt_action(start_ccb); 731 return; 732 /* NOTREACHED */ 733 break; 734 case PROBE_INVALID: 735 break; 736 default: 737 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action)); 738 panic("default: case in mmc_probe_start()"); 739 } 740 741 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 742 xpt_action(start_ccb); 743 } 744 745 static void mmcprobe_cleanup(struct cam_periph *periph) 746 { 747 free(periph->softc, M_CAMXPT); 748 } 749 750 static void 751 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb) 752 { 753 mmcprobe_softc *softc; 754 struct cam_path *path; 755 756 int err; 757 struct ccb_mmcio *mmcio; 758 u_int32_t priority; 759 760 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n")); 761 softc = (mmcprobe_softc *)periph->softc; 762 path = done_ccb->ccb_h.path; 763 priority = done_ccb->ccb_h.pinfo.priority; 764 765 switch (softc->action) { 766 case PROBE_RESET: 767 /* FALLTHROUGH */ 768 case PROBE_IDENTIFY: 769 { 770 printf("Starting completion of PROBE_RESET\n"); 771 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n")); 772 mmcio = &done_ccb->mmcio; 773 err = mmcio->cmd.error; 774 775 if (err != MMC_ERR_NONE) { 776 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 777 ("GO_IDLE_STATE failed with error %d\n", 778 err)); 779 780 /* There was a device there, but now it's gone... */ 781 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 782 xpt_async(AC_LOST_DEVICE, path, NULL); 783 } 784 PROBE_SET_ACTION(softc, PROBE_INVALID); 785 break; 786 } 787 path->device->protocol = PROTO_MMCSD; 788 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND); 789 break; 790 } 791 case PROBE_SEND_IF_COND: 792 { 793 mmcio = &done_ccb->mmcio; 794 err = mmcio->cmd.error; 795 struct mmc_params *mmcp = &path->device->mmc_ident_data; 796 797 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) { 798 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 799 ("IF_COND: error %d, pattern %08x\n", 800 err, mmcio->cmd.resp[0])); 801 } else { 802 mmcp->card_features |= CARD_FEATURE_SD20; 803 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 804 ("SD 2.0 interface conditions: OK\n")); 805 806 } 807 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET); 808 break; 809 } 810 case PROBE_SDIO_RESET: 811 { 812 mmcio = &done_ccb->mmcio; 813 err = mmcio->cmd.error; 814 815 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 816 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n", 817 err, mmcio->cmd.resp[0])); 818 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT); 819 break; 820 } 821 case PROBE_SDIO_INIT: 822 { 823 mmcio = &done_ccb->mmcio; 824 err = mmcio->cmd.error; 825 struct mmc_params *mmcp = &path->device->mmc_ident_data; 826 827 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 828 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n", 829 err, mmcio->cmd.resp[0], 830 mmcio->cmd.resp[1], 831 mmcio->cmd.resp[2], 832 mmcio->cmd.resp[3])); 833 834 /* 835 * Error here means that this card is not SDIO, 836 * so proceed with memory init as if nothing has happened 837 */ 838 if (err != MMC_ERR_NONE) { 839 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND); 840 break; 841 } 842 mmcp->card_features |= CARD_FEATURE_SDIO; 843 uint32_t ioifcond = mmcio->cmd.resp[0]; 844 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK; 845 846 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond); 847 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 848 ("SDIO card: %d functions\n", mmcp->sdio_func_count)); 849 if (io_ocr == 0) { 850 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 851 ("SDIO OCR invalid?!\n")); 852 break; /* Retry */ 853 } 854 855 if (io_ocr != 0 && mmcp->io_ocr == 0) { 856 mmcp->io_ocr = io_ocr; 857 break; /* Retry, this time with non-0 OCR */ 858 } 859 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 860 ("SDIO OCR: %08x\n", mmcp->io_ocr)); 861 862 if (ioifcond & R4_IO_MEM_PRESENT) { 863 /* Combo card -- proceed to memory initialization */ 864 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND); 865 } else { 866 /* No memory portion -- get RCA and select card */ 867 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR); 868 } 869 break; 870 } 871 case PROBE_MMC_INIT: 872 { 873 mmcio = &done_ccb->mmcio; 874 err = mmcio->cmd.error; 875 struct mmc_params *mmcp = &path->device->mmc_ident_data; 876 877 if (err != MMC_ERR_NONE) { 878 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 879 ("MMC_INIT: error %d, resp %08x\n", 880 err, mmcio->cmd.resp[0])); 881 PROBE_SET_ACTION(softc, PROBE_INVALID); 882 break; 883 } 884 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 885 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0])); 886 887 if (mmcp->card_ocr == 0) { 888 /* We haven't sent the OCR to the card yet -- do it */ 889 mmcp->card_ocr = mmcio->cmd.resp[0]; 890 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 891 ("-> sending OCR to card\n")); 892 break; 893 } 894 895 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) { 896 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 897 ("Card is still powering up\n")); 898 break; 899 } 900 901 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY; 902 PROBE_SET_ACTION(softc, PROBE_GET_CID); 903 break; 904 } 905 case PROBE_SEND_APP_OP_COND: 906 { 907 mmcio = &done_ccb->mmcio; 908 err = mmcio->cmd.error; 909 910 if (err != MMC_ERR_NONE) { 911 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 912 ("APP_OP_COND: error %d, resp %08x\n", 913 err, mmcio->cmd.resp[0])); 914 PROBE_SET_ACTION(softc, PROBE_MMC_INIT); 915 break; 916 } 917 918 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) { 919 /* Don't change the state */ 920 softc->flags |= PROBE_FLAG_ACMD_SENT; 921 break; 922 } 923 924 softc->flags &= ~PROBE_FLAG_ACMD_SENT; 925 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) || 926 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) { 927 struct mmc_params *mmcp = &path->device->mmc_ident_data; 928 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 929 ("Card OCR: %08x\n", mmcio->cmd.resp[0])); 930 if (mmcp->card_ocr == 0) { 931 mmcp->card_ocr = mmcio->cmd.resp[0]; 932 /* Now when we know OCR that we want -- send it to card */ 933 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 934 ("-> sending OCR to card\n")); 935 } else { 936 /* We already know the OCR and despite of that we 937 * are processing the answer to ACMD41 -> move on 938 */ 939 PROBE_SET_ACTION(softc, PROBE_GET_CID); 940 } 941 /* Getting an answer to ACMD41 means the card has memory */ 942 mmcp->card_features |= CARD_FEATURE_MEMORY; 943 944 /* Standard capacity vs High Capacity memory card */ 945 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) { 946 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 947 ("Card is SDHC\n")); 948 mmcp->card_features |= CARD_FEATURE_SDHC; 949 } 950 951 /* Whether the card supports 1.8V signaling */ 952 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) { 953 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 954 ("Card supports 1.8V signaling\n")); 955 mmcp->card_features |= CARD_FEATURE_18V; 956 } 957 } else { 958 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 959 ("Card not ready: %08x\n", mmcio->cmd.resp[0])); 960 /* Send CMD55+ACMD41 once again */ 961 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND); 962 } 963 964 break; 965 } 966 case PROBE_GET_CID: /* XXX move to mmc_da */ 967 { 968 mmcio = &done_ccb->mmcio; 969 err = mmcio->cmd.error; 970 971 if (err != MMC_ERR_NONE) { 972 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 973 ("PROBE_GET_CID: error %d\n", err)); 974 PROBE_SET_ACTION(softc, PROBE_INVALID); 975 break; 976 } 977 978 struct mmc_params *mmcp = &path->device->mmc_ident_data; 979 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t)); 980 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 981 ("CID %08x%08x%08x%08x\n", 982 mmcp->card_cid[0], 983 mmcp->card_cid[1], 984 mmcp->card_cid[2], 985 mmcp->card_cid[3])); 986 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR); 987 break; 988 } 989 case PROBE_SEND_RELATIVE_ADDR: { 990 mmcio = &done_ccb->mmcio; 991 err = mmcio->cmd.error; 992 struct mmc_params *mmcp = &path->device->mmc_ident_data; 993 uint16_t rca = mmcio->cmd.resp[0] >> 16; 994 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 995 ("Card published RCA: %u\n", rca)); 996 path->device->mmc_ident_data.card_rca = rca; 997 if (err != MMC_ERR_NONE) { 998 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 999 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err)); 1000 PROBE_SET_ACTION(softc, PROBE_INVALID); 1001 break; 1002 } 1003 1004 /* If memory is present, get CSD, otherwise select card */ 1005 if (mmcp->card_features & CARD_FEATURE_MEMORY) 1006 PROBE_SET_ACTION(softc, PROBE_GET_CSD); 1007 else 1008 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD); 1009 break; 1010 } 1011 case PROBE_GET_CSD: { 1012 mmcio = &done_ccb->mmcio; 1013 err = mmcio->cmd.error; 1014 1015 if (err != MMC_ERR_NONE) { 1016 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 1017 ("PROBE_GET_CSD: error %d\n", err)); 1018 PROBE_SET_ACTION(softc, PROBE_INVALID); 1019 break; 1020 } 1021 1022 struct mmc_params *mmcp = &path->device->mmc_ident_data; 1023 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t)); 1024 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 1025 ("CSD %08x%08x%08x%08x\n", 1026 mmcp->card_csd[0], 1027 mmcp->card_csd[1], 1028 mmcp->card_csd[2], 1029 mmcp->card_csd[3])); 1030 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD); 1031 break; 1032 } 1033 case PROBE_SELECT_CARD: { 1034 mmcio = &done_ccb->mmcio; 1035 err = mmcio->cmd.error; 1036 if (err != MMC_ERR_NONE) { 1037 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 1038 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err)); 1039 PROBE_SET_ACTION(softc, PROBE_INVALID); 1040 break; 1041 } 1042 1043 PROBE_SET_ACTION(softc, PROBE_DONE); 1044 break; 1045 } 1046 default: 1047 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 1048 ("mmc_probedone: invalid action state 0x%x\n", softc->action)); 1049 panic("default: case in mmc_probe_done()"); 1050 } 1051 1052 if (softc->action == PROBE_INVALID && 1053 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1054 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, 1055 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n")); 1056 //xpt_async(AC_LOST_DEVICE, path, NULL); 1057 } 1058 1059 xpt_release_ccb(done_ccb); 1060 if (softc->action != PROBE_INVALID) 1061 xpt_schedule(periph, priority); 1062 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */ 1063 int frozen = cam_release_devq(path, 0, 0, 0, FALSE); 1064 printf("mmc_probedone: remaining freezecnt %d\n", frozen); 1065 1066 if (softc->action == PROBE_DONE) { 1067 /* Notify the system that the device is found! */ 1068 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 1069 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 1070 xpt_acquire_device(path->device); 1071 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1072 xpt_action(done_ccb); 1073 xpt_async(AC_FOUND_DEVICE, path, done_ccb); 1074 } 1075 } 1076 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) { 1077 cam_periph_invalidate(periph); 1078 cam_periph_release_locked(periph); 1079 } 1080 } 1081