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 uint8_t acmd41_count; /* how many times ACMD41 has been issued */
171 struct cam_periph *periph;
174 /* XPort functions -- an interface to CAM at periph side */
176 static struct cam_ed *
177 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
179 struct cam_ed *device;
181 printf("mmc_alloc_device()\n");
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)
202 printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
208 * We only need to handle events for real devices.
210 if (target->target_id == CAM_TARGET_WILDCARD
211 || device->lun_id == CAM_LUN_WILDCARD)
214 if (async_code == AC_LOST_DEVICE) {
215 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
216 printf("AC_LOST_DEVICE -> set to unconfigured\n");
217 device->flags |= CAM_DEV_UNCONFIGURED;
218 xpt_release_device(device);
220 printf("AC_LOST_DEVICE on unconfigured device\n");
222 } else if (async_code == AC_FOUND_DEVICE) {
223 printf("Got AC_FOUND_DEVICE -- whatever...\n");
224 } else if (async_code == AC_PATH_REGISTERED) {
225 printf("Got AC_PATH_REGISTERED -- whatever...\n");
226 } else if (async_code == AC_PATH_DEREGISTERED ) {
227 printf("Got AC_PATH_DEREGISTERED -- whatever...\n");
228 } else if (async_code == AC_UNIT_ATTENTION) {
229 printf("Got interrupt generated by the card and ignored it\n");
231 panic("Unknown async code\n");
234 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
236 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
237 cam_flags flags, union ccb *request_ccb)
239 struct ccb_pathinq cpi;
241 struct cam_periph *old_periph;
244 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
246 xpt_path_inq(&cpi, path);
248 if (cpi.ccb_h.status != CAM_REQ_CMP) {
249 if (request_ccb != NULL) {
250 request_ccb->ccb_h.status = cpi.ccb_h.status;
251 xpt_done(request_ccb);
256 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
257 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
258 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
259 xpt_done(request_ccb);
263 lock = (xpt_path_owned(path) == 0);
267 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
268 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
269 // mmcprobe_softc *softc;
270 // softc = (mmcprobe_softc *)old_periph->softc;
271 // Not sure if we need request ccb queue for mmc
272 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
273 // &request_ccb->ccb_h, periph_links.tqe);
274 // softc->restart = 1;
275 CAM_DEBUG(path, CAM_DEBUG_INFO,
276 ("Got scan request, but mmcprobe already exists\n"));
277 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
278 xpt_done(request_ccb);
280 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
281 xpt_done(request_ccb);
284 xpt_print(path, " Set up the mmcprobe device...\n");
286 status = cam_periph_alloc(mmcprobe_register, NULL,
293 if (status != CAM_REQ_CMP) {
294 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
295 "returned an error, can't continue probe\n");
297 request_ccb->ccb_h.status = status;
298 xpt_done(request_ccb);
302 xpt_path_unlock(path);
306 mmc_action(union ccb *start_ccb)
308 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
309 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
310 xpt_action_name(start_ccb->ccb_h.func_code)));
311 switch (start_ccb->ccb_h.func_code) {
318 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
319 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
320 mmc_scan_lun(start_ccb->ccb_h.path->periph,
321 start_ccb->ccb_h.path, start_ccb->crcn.flags,
325 case XPT_DEV_ADVINFO:
327 mmc_dev_advinfo(start_ccb);
332 xpt_action_default(start_ccb);
338 mmc_dev_advinfo(union ccb *start_ccb)
340 struct cam_ed *device;
341 struct ccb_dev_advinfo *cdai;
344 start_ccb->ccb_h.status = CAM_REQ_INVALID;
345 device = start_ccb->ccb_h.path->device;
346 cdai = &start_ccb->cdai;
347 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
348 ("%s: request %x\n", __func__, cdai->buftype));
350 /* We don't support writing any data */
351 if (cdai->flags & CDAI_FLAG_STORE)
352 panic("Attempt to store data?!");
354 switch(cdai->buftype) {
355 case CDAI_TYPE_SCSI_DEVID:
356 cdai->provsiz = device->device_id_len;
357 if (device->device_id_len == 0)
359 amt = MIN(cdai->provsiz, cdai->bufsiz);
360 memcpy(cdai->buf, device->device_id, amt);
362 case CDAI_TYPE_SERIAL_NUM:
363 cdai->provsiz = device->serial_num_len;
364 if (device->serial_num_len == 0)
366 amt = MIN(cdai->provsiz, cdai->bufsiz);
367 memcpy(cdai->buf, device->serial_num, amt);
369 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
372 case CDAI_TYPE_MMC_PARAMS:
373 cdai->provsiz = sizeof(struct mmc_params);
374 amt = MIN(cdai->provsiz, cdai->bufsiz);
375 memcpy(cdai->buf, &device->mmc_ident_data, amt);
378 panic("Unknown buftype");
381 start_ccb->ccb_h.status = CAM_REQ_CMP;
385 mmc_announce_periph(struct cam_periph *periph)
387 struct ccb_pathinq cpi;
388 struct ccb_trans_settings cts;
389 struct cam_path *path = periph->path;
391 cam_periph_assert(periph, MA_OWNED);
393 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
394 ("mmc_announce_periph: called\n"));
396 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
397 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
398 cts.type = CTS_TYPE_CURRENT_SETTINGS;
399 xpt_action((union ccb*)&cts);
400 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
402 xpt_path_inq(&cpi, periph->path);
403 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
406 /* This func is called per attached device :-( */
408 mmc_print_ident(struct mmc_params *ident_data)
410 printf("Relative addr: %08x\n", ident_data->card_rca);
411 printf("Card features: <");
412 if (ident_data->card_features & CARD_FEATURE_MMC)
414 if (ident_data->card_features & CARD_FEATURE_MEMORY)
416 if (ident_data->card_features & CARD_FEATURE_SDHC)
417 printf("High-Capacity ");
418 if (ident_data->card_features & CARD_FEATURE_SD20)
419 printf("SD2.0-Conditions ");
420 if (ident_data->card_features & CARD_FEATURE_SDIO)
424 if (ident_data->card_features & CARD_FEATURE_MEMORY)
425 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
427 if (ident_data->card_features & CARD_FEATURE_SDIO) {
428 printf("Card IO OCR: %08x\n", ident_data->io_ocr);
429 printf("Number of funcitions: %u\n", ident_data->sdio_func_count);
434 mmc_proto_announce(struct cam_ed *device)
436 mmc_print_ident(&device->mmc_ident_data);
440 mmc_proto_denounce(struct cam_ed *device)
442 mmc_print_ident(&device->mmc_ident_data);
446 mmc_proto_debug_out(union ccb *ccb)
448 if (ccb->ccb_h.func_code != XPT_MMC_IO)
451 CAM_DEBUG(ccb->ccb_h.path,
452 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
455 static periph_init_t probe_periph_init;
457 static struct periph_driver probe_driver =
459 probe_periph_init, "mmcprobe",
460 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
464 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
466 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
474 mmcprobe_register(struct cam_periph *periph, void *arg)
476 mmcprobe_softc *softc;
477 union ccb *request_ccb; /* CCB representing the probe request */
480 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
482 request_ccb = (union ccb *)arg;
483 if (request_ccb == NULL) {
484 printf("mmcprobe_register: no probe CCB, "
485 "can't register device\n");
486 return(CAM_REQ_CMP_ERR);
489 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
492 printf("proberegister: Unable to probe new device. "
493 "Unable to allocate softc\n");
494 return(CAM_REQ_CMP_ERR);
498 softc->acmd41_count = 0;
499 periph->softc = softc;
500 softc->periph = periph;
501 softc->action = PROBE_INVALID;
503 status = cam_periph_acquire(periph);
505 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
507 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
509 return (CAM_REQ_CMP_ERR);
511 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
513 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
514 PROBE_SET_ACTION(softc, PROBE_RESET);
516 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
518 /* This will kick the ball */
519 xpt_schedule(periph, CAM_PRIORITY_XPT);
525 mmc_highest_voltage(uint32_t ocr)
529 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
530 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
537 init_standard_ccb(union ccb *ccb, uint32_t cmd)
539 ccb->ccb_h.func_code = cmd;
540 ccb->ccb_h.flags = CAM_DIR_OUT;
541 ccb->ccb_h.retry_count = 0;
542 ccb->ccb_h.timeout = 15 * 1000;
543 ccb->ccb_h.cbfcnp = mmcprobe_done;
547 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
549 mmcprobe_softc *softc;
550 struct cam_path *path;
551 struct ccb_mmcio *mmcio;
552 struct mtx *p_mtx = cam_periph_mtx(periph);
553 struct ccb_trans_settings_mmc *cts;
555 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
556 softc = (mmcprobe_softc *)periph->softc;
557 path = start_ccb->ccb_h.path;
558 mmcio = &start_ccb->mmcio;
559 cts = &start_ccb->cts.proto_specific.mmc;
560 struct mmc_params *mmcp = &path->device->mmc_ident_data;
562 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
564 if (softc->restart) {
566 if (path->device->flags & CAM_DEV_UNCONFIGURED)
567 softc->action = PROBE_RESET;
569 softc->action = PROBE_IDENTIFY;
573 /* Here is the place where the identify fun begins */
574 switch (softc->action) {
578 xpt_path_inq(&start_ccb->cpi, periph->path);
579 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
580 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
581 xpt_action(start_ccb);
582 if (cts->ios.power_mode != power_off) {
583 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
584 cts->ios.power_mode = power_off;
585 cts->ios_valid = MMC_PM;
586 xpt_action(start_ccb);
587 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
590 /* Get the host OCR */
591 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
592 xpt_action(start_ccb);
594 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
595 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
597 cts->ios.bus_mode = opendrain;
598 cts->ios.chip_select = cs_dontcare;
599 cts->ios.power_mode = power_up;
600 cts->ios.bus_width = bus_width_1;
602 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
603 MMC_CS | MMC_BW | MMC_CLK;
604 xpt_action(start_ccb);
605 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
607 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
608 cts->ios.power_mode = power_on;
609 cts->ios.clock = CARD_ID_FREQUENCY;
610 cts->ios.timing = bus_timing_normal;
611 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
612 xpt_action(start_ccb);
613 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
614 /* End for mmc_power_on */
616 /* Begin mmc_idle_cards() */
617 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
618 cts->ios.chip_select = cs_high;
619 cts->ios_valid = MMC_CS;
620 xpt_action(start_ccb);
621 mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
623 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
624 init_standard_ccb(start_ccb, XPT_MMC_IO);
625 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
627 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
628 mmcio->cmd.data = NULL;
629 mmcio->stop.opcode = 0;
631 /* XXX Reset I/O portion as well */
633 case PROBE_SDIO_RESET:
634 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
635 ("Start with PROBE_SDIO_RESET\n"));
636 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
637 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
638 cam_fill_mmcio(&start_ccb->mmcio,
640 /*cbfcnp*/ mmcprobe_done,
641 /*flags*/ CAM_DIR_NONE,
642 /*mmc_opcode*/ SD_IO_RW_DIRECT,
644 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
648 case PROBE_SEND_IF_COND:
649 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
650 ("Start with PROBE_SEND_IF_COND\n"));
651 init_standard_ccb(start_ccb, XPT_MMC_IO);
652 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
653 mmcio->cmd.arg = (1 << 8) + 0xAA;
654 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
655 mmcio->stop.opcode = 0;
658 case PROBE_SDIO_INIT:
659 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
660 ("Start with PROBE_SDIO_INIT\n"));
661 init_standard_ccb(start_ccb, XPT_MMC_IO);
662 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
663 mmcio->cmd.arg = mmcp->io_ocr;
664 mmcio->cmd.flags = MMC_RSP_R4;
665 mmcio->stop.opcode = 0;
669 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
670 ("Start with PROBE_MMC_INIT\n"));
671 init_standard_ccb(start_ccb, XPT_MMC_IO);
672 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
673 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
674 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
675 mmcio->stop.opcode = 0;
678 case PROBE_SEND_APP_OP_COND:
679 init_standard_ccb(start_ccb, XPT_MMC_IO);
680 if (softc->flags & PROBE_FLAG_ACMD_SENT) {
681 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
683 * We set CCS bit because we do support SDHC cards.
684 * XXX: Don't set CCS if no response to CMD8.
686 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
687 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
688 cmd_arg |= MMC_OCR_S18R;
689 mmcio->cmd.arg = cmd_arg;
690 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
691 softc->acmd41_count++;
693 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
694 mmcio->cmd.arg = 0; /* rca << 16 */
695 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
697 mmcio->stop.opcode = 0;
700 case PROBE_GET_CID: /* XXX move to mmc_da */
701 init_standard_ccb(start_ccb, XPT_MMC_IO);
702 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
704 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
705 mmcio->stop.opcode = 0;
707 case PROBE_SEND_RELATIVE_ADDR:
708 init_standard_ccb(start_ccb, XPT_MMC_IO);
709 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
711 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
712 mmcio->stop.opcode = 0;
714 case PROBE_MMC_SET_RELATIVE_ADDR:
715 init_standard_ccb(start_ccb, XPT_MMC_IO);
716 mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
717 mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
718 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
719 mmcio->stop.opcode = 0;
721 case PROBE_SELECT_CARD:
722 init_standard_ccb(start_ccb, XPT_MMC_IO);
723 mmcio->cmd.opcode = MMC_SELECT_CARD;
724 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
725 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
726 mmcio->stop.opcode = 0;
728 case PROBE_GET_CSD: /* XXX move to mmc_da */
729 init_standard_ccb(start_ccb, XPT_MMC_IO);
730 mmcio->cmd.opcode = MMC_SEND_CSD;
731 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
732 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
733 mmcio->stop.opcode = 0;
736 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
737 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
738 cts->ios.bus_mode = pushpull;
739 cts->ios_valid = MMC_BM;
740 xpt_action(start_ccb);
747 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
748 panic("default: case in mmc_probe_start()");
751 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
752 xpt_action(start_ccb);
755 static void mmcprobe_cleanup(struct cam_periph *periph)
757 free(periph->softc, M_CAMXPT);
761 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
763 mmcprobe_softc *softc;
764 struct cam_path *path;
767 struct ccb_mmcio *mmcio;
770 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
771 softc = (mmcprobe_softc *)periph->softc;
772 path = done_ccb->ccb_h.path;
773 priority = done_ccb->ccb_h.pinfo.priority;
775 switch (softc->action) {
780 printf("Starting completion of PROBE_RESET\n");
781 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
782 mmcio = &done_ccb->mmcio;
783 err = mmcio->cmd.error;
785 if (err != MMC_ERR_NONE) {
786 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
787 ("GO_IDLE_STATE failed with error %d\n",
790 /* There was a device there, but now it's gone... */
791 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
792 xpt_async(AC_LOST_DEVICE, path, NULL);
794 PROBE_SET_ACTION(softc, PROBE_INVALID);
797 path->device->protocol = PROTO_MMCSD;
798 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
801 case PROBE_SEND_IF_COND:
803 mmcio = &done_ccb->mmcio;
804 err = mmcio->cmd.error;
805 struct mmc_params *mmcp = &path->device->mmc_ident_data;
807 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
808 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
809 ("IF_COND: error %d, pattern %08x\n",
810 err, mmcio->cmd.resp[0]));
812 mmcp->card_features |= CARD_FEATURE_SD20;
813 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
814 ("SD 2.0 interface conditions: OK\n"));
817 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
820 case PROBE_SDIO_RESET:
822 mmcio = &done_ccb->mmcio;
823 err = mmcio->cmd.error;
825 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
826 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
827 err, mmcio->cmd.resp[0]));
828 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
831 case PROBE_SDIO_INIT:
833 mmcio = &done_ccb->mmcio;
834 err = mmcio->cmd.error;
835 struct mmc_params *mmcp = &path->device->mmc_ident_data;
837 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
838 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
839 err, mmcio->cmd.resp[0],
842 mmcio->cmd.resp[3]));
845 * Error here means that this card is not SDIO,
846 * so proceed with memory init as if nothing has happened
848 if (err != MMC_ERR_NONE) {
849 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
852 mmcp->card_features |= CARD_FEATURE_SDIO;
853 uint32_t ioifcond = mmcio->cmd.resp[0];
854 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
856 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
857 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
858 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
860 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
861 ("SDIO OCR invalid?!\n"));
865 if (io_ocr != 0 && mmcp->io_ocr == 0) {
866 mmcp->io_ocr = io_ocr;
867 break; /* Retry, this time with non-0 OCR */
869 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
870 ("SDIO OCR: %08x\n", mmcp->io_ocr));
872 if (ioifcond & R4_IO_MEM_PRESENT) {
873 /* Combo card -- proceed to memory initialization */
874 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
876 /* No memory portion -- get RCA and select card */
877 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
883 mmcio = &done_ccb->mmcio;
884 err = mmcio->cmd.error;
885 struct mmc_params *mmcp = &path->device->mmc_ident_data;
887 if (err != MMC_ERR_NONE) {
888 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
889 ("MMC_INIT: error %d, resp %08x\n",
890 err, mmcio->cmd.resp[0]));
891 PROBE_SET_ACTION(softc, PROBE_INVALID);
894 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
895 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
897 if (mmcp->card_ocr == 0) {
898 /* We haven't sent the OCR to the card yet -- do it */
899 mmcp->card_ocr = mmcio->cmd.resp[0];
900 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
901 ("-> sending OCR to card\n"));
905 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
906 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
907 ("Card is still powering up\n"));
911 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
912 PROBE_SET_ACTION(softc, PROBE_GET_CID);
915 case PROBE_SEND_APP_OP_COND:
917 mmcio = &done_ccb->mmcio;
918 err = mmcio->cmd.error;
920 if (err != MMC_ERR_NONE) {
921 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
922 ("APP_OP_COND: error %d, resp %08x\n",
923 err, mmcio->cmd.resp[0]));
924 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
928 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
929 /* Don't change the state */
930 softc->flags |= PROBE_FLAG_ACMD_SENT;
934 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
935 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
936 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
937 struct mmc_params *mmcp = &path->device->mmc_ident_data;
938 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
939 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
940 if (mmcp->card_ocr == 0) {
941 mmcp->card_ocr = mmcio->cmd.resp[0];
942 /* Now when we know OCR that we want -- send it to card */
943 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
944 ("-> sending OCR to card\n"));
946 /* We already know the OCR and despite of that we
947 * are processing the answer to ACMD41 -> move on
949 PROBE_SET_ACTION(softc, PROBE_GET_CID);
951 /* Getting an answer to ACMD41 means the card has memory */
952 mmcp->card_features |= CARD_FEATURE_MEMORY;
954 /* Standard capacity vs High Capacity memory card */
955 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
956 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
958 mmcp->card_features |= CARD_FEATURE_SDHC;
961 /* Whether the card supports 1.8V signaling */
962 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
963 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
964 ("Card supports 1.8V signaling\n"));
965 mmcp->card_features |= CARD_FEATURE_18V;
968 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
969 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
970 /* Send CMD55+ACMD41 once again */
971 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
976 case PROBE_GET_CID: /* XXX move to mmc_da */
978 mmcio = &done_ccb->mmcio;
979 err = mmcio->cmd.error;
981 if (err != MMC_ERR_NONE) {
982 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
983 ("PROBE_GET_CID: error %d\n", err));
984 PROBE_SET_ACTION(softc, PROBE_INVALID);
988 struct mmc_params *mmcp = &path->device->mmc_ident_data;
989 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
990 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
991 ("CID %08x%08x%08x%08x\n",
996 if (mmcp->card_features & CARD_FEATURE_MMC)
997 PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
999 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1002 case PROBE_SEND_RELATIVE_ADDR: {
1003 mmcio = &done_ccb->mmcio;
1004 err = mmcio->cmd.error;
1005 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1006 uint16_t rca = mmcio->cmd.resp[0] >> 16;
1007 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1008 ("Card published RCA: %u\n", rca));
1009 path->device->mmc_ident_data.card_rca = rca;
1010 if (err != MMC_ERR_NONE) {
1011 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1012 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1013 PROBE_SET_ACTION(softc, PROBE_INVALID);
1017 /* If memory is present, get CSD, otherwise select card */
1018 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1019 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1021 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1024 case PROBE_MMC_SET_RELATIVE_ADDR:
1025 mmcio = &done_ccb->mmcio;
1026 err = mmcio->cmd.error;
1027 if (err != MMC_ERR_NONE) {
1028 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1029 ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1030 PROBE_SET_ACTION(softc, PROBE_INVALID);
1033 path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1034 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1036 case PROBE_GET_CSD: {
1037 mmcio = &done_ccb->mmcio;
1038 err = mmcio->cmd.error;
1040 if (err != MMC_ERR_NONE) {
1041 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1042 ("PROBE_GET_CSD: error %d\n", err));
1043 PROBE_SET_ACTION(softc, PROBE_INVALID);
1047 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1048 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1049 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1050 ("CSD %08x%08x%08x%08x\n",
1054 mmcp->card_csd[3]));
1055 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1058 case PROBE_SELECT_CARD: {
1059 mmcio = &done_ccb->mmcio;
1060 err = mmcio->cmd.error;
1061 if (err != MMC_ERR_NONE) {
1062 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1063 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1064 PROBE_SET_ACTION(softc, PROBE_INVALID);
1068 PROBE_SET_ACTION(softc, PROBE_DONE);
1072 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1073 ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1074 panic("default: case in mmc_probe_done()");
1077 if (softc->action == PROBE_INVALID &&
1078 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1079 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1080 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1081 //xpt_async(AC_LOST_DEVICE, path, NULL);
1084 xpt_release_ccb(done_ccb);
1085 if (softc->action != PROBE_INVALID)
1086 xpt_schedule(periph, priority);
1087 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1088 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1089 printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1091 if (softc->action == PROBE_DONE) {
1092 /* Notify the system that the device is found! */
1093 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1094 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1095 xpt_acquire_device(path->device);
1096 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1097 xpt_action(done_ccb);
1098 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1101 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1102 cam_periph_invalidate(periph);
1103 cam_periph_release_locked(periph);