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