Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/interrupt.h>
#include <sys/sbuf.h>

#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#include <sys/condvar.h>

#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_queue.h>
#include <cam/cam_periph.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_xpt_periph.h>
#include <cam/cam_xpt_internal.h>
#include <cam/cam_debug.h>

#include <cam/mmc/mmc.h>
#include <cam/mmc/mmc_bus.h>

#include <machine/stdarg.h>	/* for xpt_print below */
#include <machine/_inttypes.h>  /* for PRIu64 */
#include "opt_cam.h"

FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");

static struct cam_ed * mmc_alloc_device(struct cam_eb *bus,
    struct cam_et *target, lun_id_t lun_id);
static void mmc_dev_async(u_int32_t async_code, struct cam_eb *bus,
    struct cam_et *target, struct cam_ed *device, void *async_arg);
static void	 mmc_action(union ccb *start_ccb);
static void	 mmc_dev_advinfo(union ccb *start_ccb);
static void	 mmc_announce_periph(struct cam_periph *periph);
static void	 mmc_scan_lun(struct cam_periph *periph,
    struct cam_path *path, cam_flags flags, union ccb *ccb);

/* mmcprobe methods */
static cam_status mmcprobe_register(struct cam_periph *periph, void *arg);
static void	 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb);
static void	 mmcprobe_cleanup(struct cam_periph *periph);
static void	 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb);

static void mmc_proto_announce(struct cam_ed *device);
static void mmc_proto_denounce(struct cam_ed *device);
static void mmc_proto_debug_out(union ccb *ccb);

typedef enum {
	PROBE_RESET,
	PROBE_IDENTIFY,
        PROBE_SDIO_RESET,
	PROBE_SEND_IF_COND,
        PROBE_SDIO_INIT,
        PROBE_MMC_INIT,
	PROBE_SEND_APP_OP_COND,
        PROBE_GET_CID,
        PROBE_GET_CSD,
        PROBE_SEND_RELATIVE_ADDR,
	PROBE_MMC_SET_RELATIVE_ADDR,
	PROBE_SELECT_CARD,
	PROBE_DONE,
	PROBE_INVALID
} probe_action;

static char *probe_action_text[] = {
	"PROBE_RESET",
	"PROBE_IDENTIFY",
        "PROBE_SDIO_RESET",
	"PROBE_SEND_IF_COND",
        "PROBE_SDIO_INIT",
        "PROBE_MMC_INIT",
	"PROBE_SEND_APP_OP_COND",
        "PROBE_GET_CID",
        "PROBE_GET_CSD",
        "PROBE_SEND_RELATIVE_ADDR",
	"PROBE_MMC_SET_RELATIVE_ADDR",
        "PROBE_SELECT_CARD",
	"PROBE_DONE",
	"PROBE_INVALID"
};

#define PROBE_SET_ACTION(softc, newaction)	\
do {									\
	char **text;							\
	text = probe_action_text;					\
	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
	    ("Probe %s to %s\n", text[(softc)->action],			\
	    text[(newaction)]));					\
	(softc)->action = (newaction);					\
} while(0)

static struct xpt_xport_ops mmc_xport_ops = {
	.alloc_device = mmc_alloc_device,
	.action = mmc_action,
	.async = mmc_dev_async,
	.announce = mmc_announce_periph,
};

#define MMC_XPT_XPORT(x, X)				\
	static struct xpt_xport mmc_xport_ ## x = {	\
		.xport = XPORT_ ## X,			\
		.name = #x,				\
		.ops = &mmc_xport_ops,			\
	};						\
	CAM_XPT_XPORT(mmc_xport_ ## x);

MMC_XPT_XPORT(mmc, MMCSD);

static struct xpt_proto_ops mmc_proto_ops = {
	.announce = mmc_proto_announce,
	.denounce = mmc_proto_denounce,
	.debug_out = mmc_proto_debug_out,
};

static struct xpt_proto mmc_proto = {
	.proto = PROTO_MMCSD,
	.name = "mmcsd",
	.ops = &mmc_proto_ops,
};
CAM_XPT_PROTO(mmc_proto);

typedef struct {
	probe_action	action;
	int             restart;
	union ccb	saved_ccb;
	uint32_t	flags;
#define PROBE_FLAG_ACMD_SENT	0x1 /* CMD55 is sent, card expects ACMD */
#define PROBE_FLAG_HOST_CAN_DO_18V   0x2 /* Host can do 1.8V signaling */
	uint8_t         acmd41_count; /* how many times ACMD41 has been issued */
	struct cam_periph *periph;
} mmcprobe_softc;

/* XPort functions -- an interface to CAM at periph side */

static struct cam_ed *
mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
{
	struct cam_ed *device;

	device = xpt_alloc_device(bus, target, lun_id);
	if (device == NULL)
		return (NULL);

	device->quirk = NULL;
	device->mintags = 0;
	device->maxtags = 0;
	bzero(&device->inq_data, sizeof(device->inq_data));
	device->inq_flags = 0;
	device->queue_flags = 0;
	device->serial_num = NULL;
	device->serial_num_len = 0;
	return (device);
}

static void
mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
	      struct cam_ed *device, void *async_arg)
{

	/*
	 * We only need to handle events for real devices.
	 */
	if (target->target_id == CAM_TARGET_WILDCARD
            || device->lun_id == CAM_LUN_WILDCARD)
		return;

	if (async_code == AC_LOST_DEVICE &&
	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
		device->flags |= CAM_DEV_UNCONFIGURED;
		xpt_release_device(device);
	}
}

/* Taken from nvme_scan_lun, thanks to bsdimp@ */
static void
mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
	     cam_flags flags, union ccb *request_ccb)
{
	struct ccb_pathinq cpi;
	cam_status status;
	struct cam_periph *old_periph;
	int lock;

	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));

	xpt_path_inq(&cpi, path);

	if (cpi.ccb_h.status != CAM_REQ_CMP) {
		if (request_ccb != NULL) {
			request_ccb->ccb_h.status = cpi.ccb_h.status;
			xpt_done(request_ccb);
		}
		return;
	}

	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
		xpt_done(request_ccb);
		return;
	}

	lock = (xpt_path_owned(path) == 0);
	if (lock)
		xpt_path_lock(path);

	if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
//			mmcprobe_softc *softc;
//			softc = (mmcprobe_softc *)old_periph->softc;
//                      Not sure if we need request ccb queue for mmc
//			TAILQ_INSERT_TAIL(&softc->request_ccbs,
//				&request_ccb->ccb_h, periph_links.tqe);
//			softc->restart = 1;
                        CAM_DEBUG(path, CAM_DEBUG_INFO,
                                  ("Got scan request, but mmcprobe already exists\n"));
			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
                        xpt_done(request_ccb);
		} else {
			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
			xpt_done(request_ccb);
		}
	} else {
		if (bootverbose)
			xpt_print(path, " Set up the mmcprobe device...\n");

                status = cam_periph_alloc(mmcprobe_register, NULL,
					  mmcprobe_cleanup,
					  mmcprobe_start,
					  "mmcprobe",
					  CAM_PERIPH_BIO,
					  path, NULL, 0,
					  request_ccb);
                if (status != CAM_REQ_CMP) {
			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
                                  "returned an error, can't continue probe\n");
		}
		request_ccb->ccb_h.status = status;
		xpt_done(request_ccb);
	}

	if (lock)
		xpt_path_unlock(path);
}

static void
mmc_action(union ccb *start_ccb)
{
	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
		  ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
		   xpt_action_name(start_ccb->ccb_h.func_code)));
	switch (start_ccb->ccb_h.func_code) {
	case XPT_SCAN_BUS:
                /* FALLTHROUGH */
	case XPT_SCAN_TGT:
                /* FALLTHROUGH */
	case XPT_SCAN_LUN:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
			  ("XPT_SCAN_{BUS,TGT,LUN}\n"));
		mmc_scan_lun(start_ccb->ccb_h.path->periph,
			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
			     start_ccb);
		break;

	case XPT_DEV_ADVINFO:
	{
		mmc_dev_advinfo(start_ccb);
		break;
	}

	default:
		xpt_action_default(start_ccb);
		break;
	}
}

static void
mmc_dev_advinfo(union ccb *start_ccb)
{
	struct cam_ed *device;
	struct ccb_dev_advinfo *cdai;
	off_t amt;

	xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
	start_ccb->ccb_h.status = CAM_REQ_INVALID;
	device = start_ccb->ccb_h.path->device;
	cdai = &start_ccb->cdai;
	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
		  ("%s: request %x\n", __func__, cdai->buftype));

        /* We don't support writing any data */
        if (cdai->flags & CDAI_FLAG_STORE)
                panic("Attempt to store data?!");

	switch(cdai->buftype) {
	case CDAI_TYPE_SCSI_DEVID:
		cdai->provsiz = device->device_id_len;
		if (device->device_id_len == 0)
			break;
		amt = MIN(cdai->provsiz, cdai->bufsiz);
		memcpy(cdai->buf, device->device_id, amt);
		break;
	case CDAI_TYPE_SERIAL_NUM:
		cdai->provsiz = device->serial_num_len;
		if (device->serial_num_len == 0)
			break;
		amt = MIN(cdai->provsiz, cdai->bufsiz);
		memcpy(cdai->buf, device->serial_num, amt);
		break;
        case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
                cdai->provsiz = 0;
                break;
	case CDAI_TYPE_MMC_PARAMS:
		cdai->provsiz = sizeof(struct mmc_params);
		amt = MIN(cdai->provsiz, cdai->bufsiz);
		memcpy(cdai->buf, &device->mmc_ident_data, amt);
		break;
	default:
                panic("Unknown buftype");
		return;
	}
	start_ccb->ccb_h.status = CAM_REQ_CMP;
}

static void
mmc_announce_periph(struct cam_periph *periph)
{
	struct	ccb_pathinq cpi;
	struct	ccb_trans_settings cts;
	struct	cam_path *path = periph->path;

	cam_periph_assert(periph, MA_OWNED);

	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmc_announce_periph"));

	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
	cts.type = CTS_TYPE_CURRENT_SETTINGS;
	xpt_action((union ccb*)&cts);
	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
		return;
	xpt_path_inq(&cpi, periph->path);
	printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
}

void
mmccam_start_discovery(struct cam_sim *sim)
{
	union ccb *ccb;
	uint32_t pathid;

	KASSERT(sim->sim_dev != NULL, ("mmccam_start_discovery(%s): sim_dev is not initialized,"
	    " has cam_sim_alloc_dev() been used?", cam_sim_name(sim)));
	pathid = cam_sim_path(sim);
	ccb = xpt_alloc_ccb();

	/*
	 * We create a rescan request for BUS:0:0, since the card
	 * will be at lun 0.
	 */
	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
		/* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
		xpt_free_ccb(ccb);
		return;
	}
	xpt_rescan(ccb);
}

/* This func is called per attached device :-( */
static void
mmc_print_ident(struct mmc_params *ident_data, struct sbuf *sb)
{
	bool space = false;

	sbuf_printf(sb, "Relative addr: %08x\n", ident_data->card_rca);
	sbuf_printf(sb, "Card features: <");
	if (ident_data->card_features & CARD_FEATURE_MMC) {
		sbuf_printf(sb, "MMC");
		space = true;
	}
	if (ident_data->card_features & CARD_FEATURE_MEMORY) {
		sbuf_printf(sb, "%sMemory", space ? " " : "");
		space = true;
	}
	if (ident_data->card_features & CARD_FEATURE_SDHC) {
		sbuf_printf(sb, "%sHigh-Capacity", space ? " " : "");
		space = true;
	}
	if (ident_data->card_features & CARD_FEATURE_SD20) {
		sbuf_printf(sb, "%sSD2.0-Conditions", space ? " " : "");
		space = true;
	}
	if (ident_data->card_features & CARD_FEATURE_SDIO) {
		sbuf_printf(sb, "%sSDIO", space ? " " : "");
		space = true;
	}
	if (ident_data->card_features & CARD_FEATURE_18V) {
		sbuf_printf(sb, "%s1.8-Signaling", space ? " " : "");
	}
	sbuf_printf(sb, ">\n");

	if (ident_data->card_features & CARD_FEATURE_MEMORY)
		sbuf_printf(sb, "Card memory OCR: %08x\n",
		    ident_data->card_ocr);

	if (ident_data->card_features & CARD_FEATURE_SDIO) {
		sbuf_printf(sb, "Card IO OCR: %08x\n", ident_data->io_ocr);
		sbuf_printf(sb, "Number of functions: %u\n",
		    ident_data->sdio_func_count);
	}

	sbuf_finish(sb);
	printf("%s", sbuf_data(sb));
	sbuf_clear(sb);
}

static void
mmc_proto_announce(struct cam_ed *device)
{
	struct sbuf	sb;
	char		buffer[256];

	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
	mmc_print_ident(&device->mmc_ident_data, &sb);
	sbuf_finish(&sb);
	sbuf_putbuf(&sb);
}

static void
mmc_proto_denounce(struct cam_ed *device)
{

	mmc_proto_announce(device);
}

static void
mmc_proto_debug_out(union ccb *ccb)
{
	if (ccb->ccb_h.func_code != XPT_MMC_IO)
		return;

	CAM_DEBUG(ccb->ccb_h.path,
	    CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
}

static periph_init_t probe_periph_init;

static struct periph_driver probe_driver =
{
	probe_periph_init, "mmcprobe",
	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
	CAM_PERIPH_DRV_EARLY
};

PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);

#define	CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */

static void
probe_periph_init(void)
{
}

static cam_status
mmcprobe_register(struct cam_periph *periph, void *arg)
{
	mmcprobe_softc *softc;
	union ccb *request_ccb;	/* CCB representing the probe request */
	int status;

	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));

	request_ccb = (union ccb *)arg;
	if (request_ccb == NULL) {
		printf("mmcprobe_register: no probe CCB, "
		       "can't register device\n");
		return(CAM_REQ_CMP_ERR);
	}

	softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);

	if (softc == NULL) {
		printf("proberegister: Unable to probe new device. "
		       "Unable to allocate softc\n");
		return(CAM_REQ_CMP_ERR);
	}

	softc->flags = 0;
	softc->acmd41_count = 0;
	periph->softc = softc;
	softc->periph = periph;
	softc->action = PROBE_INVALID;
        softc->restart = 0;
	status = cam_periph_acquire(periph);

        memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
	if (status != 0) {
		printf("proberegister: cam_periph_acquire failed (status=%d)\n",
			status);
		return (CAM_REQ_CMP_ERR);
	}
	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));

	if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
		PROBE_SET_ACTION(softc, PROBE_RESET);
	else
		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);

	/* This will kick the ball */
	xpt_schedule(periph, CAM_PRIORITY_XPT);

	return(CAM_REQ_CMP);
}

static int
mmc_highest_voltage(uint32_t ocr)
{
	int i;

	for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
	    i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
		if (ocr & (1 << i))
			return (i);
	return (-1);
}

static inline void
init_standard_ccb(union ccb *ccb, uint32_t cmd)
{
	ccb->ccb_h.func_code = cmd;
	ccb->ccb_h.flags = CAM_DIR_OUT;
	ccb->ccb_h.retry_count = 0;
	ccb->ccb_h.timeout = 15 * 1000;
	ccb->ccb_h.cbfcnp = mmcprobe_done;
}

static void
mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
{
	mmcprobe_softc *softc;
	struct cam_path *path;
	struct ccb_mmcio *mmcio;
	struct mtx *p_mtx = cam_periph_mtx(periph);
	struct ccb_trans_settings_mmc *cts;

	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
	softc = (mmcprobe_softc *)periph->softc;
	path = start_ccb->ccb_h.path;
	mmcio = &start_ccb->mmcio;
	cts = &start_ccb->cts.proto_specific.mmc;
	struct mmc_params *mmcp = &path->device->mmc_ident_data;

	memset(&mmcio->cmd, 0, sizeof(struct mmc_command));

	if (softc->restart) {
		softc->restart = 0;
		if (path->device->flags & CAM_DEV_UNCONFIGURED)
			softc->action = PROBE_RESET;
		else
			softc->action = PROBE_IDENTIFY;
	}

	/* Here is the place where the identify fun begins */
	switch (softc->action) {
	case PROBE_RESET:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
		/* FALLTHROUGH */
	case PROBE_IDENTIFY:
		xpt_path_inq(&start_ccb->cpi, periph->path);
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_IDENTIFY\n"));
		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
		xpt_action(start_ccb);
		if (cts->ios.power_mode != power_off) {
			init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
			cts->ios.power_mode = power_off;
			cts->ios_valid = MMC_PM;
			xpt_action(start_ccb);
			mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
		}
		/* mmc_power_up */
		/* Get the host OCR */
		init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
		xpt_action(start_ccb);

		uint32_t host_caps = cts->host_caps;
		if (host_caps & MMC_CAP_SIGNALING_180)
			softc->flags |= PROBE_FLAG_HOST_CAN_DO_18V;
		uint32_t hv = mmc_highest_voltage(cts->host_ocr);
		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
		cts->ios.vdd = hv;
		cts->ios.bus_mode = opendrain;
		cts->ios.chip_select = cs_dontcare;
		cts->ios.power_mode = power_up;
		cts->ios.bus_width = bus_width_1;
		cts->ios.clock = 0;
		cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
			MMC_CS | MMC_BW | MMC_CLK;
		xpt_action(start_ccb);
		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);

		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
		cts->ios.power_mode = power_on;
		cts->ios.clock = CARD_ID_FREQUENCY;
		cts->ios.timing = bus_timing_normal;
		cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
		xpt_action(start_ccb);
		mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
		/* End for mmc_power_on */

		/* Begin mmc_idle_cards() */
		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
		cts->ios.chip_select = cs_high;
		cts->ios_valid = MMC_CS;
		xpt_action(start_ccb);
		mtx_sleep(periph, p_mtx, 0, "mmcios", 1);

		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
		mmcio->cmd.arg = 0;
		mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
		mmcio->cmd.data = NULL;
		mmcio->stop.opcode = 0;

		/* XXX Reset I/O portion as well */
		break;
	case PROBE_SDIO_RESET:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			  ("Start with PROBE_SDIO_RESET\n"));
		uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
			| SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
		cam_fill_mmcio(&start_ccb->mmcio,
			       /*retries*/ 0,
			       /*cbfcnp*/ mmcprobe_done,
			       /*flags*/ CAM_DIR_NONE,
			       /*mmc_opcode*/ SD_IO_RW_DIRECT,
			       /*mmc_arg*/ mmc_arg,
			       /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
			       /*mmc_data*/ NULL,
			       /*timeout*/ 1000);
		break;
	case PROBE_SEND_IF_COND:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			  ("Start with PROBE_SEND_IF_COND\n"));
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
		mmcio->cmd.arg = (1 << 8) + 0xAA;
		mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
		mmcio->stop.opcode = 0;
		break;

	case PROBE_SDIO_INIT:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			  ("Start with PROBE_SDIO_INIT\n"));
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
		mmcio->cmd.arg = mmcp->io_ocr;
		mmcio->cmd.flags = MMC_RSP_R4;
		mmcio->stop.opcode = 0;
		break;

	case PROBE_MMC_INIT:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			  ("Start with PROBE_MMC_INIT\n"));
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
		mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
		mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
		mmcio->stop.opcode = 0;
		break;

	case PROBE_SEND_APP_OP_COND:
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		if (softc->flags & PROBE_FLAG_ACMD_SENT) {
			mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
			/*
			 * We set CCS bit because we do support SDHC cards.
			 * XXX: Don't set CCS if no response to CMD8.
			 */
			uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
			if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
				cmd_arg |= MMC_OCR_S18R;
			mmcio->cmd.arg = cmd_arg;
			mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
			softc->acmd41_count++;
		} else {
			mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
			mmcio->cmd.arg = 0; /* rca << 16 */
			mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
		}
		mmcio->stop.opcode = 0;
		break;

	case PROBE_GET_CID: /* XXX move to mmc_da */
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_ALL_SEND_CID;
		mmcio->cmd.arg = 0;
		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
		mmcio->stop.opcode = 0;
		break;
	case PROBE_SEND_RELATIVE_ADDR:
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
		mmcio->cmd.arg = 0;
		mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
		mmcio->stop.opcode = 0;
		break;
	case PROBE_MMC_SET_RELATIVE_ADDR:
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
		mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
		mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
		mmcio->stop.opcode = 0;
		break;
	case PROBE_SELECT_CARD:
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_SELECT_CARD;
		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
		mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
		mmcio->stop.opcode = 0;
		break;
	case PROBE_GET_CSD: /* XXX move to mmc_da */
		init_standard_ccb(start_ccb, XPT_MMC_IO);
		mmcio->cmd.opcode = MMC_SEND_CSD;
		mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
		mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
		mmcio->stop.opcode = 0;
		break;
	case PROBE_DONE:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
		init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
		cts->ios.bus_mode = pushpull;
		cts->ios_valid = MMC_BM;
		xpt_action(start_ccb);
		return;
		/* NOTREACHED */
		break;
	case PROBE_INVALID:
		break;
	default:
		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
		panic("default: case in mmc_probe_start()");
	}

	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
	xpt_action(start_ccb);
}

static void mmcprobe_cleanup(struct cam_periph *periph)
{
	free(periph->softc, M_CAMXPT);
}

static void
mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
{
	mmcprobe_softc *softc;
	struct cam_path *path;

	int err;
	struct ccb_mmcio *mmcio;
	u_int32_t  priority;

	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mmcprobe_done\n"));
	softc = (mmcprobe_softc *)periph->softc;
	path = done_ccb->ccb_h.path;
	priority = done_ccb->ccb_h.pinfo.priority;

	switch (softc->action) {
	case PROBE_RESET:
		/* FALLTHROUGH */
	case PROBE_IDENTIFY:
	{
		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;

		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("GO_IDLE_STATE failed with error %d\n",
				   err));

			/* There was a device there, but now it's gone... */
			if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("Device lost!\n"));

				xpt_async(AC_LOST_DEVICE, path, NULL);
			}
			PROBE_SET_ACTION(softc, PROBE_INVALID);
			break;
		}
		path->device->protocol = PROTO_MMCSD;
		PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
		break;
	}
	case PROBE_SEND_IF_COND:
	{
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
		struct mmc_params *mmcp = &path->device->mmc_ident_data;

		if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("IF_COND: error %d, pattern %08x\n",
				   err, mmcio->cmd.resp[0]));
		} else {
			mmcp->card_features |= CARD_FEATURE_SD20;
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("SD 2.0 interface conditions: OK\n"));
		}
                PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
		break;
	}
        case PROBE_SDIO_RESET:
        {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;

                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
                           err, mmcio->cmd.resp[0]));
                PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
                break;
        }
        case PROBE_SDIO_INIT:
        {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
                struct mmc_params *mmcp = &path->device->mmc_ident_data;

                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
                           err, mmcio->cmd.resp[0],
                           mmcio->cmd.resp[1],
                           mmcio->cmd.resp[2],
                           mmcio->cmd.resp[3]));

                /*
                 * Error here means that this card is not SDIO,
                 * so proceed with memory init as if nothing has happened
                 */
		if (err != MMC_ERR_NONE) {
                        PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
                        break;
		}
                mmcp->card_features |= CARD_FEATURE_SDIO;
                uint32_t ioifcond = mmcio->cmd.resp[0];
                uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;

                mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("SDIO card: %d functions\n", mmcp->sdio_func_count));
                if (io_ocr == 0) {
                    CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                              ("SDIO OCR invalid, retrying\n"));
                    break; /* Retry */
                }

                if (io_ocr != 0 && mmcp->io_ocr == 0) {
                        mmcp->io_ocr = io_ocr;
                        break; /* Retry, this time with non-0 OCR */
                }
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("SDIO OCR: %08x\n", mmcp->io_ocr));

                if (ioifcond & R4_IO_MEM_PRESENT) {
                        /* Combo card -- proceed to memory initialization */
                        PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
                } else {
                        /* No memory portion -- get RCA and select card */
                        PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
                }
                break;
        }
        case PROBE_MMC_INIT:
        {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
                struct mmc_params *mmcp = &path->device->mmc_ident_data;

		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("MMC_INIT: error %d, resp %08x\n",
				   err, mmcio->cmd.resp[0]));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
                        break;
                }
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));

                if (mmcp->card_ocr == 0) {
                        /* We haven't sent the OCR to the card yet -- do it */
                        mmcp->card_ocr = mmcio->cmd.resp[0];
                        CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                                  ("-> sending OCR to card\n"));
                        break;
                }

                if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
                        CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                                  ("Card is still powering up\n"));
                        break;
                }

                mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
                PROBE_SET_ACTION(softc, PROBE_GET_CID);
                break;
        }
	case PROBE_SEND_APP_OP_COND:
	{
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;

		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("APP_OP_COND: error %d, resp %08x\n",
				   err, mmcio->cmd.resp[0]));
			PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
                        break;
                }

                if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
                        /* Don't change the state */
                        softc->flags |= PROBE_FLAG_ACMD_SENT;
                        break;
                }

                softc->flags &= ~PROBE_FLAG_ACMD_SENT;
                if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
                    (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
                        struct mmc_params *mmcp = &path->device->mmc_ident_data;
                        CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                                  ("Card OCR: %08x\n",  mmcio->cmd.resp[0]));
                        if (mmcp->card_ocr == 0) {
                                mmcp->card_ocr = mmcio->cmd.resp[0];
                                /* Now when we know OCR that we want -- send it to card */
                                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                                          ("-> sending OCR to card\n"));
                        } else {
                                /* We already know the OCR and despite of that we
                                 * are processing the answer to ACMD41 -> move on
                                 */
                                PROBE_SET_ACTION(softc, PROBE_GET_CID);
                        }
                        /* Getting an answer to ACMD41 means the card has memory */
                        mmcp->card_features |= CARD_FEATURE_MEMORY;

                        /* Standard capacity vs High Capacity memory card */
                        if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
                                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                                          ("Card is SDHC\n"));
                                mmcp->card_features |= CARD_FEATURE_SDHC;
                        }

			/* Whether the card supports 1.8V signaling */
			if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
				CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
					  ("Card supports 1.8V signaling\n"));
				mmcp->card_features |= CARD_FEATURE_18V;
				if (softc->flags & PROBE_FLAG_HOST_CAN_DO_18V) {
					CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
						  ("Host supports 1.8V signaling. Switch voltage!\n"));
					done_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
					done_ccb->ccb_h.flags = CAM_DIR_NONE;
					done_ccb->ccb_h.retry_count = 0;
					done_ccb->ccb_h.timeout = 100;
					done_ccb->ccb_h.cbfcnp = NULL;
					done_ccb->cts.proto_specific.mmc.ios.vccq = vccq_180;
					done_ccb->cts.proto_specific.mmc.ios_valid = MMC_VCCQ;
					xpt_action(done_ccb);
				}
			}
		} else {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("Card not ready: %08x\n",  mmcio->cmd.resp[0]));
			/* Send CMD55+ACMD41 once again  */
			PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
		}

                break;
	}
        case PROBE_GET_CID: /* XXX move to mmc_da */
        {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;

		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("PROBE_GET_CID: error %d\n", err));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
                        break;
                }

                struct mmc_params *mmcp = &path->device->mmc_ident_data;
                memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("CID %08x%08x%08x%08x\n",
                           mmcp->card_cid[0],
                           mmcp->card_cid[1],
                           mmcp->card_cid[2],
                           mmcp->card_cid[3]));
		if (mmcp->card_features & CARD_FEATURE_MMC)
			PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
		else
			PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
                break;
        }
        case PROBE_SEND_RELATIVE_ADDR: {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
                struct mmc_params *mmcp = &path->device->mmc_ident_data;
                uint16_t rca = mmcio->cmd.resp[0] >> 16;
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("Card published RCA: %u\n", rca));
                path->device->mmc_ident_data.card_rca = rca;
		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
                        break;
                }

                /* If memory is present, get CSD, otherwise select card */
                if (mmcp->card_features & CARD_FEATURE_MEMORY)
                        PROBE_SET_ACTION(softc, PROBE_GET_CSD);
                else
                        PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
		break;
        }
	case PROBE_MMC_SET_RELATIVE_ADDR:
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			    ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
			break;
		}
		path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
		PROBE_SET_ACTION(softc, PROBE_GET_CSD);
		break;
        case PROBE_GET_CSD: {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;

		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("PROBE_GET_CSD: error %d\n", err));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
                        break;
                }

                struct mmc_params *mmcp = &path->device->mmc_ident_data;
                memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
                CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
                          ("CSD %08x%08x%08x%08x\n",
                           mmcp->card_csd[0],
                           mmcp->card_csd[1],
                           mmcp->card_csd[2],
                           mmcp->card_csd[3]));
                PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
                break;
        }
        case PROBE_SELECT_CARD: {
		mmcio = &done_ccb->mmcio;
		err = mmcio->cmd.error;
		if (err != MMC_ERR_NONE) {
			CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
				  ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
			PROBE_SET_ACTION(softc, PROBE_INVALID);
                        break;
                }

		PROBE_SET_ACTION(softc, PROBE_DONE);
                break;
        }
	default:
		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
			  ("mmcprobe_done: invalid action state 0x%x\n", softc->action));
		panic("default: case in mmc_probe_done()");
	}

	if (softc->action == PROBE_INVALID &&
	  (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
		xpt_async(AC_LOST_DEVICE, path, NULL);
	}

	if (softc->action != PROBE_INVALID)
		xpt_schedule(periph, priority);
	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
	int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
	  ("mmcprobe_done: remaining freeze count %d\n", frozen));

	if (softc->action == PROBE_DONE) {
                /* Notify the system that the device is found! */
		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
			xpt_acquire_device(path->device);
			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
			xpt_action(done_ccb);
			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
		}
	}
	xpt_release_ccb(done_ccb);
	if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
		cam_periph_invalidate(periph);
		cam_periph_release_locked(periph);
	}
}

void
mmc_path_inq(struct ccb_pathinq *cpi, const char *hba,
    const struct cam_sim *sim, size_t maxio)
{

	cpi->version_num = 1;
	cpi->hba_inquiry = 0;
	cpi->target_sprt = 0;
	cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
	cpi->hba_eng_cnt = 0;
	cpi->max_target = 0;
	cpi->max_lun = 0;
	cpi->initiator_id = 1;
	cpi->maxio = maxio;
	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
	strncpy(cpi->hba_vid, hba, HBA_IDLEN);
	strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
	cpi->unit_number = cam_sim_unit(sim);
	cpi->bus_id = cam_sim_bus(sim);
	cpi->protocol = PROTO_MMCSD;
	cpi->protocol_version = SCSI_REV_0;
	cpi->transport = XPORT_MMCSD;
	cpi->transport_version = 1;

	cpi->base_transfer_speed = 100; /* XXX WTF? */

	cpi->ccb_h.status = CAM_REQ_CMP;
}