1a94a63f0SWarner Losh /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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 "opt_sdda.h"
36a94a63f0SWarner Losh
37a94a63f0SWarner Losh #include <sys/param.h>
38a94a63f0SWarner Losh
39a94a63f0SWarner Losh #ifdef _KERNEL
40a94a63f0SWarner Losh #include <sys/systm.h>
41a94a63f0SWarner Losh #include <sys/kernel.h>
42a94a63f0SWarner Losh #include <sys/bio.h>
43bf286853SEmmanuel Vadot #include <sys/sysctl.h>
44a94a63f0SWarner Losh #include <sys/endian.h>
45a94a63f0SWarner Losh #include <sys/taskqueue.h>
46a94a63f0SWarner Losh #include <sys/lock.h>
47a94a63f0SWarner Losh #include <sys/mutex.h>
48a94a63f0SWarner Losh #include <sys/conf.h>
49a94a63f0SWarner Losh #include <sys/devicestat.h>
50a94a63f0SWarner Losh #include <sys/eventhandler.h>
51a94a63f0SWarner Losh #include <sys/malloc.h>
52a94a63f0SWarner Losh #include <sys/cons.h>
53a94a63f0SWarner Losh #include <sys/proc.h>
54a94a63f0SWarner Losh #include <sys/reboot.h>
55a94a63f0SWarner Losh #include <geom/geom_disk.h>
56a94a63f0SWarner Losh #include <machine/_inttypes.h> /* for PRIu64 */
57a94a63f0SWarner Losh #endif /* _KERNEL */
58a94a63f0SWarner Losh
59a94a63f0SWarner Losh #ifndef _KERNEL
60a94a63f0SWarner Losh #include <stdio.h>
61a94a63f0SWarner Losh #include <string.h>
62a94a63f0SWarner Losh #endif /* _KERNEL */
63a94a63f0SWarner Losh
64a94a63f0SWarner Losh #include <cam/cam.h>
65a94a63f0SWarner Losh #include <cam/cam_ccb.h>
66a94a63f0SWarner Losh #include <cam/cam_queue.h>
67a94a63f0SWarner Losh #include <cam/cam_periph.h>
68a94a63f0SWarner Losh #include <cam/cam_sim.h>
69a94a63f0SWarner Losh #include <cam/cam_xpt.h>
70a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
71a94a63f0SWarner Losh #include <cam/cam_xpt_periph.h>
72a94a63f0SWarner Losh #include <cam/cam_xpt_internal.h>
73a94a63f0SWarner Losh #include <cam/cam_debug.h>
74a94a63f0SWarner Losh
75a94a63f0SWarner Losh #include <cam/mmc/mmc_all.h>
76a94a63f0SWarner Losh
77a94a63f0SWarner Losh #ifdef _KERNEL
78a94a63f0SWarner Losh
79a94a63f0SWarner Losh typedef enum {
80a94a63f0SWarner Losh SDDA_FLAG_OPEN = 0x0002,
81a94a63f0SWarner Losh SDDA_FLAG_DIRTY = 0x0004
82a94a63f0SWarner Losh } sdda_flags;
83a94a63f0SWarner Losh
84a94a63f0SWarner Losh typedef enum {
85a94a63f0SWarner Losh SDDA_STATE_INIT,
86a94a63f0SWarner Losh SDDA_STATE_INVALID,
8796e47614SIlya Bakulin SDDA_STATE_NORMAL,
8896e47614SIlya Bakulin SDDA_STATE_PART_SWITCH,
89a94a63f0SWarner Losh } sdda_state;
90a94a63f0SWarner Losh
91*a84d91d8SBjoern A. Zeeb /* Purposefully ignore a '%d' argument to snprintf in SDDA_FMT! */
92*a84d91d8SBjoern A. Zeeb #define SDDA_FMT "%s"
93*a84d91d8SBjoern A. Zeeb #define SDDA_FMT_BOOT "%s%dboot"
94*a84d91d8SBjoern A. Zeeb #define SDDA_FMT_GP "%s%dgp"
95*a84d91d8SBjoern A. Zeeb #define SDDA_FMT_RPMB "%s%drpmb"
9696e47614SIlya Bakulin #define SDDA_LABEL_ENH "enh"
9796e47614SIlya Bakulin
9896e47614SIlya Bakulin #define SDDA_PART_NAMELEN (16 + 1)
9996e47614SIlya Bakulin
10096e47614SIlya Bakulin struct sdda_softc;
10196e47614SIlya Bakulin
10296e47614SIlya Bakulin struct sdda_part {
10396e47614SIlya Bakulin struct disk *disk;
104a94a63f0SWarner Losh struct bio_queue_head bio_queue;
10596e47614SIlya Bakulin sdda_flags flags;
10696e47614SIlya Bakulin struct sdda_softc *sc;
10796e47614SIlya Bakulin u_int cnt;
10896e47614SIlya Bakulin u_int type;
10996e47614SIlya Bakulin bool ro;
11096e47614SIlya Bakulin char name[SDDA_PART_NAMELEN];
11196e47614SIlya Bakulin };
11296e47614SIlya Bakulin
11396e47614SIlya Bakulin struct sdda_softc {
114a94a63f0SWarner Losh int outstanding_cmds; /* Number of active commands */
115a94a63f0SWarner Losh int refcount; /* Active xpt_action() calls */
116a94a63f0SWarner Losh sdda_state state;
117a94a63f0SWarner Losh struct mmc_data *mmcdata;
11896e47614SIlya Bakulin struct cam_periph *periph;
119a94a63f0SWarner Losh // sdda_quirks quirks;
120a94a63f0SWarner Losh struct task start_init_task;
121a94a63f0SWarner Losh uint32_t raw_csd[4];
122a94a63f0SWarner Losh uint8_t raw_ext_csd[512]; /* MMC only? */
123a94a63f0SWarner Losh struct mmc_csd csd;
124a94a63f0SWarner Losh struct mmc_cid cid;
125a94a63f0SWarner Losh struct mmc_scr scr;
126a94a63f0SWarner Losh /* Calculated from CSD */
127a94a63f0SWarner Losh uint64_t sector_count;
128a94a63f0SWarner Losh uint64_t mediasize;
129a94a63f0SWarner Losh
130a94a63f0SWarner Losh /* Calculated from CID */
131a94a63f0SWarner Losh char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
132a94a63f0SWarner Losh char card_sn_string[16];/* Formatted serial # for disk->d_ident */
133a94a63f0SWarner Losh /* Determined from CSD + is highspeed card*/
134a94a63f0SWarner Losh uint32_t card_f_max;
13596e47614SIlya Bakulin
13696e47614SIlya Bakulin /* Generic switch timeout */
13796e47614SIlya Bakulin uint32_t cmd6_time;
138fd7371f7SEmmanuel Vadot uint32_t timings; /* Mask of bus timings supported */
139fd7371f7SEmmanuel Vadot uint32_t vccq_120; /* Mask of bus timings at VCCQ of 1.2 V */
140fd7371f7SEmmanuel Vadot uint32_t vccq_180; /* Mask of bus timings at VCCQ of 1.8 V */
14196e47614SIlya Bakulin /* MMC partitions support */
14296e47614SIlya Bakulin struct sdda_part *part[MMC_PART_MAX];
14396e47614SIlya Bakulin uint8_t part_curr; /* Partition currently switched to */
14496e47614SIlya Bakulin uint8_t part_requested; /* What partition we're currently switching to */
14596e47614SIlya Bakulin uint32_t part_time; /* Partition switch timeout [us] */
14696e47614SIlya Bakulin off_t enh_base; /* Enhanced user data area slice base ... */
14796e47614SIlya Bakulin off_t enh_size; /* ... and size [bytes] */
14896e47614SIlya Bakulin int log_count;
14996e47614SIlya Bakulin struct timeval log_time;
150a94a63f0SWarner Losh };
151a94a63f0SWarner Losh
1521a22fb3fSIlya Bakulin static const char *mmc_errmsg[] =
1531a22fb3fSIlya Bakulin {
1541a22fb3fSIlya Bakulin "None",
1551a22fb3fSIlya Bakulin "Timeout",
1561a22fb3fSIlya Bakulin "Bad CRC",
1571a22fb3fSIlya Bakulin "Fifo",
1581a22fb3fSIlya Bakulin "Failed",
1591a22fb3fSIlya Bakulin "Invalid",
1601a22fb3fSIlya Bakulin "NO MEMORY"
1611a22fb3fSIlya Bakulin };
1621a22fb3fSIlya Bakulin
163a94a63f0SWarner Losh #define ccb_bp ppriv_ptr1
164a94a63f0SWarner Losh
165a94a63f0SWarner Losh static disk_strategy_t sddastrategy;
16644682688SAndriy Gapon static dumper_t sddadump;
167a94a63f0SWarner Losh static periph_init_t sddainit;
16873551d4fSWarner Losh static void sddaasync(void *callback_arg, uint32_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);
17673551d4fSWarner Losh static int sddaerror(union ccb *ccb, uint32_t cam_flags,
17773551d4fSWarner Losh uint32_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);
mmc_get_sector_size(struct cam_periph * periph)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 */
sdda_get_read_only(struct cam_periph * periph,union ccb * start_ccb)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
get_rca(struct cam_periph * periph)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
mmc_handle_reply(union ccb * ccb)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
mmc_get_bits(uint32_t * bits,int bit_len,int start,int size)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
mmc_decode_csd_sd(uint32_t * raw_csd,struct mmc_csd * csd)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);
2876506efeaSEmmanuel Vadot
2886506efeaSEmmanuel 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);
3026506efeaSEmmanuel Vadot csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
3036506efeaSEmmanuel Vadot csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
3046506efeaSEmmanuel Vadot csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
3056506efeaSEmmanuel Vadot csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
3066506efeaSEmmanuel Vadot csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
3076506efeaSEmmanuel Vadot csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
3086506efeaSEmmanuel Vadot csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
3096506efeaSEmmanuel Vadot
3106506efeaSEmmanuel 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
mmc_decode_csd_mmc(uint32_t * raw_csd,struct mmc_csd * csd)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
mmc_decode_cid_sd(uint32_t * raw_cid,struct mmc_cid * cid)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
mmc_decode_cid_mmc(uint32_t * raw_cid,struct mmc_cid * cid)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
mmc_format_card_id_string(struct sdda_softc * sc,struct mmc_params * mmcp)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
sddaopen(struct disk * dp)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
sddaclose(struct disk * dp)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
sddaschedule(struct cam_periph * periph)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
sddastrategy(struct bio * bp)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
sddainit(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
sddadiskgonecb(struct disk * dp)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
sddaoninvalidate(struct cam_periph * periph)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
sddacleanup(struct cam_periph * periph)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
sddaasync(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)64873551d4fSWarner Losh sddaasync(void *callback_arg, uint32_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
654a94a63f0SWarner Losh periph = (struct cam_periph *)callback_arg;
655a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddaasync(code=%d)\n", code));
656a94a63f0SWarner Losh switch (code) {
657a94a63f0SWarner Losh case AC_FOUND_DEVICE:
658a94a63f0SWarner Losh {
659a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_FOUND_DEVICE\n"));
660a94a63f0SWarner Losh struct ccb_getdev *cgd;
661a94a63f0SWarner Losh cam_status status;
662a94a63f0SWarner Losh
663a94a63f0SWarner Losh cgd = (struct ccb_getdev *)arg;
664a94a63f0SWarner Losh if (cgd == NULL)
665a94a63f0SWarner Losh break;
666a94a63f0SWarner Losh
667a94a63f0SWarner Losh if (cgd->protocol != PROTO_MMCSD)
668a94a63f0SWarner Losh break;
669a94a63f0SWarner Losh
670a94a63f0SWarner Losh if (!(path->device->mmc_ident_data.card_features & CARD_FEATURE_MEMORY)) {
671a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("No memory on the card!\n"));
672a94a63f0SWarner Losh break;
673a94a63f0SWarner Losh }
674a94a63f0SWarner Losh
675a94a63f0SWarner Losh /*
676a94a63f0SWarner Losh * Allocate a peripheral instance for
677a94a63f0SWarner Losh * this device and start the probe
678a94a63f0SWarner Losh * process.
679a94a63f0SWarner Losh */
680a94a63f0SWarner Losh status = cam_periph_alloc(sddaregister, sddaoninvalidate,
681a94a63f0SWarner Losh sddacleanup, sddastart,
682a94a63f0SWarner Losh "sdda", CAM_PERIPH_BIO,
683a94a63f0SWarner Losh path, sddaasync,
684a94a63f0SWarner Losh AC_FOUND_DEVICE, cgd);
685a94a63f0SWarner Losh
686a94a63f0SWarner Losh if (status != CAM_REQ_CMP
687a94a63f0SWarner Losh && status != CAM_REQ_INPROG)
688a94a63f0SWarner Losh printf("sddaasync: Unable to attach to new device "
689a94a63f0SWarner Losh "due to status 0x%x\n", status);
690a94a63f0SWarner Losh break;
691a94a63f0SWarner Losh }
692a94a63f0SWarner Losh case AC_GETDEV_CHANGED:
693a94a63f0SWarner Losh {
694a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_GETDEV_CHANGED\n"));
695ec5325dbSEdward Tomasz Napierala memset(&cgd, 0, sizeof(cgd));
696a94a63f0SWarner Losh xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
697a94a63f0SWarner Losh cgd.ccb_h.func_code = XPT_GDEV_TYPE;
698a94a63f0SWarner Losh xpt_action((union ccb *)&cgd);
699a94a63f0SWarner Losh cam_periph_async(periph, code, path, arg);
700a94a63f0SWarner Losh break;
701a94a63f0SWarner Losh }
702a94a63f0SWarner Losh case AC_ADVINFO_CHANGED:
703a94a63f0SWarner Losh {
704a94a63f0SWarner Losh uintptr_t buftype;
70596e47614SIlya Bakulin int i;
70696e47614SIlya Bakulin
707a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_ADVINFO_CHANGED\n"));
708a94a63f0SWarner Losh buftype = (uintptr_t)arg;
709a94a63f0SWarner Losh if (buftype == CDAI_TYPE_PHYS_PATH) {
710a94a63f0SWarner Losh struct sdda_softc *softc;
71196e47614SIlya Bakulin struct sdda_part *part;
712a94a63f0SWarner Losh
713a94a63f0SWarner Losh softc = periph->softc;
71496e47614SIlya Bakulin for (i = 0; i < MMC_PART_MAX; i++) {
71596e47614SIlya Bakulin if ((part = softc->part[i]) != NULL) {
71696e47614SIlya Bakulin disk_attr_changed(part->disk, "GEOM::physpath",
717a94a63f0SWarner Losh M_NOWAIT);
718a94a63f0SWarner Losh }
719a94a63f0SWarner Losh }
72096e47614SIlya Bakulin }
72196e47614SIlya Bakulin break;
722a94a63f0SWarner Losh }
723a94a63f0SWarner Losh default:
724a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> default?!\n"));
725a94a63f0SWarner Losh cam_periph_async(periph, code, path, arg);
726a94a63f0SWarner Losh break;
727a94a63f0SWarner Losh }
728a94a63f0SWarner Losh }
729a94a63f0SWarner Losh
730a94a63f0SWarner Losh static int
sddagetattr(struct bio * bp)731a94a63f0SWarner Losh sddagetattr(struct bio *bp)
732a94a63f0SWarner Losh {
733a94a63f0SWarner Losh struct cam_periph *periph;
73496e47614SIlya Bakulin struct sdda_softc *softc;
73596e47614SIlya Bakulin struct sdda_part *part;
73696e47614SIlya Bakulin int ret;
737a94a63f0SWarner Losh
73896e47614SIlya Bakulin part = (struct sdda_part *)bp->bio_disk->d_drv1;
73996e47614SIlya Bakulin softc = part->sc;
74096e47614SIlya Bakulin periph = softc->periph;
741a94a63f0SWarner Losh cam_periph_lock(periph);
742a94a63f0SWarner Losh ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
743a94a63f0SWarner Losh periph->path);
744a94a63f0SWarner Losh cam_periph_unlock(periph);
745a94a63f0SWarner Losh if (ret == 0)
746a94a63f0SWarner Losh bp->bio_completed = bp->bio_length;
74796e47614SIlya Bakulin return (ret);
748a94a63f0SWarner Losh }
749a94a63f0SWarner Losh
750a94a63f0SWarner Losh static cam_status
sddaregister(struct cam_periph * periph,void * arg)751a94a63f0SWarner Losh sddaregister(struct cam_periph *periph, void *arg)
752a94a63f0SWarner Losh {
753a94a63f0SWarner Losh struct sdda_softc *softc;
754a94a63f0SWarner Losh struct ccb_getdev *cgd;
755a94a63f0SWarner Losh
756a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaregister\n"));
757a94a63f0SWarner Losh cgd = (struct ccb_getdev *)arg;
758a94a63f0SWarner Losh if (cgd == NULL) {
759a94a63f0SWarner Losh printf("sddaregister: no getdev CCB, can't register device\n");
760a94a63f0SWarner Losh return (CAM_REQ_CMP_ERR);
761a94a63f0SWarner Losh }
762a94a63f0SWarner Losh
763a94a63f0SWarner Losh softc = (struct sdda_softc *)malloc(sizeof(*softc), M_DEVBUF,
764a94a63f0SWarner Losh M_NOWAIT|M_ZERO);
765a94a63f0SWarner Losh if (softc == NULL) {
766a94a63f0SWarner Losh printf("sddaregister: Unable to probe new device. "
767a94a63f0SWarner Losh "Unable to allocate softc\n");
768a94a63f0SWarner Losh return (CAM_REQ_CMP_ERR);
769a94a63f0SWarner Losh }
770a94a63f0SWarner Losh
771a94a63f0SWarner Losh softc->state = SDDA_STATE_INIT;
772a94a63f0SWarner Losh softc->mmcdata =
773a94a63f0SWarner Losh (struct mmc_data *)malloc(sizeof(struct mmc_data), M_DEVBUF, M_NOWAIT|M_ZERO);
7740660cfa0SIlya Bakulin if (softc->mmcdata == NULL) {
7750660cfa0SIlya Bakulin printf("sddaregister: Unable to probe new device. "
7760660cfa0SIlya Bakulin "Unable to allocate mmcdata\n");
7771e5d7335SBjoern A. Zeeb free(softc, M_DEVBUF);
7780660cfa0SIlya Bakulin return (CAM_REQ_CMP_ERR);
7790660cfa0SIlya Bakulin }
780a94a63f0SWarner Losh periph->softc = softc;
78196e47614SIlya Bakulin softc->periph = periph;
782a94a63f0SWarner Losh
783a94a63f0SWarner Losh xpt_schedule(periph, CAM_PRIORITY_XPT);
784a94a63f0SWarner Losh TASK_INIT(&softc->start_init_task, 0, sdda_start_init_task, periph);
785a94a63f0SWarner Losh taskqueue_enqueue(taskqueue_thread, &softc->start_init_task);
786a94a63f0SWarner Losh
787a94a63f0SWarner Losh return (CAM_REQ_CMP);
788a94a63f0SWarner Losh }
789a94a63f0SWarner Losh
790a94a63f0SWarner Losh static int
mmc_exec_app_cmd(struct cam_periph * periph,union ccb * ccb,struct mmc_command * cmd)791a94a63f0SWarner Losh mmc_exec_app_cmd(struct cam_periph *periph, union ccb *ccb,
792a94a63f0SWarner Losh struct mmc_command *cmd) {
793a94a63f0SWarner Losh int err;
794a94a63f0SWarner Losh
795a94a63f0SWarner Losh /* Send APP_CMD first */
796a94a63f0SWarner Losh memset(&ccb->mmcio.cmd, 0, sizeof(struct mmc_command));
797a94a63f0SWarner Losh memset(&ccb->mmcio.stop, 0, sizeof(struct mmc_command));
798a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio,
799a94a63f0SWarner Losh /*retries*/ 0,
800a94a63f0SWarner Losh /*cbfcnp*/ NULL,
801a94a63f0SWarner Losh /*flags*/ CAM_DIR_NONE,
802a94a63f0SWarner Losh /*mmc_opcode*/ MMC_APP_CMD,
803a94a63f0SWarner Losh /*mmc_arg*/ get_rca(periph) << 16,
804a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_AC,
805a94a63f0SWarner Losh /*mmc_data*/ NULL,
806a94a63f0SWarner Losh /*timeout*/ 0);
807a94a63f0SWarner Losh
8081a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8091a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
810a94a63f0SWarner Losh if (err != 0)
8111a22fb3fSIlya Bakulin return (err);
812a94a63f0SWarner Losh if (!(ccb->mmcio.cmd.resp[0] & R1_APP_CMD))
8131a22fb3fSIlya Bakulin return (EIO);
814a94a63f0SWarner Losh
815a94a63f0SWarner Losh /* Now exec actual command */
816a94a63f0SWarner Losh int flags = 0;
817a94a63f0SWarner Losh if (cmd->data != NULL) {
818a94a63f0SWarner Losh ccb->mmcio.cmd.data = cmd->data;
819a94a63f0SWarner Losh if (cmd->data->flags & MMC_DATA_READ)
820a94a63f0SWarner Losh flags |= CAM_DIR_IN;
821a94a63f0SWarner Losh if (cmd->data->flags & MMC_DATA_WRITE)
822a94a63f0SWarner Losh flags |= CAM_DIR_OUT;
823a94a63f0SWarner Losh } else flags = CAM_DIR_NONE;
824a94a63f0SWarner Losh
825a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio,
826a94a63f0SWarner Losh /*retries*/ 0,
827a94a63f0SWarner Losh /*cbfcnp*/ NULL,
828a94a63f0SWarner Losh /*flags*/ flags,
829a94a63f0SWarner Losh /*mmc_opcode*/ cmd->opcode,
830a94a63f0SWarner Losh /*mmc_arg*/ cmd->arg,
831a94a63f0SWarner Losh /*mmc_flags*/ cmd->flags,
832a94a63f0SWarner Losh /*mmc_data*/ cmd->data,
833a94a63f0SWarner Losh /*timeout*/ 0);
834a94a63f0SWarner Losh
8351a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8361a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
8371a22fb3fSIlya Bakulin if (err != 0)
8381a22fb3fSIlya Bakulin return (err);
839a94a63f0SWarner Losh memcpy(cmd->resp, ccb->mmcio.cmd.resp, sizeof(cmd->resp));
840a94a63f0SWarner Losh cmd->error = ccb->mmcio.cmd.error;
8411a22fb3fSIlya Bakulin
8421a22fb3fSIlya Bakulin return (0);
843a94a63f0SWarner Losh }
844a94a63f0SWarner Losh
845a94a63f0SWarner Losh static int
mmc_app_get_scr(struct cam_periph * periph,union ccb * ccb,uint32_t * rawscr)846a94a63f0SWarner Losh mmc_app_get_scr(struct cam_periph *periph, union ccb *ccb, uint32_t *rawscr) {
847a94a63f0SWarner Losh int err;
848a94a63f0SWarner Losh struct mmc_command cmd;
849a94a63f0SWarner Losh struct mmc_data d;
850a94a63f0SWarner Losh
851a94a63f0SWarner Losh memset(&cmd, 0, sizeof(cmd));
8524c4200c6SIlya Bakulin memset(&d, 0, sizeof(d));
853a94a63f0SWarner Losh
854a94a63f0SWarner Losh memset(rawscr, 0, 8);
855a94a63f0SWarner Losh cmd.opcode = ACMD_SEND_SCR;
856a94a63f0SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
857a94a63f0SWarner Losh cmd.arg = 0;
858a94a63f0SWarner Losh
859a94a63f0SWarner Losh d.data = rawscr;
860a94a63f0SWarner Losh d.len = 8;
861a94a63f0SWarner Losh d.flags = MMC_DATA_READ;
862a94a63f0SWarner Losh cmd.data = &d;
863a94a63f0SWarner Losh
864a94a63f0SWarner Losh err = mmc_exec_app_cmd(periph, ccb, &cmd);
865a94a63f0SWarner Losh rawscr[0] = be32toh(rawscr[0]);
866a94a63f0SWarner Losh rawscr[1] = be32toh(rawscr[1]);
867a94a63f0SWarner Losh return (err);
868a94a63f0SWarner Losh }
869a94a63f0SWarner Losh
870a94a63f0SWarner Losh static int
mmc_send_ext_csd(struct cam_periph * periph,union ccb * ccb,uint8_t * rawextcsd,size_t buf_len)871a94a63f0SWarner Losh mmc_send_ext_csd(struct cam_periph *periph, union ccb *ccb,
872a94a63f0SWarner Losh uint8_t *rawextcsd, size_t buf_len) {
873a94a63f0SWarner Losh int err;
874a94a63f0SWarner Losh struct mmc_data d;
875a94a63f0SWarner Losh
876a94a63f0SWarner Losh KASSERT(buf_len == 512, ("Buffer for ext csd must be 512 bytes"));
8770660cfa0SIlya Bakulin memset(&d, 0, sizeof(d));
878a94a63f0SWarner Losh d.data = rawextcsd;
879a94a63f0SWarner Losh d.len = buf_len;
880a94a63f0SWarner Losh d.flags = MMC_DATA_READ;
881a94a63f0SWarner Losh memset(d.data, 0, d.len);
882a94a63f0SWarner Losh
883a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio,
884a94a63f0SWarner Losh /*retries*/ 0,
885a94a63f0SWarner Losh /*cbfcnp*/ NULL,
886a94a63f0SWarner Losh /*flags*/ CAM_DIR_IN,
887a94a63f0SWarner Losh /*mmc_opcode*/ MMC_SEND_EXT_CSD,
888a94a63f0SWarner Losh /*mmc_arg*/ 0,
889a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
890a94a63f0SWarner Losh /*mmc_data*/ &d,
891a94a63f0SWarner Losh /*timeout*/ 0);
892a94a63f0SWarner Losh
8931a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
8941a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
89596e47614SIlya Bakulin return (err);
896a94a63f0SWarner Losh }
897a94a63f0SWarner Losh
898a94a63f0SWarner Losh static void
mmc_app_decode_scr(uint32_t * raw_scr,struct mmc_scr * scr)899a94a63f0SWarner Losh mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
900a94a63f0SWarner Losh {
901a94a63f0SWarner Losh unsigned int scr_struct;
902a94a63f0SWarner Losh
903a94a63f0SWarner Losh memset(scr, 0, sizeof(*scr));
904a94a63f0SWarner Losh
905a94a63f0SWarner Losh scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
906a94a63f0SWarner Losh if (scr_struct != 0) {
907a94a63f0SWarner Losh printf("Unrecognised SCR structure version %d\n",
908a94a63f0SWarner Losh scr_struct);
909a94a63f0SWarner Losh return;
910a94a63f0SWarner Losh }
911a94a63f0SWarner Losh scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
912a94a63f0SWarner Losh scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
913a94a63f0SWarner Losh }
914a94a63f0SWarner Losh
91596e47614SIlya Bakulin static inline void
mmc_switch_fill_mmcio(union ccb * ccb,uint8_t set,uint8_t index,uint8_t value,u_int timeout)91696e47614SIlya Bakulin mmc_switch_fill_mmcio(union ccb *ccb,
91796e47614SIlya Bakulin uint8_t set, uint8_t index, uint8_t value, u_int timeout)
918a94a63f0SWarner Losh {
919a94a63f0SWarner Losh int arg = (MMC_SWITCH_FUNC_WR << 24) |
920a94a63f0SWarner Losh (index << 16) |
921a94a63f0SWarner Losh (value << 8) |
922a94a63f0SWarner Losh set;
92396e47614SIlya Bakulin
924a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio,
925a94a63f0SWarner Losh /*retries*/ 0,
926a94a63f0SWarner Losh /*cbfcnp*/ NULL,
927a94a63f0SWarner Losh /*flags*/ CAM_DIR_NONE,
928a94a63f0SWarner Losh /*mmc_opcode*/ MMC_SWITCH_FUNC,
929a94a63f0SWarner Losh /*mmc_arg*/ arg,
930a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1B | MMC_CMD_AC,
931a94a63f0SWarner Losh /*mmc_data*/ NULL,
93296e47614SIlya Bakulin /*timeout*/ timeout);
93396e47614SIlya Bakulin }
934a94a63f0SWarner Losh
93596e47614SIlya Bakulin static int
mmc_select_card(struct cam_periph * periph,union ccb * ccb,uint32_t rca)936d670d951SIlya Bakulin mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca)
937d670d951SIlya Bakulin {
9381a22fb3fSIlya Bakulin int flags, err;
939d670d951SIlya Bakulin
940d670d951SIlya Bakulin flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
941d670d951SIlya Bakulin cam_fill_mmcio(&ccb->mmcio,
942d670d951SIlya Bakulin /*retries*/ 0,
943d670d951SIlya Bakulin /*cbfcnp*/ NULL,
944d670d951SIlya Bakulin /*flags*/ CAM_DIR_IN,
945d670d951SIlya Bakulin /*mmc_opcode*/ MMC_SELECT_CARD,
946d670d951SIlya Bakulin /*mmc_arg*/ rca << 16,
947d670d951SIlya Bakulin /*mmc_flags*/ flags,
948d670d951SIlya Bakulin /*mmc_data*/ NULL,
949d670d951SIlya Bakulin /*timeout*/ 0);
950d670d951SIlya Bakulin
951d670d951SIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
9521a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
9531a22fb3fSIlya Bakulin return (err);
954d670d951SIlya Bakulin }
955d670d951SIlya Bakulin
956d670d951SIlya Bakulin static int
mmc_switch(struct cam_periph * periph,union ccb * ccb,uint8_t set,uint8_t index,uint8_t value,u_int timeout)95796e47614SIlya Bakulin mmc_switch(struct cam_periph *periph, union ccb *ccb,
95896e47614SIlya Bakulin uint8_t set, uint8_t index, uint8_t value, u_int timeout)
95996e47614SIlya Bakulin {
9601a22fb3fSIlya Bakulin int err;
96196e47614SIlya Bakulin
96296e47614SIlya Bakulin mmc_switch_fill_mmcio(ccb, set, index, value, timeout);
963a94a63f0SWarner Losh cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
9641a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
9651a22fb3fSIlya Bakulin return (err);
966a94a63f0SWarner Losh }
967a94a63f0SWarner Losh
96896e47614SIlya Bakulin static uint32_t
mmc_get_spec_vers(struct cam_periph * periph)96996e47614SIlya Bakulin mmc_get_spec_vers(struct cam_periph *periph) {
97096e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
97196e47614SIlya Bakulin
97296e47614SIlya Bakulin return (softc->csd.spec_vers);
97396e47614SIlya Bakulin }
97496e47614SIlya Bakulin
97596e47614SIlya Bakulin static uint64_t
mmc_get_media_size(struct cam_periph * periph)97696e47614SIlya Bakulin mmc_get_media_size(struct cam_periph *periph) {
97796e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
97896e47614SIlya Bakulin
97996e47614SIlya Bakulin return (softc->mediasize);
98096e47614SIlya Bakulin }
98196e47614SIlya Bakulin
98296e47614SIlya Bakulin static uint32_t
mmc_get_cmd6_timeout(struct cam_periph * periph)98396e47614SIlya Bakulin mmc_get_cmd6_timeout(struct cam_periph *periph)
98496e47614SIlya Bakulin {
98596e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
98696e47614SIlya Bakulin
98796e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 6)
98896e47614SIlya Bakulin return (softc->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME] * 10);
98996e47614SIlya Bakulin return (500 * 1000);
99096e47614SIlya Bakulin }
99196e47614SIlya Bakulin
992a94a63f0SWarner Losh static int
mmc_sd_switch(struct cam_periph * periph,union ccb * ccb,uint8_t mode,uint8_t grp,uint8_t value,uint8_t * res)993a94a63f0SWarner Losh mmc_sd_switch(struct cam_periph *periph, union ccb *ccb,
994a94a63f0SWarner Losh uint8_t mode, uint8_t grp, uint8_t value,
995a94a63f0SWarner Losh uint8_t *res) {
996a94a63f0SWarner Losh struct mmc_data mmc_d;
997d670d951SIlya Bakulin uint32_t arg;
9981a22fb3fSIlya Bakulin int err;
999a94a63f0SWarner Losh
1000a94a63f0SWarner Losh memset(res, 0, 64);
10010660cfa0SIlya Bakulin memset(&mmc_d, 0, sizeof(mmc_d));
1002a94a63f0SWarner Losh mmc_d.len = 64;
1003a94a63f0SWarner Losh mmc_d.data = res;
1004a94a63f0SWarner Losh mmc_d.flags = MMC_DATA_READ;
1005a94a63f0SWarner Losh
1006d670d951SIlya Bakulin arg = mode << 31; /* 0 - check, 1 - set */
1007d670d951SIlya Bakulin arg |= 0x00FFFFFF;
1008d670d951SIlya Bakulin arg &= ~(0xF << (grp * 4));
1009d670d951SIlya Bakulin arg |= value << (grp * 4);
1010d670d951SIlya Bakulin
1011a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio,
1012a94a63f0SWarner Losh /*retries*/ 0,
1013a94a63f0SWarner Losh /*cbfcnp*/ NULL,
1014a94a63f0SWarner Losh /*flags*/ CAM_DIR_IN,
1015a94a63f0SWarner Losh /*mmc_opcode*/ SD_SWITCH_FUNC,
1016d670d951SIlya Bakulin /*mmc_arg*/ arg,
1017a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
1018a94a63f0SWarner Losh /*mmc_data*/ &mmc_d,
1019a94a63f0SWarner Losh /*timeout*/ 0);
1020a94a63f0SWarner Losh
1021a94a63f0SWarner Losh cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
10221a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb);
10231a22fb3fSIlya Bakulin return (err);
1024a94a63f0SWarner Losh }
1025a94a63f0SWarner Losh
1026a94a63f0SWarner Losh static int
mmc_set_timing(struct cam_periph * periph,union ccb * ccb,enum mmc_bus_timing timing)1027a94a63f0SWarner Losh mmc_set_timing(struct cam_periph *periph,
1028a94a63f0SWarner Losh union ccb *ccb,
1029a94a63f0SWarner Losh enum mmc_bus_timing timing)
1030a94a63f0SWarner Losh {
1031a94a63f0SWarner Losh u_char switch_res[64];
1032a94a63f0SWarner Losh int err;
1033a94a63f0SWarner Losh uint8_t value;
103496e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1035a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1036a94a63f0SWarner Losh
1037a94a63f0SWarner Losh CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1038a94a63f0SWarner Losh ("mmc_set_timing(timing=%d)", timing));
1039a94a63f0SWarner Losh switch (timing) {
1040a94a63f0SWarner Losh case bus_timing_normal:
1041a94a63f0SWarner Losh value = 0;
1042a94a63f0SWarner Losh break;
1043a94a63f0SWarner Losh case bus_timing_hs:
1044a94a63f0SWarner Losh value = 1;
1045a94a63f0SWarner Losh break;
1046a94a63f0SWarner Losh default:
1047a94a63f0SWarner Losh return (MMC_ERR_INVALID);
1048a94a63f0SWarner Losh }
1049a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) {
1050a94a63f0SWarner Losh err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
105196e47614SIlya Bakulin EXT_CSD_HS_TIMING, value, softc->cmd6_time);
1052a94a63f0SWarner Losh } else {
1053a94a63f0SWarner Losh err = mmc_sd_switch(periph, ccb, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, value, switch_res);
1054a94a63f0SWarner Losh }
1055a94a63f0SWarner Losh
1056a94a63f0SWarner Losh /* Set high-speed timing on the host */
1057a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts;
1058a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc;
1059a94a63f0SWarner Losh ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1060a94a63f0SWarner Losh ccb->ccb_h.flags = CAM_DIR_NONE;
1061a94a63f0SWarner Losh ccb->ccb_h.retry_count = 0;
1062a94a63f0SWarner Losh ccb->ccb_h.timeout = 100;
1063a94a63f0SWarner Losh ccb->ccb_h.cbfcnp = NULL;
1064a94a63f0SWarner Losh cts->ios.timing = timing;
1065a94a63f0SWarner Losh cts->ios_valid = MMC_BT;
1066a94a63f0SWarner Losh xpt_action(ccb);
1067a94a63f0SWarner Losh
1068a94a63f0SWarner Losh return (err);
1069a94a63f0SWarner Losh }
1070a94a63f0SWarner Losh
1071a94a63f0SWarner Losh static void
sdda_start_init_task(void * context,int pending)1072a94a63f0SWarner Losh sdda_start_init_task(void *context, int pending) {
1073a94a63f0SWarner Losh union ccb *new_ccb;
1074a94a63f0SWarner Losh struct cam_periph *periph;
1075a94a63f0SWarner Losh
1076a94a63f0SWarner Losh periph = (struct cam_periph *)context;
1077a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init_task\n"));
1078a94a63f0SWarner Losh new_ccb = xpt_alloc_ccb();
1079a94a63f0SWarner Losh xpt_setup_ccb(&new_ccb->ccb_h, periph->path,
1080a94a63f0SWarner Losh CAM_PRIORITY_NONE);
1081a94a63f0SWarner Losh
1082a94a63f0SWarner Losh cam_periph_lock(periph);
1083f2df51ecSEmmanuel Vadot cam_periph_hold(periph, PRIBIO|PCATCH);
1084a94a63f0SWarner Losh sdda_start_init(context, new_ccb);
1085f2df51ecSEmmanuel Vadot cam_periph_unhold(periph);
1086a94a63f0SWarner Losh cam_periph_unlock(periph);
1087a94a63f0SWarner Losh xpt_free_ccb(new_ccb);
1088a94a63f0SWarner Losh }
1089a94a63f0SWarner Losh
1090a94a63f0SWarner Losh static void
sdda_set_bus_width(struct cam_periph * periph,union ccb * ccb,int width)1091a94a63f0SWarner Losh sdda_set_bus_width(struct cam_periph *periph, union ccb *ccb, int width) {
109296e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1093a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1094a94a63f0SWarner Losh int err;
1095a94a63f0SWarner Losh
1096a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_set_bus_width\n"));
1097a94a63f0SWarner Losh
1098a94a63f0SWarner Losh /* First set for the card, then for the host */
1099a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) {
1100a94a63f0SWarner Losh uint8_t value;
1101a94a63f0SWarner Losh switch (width) {
1102a94a63f0SWarner Losh case bus_width_1:
1103a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_1;
1104a94a63f0SWarner Losh break;
1105a94a63f0SWarner Losh case bus_width_4:
1106a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_4;
1107a94a63f0SWarner Losh break;
1108a94a63f0SWarner Losh case bus_width_8:
1109a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_8;
1110a94a63f0SWarner Losh break;
1111a94a63f0SWarner Losh default:
1112a94a63f0SWarner Losh panic("Invalid bus width %d", width);
1113a94a63f0SWarner Losh }
1114a94a63f0SWarner Losh err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
111596e47614SIlya Bakulin EXT_CSD_BUS_WIDTH, value, softc->cmd6_time);
1116a94a63f0SWarner Losh } else {
1117a94a63f0SWarner Losh /* For SD cards we send ACMD6 with the required bus width in arg */
1118a94a63f0SWarner Losh struct mmc_command cmd;
1119a94a63f0SWarner Losh memset(&cmd, 0, sizeof(struct mmc_command));
1120a94a63f0SWarner Losh cmd.opcode = ACMD_SET_BUS_WIDTH;
1121a94a63f0SWarner Losh cmd.arg = width;
1122a94a63f0SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1123a94a63f0SWarner Losh err = mmc_exec_app_cmd(periph, ccb, &cmd);
1124a94a63f0SWarner Losh }
1125a94a63f0SWarner Losh
1126a94a63f0SWarner Losh if (err != MMC_ERR_NONE) {
1127a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Error %d when setting bus width on the card\n", err));
1128a94a63f0SWarner Losh return;
1129a94a63f0SWarner Losh }
1130a94a63f0SWarner Losh /* Now card is done, set the host to the same width */
1131a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts;
1132a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc;
1133a94a63f0SWarner Losh ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1134a94a63f0SWarner Losh ccb->ccb_h.flags = CAM_DIR_NONE;
1135a94a63f0SWarner Losh ccb->ccb_h.retry_count = 0;
1136a94a63f0SWarner Losh ccb->ccb_h.timeout = 100;
1137a94a63f0SWarner Losh ccb->ccb_h.cbfcnp = NULL;
1138a94a63f0SWarner Losh cts->ios.bus_width = width;
1139a94a63f0SWarner Losh cts->ios_valid = MMC_BW;
1140a94a63f0SWarner Losh xpt_action(ccb);
1141a94a63f0SWarner Losh }
1142a94a63f0SWarner Losh
114396e47614SIlya Bakulin static inline const char
part_type(u_int type)114496e47614SIlya Bakulin *part_type(u_int type)
114596e47614SIlya Bakulin {
114696e47614SIlya Bakulin
114796e47614SIlya Bakulin switch (type) {
114896e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_RPMB:
114996e47614SIlya Bakulin return ("RPMB");
115096e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_DEFAULT:
115196e47614SIlya Bakulin return ("default");
115296e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_BOOT0:
115396e47614SIlya Bakulin return ("boot0");
115496e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_BOOT1:
115596e47614SIlya Bakulin return ("boot1");
115696e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP0:
115796e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP1:
115896e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP2:
115996e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP3:
116096e47614SIlya Bakulin return ("general purpose");
116196e47614SIlya Bakulin default:
116296e47614SIlya Bakulin return ("(unknown type)");
1163a94a63f0SWarner Losh }
1164a94a63f0SWarner Losh }
1165a94a63f0SWarner Losh
116696e47614SIlya Bakulin static inline const char
bus_width_str(enum mmc_bus_width w)116796e47614SIlya Bakulin *bus_width_str(enum mmc_bus_width w)
116896e47614SIlya Bakulin {
116996e47614SIlya Bakulin
117096e47614SIlya Bakulin switch (w) {
117196e47614SIlya Bakulin case bus_width_1:
117296e47614SIlya Bakulin return ("1-bit");
117396e47614SIlya Bakulin case bus_width_4:
117496e47614SIlya Bakulin return ("4-bit");
117596e47614SIlya Bakulin case bus_width_8:
117696e47614SIlya Bakulin return ("8-bit");
1177d2bc7754SJohn Baldwin default:
1178d2bc7754SJohn Baldwin __assert_unreachable();
117996e47614SIlya Bakulin }
118096e47614SIlya Bakulin }
118196e47614SIlya Bakulin
118296e47614SIlya Bakulin static uint32_t
sdda_get_host_caps(struct cam_periph * periph,union ccb * ccb)118396e47614SIlya Bakulin sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb)
118496e47614SIlya Bakulin {
118596e47614SIlya Bakulin struct ccb_trans_settings_mmc *cts;
118696e47614SIlya Bakulin
118796e47614SIlya Bakulin cts = &ccb->cts.proto_specific.mmc;
118896e47614SIlya Bakulin
118996e47614SIlya Bakulin ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
119096e47614SIlya Bakulin ccb->ccb_h.flags = CAM_DIR_NONE;
119196e47614SIlya Bakulin ccb->ccb_h.retry_count = 0;
119296e47614SIlya Bakulin ccb->ccb_h.timeout = 100;
119396e47614SIlya Bakulin ccb->ccb_h.cbfcnp = NULL;
119496e47614SIlya Bakulin xpt_action(ccb);
119596e47614SIlya Bakulin
119696e47614SIlya Bakulin if (ccb->ccb_h.status != CAM_REQ_CMP)
119796e47614SIlya Bakulin panic("Cannot get host caps");
119896e47614SIlya Bakulin return (cts->host_caps);
119996e47614SIlya Bakulin }
120096e47614SIlya Bakulin
12015d20e651SIlya Bakulin static uint32_t
sdda_get_max_data(struct cam_periph * periph,union ccb * ccb)12025d20e651SIlya Bakulin sdda_get_max_data(struct cam_periph *periph, union ccb *ccb)
12035d20e651SIlya Bakulin {
12045d20e651SIlya Bakulin struct ccb_trans_settings_mmc *cts;
12055d20e651SIlya Bakulin
12065d20e651SIlya Bakulin cts = &ccb->cts.proto_specific.mmc;
12075d20e651SIlya Bakulin memset(cts, 0, sizeof(struct ccb_trans_settings_mmc));
12085d20e651SIlya Bakulin
12095d20e651SIlya Bakulin ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
12105d20e651SIlya Bakulin ccb->ccb_h.flags = CAM_DIR_NONE;
12115d20e651SIlya Bakulin ccb->ccb_h.retry_count = 0;
12125d20e651SIlya Bakulin ccb->ccb_h.timeout = 100;
12135d20e651SIlya Bakulin ccb->ccb_h.cbfcnp = NULL;
12145d20e651SIlya Bakulin xpt_action(ccb);
12155d20e651SIlya Bakulin
12165d20e651SIlya Bakulin if (ccb->ccb_h.status != CAM_REQ_CMP)
12175d20e651SIlya Bakulin panic("Cannot get host max data");
12185d20e651SIlya Bakulin KASSERT(cts->host_max_data != 0, ("host_max_data == 0?!"));
12195d20e651SIlya Bakulin return (cts->host_max_data);
12205d20e651SIlya Bakulin }
12215d20e651SIlya Bakulin
1222a94a63f0SWarner Losh static void
sdda_start_init(void * context,union ccb * start_ccb)122396e47614SIlya Bakulin sdda_start_init(void *context, union ccb *start_ccb)
122496e47614SIlya Bakulin {
122596e47614SIlya Bakulin struct cam_periph *periph = (struct cam_periph *)context;
122696e47614SIlya Bakulin struct ccb_trans_settings_mmc *cts;
122796e47614SIlya Bakulin uint32_t host_caps;
122896e47614SIlya Bakulin uint32_t sec_count;
1229a94a63f0SWarner Losh int err;
123096e47614SIlya Bakulin int host_f_max;
1231fd7371f7SEmmanuel Vadot uint8_t card_type;
1232a94a63f0SWarner Losh
1233a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init\n"));
1234a94a63f0SWarner Losh /* periph was held for us when this task was enqueued */
1235a94a63f0SWarner Losh if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1236a94a63f0SWarner Losh cam_periph_release(periph);
1237a94a63f0SWarner Losh return;
1238a94a63f0SWarner Losh }
1239a94a63f0SWarner Losh
1240a94a63f0SWarner Losh struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1241a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1242a94a63f0SWarner Losh struct cam_ed *device = periph->path->device;
1243a94a63f0SWarner Losh
1244a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) {
1245a94a63f0SWarner Losh mmc_decode_csd_mmc(mmcp->card_csd, &softc->csd);
1246a94a63f0SWarner Losh mmc_decode_cid_mmc(mmcp->card_cid, &softc->cid);
124796e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 4) {
1248a94a63f0SWarner Losh err = mmc_send_ext_csd(periph, start_ccb,
1249a94a63f0SWarner Losh (uint8_t *)&softc->raw_ext_csd,
1250a94a63f0SWarner Losh sizeof(softc->raw_ext_csd));
125196e47614SIlya Bakulin if (err != 0) {
125296e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
125396e47614SIlya Bakulin ("Cannot read EXT_CSD, err %d", err));
125496e47614SIlya Bakulin return;
125596e47614SIlya Bakulin }
125696e47614SIlya Bakulin }
1257a94a63f0SWarner Losh } else {
1258a94a63f0SWarner Losh mmc_decode_csd_sd(mmcp->card_csd, &softc->csd);
1259a94a63f0SWarner Losh mmc_decode_cid_sd(mmcp->card_cid, &softc->cid);
1260a94a63f0SWarner Losh }
1261a94a63f0SWarner Losh
126260b7d5a2SAndriy Gapon softc->sector_count = softc->csd.capacity / MMC_SECTOR_SIZE;
1263a94a63f0SWarner Losh softc->mediasize = softc->csd.capacity;
126496e47614SIlya Bakulin softc->cmd6_time = mmc_get_cmd6_timeout(periph);
1265a94a63f0SWarner Losh
1266a94a63f0SWarner Losh /* MMC >= 4.x have EXT_CSD that has its own opinion about capacity */
126796e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 4) {
126896e47614SIlya Bakulin sec_count = softc->raw_ext_csd[EXT_CSD_SEC_CNT] +
1269a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1270a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1271a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1272a94a63f0SWarner Losh if (sec_count != 0) {
1273a94a63f0SWarner Losh softc->sector_count = sec_count;
127460b7d5a2SAndriy Gapon softc->mediasize = softc->sector_count * MMC_SECTOR_SIZE;
1275a94a63f0SWarner Losh /* FIXME: there should be a better name for this option...*/
1276a94a63f0SWarner Losh mmcp->card_features |= CARD_FEATURE_SDHC;
1277a94a63f0SWarner Losh }
1278a94a63f0SWarner Losh }
1279a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1280a94a63f0SWarner Losh ("Capacity: %"PRIu64", sectors: %"PRIu64"\n",
1281a94a63f0SWarner Losh softc->mediasize,
1282a94a63f0SWarner Losh softc->sector_count));
1283a94a63f0SWarner Losh mmc_format_card_id_string(softc, mmcp);
1284a94a63f0SWarner Losh
1285a94a63f0SWarner Losh /* Update info for CAM */
1286a94a63f0SWarner Losh device->serial_num_len = strlen(softc->card_sn_string);
128773551d4fSWarner Losh device->serial_num = (uint8_t *)malloc((device->serial_num_len + 1),
1288a94a63f0SWarner Losh M_CAMXPT, M_NOWAIT);
12897e06495bSJung-uk Kim strlcpy(device->serial_num, softc->card_sn_string, device->serial_num_len + 1);
1290a94a63f0SWarner Losh
1291a94a63f0SWarner Losh device->device_id_len = strlen(softc->card_id_string);
129273551d4fSWarner Losh device->device_id = (uint8_t *)malloc((device->device_id_len + 1),
1293a94a63f0SWarner Losh M_CAMXPT, M_NOWAIT);
12947e06495bSJung-uk Kim strlcpy(device->device_id, softc->card_id_string, device->device_id_len + 1);
1295a94a63f0SWarner Losh
1296a94a63f0SWarner Losh strlcpy(mmcp->model, softc->card_id_string, sizeof(mmcp->model));
1297a94a63f0SWarner Losh
1298a94a63f0SWarner Losh /* Set the clock frequency that the card can handle */
1299a94a63f0SWarner Losh cts = &start_ccb->cts.proto_specific.mmc;
1300a94a63f0SWarner Losh
1301a94a63f0SWarner Losh /* First, get the host's max freq */
1302a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1303a94a63f0SWarner Losh start_ccb->ccb_h.flags = CAM_DIR_NONE;
1304a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0;
1305a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 100;
1306a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = NULL;
1307a94a63f0SWarner Losh xpt_action(start_ccb);
1308a94a63f0SWarner Losh
1309a94a63f0SWarner Losh if (start_ccb->ccb_h.status != CAM_REQ_CMP)
1310a94a63f0SWarner Losh panic("Cannot get max host freq");
131196e47614SIlya Bakulin host_f_max = cts->host_f_max;
131296e47614SIlya Bakulin host_caps = cts->host_caps;
1313a94a63f0SWarner Losh if (cts->ios.bus_width != bus_width_1)
1314a94a63f0SWarner Losh panic("Bus width in ios is not 1-bit");
1315a94a63f0SWarner Losh
1316a94a63f0SWarner Losh /* Now check if the card supports High-speed */
1317a94a63f0SWarner Losh softc->card_f_max = softc->csd.tran_speed;
1318a94a63f0SWarner Losh
1319a94a63f0SWarner Losh if (host_caps & MMC_CAP_HSPEED) {
1320a94a63f0SWarner Losh /* Find out if the card supports High speed timing */
1321a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_SD20) {
1322a94a63f0SWarner Losh /* Get and decode SCR */
13234c4200c6SIlya Bakulin uint32_t rawscr[2];
1324a94a63f0SWarner Losh uint8_t res[64];
13254c4200c6SIlya Bakulin if (mmc_app_get_scr(periph, start_ccb, rawscr)) {
1326a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Cannot get SCR\n"));
1327a94a63f0SWarner Losh goto finish_hs_tests;
1328a94a63f0SWarner Losh }
13294c4200c6SIlya Bakulin mmc_app_decode_scr(rawscr, &softc->scr);
1330a94a63f0SWarner Losh
1331a94a63f0SWarner Losh if ((softc->scr.sda_vsn >= 1) && (softc->csd.ccc & (1<<10))) {
1332a94a63f0SWarner Losh mmc_sd_switch(periph, start_ccb, SD_SWITCH_MODE_CHECK,
1333a94a63f0SWarner Losh SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, res);
1334a94a63f0SWarner Losh if (res[13] & 2) {
1335a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS\n"));
1336a94a63f0SWarner Losh softc->card_f_max = SD_HS_MAX;
1337a94a63f0SWarner Losh }
1338d670d951SIlya Bakulin
1339d670d951SIlya Bakulin /*
1340d670d951SIlya Bakulin * We deselect then reselect the card here. Some cards
1341d670d951SIlya Bakulin * become unselected and timeout with the above two
1342d670d951SIlya Bakulin * commands, although the state tables / diagrams in the
1343d670d951SIlya Bakulin * standard suggest they go back to the transfer state.
1344d670d951SIlya Bakulin * Other cards don't become deselected, and if we
1345d670d951SIlya Bakulin * attempt to blindly re-select them, we get timeout
1346d670d951SIlya Bakulin * errors from some controllers. So we deselect then
1347d670d951SIlya Bakulin * reselect to handle all situations.
1348d670d951SIlya Bakulin */
1349d670d951SIlya Bakulin mmc_select_card(periph, start_ccb, 0);
1350d670d951SIlya Bakulin mmc_select_card(periph, start_ccb, get_rca(periph));
1351a94a63f0SWarner Losh } else {
1352a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Not trying the switch\n"));
1353a94a63f0SWarner Losh goto finish_hs_tests;
1354a94a63f0SWarner Losh }
1355a94a63f0SWarner Losh }
1356a94a63f0SWarner Losh
135796e47614SIlya Bakulin if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
1358fd7371f7SEmmanuel Vadot card_type = softc->raw_ext_csd[EXT_CSD_CARD_TYPE];
1359fd7371f7SEmmanuel Vadot if (card_type & EXT_CSD_CARD_TYPE_HS_52)
1360a94a63f0SWarner Losh softc->card_f_max = MMC_TYPE_HS_52_MAX;
1361fd7371f7SEmmanuel Vadot else if (card_type & EXT_CSD_CARD_TYPE_HS_26)
1362a94a63f0SWarner Losh softc->card_f_max = MMC_TYPE_HS_26_MAX;
1363fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 &&
1364fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1365fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_ddr52);
1366fd7371f7SEmmanuel Vadot setbit(&softc->vccq_120, bus_timing_mmc_ddr52);
1367fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.2V\n"));
1368fd7371f7SEmmanuel Vadot }
1369fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 &&
1370fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1371fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_ddr52);
1372fd7371f7SEmmanuel Vadot setbit(&softc->vccq_180, bus_timing_mmc_ddr52);
1373fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.8V\n"));
1374fd7371f7SEmmanuel Vadot }
1375fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) != 0 &&
1376fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1377fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_hs200);
1378fd7371f7SEmmanuel Vadot setbit(&softc->vccq_120, bus_timing_mmc_hs200);
1379fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.2V\n"));
1380fd7371f7SEmmanuel Vadot }
1381fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) != 0 &&
1382fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1383fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_hs200);
1384fd7371f7SEmmanuel Vadot setbit(&softc->vccq_180, bus_timing_mmc_hs200);
1385fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.8V\n"));
1386fd7371f7SEmmanuel Vadot }
1387a94a63f0SWarner Losh }
1388a94a63f0SWarner Losh }
1389a94a63f0SWarner Losh int f_max;
1390a94a63f0SWarner Losh finish_hs_tests:
1391a94a63f0SWarner Losh f_max = min(host_f_max, softc->card_f_max);
1392a94a63f0SWarner 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));
1393a94a63f0SWarner Losh
1394d670d951SIlya Bakulin /* Enable high-speed timing on the card */
1395d670d951SIlya Bakulin if (f_max > 25000000) {
1396d670d951SIlya Bakulin err = mmc_set_timing(periph, start_ccb, bus_timing_hs);
1397d670d951SIlya Bakulin if (err != MMC_ERR_NONE) {
1398d670d951SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("Cannot switch card to high-speed mode"));
1399d670d951SIlya Bakulin f_max = 25000000;
1400d670d951SIlya Bakulin }
1401d670d951SIlya Bakulin }
1402fd7371f7SEmmanuel Vadot /* If possible, set lower-level signaling */
1403fd7371f7SEmmanuel Vadot enum mmc_bus_timing timing;
1404fd7371f7SEmmanuel Vadot /* FIXME: MMCCAM supports max. bus_timing_mmc_ddr52 at the moment. */
1405fd7371f7SEmmanuel Vadot for (timing = bus_timing_mmc_ddr52; timing > bus_timing_normal; timing--) {
1406fd7371f7SEmmanuel Vadot if (isset(&softc->vccq_120, timing)) {
1407fd7371f7SEmmanuel Vadot /* Set VCCQ = 1.2V */
1408fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1409fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE;
1410fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0;
1411fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100;
1412fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL;
1413fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_120;
1414fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ;
1415fd7371f7SEmmanuel Vadot xpt_action(start_ccb);
1416fd7371f7SEmmanuel Vadot break;
1417fd7371f7SEmmanuel Vadot } else if (isset(&softc->vccq_180, timing)) {
1418fd7371f7SEmmanuel Vadot /* Set VCCQ = 1.8V */
1419fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1420fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE;
1421fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0;
1422fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100;
1423fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL;
1424fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_180;
1425fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ;
1426fd7371f7SEmmanuel Vadot xpt_action(start_ccb);
1427fd7371f7SEmmanuel Vadot break;
1428fd7371f7SEmmanuel Vadot } else {
1429fd7371f7SEmmanuel Vadot /* Set VCCQ = 3.3V */
1430fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1431fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE;
1432fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0;
1433fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100;
1434fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL;
1435fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_330;
1436fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ;
1437fd7371f7SEmmanuel Vadot xpt_action(start_ccb);
1438fd7371f7SEmmanuel Vadot break;
1439fd7371f7SEmmanuel Vadot }
1440fd7371f7SEmmanuel Vadot }
1441fd7371f7SEmmanuel Vadot
1442d670d951SIlya Bakulin /* Set frequency on the controller */
1443a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1444a94a63f0SWarner Losh start_ccb->ccb_h.flags = CAM_DIR_NONE;
1445a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0;
1446a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 100;
1447a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = NULL;
1448a94a63f0SWarner Losh cts->ios.clock = f_max;
1449a94a63f0SWarner Losh cts->ios_valid = MMC_CLK;
1450a94a63f0SWarner Losh xpt_action(start_ccb);
1451a94a63f0SWarner Losh
1452a94a63f0SWarner Losh /* Set bus width */
1453a94a63f0SWarner Losh enum mmc_bus_width desired_bus_width = bus_width_1;
1454a94a63f0SWarner Losh enum mmc_bus_width max_host_bus_width =
1455a94a63f0SWarner Losh (host_caps & MMC_CAP_8_BIT_DATA ? bus_width_8 :
1456a94a63f0SWarner Losh host_caps & MMC_CAP_4_BIT_DATA ? bus_width_4 : bus_width_1);
1457a94a63f0SWarner Losh enum mmc_bus_width max_card_bus_width = bus_width_1;
1458a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_SD20 &&
1459a94a63f0SWarner Losh softc->scr.bus_widths & SD_SCR_BUS_WIDTH_4)
1460a94a63f0SWarner Losh max_card_bus_width = bus_width_4;
1461a94a63f0SWarner Losh /*
1462a94a63f0SWarner Losh * Unlike SD, MMC cards don't have any information about supported bus width...
1463a94a63f0SWarner Losh * So we need to perform read/write test to find out the width.
1464a94a63f0SWarner Losh */
1465a94a63f0SWarner Losh /* TODO: figure out bus width for MMC; use 8-bit for now (to test on BBB) */
1466a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC)
1467a94a63f0SWarner Losh max_card_bus_width = bus_width_8;
1468a94a63f0SWarner Losh
1469a94a63f0SWarner Losh desired_bus_width = min(max_host_bus_width, max_card_bus_width);
1470a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1471a94a63f0SWarner Losh ("Set bus width to %s (min of host %s and card %s)\n",
1472a94a63f0SWarner Losh bus_width_str(desired_bus_width),
1473a94a63f0SWarner Losh bus_width_str(max_host_bus_width),
1474a94a63f0SWarner Losh bus_width_str(max_card_bus_width)));
1475a94a63f0SWarner Losh sdda_set_bus_width(periph, start_ccb, desired_bus_width);
1476a94a63f0SWarner Losh
1477a94a63f0SWarner Losh softc->state = SDDA_STATE_NORMAL;
147896e47614SIlya Bakulin
1479e70d59c0SEmmanuel Vadot cam_periph_unhold(periph);
148096e47614SIlya Bakulin /* MMC partitions support */
148196e47614SIlya Bakulin if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
148296e47614SIlya Bakulin sdda_process_mmc_partitions(periph, start_ccb);
14836fd84a62SAndriy Gapon } else if (mmcp->card_features & CARD_FEATURE_MEMORY) {
148496e47614SIlya Bakulin /* For SD[HC] cards, just add one partition that is the whole card */
1485*a84d91d8SBjoern A. Zeeb if (sdda_add_part(periph, 0, SDDA_FMT,
148696e47614SIlya Bakulin periph->unit_number,
148796e47614SIlya Bakulin mmc_get_media_size(periph),
14882fe1b4caSEmmanuel Vadot sdda_get_read_only(periph, start_ccb)) == false)
14892fe1b4caSEmmanuel Vadot return;
149096e47614SIlya Bakulin softc->part_curr = 0;
149196e47614SIlya Bakulin }
1492e70d59c0SEmmanuel Vadot cam_periph_hold(periph, PRIBIO|PCATCH);
149396e47614SIlya Bakulin
149496e47614SIlya Bakulin xpt_announce_periph(periph, softc->card_id_string);
149596e47614SIlya Bakulin /*
149696e47614SIlya Bakulin * Add async callbacks for bus reset and bus device reset calls.
149796e47614SIlya Bakulin * I don't bother checking if this fails as, in most cases,
149896e47614SIlya Bakulin * the system will function just fine without them and the only
149996e47614SIlya Bakulin * alternative would be to not attach the device on failure.
150096e47614SIlya Bakulin */
150196e47614SIlya Bakulin xpt_register_async(AC_LOST_DEVICE | AC_GETDEV_CHANGED |
150296e47614SIlya Bakulin AC_ADVINFO_CHANGED, sddaasync, periph, periph->path);
150396e47614SIlya Bakulin }
150496e47614SIlya Bakulin
15052fe1b4caSEmmanuel Vadot static bool
sdda_add_part(struct cam_periph * periph,u_int type,const char * name,u_int cnt,off_t media_size,bool ro)150696e47614SIlya Bakulin sdda_add_part(struct cam_periph *periph, u_int type, const char *name,
150796e47614SIlya Bakulin u_int cnt, off_t media_size, bool ro)
150896e47614SIlya Bakulin {
150996e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
151096e47614SIlya Bakulin struct sdda_part *part;
151196e47614SIlya Bakulin struct ccb_pathinq cpi;
151296e47614SIlya Bakulin
151396e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
151496e47614SIlya Bakulin ("Partition type '%s', size %ju %s\n",
151596e47614SIlya Bakulin part_type(type),
151696e47614SIlya Bakulin media_size,
151796e47614SIlya Bakulin ro ? "(read-only)" : ""));
151896e47614SIlya Bakulin
151996e47614SIlya Bakulin part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
15202fe1b4caSEmmanuel Vadot M_NOWAIT | M_ZERO);
15212fe1b4caSEmmanuel Vadot if (part == NULL) {
15222fe1b4caSEmmanuel Vadot printf("Cannot add partition for sdda\n");
15232fe1b4caSEmmanuel Vadot return (false);
15242fe1b4caSEmmanuel Vadot }
152596e47614SIlya Bakulin
152696e47614SIlya Bakulin part->cnt = cnt;
152796e47614SIlya Bakulin part->type = type;
152896e47614SIlya Bakulin part->ro = ro;
152996e47614SIlya Bakulin part->sc = sc;
1530*a84d91d8SBjoern A. Zeeb snprintf(part->name, sizeof(part->name), name, "sdda", periph->unit_number);
153196e47614SIlya Bakulin
153296e47614SIlya Bakulin /*
153396e47614SIlya Bakulin * Due to the nature of RPMB partition it doesn't make much sense
153496e47614SIlya Bakulin * to add it as a disk. It would be more appropriate to create a
153596e47614SIlya Bakulin * userland tool to operate on the partition or leverage the existing
153696e47614SIlya Bakulin * tools from sysutils/mmc-utils.
153796e47614SIlya Bakulin */
153896e47614SIlya Bakulin if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
153996e47614SIlya Bakulin /* TODO: Create device, assign IOCTL handler */
154096e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
154196e47614SIlya Bakulin ("Don't know what to do with RPMB partitions yet\n"));
15422fe1b4caSEmmanuel Vadot return (false);
154396e47614SIlya Bakulin }
154496e47614SIlya Bakulin
154596e47614SIlya Bakulin bioq_init(&part->bio_queue);
154696e47614SIlya Bakulin
154796e47614SIlya Bakulin bzero(&cpi, sizeof(cpi));
154896e47614SIlya Bakulin xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
154996e47614SIlya Bakulin cpi.ccb_h.func_code = XPT_PATH_INQ;
155096e47614SIlya Bakulin xpt_action((union ccb *)&cpi);
155196e47614SIlya Bakulin
155296e47614SIlya Bakulin /*
155396e47614SIlya Bakulin * Register this media as a disk
155496e47614SIlya Bakulin */
155596e47614SIlya Bakulin (void)cam_periph_hold(periph, PRIBIO);
155696e47614SIlya Bakulin cam_periph_unlock(periph);
155796e47614SIlya Bakulin
155896e47614SIlya Bakulin part->disk = disk_alloc();
155996e47614SIlya Bakulin part->disk->d_rotation_rate = DISK_RR_NON_ROTATING;
156096e47614SIlya Bakulin part->disk->d_devstat = devstat_new_entry(part->name,
156160b7d5a2SAndriy Gapon cnt, MMC_SECTOR_SIZE,
156296e47614SIlya Bakulin DEVSTAT_ALL_SUPPORTED,
156396e47614SIlya Bakulin DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
156496e47614SIlya Bakulin DEVSTAT_PRIORITY_DISK);
156596e47614SIlya Bakulin
156696e47614SIlya Bakulin part->disk->d_open = sddaopen;
156796e47614SIlya Bakulin part->disk->d_close = sddaclose;
156896e47614SIlya Bakulin part->disk->d_strategy = sddastrategy;
156944682688SAndriy Gapon if (cam_sim_pollable(periph->sim))
157044682688SAndriy Gapon part->disk->d_dump = sddadump;
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
1597*a84d91d8SBjoern A. Zeeb if (sdda_mmcsd_compat) {
1598*a84d91d8SBjoern A. Zeeb char cname[SDDA_PART_NAMELEN]; /* This equals the mmcsd namelen. */
1599*a84d91d8SBjoern A. Zeeb snprintf(cname, sizeof(cname), name, "mmcsd", periph->unit_number);
1600*a84d91d8SBjoern A. Zeeb disk_add_alias(part->disk, cname);
1601*a84d91d8SBjoern A. Zeeb }
1602bf286853SEmmanuel Vadot
160396e47614SIlya Bakulin /*
160496e47614SIlya Bakulin * Acquire a reference to the periph before we register with GEOM.
160596e47614SIlya Bakulin * We'll release this reference once GEOM calls us back (via
160696e47614SIlya Bakulin * sddadiskgonecb()) telling us that our provider has been freed.
160796e47614SIlya Bakulin */
160896e47614SIlya Bakulin if (cam_periph_acquire(periph) != 0) {
160996e47614SIlya Bakulin xpt_print(periph->path, "%s: lost periph during "
161096e47614SIlya Bakulin "registration!\n", __func__);
161196e47614SIlya Bakulin cam_periph_lock(periph);
16122fe1b4caSEmmanuel Vadot return (false);
161396e47614SIlya Bakulin }
161496e47614SIlya Bakulin disk_create(part->disk, DISK_VERSION);
161596e47614SIlya Bakulin cam_periph_lock(periph);
161696e47614SIlya Bakulin cam_periph_unhold(periph);
16172fe1b4caSEmmanuel Vadot
16182fe1b4caSEmmanuel Vadot return (true);
161996e47614SIlya Bakulin }
162096e47614SIlya Bakulin
162196e47614SIlya Bakulin /*
162296e47614SIlya Bakulin * For MMC cards, process EXT_CSD and add partitions that are supported by
162396e47614SIlya Bakulin * this device.
162496e47614SIlya Bakulin */
162596e47614SIlya Bakulin static void
sdda_process_mmc_partitions(struct cam_periph * periph,union ccb * ccb)162696e47614SIlya Bakulin sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *ccb)
162796e47614SIlya Bakulin {
162896e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
162996e47614SIlya Bakulin struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
163096e47614SIlya Bakulin off_t erase_size, sector_size, size, wp_size;
163196e47614SIlya Bakulin int i;
163296e47614SIlya Bakulin const uint8_t *ext_csd;
163396e47614SIlya Bakulin uint8_t rev;
163496e47614SIlya Bakulin bool comp, ro;
163596e47614SIlya Bakulin
163696e47614SIlya Bakulin ext_csd = sc->raw_ext_csd;
163796e47614SIlya Bakulin
163896e47614SIlya Bakulin /*
163996e47614SIlya Bakulin * Enhanced user data area and general purpose partitions are only
164096e47614SIlya Bakulin * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
164196e47614SIlya Bakulin * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
164296e47614SIlya Bakulin */
164396e47614SIlya Bakulin rev = ext_csd[EXT_CSD_REV];
164496e47614SIlya Bakulin
164596e47614SIlya Bakulin /*
164696e47614SIlya Bakulin * Ignore user-creatable enhanced user data area and general purpose
164796e47614SIlya Bakulin * partitions partitions as long as partitioning hasn't been finished.
164896e47614SIlya Bakulin */
164996e47614SIlya Bakulin comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
165096e47614SIlya Bakulin
165196e47614SIlya Bakulin /*
165296e47614SIlya Bakulin * Add enhanced user data area slice, unless it spans the entirety of
165396e47614SIlya Bakulin * the user data area. The enhanced area is of a multiple of high
165496e47614SIlya Bakulin * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
165596e47614SIlya Bakulin * 512 KB) and its offset given in either sectors or bytes, depending
165696e47614SIlya Bakulin * on whether it's a high capacity device or not.
165796e47614SIlya Bakulin * NB: The slicer and its slices need to be registered before adding
165896e47614SIlya Bakulin * the disk for the corresponding user data area as re-tasting is
165996e47614SIlya Bakulin * racy.
166096e47614SIlya Bakulin */
166196e47614SIlya Bakulin sector_size = mmc_get_sector_size(periph);
166296e47614SIlya Bakulin size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
166396e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
166496e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
166596e47614SIlya Bakulin if (rev >= 4 && comp == TRUE && size > 0 &&
166696e47614SIlya Bakulin (ext_csd[EXT_CSD_PART_SUPPORT] &
166796e47614SIlya Bakulin EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
166896e47614SIlya Bakulin (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
166996e47614SIlya Bakulin erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
167096e47614SIlya Bakulin MMC_SECTOR_SIZE;
167196e47614SIlya Bakulin wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
167296e47614SIlya Bakulin size *= erase_size * wp_size;
167396e47614SIlya Bakulin if (size != mmc_get_media_size(periph) * sector_size) {
167496e47614SIlya Bakulin sc->enh_size = size;
167596e47614SIlya Bakulin sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
167696e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
167796e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
167896e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
167996e47614SIlya Bakulin ((mmcp->card_features & CARD_FEATURE_SDHC) ? 1: MMC_SECTOR_SIZE);
168096e47614SIlya Bakulin } else
168196e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
168296e47614SIlya Bakulin ("enhanced user data area spans entire device"));
168396e47614SIlya Bakulin }
168496e47614SIlya Bakulin
168596e47614SIlya Bakulin /*
168696e47614SIlya Bakulin * Add default partition. This may be the only one or the user
168796e47614SIlya Bakulin * data area in case partitions are supported.
168896e47614SIlya Bakulin */
168996e47614SIlya Bakulin ro = sdda_get_read_only(periph, ccb);
1690*a84d91d8SBjoern A. Zeeb sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_DEFAULT, SDDA_FMT,
169196e47614SIlya Bakulin periph->unit_number, mmc_get_media_size(periph), ro);
169296e47614SIlya Bakulin sc->part_curr = EXT_CSD_PART_CONFIG_ACC_DEFAULT;
169396e47614SIlya Bakulin
169496e47614SIlya Bakulin if (mmc_get_spec_vers(periph) < 3)
169596e47614SIlya Bakulin return;
169696e47614SIlya Bakulin
169796e47614SIlya Bakulin /* Belatedly announce enhanced user data slice. */
169896e47614SIlya Bakulin if (sc->enh_size != 0) {
169996e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
170096e47614SIlya Bakulin ("enhanced user data area off 0x%jx size %ju bytes\n",
170196e47614SIlya Bakulin sc->enh_base, sc->enh_size));
170296e47614SIlya Bakulin }
170396e47614SIlya Bakulin
170496e47614SIlya Bakulin /*
170596e47614SIlya Bakulin * Determine partition switch timeout (provided in units of 10 ms)
170696e47614SIlya Bakulin * and ensure it's at least 300 ms as some eMMC chips lie.
170796e47614SIlya Bakulin */
170896e47614SIlya Bakulin sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
170996e47614SIlya Bakulin 300 * 1000);
171096e47614SIlya Bakulin
171196e47614SIlya Bakulin /* Add boot partitions, which are of a fixed multiple of 128 KB. */
171296e47614SIlya Bakulin size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
171396e47614SIlya Bakulin if (size > 0 && (sdda_get_host_caps(periph, ccb) & MMC_CAP_BOOT_NOACC) == 0) {
171496e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT0,
171596e47614SIlya Bakulin SDDA_FMT_BOOT, 0, size,
171696e47614SIlya Bakulin ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
171796e47614SIlya Bakulin EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
171896e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT1,
171996e47614SIlya Bakulin SDDA_FMT_BOOT, 1, size,
172096e47614SIlya Bakulin ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
172196e47614SIlya Bakulin EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
172296e47614SIlya Bakulin }
172396e47614SIlya Bakulin
172496e47614SIlya Bakulin /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
172596e47614SIlya Bakulin size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
172696e47614SIlya Bakulin if (rev >= 5 && size > 0)
172796e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_RPMB,
172896e47614SIlya Bakulin SDDA_FMT_RPMB, 0, size, ro);
172996e47614SIlya Bakulin
173096e47614SIlya Bakulin if (rev <= 3 || comp == FALSE)
173196e47614SIlya Bakulin return;
173296e47614SIlya Bakulin
173396e47614SIlya Bakulin /*
173496e47614SIlya Bakulin * Add general purpose partitions, which are of a multiple of high
173596e47614SIlya Bakulin * capacity write protect groups, too.
173696e47614SIlya Bakulin */
173796e47614SIlya Bakulin if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
173896e47614SIlya Bakulin erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
173996e47614SIlya Bakulin MMC_SECTOR_SIZE;
174096e47614SIlya Bakulin wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
174196e47614SIlya Bakulin for (i = 0; i < MMC_PART_GP_MAX; i++) {
174296e47614SIlya Bakulin size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
174396e47614SIlya Bakulin (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
174496e47614SIlya Bakulin (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
174596e47614SIlya Bakulin if (size == 0)
174696e47614SIlya Bakulin continue;
174796e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
174896e47614SIlya Bakulin SDDA_FMT_GP, i, size * erase_size * wp_size, ro);
174996e47614SIlya Bakulin }
175096e47614SIlya Bakulin }
175196e47614SIlya Bakulin }
175296e47614SIlya Bakulin
175396e47614SIlya Bakulin /*
175496e47614SIlya Bakulin * We cannot just call mmc_switch() since it will sleep, and we are in
175596e47614SIlya Bakulin * GEOM context and cannot sleep. Instead, create an MMCIO request to switch
175696e47614SIlya Bakulin * partitions and send it to h/w, and upon completion resume processing
175796e47614SIlya Bakulin * the I/O queue.
175896e47614SIlya Bakulin * This function cannot fail, instead check switch errors in sddadone().
175996e47614SIlya Bakulin */
176096e47614SIlya Bakulin static void
sdda_init_switch_part(struct cam_periph * periph,union ccb * start_ccb,uint8_t part)176115f4848aSAndriy Gapon sdda_init_switch_part(struct cam_periph *periph, union ccb *start_ccb,
176215f4848aSAndriy Gapon uint8_t part)
176315f4848aSAndriy Gapon {
176496e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
176596e47614SIlya Bakulin uint8_t value;
176696e47614SIlya Bakulin
176715f4848aSAndriy Gapon KASSERT(part < MMC_PART_MAX, ("%s: invalid partition index", __func__));
176896e47614SIlya Bakulin sc->part_requested = part;
176996e47614SIlya Bakulin
177096e47614SIlya Bakulin value = (sc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
177196e47614SIlya Bakulin ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
177296e47614SIlya Bakulin
177396e47614SIlya Bakulin mmc_switch_fill_mmcio(start_ccb, EXT_CSD_CMD_SET_NORMAL,
177496e47614SIlya Bakulin EXT_CSD_PART_CONFIG, value, sc->part_time);
177596e47614SIlya Bakulin start_ccb->ccb_h.cbfcnp = sddadone;
177696e47614SIlya Bakulin
177796e47614SIlya Bakulin sc->outstanding_cmds++;
177896e47614SIlya Bakulin cam_periph_unlock(periph);
177996e47614SIlya Bakulin xpt_action(start_ccb);
178096e47614SIlya Bakulin cam_periph_lock(periph);
1781a94a63f0SWarner Losh }
1782a94a63f0SWarner Losh
1783a94a63f0SWarner Losh /* Called with periph lock held! */
1784a94a63f0SWarner Losh static void
sddastart(struct cam_periph * periph,union ccb * start_ccb)1785a94a63f0SWarner Losh sddastart(struct cam_periph *periph, union ccb *start_ccb)
1786a94a63f0SWarner Losh {
178796e47614SIlya Bakulin struct bio *bp;
1788a94a63f0SWarner Losh struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
178996e47614SIlya Bakulin struct sdda_part *part;
1790a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
179115f4848aSAndriy Gapon uint8_t part_index;
1792a94a63f0SWarner Losh
1793a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastart\n"));
1794a94a63f0SWarner Losh
1795a94a63f0SWarner Losh if (softc->state != SDDA_STATE_NORMAL) {
179696e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("device is not in SDDA_STATE_NORMAL yet\n"));
1797a94a63f0SWarner Losh xpt_release_ccb(start_ccb);
1798a94a63f0SWarner Losh return;
1799a94a63f0SWarner Losh }
1800a94a63f0SWarner Losh
180196e47614SIlya Bakulin /* Find partition that has outstanding commands. Prefer current partition. */
18024dfdaf4dSAndriy Gapon part_index = softc->part_curr;
180396e47614SIlya Bakulin part = softc->part[softc->part_curr];
180496e47614SIlya Bakulin bp = bioq_first(&part->bio_queue);
180596e47614SIlya Bakulin if (bp == NULL) {
180696e47614SIlya Bakulin for (part_index = 0; part_index < MMC_PART_MAX; part_index++) {
180796e47614SIlya Bakulin if ((part = softc->part[part_index]) != NULL &&
180896e47614SIlya Bakulin (bp = bioq_first(&softc->part[part_index]->bio_queue)) != NULL)
180996e47614SIlya Bakulin break;
181096e47614SIlya Bakulin }
181196e47614SIlya Bakulin }
1812a94a63f0SWarner Losh if (bp == NULL) {
1813a94a63f0SWarner Losh xpt_release_ccb(start_ccb);
1814a94a63f0SWarner Losh return;
1815a94a63f0SWarner Losh }
181696e47614SIlya Bakulin if (part_index != softc->part_curr) {
181796e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
181896e47614SIlya Bakulin ("Partition %d -> %d\n", softc->part_curr, part_index));
181996e47614SIlya Bakulin /*
182096e47614SIlya Bakulin * According to section "6.2.2 Command restrictions" of the eMMC
182196e47614SIlya Bakulin * specification v5.1, CMD19/CMD21 aren't allowed to be used with
182296e47614SIlya Bakulin * RPMB partitions. So we pause re-tuning along with triggering
182396e47614SIlya Bakulin * it up-front to decrease the likelihood of re-tuning becoming
182496e47614SIlya Bakulin * necessary while accessing an RPMB partition. Consequently, an
182596e47614SIlya Bakulin * RPMB partition should immediately be switched away from again
182696e47614SIlya Bakulin * after an access in order to allow for re-tuning to take place
182796e47614SIlya Bakulin * anew.
182896e47614SIlya Bakulin */
182996e47614SIlya Bakulin /* TODO: pause retune if switching to RPMB partition */
183096e47614SIlya Bakulin softc->state = SDDA_STATE_PART_SWITCH;
183196e47614SIlya Bakulin sdda_init_switch_part(periph, start_ccb, part_index);
183296e47614SIlya Bakulin return;
183396e47614SIlya Bakulin }
183496e47614SIlya Bakulin
183596e47614SIlya Bakulin bioq_remove(&part->bio_queue, bp);
1836a94a63f0SWarner Losh
1837a94a63f0SWarner Losh switch (bp->bio_cmd) {
1838a94a63f0SWarner Losh case BIO_WRITE:
1839a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_WRITE\n"));
184096e47614SIlya Bakulin part->flags |= SDDA_FLAG_DIRTY;
1841a94a63f0SWarner Losh /* FALLTHROUGH */
1842a94a63f0SWarner Losh case BIO_READ:
1843a94a63f0SWarner Losh {
184496e47614SIlya Bakulin struct ccb_mmcio *mmcio;
1845a94a63f0SWarner Losh uint64_t blockno = bp->bio_pblkno;
184660b7d5a2SAndriy Gapon uint16_t count = bp->bio_bcount / MMC_SECTOR_SIZE;
1847a94a63f0SWarner Losh uint16_t opcode;
1848a94a63f0SWarner Losh
184996e47614SIlya Bakulin if (bp->bio_cmd == BIO_READ)
185096e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_READ\n"));
185196e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
185296e47614SIlya Bakulin ("Block %"PRIu64" cnt %u\n", blockno, count));
1853a94a63f0SWarner Losh
1854a94a63f0SWarner Losh /* Construct new MMC command */
1855a94a63f0SWarner Losh if (bp->bio_cmd == BIO_READ) {
1856a94a63f0SWarner Losh if (count > 1)
1857a94a63f0SWarner Losh opcode = MMC_READ_MULTIPLE_BLOCK;
1858a94a63f0SWarner Losh else
1859a94a63f0SWarner Losh opcode = MMC_READ_SINGLE_BLOCK;
1860a94a63f0SWarner Losh } else {
1861a94a63f0SWarner Losh if (count > 1)
1862a94a63f0SWarner Losh opcode = MMC_WRITE_MULTIPLE_BLOCK;
1863a94a63f0SWarner Losh else
1864a94a63f0SWarner Losh opcode = MMC_WRITE_BLOCK;
1865a94a63f0SWarner Losh }
1866a94a63f0SWarner Losh
1867a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_MMC_IO;
1868a94a63f0SWarner Losh start_ccb->ccb_h.flags = (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : CAM_DIR_OUT);
1869a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0;
1870a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 15 * 1000;
1871a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = sddadone;
1872a94a63f0SWarner Losh
1873a94a63f0SWarner Losh mmcio = &start_ccb->mmcio;
1874a94a63f0SWarner Losh mmcio->cmd.opcode = opcode;
1875a94a63f0SWarner Losh mmcio->cmd.arg = blockno;
1876a94a63f0SWarner Losh if (!(mmcp->card_features & CARD_FEATURE_SDHC))
1877a94a63f0SWarner Losh mmcio->cmd.arg <<= 9;
1878a94a63f0SWarner Losh
1879a94a63f0SWarner Losh mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1880a94a63f0SWarner Losh mmcio->cmd.data = softc->mmcdata;
18810660cfa0SIlya Bakulin memset(mmcio->cmd.data, 0, sizeof(struct mmc_data));
1882a94a63f0SWarner Losh mmcio->cmd.data->data = bp->bio_data;
188360b7d5a2SAndriy Gapon mmcio->cmd.data->len = MMC_SECTOR_SIZE * count;
1884a94a63f0SWarner Losh mmcio->cmd.data->flags = (bp->bio_cmd == BIO_READ ? MMC_DATA_READ : MMC_DATA_WRITE);
1885a94a63f0SWarner Losh /* Direct h/w to issue CMD12 upon completion */
1886a94a63f0SWarner Losh if (count > 1) {
18873f1cfdb1SIlya Bakulin mmcio->cmd.data->flags |= MMC_DATA_MULTI;
1888a94a63f0SWarner Losh mmcio->stop.opcode = MMC_STOP_TRANSMISSION;
1889a94a63f0SWarner Losh mmcio->stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1890a94a63f0SWarner Losh mmcio->stop.arg = 0;
1891a94a63f0SWarner Losh }
1892a94a63f0SWarner Losh
1893a94a63f0SWarner Losh break;
1894a94a63f0SWarner Losh }
1895a94a63f0SWarner Losh case BIO_FLUSH:
1896a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_FLUSH\n"));
1897a94a63f0SWarner Losh sddaschedule(periph);
1898a94a63f0SWarner Losh break;
1899a94a63f0SWarner Losh case BIO_DELETE:
1900a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_DELETE\n"));
1901a94a63f0SWarner Losh sddaschedule(periph);
1902a94a63f0SWarner Losh break;
1903d176b803SScott Long default:
1904d176b803SScott Long biofinish(bp, NULL, EOPNOTSUPP);
1905d176b803SScott Long xpt_release_ccb(start_ccb);
1906d176b803SScott Long return;
1907a94a63f0SWarner Losh }
1908a94a63f0SWarner Losh start_ccb->ccb_h.ccb_bp = bp;
1909a94a63f0SWarner Losh softc->outstanding_cmds++;
1910a94a63f0SWarner Losh softc->refcount++;
1911a94a63f0SWarner Losh cam_periph_unlock(periph);
1912a94a63f0SWarner Losh xpt_action(start_ccb);
1913a94a63f0SWarner Losh cam_periph_lock(periph);
1914a94a63f0SWarner Losh
1915a94a63f0SWarner Losh /* May have more work to do, so ensure we stay scheduled */
1916a94a63f0SWarner Losh sddaschedule(periph);
1917a94a63f0SWarner Losh }
1918a94a63f0SWarner Losh
1919a94a63f0SWarner Losh static void
sddadone(struct cam_periph * periph,union ccb * done_ccb)1920a94a63f0SWarner Losh sddadone(struct cam_periph *periph, union ccb *done_ccb)
1921a94a63f0SWarner Losh {
192296e47614SIlya Bakulin struct bio *bp;
1923a94a63f0SWarner Losh struct sdda_softc *softc;
1924a94a63f0SWarner Losh struct ccb_mmcio *mmcio;
1925a94a63f0SWarner Losh struct cam_path *path;
192696e47614SIlya Bakulin uint32_t card_status;
192796e47614SIlya Bakulin int error = 0;
1928a94a63f0SWarner Losh
1929a94a63f0SWarner Losh softc = (struct sdda_softc *)periph->softc;
1930a94a63f0SWarner Losh mmcio = &done_ccb->mmcio;
1931a94a63f0SWarner Losh path = done_ccb->ccb_h.path;
1932a94a63f0SWarner Losh
1933a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddadone\n"));
1934a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1935a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Error!!!\n"));
1936a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1937a94a63f0SWarner Losh cam_release_devq(path,
1938a94a63f0SWarner Losh /*relsim_flags*/0,
1939a94a63f0SWarner Losh /*reduction*/0,
1940a94a63f0SWarner Losh /*timeout*/0,
1941a94a63f0SWarner Losh /*getcount_only*/0);
1942e17b58ecSAndriy Gapon error = EIO;
1943a94a63f0SWarner Losh } else {
1944a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1945a94a63f0SWarner Losh panic("REQ_CMP with QFRZN");
1946a94a63f0SWarner Losh error = 0;
1947a94a63f0SWarner Losh }
1948a94a63f0SWarner Losh
194996e47614SIlya Bakulin card_status = mmcio->cmd.resp[0];
195096e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE,
195196e47614SIlya Bakulin ("Card status: %08x\n", R1_STATUS(card_status)));
195296e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE,
195396e47614SIlya Bakulin ("Current state: %d\n", R1_CURRENT_STATE(card_status)));
195496e47614SIlya Bakulin
195596e47614SIlya Bakulin /* Process result of switching MMC partitions */
195696e47614SIlya Bakulin if (softc->state == SDDA_STATE_PART_SWITCH) {
195796e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE,
1958fd38fa39SAndriy Gapon ("Completing partition switch to %d\n",
1959fd38fa39SAndriy Gapon softc->part_requested));
196096e47614SIlya Bakulin softc->outstanding_cmds--;
196196e47614SIlya Bakulin /* Complete partition switch */
196296e47614SIlya Bakulin softc->state = SDDA_STATE_NORMAL;
1963e17b58ecSAndriy Gapon if (error != 0) {
196496e47614SIlya Bakulin /* TODO: Unpause retune if accessing RPMB */
196596e47614SIlya Bakulin xpt_release_ccb(done_ccb);
196696e47614SIlya Bakulin xpt_schedule(periph, CAM_PRIORITY_NORMAL);
196796e47614SIlya Bakulin return;
196896e47614SIlya Bakulin }
196996e47614SIlya Bakulin
197096e47614SIlya Bakulin softc->raw_ext_csd[EXT_CSD_PART_CONFIG] =
197196e47614SIlya Bakulin (softc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
197296e47614SIlya Bakulin ~EXT_CSD_PART_CONFIG_ACC_MASK) | softc->part_requested;
197396e47614SIlya Bakulin /* TODO: Unpause retune if accessing RPMB */
197496e47614SIlya Bakulin softc->part_curr = softc->part_requested;
197596e47614SIlya Bakulin xpt_release_ccb(done_ccb);
197696e47614SIlya Bakulin
197796e47614SIlya Bakulin /* Return to processing BIO requests */
197896e47614SIlya Bakulin xpt_schedule(periph, CAM_PRIORITY_NORMAL);
197996e47614SIlya Bakulin return;
198096e47614SIlya Bakulin }
1981a94a63f0SWarner Losh
1982a94a63f0SWarner Losh bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1983a94a63f0SWarner Losh bp->bio_error = error;
1984a94a63f0SWarner Losh if (error != 0) {
1985a94a63f0SWarner Losh bp->bio_resid = bp->bio_bcount;
1986a94a63f0SWarner Losh bp->bio_flags |= BIO_ERROR;
1987a94a63f0SWarner Losh } else {
1988a94a63f0SWarner Losh /* XXX: How many bytes remaining? */
1989a94a63f0SWarner Losh bp->bio_resid = 0;
1990a94a63f0SWarner Losh if (bp->bio_resid > 0)
1991a94a63f0SWarner Losh bp->bio_flags |= BIO_ERROR;
1992a94a63f0SWarner Losh }
1993a94a63f0SWarner Losh
1994a94a63f0SWarner Losh softc->outstanding_cmds--;
1995a94a63f0SWarner Losh xpt_release_ccb(done_ccb);
1996d9a7a61bSWarner Losh /*
199796e47614SIlya Bakulin * Release the periph refcount taken in sddastart() for each CCB.
1998d9a7a61bSWarner Losh */
199996e47614SIlya Bakulin KASSERT(softc->refcount >= 1, ("sddadone softc %p refcount %d", softc, softc->refcount));
2000d9a7a61bSWarner Losh softc->refcount--;
2001a94a63f0SWarner Losh biodone(bp);
2002a94a63f0SWarner Losh }
2003a94a63f0SWarner Losh
2004a94a63f0SWarner Losh static int
sddaerror(union ccb * ccb,uint32_t cam_flags,uint32_t sense_flags)200573551d4fSWarner Losh sddaerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
2006a94a63f0SWarner Losh {
2007553484aeSWarner Losh return(cam_periph_error(ccb, cam_flags, sense_flags));
2008a94a63f0SWarner Losh }
200944682688SAndriy Gapon
201044682688SAndriy Gapon static int
sddadump(void * arg,void * virtual,off_t offset,size_t length)2011489ba222SMitchell Horne sddadump(void *arg, void *virtual, off_t offset, size_t length)
201244682688SAndriy Gapon {
201344682688SAndriy Gapon struct ccb_mmcio mmcio;
201444682688SAndriy Gapon struct disk *dp;
201544682688SAndriy Gapon struct sdda_part *part;
201644682688SAndriy Gapon struct sdda_softc *softc;
201744682688SAndriy Gapon struct cam_periph *periph;
201844682688SAndriy Gapon struct mmc_params *mmcp;
201944682688SAndriy Gapon uint16_t count;
202044682688SAndriy Gapon uint16_t opcode;
202144682688SAndriy Gapon int error;
202244682688SAndriy Gapon
202344682688SAndriy Gapon dp = arg;
202444682688SAndriy Gapon part = dp->d_drv1;
202544682688SAndriy Gapon softc = part->sc;
202644682688SAndriy Gapon periph = softc->periph;
202744682688SAndriy Gapon mmcp = &periph->path->device->mmc_ident_data;
202844682688SAndriy Gapon
202944682688SAndriy Gapon if (softc->state != SDDA_STATE_NORMAL)
203044682688SAndriy Gapon return (ENXIO);
203144682688SAndriy Gapon
203260b7d5a2SAndriy Gapon count = length / MMC_SECTOR_SIZE;
203344682688SAndriy Gapon if (count == 0)
203444682688SAndriy Gapon return (0);
203544682688SAndriy Gapon
203644682688SAndriy Gapon if (softc->part[softc->part_curr] != part)
203744682688SAndriy Gapon return (EIO); /* TODO implement polled partition switch */
203844682688SAndriy Gapon
203944682688SAndriy Gapon memset(&mmcio, 0, sizeof(mmcio));
204044682688SAndriy Gapon xpt_setup_ccb(&mmcio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); /* XXX needed? */
204144682688SAndriy Gapon
204244682688SAndriy Gapon mmcio.ccb_h.func_code = XPT_MMC_IO;
204344682688SAndriy Gapon mmcio.ccb_h.flags = CAM_DIR_OUT;
204444682688SAndriy Gapon mmcio.ccb_h.retry_count = 0;
204544682688SAndriy Gapon mmcio.ccb_h.timeout = 15 * 1000;
204644682688SAndriy Gapon
204744682688SAndriy Gapon if (count > 1)
204844682688SAndriy Gapon opcode = MMC_WRITE_MULTIPLE_BLOCK;
204944682688SAndriy Gapon else
205044682688SAndriy Gapon opcode = MMC_WRITE_BLOCK;
205144682688SAndriy Gapon mmcio.cmd.opcode = opcode;
205260b7d5a2SAndriy Gapon mmcio.cmd.arg = offset / MMC_SECTOR_SIZE;
205344682688SAndriy Gapon if (!(mmcp->card_features & CARD_FEATURE_SDHC))
205444682688SAndriy Gapon mmcio.cmd.arg <<= 9;
205544682688SAndriy Gapon
205644682688SAndriy Gapon mmcio.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
205744682688SAndriy Gapon mmcio.cmd.data = softc->mmcdata;
205844682688SAndriy Gapon memset(mmcio.cmd.data, 0, sizeof(struct mmc_data));
205944682688SAndriy Gapon mmcio.cmd.data->data = virtual;
206060b7d5a2SAndriy Gapon mmcio.cmd.data->len = MMC_SECTOR_SIZE * count;
206144682688SAndriy Gapon mmcio.cmd.data->flags = MMC_DATA_WRITE;
206244682688SAndriy Gapon
206344682688SAndriy Gapon /* Direct h/w to issue CMD12 upon completion */
206444682688SAndriy Gapon if (count > 1) {
206544682688SAndriy Gapon mmcio.cmd.data->flags |= MMC_DATA_MULTI;
206644682688SAndriy Gapon mmcio.stop.opcode = MMC_STOP_TRANSMISSION;
206744682688SAndriy Gapon mmcio.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
206844682688SAndriy Gapon mmcio.stop.arg = 0;
206944682688SAndriy Gapon }
207044682688SAndriy Gapon
207144682688SAndriy Gapon error = cam_periph_runccb((union ccb *)&mmcio, cam_periph_error,
207244682688SAndriy Gapon 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
207344682688SAndriy Gapon if (error != 0)
207444682688SAndriy Gapon printf("Aborting dump due to I/O error.\n");
207544682688SAndriy Gapon return (error);
207644682688SAndriy Gapon }
207744682688SAndriy Gapon
2078a94a63f0SWarner Losh #endif /* _KERNEL */
2079