xref: /freebsd/sys/cam/mmc/mmc_da.c (revision 6506efea63f5a52d62ceea4bc18b8faf8a037ff3)
1a94a63f0SWarner Losh /*-
2f24882ecSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3f24882ecSPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 2006 Bernd Walter <tisco@FreeBSD.org> All rights reserved.
5f86e6000SWarner Losh  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> All rights reserved.
6f86e6000SWarner Losh  * Copyright (c) 2015-2017 Ilya Bakulin <kibab@FreeBSD.org> All rights reserved.
7a94a63f0SWarner Losh  * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
8a94a63f0SWarner Losh  *
9a94a63f0SWarner Losh  * Redistribution and use in source and binary forms, with or without
10a94a63f0SWarner Losh  * modification, are permitted provided that the following conditions
11a94a63f0SWarner Losh  * are met:
12a94a63f0SWarner Losh  * 1. Redistributions of source code must retain the above copyright
13a94a63f0SWarner Losh  *    notice, this list of conditions and the following disclaimer,
14a94a63f0SWarner Losh  *    without modification, immediately at the beginning of the file.
15a94a63f0SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
16a94a63f0SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
17a94a63f0SWarner Losh  *    documentation and/or other materials provided with the distribution.
18a94a63f0SWarner Losh  *
19a94a63f0SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20a94a63f0SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21a94a63f0SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22a94a63f0SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23a94a63f0SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24a94a63f0SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a94a63f0SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a94a63f0SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a94a63f0SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28a94a63f0SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a94a63f0SWarner Losh  *
30a94a63f0SWarner Losh  * Some code derived from the sys/dev/mmc and sys/cam/ata
31a94a63f0SWarner Losh  * Thanks to Warner Losh <imp@FreeBSD.org>, Alexander Motin <mav@FreeBSD.org>
32a94a63f0SWarner Losh  * Bernd Walter <tisco@FreeBSD.org>, and other authors.
33a94a63f0SWarner Losh  */
34a94a63f0SWarner Losh 
35a94a63f0SWarner Losh #include <sys/cdefs.h>
36a94a63f0SWarner Losh __FBSDID("$FreeBSD$");
37a94a63f0SWarner Losh 
38a94a63f0SWarner Losh //#include "opt_sdda.h"
39a94a63f0SWarner Losh 
40a94a63f0SWarner Losh #include <sys/param.h>
41a94a63f0SWarner Losh 
42a94a63f0SWarner Losh #ifdef _KERNEL
43a94a63f0SWarner Losh #include <sys/systm.h>
44a94a63f0SWarner Losh #include <sys/kernel.h>
45a94a63f0SWarner Losh #include <sys/bio.h>
46bf286853SEmmanuel Vadot #include <sys/sysctl.h>
47a94a63f0SWarner Losh #include <sys/endian.h>
48a94a63f0SWarner Losh #include <sys/taskqueue.h>
49a94a63f0SWarner Losh #include <sys/lock.h>
50a94a63f0SWarner Losh #include <sys/mutex.h>
51a94a63f0SWarner Losh #include <sys/conf.h>
52a94a63f0SWarner Losh #include <sys/devicestat.h>
53a94a63f0SWarner Losh #include <sys/eventhandler.h>
54a94a63f0SWarner Losh #include <sys/malloc.h>
55a94a63f0SWarner Losh #include <sys/cons.h>
56a94a63f0SWarner Losh #include <sys/proc.h>
57a94a63f0SWarner Losh #include <sys/reboot.h>
58a94a63f0SWarner Losh #include <geom/geom_disk.h>
59a94a63f0SWarner Losh #include <machine/_inttypes.h>  /* for PRIu64 */
60a94a63f0SWarner Losh #endif /* _KERNEL */
61a94a63f0SWarner Losh 
62a94a63f0SWarner Losh #ifndef _KERNEL
63a94a63f0SWarner Losh #include <stdio.h>
64a94a63f0SWarner Losh #include <string.h>
65a94a63f0SWarner Losh #endif /* _KERNEL */
66a94a63f0SWarner Losh 
67a94a63f0SWarner Losh #include <cam/cam.h>
68a94a63f0SWarner Losh #include <cam/cam_ccb.h>
69a94a63f0SWarner Losh #include <cam/cam_queue.h>
70a94a63f0SWarner Losh #include <cam/cam_periph.h>
71a94a63f0SWarner Losh #include <cam/cam_sim.h>
72a94a63f0SWarner Losh #include <cam/cam_xpt.h>
73a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
74a94a63f0SWarner Losh #include <cam/cam_xpt_periph.h>
75a94a63f0SWarner Losh #include <cam/cam_xpt_internal.h>
76a94a63f0SWarner Losh #include <cam/cam_debug.h>
77a94a63f0SWarner Losh 
78a94a63f0SWarner Losh #include <cam/mmc/mmc_all.h>
79a94a63f0SWarner Losh 
80a94a63f0SWarner Losh #ifdef _KERNEL
81a94a63f0SWarner Losh 
82a94a63f0SWarner Losh typedef enum {
83a94a63f0SWarner Losh 	SDDA_FLAG_OPEN		= 0x0002,
84a94a63f0SWarner Losh 	SDDA_FLAG_DIRTY		= 0x0004
85a94a63f0SWarner Losh } sdda_flags;
86a94a63f0SWarner Losh 
87a94a63f0SWarner Losh typedef enum {
88a94a63f0SWarner Losh 	SDDA_STATE_INIT,
89a94a63f0SWarner Losh 	SDDA_STATE_INVALID,
9096e47614SIlya Bakulin 	SDDA_STATE_NORMAL,
9196e47614SIlya Bakulin 	SDDA_STATE_PART_SWITCH,
92a94a63f0SWarner Losh } sdda_state;
93a94a63f0SWarner Losh 
9496e47614SIlya Bakulin #define	SDDA_FMT_BOOT		"sdda%dboot"
9596e47614SIlya Bakulin #define	SDDA_FMT_GP		"sdda%dgp"
9696e47614SIlya Bakulin #define	SDDA_FMT_RPMB		"sdda%drpmb"
9796e47614SIlya Bakulin #define	SDDA_LABEL_ENH		"enh"
9896e47614SIlya Bakulin 
9996e47614SIlya Bakulin #define	SDDA_PART_NAMELEN	(16 + 1)
10096e47614SIlya Bakulin 
10196e47614SIlya Bakulin struct sdda_softc;
10296e47614SIlya Bakulin 
10396e47614SIlya Bakulin struct sdda_part {
10496e47614SIlya Bakulin 	struct disk *disk;
105a94a63f0SWarner Losh 	struct bio_queue_head bio_queue;
10696e47614SIlya Bakulin 	sdda_flags flags;
10796e47614SIlya Bakulin 	struct sdda_softc *sc;
10896e47614SIlya Bakulin 	u_int cnt;
10996e47614SIlya Bakulin 	u_int type;
11096e47614SIlya Bakulin 	bool ro;
11196e47614SIlya Bakulin 	char name[SDDA_PART_NAMELEN];
11296e47614SIlya Bakulin };
11396e47614SIlya Bakulin 
11496e47614SIlya Bakulin struct sdda_softc {
115a94a63f0SWarner Losh 	int	 outstanding_cmds;	/* Number of active commands */
116a94a63f0SWarner Losh 	int	 refcount;		/* Active xpt_action() calls */
117a94a63f0SWarner Losh 	sdda_state state;
118a94a63f0SWarner Losh 	struct mmc_data *mmcdata;
11996e47614SIlya Bakulin 	struct cam_periph *periph;
120a94a63f0SWarner Losh //	sdda_quirks quirks;
121a94a63f0SWarner Losh 	struct task start_init_task;
122a94a63f0SWarner Losh 	uint32_t raw_csd[4];
123a94a63f0SWarner Losh 	uint8_t raw_ext_csd[512]; /* MMC only? */
124a94a63f0SWarner Losh 	struct mmc_csd csd;
125a94a63f0SWarner Losh 	struct mmc_cid cid;
126a94a63f0SWarner Losh 	struct mmc_scr scr;
127a94a63f0SWarner Losh 	/* Calculated from CSD */
128a94a63f0SWarner Losh 	uint64_t sector_count;
129a94a63f0SWarner Losh 	uint64_t mediasize;
130a94a63f0SWarner Losh 
131a94a63f0SWarner Losh 	/* Calculated from CID */
132a94a63f0SWarner Losh 	char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
133a94a63f0SWarner Losh 	char card_sn_string[16];/* Formatted serial # for disk->d_ident */
134a94a63f0SWarner Losh 	/* Determined from CSD + is highspeed card*/
135a94a63f0SWarner Losh 	uint32_t card_f_max;
13696e47614SIlya Bakulin 
13796e47614SIlya Bakulin 	/* Generic switch timeout */
13896e47614SIlya Bakulin 	uint32_t cmd6_time;
139fd7371f7SEmmanuel Vadot 	uint32_t timings;	/* Mask of bus timings supported */
140fd7371f7SEmmanuel Vadot 	uint32_t vccq_120;	/* Mask of bus timings at VCCQ of 1.2 V */
141fd7371f7SEmmanuel Vadot 	uint32_t vccq_180;	/* Mask of bus timings at VCCQ of 1.8 V */
14296e47614SIlya Bakulin 	/* MMC partitions support */
14396e47614SIlya Bakulin 	struct sdda_part *part[MMC_PART_MAX];
14496e47614SIlya Bakulin 	uint8_t part_curr;	/* Partition currently switched to */
14596e47614SIlya Bakulin 	uint8_t part_requested; /* What partition we're currently switching to */
14696e47614SIlya Bakulin 	uint32_t part_time;	/* Partition switch timeout [us] */
14796e47614SIlya Bakulin 	off_t enh_base;		/* Enhanced user data area slice base ... */
14896e47614SIlya Bakulin 	off_t enh_size;		/* ... and size [bytes] */
14996e47614SIlya Bakulin 	int log_count;
15096e47614SIlya Bakulin 	struct timeval log_time;
151a94a63f0SWarner Losh };
152a94a63f0SWarner Losh 
1531a22fb3fSIlya Bakulin static const char *mmc_errmsg[] =
1541a22fb3fSIlya Bakulin {
1551a22fb3fSIlya Bakulin 	"None",
1561a22fb3fSIlya Bakulin 	"Timeout",
1571a22fb3fSIlya Bakulin 	"Bad CRC",
1581a22fb3fSIlya Bakulin 	"Fifo",
1591a22fb3fSIlya Bakulin 	"Failed",
1601a22fb3fSIlya Bakulin 	"Invalid",
1611a22fb3fSIlya Bakulin 	"NO MEMORY"
1621a22fb3fSIlya Bakulin };
1631a22fb3fSIlya Bakulin 
164a94a63f0SWarner Losh #define ccb_bp		ppriv_ptr1
165a94a63f0SWarner Losh 
166a94a63f0SWarner Losh static	disk_strategy_t	sddastrategy;
167a94a63f0SWarner Losh static	periph_init_t	sddainit;
168a94a63f0SWarner Losh static	void		sddaasync(void *callback_arg, u_int32_t code,
169a94a63f0SWarner Losh 				struct cam_path *path, void *arg);
170a94a63f0SWarner Losh static	periph_ctor_t	sddaregister;
171a94a63f0SWarner Losh static	periph_dtor_t	sddacleanup;
172a94a63f0SWarner Losh static	periph_start_t	sddastart;
173a94a63f0SWarner Losh static	periph_oninv_t	sddaoninvalidate;
174a94a63f0SWarner Losh static	void		sddadone(struct cam_periph *periph,
175a94a63f0SWarner Losh 			       union ccb *done_ccb);
176a94a63f0SWarner Losh static  int		sddaerror(union ccb *ccb, u_int32_t cam_flags,
177a94a63f0SWarner Losh 				u_int32_t sense_flags);
178a94a63f0SWarner Losh 
1791a22fb3fSIlya Bakulin static int mmc_handle_reply(union ccb *ccb);
180a94a63f0SWarner Losh static uint16_t get_rca(struct cam_periph *periph);
181a94a63f0SWarner Losh static void sdda_start_init(void *context, union ccb *start_ccb);
182a94a63f0SWarner Losh static void sdda_start_init_task(void *context, int pending);
18396e47614SIlya Bakulin static void sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *start_ccb);
18496e47614SIlya Bakulin static uint32_t sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb);
185d670d951SIlya Bakulin static int mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca);
18696e47614SIlya Bakulin static inline uint32_t mmc_get_sector_size(struct cam_periph *periph) {return MMC_SECTOR_SIZE;}
18796e47614SIlya Bakulin 
188bf286853SEmmanuel Vadot static SYSCTL_NODE(_kern_cam, OID_AUTO, sdda, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
189bf286853SEmmanuel Vadot     "CAM Direct Access Disk driver");
190bf286853SEmmanuel Vadot 
191bf286853SEmmanuel Vadot static int sdda_mmcsd_compat = 1;
192bf286853SEmmanuel Vadot SYSCTL_INT(_kern_cam_sdda, OID_AUTO, mmcsd_compat, CTLFLAG_RDTUN,
193bf286853SEmmanuel Vadot     &sdda_mmcsd_compat, 1, "Enable creation of mmcsd aliases.");
194bf286853SEmmanuel Vadot 
19596e47614SIlya Bakulin /* TODO: actually issue GET_TRAN_SETTINGS to get R/O status */
19696e47614SIlya Bakulin static inline bool sdda_get_read_only(struct cam_periph *periph, union ccb *start_ccb)
19796e47614SIlya Bakulin {
19896e47614SIlya Bakulin 
19996e47614SIlya Bakulin 	return (false);
20096e47614SIlya Bakulin }
20196e47614SIlya Bakulin 
20296e47614SIlya Bakulin static uint32_t mmc_get_spec_vers(struct cam_periph *periph);
20396e47614SIlya Bakulin static uint64_t mmc_get_media_size(struct cam_periph *periph);
20496e47614SIlya Bakulin static uint32_t mmc_get_cmd6_timeout(struct cam_periph *periph);
2052fe1b4caSEmmanuel Vadot static bool sdda_add_part(struct cam_periph *periph, u_int type,
20696e47614SIlya Bakulin     const char *name, u_int cnt, off_t media_size, bool ro);
207a94a63f0SWarner Losh 
208a94a63f0SWarner Losh static struct periph_driver sddadriver =
209a94a63f0SWarner Losh {
210a94a63f0SWarner Losh 	sddainit, "sdda",
211a94a63f0SWarner Losh 	TAILQ_HEAD_INITIALIZER(sddadriver.units), /* generation */ 0
212a94a63f0SWarner Losh };
213a94a63f0SWarner Losh 
214a94a63f0SWarner Losh PERIPHDRIVER_DECLARE(sdda, sddadriver);
215a94a63f0SWarner Losh 
216a94a63f0SWarner Losh static MALLOC_DEFINE(M_SDDA, "sd_da", "sd_da buffers");
217a94a63f0SWarner Losh 
218a94a63f0SWarner Losh static const int exp[8] = {
219a94a63f0SWarner Losh 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
220a94a63f0SWarner Losh };
221a94a63f0SWarner Losh 
222a94a63f0SWarner Losh static const int mant[16] = {
223a94a63f0SWarner Losh 	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
224a94a63f0SWarner Losh };
225a94a63f0SWarner Losh 
226a94a63f0SWarner Losh static const int cur_min[8] = {
227a94a63f0SWarner Losh 	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
228a94a63f0SWarner Losh };
229a94a63f0SWarner Losh 
230a94a63f0SWarner Losh static const int cur_max[8] = {
231a94a63f0SWarner Losh 	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
232a94a63f0SWarner Losh };
233a94a63f0SWarner Losh 
234a94a63f0SWarner Losh static uint16_t
235a94a63f0SWarner Losh get_rca(struct cam_periph *periph) {
236a94a63f0SWarner Losh 	return periph->path->device->mmc_ident_data.card_rca;
237a94a63f0SWarner Losh }
238a94a63f0SWarner Losh 
2391a22fb3fSIlya Bakulin /*
2401a22fb3fSIlya Bakulin  * Figure out if CCB execution resulted in error.
2411a22fb3fSIlya Bakulin  * Look at both CAM-level errors and on MMC protocol errors.
242b6b885c4SIlya Bakulin  *
243b6b885c4SIlya Bakulin  * Return value is always MMC error.
2441a22fb3fSIlya Bakulin */
2451a22fb3fSIlya Bakulin static int
2461a22fb3fSIlya Bakulin mmc_handle_reply(union ccb *ccb)
2471a22fb3fSIlya Bakulin {
2481a22fb3fSIlya Bakulin 	KASSERT(ccb->ccb_h.func_code == XPT_MMC_IO,
2491a22fb3fSIlya Bakulin 	    ("ccb %p: cannot handle non-XPT_MMC_IO errors, got func_code=%d",
2501a22fb3fSIlya Bakulin 		ccb, ccb->ccb_h.func_code));
2511a22fb3fSIlya Bakulin 
252b6b885c4SIlya Bakulin 	/* CAM-level error should always correspond to MMC-level error */
253b6b885c4SIlya Bakulin 	if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) &&
254b6b885c4SIlya Bakulin 	  (ccb->mmcio.cmd.error != MMC_ERR_NONE))
255b6b885c4SIlya Bakulin 		panic("CCB status is OK but MMC error != MMC_ERR_NONE");
256b6b885c4SIlya Bakulin 
257b6b885c4SIlya Bakulin 	if (ccb->mmcio.cmd.error != MMC_ERR_NONE) {
2581a22fb3fSIlya Bakulin 		xpt_print_path(ccb->ccb_h.path);
2591a22fb3fSIlya Bakulin 		printf("CMD%d failed, err %d (%s)\n",
2601a22fb3fSIlya Bakulin 		  ccb->mmcio.cmd.opcode,
2611a22fb3fSIlya Bakulin 		  ccb->mmcio.cmd.error,
2621a22fb3fSIlya Bakulin 		  mmc_errmsg[ccb->mmcio.cmd.error]);
2631a22fb3fSIlya Bakulin 	}
264b6b885c4SIlya Bakulin 	return (ccb->mmcio.cmd.error);
2651a22fb3fSIlya Bakulin }
2661a22fb3fSIlya Bakulin 
267a94a63f0SWarner Losh static uint32_t
268a94a63f0SWarner Losh mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
269a94a63f0SWarner Losh {
270a94a63f0SWarner Losh 	const int i = (bit_len / 32) - (start / 32) - 1;
271a94a63f0SWarner Losh 	const int shift = start & 31;
272a94a63f0SWarner Losh 	uint32_t retval = bits[i] >> shift;
273a94a63f0SWarner Losh 	if (size + shift > 32)
274a94a63f0SWarner Losh 		retval |= bits[i - 1] << (32 - shift);
275a94a63f0SWarner Losh 	return (retval & ((1llu << size) - 1));
276a94a63f0SWarner Losh }
277a94a63f0SWarner Losh 
278a94a63f0SWarner Losh static void
279a94a63f0SWarner Losh mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
280a94a63f0SWarner Losh {
281a94a63f0SWarner Losh 	int v;
282a94a63f0SWarner Losh 	int m;
283a94a63f0SWarner Losh 	int e;
284a94a63f0SWarner Losh 
285a94a63f0SWarner Losh 	memset(csd, 0, sizeof(*csd));
286a94a63f0SWarner Losh 	csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
287*6506efeaSEmmanuel Vadot 
288*6506efeaSEmmanuel Vadot 	/* Common members between 1.0 and 2.0 */
289a94a63f0SWarner Losh 	m = mmc_get_bits(raw_csd, 128, 115, 4);
290a94a63f0SWarner Losh 	e = mmc_get_bits(raw_csd, 128, 112, 3);
291a94a63f0SWarner Losh 	csd->tacc = (exp[e] * mant[m] + 9) / 10;
292a94a63f0SWarner Losh 	csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
293a94a63f0SWarner Losh 	m = mmc_get_bits(raw_csd, 128, 99, 4);
294a94a63f0SWarner Losh 	e = mmc_get_bits(raw_csd, 128, 96, 3);
295a94a63f0SWarner Losh 	csd->tran_speed = exp[e] * 10000 * mant[m];
296a94a63f0SWarner Losh 	csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
297a94a63f0SWarner Losh 	csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
298a94a63f0SWarner Losh 	csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
299a94a63f0SWarner Losh 	csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
300a94a63f0SWarner Losh 	csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
301a94a63f0SWarner Losh 	csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
302*6506efeaSEmmanuel Vadot 	csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
303*6506efeaSEmmanuel Vadot 	csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
304*6506efeaSEmmanuel Vadot 	csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
305*6506efeaSEmmanuel Vadot 	csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
306*6506efeaSEmmanuel Vadot 	csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
307*6506efeaSEmmanuel Vadot 	csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
308*6506efeaSEmmanuel Vadot 	csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
309*6506efeaSEmmanuel Vadot 
310*6506efeaSEmmanuel Vadot 	if (v == 0) {
311a94a63f0SWarner Losh 		csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
312a94a63f0SWarner Losh 		csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
313a94a63f0SWarner Losh 		csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
314a94a63f0SWarner Losh 		csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
315a94a63f0SWarner Losh 		m = mmc_get_bits(raw_csd, 128, 62, 12);
316a94a63f0SWarner Losh 		e = mmc_get_bits(raw_csd, 128, 47, 3);
317a94a63f0SWarner Losh 		csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
318a94a63f0SWarner Losh 	} else if (v == 1) {
319a94a63f0SWarner Losh 		csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
320a94a63f0SWarner Losh 		    512 * 1024;
321a94a63f0SWarner Losh 	} else
322a94a63f0SWarner Losh 		panic("unknown SD CSD version");
323a94a63f0SWarner Losh }
324a94a63f0SWarner Losh 
325a94a63f0SWarner Losh static void
326a94a63f0SWarner Losh mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
327a94a63f0SWarner Losh {
328a94a63f0SWarner Losh 	int m;
329a94a63f0SWarner Losh 	int e;
330a94a63f0SWarner Losh 
331a94a63f0SWarner Losh 	memset(csd, 0, sizeof(*csd));
332a94a63f0SWarner Losh 	csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
333a94a63f0SWarner Losh 	csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
334a94a63f0SWarner Losh 	m = mmc_get_bits(raw_csd, 128, 115, 4);
335a94a63f0SWarner Losh 	e = mmc_get_bits(raw_csd, 128, 112, 3);
336a94a63f0SWarner Losh 	csd->tacc = exp[e] * mant[m] + 9 / 10;
337a94a63f0SWarner Losh 	csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
338a94a63f0SWarner Losh 	m = mmc_get_bits(raw_csd, 128, 99, 4);
339a94a63f0SWarner Losh 	e = mmc_get_bits(raw_csd, 128, 96, 3);
340a94a63f0SWarner Losh 	csd->tran_speed = exp[e] * 10000 * mant[m];
341a94a63f0SWarner Losh 	csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
342a94a63f0SWarner Losh 	csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
343a94a63f0SWarner Losh 	csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
344a94a63f0SWarner Losh 	csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
345a94a63f0SWarner Losh 	csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
346a94a63f0SWarner Losh 	csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
347a94a63f0SWarner Losh 	csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
348a94a63f0SWarner Losh 	csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
349a94a63f0SWarner Losh 	csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
350a94a63f0SWarner Losh 	csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
351a94a63f0SWarner Losh 	m = mmc_get_bits(raw_csd, 128, 62, 12);
352a94a63f0SWarner Losh 	e = mmc_get_bits(raw_csd, 128, 47, 3);
353a94a63f0SWarner Losh 	csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
354a94a63f0SWarner Losh 	csd->erase_blk_en = 0;
355a94a63f0SWarner Losh 	csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
356a94a63f0SWarner Losh 	    (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
357a94a63f0SWarner Losh 	csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
358a94a63f0SWarner Losh 	csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
359a94a63f0SWarner Losh 	csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
360a94a63f0SWarner Losh 	csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
361a94a63f0SWarner Losh 	csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
362a94a63f0SWarner Losh }
363a94a63f0SWarner Losh 
364a94a63f0SWarner Losh static void
365a94a63f0SWarner Losh mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
366a94a63f0SWarner Losh {
367a94a63f0SWarner Losh 	int i;
368a94a63f0SWarner Losh 
369a94a63f0SWarner Losh 	/* There's no version info, so we take it on faith */
370a94a63f0SWarner Losh 	memset(cid, 0, sizeof(*cid));
371a94a63f0SWarner Losh 	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
372a94a63f0SWarner Losh 	cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
373a94a63f0SWarner Losh 	for (i = 0; i < 5; i++)
374a94a63f0SWarner Losh 		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
375a94a63f0SWarner Losh 	cid->pnm[5] = 0;
376a94a63f0SWarner Losh 	cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
377a94a63f0SWarner Losh 	cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
378a94a63f0SWarner Losh 	cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
379a94a63f0SWarner Losh 	cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
380a94a63f0SWarner Losh }
381a94a63f0SWarner Losh 
382a94a63f0SWarner Losh static void
383a94a63f0SWarner Losh mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
384a94a63f0SWarner Losh {
385a94a63f0SWarner Losh 	int i;
386a94a63f0SWarner Losh 
387a94a63f0SWarner Losh 	/* There's no version info, so we take it on faith */
388a94a63f0SWarner Losh 	memset(cid, 0, sizeof(*cid));
389a94a63f0SWarner Losh 	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
390a94a63f0SWarner Losh 	cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
391a94a63f0SWarner Losh 	for (i = 0; i < 6; i++)
392a94a63f0SWarner Losh 		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
393a94a63f0SWarner Losh 	cid->pnm[6] = 0;
394a94a63f0SWarner Losh 	cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
395a94a63f0SWarner Losh 	cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
396a94a63f0SWarner Losh 	cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
397a94a63f0SWarner Losh 	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
398a94a63f0SWarner Losh }
399a94a63f0SWarner Losh 
400a94a63f0SWarner Losh static void
401a94a63f0SWarner Losh mmc_format_card_id_string(struct sdda_softc *sc, struct mmc_params *mmcp)
402a94a63f0SWarner Losh {
403a94a63f0SWarner Losh 	char oidstr[8];
404a94a63f0SWarner Losh 	uint8_t c1;
405a94a63f0SWarner Losh 	uint8_t c2;
406a94a63f0SWarner Losh 
407a94a63f0SWarner Losh 	/*
408a94a63f0SWarner Losh 	 * Format a card ID string for use by the mmcsd driver, it's what
409a94a63f0SWarner Losh 	 * appears between the <> in the following:
410a94a63f0SWarner Losh 	 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
411a94a63f0SWarner Losh 	 * 22.5MHz/4bit/128-block
412a94a63f0SWarner Losh 	 *
413a94a63f0SWarner Losh 	 * Also format just the card serial number, which the mmcsd driver will
414a94a63f0SWarner Losh 	 * use as the disk->d_ident string.
415a94a63f0SWarner Losh 	 *
416a94a63f0SWarner Losh 	 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
417a94a63f0SWarner Losh 	 * and our max formatted length is currently 55 bytes if every field
418a94a63f0SWarner Losh 	 * contains the largest value.
419a94a63f0SWarner Losh 	 *
420a94a63f0SWarner Losh 	 * Sometimes the oid is two printable ascii chars; when it's not,
421a94a63f0SWarner Losh 	 * format it as 0xnnnn instead.
422a94a63f0SWarner Losh 	 */
423a94a63f0SWarner Losh 	c1 = (sc->cid.oid >> 8) & 0x0ff;
424a94a63f0SWarner Losh 	c2 = sc->cid.oid & 0x0ff;
425a94a63f0SWarner Losh 	if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
426a94a63f0SWarner Losh 		snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
427a94a63f0SWarner Losh 	else
428a94a63f0SWarner Losh 		snprintf(oidstr, sizeof(oidstr), "0x%04x", sc->cid.oid);
429a94a63f0SWarner Losh 	snprintf(sc->card_sn_string, sizeof(sc->card_sn_string),
430a94a63f0SWarner Losh 	    "%08X", sc->cid.psn);
431a94a63f0SWarner Losh 	snprintf(sc->card_id_string, sizeof(sc->card_id_string),
432a94a63f0SWarner Losh 		 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
433a94a63f0SWarner Losh 		 mmcp->card_features & CARD_FEATURE_MMC ? "MMC" : "SD",
434a94a63f0SWarner Losh 		 mmcp->card_features & CARD_FEATURE_SDHC ? "HC" : "",
435a94a63f0SWarner Losh 		 sc->cid.pnm, sc->cid.prv >> 4, sc->cid.prv & 0x0f,
436a94a63f0SWarner Losh 		 sc->cid.psn, sc->cid.mdt_month, sc->cid.mdt_year,
437a94a63f0SWarner Losh 		 sc->cid.mid, oidstr);
438a94a63f0SWarner Losh }
439a94a63f0SWarner Losh 
440a94a63f0SWarner Losh static int
441a94a63f0SWarner Losh sddaopen(struct disk *dp)
442a94a63f0SWarner Losh {
44396e47614SIlya Bakulin 	struct sdda_part *part;
444a94a63f0SWarner Losh 	struct cam_periph *periph;
445a94a63f0SWarner Losh 	struct sdda_softc *softc;
446a94a63f0SWarner Losh 	int error;
447a94a63f0SWarner Losh 
44896e47614SIlya Bakulin 	part = (struct sdda_part *)dp->d_drv1;
44996e47614SIlya Bakulin 	softc = part->sc;
45096e47614SIlya Bakulin 	periph = softc->periph;
45199e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
452a94a63f0SWarner Losh 		return(ENXIO);
453a94a63f0SWarner Losh 	}
454a94a63f0SWarner Losh 
455a94a63f0SWarner Losh 	cam_periph_lock(periph);
456a94a63f0SWarner Losh 	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
457a94a63f0SWarner Losh 		cam_periph_unlock(periph);
458a94a63f0SWarner Losh 		cam_periph_release(periph);
459a94a63f0SWarner Losh 		return (error);
460a94a63f0SWarner Losh 	}
461a94a63f0SWarner Losh 
46202c474b4SIlya Bakulin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaopen\n"));
463a94a63f0SWarner Losh 
46496e47614SIlya Bakulin 	part->flags |= SDDA_FLAG_OPEN;
465a94a63f0SWarner Losh 
466a94a63f0SWarner Losh 	cam_periph_unhold(periph);
467a94a63f0SWarner Losh 	cam_periph_unlock(periph);
468a94a63f0SWarner Losh 	return (0);
469a94a63f0SWarner Losh }
470a94a63f0SWarner Losh 
471a94a63f0SWarner Losh static int
472a94a63f0SWarner Losh sddaclose(struct disk *dp)
473a94a63f0SWarner Losh {
47496e47614SIlya Bakulin 	struct sdda_part *part;
475a94a63f0SWarner Losh 	struct	cam_periph *periph;
476a94a63f0SWarner Losh 	struct	sdda_softc *softc;
477a94a63f0SWarner Losh 
47896e47614SIlya Bakulin 	part = (struct sdda_part *)dp->d_drv1;
47996e47614SIlya Bakulin 	softc = part->sc;
48096e47614SIlya Bakulin 	periph = softc->periph;
48196e47614SIlya Bakulin 	part->flags &= ~SDDA_FLAG_OPEN;
482a94a63f0SWarner Losh 
483a94a63f0SWarner Losh 	cam_periph_lock(periph);
484a94a63f0SWarner Losh 
48502c474b4SIlya Bakulin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaclose\n"));
486a94a63f0SWarner Losh 
487a94a63f0SWarner Losh 	while (softc->refcount != 0)
488a94a63f0SWarner Losh 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "sddaclose", 1);
489a94a63f0SWarner Losh 	cam_periph_unlock(periph);
490a94a63f0SWarner Losh 	cam_periph_release(periph);
491a94a63f0SWarner Losh 	return (0);
492a94a63f0SWarner Losh }
493a94a63f0SWarner Losh 
494a94a63f0SWarner Losh static void
495a94a63f0SWarner Losh sddaschedule(struct cam_periph *periph)
496a94a63f0SWarner Losh {
497a94a63f0SWarner Losh 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
49896e47614SIlya Bakulin 	struct sdda_part *part;
49996e47614SIlya Bakulin 	struct bio *bp;
50096e47614SIlya Bakulin 	int i;
501a94a63f0SWarner Losh 
502a94a63f0SWarner Losh 	/* Check if we have more work to do. */
50396e47614SIlya Bakulin 	/* Find partition that has outstanding commands. Prefer current partition. */
50496e47614SIlya Bakulin 	bp = bioq_first(&softc->part[softc->part_curr]->bio_queue);
50596e47614SIlya Bakulin 	if (bp == NULL) {
50696e47614SIlya Bakulin 		for (i = 0; i < MMC_PART_MAX; i++) {
50796e47614SIlya Bakulin 			if ((part = softc->part[i]) != NULL &&
50896e47614SIlya Bakulin 			    (bp = bioq_first(&softc->part[i]->bio_queue)) != NULL)
50996e47614SIlya Bakulin 				break;
51096e47614SIlya Bakulin 		}
51196e47614SIlya Bakulin 	}
51296e47614SIlya Bakulin 	if (bp != NULL) {
513a94a63f0SWarner Losh 		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
514a94a63f0SWarner Losh 	}
515a94a63f0SWarner Losh }
516a94a63f0SWarner Losh 
517a94a63f0SWarner Losh /*
518a94a63f0SWarner Losh  * Actually translate the requested transfer into one the physical driver
519a94a63f0SWarner Losh  * can understand.  The transfer is described by a buf and will include
520a94a63f0SWarner Losh  * only one physical transfer.
521a94a63f0SWarner Losh  */
522a94a63f0SWarner Losh static void
523a94a63f0SWarner Losh sddastrategy(struct bio *bp)
524a94a63f0SWarner Losh {
525a94a63f0SWarner Losh 	struct cam_periph *periph;
52696e47614SIlya Bakulin 	struct sdda_part *part;
527a94a63f0SWarner Losh 	struct sdda_softc *softc;
528a94a63f0SWarner Losh 
52996e47614SIlya Bakulin 	part = (struct sdda_part *)bp->bio_disk->d_drv1;
53096e47614SIlya Bakulin 	softc = part->sc;
53196e47614SIlya Bakulin 	periph = softc->periph;
532a94a63f0SWarner Losh 
533a94a63f0SWarner Losh 	cam_periph_lock(periph);
534a94a63f0SWarner Losh 
535a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastrategy(%p)\n", bp));
536a94a63f0SWarner Losh 
537a94a63f0SWarner Losh 	/*
538a94a63f0SWarner Losh 	 * If the device has been made invalid, error out
539a94a63f0SWarner Losh 	 */
540a94a63f0SWarner Losh 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
541a94a63f0SWarner Losh 		cam_periph_unlock(periph);
542a94a63f0SWarner Losh 		biofinish(bp, NULL, ENXIO);
543a94a63f0SWarner Losh 		return;
544a94a63f0SWarner Losh 	}
545a94a63f0SWarner Losh 
546a94a63f0SWarner Losh 	/*
547a94a63f0SWarner Losh 	 * Place it in the queue of disk activities for this disk
548a94a63f0SWarner Losh 	 */
54996e47614SIlya Bakulin 	bioq_disksort(&part->bio_queue, bp);
550a94a63f0SWarner Losh 
551a94a63f0SWarner Losh 	/*
552a94a63f0SWarner Losh 	 * Schedule ourselves for performing the work.
553a94a63f0SWarner Losh 	 */
554a94a63f0SWarner Losh 	sddaschedule(periph);
555a94a63f0SWarner Losh 	cam_periph_unlock(periph);
556a94a63f0SWarner Losh 
557a94a63f0SWarner Losh 	return;
558a94a63f0SWarner Losh }
559a94a63f0SWarner Losh 
560a94a63f0SWarner Losh static void
561a94a63f0SWarner Losh sddainit(void)
562a94a63f0SWarner Losh {
563a94a63f0SWarner Losh 	cam_status status;
564a94a63f0SWarner Losh 
565a94a63f0SWarner Losh 	/*
566a94a63f0SWarner Losh 	 * Install a global async callback.  This callback will
567a94a63f0SWarner Losh 	 * receive async callbacks like "new device found".
568a94a63f0SWarner Losh 	 */
569a94a63f0SWarner Losh 	status = xpt_register_async(AC_FOUND_DEVICE, sddaasync, NULL, NULL);
570a94a63f0SWarner Losh 
571a94a63f0SWarner Losh 	if (status != CAM_REQ_CMP) {
572a94a63f0SWarner Losh 		printf("sdda: Failed to attach master async callback "
573a94a63f0SWarner Losh 		       "due to status 0x%x!\n", status);
574a94a63f0SWarner Losh 	}
575a94a63f0SWarner Losh }
576a94a63f0SWarner Losh 
577a94a63f0SWarner Losh /*
578a94a63f0SWarner Losh  * Callback from GEOM, called when it has finished cleaning up its
579a94a63f0SWarner Losh  * resources.
580a94a63f0SWarner Losh  */
581a94a63f0SWarner Losh static void
582a94a63f0SWarner Losh sddadiskgonecb(struct disk *dp)
583a94a63f0SWarner Losh {
584a94a63f0SWarner Losh 	struct cam_periph *periph;
58596e47614SIlya Bakulin 	struct sdda_part *part;
586a94a63f0SWarner Losh 
58796e47614SIlya Bakulin 	part = (struct sdda_part *)dp->d_drv1;
58896e47614SIlya Bakulin 	periph = part->sc->periph;
589a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddadiskgonecb\n"));
590a94a63f0SWarner Losh 
591a94a63f0SWarner Losh 	cam_periph_release(periph);
592a94a63f0SWarner Losh }
593a94a63f0SWarner Losh 
594a94a63f0SWarner Losh static void
595a94a63f0SWarner Losh sddaoninvalidate(struct cam_periph *periph)
596a94a63f0SWarner Losh {
597a94a63f0SWarner Losh 	struct sdda_softc *softc;
59896e47614SIlya Bakulin 	struct sdda_part *part;
599a94a63f0SWarner Losh 
600a94a63f0SWarner Losh 	softc = (struct sdda_softc *)periph->softc;
601a94a63f0SWarner Losh 
602a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaoninvalidate\n"));
603a94a63f0SWarner Losh 
604a94a63f0SWarner Losh 	/*
605a94a63f0SWarner Losh 	 * De-register any async callbacks.
606a94a63f0SWarner Losh 	 */
607a94a63f0SWarner Losh 	xpt_register_async(0, sddaasync, periph, periph->path);
608a94a63f0SWarner Losh 
609a94a63f0SWarner Losh 	/*
610a94a63f0SWarner Losh 	 * Return all queued I/O with ENXIO.
611a94a63f0SWarner Losh 	 * XXX Handle any transactions queued to the card
612a94a63f0SWarner Losh 	 *     with XPT_ABORT_CCB.
613a94a63f0SWarner Losh 	 */
614a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush start\n"));
61596e47614SIlya Bakulin 	for (int i = 0; i < MMC_PART_MAX; i++) {
61696e47614SIlya Bakulin 		if ((part = softc->part[i]) != NULL) {
61796e47614SIlya Bakulin 			bioq_flush(&part->bio_queue, NULL, ENXIO);
61896e47614SIlya Bakulin 			disk_gone(part->disk);
61996e47614SIlya Bakulin 		}
62096e47614SIlya Bakulin 	}
621a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush end\n"));
622a94a63f0SWarner Losh }
623a94a63f0SWarner Losh 
624a94a63f0SWarner Losh static void
625a94a63f0SWarner Losh sddacleanup(struct cam_periph *periph)
626a94a63f0SWarner Losh {
627a94a63f0SWarner Losh 	struct sdda_softc *softc;
62896e47614SIlya Bakulin 	struct sdda_part *part;
62996e47614SIlya Bakulin 	int i;
630a94a63f0SWarner Losh 
631a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddacleanup\n"));
632a94a63f0SWarner Losh 	softc = (struct sdda_softc *)periph->softc;
633a94a63f0SWarner Losh 
634a94a63f0SWarner Losh 	cam_periph_unlock(periph);
635a94a63f0SWarner Losh 
63696e47614SIlya Bakulin 	for (i = 0; i < MMC_PART_MAX; i++) {
63796e47614SIlya Bakulin 		if ((part = softc->part[i]) != NULL) {
63896e47614SIlya Bakulin 			disk_destroy(part->disk);
63996e47614SIlya Bakulin 			free(part, M_DEVBUF);
64096e47614SIlya Bakulin 			softc->part[i] = NULL;
64196e47614SIlya Bakulin 		}
64296e47614SIlya Bakulin 	}
643a94a63f0SWarner Losh 	free(softc, M_DEVBUF);
644a94a63f0SWarner Losh 	cam_periph_lock(periph);
645a94a63f0SWarner Losh }
646a94a63f0SWarner Losh 
647a94a63f0SWarner Losh static void
648a94a63f0SWarner Losh sddaasync(void *callback_arg, u_int32_t code,
649a94a63f0SWarner Losh 	struct cam_path *path, void *arg)
650a94a63f0SWarner Losh {
651a94a63f0SWarner Losh 	struct ccb_getdev cgd;
652a94a63f0SWarner Losh 	struct cam_periph *periph;
653a94a63f0SWarner Losh 	struct sdda_softc *softc;
654a94a63f0SWarner Losh 
655a94a63f0SWarner Losh 	periph = (struct cam_periph *)callback_arg;
656a94a63f0SWarner Losh         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddaasync(code=%d)\n", code));
657a94a63f0SWarner Losh 	switch (code) {
658a94a63f0SWarner Losh 	case AC_FOUND_DEVICE:
659a94a63f0SWarner Losh 	{
660a94a63f0SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_FOUND_DEVICE\n"));
661a94a63f0SWarner Losh 		struct ccb_getdev *cgd;
662a94a63f0SWarner Losh 		cam_status status;
663a94a63f0SWarner Losh 
664a94a63f0SWarner Losh 		cgd = (struct ccb_getdev *)arg;
665a94a63f0SWarner Losh 		if (cgd == NULL)
666a94a63f0SWarner Losh 			break;
667a94a63f0SWarner Losh 
668a94a63f0SWarner Losh 		if (cgd->protocol != PROTO_MMCSD)
669a94a63f0SWarner Losh 			break;
670a94a63f0SWarner Losh 
671a94a63f0SWarner Losh 		if (!(path->device->mmc_ident_data.card_features & CARD_FEATURE_MEMORY)) {
672a94a63f0SWarner Losh 			CAM_DEBUG(path, CAM_DEBUG_TRACE, ("No memory on the card!\n"));
673a94a63f0SWarner Losh 			break;
674a94a63f0SWarner Losh 		}
675a94a63f0SWarner Losh 
676a94a63f0SWarner Losh 		/*
677a94a63f0SWarner Losh 		 * Allocate a peripheral instance for
678a94a63f0SWarner Losh 		 * this device and start the probe
679a94a63f0SWarner Losh 		 * process.
680a94a63f0SWarner Losh 		 */
681a94a63f0SWarner Losh 		status = cam_periph_alloc(sddaregister, sddaoninvalidate,
682a94a63f0SWarner Losh 					  sddacleanup, sddastart,
683a94a63f0SWarner Losh 					  "sdda", CAM_PERIPH_BIO,
684a94a63f0SWarner Losh 					  path, sddaasync,
685a94a63f0SWarner Losh 					  AC_FOUND_DEVICE, cgd);
686a94a63f0SWarner Losh 
687a94a63f0SWarner Losh 		if (status != CAM_REQ_CMP
688a94a63f0SWarner Losh 		 && status != CAM_REQ_INPROG)
689a94a63f0SWarner Losh 			printf("sddaasync: Unable to attach to new device "
690a94a63f0SWarner Losh 				"due to status 0x%x\n", status);
691a94a63f0SWarner Losh 		break;
692a94a63f0SWarner Losh 	}
693a94a63f0SWarner Losh 	case AC_GETDEV_CHANGED:
694a94a63f0SWarner Losh 	{
695a94a63f0SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_GETDEV_CHANGED\n"));
696a94a63f0SWarner Losh 		softc = (struct sdda_softc *)periph->softc;
697ec5325dbSEdward Tomasz Napierala 		memset(&cgd, 0, sizeof(cgd));
698a94a63f0SWarner Losh 		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
699a94a63f0SWarner Losh 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
700a94a63f0SWarner Losh 		xpt_action((union ccb *)&cgd);
701a94a63f0SWarner Losh 		cam_periph_async(periph, code, path, arg);
702a94a63f0SWarner Losh 		break;
703a94a63f0SWarner Losh 	}
704a94a63f0SWarner Losh 	case AC_ADVINFO_CHANGED:
705a94a63f0SWarner Losh 	{
706a94a63f0SWarner Losh 		uintptr_t buftype;
70796e47614SIlya Bakulin 		int i;
70896e47614SIlya Bakulin 
709a94a63f0SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_ADVINFO_CHANGED\n"));
710a94a63f0SWarner Losh 		buftype = (uintptr_t)arg;
711a94a63f0SWarner Losh 		if (buftype == CDAI_TYPE_PHYS_PATH) {
712a94a63f0SWarner Losh 			struct sdda_softc *softc;
71396e47614SIlya Bakulin 			struct sdda_part *part;
714a94a63f0SWarner Losh 
715a94a63f0SWarner Losh 			softc = periph->softc;
71696e47614SIlya Bakulin 			for (i = 0; i < MMC_PART_MAX; i++) {
71796e47614SIlya Bakulin 				if ((part = softc->part[i]) != NULL) {
71896e47614SIlya Bakulin 					disk_attr_changed(part->disk, "GEOM::physpath",
719a94a63f0SWarner Losh 					    M_NOWAIT);
720a94a63f0SWarner Losh 				}
721a94a63f0SWarner Losh 			}
72296e47614SIlya Bakulin 		}
72396e47614SIlya Bakulin 		break;
724a94a63f0SWarner Losh 	}
725a94a63f0SWarner Losh 	default:
726a94a63f0SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> default?!\n"));
727a94a63f0SWarner Losh 		cam_periph_async(periph, code, path, arg);
728a94a63f0SWarner Losh 		break;
729a94a63f0SWarner Losh 	}
730a94a63f0SWarner Losh }
731a94a63f0SWarner Losh 
732a94a63f0SWarner Losh static int
733a94a63f0SWarner Losh sddagetattr(struct bio *bp)
734a94a63f0SWarner Losh {
735a94a63f0SWarner Losh 	struct cam_periph *periph;
73696e47614SIlya Bakulin 	struct sdda_softc *softc;
73796e47614SIlya Bakulin 	struct sdda_part *part;
73896e47614SIlya Bakulin 	int ret;
739a94a63f0SWarner Losh 
74096e47614SIlya Bakulin 	part = (struct sdda_part *)bp->bio_disk->d_drv1;
74196e47614SIlya Bakulin 	softc = part->sc;
74296e47614SIlya Bakulin 	periph = softc->periph;
743a94a63f0SWarner Losh 	cam_periph_lock(periph);
744a94a63f0SWarner Losh 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
745a94a63f0SWarner Losh 	    periph->path);
746a94a63f0SWarner Losh 	cam_periph_unlock(periph);
747a94a63f0SWarner Losh 	if (ret == 0)
748a94a63f0SWarner Losh 		bp->bio_completed = bp->bio_length;
74996e47614SIlya Bakulin 	return (ret);
750a94a63f0SWarner Losh }
751a94a63f0SWarner Losh 
752a94a63f0SWarner Losh static cam_status
753a94a63f0SWarner Losh sddaregister(struct cam_periph *periph, void *arg)
754a94a63f0SWarner Losh {
755a94a63f0SWarner Losh 	struct sdda_softc *softc;
756a94a63f0SWarner Losh 	struct ccb_getdev *cgd;
757a94a63f0SWarner Losh 	union ccb *request_ccb;	/* CCB representing the probe request */
758a94a63f0SWarner Losh 
759a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaregister\n"));
760a94a63f0SWarner Losh 	cgd = (struct ccb_getdev *)arg;
761a94a63f0SWarner Losh 	if (cgd == NULL) {
762a94a63f0SWarner Losh 		printf("sddaregister: no getdev CCB, can't register device\n");
763a94a63f0SWarner Losh 		return (CAM_REQ_CMP_ERR);
764a94a63f0SWarner Losh 	}
765a94a63f0SWarner Losh 
766a94a63f0SWarner Losh 	softc = (struct sdda_softc *)malloc(sizeof(*softc), M_DEVBUF,
767a94a63f0SWarner Losh 	    M_NOWAIT|M_ZERO);
768a94a63f0SWarner Losh 	if (softc == NULL) {
769a94a63f0SWarner Losh 		printf("sddaregister: Unable to probe new device. "
770a94a63f0SWarner Losh 		    "Unable to allocate softc\n");
771a94a63f0SWarner Losh 		return (CAM_REQ_CMP_ERR);
772a94a63f0SWarner Losh 	}
773a94a63f0SWarner Losh 
774a94a63f0SWarner Losh 	softc->state = SDDA_STATE_INIT;
775a94a63f0SWarner Losh 	softc->mmcdata =
776a94a63f0SWarner Losh 		(struct mmc_data *)malloc(sizeof(struct mmc_data), M_DEVBUF, M_NOWAIT|M_ZERO);
7770660cfa0SIlya Bakulin 	if (softc->mmcdata == NULL) {
7780660cfa0SIlya Bakulin 		printf("sddaregister: Unable to probe new device. "
7790660cfa0SIlya Bakulin 		    "Unable to allocate mmcdata\n");
7801e5d7335SBjoern A. Zeeb 		free(softc, M_DEVBUF);
7810660cfa0SIlya Bakulin 		return (CAM_REQ_CMP_ERR);
7820660cfa0SIlya Bakulin 	}
783a94a63f0SWarner Losh 	periph->softc = softc;
78496e47614SIlya Bakulin 	softc->periph = periph;
785a94a63f0SWarner Losh 
786a94a63f0SWarner Losh 	request_ccb = (union ccb*) arg;
787a94a63f0SWarner Losh 	xpt_schedule(periph, CAM_PRIORITY_XPT);
788a94a63f0SWarner Losh 	TASK_INIT(&softc->start_init_task, 0, sdda_start_init_task, periph);
789a94a63f0SWarner Losh 	taskqueue_enqueue(taskqueue_thread, &softc->start_init_task);
790a94a63f0SWarner Losh 
791a94a63f0SWarner Losh 	return (CAM_REQ_CMP);
792a94a63f0SWarner Losh }
793a94a63f0SWarner Losh 
794a94a63f0SWarner Losh static int
795a94a63f0SWarner Losh mmc_exec_app_cmd(struct cam_periph *periph, union ccb *ccb,
796a94a63f0SWarner Losh 	struct mmc_command *cmd) {
797a94a63f0SWarner Losh 	int err;
798a94a63f0SWarner Losh 
799a94a63f0SWarner Losh 	/* Send APP_CMD first */
800a94a63f0SWarner Losh 	memset(&ccb->mmcio.cmd, 0, sizeof(struct mmc_command));
801a94a63f0SWarner Losh 	memset(&ccb->mmcio.stop, 0, sizeof(struct mmc_command));
802a94a63f0SWarner Losh 	cam_fill_mmcio(&ccb->mmcio,
803a94a63f0SWarner Losh 		       /*retries*/ 0,
804a94a63f0SWarner Losh 		       /*cbfcnp*/ NULL,
805a94a63f0SWarner Losh 		       /*flags*/ CAM_DIR_NONE,
806a94a63f0SWarner Losh 		       /*mmc_opcode*/ MMC_APP_CMD,
807a94a63f0SWarner Losh 		       /*mmc_arg*/ get_rca(periph) << 16,
808a94a63f0SWarner Losh 		       /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_AC,
809a94a63f0SWarner Losh 		       /*mmc_data*/ NULL,
810a94a63f0SWarner Losh 		       /*timeout*/ 0);
811a94a63f0SWarner Losh 
8121a22fb3fSIlya Bakulin 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8131a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
814a94a63f0SWarner Losh 	if (err != 0)
8151a22fb3fSIlya Bakulin 		return (err);
816a94a63f0SWarner Losh 	if (!(ccb->mmcio.cmd.resp[0] & R1_APP_CMD))
8171a22fb3fSIlya Bakulin 		return (EIO);
818a94a63f0SWarner Losh 
819a94a63f0SWarner Losh 	/* Now exec actual command */
820a94a63f0SWarner Losh 	int flags = 0;
821a94a63f0SWarner Losh 	if (cmd->data != NULL) {
822a94a63f0SWarner Losh 		ccb->mmcio.cmd.data = cmd->data;
823a94a63f0SWarner Losh 		if (cmd->data->flags & MMC_DATA_READ)
824a94a63f0SWarner Losh 			flags |= CAM_DIR_IN;
825a94a63f0SWarner Losh 		if (cmd->data->flags & MMC_DATA_WRITE)
826a94a63f0SWarner Losh 			flags |= CAM_DIR_OUT;
827a94a63f0SWarner Losh 	} else flags = CAM_DIR_NONE;
828a94a63f0SWarner Losh 
829a94a63f0SWarner Losh 	cam_fill_mmcio(&ccb->mmcio,
830a94a63f0SWarner Losh 		       /*retries*/ 0,
831a94a63f0SWarner Losh 		       /*cbfcnp*/ NULL,
832a94a63f0SWarner Losh 		       /*flags*/ flags,
833a94a63f0SWarner Losh 		       /*mmc_opcode*/ cmd->opcode,
834a94a63f0SWarner Losh 		       /*mmc_arg*/ cmd->arg,
835a94a63f0SWarner Losh 		       /*mmc_flags*/ cmd->flags,
836a94a63f0SWarner Losh 		       /*mmc_data*/ cmd->data,
837a94a63f0SWarner Losh 		       /*timeout*/ 0);
838a94a63f0SWarner Losh 
8391a22fb3fSIlya Bakulin 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8401a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
8411a22fb3fSIlya Bakulin 	if (err != 0)
8421a22fb3fSIlya Bakulin 		return (err);
843a94a63f0SWarner Losh 	memcpy(cmd->resp, ccb->mmcio.cmd.resp, sizeof(cmd->resp));
844a94a63f0SWarner Losh 	cmd->error = ccb->mmcio.cmd.error;
8451a22fb3fSIlya Bakulin 
8461a22fb3fSIlya Bakulin 	return (0);
847a94a63f0SWarner Losh }
848a94a63f0SWarner Losh 
849a94a63f0SWarner Losh static int
850a94a63f0SWarner Losh mmc_app_get_scr(struct cam_periph *periph, union ccb *ccb, uint32_t *rawscr) {
851a94a63f0SWarner Losh 	int err;
852a94a63f0SWarner Losh 	struct mmc_command cmd;
853a94a63f0SWarner Losh 	struct mmc_data d;
854a94a63f0SWarner Losh 
855a94a63f0SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
8564c4200c6SIlya Bakulin 	memset(&d, 0, sizeof(d));
857a94a63f0SWarner Losh 
858a94a63f0SWarner Losh 	memset(rawscr, 0, 8);
859a94a63f0SWarner Losh 	cmd.opcode = ACMD_SEND_SCR;
860a94a63f0SWarner Losh 	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
861a94a63f0SWarner Losh 	cmd.arg = 0;
862a94a63f0SWarner Losh 
863a94a63f0SWarner Losh 	d.data = rawscr;
864a94a63f0SWarner Losh 	d.len = 8;
865a94a63f0SWarner Losh 	d.flags = MMC_DATA_READ;
866a94a63f0SWarner Losh 	cmd.data = &d;
867a94a63f0SWarner Losh 
868a94a63f0SWarner Losh 	err = mmc_exec_app_cmd(periph, ccb, &cmd);
869a94a63f0SWarner Losh 	rawscr[0] = be32toh(rawscr[0]);
870a94a63f0SWarner Losh 	rawscr[1] = be32toh(rawscr[1]);
871a94a63f0SWarner Losh 	return (err);
872a94a63f0SWarner Losh }
873a94a63f0SWarner Losh 
874a94a63f0SWarner Losh static int
875a94a63f0SWarner Losh mmc_send_ext_csd(struct cam_periph *periph, union ccb *ccb,
876a94a63f0SWarner Losh 		 uint8_t *rawextcsd, size_t buf_len) {
877a94a63f0SWarner Losh 	int err;
878a94a63f0SWarner Losh 	struct mmc_data d;
879a94a63f0SWarner Losh 
880a94a63f0SWarner Losh 	KASSERT(buf_len == 512, ("Buffer for ext csd must be 512 bytes"));
8810660cfa0SIlya Bakulin 	memset(&d, 0, sizeof(d));
882a94a63f0SWarner Losh 	d.data = rawextcsd;
883a94a63f0SWarner Losh 	d.len = buf_len;
884a94a63f0SWarner Losh 	d.flags = MMC_DATA_READ;
885a94a63f0SWarner Losh 	memset(d.data, 0, d.len);
886a94a63f0SWarner Losh 
887a94a63f0SWarner Losh 	cam_fill_mmcio(&ccb->mmcio,
888a94a63f0SWarner Losh 		       /*retries*/ 0,
889a94a63f0SWarner Losh 		       /*cbfcnp*/ NULL,
890a94a63f0SWarner Losh 		       /*flags*/ CAM_DIR_IN,
891a94a63f0SWarner Losh 		       /*mmc_opcode*/ MMC_SEND_EXT_CSD,
892a94a63f0SWarner Losh 		       /*mmc_arg*/ 0,
893a94a63f0SWarner Losh 		       /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
894a94a63f0SWarner Losh 		       /*mmc_data*/ &d,
895a94a63f0SWarner Losh 		       /*timeout*/ 0);
896a94a63f0SWarner Losh 
8971a22fb3fSIlya Bakulin 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8981a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
89996e47614SIlya Bakulin 	return (err);
900a94a63f0SWarner Losh }
901a94a63f0SWarner Losh 
902a94a63f0SWarner Losh static void
903a94a63f0SWarner Losh mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
904a94a63f0SWarner Losh {
905a94a63f0SWarner Losh 	unsigned int scr_struct;
906a94a63f0SWarner Losh 
907a94a63f0SWarner Losh 	memset(scr, 0, sizeof(*scr));
908a94a63f0SWarner Losh 
909a94a63f0SWarner Losh 	scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
910a94a63f0SWarner Losh 	if (scr_struct != 0) {
911a94a63f0SWarner Losh 		printf("Unrecognised SCR structure version %d\n",
912a94a63f0SWarner Losh 		    scr_struct);
913a94a63f0SWarner Losh 		return;
914a94a63f0SWarner Losh 	}
915a94a63f0SWarner Losh 	scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
916a94a63f0SWarner Losh 	scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
917a94a63f0SWarner Losh }
918a94a63f0SWarner Losh 
91996e47614SIlya Bakulin static inline void
92096e47614SIlya Bakulin mmc_switch_fill_mmcio(union ccb *ccb,
92196e47614SIlya Bakulin     uint8_t set, uint8_t index, uint8_t value, u_int timeout)
922a94a63f0SWarner Losh {
923a94a63f0SWarner Losh 	int arg = (MMC_SWITCH_FUNC_WR << 24) |
924a94a63f0SWarner Losh 	    (index << 16) |
925a94a63f0SWarner Losh 	    (value << 8) |
926a94a63f0SWarner Losh 	    set;
92796e47614SIlya Bakulin 
928a94a63f0SWarner Losh 	cam_fill_mmcio(&ccb->mmcio,
929a94a63f0SWarner Losh 		       /*retries*/ 0,
930a94a63f0SWarner Losh 		       /*cbfcnp*/ NULL,
931a94a63f0SWarner Losh 		       /*flags*/ CAM_DIR_NONE,
932a94a63f0SWarner Losh 		       /*mmc_opcode*/ MMC_SWITCH_FUNC,
933a94a63f0SWarner Losh 		       /*mmc_arg*/ arg,
934a94a63f0SWarner Losh 		       /*mmc_flags*/ MMC_RSP_R1B | MMC_CMD_AC,
935a94a63f0SWarner Losh 		       /*mmc_data*/ NULL,
93696e47614SIlya Bakulin 		       /*timeout*/ timeout);
93796e47614SIlya Bakulin }
938a94a63f0SWarner Losh 
93996e47614SIlya Bakulin static int
940d670d951SIlya Bakulin mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca)
941d670d951SIlya Bakulin {
9421a22fb3fSIlya Bakulin 	int flags, err;
943d670d951SIlya Bakulin 
944d670d951SIlya Bakulin 	flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
945d670d951SIlya Bakulin 	cam_fill_mmcio(&ccb->mmcio,
946d670d951SIlya Bakulin 		       /*retries*/ 0,
947d670d951SIlya Bakulin 		       /*cbfcnp*/ NULL,
948d670d951SIlya Bakulin 		       /*flags*/ CAM_DIR_IN,
949d670d951SIlya Bakulin 		       /*mmc_opcode*/ MMC_SELECT_CARD,
950d670d951SIlya Bakulin 		       /*mmc_arg*/ rca << 16,
951d670d951SIlya Bakulin 		       /*mmc_flags*/ flags,
952d670d951SIlya Bakulin 		       /*mmc_data*/ NULL,
953d670d951SIlya Bakulin 		       /*timeout*/ 0);
954d670d951SIlya Bakulin 
955d670d951SIlya Bakulin 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
9561a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
9571a22fb3fSIlya Bakulin 	return (err);
958d670d951SIlya Bakulin }
959d670d951SIlya Bakulin 
960d670d951SIlya Bakulin static int
96196e47614SIlya Bakulin mmc_switch(struct cam_periph *periph, union ccb *ccb,
96296e47614SIlya Bakulin     uint8_t set, uint8_t index, uint8_t value, u_int timeout)
96396e47614SIlya Bakulin {
9641a22fb3fSIlya Bakulin 	int err;
96596e47614SIlya Bakulin 
96696e47614SIlya Bakulin 	mmc_switch_fill_mmcio(ccb, set, index, value, timeout);
967a94a63f0SWarner Losh 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
9681a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
9691a22fb3fSIlya Bakulin 	return (err);
970a94a63f0SWarner Losh }
971a94a63f0SWarner Losh 
97296e47614SIlya Bakulin static uint32_t
97396e47614SIlya Bakulin mmc_get_spec_vers(struct cam_periph *periph) {
97496e47614SIlya Bakulin 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
97596e47614SIlya Bakulin 
97696e47614SIlya Bakulin 	return (softc->csd.spec_vers);
97796e47614SIlya Bakulin }
97896e47614SIlya Bakulin 
97996e47614SIlya Bakulin static uint64_t
98096e47614SIlya Bakulin mmc_get_media_size(struct cam_periph *periph) {
98196e47614SIlya Bakulin 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
98296e47614SIlya Bakulin 
98396e47614SIlya Bakulin 	return (softc->mediasize);
98496e47614SIlya Bakulin }
98596e47614SIlya Bakulin 
98696e47614SIlya Bakulin static uint32_t
98796e47614SIlya Bakulin mmc_get_cmd6_timeout(struct cam_periph *periph)
98896e47614SIlya Bakulin {
98996e47614SIlya Bakulin 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
99096e47614SIlya Bakulin 
99196e47614SIlya Bakulin 	if (mmc_get_spec_vers(periph) >= 6)
99296e47614SIlya Bakulin 		return (softc->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME] * 10);
99396e47614SIlya Bakulin 	return (500 * 1000);
99496e47614SIlya Bakulin }
99596e47614SIlya Bakulin 
996a94a63f0SWarner Losh static int
997a94a63f0SWarner Losh mmc_sd_switch(struct cam_periph *periph, union ccb *ccb,
998a94a63f0SWarner Losh 	      uint8_t mode, uint8_t grp, uint8_t value,
999a94a63f0SWarner Losh 	      uint8_t *res) {
1000a94a63f0SWarner Losh 	struct mmc_data mmc_d;
1001d670d951SIlya Bakulin 	uint32_t arg;
10021a22fb3fSIlya Bakulin 	int err;
1003a94a63f0SWarner Losh 
1004a94a63f0SWarner Losh 	memset(res, 0, 64);
10050660cfa0SIlya Bakulin 	memset(&mmc_d, 0, sizeof(mmc_d));
1006a94a63f0SWarner Losh 	mmc_d.len = 64;
1007a94a63f0SWarner Losh 	mmc_d.data = res;
1008a94a63f0SWarner Losh 	mmc_d.flags = MMC_DATA_READ;
1009a94a63f0SWarner Losh 
1010d670d951SIlya Bakulin 	arg = mode << 31;			/* 0 - check, 1 - set */
1011d670d951SIlya Bakulin 	arg |= 0x00FFFFFF;
1012d670d951SIlya Bakulin 	arg &= ~(0xF << (grp * 4));
1013d670d951SIlya Bakulin 	arg |= value << (grp * 4);
1014d670d951SIlya Bakulin 
1015a94a63f0SWarner Losh 	cam_fill_mmcio(&ccb->mmcio,
1016a94a63f0SWarner Losh 		       /*retries*/ 0,
1017a94a63f0SWarner Losh 		       /*cbfcnp*/ NULL,
1018a94a63f0SWarner Losh 		       /*flags*/ CAM_DIR_IN,
1019a94a63f0SWarner Losh 		       /*mmc_opcode*/ SD_SWITCH_FUNC,
1020d670d951SIlya Bakulin 		       /*mmc_arg*/ arg,
1021a94a63f0SWarner Losh 		       /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
1022a94a63f0SWarner Losh 		       /*mmc_data*/ &mmc_d,
1023a94a63f0SWarner Losh 		       /*timeout*/ 0);
1024a94a63f0SWarner Losh 
1025a94a63f0SWarner Losh 	cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
10261a22fb3fSIlya Bakulin 	err = mmc_handle_reply(ccb);
10271a22fb3fSIlya Bakulin 	return (err);
1028a94a63f0SWarner Losh }
1029a94a63f0SWarner Losh 
1030a94a63f0SWarner Losh static int
1031a94a63f0SWarner Losh mmc_set_timing(struct cam_periph *periph,
1032a94a63f0SWarner Losh 	       union ccb *ccb,
1033a94a63f0SWarner Losh 	       enum mmc_bus_timing timing)
1034a94a63f0SWarner Losh {
1035a94a63f0SWarner Losh 	u_char switch_res[64];
1036a94a63f0SWarner Losh 	int err;
1037a94a63f0SWarner Losh 	uint8_t	value;
103896e47614SIlya Bakulin 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1039a94a63f0SWarner Losh 	struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1040a94a63f0SWarner Losh 
1041a94a63f0SWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1042a94a63f0SWarner Losh 		  ("mmc_set_timing(timing=%d)", timing));
1043a94a63f0SWarner Losh 	switch (timing) {
1044a94a63f0SWarner Losh 	case bus_timing_normal:
1045a94a63f0SWarner Losh 		value = 0;
1046a94a63f0SWarner Losh 		break;
1047a94a63f0SWarner Losh 	case bus_timing_hs:
1048a94a63f0SWarner Losh 		value = 1;
1049a94a63f0SWarner Losh 		break;
1050a94a63f0SWarner Losh 	default:
1051a94a63f0SWarner Losh 		return (MMC_ERR_INVALID);
1052a94a63f0SWarner Losh 	}
1053a94a63f0SWarner Losh 	if (mmcp->card_features & CARD_FEATURE_MMC) {
1054a94a63f0SWarner Losh 		err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
105596e47614SIlya Bakulin 		    EXT_CSD_HS_TIMING, value, softc->cmd6_time);
1056a94a63f0SWarner Losh 	} else {
1057a94a63f0SWarner Losh 		err = mmc_sd_switch(periph, ccb, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, value, switch_res);
1058a94a63f0SWarner Losh 	}
1059a94a63f0SWarner Losh 
1060a94a63f0SWarner Losh 	/* Set high-speed timing on the host */
1061a94a63f0SWarner Losh 	struct ccb_trans_settings_mmc *cts;
1062a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
1063a94a63f0SWarner Losh 	ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1064a94a63f0SWarner Losh 	ccb->ccb_h.flags = CAM_DIR_NONE;
1065a94a63f0SWarner Losh 	ccb->ccb_h.retry_count = 0;
1066a94a63f0SWarner Losh 	ccb->ccb_h.timeout = 100;
1067a94a63f0SWarner Losh 	ccb->ccb_h.cbfcnp = NULL;
1068a94a63f0SWarner Losh 	cts->ios.timing = timing;
1069a94a63f0SWarner Losh 	cts->ios_valid = MMC_BT;
1070a94a63f0SWarner Losh 	xpt_action(ccb);
1071a94a63f0SWarner Losh 
1072a94a63f0SWarner Losh 	return (err);
1073a94a63f0SWarner Losh }
1074a94a63f0SWarner Losh 
1075a94a63f0SWarner Losh static void
1076a94a63f0SWarner Losh sdda_start_init_task(void *context, int pending) {
1077a94a63f0SWarner Losh 	union ccb *new_ccb;
1078a94a63f0SWarner Losh 	struct cam_periph *periph;
1079a94a63f0SWarner Losh 
1080a94a63f0SWarner Losh 	periph = (struct cam_periph *)context;
1081a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init_task\n"));
1082a94a63f0SWarner Losh 	new_ccb = xpt_alloc_ccb();
1083a94a63f0SWarner Losh 	xpt_setup_ccb(&new_ccb->ccb_h, periph->path,
1084a94a63f0SWarner Losh 		      CAM_PRIORITY_NONE);
1085a94a63f0SWarner Losh 
1086a94a63f0SWarner Losh 	cam_periph_lock(periph);
1087f2df51ecSEmmanuel Vadot 	cam_periph_hold(periph, PRIBIO|PCATCH);
1088a94a63f0SWarner Losh 	sdda_start_init(context, new_ccb);
1089f2df51ecSEmmanuel Vadot 	cam_periph_unhold(periph);
1090a94a63f0SWarner Losh 	cam_periph_unlock(periph);
1091a94a63f0SWarner Losh 	xpt_free_ccb(new_ccb);
1092a94a63f0SWarner Losh }
1093a94a63f0SWarner Losh 
1094a94a63f0SWarner Losh static void
1095a94a63f0SWarner Losh sdda_set_bus_width(struct cam_periph *periph, union ccb *ccb, int width) {
109696e47614SIlya Bakulin 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1097a94a63f0SWarner Losh 	struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1098a94a63f0SWarner Losh 	int err;
1099a94a63f0SWarner Losh 
1100a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_set_bus_width\n"));
1101a94a63f0SWarner Losh 
1102a94a63f0SWarner Losh 	/* First set for the card, then for the host */
1103a94a63f0SWarner Losh 	if (mmcp->card_features & CARD_FEATURE_MMC) {
1104a94a63f0SWarner Losh 		uint8_t	value;
1105a94a63f0SWarner Losh 		switch (width) {
1106a94a63f0SWarner Losh 		case bus_width_1:
1107a94a63f0SWarner Losh 			value = EXT_CSD_BUS_WIDTH_1;
1108a94a63f0SWarner Losh 			break;
1109a94a63f0SWarner Losh 		case bus_width_4:
1110a94a63f0SWarner Losh 			value = EXT_CSD_BUS_WIDTH_4;
1111a94a63f0SWarner Losh 			break;
1112a94a63f0SWarner Losh 		case bus_width_8:
1113a94a63f0SWarner Losh 			value = EXT_CSD_BUS_WIDTH_8;
1114a94a63f0SWarner Losh 			break;
1115a94a63f0SWarner Losh 		default:
1116a94a63f0SWarner Losh 			panic("Invalid bus width %d", width);
1117a94a63f0SWarner Losh 		}
1118a94a63f0SWarner Losh 		err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
111996e47614SIlya Bakulin 		    EXT_CSD_BUS_WIDTH, value, softc->cmd6_time);
1120a94a63f0SWarner Losh 	} else {
1121a94a63f0SWarner Losh 		/* For SD cards we send ACMD6 with the required bus width in arg */
1122a94a63f0SWarner Losh 		struct mmc_command cmd;
1123a94a63f0SWarner Losh 		memset(&cmd, 0, sizeof(struct mmc_command));
1124a94a63f0SWarner Losh 		cmd.opcode = ACMD_SET_BUS_WIDTH;
1125a94a63f0SWarner Losh 		cmd.arg = width;
1126a94a63f0SWarner Losh 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1127a94a63f0SWarner Losh 		err = mmc_exec_app_cmd(periph, ccb, &cmd);
1128a94a63f0SWarner Losh 	}
1129a94a63f0SWarner Losh 
1130a94a63f0SWarner Losh 	if (err != MMC_ERR_NONE) {
1131a94a63f0SWarner Losh 		CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Error %d when setting bus width on the card\n", err));
1132a94a63f0SWarner Losh 		return;
1133a94a63f0SWarner Losh 	}
1134a94a63f0SWarner Losh 	/* Now card is done, set the host to the same width */
1135a94a63f0SWarner Losh 	struct ccb_trans_settings_mmc *cts;
1136a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
1137a94a63f0SWarner Losh 	ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1138a94a63f0SWarner Losh 	ccb->ccb_h.flags = CAM_DIR_NONE;
1139a94a63f0SWarner Losh 	ccb->ccb_h.retry_count = 0;
1140a94a63f0SWarner Losh 	ccb->ccb_h.timeout = 100;
1141a94a63f0SWarner Losh 	ccb->ccb_h.cbfcnp = NULL;
1142a94a63f0SWarner Losh 	cts->ios.bus_width = width;
1143a94a63f0SWarner Losh 	cts->ios_valid = MMC_BW;
1144a94a63f0SWarner Losh 	xpt_action(ccb);
1145a94a63f0SWarner Losh }
1146a94a63f0SWarner Losh 
114796e47614SIlya Bakulin static inline const char
114896e47614SIlya Bakulin *part_type(u_int type)
114996e47614SIlya Bakulin {
115096e47614SIlya Bakulin 
115196e47614SIlya Bakulin 	switch (type) {
115296e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_RPMB:
115396e47614SIlya Bakulin 		return ("RPMB");
115496e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_DEFAULT:
115596e47614SIlya Bakulin 		return ("default");
115696e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_BOOT0:
115796e47614SIlya Bakulin 		return ("boot0");
115896e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_BOOT1:
115996e47614SIlya Bakulin 		return ("boot1");
116096e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_GP0:
116196e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_GP1:
116296e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_GP2:
116396e47614SIlya Bakulin 	case EXT_CSD_PART_CONFIG_ACC_GP3:
116496e47614SIlya Bakulin 		return ("general purpose");
116596e47614SIlya Bakulin 	default:
116696e47614SIlya Bakulin 		return ("(unknown type)");
1167a94a63f0SWarner Losh 	}
1168a94a63f0SWarner Losh }
1169a94a63f0SWarner Losh 
117096e47614SIlya Bakulin static inline const char
117196e47614SIlya Bakulin *bus_width_str(enum mmc_bus_width w)
117296e47614SIlya Bakulin {
117396e47614SIlya Bakulin 
117496e47614SIlya Bakulin 	switch (w) {
117596e47614SIlya Bakulin 	case bus_width_1:
117696e47614SIlya Bakulin 		return ("1-bit");
117796e47614SIlya Bakulin 	case bus_width_4:
117896e47614SIlya Bakulin 		return ("4-bit");
117996e47614SIlya Bakulin 	case bus_width_8:
118096e47614SIlya Bakulin 		return ("8-bit");
118196e47614SIlya Bakulin 	}
118296e47614SIlya Bakulin }
118396e47614SIlya Bakulin 
118496e47614SIlya Bakulin static uint32_t
118596e47614SIlya Bakulin sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb)
118696e47614SIlya Bakulin {
118796e47614SIlya Bakulin 	struct ccb_trans_settings_mmc *cts;
118896e47614SIlya Bakulin 
118996e47614SIlya Bakulin 	cts = &ccb->cts.proto_specific.mmc;
119096e47614SIlya Bakulin 
119196e47614SIlya Bakulin 	ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
119296e47614SIlya Bakulin 	ccb->ccb_h.flags = CAM_DIR_NONE;
119396e47614SIlya Bakulin 	ccb->ccb_h.retry_count = 0;
119496e47614SIlya Bakulin 	ccb->ccb_h.timeout = 100;
119596e47614SIlya Bakulin 	ccb->ccb_h.cbfcnp = NULL;
119696e47614SIlya Bakulin 	xpt_action(ccb);
119796e47614SIlya Bakulin 
119896e47614SIlya Bakulin 	if (ccb->ccb_h.status != CAM_REQ_CMP)
119996e47614SIlya Bakulin 		panic("Cannot get host caps");
120096e47614SIlya Bakulin 	return (cts->host_caps);
120196e47614SIlya Bakulin }
120296e47614SIlya Bakulin 
12035d20e651SIlya Bakulin static uint32_t
12045d20e651SIlya Bakulin sdda_get_max_data(struct cam_periph *periph, union ccb *ccb)
12055d20e651SIlya Bakulin {
12065d20e651SIlya Bakulin 	struct ccb_trans_settings_mmc *cts;
12075d20e651SIlya Bakulin 
12085d20e651SIlya Bakulin 	cts = &ccb->cts.proto_specific.mmc;
12095d20e651SIlya Bakulin 	memset(cts, 0, sizeof(struct ccb_trans_settings_mmc));
12105d20e651SIlya Bakulin 
12115d20e651SIlya Bakulin 	ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
12125d20e651SIlya Bakulin 	ccb->ccb_h.flags = CAM_DIR_NONE;
12135d20e651SIlya Bakulin 	ccb->ccb_h.retry_count = 0;
12145d20e651SIlya Bakulin 	ccb->ccb_h.timeout = 100;
12155d20e651SIlya Bakulin 	ccb->ccb_h.cbfcnp = NULL;
12165d20e651SIlya Bakulin 	xpt_action(ccb);
12175d20e651SIlya Bakulin 
12185d20e651SIlya Bakulin 	if (ccb->ccb_h.status != CAM_REQ_CMP)
12195d20e651SIlya Bakulin 		panic("Cannot get host max data");
12205d20e651SIlya Bakulin 	KASSERT(cts->host_max_data != 0, ("host_max_data == 0?!"));
12215d20e651SIlya Bakulin 	return (cts->host_max_data);
12225d20e651SIlya Bakulin }
12235d20e651SIlya Bakulin 
1224a94a63f0SWarner Losh static void
122596e47614SIlya Bakulin sdda_start_init(void *context, union ccb *start_ccb)
122696e47614SIlya Bakulin {
122796e47614SIlya Bakulin 	struct cam_periph *periph = (struct cam_periph *)context;
122896e47614SIlya Bakulin 	struct ccb_trans_settings_mmc *cts;
122996e47614SIlya Bakulin 	uint32_t host_caps;
123096e47614SIlya Bakulin 	uint32_t sec_count;
1231a94a63f0SWarner Losh 	int err;
123296e47614SIlya Bakulin 	int host_f_max;
1233fd7371f7SEmmanuel Vadot 	uint8_t card_type;
1234a94a63f0SWarner Losh 
1235a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init\n"));
1236a94a63f0SWarner Losh 	/* periph was held for us when this task was enqueued */
1237a94a63f0SWarner Losh 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1238a94a63f0SWarner Losh 		cam_periph_release(periph);
1239a94a63f0SWarner Losh 		return;
1240a94a63f0SWarner Losh 	}
1241a94a63f0SWarner Losh 
1242a94a63f0SWarner Losh 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1243a94a63f0SWarner Losh 	struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1244a94a63f0SWarner Losh 	struct cam_ed *device = periph->path->device;
1245a94a63f0SWarner Losh 
1246a94a63f0SWarner Losh 	if (mmcp->card_features & CARD_FEATURE_MMC) {
1247a94a63f0SWarner Losh 		mmc_decode_csd_mmc(mmcp->card_csd, &softc->csd);
1248a94a63f0SWarner Losh 		mmc_decode_cid_mmc(mmcp->card_cid, &softc->cid);
124996e47614SIlya Bakulin 		if (mmc_get_spec_vers(periph) >= 4) {
1250a94a63f0SWarner Losh 			err = mmc_send_ext_csd(periph, start_ccb,
1251a94a63f0SWarner Losh 					       (uint8_t *)&softc->raw_ext_csd,
1252a94a63f0SWarner Losh 					       sizeof(softc->raw_ext_csd));
125396e47614SIlya Bakulin 			if (err != 0) {
125496e47614SIlya Bakulin 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
125596e47614SIlya Bakulin 				    ("Cannot read EXT_CSD, err %d", err));
125696e47614SIlya Bakulin 				return;
125796e47614SIlya Bakulin 			}
125896e47614SIlya Bakulin 		}
1259a94a63f0SWarner Losh 	} else {
1260a94a63f0SWarner Losh 		mmc_decode_csd_sd(mmcp->card_csd, &softc->csd);
1261a94a63f0SWarner Losh 		mmc_decode_cid_sd(mmcp->card_cid, &softc->cid);
1262a94a63f0SWarner Losh 	}
1263a94a63f0SWarner Losh 
1264a94a63f0SWarner Losh 	softc->sector_count = softc->csd.capacity / 512;
1265a94a63f0SWarner Losh 	softc->mediasize = softc->csd.capacity;
126696e47614SIlya Bakulin 	softc->cmd6_time = mmc_get_cmd6_timeout(periph);
1267a94a63f0SWarner Losh 
1268a94a63f0SWarner Losh 	/* MMC >= 4.x have EXT_CSD that has its own opinion about capacity */
126996e47614SIlya Bakulin 	if (mmc_get_spec_vers(periph) >= 4) {
127096e47614SIlya Bakulin 		sec_count = softc->raw_ext_csd[EXT_CSD_SEC_CNT] +
1271a94a63f0SWarner Losh 		    (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1272a94a63f0SWarner Losh 		    (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1273a94a63f0SWarner Losh 		    (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1274a94a63f0SWarner Losh 		if (sec_count != 0) {
1275a94a63f0SWarner Losh 			softc->sector_count = sec_count;
1276a94a63f0SWarner Losh 			softc->mediasize = softc->sector_count * 512;
1277a94a63f0SWarner Losh 			/* FIXME: there should be a better name for this option...*/
1278a94a63f0SWarner Losh 			mmcp->card_features |= CARD_FEATURE_SDHC;
1279a94a63f0SWarner Losh 		}
1280a94a63f0SWarner Losh 	}
1281a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1282a94a63f0SWarner Losh 	    ("Capacity: %"PRIu64", sectors: %"PRIu64"\n",
1283a94a63f0SWarner Losh 		softc->mediasize,
1284a94a63f0SWarner Losh 		softc->sector_count));
1285a94a63f0SWarner Losh 	mmc_format_card_id_string(softc, mmcp);
1286a94a63f0SWarner Losh 
1287a94a63f0SWarner Losh 	/* Update info for CAM */
1288a94a63f0SWarner Losh 	device->serial_num_len = strlen(softc->card_sn_string);
128996e47614SIlya Bakulin 	device->serial_num = (u_int8_t *)malloc((device->serial_num_len + 1),
1290a94a63f0SWarner Losh 	    M_CAMXPT, M_NOWAIT);
12917e06495bSJung-uk Kim 	strlcpy(device->serial_num, softc->card_sn_string, device->serial_num_len + 1);
1292a94a63f0SWarner Losh 
1293a94a63f0SWarner Losh 	device->device_id_len = strlen(softc->card_id_string);
129496e47614SIlya Bakulin 	device->device_id = (u_int8_t *)malloc((device->device_id_len + 1),
1295a94a63f0SWarner Losh 	    M_CAMXPT, M_NOWAIT);
12967e06495bSJung-uk Kim 	strlcpy(device->device_id, softc->card_id_string, device->device_id_len + 1);
1297a94a63f0SWarner Losh 
1298a94a63f0SWarner Losh 	strlcpy(mmcp->model, softc->card_id_string, sizeof(mmcp->model));
1299a94a63f0SWarner Losh 
1300a94a63f0SWarner Losh 	/* Set the clock frequency that the card can handle */
1301a94a63f0SWarner Losh 	cts = &start_ccb->cts.proto_specific.mmc;
1302a94a63f0SWarner Losh 
1303a94a63f0SWarner Losh 	/* First, get the host's max freq */
1304a94a63f0SWarner Losh 	start_ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1305a94a63f0SWarner Losh 	start_ccb->ccb_h.flags = CAM_DIR_NONE;
1306a94a63f0SWarner Losh 	start_ccb->ccb_h.retry_count = 0;
1307a94a63f0SWarner Losh 	start_ccb->ccb_h.timeout = 100;
1308a94a63f0SWarner Losh 	start_ccb->ccb_h.cbfcnp = NULL;
1309a94a63f0SWarner Losh 	xpt_action(start_ccb);
1310a94a63f0SWarner Losh 
1311a94a63f0SWarner Losh 	if (start_ccb->ccb_h.status != CAM_REQ_CMP)
1312a94a63f0SWarner Losh 		panic("Cannot get max host freq");
131396e47614SIlya Bakulin 	host_f_max = cts->host_f_max;
131496e47614SIlya Bakulin 	host_caps = cts->host_caps;
1315a94a63f0SWarner Losh 	if (cts->ios.bus_width != bus_width_1)
1316a94a63f0SWarner Losh 		panic("Bus width in ios is not 1-bit");
1317a94a63f0SWarner Losh 
1318a94a63f0SWarner Losh 	/* Now check if the card supports High-speed */
1319a94a63f0SWarner Losh 	softc->card_f_max = softc->csd.tran_speed;
1320a94a63f0SWarner Losh 
1321a94a63f0SWarner Losh 	if (host_caps & MMC_CAP_HSPEED) {
1322a94a63f0SWarner Losh 		/* Find out if the card supports High speed timing */
1323a94a63f0SWarner Losh 		if (mmcp->card_features & CARD_FEATURE_SD20) {
1324a94a63f0SWarner Losh 			/* Get and decode SCR */
13254c4200c6SIlya Bakulin 			uint32_t rawscr[2];
1326a94a63f0SWarner Losh 			uint8_t res[64];
13274c4200c6SIlya Bakulin 			if (mmc_app_get_scr(periph, start_ccb, rawscr)) {
1328a94a63f0SWarner Losh 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Cannot get SCR\n"));
1329a94a63f0SWarner Losh 				goto finish_hs_tests;
1330a94a63f0SWarner Losh 			}
13314c4200c6SIlya Bakulin 			mmc_app_decode_scr(rawscr, &softc->scr);
1332a94a63f0SWarner Losh 
1333a94a63f0SWarner Losh 			if ((softc->scr.sda_vsn >= 1) && (softc->csd.ccc & (1<<10))) {
1334a94a63f0SWarner Losh 				mmc_sd_switch(periph, start_ccb, SD_SWITCH_MODE_CHECK,
1335a94a63f0SWarner Losh 					      SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, res);
1336a94a63f0SWarner Losh 				if (res[13] & 2) {
1337a94a63f0SWarner Losh 					CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS\n"));
1338a94a63f0SWarner Losh 					softc->card_f_max = SD_HS_MAX;
1339a94a63f0SWarner Losh 				}
1340d670d951SIlya Bakulin 
1341d670d951SIlya Bakulin 				/*
1342d670d951SIlya Bakulin 				 * We deselect then reselect the card here.  Some cards
1343d670d951SIlya Bakulin 				 * become unselected and timeout with the above two
1344d670d951SIlya Bakulin 				 * commands, although the state tables / diagrams in the
1345d670d951SIlya Bakulin 				 * standard suggest they go back to the transfer state.
1346d670d951SIlya Bakulin 				 * Other cards don't become deselected, and if we
1347d670d951SIlya Bakulin 				 * attempt to blindly re-select them, we get timeout
1348d670d951SIlya Bakulin 				 * errors from some controllers.  So we deselect then
1349d670d951SIlya Bakulin 				 * reselect to handle all situations.
1350d670d951SIlya Bakulin 				 */
1351d670d951SIlya Bakulin 				mmc_select_card(periph, start_ccb, 0);
1352d670d951SIlya Bakulin 				mmc_select_card(periph, start_ccb, get_rca(periph));
1353a94a63f0SWarner Losh 			} else {
1354a94a63f0SWarner Losh 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Not trying the switch\n"));
1355a94a63f0SWarner Losh 				goto finish_hs_tests;
1356a94a63f0SWarner Losh 			}
1357a94a63f0SWarner Losh 		}
1358a94a63f0SWarner Losh 
135996e47614SIlya Bakulin 		if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
1360fd7371f7SEmmanuel Vadot 			card_type = softc->raw_ext_csd[EXT_CSD_CARD_TYPE];
1361fd7371f7SEmmanuel Vadot 			if (card_type & EXT_CSD_CARD_TYPE_HS_52)
1362a94a63f0SWarner Losh 				softc->card_f_max = MMC_TYPE_HS_52_MAX;
1363fd7371f7SEmmanuel Vadot 			else if (card_type & EXT_CSD_CARD_TYPE_HS_26)
1364a94a63f0SWarner Losh 				softc->card_f_max = MMC_TYPE_HS_26_MAX;
1365fd7371f7SEmmanuel Vadot 			if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 &&
1366fd7371f7SEmmanuel Vadot 			    (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1367fd7371f7SEmmanuel Vadot 				setbit(&softc->timings, bus_timing_mmc_ddr52);
1368fd7371f7SEmmanuel Vadot 				setbit(&softc->vccq_120, bus_timing_mmc_ddr52);
1369fd7371f7SEmmanuel Vadot 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.2V\n"));
1370fd7371f7SEmmanuel Vadot 			}
1371fd7371f7SEmmanuel Vadot 			if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 &&
1372fd7371f7SEmmanuel Vadot 			    (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1373fd7371f7SEmmanuel Vadot 				setbit(&softc->timings, bus_timing_mmc_ddr52);
1374fd7371f7SEmmanuel Vadot 				setbit(&softc->vccq_180, bus_timing_mmc_ddr52);
1375fd7371f7SEmmanuel Vadot 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.8V\n"));
1376fd7371f7SEmmanuel Vadot 			}
1377fd7371f7SEmmanuel Vadot 			if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) != 0 &&
1378fd7371f7SEmmanuel Vadot 			    (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1379fd7371f7SEmmanuel Vadot 				setbit(&softc->timings, bus_timing_mmc_hs200);
1380fd7371f7SEmmanuel Vadot 				setbit(&softc->vccq_120, bus_timing_mmc_hs200);
1381fd7371f7SEmmanuel Vadot 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.2V\n"));
1382fd7371f7SEmmanuel Vadot 			}
1383fd7371f7SEmmanuel Vadot 			if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) != 0 &&
1384fd7371f7SEmmanuel Vadot 			    (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1385fd7371f7SEmmanuel Vadot 				setbit(&softc->timings, bus_timing_mmc_hs200);
1386fd7371f7SEmmanuel Vadot 				setbit(&softc->vccq_180, bus_timing_mmc_hs200);
1387fd7371f7SEmmanuel Vadot 				CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.8V\n"));
1388fd7371f7SEmmanuel Vadot 			}
1389a94a63f0SWarner Losh 		}
1390a94a63f0SWarner Losh 	}
1391a94a63f0SWarner Losh 	int f_max;
1392a94a63f0SWarner Losh finish_hs_tests:
1393a94a63f0SWarner Losh 	f_max = min(host_f_max, softc->card_f_max);
1394a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Set SD freq to %d MHz (min out of host f=%d MHz and card f=%d MHz)\n", f_max  / 1000000, host_f_max / 1000000, softc->card_f_max / 1000000));
1395a94a63f0SWarner Losh 
1396d670d951SIlya Bakulin 	/* Enable high-speed timing on the card */
1397d670d951SIlya Bakulin 	if (f_max > 25000000) {
1398d670d951SIlya Bakulin 		err = mmc_set_timing(periph, start_ccb, bus_timing_hs);
1399d670d951SIlya Bakulin 		if (err != MMC_ERR_NONE) {
1400d670d951SIlya Bakulin 			CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("Cannot switch card to high-speed mode"));
1401d670d951SIlya Bakulin 			f_max = 25000000;
1402d670d951SIlya Bakulin 		}
1403d670d951SIlya Bakulin 	}
1404fd7371f7SEmmanuel Vadot 	/* If possible, set lower-level signaling */
1405fd7371f7SEmmanuel Vadot 	enum mmc_bus_timing timing;
1406fd7371f7SEmmanuel Vadot 	/* FIXME: MMCCAM supports max. bus_timing_mmc_ddr52 at the moment. */
1407fd7371f7SEmmanuel Vadot 	for (timing = bus_timing_mmc_ddr52; timing > bus_timing_normal; timing--) {
1408fd7371f7SEmmanuel Vadot 		if (isset(&softc->vccq_120, timing)) {
1409fd7371f7SEmmanuel Vadot 			/* Set VCCQ = 1.2V */
1410fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1411fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.flags = CAM_DIR_NONE;
1412fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.retry_count = 0;
1413fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.timeout = 100;
1414fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.cbfcnp = NULL;
1415fd7371f7SEmmanuel Vadot 			cts->ios.vccq = vccq_120;
1416fd7371f7SEmmanuel Vadot 			cts->ios_valid = MMC_VCCQ;
1417fd7371f7SEmmanuel Vadot 			xpt_action(start_ccb);
1418fd7371f7SEmmanuel Vadot 			break;
1419fd7371f7SEmmanuel Vadot 		} else if (isset(&softc->vccq_180, timing)) {
1420fd7371f7SEmmanuel Vadot 			/* Set VCCQ = 1.8V */
1421fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1422fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.flags = CAM_DIR_NONE;
1423fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.retry_count = 0;
1424fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.timeout = 100;
1425fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.cbfcnp = NULL;
1426fd7371f7SEmmanuel Vadot 			cts->ios.vccq = vccq_180;
1427fd7371f7SEmmanuel Vadot 			cts->ios_valid = MMC_VCCQ;
1428fd7371f7SEmmanuel Vadot 			xpt_action(start_ccb);
1429fd7371f7SEmmanuel Vadot 			break;
1430fd7371f7SEmmanuel Vadot 		} else {
1431fd7371f7SEmmanuel Vadot 			/* Set VCCQ = 3.3V */
1432fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1433fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.flags = CAM_DIR_NONE;
1434fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.retry_count = 0;
1435fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.timeout = 100;
1436fd7371f7SEmmanuel Vadot 			start_ccb->ccb_h.cbfcnp = NULL;
1437fd7371f7SEmmanuel Vadot 			cts->ios.vccq = vccq_330;
1438fd7371f7SEmmanuel Vadot 			cts->ios_valid = MMC_VCCQ;
1439fd7371f7SEmmanuel Vadot 			xpt_action(start_ccb);
1440fd7371f7SEmmanuel Vadot 			break;
1441fd7371f7SEmmanuel Vadot 		}
1442fd7371f7SEmmanuel Vadot 	}
1443fd7371f7SEmmanuel Vadot 
1444d670d951SIlya Bakulin 	/* Set frequency on the controller */
1445a94a63f0SWarner Losh 	start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1446a94a63f0SWarner Losh 	start_ccb->ccb_h.flags = CAM_DIR_NONE;
1447a94a63f0SWarner Losh 	start_ccb->ccb_h.retry_count = 0;
1448a94a63f0SWarner Losh 	start_ccb->ccb_h.timeout = 100;
1449a94a63f0SWarner Losh 	start_ccb->ccb_h.cbfcnp = NULL;
1450a94a63f0SWarner Losh 	cts->ios.clock = f_max;
1451a94a63f0SWarner Losh 	cts->ios_valid = MMC_CLK;
1452a94a63f0SWarner Losh 	xpt_action(start_ccb);
1453a94a63f0SWarner Losh 
1454a94a63f0SWarner Losh 	/* Set bus width */
1455a94a63f0SWarner Losh 	enum mmc_bus_width desired_bus_width = bus_width_1;
1456a94a63f0SWarner Losh 	enum mmc_bus_width max_host_bus_width =
1457a94a63f0SWarner Losh 		(host_caps & MMC_CAP_8_BIT_DATA ? bus_width_8 :
1458a94a63f0SWarner Losh 		 host_caps & MMC_CAP_4_BIT_DATA ? bus_width_4 : bus_width_1);
1459a94a63f0SWarner Losh 	enum mmc_bus_width max_card_bus_width = bus_width_1;
1460a94a63f0SWarner Losh 	if (mmcp->card_features & CARD_FEATURE_SD20 &&
1461a94a63f0SWarner Losh 	    softc->scr.bus_widths & SD_SCR_BUS_WIDTH_4)
1462a94a63f0SWarner Losh 		max_card_bus_width = bus_width_4;
1463a94a63f0SWarner Losh 	/*
1464a94a63f0SWarner Losh 	 * Unlike SD, MMC cards don't have any information about supported bus width...
1465a94a63f0SWarner Losh 	 * So we need to perform read/write test to find out the width.
1466a94a63f0SWarner Losh 	 */
1467a94a63f0SWarner Losh 	/* TODO: figure out bus width for MMC; use 8-bit for now (to test on BBB) */
1468a94a63f0SWarner Losh 	if (mmcp->card_features & CARD_FEATURE_MMC)
1469a94a63f0SWarner Losh 		max_card_bus_width = bus_width_8;
1470a94a63f0SWarner Losh 
1471a94a63f0SWarner Losh 	desired_bus_width = min(max_host_bus_width, max_card_bus_width);
1472a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1473a94a63f0SWarner Losh 		  ("Set bus width to %s (min of host %s and card %s)\n",
1474a94a63f0SWarner Losh 		   bus_width_str(desired_bus_width),
1475a94a63f0SWarner Losh 		   bus_width_str(max_host_bus_width),
1476a94a63f0SWarner Losh 		   bus_width_str(max_card_bus_width)));
1477a94a63f0SWarner Losh 	sdda_set_bus_width(periph, start_ccb, desired_bus_width);
1478a94a63f0SWarner Losh 
1479a94a63f0SWarner Losh 	softc->state = SDDA_STATE_NORMAL;
148096e47614SIlya Bakulin 
1481e70d59c0SEmmanuel Vadot 	cam_periph_unhold(periph);
148296e47614SIlya Bakulin 	/* MMC partitions support */
148396e47614SIlya Bakulin 	if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
148496e47614SIlya Bakulin 		sdda_process_mmc_partitions(periph, start_ccb);
148596e47614SIlya Bakulin 	} else if (mmcp->card_features & CARD_FEATURE_SD20) {
148696e47614SIlya Bakulin 		/* For SD[HC] cards, just add one partition that is the whole card */
14872fe1b4caSEmmanuel Vadot 		if (sdda_add_part(periph, 0, "sdda",
148896e47614SIlya Bakulin 		    periph->unit_number,
148996e47614SIlya Bakulin 		    mmc_get_media_size(periph),
14902fe1b4caSEmmanuel Vadot 		    sdda_get_read_only(periph, start_ccb)) == false)
14912fe1b4caSEmmanuel Vadot 			return;
149296e47614SIlya Bakulin 		softc->part_curr = 0;
149396e47614SIlya Bakulin 	}
1494e70d59c0SEmmanuel Vadot 	cam_periph_hold(periph, PRIBIO|PCATCH);
149596e47614SIlya Bakulin 
149696e47614SIlya Bakulin 	xpt_announce_periph(periph, softc->card_id_string);
149796e47614SIlya Bakulin 	/*
149896e47614SIlya Bakulin 	 * Add async callbacks for bus reset and bus device reset calls.
149996e47614SIlya Bakulin 	 * I don't bother checking if this fails as, in most cases,
150096e47614SIlya Bakulin 	 * the system will function just fine without them and the only
150196e47614SIlya Bakulin 	 * alternative would be to not attach the device on failure.
150296e47614SIlya Bakulin 	 */
150396e47614SIlya Bakulin 	xpt_register_async(AC_LOST_DEVICE | AC_GETDEV_CHANGED |
150496e47614SIlya Bakulin 	    AC_ADVINFO_CHANGED, sddaasync, periph, periph->path);
150596e47614SIlya Bakulin }
150696e47614SIlya Bakulin 
15072fe1b4caSEmmanuel Vadot static bool
150896e47614SIlya Bakulin sdda_add_part(struct cam_periph *periph, u_int type, const char *name,
150996e47614SIlya Bakulin     u_int cnt, off_t media_size, bool ro)
151096e47614SIlya Bakulin {
151196e47614SIlya Bakulin 	struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
151296e47614SIlya Bakulin 	struct sdda_part *part;
151396e47614SIlya Bakulin 	struct ccb_pathinq cpi;
151496e47614SIlya Bakulin 
151596e47614SIlya Bakulin 	CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
151696e47614SIlya Bakulin 	    ("Partition type '%s', size %ju %s\n",
151796e47614SIlya Bakulin 	    part_type(type),
151896e47614SIlya Bakulin 	    media_size,
151996e47614SIlya Bakulin 	    ro ? "(read-only)" : ""));
152096e47614SIlya Bakulin 
152196e47614SIlya Bakulin 	part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
15222fe1b4caSEmmanuel Vadot 	    M_NOWAIT | M_ZERO);
15232fe1b4caSEmmanuel Vadot 	if (part == NULL) {
15242fe1b4caSEmmanuel Vadot 		printf("Cannot add partition for sdda\n");
15252fe1b4caSEmmanuel Vadot 		return (false);
15262fe1b4caSEmmanuel Vadot 	}
152796e47614SIlya Bakulin 
152896e47614SIlya Bakulin 	part->cnt = cnt;
152996e47614SIlya Bakulin 	part->type = type;
153096e47614SIlya Bakulin 	part->ro = ro;
153196e47614SIlya Bakulin 	part->sc = sc;
153296e47614SIlya Bakulin 	snprintf(part->name, sizeof(part->name), name, periph->unit_number);
153396e47614SIlya Bakulin 
153496e47614SIlya Bakulin 	/*
153596e47614SIlya Bakulin 	 * Due to the nature of RPMB partition it doesn't make much sense
153696e47614SIlya Bakulin 	 * to add it as a disk. It would be more appropriate to create a
153796e47614SIlya Bakulin 	 * userland tool to operate on the partition or leverage the existing
153896e47614SIlya Bakulin 	 * tools from sysutils/mmc-utils.
153996e47614SIlya Bakulin 	 */
154096e47614SIlya Bakulin 	if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
154196e47614SIlya Bakulin 		/* TODO: Create device, assign IOCTL handler */
154296e47614SIlya Bakulin 		CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
154396e47614SIlya Bakulin 		    ("Don't know what to do with RPMB partitions yet\n"));
15442fe1b4caSEmmanuel Vadot 		return (false);
154596e47614SIlya Bakulin 	}
154696e47614SIlya Bakulin 
154796e47614SIlya Bakulin 	bioq_init(&part->bio_queue);
154896e47614SIlya Bakulin 
154996e47614SIlya Bakulin 	bzero(&cpi, sizeof(cpi));
155096e47614SIlya Bakulin 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
155196e47614SIlya Bakulin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
155296e47614SIlya Bakulin 	xpt_action((union ccb *)&cpi);
155396e47614SIlya Bakulin 
155496e47614SIlya Bakulin 	/*
155596e47614SIlya Bakulin 	 * Register this media as a disk
155696e47614SIlya Bakulin 	 */
155796e47614SIlya Bakulin 	(void)cam_periph_hold(periph, PRIBIO);
155896e47614SIlya Bakulin 	cam_periph_unlock(periph);
155996e47614SIlya Bakulin 
156096e47614SIlya Bakulin 	part->disk = disk_alloc();
156196e47614SIlya Bakulin 	part->disk->d_rotation_rate = DISK_RR_NON_ROTATING;
156296e47614SIlya Bakulin 	part->disk->d_devstat = devstat_new_entry(part->name,
156396e47614SIlya Bakulin 	    cnt, 512,
156496e47614SIlya Bakulin 	    DEVSTAT_ALL_SUPPORTED,
156596e47614SIlya Bakulin 	    DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
156696e47614SIlya Bakulin 	    DEVSTAT_PRIORITY_DISK);
156796e47614SIlya Bakulin 
156896e47614SIlya Bakulin 	part->disk->d_open = sddaopen;
156996e47614SIlya Bakulin 	part->disk->d_close = sddaclose;
157096e47614SIlya Bakulin 	part->disk->d_strategy = sddastrategy;
157196e47614SIlya Bakulin 	part->disk->d_getattr = sddagetattr;
157296e47614SIlya Bakulin 	part->disk->d_gone = sddadiskgonecb;
157396e47614SIlya Bakulin 	part->disk->d_name = part->name;
157496e47614SIlya Bakulin 	part->disk->d_drv1 = part;
15755d20e651SIlya Bakulin 	part->disk->d_maxsize =
1576cd853791SKonstantin Belousov 	    MIN(maxphys, sdda_get_max_data(periph,
15775d20e651SIlya Bakulin 		    (union ccb *)&cpi) * mmc_get_sector_size(periph));
157896e47614SIlya Bakulin 	part->disk->d_unit = cnt;
157996e47614SIlya Bakulin 	part->disk->d_flags = 0;
158096e47614SIlya Bakulin 	strlcpy(part->disk->d_descr, sc->card_id_string,
158196e47614SIlya Bakulin 	    MIN(sizeof(part->disk->d_descr), sizeof(sc->card_id_string)));
158296e47614SIlya Bakulin 	strlcpy(part->disk->d_ident, sc->card_sn_string,
158396e47614SIlya Bakulin 	    MIN(sizeof(part->disk->d_ident), sizeof(sc->card_sn_string)));
158496e47614SIlya Bakulin 	part->disk->d_hba_vendor = cpi.hba_vendor;
158596e47614SIlya Bakulin 	part->disk->d_hba_device = cpi.hba_device;
158696e47614SIlya Bakulin 	part->disk->d_hba_subvendor = cpi.hba_subvendor;
158796e47614SIlya Bakulin 	part->disk->d_hba_subdevice = cpi.hba_subdevice;
1588b5961be1SEdward Tomasz Napierala 	snprintf(part->disk->d_attachment, sizeof(part->disk->d_attachment),
1589b5961be1SEdward Tomasz Napierala 	    "%s%d", cpi.dev_name, cpi.unit_number);
159096e47614SIlya Bakulin 
159196e47614SIlya Bakulin 	part->disk->d_sectorsize = mmc_get_sector_size(periph);
159296e47614SIlya Bakulin 	part->disk->d_mediasize = media_size;
159396e47614SIlya Bakulin 	part->disk->d_stripesize = 0;
159496e47614SIlya Bakulin 	part->disk->d_fwsectors = 0;
159596e47614SIlya Bakulin 	part->disk->d_fwheads = 0;
159696e47614SIlya Bakulin 
1597bf286853SEmmanuel Vadot 	if (sdda_mmcsd_compat)
1598bf286853SEmmanuel Vadot 		disk_add_alias(part->disk, "mmcsd");
1599bf286853SEmmanuel Vadot 
160096e47614SIlya Bakulin 	/*
160196e47614SIlya Bakulin 	 * Acquire a reference to the periph before we register with GEOM.
160296e47614SIlya Bakulin 	 * We'll release this reference once GEOM calls us back (via
160396e47614SIlya Bakulin 	 * sddadiskgonecb()) telling us that our provider has been freed.
160496e47614SIlya Bakulin 	 */
160596e47614SIlya Bakulin 	if (cam_periph_acquire(periph) != 0) {
160696e47614SIlya Bakulin 		xpt_print(periph->path, "%s: lost periph during "
160796e47614SIlya Bakulin 		    "registration!\n", __func__);
160896e47614SIlya Bakulin 		cam_periph_lock(periph);
16092fe1b4caSEmmanuel Vadot 		return (false);
161096e47614SIlya Bakulin 	}
161196e47614SIlya Bakulin 	disk_create(part->disk, DISK_VERSION);
161296e47614SIlya Bakulin 	cam_periph_lock(periph);
161396e47614SIlya Bakulin 	cam_periph_unhold(periph);
16142fe1b4caSEmmanuel Vadot 
16152fe1b4caSEmmanuel Vadot 	return (true);
161696e47614SIlya Bakulin }
161796e47614SIlya Bakulin 
161896e47614SIlya Bakulin /*
161996e47614SIlya Bakulin  * For MMC cards, process EXT_CSD and add partitions that are supported by
162096e47614SIlya Bakulin  * this device.
162196e47614SIlya Bakulin  */
162296e47614SIlya Bakulin static void
162396e47614SIlya Bakulin sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *ccb)
162496e47614SIlya Bakulin {
162596e47614SIlya Bakulin 	struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
162696e47614SIlya Bakulin 	struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
162796e47614SIlya Bakulin 	off_t erase_size, sector_size, size, wp_size;
162896e47614SIlya Bakulin 	int i;
162996e47614SIlya Bakulin 	const uint8_t *ext_csd;
163096e47614SIlya Bakulin 	uint8_t rev;
163196e47614SIlya Bakulin 	bool comp, ro;
163296e47614SIlya Bakulin 
163396e47614SIlya Bakulin 	ext_csd = sc->raw_ext_csd;
163496e47614SIlya Bakulin 
163596e47614SIlya Bakulin 	/*
163696e47614SIlya Bakulin 	 * Enhanced user data area and general purpose partitions are only
163796e47614SIlya Bakulin 	 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
163896e47614SIlya Bakulin 	 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
163996e47614SIlya Bakulin 	 */
164096e47614SIlya Bakulin 	rev = ext_csd[EXT_CSD_REV];
164196e47614SIlya Bakulin 
164296e47614SIlya Bakulin 	/*
164396e47614SIlya Bakulin 	 * Ignore user-creatable enhanced user data area and general purpose
164496e47614SIlya Bakulin 	 * partitions partitions as long as partitioning hasn't been finished.
164596e47614SIlya Bakulin 	 */
164696e47614SIlya Bakulin 	comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
164796e47614SIlya Bakulin 
164896e47614SIlya Bakulin 	/*
164996e47614SIlya Bakulin 	 * Add enhanced user data area slice, unless it spans the entirety of
165096e47614SIlya Bakulin 	 * the user data area.  The enhanced area is of a multiple of high
165196e47614SIlya Bakulin 	 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
165296e47614SIlya Bakulin 	 * 512 KB) and its offset given in either sectors or bytes, depending
165396e47614SIlya Bakulin 	 * on whether it's a high capacity device or not.
165496e47614SIlya Bakulin 	 * NB: The slicer and its slices need to be registered before adding
165596e47614SIlya Bakulin 	 *     the disk for the corresponding user data area as re-tasting is
165696e47614SIlya Bakulin 	 *     racy.
165796e47614SIlya Bakulin 	 */
165896e47614SIlya Bakulin 	sector_size = mmc_get_sector_size(periph);
165996e47614SIlya Bakulin 	size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
166096e47614SIlya Bakulin 		(ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
166196e47614SIlya Bakulin 		(ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
166296e47614SIlya Bakulin 	if (rev >= 4 && comp == TRUE && size > 0 &&
166396e47614SIlya Bakulin 	    (ext_csd[EXT_CSD_PART_SUPPORT] &
166496e47614SIlya Bakulin 		EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
166596e47614SIlya Bakulin 	    (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
166696e47614SIlya Bakulin 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
166796e47614SIlya Bakulin 			MMC_SECTOR_SIZE;
166896e47614SIlya Bakulin 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
166996e47614SIlya Bakulin 		size *= erase_size * wp_size;
167096e47614SIlya Bakulin 		if (size != mmc_get_media_size(periph) * sector_size) {
167196e47614SIlya Bakulin 			sc->enh_size = size;
167296e47614SIlya Bakulin 			sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
167396e47614SIlya Bakulin 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
167496e47614SIlya Bakulin 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
167596e47614SIlya Bakulin 			    (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
167696e47614SIlya Bakulin 				((mmcp->card_features & CARD_FEATURE_SDHC) ? 1: MMC_SECTOR_SIZE);
167796e47614SIlya Bakulin 		} else
167896e47614SIlya Bakulin 			CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
167996e47614SIlya Bakulin 			    ("enhanced user data area spans entire device"));
168096e47614SIlya Bakulin 	}
168196e47614SIlya Bakulin 
168296e47614SIlya Bakulin 	/*
168396e47614SIlya Bakulin 	 * Add default partition.  This may be the only one or the user
168496e47614SIlya Bakulin 	 * data area in case partitions are supported.
168596e47614SIlya Bakulin 	 */
168696e47614SIlya Bakulin 	ro = sdda_get_read_only(periph, ccb);
168796e47614SIlya Bakulin 	sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "sdda",
168896e47614SIlya Bakulin 	    periph->unit_number, mmc_get_media_size(periph), ro);
168996e47614SIlya Bakulin 	sc->part_curr = EXT_CSD_PART_CONFIG_ACC_DEFAULT;
169096e47614SIlya Bakulin 
169196e47614SIlya Bakulin 	if (mmc_get_spec_vers(periph) < 3)
169296e47614SIlya Bakulin 		return;
169396e47614SIlya Bakulin 
169496e47614SIlya Bakulin 	/* Belatedly announce enhanced user data slice. */
169596e47614SIlya Bakulin 	if (sc->enh_size != 0) {
169696e47614SIlya Bakulin 		CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
169796e47614SIlya Bakulin 		    ("enhanced user data area off 0x%jx size %ju bytes\n",
169896e47614SIlya Bakulin 			sc->enh_base, sc->enh_size));
169996e47614SIlya Bakulin 	}
170096e47614SIlya Bakulin 
170196e47614SIlya Bakulin 	/*
170296e47614SIlya Bakulin 	 * Determine partition switch timeout (provided in units of 10 ms)
170396e47614SIlya Bakulin 	 * and ensure it's at least 300 ms as some eMMC chips lie.
170496e47614SIlya Bakulin 	 */
170596e47614SIlya Bakulin 	sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
170696e47614SIlya Bakulin 	    300 * 1000);
170796e47614SIlya Bakulin 
170896e47614SIlya Bakulin 	/* Add boot partitions, which are of a fixed multiple of 128 KB. */
170996e47614SIlya Bakulin 	size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
171096e47614SIlya Bakulin 	if (size > 0 && (sdda_get_host_caps(periph, ccb) & MMC_CAP_BOOT_NOACC) == 0) {
171196e47614SIlya Bakulin 		sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT0,
171296e47614SIlya Bakulin 		    SDDA_FMT_BOOT, 0, size,
171396e47614SIlya Bakulin 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
171496e47614SIlya Bakulin 		    EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
171596e47614SIlya Bakulin 		sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT1,
171696e47614SIlya Bakulin 		    SDDA_FMT_BOOT, 1, size,
171796e47614SIlya Bakulin 		    ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
171896e47614SIlya Bakulin 		    EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
171996e47614SIlya Bakulin 	}
172096e47614SIlya Bakulin 
172196e47614SIlya Bakulin 	/* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
172296e47614SIlya Bakulin 	size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
172396e47614SIlya Bakulin 	if (rev >= 5 && size > 0)
172496e47614SIlya Bakulin 		sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_RPMB,
172596e47614SIlya Bakulin 		    SDDA_FMT_RPMB, 0, size, ro);
172696e47614SIlya Bakulin 
172796e47614SIlya Bakulin 	if (rev <= 3 || comp == FALSE)
172896e47614SIlya Bakulin 		return;
172996e47614SIlya Bakulin 
173096e47614SIlya Bakulin 	/*
173196e47614SIlya Bakulin 	 * Add general purpose partitions, which are of a multiple of high
173296e47614SIlya Bakulin 	 * capacity write protect groups, too.
173396e47614SIlya Bakulin 	 */
173496e47614SIlya Bakulin 	if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
173596e47614SIlya Bakulin 		erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
173696e47614SIlya Bakulin 			MMC_SECTOR_SIZE;
173796e47614SIlya Bakulin 		wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
173896e47614SIlya Bakulin 		for (i = 0; i < MMC_PART_GP_MAX; i++) {
173996e47614SIlya Bakulin 			size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
174096e47614SIlya Bakulin 				(ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
174196e47614SIlya Bakulin 				(ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
174296e47614SIlya Bakulin 			if (size == 0)
174396e47614SIlya Bakulin 				continue;
174496e47614SIlya Bakulin 			sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
174596e47614SIlya Bakulin 			    SDDA_FMT_GP, i, size * erase_size * wp_size, ro);
174696e47614SIlya Bakulin 		}
174796e47614SIlya Bakulin 	}
174896e47614SIlya Bakulin }
174996e47614SIlya Bakulin 
175096e47614SIlya Bakulin /*
175196e47614SIlya Bakulin  * We cannot just call mmc_switch() since it will sleep, and we are in
175296e47614SIlya Bakulin  * GEOM context and cannot sleep. Instead, create an MMCIO request to switch
175396e47614SIlya Bakulin  * partitions and send it to h/w, and upon completion resume processing
175496e47614SIlya Bakulin  * the I/O queue.
175596e47614SIlya Bakulin  * This function cannot fail, instead check switch errors in sddadone().
175696e47614SIlya Bakulin  */
175796e47614SIlya Bakulin static void
175815f4848aSAndriy Gapon sdda_init_switch_part(struct cam_periph *periph, union ccb *start_ccb,
175915f4848aSAndriy Gapon     uint8_t part)
176015f4848aSAndriy Gapon {
176196e47614SIlya Bakulin 	struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
176296e47614SIlya Bakulin 	uint8_t value;
176396e47614SIlya Bakulin 
176415f4848aSAndriy Gapon 	KASSERT(part < MMC_PART_MAX, ("%s: invalid partition index", __func__));
176596e47614SIlya Bakulin 	sc->part_requested = part;
176696e47614SIlya Bakulin 
176796e47614SIlya Bakulin 	value = (sc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
176896e47614SIlya Bakulin 	    ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
176996e47614SIlya Bakulin 
177096e47614SIlya Bakulin 	mmc_switch_fill_mmcio(start_ccb, EXT_CSD_CMD_SET_NORMAL,
177196e47614SIlya Bakulin 	    EXT_CSD_PART_CONFIG, value, sc->part_time);
177296e47614SIlya Bakulin 	start_ccb->ccb_h.cbfcnp = sddadone;
177396e47614SIlya Bakulin 
177496e47614SIlya Bakulin 	sc->outstanding_cmds++;
177596e47614SIlya Bakulin 	cam_periph_unlock(periph);
177696e47614SIlya Bakulin 	xpt_action(start_ccb);
177796e47614SIlya Bakulin 	cam_periph_lock(periph);
1778a94a63f0SWarner Losh }
1779a94a63f0SWarner Losh 
1780a94a63f0SWarner Losh /* Called with periph lock held! */
1781a94a63f0SWarner Losh static void
1782a94a63f0SWarner Losh sddastart(struct cam_periph *periph, union ccb *start_ccb)
1783a94a63f0SWarner Losh {
178496e47614SIlya Bakulin 	struct bio *bp;
1785a94a63f0SWarner Losh 	struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
178696e47614SIlya Bakulin 	struct sdda_part *part;
1787a94a63f0SWarner Losh 	struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
178815f4848aSAndriy Gapon 	uint8_t part_index;
1789a94a63f0SWarner Losh 
1790a94a63f0SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastart\n"));
1791a94a63f0SWarner Losh 
1792a94a63f0SWarner Losh 	if (softc->state != SDDA_STATE_NORMAL) {
179396e47614SIlya Bakulin 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("device is not in SDDA_STATE_NORMAL yet\n"));
1794a94a63f0SWarner Losh 		xpt_release_ccb(start_ccb);
1795a94a63f0SWarner Losh 		return;
1796a94a63f0SWarner Losh 	}
1797a94a63f0SWarner Losh 
179896e47614SIlya Bakulin 	/* Find partition that has outstanding commands.  Prefer current partition. */
17994dfdaf4dSAndriy Gapon 	part_index = softc->part_curr;
180096e47614SIlya Bakulin 	part = softc->part[softc->part_curr];
180196e47614SIlya Bakulin 	bp = bioq_first(&part->bio_queue);
180296e47614SIlya Bakulin 	if (bp == NULL) {
180396e47614SIlya Bakulin 		for (part_index = 0; part_index < MMC_PART_MAX; part_index++) {
180496e47614SIlya Bakulin 			if ((part = softc->part[part_index]) != NULL &&
180596e47614SIlya Bakulin 			    (bp = bioq_first(&softc->part[part_index]->bio_queue)) != NULL)
180696e47614SIlya Bakulin 				break;
180796e47614SIlya Bakulin 		}
180896e47614SIlya Bakulin 	}
1809a94a63f0SWarner Losh 	if (bp == NULL) {
1810a94a63f0SWarner Losh 		xpt_release_ccb(start_ccb);
1811a94a63f0SWarner Losh 		return;
1812a94a63f0SWarner Losh 	}
181396e47614SIlya Bakulin 	if (part_index != softc->part_curr) {
181496e47614SIlya Bakulin 		CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
181596e47614SIlya Bakulin 		    ("Partition  %d -> %d\n", softc->part_curr, part_index));
181696e47614SIlya Bakulin 		/*
181796e47614SIlya Bakulin 		 * According to section "6.2.2 Command restrictions" of the eMMC
181896e47614SIlya Bakulin 		 * specification v5.1, CMD19/CMD21 aren't allowed to be used with
181996e47614SIlya Bakulin 		 * RPMB partitions.  So we pause re-tuning along with triggering
182096e47614SIlya Bakulin 		 * it up-front to decrease the likelihood of re-tuning becoming
182196e47614SIlya Bakulin 		 * necessary while accessing an RPMB partition.  Consequently, an
182296e47614SIlya Bakulin 		 * RPMB partition should immediately be switched away from again
182396e47614SIlya Bakulin 		 * after an access in order to allow for re-tuning to take place
182496e47614SIlya Bakulin 		 * anew.
182596e47614SIlya Bakulin 		 */
182696e47614SIlya Bakulin 		/* TODO: pause retune if switching to RPMB partition */
182796e47614SIlya Bakulin 		softc->state = SDDA_STATE_PART_SWITCH;
182896e47614SIlya Bakulin 		sdda_init_switch_part(periph, start_ccb, part_index);
182996e47614SIlya Bakulin 		return;
183096e47614SIlya Bakulin 	}
183196e47614SIlya Bakulin 
183296e47614SIlya Bakulin 	bioq_remove(&part->bio_queue, bp);
1833a94a63f0SWarner Losh 
1834a94a63f0SWarner Losh 	switch (bp->bio_cmd) {
1835a94a63f0SWarner Losh 	case BIO_WRITE:
1836a94a63f0SWarner Losh 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_WRITE\n"));
183796e47614SIlya Bakulin 		part->flags |= SDDA_FLAG_DIRTY;
1838a94a63f0SWarner Losh 		/* FALLTHROUGH */
1839a94a63f0SWarner Losh 	case BIO_READ:
1840a94a63f0SWarner Losh 	{
184196e47614SIlya Bakulin 		struct ccb_mmcio *mmcio;
1842a94a63f0SWarner Losh 		uint64_t blockno = bp->bio_pblkno;
1843a94a63f0SWarner Losh 		uint16_t count = bp->bio_bcount / 512;
1844a94a63f0SWarner Losh 		uint16_t opcode;
1845a94a63f0SWarner Losh 
184696e47614SIlya Bakulin 		if (bp->bio_cmd == BIO_READ)
184796e47614SIlya Bakulin 			CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_READ\n"));
184896e47614SIlya Bakulin 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
184996e47614SIlya Bakulin 		    ("Block %"PRIu64" cnt %u\n", blockno, count));
1850a94a63f0SWarner Losh 
1851a94a63f0SWarner Losh 		/* Construct new MMC command */
1852a94a63f0SWarner Losh 		if (bp->bio_cmd == BIO_READ) {
1853a94a63f0SWarner Losh 			if (count > 1)
1854a94a63f0SWarner Losh 				opcode = MMC_READ_MULTIPLE_BLOCK;
1855a94a63f0SWarner Losh 			else
1856a94a63f0SWarner Losh 				opcode = MMC_READ_SINGLE_BLOCK;
1857a94a63f0SWarner Losh 		} else {
1858a94a63f0SWarner Losh 			if (count > 1)
1859a94a63f0SWarner Losh 				opcode = MMC_WRITE_MULTIPLE_BLOCK;
1860a94a63f0SWarner Losh 			else
1861a94a63f0SWarner Losh 				opcode = MMC_WRITE_BLOCK;
1862a94a63f0SWarner Losh 		}
1863a94a63f0SWarner Losh 
1864a94a63f0SWarner Losh 		start_ccb->ccb_h.func_code = XPT_MMC_IO;
1865a94a63f0SWarner Losh 		start_ccb->ccb_h.flags = (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : CAM_DIR_OUT);
1866a94a63f0SWarner Losh 		start_ccb->ccb_h.retry_count = 0;
1867a94a63f0SWarner Losh 		start_ccb->ccb_h.timeout = 15 * 1000;
1868a94a63f0SWarner Losh 		start_ccb->ccb_h.cbfcnp = sddadone;
1869a94a63f0SWarner Losh 
1870a94a63f0SWarner Losh 		mmcio = &start_ccb->mmcio;
1871a94a63f0SWarner Losh 		mmcio->cmd.opcode = opcode;
1872a94a63f0SWarner Losh 		mmcio->cmd.arg = blockno;
1873a94a63f0SWarner Losh 		if (!(mmcp->card_features & CARD_FEATURE_SDHC))
1874a94a63f0SWarner Losh 			mmcio->cmd.arg <<= 9;
1875a94a63f0SWarner Losh 
1876a94a63f0SWarner Losh 		mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1877a94a63f0SWarner Losh 		mmcio->cmd.data = softc->mmcdata;
18780660cfa0SIlya Bakulin 		memset(mmcio->cmd.data, 0, sizeof(struct mmc_data));
1879a94a63f0SWarner Losh 		mmcio->cmd.data->data = bp->bio_data;
1880a94a63f0SWarner Losh 		mmcio->cmd.data->len = 512 * count;
1881a94a63f0SWarner Losh 		mmcio->cmd.data->flags = (bp->bio_cmd == BIO_READ ? MMC_DATA_READ : MMC_DATA_WRITE);
1882a94a63f0SWarner Losh 		/* Direct h/w to issue CMD12 upon completion */
1883a94a63f0SWarner Losh 		if (count > 1) {
18843f1cfdb1SIlya Bakulin 			mmcio->cmd.data->flags |= MMC_DATA_MULTI;
1885a94a63f0SWarner Losh 			mmcio->stop.opcode = MMC_STOP_TRANSMISSION;
1886a94a63f0SWarner Losh 			mmcio->stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1887a94a63f0SWarner Losh 			mmcio->stop.arg = 0;
1888a94a63f0SWarner Losh 		}
1889a94a63f0SWarner Losh 
1890a94a63f0SWarner Losh 		break;
1891a94a63f0SWarner Losh 	}
1892a94a63f0SWarner Losh 	case BIO_FLUSH:
1893a94a63f0SWarner Losh 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_FLUSH\n"));
1894a94a63f0SWarner Losh 		sddaschedule(periph);
1895a94a63f0SWarner Losh 		break;
1896a94a63f0SWarner Losh 	case BIO_DELETE:
1897a94a63f0SWarner Losh 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_DELETE\n"));
1898a94a63f0SWarner Losh 		sddaschedule(periph);
1899a94a63f0SWarner Losh 		break;
1900d176b803SScott Long 	default:
1901d176b803SScott Long 		biofinish(bp, NULL, EOPNOTSUPP);
1902d176b803SScott Long 		xpt_release_ccb(start_ccb);
1903d176b803SScott Long 		return;
1904a94a63f0SWarner Losh 	}
1905a94a63f0SWarner Losh 	start_ccb->ccb_h.ccb_bp = bp;
1906a94a63f0SWarner Losh 	softc->outstanding_cmds++;
1907a94a63f0SWarner Losh 	softc->refcount++;
1908a94a63f0SWarner Losh 	cam_periph_unlock(periph);
1909a94a63f0SWarner Losh 	xpt_action(start_ccb);
1910a94a63f0SWarner Losh 	cam_periph_lock(periph);
1911a94a63f0SWarner Losh 
1912a94a63f0SWarner Losh 	/* May have more work to do, so ensure we stay scheduled */
1913a94a63f0SWarner Losh 	sddaschedule(periph);
1914a94a63f0SWarner Losh }
1915a94a63f0SWarner Losh 
1916a94a63f0SWarner Losh static void
1917a94a63f0SWarner Losh sddadone(struct cam_periph *periph, union ccb *done_ccb)
1918a94a63f0SWarner Losh {
191996e47614SIlya Bakulin 	struct bio *bp;
1920a94a63f0SWarner Losh 	struct sdda_softc *softc;
1921a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
1922a94a63f0SWarner Losh 	struct cam_path *path;
192396e47614SIlya Bakulin 	uint32_t card_status;
192496e47614SIlya Bakulin 	int error = 0;
1925a94a63f0SWarner Losh 
1926a94a63f0SWarner Losh 	softc = (struct sdda_softc *)periph->softc;
1927a94a63f0SWarner Losh 	mmcio = &done_ccb->mmcio;
1928a94a63f0SWarner Losh 	path = done_ccb->ccb_h.path;
1929a94a63f0SWarner Losh 
1930a94a63f0SWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddadone\n"));
1931a94a63f0SWarner Losh 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1932a94a63f0SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Error!!!\n"));
1933a94a63f0SWarner Losh 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1934a94a63f0SWarner Losh 			cam_release_devq(path,
1935a94a63f0SWarner Losh 			    /*relsim_flags*/0,
1936a94a63f0SWarner Losh 			    /*reduction*/0,
1937a94a63f0SWarner Losh 			    /*timeout*/0,
1938a94a63f0SWarner Losh 			    /*getcount_only*/0);
1939a94a63f0SWarner Losh 		error = 5; /* EIO */
1940a94a63f0SWarner Losh 	} else {
1941a94a63f0SWarner Losh 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1942a94a63f0SWarner Losh 			panic("REQ_CMP with QFRZN");
1943a94a63f0SWarner Losh 		error = 0;
1944a94a63f0SWarner Losh 	}
1945a94a63f0SWarner Losh 
194696e47614SIlya Bakulin 	card_status = mmcio->cmd.resp[0];
194796e47614SIlya Bakulin 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
194896e47614SIlya Bakulin 	    ("Card status: %08x\n", R1_STATUS(card_status)));
194996e47614SIlya Bakulin 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
195096e47614SIlya Bakulin 	    ("Current state: %d\n", R1_CURRENT_STATE(card_status)));
195196e47614SIlya Bakulin 
195296e47614SIlya Bakulin 	/* Process result of switching MMC partitions */
195396e47614SIlya Bakulin 	if (softc->state == SDDA_STATE_PART_SWITCH) {
195496e47614SIlya Bakulin 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
1955fd38fa39SAndriy Gapon 		    ("Completing partition switch to %d\n",
1956fd38fa39SAndriy Gapon 		    softc->part_requested));
195796e47614SIlya Bakulin 		softc->outstanding_cmds--;
195896e47614SIlya Bakulin 		/* Complete partition switch */
195996e47614SIlya Bakulin 		softc->state = SDDA_STATE_NORMAL;
196096e47614SIlya Bakulin 		if (error != MMC_ERR_NONE) {
196196e47614SIlya Bakulin 			/* TODO: Unpause retune if accessing RPMB */
196296e47614SIlya Bakulin 			xpt_release_ccb(done_ccb);
196396e47614SIlya Bakulin 			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
196496e47614SIlya Bakulin 			return;
196596e47614SIlya Bakulin 		}
196696e47614SIlya Bakulin 
196796e47614SIlya Bakulin 		softc->raw_ext_csd[EXT_CSD_PART_CONFIG] =
196896e47614SIlya Bakulin 		    (softc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
196996e47614SIlya Bakulin 			~EXT_CSD_PART_CONFIG_ACC_MASK) | softc->part_requested;
197096e47614SIlya Bakulin 		/* TODO: Unpause retune if accessing RPMB */
197196e47614SIlya Bakulin 		softc->part_curr = softc->part_requested;
197296e47614SIlya Bakulin 		xpt_release_ccb(done_ccb);
197396e47614SIlya Bakulin 
197496e47614SIlya Bakulin 		/* Return to processing BIO requests */
197596e47614SIlya Bakulin 		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
197696e47614SIlya Bakulin 		return;
197796e47614SIlya Bakulin 	}
1978a94a63f0SWarner Losh 
1979a94a63f0SWarner Losh 	bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1980a94a63f0SWarner Losh 	bp->bio_error = error;
1981a94a63f0SWarner Losh 	if (error != 0) {
1982a94a63f0SWarner Losh 		bp->bio_resid = bp->bio_bcount;
1983a94a63f0SWarner Losh 		bp->bio_flags |= BIO_ERROR;
1984a94a63f0SWarner Losh 	} else {
1985a94a63f0SWarner Losh 		/* XXX: How many bytes remaining? */
1986a94a63f0SWarner Losh 		bp->bio_resid = 0;
1987a94a63f0SWarner Losh 		if (bp->bio_resid > 0)
1988a94a63f0SWarner Losh 			bp->bio_flags |= BIO_ERROR;
1989a94a63f0SWarner Losh 	}
1990a94a63f0SWarner Losh 
1991a94a63f0SWarner Losh 	softc->outstanding_cmds--;
1992a94a63f0SWarner Losh 	xpt_release_ccb(done_ccb);
1993d9a7a61bSWarner Losh 	/*
199496e47614SIlya Bakulin 	 * Release the periph refcount taken in sddastart() for each CCB.
1995d9a7a61bSWarner Losh 	 */
199696e47614SIlya Bakulin 	KASSERT(softc->refcount >= 1, ("sddadone softc %p refcount %d", softc, softc->refcount));
1997d9a7a61bSWarner Losh 	softc->refcount--;
1998a94a63f0SWarner Losh 	biodone(bp);
1999a94a63f0SWarner Losh }
2000a94a63f0SWarner Losh 
2001a94a63f0SWarner Losh static int
2002a94a63f0SWarner Losh sddaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2003a94a63f0SWarner Losh {
2004553484aeSWarner Losh 	return(cam_periph_error(ccb, cam_flags, sense_flags));
2005a94a63f0SWarner Losh }
2006a94a63f0SWarner Losh #endif /* _KERNEL */
2007