2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.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>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/condvar.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>
61 #include <cam/mmc/mmc.h>
62 #include <cam/mmc/mmc_bus.h>
64 #include <machine/stdarg.h> /* for xpt_print below */
65 #include <machine/_inttypes.h> /* for PRIu64 */
68 FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");
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);
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);
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);
97 PROBE_SEND_APP_OP_COND,
100 PROBE_SEND_RELATIVE_ADDR,
101 PROBE_MMC_SET_RELATIVE_ADDR,
107 static char *probe_action_text[] = {
111 "PROBE_SEND_IF_COND",
114 "PROBE_SEND_APP_OP_COND",
117 "PROBE_SEND_RELATIVE_ADDR",
118 "PROBE_MMC_SET_RELATIVE_ADDR",
124 #define PROBE_SET_ACTION(softc, newaction) \
127 text = probe_action_text; \
128 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
129 ("Probe %s to %s\n", text[(softc)->action], \
130 text[(newaction)])); \
131 (softc)->action = (newaction); \
134 static struct xpt_xport_ops mmc_xport_ops = {
135 .alloc_device = mmc_alloc_device,
136 .action = mmc_action,
137 .async = mmc_dev_async,
138 .announce = mmc_announce_periph,
141 #define MMC_XPT_XPORT(x, X) \
142 static struct xpt_xport mmc_xport_ ## x = { \
143 .xport = XPORT_ ## X, \
145 .ops = &mmc_xport_ops, \
147 CAM_XPT_XPORT(mmc_xport_ ## x);
149 MMC_XPT_XPORT(mmc, MMCSD);
151 static struct xpt_proto_ops mmc_proto_ops = {
152 .announce = mmc_proto_announce,
153 .denounce = mmc_proto_denounce,
154 .debug_out = mmc_proto_debug_out,
157 static struct xpt_proto mmc_proto = {
158 .proto = PROTO_MMCSD,
160 .ops = &mmc_proto_ops,
162 CAM_XPT_PROTO(mmc_proto);
169 #define PROBE_FLAG_ACMD_SENT 0x1 /* CMD55 is sent, card expects ACMD */
170 #define PROBE_FLAG_HOST_CAN_DO_18V 0x2 /* Host can do 1.8V signaling */
171 uint8_t acmd41_count; /* how many times ACMD41 has been issued */
172 struct cam_periph *periph;
175 /* XPort functions -- an interface to CAM at periph side */
177 static struct cam_ed *
178 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
180 struct cam_ed *device;
182 device = xpt_alloc_device(bus, target, lun_id);
186 device->quirk = NULL;
189 bzero(&device->inq_data, sizeof(device->inq_data));
190 device->inq_flags = 0;
191 device->queue_flags = 0;
192 device->serial_num = NULL;
193 device->serial_num_len = 0;
198 mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
199 struct cam_ed *device, void *async_arg)
203 * We only need to handle events for real devices.
205 if (target->target_id == CAM_TARGET_WILDCARD
206 || device->lun_id == CAM_LUN_WILDCARD)
209 if (async_code == AC_LOST_DEVICE &&
210 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
211 device->flags |= CAM_DEV_UNCONFIGURED;
212 xpt_release_device(device);
216 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
218 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
219 cam_flags flags, union ccb *request_ccb)
221 struct ccb_pathinq cpi;
223 struct cam_periph *old_periph;
226 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
228 xpt_path_inq(&cpi, path);
230 if (cpi.ccb_h.status != CAM_REQ_CMP) {
231 if (request_ccb != NULL) {
232 request_ccb->ccb_h.status = cpi.ccb_h.status;
233 xpt_done(request_ccb);
238 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
239 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
240 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
241 xpt_done(request_ccb);
245 lock = (xpt_path_owned(path) == 0);
249 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
250 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
251 // mmcprobe_softc *softc;
252 // softc = (mmcprobe_softc *)old_periph->softc;
253 // Not sure if we need request ccb queue for mmc
254 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
255 // &request_ccb->ccb_h, periph_links.tqe);
256 // softc->restart = 1;
257 CAM_DEBUG(path, CAM_DEBUG_INFO,
258 ("Got scan request, but mmcprobe already exists\n"));
259 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
260 xpt_done(request_ccb);
262 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
263 xpt_done(request_ccb);
267 xpt_print(path, " Set up the mmcprobe device...\n");
269 status = cam_periph_alloc(mmcprobe_register, NULL,
276 if (status != CAM_REQ_CMP) {
277 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
278 "returned an error, can't continue probe\n");
280 request_ccb->ccb_h.status = status;
281 xpt_done(request_ccb);
285 xpt_path_unlock(path);
289 mmc_action(union ccb *start_ccb)
291 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
292 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
293 xpt_action_name(start_ccb->ccb_h.func_code)));
294 switch (start_ccb->ccb_h.func_code) {
300 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
301 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
302 mmc_scan_lun(start_ccb->ccb_h.path->periph,
303 start_ccb->ccb_h.path, start_ccb->crcn.flags,
307 case XPT_DEV_ADVINFO:
309 mmc_dev_advinfo(start_ccb);
314 xpt_action_default(start_ccb);
320 mmc_dev_advinfo(union ccb *start_ccb)
322 struct cam_ed *device;
323 struct ccb_dev_advinfo *cdai;
326 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
327 start_ccb->ccb_h.status = CAM_REQ_INVALID;
328 device = start_ccb->ccb_h.path->device;
329 cdai = &start_ccb->cdai;
330 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
331 ("%s: request %x\n", __func__, cdai->buftype));
333 /* We don't support writing any data */
334 if (cdai->flags & CDAI_FLAG_STORE)
335 panic("Attempt to store data?!");
337 switch(cdai->buftype) {
338 case CDAI_TYPE_SCSI_DEVID:
339 cdai->provsiz = device->device_id_len;
340 if (device->device_id_len == 0)
342 amt = MIN(cdai->provsiz, cdai->bufsiz);
343 memcpy(cdai->buf, device->device_id, amt);
345 case CDAI_TYPE_SERIAL_NUM:
346 cdai->provsiz = device->serial_num_len;
347 if (device->serial_num_len == 0)
349 amt = MIN(cdai->provsiz, cdai->bufsiz);
350 memcpy(cdai->buf, device->serial_num, amt);
352 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
355 case CDAI_TYPE_MMC_PARAMS:
356 cdai->provsiz = sizeof(struct mmc_params);
357 amt = MIN(cdai->provsiz, cdai->bufsiz);
358 memcpy(cdai->buf, &device->mmc_ident_data, amt);
361 panic("Unknown buftype");
364 start_ccb->ccb_h.status = CAM_REQ_CMP;
368 mmc_announce_periph(struct cam_periph *periph)
370 struct ccb_pathinq cpi;
371 struct ccb_trans_settings cts;
372 struct cam_path *path = periph->path;
374 cam_periph_assert(periph, MA_OWNED);
376 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmc_announce_periph"));
378 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
379 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
380 cts.type = CTS_TYPE_CURRENT_SETTINGS;
381 xpt_action((union ccb*)&cts);
382 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
384 xpt_path_inq(&cpi, periph->path);
385 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
389 mmccam_start_discovery(struct cam_sim *sim)
394 KASSERT(sim->sim_dev != NULL, ("mmccam_start_discovery(%s): sim_dev is not initialized,"
395 " has cam_sim_alloc_dev() been used?", cam_sim_name(sim)));
396 pathid = cam_sim_path(sim);
397 ccb = xpt_alloc_ccb();
400 * We create a rescan request for BUS:0:0, since the card
403 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
404 /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
411 /* This func is called per attached device :-( */
413 mmc_print_ident(struct mmc_params *ident_data, struct sbuf *sb)
417 sbuf_printf(sb, "Relative addr: %08x\n", ident_data->card_rca);
418 sbuf_printf(sb, "Card features: <");
419 if (ident_data->card_features & CARD_FEATURE_MMC) {
420 sbuf_printf(sb, "MMC");
423 if (ident_data->card_features & CARD_FEATURE_MEMORY) {
424 sbuf_printf(sb, "%sMemory", space ? " " : "");
427 if (ident_data->card_features & CARD_FEATURE_SDHC) {
428 sbuf_printf(sb, "%sHigh-Capacity", space ? " " : "");
431 if (ident_data->card_features & CARD_FEATURE_SD20) {
432 sbuf_printf(sb, "%sSD2.0-Conditions", space ? " " : "");
435 if (ident_data->card_features & CARD_FEATURE_SDIO) {
436 sbuf_printf(sb, "%sSDIO", space ? " " : "");
439 if (ident_data->card_features & CARD_FEATURE_18V) {
440 sbuf_printf(sb, "%s1.8-Signaling", space ? " " : "");
442 sbuf_printf(sb, ">\n");
444 if (ident_data->card_features & CARD_FEATURE_MEMORY)
445 sbuf_printf(sb, "Card memory OCR: %08x\n",
446 ident_data->card_ocr);
448 if (ident_data->card_features & CARD_FEATURE_SDIO) {
449 sbuf_printf(sb, "Card IO OCR: %08x\n", ident_data->io_ocr);
450 sbuf_printf(sb, "Number of functions: %u\n",
451 ident_data->sdio_func_count);
455 printf("%s", sbuf_data(sb));
460 mmc_proto_announce(struct cam_ed *device)
465 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
466 mmc_print_ident(&device->mmc_ident_data, &sb);
472 mmc_proto_denounce(struct cam_ed *device)
475 mmc_proto_announce(device);
479 mmc_proto_debug_out(union ccb *ccb)
481 if (ccb->ccb_h.func_code != XPT_MMC_IO)
484 CAM_DEBUG(ccb->ccb_h.path,
485 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
488 static periph_init_t probe_periph_init;
490 static struct periph_driver probe_driver =
492 probe_periph_init, "mmcprobe",
493 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
497 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
499 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
502 probe_periph_init(void)
507 mmcprobe_register(struct cam_periph *periph, void *arg)
509 mmcprobe_softc *softc;
510 union ccb *request_ccb; /* CCB representing the probe request */
513 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
515 request_ccb = (union ccb *)arg;
516 if (request_ccb == NULL) {
517 printf("mmcprobe_register: no probe CCB, "
518 "can't register device\n");
519 return(CAM_REQ_CMP_ERR);
522 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
525 printf("proberegister: Unable to probe new device. "
526 "Unable to allocate softc\n");
527 return(CAM_REQ_CMP_ERR);
531 softc->acmd41_count = 0;
532 periph->softc = softc;
533 softc->periph = periph;
534 softc->action = PROBE_INVALID;
536 status = cam_periph_acquire(periph);
538 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
540 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
542 return (CAM_REQ_CMP_ERR);
544 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
546 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
547 PROBE_SET_ACTION(softc, PROBE_RESET);
549 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
551 /* This will kick the ball */
552 xpt_schedule(periph, CAM_PRIORITY_XPT);
558 mmc_highest_voltage(uint32_t ocr)
562 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
563 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
570 init_standard_ccb(union ccb *ccb, uint32_t cmd)
572 ccb->ccb_h.func_code = cmd;
573 ccb->ccb_h.flags = CAM_DIR_OUT;
574 ccb->ccb_h.retry_count = 0;
575 ccb->ccb_h.timeout = 15 * 1000;
576 ccb->ccb_h.cbfcnp = mmcprobe_done;
580 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
582 mmcprobe_softc *softc;
583 struct cam_path *path;
584 struct ccb_mmcio *mmcio;
585 struct mtx *p_mtx = cam_periph_mtx(periph);
586 struct ccb_trans_settings_mmc *cts;
588 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
589 softc = (mmcprobe_softc *)periph->softc;
590 path = start_ccb->ccb_h.path;
591 mmcio = &start_ccb->mmcio;
592 cts = &start_ccb->cts.proto_specific.mmc;
593 struct mmc_params *mmcp = &path->device->mmc_ident_data;
595 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
597 if (softc->restart) {
599 if (path->device->flags & CAM_DEV_UNCONFIGURED)
600 softc->action = PROBE_RESET;
602 softc->action = PROBE_IDENTIFY;
605 /* Here is the place where the identify fun begins */
606 switch (softc->action) {
608 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
611 xpt_path_inq(&start_ccb->cpi, periph->path);
612 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_IDENTIFY\n"));
613 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
614 xpt_action(start_ccb);
615 if (cts->ios.power_mode != power_off) {
616 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
617 cts->ios.power_mode = power_off;
618 cts->ios_valid = MMC_PM;
619 xpt_action(start_ccb);
620 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
623 /* Get the host OCR */
624 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
625 xpt_action(start_ccb);
627 uint32_t host_caps = cts->host_caps;
628 if (host_caps & MMC_CAP_SIGNALING_180)
629 softc->flags |= PROBE_FLAG_HOST_CAN_DO_18V;
630 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
631 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
633 cts->ios.bus_mode = opendrain;
634 cts->ios.chip_select = cs_dontcare;
635 cts->ios.power_mode = power_up;
636 cts->ios.bus_width = bus_width_1;
638 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
639 MMC_CS | MMC_BW | MMC_CLK;
640 xpt_action(start_ccb);
641 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
643 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
644 cts->ios.power_mode = power_on;
645 cts->ios.clock = CARD_ID_FREQUENCY;
646 cts->ios.timing = bus_timing_normal;
647 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
648 xpt_action(start_ccb);
649 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
650 /* End for mmc_power_on */
652 /* Begin mmc_idle_cards() */
653 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
654 cts->ios.chip_select = cs_high;
655 cts->ios_valid = MMC_CS;
656 xpt_action(start_ccb);
657 mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
659 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
660 init_standard_ccb(start_ccb, XPT_MMC_IO);
661 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
663 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
664 mmcio->cmd.data = NULL;
665 mmcio->stop.opcode = 0;
667 /* XXX Reset I/O portion as well */
669 case PROBE_SDIO_RESET:
670 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
671 ("Start with PROBE_SDIO_RESET\n"));
672 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
673 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
674 cam_fill_mmcio(&start_ccb->mmcio,
676 /*cbfcnp*/ mmcprobe_done,
677 /*flags*/ CAM_DIR_NONE,
678 /*mmc_opcode*/ SD_IO_RW_DIRECT,
680 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
684 case PROBE_SEND_IF_COND:
685 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
686 ("Start with PROBE_SEND_IF_COND\n"));
687 init_standard_ccb(start_ccb, XPT_MMC_IO);
688 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
689 mmcio->cmd.arg = (1 << 8) + 0xAA;
690 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
691 mmcio->stop.opcode = 0;
694 case PROBE_SDIO_INIT:
695 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
696 ("Start with PROBE_SDIO_INIT\n"));
697 init_standard_ccb(start_ccb, XPT_MMC_IO);
698 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
699 mmcio->cmd.arg = mmcp->io_ocr;
700 mmcio->cmd.flags = MMC_RSP_R4;
701 mmcio->stop.opcode = 0;
705 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
706 ("Start with PROBE_MMC_INIT\n"));
707 init_standard_ccb(start_ccb, XPT_MMC_IO);
708 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
709 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
710 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
711 mmcio->stop.opcode = 0;
714 case PROBE_SEND_APP_OP_COND:
715 init_standard_ccb(start_ccb, XPT_MMC_IO);
716 if (softc->flags & PROBE_FLAG_ACMD_SENT) {
717 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
719 * We set CCS bit because we do support SDHC cards.
720 * XXX: Don't set CCS if no response to CMD8.
722 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
723 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
724 cmd_arg |= MMC_OCR_S18R;
725 mmcio->cmd.arg = cmd_arg;
726 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
727 softc->acmd41_count++;
729 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
730 mmcio->cmd.arg = 0; /* rca << 16 */
731 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
733 mmcio->stop.opcode = 0;
736 case PROBE_GET_CID: /* XXX move to mmc_da */
737 init_standard_ccb(start_ccb, XPT_MMC_IO);
738 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
740 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
741 mmcio->stop.opcode = 0;
743 case PROBE_SEND_RELATIVE_ADDR:
744 init_standard_ccb(start_ccb, XPT_MMC_IO);
745 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
747 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
748 mmcio->stop.opcode = 0;
750 case PROBE_MMC_SET_RELATIVE_ADDR:
751 init_standard_ccb(start_ccb, XPT_MMC_IO);
752 mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
753 mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
754 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
755 mmcio->stop.opcode = 0;
757 case PROBE_SELECT_CARD:
758 init_standard_ccb(start_ccb, XPT_MMC_IO);
759 mmcio->cmd.opcode = MMC_SELECT_CARD;
760 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
761 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
762 mmcio->stop.opcode = 0;
764 case PROBE_GET_CSD: /* XXX move to mmc_da */
765 init_standard_ccb(start_ccb, XPT_MMC_IO);
766 mmcio->cmd.opcode = MMC_SEND_CSD;
767 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
768 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
769 mmcio->stop.opcode = 0;
772 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
773 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
774 cts->ios.bus_mode = pushpull;
775 cts->ios_valid = MMC_BM;
776 xpt_action(start_ccb);
783 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
784 panic("default: case in mmc_probe_start()");
787 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
788 xpt_action(start_ccb);
791 static void mmcprobe_cleanup(struct cam_periph *periph)
793 free(periph->softc, M_CAMXPT);
797 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
799 mmcprobe_softc *softc;
800 struct cam_path *path;
803 struct ccb_mmcio *mmcio;
806 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mmcprobe_done\n"));
807 softc = (mmcprobe_softc *)periph->softc;
808 path = done_ccb->ccb_h.path;
809 priority = done_ccb->ccb_h.pinfo.priority;
811 switch (softc->action) {
816 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
817 mmcio = &done_ccb->mmcio;
818 err = mmcio->cmd.error;
820 if (err != MMC_ERR_NONE) {
821 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
822 ("GO_IDLE_STATE failed with error %d\n",
825 /* There was a device there, but now it's gone... */
826 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
827 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
830 xpt_async(AC_LOST_DEVICE, path, NULL);
832 PROBE_SET_ACTION(softc, PROBE_INVALID);
835 path->device->protocol = PROTO_MMCSD;
836 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
839 case PROBE_SEND_IF_COND:
841 mmcio = &done_ccb->mmcio;
842 err = mmcio->cmd.error;
843 struct mmc_params *mmcp = &path->device->mmc_ident_data;
845 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
846 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
847 ("IF_COND: error %d, pattern %08x\n",
848 err, mmcio->cmd.resp[0]));
850 mmcp->card_features |= CARD_FEATURE_SD20;
851 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
852 ("SD 2.0 interface conditions: OK\n"));
854 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
857 case PROBE_SDIO_RESET:
859 mmcio = &done_ccb->mmcio;
860 err = mmcio->cmd.error;
862 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
863 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
864 err, mmcio->cmd.resp[0]));
865 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
868 case PROBE_SDIO_INIT:
870 mmcio = &done_ccb->mmcio;
871 err = mmcio->cmd.error;
872 struct mmc_params *mmcp = &path->device->mmc_ident_data;
874 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
875 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
876 err, mmcio->cmd.resp[0],
879 mmcio->cmd.resp[3]));
882 * Error here means that this card is not SDIO,
883 * so proceed with memory init as if nothing has happened
885 if (err != MMC_ERR_NONE) {
886 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
889 mmcp->card_features |= CARD_FEATURE_SDIO;
890 uint32_t ioifcond = mmcio->cmd.resp[0];
891 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
893 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
894 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
895 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
897 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
898 ("SDIO OCR invalid, retrying\n"));
902 if (io_ocr != 0 && mmcp->io_ocr == 0) {
903 mmcp->io_ocr = io_ocr;
904 break; /* Retry, this time with non-0 OCR */
906 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
907 ("SDIO OCR: %08x\n", mmcp->io_ocr));
909 if (ioifcond & R4_IO_MEM_PRESENT) {
910 /* Combo card -- proceed to memory initialization */
911 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
913 /* No memory portion -- get RCA and select card */
914 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
920 mmcio = &done_ccb->mmcio;
921 err = mmcio->cmd.error;
922 struct mmc_params *mmcp = &path->device->mmc_ident_data;
924 if (err != MMC_ERR_NONE) {
925 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
926 ("MMC_INIT: error %d, resp %08x\n",
927 err, mmcio->cmd.resp[0]));
928 PROBE_SET_ACTION(softc, PROBE_INVALID);
931 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
932 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
934 if (mmcp->card_ocr == 0) {
935 /* We haven't sent the OCR to the card yet -- do it */
936 mmcp->card_ocr = mmcio->cmd.resp[0];
937 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
938 ("-> sending OCR to card\n"));
942 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
943 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
944 ("Card is still powering up\n"));
948 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
949 PROBE_SET_ACTION(softc, PROBE_GET_CID);
952 case PROBE_SEND_APP_OP_COND:
954 mmcio = &done_ccb->mmcio;
955 err = mmcio->cmd.error;
957 if (err != MMC_ERR_NONE) {
958 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
959 ("APP_OP_COND: error %d, resp %08x\n",
960 err, mmcio->cmd.resp[0]));
961 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
965 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
966 /* Don't change the state */
967 softc->flags |= PROBE_FLAG_ACMD_SENT;
971 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
972 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
973 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
974 struct mmc_params *mmcp = &path->device->mmc_ident_data;
975 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
976 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
977 if (mmcp->card_ocr == 0) {
978 mmcp->card_ocr = mmcio->cmd.resp[0];
979 /* Now when we know OCR that we want -- send it to card */
980 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
981 ("-> sending OCR to card\n"));
983 /* We already know the OCR and despite of that we
984 * are processing the answer to ACMD41 -> move on
986 PROBE_SET_ACTION(softc, PROBE_GET_CID);
988 /* Getting an answer to ACMD41 means the card has memory */
989 mmcp->card_features |= CARD_FEATURE_MEMORY;
991 /* Standard capacity vs High Capacity memory card */
992 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
993 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
995 mmcp->card_features |= CARD_FEATURE_SDHC;
998 /* Whether the card supports 1.8V signaling */
999 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
1000 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1001 ("Card supports 1.8V signaling\n"));
1002 mmcp->card_features |= CARD_FEATURE_18V;
1003 if (softc->flags & PROBE_FLAG_HOST_CAN_DO_18V) {
1004 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1005 ("Host supports 1.8V signaling. Switch voltage!\n"));
1006 done_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1007 done_ccb->ccb_h.flags = CAM_DIR_NONE;
1008 done_ccb->ccb_h.retry_count = 0;
1009 done_ccb->ccb_h.timeout = 100;
1010 done_ccb->ccb_h.cbfcnp = NULL;
1011 done_ccb->cts.proto_specific.mmc.ios.vccq = vccq_180;
1012 done_ccb->cts.proto_specific.mmc.ios_valid = MMC_VCCQ;
1013 xpt_action(done_ccb);
1017 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1018 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
1019 /* Send CMD55+ACMD41 once again */
1020 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
1025 case PROBE_GET_CID: /* XXX move to mmc_da */
1027 mmcio = &done_ccb->mmcio;
1028 err = mmcio->cmd.error;
1030 if (err != MMC_ERR_NONE) {
1031 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1032 ("PROBE_GET_CID: error %d\n", err));
1033 PROBE_SET_ACTION(softc, PROBE_INVALID);
1037 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1038 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1039 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1040 ("CID %08x%08x%08x%08x\n",
1044 mmcp->card_cid[3]));
1045 if (mmcp->card_features & CARD_FEATURE_MMC)
1046 PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
1048 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1051 case PROBE_SEND_RELATIVE_ADDR: {
1052 mmcio = &done_ccb->mmcio;
1053 err = mmcio->cmd.error;
1054 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1055 uint16_t rca = mmcio->cmd.resp[0] >> 16;
1056 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1057 ("Card published RCA: %u\n", rca));
1058 path->device->mmc_ident_data.card_rca = rca;
1059 if (err != MMC_ERR_NONE) {
1060 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1061 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1062 PROBE_SET_ACTION(softc, PROBE_INVALID);
1066 /* If memory is present, get CSD, otherwise select card */
1067 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1068 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1070 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1073 case PROBE_MMC_SET_RELATIVE_ADDR:
1074 mmcio = &done_ccb->mmcio;
1075 err = mmcio->cmd.error;
1076 if (err != MMC_ERR_NONE) {
1077 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1078 ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1079 PROBE_SET_ACTION(softc, PROBE_INVALID);
1082 path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1083 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1085 case PROBE_GET_CSD: {
1086 mmcio = &done_ccb->mmcio;
1087 err = mmcio->cmd.error;
1089 if (err != MMC_ERR_NONE) {
1090 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1091 ("PROBE_GET_CSD: error %d\n", err));
1092 PROBE_SET_ACTION(softc, PROBE_INVALID);
1096 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1097 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1098 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1099 ("CSD %08x%08x%08x%08x\n",
1103 mmcp->card_csd[3]));
1104 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1107 case PROBE_SELECT_CARD: {
1108 mmcio = &done_ccb->mmcio;
1109 err = mmcio->cmd.error;
1110 if (err != MMC_ERR_NONE) {
1111 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1112 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1113 PROBE_SET_ACTION(softc, PROBE_INVALID);
1117 PROBE_SET_ACTION(softc, PROBE_DONE);
1121 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1122 ("mmcprobe_done: invalid action state 0x%x\n", softc->action));
1123 panic("default: case in mmc_probe_done()");
1126 if (softc->action == PROBE_INVALID &&
1127 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1128 xpt_async(AC_LOST_DEVICE, path, NULL);
1131 if (softc->action != PROBE_INVALID)
1132 xpt_schedule(periph, priority);
1133 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1134 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1135 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1136 ("mmcprobe_done: remaining freeze count %d\n", frozen));
1138 if (softc->action == PROBE_DONE) {
1139 /* Notify the system that the device is found! */
1140 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1141 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1142 xpt_acquire_device(path->device);
1143 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1144 xpt_action(done_ccb);
1145 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1148 xpt_release_ccb(done_ccb);
1149 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1150 cam_periph_invalidate(periph);
1151 cam_periph_release_locked(periph);
1156 mmc_path_inq(struct ccb_pathinq *cpi, const char *hba,
1157 const struct cam_sim *sim, size_t maxio)
1160 cpi->version_num = 1;
1161 cpi->hba_inquiry = 0;
1162 cpi->target_sprt = 0;
1163 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
1164 cpi->hba_eng_cnt = 0;
1165 cpi->max_target = 0;
1167 cpi->initiator_id = 1;
1169 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1170 strncpy(cpi->hba_vid, hba, HBA_IDLEN);
1171 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1172 cpi->unit_number = cam_sim_unit(sim);
1173 cpi->bus_id = cam_sim_bus(sim);
1174 cpi->protocol = PROTO_MMCSD;
1175 cpi->protocol_version = SCSI_REV_0;
1176 cpi->transport = XPORT_MMCSD;
1177 cpi->transport_version = 1;
1179 cpi->base_transfer_speed = 100; /* XXX WTF? */
1181 cpi->ccb_h.status = CAM_REQ_CMP;