2 * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.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>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
46 #include <sys/condvar.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>
59 #include <cam/mmc/mmc.h>
60 #include <cam/mmc/mmc_bus.h>
62 #include <machine/stdarg.h> /* for xpt_print below */
63 #include <machine/_inttypes.h> /* for PRIu64 */
66 FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");
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);
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);
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);
95 PROBE_SEND_APP_OP_COND,
98 PROBE_SEND_RELATIVE_ADDR,
104 static char *probe_action_text[] = {
108 "PROBE_SEND_IF_COND",
111 "PROBE_SEND_APP_OP_COND",
114 "PROBE_SEND_RELATIVE_ADDR",
120 #define PROBE_SET_ACTION(softc, newaction) \
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); \
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,
137 #define MMC_XPT_XPORT(x, X) \
138 static struct xpt_xport mmc_xport_ ## x = { \
139 .xport = XPORT_ ## X, \
141 .ops = &mmc_xport_ops, \
143 CAM_XPT_XPORT(mmc_xport_ ## x);
145 MMC_XPT_XPORT(mmc, MMCSD);
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,
153 static struct xpt_proto mmc_proto = {
154 .proto = PROTO_MMCSD,
156 .ops = &mmc_proto_ops,
158 CAM_XPT_PROTO(mmc_proto);
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;
170 /* XPort functions -- an interface to CAM at periph side */
172 static struct cam_ed *
173 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
175 struct cam_ed *device;
177 printf("mmc_alloc_device()\n");
178 device = xpt_alloc_device(bus, target, lun_id);
182 device->quirk = NULL;
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;
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)
198 printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
204 * We only need to handle events for real devices.
206 if (target->target_id == CAM_TARGET_WILDCARD
207 || device->lun_id == CAM_LUN_WILDCARD)
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);
216 printf("AC_LOST_DEVICE on unconfigured device\n");
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");
227 panic("Unknown async code\n");
230 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
232 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
233 cam_flags flags, union ccb *request_ccb)
235 struct ccb_pathinq cpi;
237 struct cam_periph *old_periph;
240 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
242 xpt_path_inq(&cpi, periph->path);
244 if (cpi.ccb_h.status != CAM_REQ_CMP) {
245 if (request_ccb != NULL) {
246 request_ccb->ccb_h.status = cpi.ccb_h.status;
247 xpt_done(request_ccb);
252 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
253 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
254 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
255 xpt_done(request_ccb);
259 lock = (xpt_path_owned(path) == 0);
263 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
264 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
265 // mmcprobe_softc *softc;
266 // softc = (mmcprobe_softc *)old_periph->softc;
267 // Not sure if we need request ccb queue for mmc
268 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
269 // &request_ccb->ccb_h, periph_links.tqe);
270 // softc->restart = 1;
271 CAM_DEBUG(path, CAM_DEBUG_INFO,
272 ("Got scan request, but mmcprobe already exists\n"));
273 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
274 xpt_done(request_ccb);
276 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
277 xpt_done(request_ccb);
280 xpt_print(path, " Set up the mmcprobe device...\n");
282 status = cam_periph_alloc(mmcprobe_register, NULL,
289 if (status != CAM_REQ_CMP) {
290 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
291 "returned an error, can't continue probe\n");
293 request_ccb->ccb_h.status = status;
294 xpt_done(request_ccb);
298 xpt_path_unlock(path);
302 mmc_action(union ccb *start_ccb)
304 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
305 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
306 xpt_action_name(start_ccb->ccb_h.func_code)));
307 switch (start_ccb->ccb_h.func_code) {
314 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
315 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
316 mmc_scan_lun(start_ccb->ccb_h.path->periph,
317 start_ccb->ccb_h.path, start_ccb->crcn.flags,
321 case XPT_DEV_ADVINFO:
323 mmc_dev_advinfo(start_ccb);
328 xpt_action_default(start_ccb);
334 mmc_dev_advinfo(union ccb *start_ccb)
336 struct cam_ed *device;
337 struct ccb_dev_advinfo *cdai;
340 start_ccb->ccb_h.status = CAM_REQ_INVALID;
341 device = start_ccb->ccb_h.path->device;
342 cdai = &start_ccb->cdai;
343 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
344 ("%s: request %x\n", __func__, cdai->buftype));
346 /* We don't support writing any data */
347 if (cdai->flags & CDAI_FLAG_STORE)
348 panic("Attempt to store data?!");
350 switch(cdai->buftype) {
351 case CDAI_TYPE_SCSI_DEVID:
352 cdai->provsiz = device->device_id_len;
353 if (device->device_id_len == 0)
355 amt = MIN(cdai->provsiz, cdai->bufsiz);
356 memcpy(cdai->buf, device->device_id, amt);
358 case CDAI_TYPE_SERIAL_NUM:
359 cdai->provsiz = device->serial_num_len;
360 if (device->serial_num_len == 0)
362 amt = MIN(cdai->provsiz, cdai->bufsiz);
363 memcpy(cdai->buf, device->serial_num, amt);
365 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
369 panic("Unknown buftype");
372 start_ccb->ccb_h.status = CAM_REQ_CMP;
376 mmc_announce_periph(struct cam_periph *periph)
378 struct ccb_pathinq cpi;
379 struct ccb_trans_settings cts;
380 struct cam_path *path = periph->path;
382 cam_periph_assert(periph, MA_OWNED);
384 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
385 ("mmc_announce_periph: called\n"));
387 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
388 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
389 cts.type = CTS_TYPE_CURRENT_SETTINGS;
390 xpt_action((union ccb*)&cts);
391 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
393 xpt_path_inq(&cpi, periph->path);
394 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
397 /* This func is called per attached device :-( */
399 mmc_print_ident(struct mmc_params *ident_data)
401 printf("Relative addr: %08x\n", ident_data->card_rca);
402 printf("Card features: <");
403 if (ident_data->card_features & CARD_FEATURE_MMC)
405 if (ident_data->card_features & CARD_FEATURE_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)
415 if (ident_data->card_features & CARD_FEATURE_MEMORY)
416 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
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);
425 mmc_proto_announce(struct cam_ed *device)
427 mmc_print_ident(&device->mmc_ident_data);
431 mmc_proto_denounce(struct cam_ed *device)
433 mmc_print_ident(&device->mmc_ident_data);
437 mmc_proto_debug_out(union ccb *ccb)
439 if (ccb->ccb_h.func_code != XPT_MMC_IO)
442 CAM_DEBUG(ccb->ccb_h.path,
443 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
446 static periph_init_t probe_periph_init;
448 static struct periph_driver probe_driver =
450 probe_periph_init, "mmcprobe",
451 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
455 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
457 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
465 mmcprobe_register(struct cam_periph *periph, void *arg)
467 union ccb *request_ccb; /* CCB representing the probe request */
469 mmcprobe_softc *softc;
471 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
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);
480 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
483 printf("proberegister: Unable to probe new device. "
484 "Unable to allocate softc\n");
485 return(CAM_REQ_CMP_ERR);
489 softc->acmd41_count = 0;
490 periph->softc = softc;
491 softc->periph = periph;
492 softc->action = PROBE_INVALID;
494 status = cam_periph_acquire(periph);
496 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
497 if (status != CAM_REQ_CMP) {
498 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
502 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
504 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
505 PROBE_SET_ACTION(softc, PROBE_RESET);
507 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
509 /* This will kick the ball */
510 xpt_schedule(periph, CAM_PRIORITY_XPT);
516 mmc_highest_voltage(uint32_t ocr)
520 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
521 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
528 init_standard_ccb(union ccb *ccb, uint32_t cmd)
530 ccb->ccb_h.func_code = cmd;
531 ccb->ccb_h.flags = CAM_DIR_OUT;
532 ccb->ccb_h.retry_count = 0;
533 ccb->ccb_h.timeout = 15 * 1000;
534 ccb->ccb_h.cbfcnp = mmcprobe_done;
538 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
540 mmcprobe_softc *softc;
541 struct cam_path *path;
542 struct ccb_mmcio *mmcio;
543 struct mtx *p_mtx = cam_periph_mtx(periph);
544 struct ccb_trans_settings_mmc *cts;
546 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
547 softc = (mmcprobe_softc *)periph->softc;
548 path = start_ccb->ccb_h.path;
549 mmcio = &start_ccb->mmcio;
550 cts = &start_ccb->cts.proto_specific.mmc;
551 struct mmc_params *mmcp = &path->device->mmc_ident_data;
553 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
555 if (softc->restart) {
557 if (path->device->flags & CAM_DEV_UNCONFIGURED)
558 softc->action = PROBE_RESET;
560 softc->action = PROBE_IDENTIFY;
564 /* Here is the place where the identify fun begins */
565 switch (softc->action) {
569 xpt_path_inq(&start_ccb->cpi, periph->path);
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);
579 /* Get the host OCR */
580 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
581 xpt_action(start_ccb);
583 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
584 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
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;
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);
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 */
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);
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 */
616 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
617 mmcio->cmd.data = NULL;
618 mmcio->stop.opcode = 0;
620 /* XXX Reset I/O portion as well */
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,
629 /*cbfcnp*/ mmcprobe_done,
630 /*flags*/ CAM_DIR_NONE,
631 /*mmc_opcode*/ SD_IO_RW_DIRECT,
633 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
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;
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;
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;
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 */
672 * We set CCS bit because we do support SDHC cards.
673 * XXX: Don't set CCS if no response to CMD8.
675 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
676 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
677 cmd_arg |= MMC_OCR_S18R;
678 mmcio->cmd.arg = cmd_arg;
679 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
680 softc->acmd41_count++;
682 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
683 mmcio->cmd.arg = 0; /* rca << 16 */
684 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
686 mmcio->stop.opcode = 0;
689 case PROBE_GET_CID: /* XXX move to mmc_da */
690 init_standard_ccb(start_ccb, XPT_MMC_IO);
691 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
693 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
694 mmcio->stop.opcode = 0;
697 case PROBE_SEND_RELATIVE_ADDR:
698 init_standard_ccb(start_ccb, XPT_MMC_IO);
699 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
701 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
702 mmcio->stop.opcode = 0;
704 case PROBE_SELECT_CARD:
705 init_standard_ccb(start_ccb, XPT_MMC_IO);
706 mmcio->cmd.opcode = MMC_SELECT_CARD;
707 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
708 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
709 mmcio->stop.opcode = 0;
711 case PROBE_GET_CSD: /* XXX move to mmc_da */
712 init_standard_ccb(start_ccb, XPT_MMC_IO);
713 mmcio->cmd.opcode = MMC_SEND_CSD;
714 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
715 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
716 mmcio->stop.opcode = 0;
719 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
720 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
721 cts->ios.bus_mode = pushpull;
722 cts->ios_valid = MMC_BM;
723 xpt_action(start_ccb);
730 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
731 panic("default: case in mmc_probe_start()");
734 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
735 xpt_action(start_ccb);
738 static void mmcprobe_cleanup(struct cam_periph *periph)
740 free(periph->softc, M_CAMXPT);
744 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
746 mmcprobe_softc *softc;
747 struct cam_path *path;
750 struct ccb_mmcio *mmcio;
753 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
754 softc = (mmcprobe_softc *)periph->softc;
755 path = done_ccb->ccb_h.path;
756 priority = done_ccb->ccb_h.pinfo.priority;
758 switch (softc->action) {
763 printf("Starting completion of PROBE_RESET\n");
764 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
765 mmcio = &done_ccb->mmcio;
766 err = mmcio->cmd.error;
768 if (err != MMC_ERR_NONE) {
769 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
770 ("GO_IDLE_STATE failed with error %d\n",
773 /* There was a device there, but now it's gone... */
774 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
775 xpt_async(AC_LOST_DEVICE, path, NULL);
777 PROBE_SET_ACTION(softc, PROBE_INVALID);
780 path->device->protocol = PROTO_MMCSD;
781 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
784 case PROBE_SEND_IF_COND:
786 mmcio = &done_ccb->mmcio;
787 err = mmcio->cmd.error;
788 struct mmc_params *mmcp = &path->device->mmc_ident_data;
790 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
791 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
792 ("IF_COND: error %d, pattern %08x\n",
793 err, mmcio->cmd.resp[0]));
795 mmcp->card_features |= CARD_FEATURE_SD20;
796 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
797 ("SD 2.0 interface conditions: OK\n"));
800 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
803 case PROBE_SDIO_RESET:
805 mmcio = &done_ccb->mmcio;
806 err = mmcio->cmd.error;
808 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
809 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
810 err, mmcio->cmd.resp[0]));
811 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
814 case PROBE_SDIO_INIT:
816 mmcio = &done_ccb->mmcio;
817 err = mmcio->cmd.error;
818 struct mmc_params *mmcp = &path->device->mmc_ident_data;
820 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
821 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
822 err, mmcio->cmd.resp[0],
825 mmcio->cmd.resp[3]));
828 * Error here means that this card is not SDIO,
829 * so proceed with memory init as if nothing has happened
831 if (err != MMC_ERR_NONE) {
832 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
835 mmcp->card_features |= CARD_FEATURE_SDIO;
836 uint32_t ioifcond = mmcio->cmd.resp[0];
837 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
839 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
840 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
841 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
843 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
844 ("SDIO OCR invalid?!\n"));
848 if (io_ocr != 0 && mmcp->io_ocr == 0) {
849 mmcp->io_ocr = io_ocr;
850 break; /* Retry, this time with non-0 OCR */
852 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
853 ("SDIO OCR: %08x\n", mmcp->io_ocr));
855 if (ioifcond & R4_IO_MEM_PRESENT) {
856 /* Combo card -- proceed to memory initialization */
857 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
859 /* No memory portion -- get RCA and select card */
860 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
866 mmcio = &done_ccb->mmcio;
867 err = mmcio->cmd.error;
868 struct mmc_params *mmcp = &path->device->mmc_ident_data;
870 if (err != MMC_ERR_NONE) {
871 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
872 ("MMC_INIT: error %d, resp %08x\n",
873 err, mmcio->cmd.resp[0]));
874 PROBE_SET_ACTION(softc, PROBE_INVALID);
877 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
878 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
880 if (mmcp->card_ocr == 0) {
881 /* We haven't sent the OCR to the card yet -- do it */
882 mmcp->card_ocr = mmcio->cmd.resp[0];
883 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
884 ("-> sending OCR to card\n"));
888 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
889 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
890 ("Card is still powering up\n"));
894 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
895 PROBE_SET_ACTION(softc, PROBE_GET_CID);
898 case PROBE_SEND_APP_OP_COND:
900 mmcio = &done_ccb->mmcio;
901 err = mmcio->cmd.error;
903 if (err != MMC_ERR_NONE) {
904 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
905 ("APP_OP_COND: error %d, resp %08x\n",
906 err, mmcio->cmd.resp[0]));
907 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
911 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
912 /* Don't change the state */
913 softc->flags |= PROBE_FLAG_ACMD_SENT;
917 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
918 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
919 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
920 struct mmc_params *mmcp = &path->device->mmc_ident_data;
921 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
922 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
923 if (mmcp->card_ocr == 0) {
924 mmcp->card_ocr = mmcio->cmd.resp[0];
925 /* Now when we know OCR that we want -- send it to card */
926 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
927 ("-> sending OCR to card\n"));
929 /* We already know the OCR and despite of that we
930 * are processing the answer to ACMD41 -> move on
932 PROBE_SET_ACTION(softc, PROBE_GET_CID);
934 /* Getting an answer to ACMD41 means the card has memory */
935 mmcp->card_features |= CARD_FEATURE_MEMORY;
937 /* Standard capacity vs High Capacity memory card */
938 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
939 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
941 mmcp->card_features |= CARD_FEATURE_SDHC;
944 /* Whether the card supports 1.8V signaling */
945 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
946 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
947 ("Card supports 1.8V signaling\n"));
948 mmcp->card_features |= CARD_FEATURE_18V;
951 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
952 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
953 /* Send CMD55+ACMD41 once again */
954 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
959 case PROBE_GET_CID: /* XXX move to mmc_da */
961 mmcio = &done_ccb->mmcio;
962 err = mmcio->cmd.error;
964 if (err != MMC_ERR_NONE) {
965 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
966 ("PROBE_GET_CID: error %d\n", err));
967 PROBE_SET_ACTION(softc, PROBE_INVALID);
971 struct mmc_params *mmcp = &path->device->mmc_ident_data;
972 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
973 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
974 ("CID %08x%08x%08x%08x\n",
979 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
982 case PROBE_SEND_RELATIVE_ADDR: {
983 mmcio = &done_ccb->mmcio;
984 err = mmcio->cmd.error;
985 struct mmc_params *mmcp = &path->device->mmc_ident_data;
986 uint16_t rca = mmcio->cmd.resp[0] >> 16;
987 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
988 ("Card published RCA: %u\n", rca));
989 path->device->mmc_ident_data.card_rca = rca;
990 if (err != MMC_ERR_NONE) {
991 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
992 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
993 PROBE_SET_ACTION(softc, PROBE_INVALID);
997 /* If memory is present, get CSD, otherwise select card */
998 if (mmcp->card_features & CARD_FEATURE_MEMORY)
999 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1001 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1004 case PROBE_GET_CSD: {
1005 mmcio = &done_ccb->mmcio;
1006 err = mmcio->cmd.error;
1008 if (err != MMC_ERR_NONE) {
1009 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1010 ("PROBE_GET_CSD: error %d\n", err));
1011 PROBE_SET_ACTION(softc, PROBE_INVALID);
1015 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1016 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1017 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1018 ("CSD %08x%08x%08x%08x\n",
1022 mmcp->card_csd[3]));
1023 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1026 case PROBE_SELECT_CARD: {
1027 mmcio = &done_ccb->mmcio;
1028 err = mmcio->cmd.error;
1029 if (err != MMC_ERR_NONE) {
1030 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1031 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1032 PROBE_SET_ACTION(softc, PROBE_INVALID);
1036 PROBE_SET_ACTION(softc, PROBE_DONE);
1040 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1041 ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1042 panic("default: case in mmc_probe_done()");
1045 if (softc->action == PROBE_INVALID &&
1046 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1047 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1048 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1049 //xpt_async(AC_LOST_DEVICE, path, NULL);
1052 xpt_release_ccb(done_ccb);
1053 if (softc->action != PROBE_INVALID)
1054 xpt_schedule(periph, priority);
1055 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1056 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1057 printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1059 if (softc->action == PROBE_DONE) {
1060 /* Notify the system that the device is found! */
1061 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1062 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1063 xpt_acquire_device(path->device);
1064 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1065 xpt_action(done_ccb);
1066 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1069 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1070 cam_periph_invalidate(periph);
1071 cam_periph_release_locked(periph);