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,
106 static char *probe_action_text[] = {
110 "PROBE_SEND_IF_COND",
113 "PROBE_SEND_APP_OP_COND",
116 "PROBE_SEND_RELATIVE_ADDR",
122 #define PROBE_SET_ACTION(softc, newaction) \
125 text = probe_action_text; \
126 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
127 ("Probe %s to %s\n", text[(softc)->action], \
128 text[(newaction)])); \
129 (softc)->action = (newaction); \
132 static struct xpt_xport_ops mmc_xport_ops = {
133 .alloc_device = mmc_alloc_device,
134 .action = mmc_action,
135 .async = mmc_dev_async,
136 .announce = mmc_announce_periph,
139 #define MMC_XPT_XPORT(x, X) \
140 static struct xpt_xport mmc_xport_ ## x = { \
141 .xport = XPORT_ ## X, \
143 .ops = &mmc_xport_ops, \
145 CAM_XPT_XPORT(mmc_xport_ ## x);
147 MMC_XPT_XPORT(mmc, MMCSD);
149 static struct xpt_proto_ops mmc_proto_ops = {
150 .announce = mmc_proto_announce,
151 .denounce = mmc_proto_denounce,
152 .debug_out = mmc_proto_debug_out,
155 static struct xpt_proto mmc_proto = {
156 .proto = PROTO_MMCSD,
158 .ops = &mmc_proto_ops,
160 CAM_XPT_PROTO(mmc_proto);
167 #define PROBE_FLAG_ACMD_SENT 0x1 /* CMD55 is sent, card expects ACMD */
168 uint8_t acmd41_count; /* how many times ACMD41 has been issued */
169 struct cam_periph *periph;
172 /* XPort functions -- an interface to CAM at periph side */
174 static struct cam_ed *
175 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
177 struct cam_ed *device;
179 printf("mmc_alloc_device()\n");
180 device = xpt_alloc_device(bus, target, lun_id);
184 device->quirk = NULL;
187 bzero(&device->inq_data, sizeof(device->inq_data));
188 device->inq_flags = 0;
189 device->queue_flags = 0;
190 device->serial_num = NULL;
191 device->serial_num_len = 0;
196 mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
197 struct cam_ed *device, void *async_arg)
200 printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
206 * We only need to handle events for real devices.
208 if (target->target_id == CAM_TARGET_WILDCARD
209 || device->lun_id == CAM_LUN_WILDCARD)
212 if (async_code == AC_LOST_DEVICE) {
213 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
214 printf("AC_LOST_DEVICE -> set to unconfigured\n");
215 device->flags |= CAM_DEV_UNCONFIGURED;
216 xpt_release_device(device);
218 printf("AC_LOST_DEVICE on unconfigured device\n");
220 } else if (async_code == AC_FOUND_DEVICE) {
221 printf("Got AC_FOUND_DEVICE -- whatever...\n");
222 } else if (async_code == AC_PATH_REGISTERED) {
223 printf("Got AC_PATH_REGISTERED -- whatever...\n");
224 } else if (async_code == AC_PATH_DEREGISTERED ) {
225 printf("Got AC_PATH_DEREGISTERED -- whatever...\n");
226 } else if (async_code == AC_UNIT_ATTENTION) {
227 printf("Got interrupt generated by the card and ignored it\n");
229 panic("Unknown async code\n");
232 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
234 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
235 cam_flags flags, union ccb *request_ccb)
237 struct ccb_pathinq cpi;
239 struct cam_periph *old_periph;
242 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
244 xpt_path_inq(&cpi, periph->path);
246 if (cpi.ccb_h.status != CAM_REQ_CMP) {
247 if (request_ccb != NULL) {
248 request_ccb->ccb_h.status = cpi.ccb_h.status;
249 xpt_done(request_ccb);
254 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
255 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
256 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
257 xpt_done(request_ccb);
261 lock = (xpt_path_owned(path) == 0);
265 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
266 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
267 // mmcprobe_softc *softc;
268 // softc = (mmcprobe_softc *)old_periph->softc;
269 // Not sure if we need request ccb queue for mmc
270 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
271 // &request_ccb->ccb_h, periph_links.tqe);
272 // softc->restart = 1;
273 CAM_DEBUG(path, CAM_DEBUG_INFO,
274 ("Got scan request, but mmcprobe already exists\n"));
275 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
276 xpt_done(request_ccb);
278 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
279 xpt_done(request_ccb);
282 xpt_print(path, " Set up the mmcprobe device...\n");
284 status = cam_periph_alloc(mmcprobe_register, NULL,
291 if (status != CAM_REQ_CMP) {
292 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
293 "returned an error, can't continue probe\n");
295 request_ccb->ccb_h.status = status;
296 xpt_done(request_ccb);
300 xpt_path_unlock(path);
304 mmc_action(union ccb *start_ccb)
306 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
307 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
308 xpt_action_name(start_ccb->ccb_h.func_code)));
309 switch (start_ccb->ccb_h.func_code) {
316 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
317 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
318 mmc_scan_lun(start_ccb->ccb_h.path->periph,
319 start_ccb->ccb_h.path, start_ccb->crcn.flags,
323 case XPT_DEV_ADVINFO:
325 mmc_dev_advinfo(start_ccb);
330 xpt_action_default(start_ccb);
336 mmc_dev_advinfo(union ccb *start_ccb)
338 struct cam_ed *device;
339 struct ccb_dev_advinfo *cdai;
342 start_ccb->ccb_h.status = CAM_REQ_INVALID;
343 device = start_ccb->ccb_h.path->device;
344 cdai = &start_ccb->cdai;
345 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
346 ("%s: request %x\n", __func__, cdai->buftype));
348 /* We don't support writing any data */
349 if (cdai->flags & CDAI_FLAG_STORE)
350 panic("Attempt to store data?!");
352 switch(cdai->buftype) {
353 case CDAI_TYPE_SCSI_DEVID:
354 cdai->provsiz = device->device_id_len;
355 if (device->device_id_len == 0)
357 amt = MIN(cdai->provsiz, cdai->bufsiz);
358 memcpy(cdai->buf, device->device_id, amt);
360 case CDAI_TYPE_SERIAL_NUM:
361 cdai->provsiz = device->serial_num_len;
362 if (device->serial_num_len == 0)
364 amt = MIN(cdai->provsiz, cdai->bufsiz);
365 memcpy(cdai->buf, device->serial_num, amt);
367 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
370 case CDAI_TYPE_MMC_PARAMS:
371 cdai->provsiz = sizeof(struct mmc_params);
372 amt = MIN(cdai->provsiz, cdai->bufsiz);
373 memcpy(cdai->buf, &device->mmc_ident_data, amt);
376 panic("Unknown buftype");
379 start_ccb->ccb_h.status = CAM_REQ_CMP;
383 mmc_announce_periph(struct cam_periph *periph)
385 struct ccb_pathinq cpi;
386 struct ccb_trans_settings cts;
387 struct cam_path *path = periph->path;
389 cam_periph_assert(periph, MA_OWNED);
391 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
392 ("mmc_announce_periph: called\n"));
394 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
395 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
396 cts.type = CTS_TYPE_CURRENT_SETTINGS;
397 xpt_action((union ccb*)&cts);
398 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
400 xpt_path_inq(&cpi, periph->path);
401 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
404 /* This func is called per attached device :-( */
406 mmc_print_ident(struct mmc_params *ident_data)
408 printf("Relative addr: %08x\n", ident_data->card_rca);
409 printf("Card features: <");
410 if (ident_data->card_features & CARD_FEATURE_MMC)
412 if (ident_data->card_features & CARD_FEATURE_MEMORY)
414 if (ident_data->card_features & CARD_FEATURE_SDHC)
415 printf("High-Capacity ");
416 if (ident_data->card_features & CARD_FEATURE_SD20)
417 printf("SD2.0-Conditions ");
418 if (ident_data->card_features & CARD_FEATURE_SDIO)
422 if (ident_data->card_features & CARD_FEATURE_MEMORY)
423 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
425 if (ident_data->card_features & CARD_FEATURE_SDIO) {
426 printf("Card IO OCR: %08x\n", ident_data->io_ocr);
427 printf("Number of funcitions: %u\n", ident_data->sdio_func_count);
432 mmc_proto_announce(struct cam_ed *device)
434 mmc_print_ident(&device->mmc_ident_data);
438 mmc_proto_denounce(struct cam_ed *device)
440 mmc_print_ident(&device->mmc_ident_data);
444 mmc_proto_debug_out(union ccb *ccb)
446 if (ccb->ccb_h.func_code != XPT_MMC_IO)
449 CAM_DEBUG(ccb->ccb_h.path,
450 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
453 static periph_init_t probe_periph_init;
455 static struct periph_driver probe_driver =
457 probe_periph_init, "mmcprobe",
458 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
462 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
464 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
472 mmcprobe_register(struct cam_periph *periph, void *arg)
474 mmcprobe_softc *softc;
475 union ccb *request_ccb; /* CCB representing the probe request */
478 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
480 request_ccb = (union ccb *)arg;
481 if (request_ccb == NULL) {
482 printf("mmcprobe_register: no probe CCB, "
483 "can't register device\n");
484 return(CAM_REQ_CMP_ERR);
487 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
490 printf("proberegister: Unable to probe new device. "
491 "Unable to allocate softc\n");
492 return(CAM_REQ_CMP_ERR);
496 softc->acmd41_count = 0;
497 periph->softc = softc;
498 softc->periph = periph;
499 softc->action = PROBE_INVALID;
501 status = cam_periph_acquire(periph);
503 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
505 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
507 return (CAM_REQ_CMP_ERR);
509 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
511 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
512 PROBE_SET_ACTION(softc, PROBE_RESET);
514 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
516 /* This will kick the ball */
517 xpt_schedule(periph, CAM_PRIORITY_XPT);
523 mmc_highest_voltage(uint32_t ocr)
527 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
528 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
535 init_standard_ccb(union ccb *ccb, uint32_t cmd)
537 ccb->ccb_h.func_code = cmd;
538 ccb->ccb_h.flags = CAM_DIR_OUT;
539 ccb->ccb_h.retry_count = 0;
540 ccb->ccb_h.timeout = 15 * 1000;
541 ccb->ccb_h.cbfcnp = mmcprobe_done;
545 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
547 mmcprobe_softc *softc;
548 struct cam_path *path;
549 struct ccb_mmcio *mmcio;
550 struct mtx *p_mtx = cam_periph_mtx(periph);
551 struct ccb_trans_settings_mmc *cts;
553 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
554 softc = (mmcprobe_softc *)periph->softc;
555 path = start_ccb->ccb_h.path;
556 mmcio = &start_ccb->mmcio;
557 cts = &start_ccb->cts.proto_specific.mmc;
558 struct mmc_params *mmcp = &path->device->mmc_ident_data;
560 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
562 if (softc->restart) {
564 if (path->device->flags & CAM_DEV_UNCONFIGURED)
565 softc->action = PROBE_RESET;
567 softc->action = PROBE_IDENTIFY;
571 /* Here is the place where the identify fun begins */
572 switch (softc->action) {
576 xpt_path_inq(&start_ccb->cpi, periph->path);
578 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
579 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
580 cts->ios.power_mode = power_off;
581 cts->ios_valid = MMC_PM;
582 xpt_action(start_ccb);
583 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
586 /* Get the host OCR */
587 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
588 xpt_action(start_ccb);
590 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
591 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
593 cts->ios.bus_mode = opendrain;
594 cts->ios.chip_select = cs_dontcare;
595 cts->ios.power_mode = power_up;
596 cts->ios.bus_width = bus_width_1;
598 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
599 MMC_CS | MMC_BW | MMC_CLK;
600 xpt_action(start_ccb);
601 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
603 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
604 cts->ios.power_mode = power_on;
605 cts->ios.clock = CARD_ID_FREQUENCY;
606 cts->ios.timing = bus_timing_normal;
607 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
608 xpt_action(start_ccb);
609 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
610 /* End for mmc_power_on */
612 /* Begin mmc_idle_cards() */
613 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
614 cts->ios.chip_select = cs_high;
615 cts->ios_valid = MMC_CS;
616 xpt_action(start_ccb);
617 mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
619 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
620 init_standard_ccb(start_ccb, XPT_MMC_IO);
621 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
623 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
624 mmcio->cmd.data = NULL;
625 mmcio->stop.opcode = 0;
627 /* XXX Reset I/O portion as well */
629 case PROBE_SDIO_RESET:
630 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
631 ("Start with PROBE_SDIO_RESET\n"));
632 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
633 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
634 cam_fill_mmcio(&start_ccb->mmcio,
636 /*cbfcnp*/ mmcprobe_done,
637 /*flags*/ CAM_DIR_NONE,
638 /*mmc_opcode*/ SD_IO_RW_DIRECT,
640 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
644 case PROBE_SEND_IF_COND:
645 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
646 ("Start with PROBE_SEND_IF_COND\n"));
647 init_standard_ccb(start_ccb, XPT_MMC_IO);
648 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
649 mmcio->cmd.arg = (1 << 8) + 0xAA;
650 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
651 mmcio->stop.opcode = 0;
654 case PROBE_SDIO_INIT:
655 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
656 ("Start with PROBE_SDIO_INIT\n"));
657 init_standard_ccb(start_ccb, XPT_MMC_IO);
658 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
659 mmcio->cmd.arg = mmcp->io_ocr;
660 mmcio->cmd.flags = MMC_RSP_R4;
661 mmcio->stop.opcode = 0;
665 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
666 ("Start with PROBE_MMC_INIT\n"));
667 init_standard_ccb(start_ccb, XPT_MMC_IO);
668 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
669 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
670 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
671 mmcio->stop.opcode = 0;
674 case PROBE_SEND_APP_OP_COND:
675 init_standard_ccb(start_ccb, XPT_MMC_IO);
676 if (softc->flags & PROBE_FLAG_ACMD_SENT) {
677 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
679 * We set CCS bit because we do support SDHC cards.
680 * XXX: Don't set CCS if no response to CMD8.
682 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
683 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
684 cmd_arg |= MMC_OCR_S18R;
685 mmcio->cmd.arg = cmd_arg;
686 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
687 softc->acmd41_count++;
689 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
690 mmcio->cmd.arg = 0; /* rca << 16 */
691 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
693 mmcio->stop.opcode = 0;
696 case PROBE_GET_CID: /* XXX move to mmc_da */
697 init_standard_ccb(start_ccb, XPT_MMC_IO);
698 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
700 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
701 mmcio->stop.opcode = 0;
704 case PROBE_SEND_RELATIVE_ADDR:
705 init_standard_ccb(start_ccb, XPT_MMC_IO);
706 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
708 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
709 mmcio->stop.opcode = 0;
711 case PROBE_SELECT_CARD:
712 init_standard_ccb(start_ccb, XPT_MMC_IO);
713 mmcio->cmd.opcode = MMC_SELECT_CARD;
714 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
715 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
716 mmcio->stop.opcode = 0;
718 case PROBE_GET_CSD: /* XXX move to mmc_da */
719 init_standard_ccb(start_ccb, XPT_MMC_IO);
720 mmcio->cmd.opcode = MMC_SEND_CSD;
721 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
722 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
723 mmcio->stop.opcode = 0;
726 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
727 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
728 cts->ios.bus_mode = pushpull;
729 cts->ios_valid = MMC_BM;
730 xpt_action(start_ccb);
737 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
738 panic("default: case in mmc_probe_start()");
741 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
742 xpt_action(start_ccb);
745 static void mmcprobe_cleanup(struct cam_periph *periph)
747 free(periph->softc, M_CAMXPT);
751 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
753 mmcprobe_softc *softc;
754 struct cam_path *path;
757 struct ccb_mmcio *mmcio;
760 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
761 softc = (mmcprobe_softc *)periph->softc;
762 path = done_ccb->ccb_h.path;
763 priority = done_ccb->ccb_h.pinfo.priority;
765 switch (softc->action) {
770 printf("Starting completion of PROBE_RESET\n");
771 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
772 mmcio = &done_ccb->mmcio;
773 err = mmcio->cmd.error;
775 if (err != MMC_ERR_NONE) {
776 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
777 ("GO_IDLE_STATE failed with error %d\n",
780 /* There was a device there, but now it's gone... */
781 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
782 xpt_async(AC_LOST_DEVICE, path, NULL);
784 PROBE_SET_ACTION(softc, PROBE_INVALID);
787 path->device->protocol = PROTO_MMCSD;
788 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
791 case PROBE_SEND_IF_COND:
793 mmcio = &done_ccb->mmcio;
794 err = mmcio->cmd.error;
795 struct mmc_params *mmcp = &path->device->mmc_ident_data;
797 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
798 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
799 ("IF_COND: error %d, pattern %08x\n",
800 err, mmcio->cmd.resp[0]));
802 mmcp->card_features |= CARD_FEATURE_SD20;
803 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
804 ("SD 2.0 interface conditions: OK\n"));
807 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
810 case PROBE_SDIO_RESET:
812 mmcio = &done_ccb->mmcio;
813 err = mmcio->cmd.error;
815 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
816 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
817 err, mmcio->cmd.resp[0]));
818 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
821 case PROBE_SDIO_INIT:
823 mmcio = &done_ccb->mmcio;
824 err = mmcio->cmd.error;
825 struct mmc_params *mmcp = &path->device->mmc_ident_data;
827 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
828 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
829 err, mmcio->cmd.resp[0],
832 mmcio->cmd.resp[3]));
835 * Error here means that this card is not SDIO,
836 * so proceed with memory init as if nothing has happened
838 if (err != MMC_ERR_NONE) {
839 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
842 mmcp->card_features |= CARD_FEATURE_SDIO;
843 uint32_t ioifcond = mmcio->cmd.resp[0];
844 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
846 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
847 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
848 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
850 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
851 ("SDIO OCR invalid?!\n"));
855 if (io_ocr != 0 && mmcp->io_ocr == 0) {
856 mmcp->io_ocr = io_ocr;
857 break; /* Retry, this time with non-0 OCR */
859 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
860 ("SDIO OCR: %08x\n", mmcp->io_ocr));
862 if (ioifcond & R4_IO_MEM_PRESENT) {
863 /* Combo card -- proceed to memory initialization */
864 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
866 /* No memory portion -- get RCA and select card */
867 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
873 mmcio = &done_ccb->mmcio;
874 err = mmcio->cmd.error;
875 struct mmc_params *mmcp = &path->device->mmc_ident_data;
877 if (err != MMC_ERR_NONE) {
878 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
879 ("MMC_INIT: error %d, resp %08x\n",
880 err, mmcio->cmd.resp[0]));
881 PROBE_SET_ACTION(softc, PROBE_INVALID);
884 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
885 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
887 if (mmcp->card_ocr == 0) {
888 /* We haven't sent the OCR to the card yet -- do it */
889 mmcp->card_ocr = mmcio->cmd.resp[0];
890 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
891 ("-> sending OCR to card\n"));
895 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
896 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
897 ("Card is still powering up\n"));
901 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
902 PROBE_SET_ACTION(softc, PROBE_GET_CID);
905 case PROBE_SEND_APP_OP_COND:
907 mmcio = &done_ccb->mmcio;
908 err = mmcio->cmd.error;
910 if (err != MMC_ERR_NONE) {
911 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
912 ("APP_OP_COND: error %d, resp %08x\n",
913 err, mmcio->cmd.resp[0]));
914 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
918 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
919 /* Don't change the state */
920 softc->flags |= PROBE_FLAG_ACMD_SENT;
924 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
925 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
926 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
927 struct mmc_params *mmcp = &path->device->mmc_ident_data;
928 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
929 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
930 if (mmcp->card_ocr == 0) {
931 mmcp->card_ocr = mmcio->cmd.resp[0];
932 /* Now when we know OCR that we want -- send it to card */
933 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
934 ("-> sending OCR to card\n"));
936 /* We already know the OCR and despite of that we
937 * are processing the answer to ACMD41 -> move on
939 PROBE_SET_ACTION(softc, PROBE_GET_CID);
941 /* Getting an answer to ACMD41 means the card has memory */
942 mmcp->card_features |= CARD_FEATURE_MEMORY;
944 /* Standard capacity vs High Capacity memory card */
945 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
946 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
948 mmcp->card_features |= CARD_FEATURE_SDHC;
951 /* Whether the card supports 1.8V signaling */
952 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
953 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
954 ("Card supports 1.8V signaling\n"));
955 mmcp->card_features |= CARD_FEATURE_18V;
958 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
959 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
960 /* Send CMD55+ACMD41 once again */
961 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
966 case PROBE_GET_CID: /* XXX move to mmc_da */
968 mmcio = &done_ccb->mmcio;
969 err = mmcio->cmd.error;
971 if (err != MMC_ERR_NONE) {
972 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
973 ("PROBE_GET_CID: error %d\n", err));
974 PROBE_SET_ACTION(softc, PROBE_INVALID);
978 struct mmc_params *mmcp = &path->device->mmc_ident_data;
979 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
980 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
981 ("CID %08x%08x%08x%08x\n",
986 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
989 case PROBE_SEND_RELATIVE_ADDR: {
990 mmcio = &done_ccb->mmcio;
991 err = mmcio->cmd.error;
992 struct mmc_params *mmcp = &path->device->mmc_ident_data;
993 uint16_t rca = mmcio->cmd.resp[0] >> 16;
994 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
995 ("Card published RCA: %u\n", rca));
996 path->device->mmc_ident_data.card_rca = rca;
997 if (err != MMC_ERR_NONE) {
998 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
999 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1000 PROBE_SET_ACTION(softc, PROBE_INVALID);
1004 /* If memory is present, get CSD, otherwise select card */
1005 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1006 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1008 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1011 case PROBE_GET_CSD: {
1012 mmcio = &done_ccb->mmcio;
1013 err = mmcio->cmd.error;
1015 if (err != MMC_ERR_NONE) {
1016 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1017 ("PROBE_GET_CSD: error %d\n", err));
1018 PROBE_SET_ACTION(softc, PROBE_INVALID);
1022 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1023 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1024 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1025 ("CSD %08x%08x%08x%08x\n",
1029 mmcp->card_csd[3]));
1030 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1033 case PROBE_SELECT_CARD: {
1034 mmcio = &done_ccb->mmcio;
1035 err = mmcio->cmd.error;
1036 if (err != MMC_ERR_NONE) {
1037 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1038 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1039 PROBE_SET_ACTION(softc, PROBE_INVALID);
1043 PROBE_SET_ACTION(softc, PROBE_DONE);
1047 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1048 ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1049 panic("default: case in mmc_probe_done()");
1052 if (softc->action == PROBE_INVALID &&
1053 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1054 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1055 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1056 //xpt_async(AC_LOST_DEVICE, path, NULL);
1059 xpt_release_ccb(done_ccb);
1060 if (softc->action != PROBE_INVALID)
1061 xpt_schedule(periph, priority);
1062 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1063 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1064 printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1066 if (softc->action == PROBE_DONE) {
1067 /* Notify the system that the device is found! */
1068 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1069 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1070 xpt_acquire_device(path->device);
1071 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1072 xpt_action(done_ccb);
1073 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1076 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1077 cam_periph_invalidate(periph);
1078 cam_periph_release_locked(periph);