1a94a63f0SWarner Losh /*- 2f24882ecSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3f24882ecSPedro F. Giffuni * 4f86e6000SWarner Losh * Copyright (c) 2006 Bernd Walter <tisco@FreeBSD.org> All rights reserved. 5f86e6000SWarner Losh * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> All rights reserved. 6f86e6000SWarner Losh * Copyright (c) 2015-2017 Ilya Bakulin <kibab@FreeBSD.org> All rights reserved. 7a94a63f0SWarner Losh * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org> 8a94a63f0SWarner Losh * 9a94a63f0SWarner Losh * Redistribution and use in source and binary forms, with or without 10a94a63f0SWarner Losh * modification, are permitted provided that the following conditions 11a94a63f0SWarner Losh * are met: 12a94a63f0SWarner Losh * 1. Redistributions of source code must retain the above copyright 13a94a63f0SWarner Losh * notice, this list of conditions and the following disclaimer, 14a94a63f0SWarner Losh * without modification, immediately at the beginning of the file. 15a94a63f0SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 16a94a63f0SWarner Losh * notice, this list of conditions and the following disclaimer in the 17a94a63f0SWarner Losh * documentation and/or other materials provided with the distribution. 18a94a63f0SWarner Losh * 19a94a63f0SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20a94a63f0SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21a94a63f0SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22a94a63f0SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23a94a63f0SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24a94a63f0SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25a94a63f0SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26a94a63f0SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27a94a63f0SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28a94a63f0SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29a94a63f0SWarner Losh * 30a94a63f0SWarner Losh * Some code derived from the sys/dev/mmc and sys/cam/ata 31a94a63f0SWarner Losh * Thanks to Warner Losh <imp@FreeBSD.org>, Alexander Motin <mav@FreeBSD.org> 32a94a63f0SWarner Losh * Bernd Walter <tisco@FreeBSD.org>, and other authors. 33a94a63f0SWarner Losh */ 34a94a63f0SWarner Losh 35a94a63f0SWarner Losh #include <sys/cdefs.h> 36a94a63f0SWarner Losh __FBSDID("$FreeBSD$"); 37a94a63f0SWarner Losh 38a94a63f0SWarner Losh //#include "opt_sdda.h" 39a94a63f0SWarner Losh 40a94a63f0SWarner Losh #include <sys/param.h> 41a94a63f0SWarner Losh 42a94a63f0SWarner Losh #ifdef _KERNEL 43a94a63f0SWarner Losh #include <sys/systm.h> 44a94a63f0SWarner Losh #include <sys/kernel.h> 45a94a63f0SWarner Losh #include <sys/bio.h> 46bf286853SEmmanuel Vadot #include <sys/sysctl.h> 47a94a63f0SWarner Losh #include <sys/endian.h> 48a94a63f0SWarner Losh #include <sys/taskqueue.h> 49a94a63f0SWarner Losh #include <sys/lock.h> 50a94a63f0SWarner Losh #include <sys/mutex.h> 51a94a63f0SWarner Losh #include <sys/conf.h> 52a94a63f0SWarner Losh #include <sys/devicestat.h> 53a94a63f0SWarner Losh #include <sys/eventhandler.h> 54a94a63f0SWarner Losh #include <sys/malloc.h> 55a94a63f0SWarner Losh #include <sys/cons.h> 56a94a63f0SWarner Losh #include <sys/proc.h> 57a94a63f0SWarner Losh #include <sys/reboot.h> 58a94a63f0SWarner Losh #include <geom/geom_disk.h> 59a94a63f0SWarner Losh #include <machine/_inttypes.h> /* for PRIu64 */ 60a94a63f0SWarner Losh #endif /* _KERNEL */ 61a94a63f0SWarner Losh 62a94a63f0SWarner Losh #ifndef _KERNEL 63a94a63f0SWarner Losh #include <stdio.h> 64a94a63f0SWarner Losh #include <string.h> 65a94a63f0SWarner Losh #endif /* _KERNEL */ 66a94a63f0SWarner Losh 67a94a63f0SWarner Losh #include <cam/cam.h> 68a94a63f0SWarner Losh #include <cam/cam_ccb.h> 69a94a63f0SWarner Losh #include <cam/cam_queue.h> 70a94a63f0SWarner Losh #include <cam/cam_periph.h> 71a94a63f0SWarner Losh #include <cam/cam_sim.h> 72a94a63f0SWarner Losh #include <cam/cam_xpt.h> 73a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 74a94a63f0SWarner Losh #include <cam/cam_xpt_periph.h> 75a94a63f0SWarner Losh #include <cam/cam_xpt_internal.h> 76a94a63f0SWarner Losh #include <cam/cam_debug.h> 77a94a63f0SWarner Losh 78a94a63f0SWarner Losh #include <cam/mmc/mmc_all.h> 79a94a63f0SWarner Losh 80a94a63f0SWarner Losh #ifdef _KERNEL 81a94a63f0SWarner Losh 82a94a63f0SWarner Losh typedef enum { 83a94a63f0SWarner Losh SDDA_FLAG_OPEN = 0x0002, 84a94a63f0SWarner Losh SDDA_FLAG_DIRTY = 0x0004 85a94a63f0SWarner Losh } sdda_flags; 86a94a63f0SWarner Losh 87a94a63f0SWarner Losh typedef enum { 88a94a63f0SWarner Losh SDDA_STATE_INIT, 89a94a63f0SWarner Losh SDDA_STATE_INVALID, 9096e47614SIlya Bakulin SDDA_STATE_NORMAL, 9196e47614SIlya Bakulin SDDA_STATE_PART_SWITCH, 92a94a63f0SWarner Losh } sdda_state; 93a94a63f0SWarner Losh 9496e47614SIlya Bakulin #define SDDA_FMT_BOOT "sdda%dboot" 9596e47614SIlya Bakulin #define SDDA_FMT_GP "sdda%dgp" 9696e47614SIlya Bakulin #define SDDA_FMT_RPMB "sdda%drpmb" 9796e47614SIlya Bakulin #define SDDA_LABEL_ENH "enh" 9896e47614SIlya Bakulin 9996e47614SIlya Bakulin #define SDDA_PART_NAMELEN (16 + 1) 10096e47614SIlya Bakulin 10196e47614SIlya Bakulin struct sdda_softc; 10296e47614SIlya Bakulin 10396e47614SIlya Bakulin struct sdda_part { 10496e47614SIlya Bakulin struct disk *disk; 105a94a63f0SWarner Losh struct bio_queue_head bio_queue; 10696e47614SIlya Bakulin sdda_flags flags; 10796e47614SIlya Bakulin struct sdda_softc *sc; 10896e47614SIlya Bakulin u_int cnt; 10996e47614SIlya Bakulin u_int type; 11096e47614SIlya Bakulin bool ro; 11196e47614SIlya Bakulin char name[SDDA_PART_NAMELEN]; 11296e47614SIlya Bakulin }; 11396e47614SIlya Bakulin 11496e47614SIlya Bakulin struct sdda_softc { 115a94a63f0SWarner Losh int outstanding_cmds; /* Number of active commands */ 116a94a63f0SWarner Losh int refcount; /* Active xpt_action() calls */ 117a94a63f0SWarner Losh sdda_state state; 118a94a63f0SWarner Losh struct mmc_data *mmcdata; 11996e47614SIlya Bakulin struct cam_periph *periph; 120a94a63f0SWarner Losh // sdda_quirks quirks; 121a94a63f0SWarner Losh struct task start_init_task; 122a94a63f0SWarner Losh uint32_t raw_csd[4]; 123a94a63f0SWarner Losh uint8_t raw_ext_csd[512]; /* MMC only? */ 124a94a63f0SWarner Losh struct mmc_csd csd; 125a94a63f0SWarner Losh struct mmc_cid cid; 126a94a63f0SWarner Losh struct mmc_scr scr; 127a94a63f0SWarner Losh /* Calculated from CSD */ 128a94a63f0SWarner Losh uint64_t sector_count; 129a94a63f0SWarner Losh uint64_t mediasize; 130a94a63f0SWarner Losh 131a94a63f0SWarner Losh /* Calculated from CID */ 132a94a63f0SWarner Losh char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */ 133a94a63f0SWarner Losh char card_sn_string[16];/* Formatted serial # for disk->d_ident */ 134a94a63f0SWarner Losh /* Determined from CSD + is highspeed card*/ 135a94a63f0SWarner Losh uint32_t card_f_max; 13696e47614SIlya Bakulin 13796e47614SIlya Bakulin /* Generic switch timeout */ 13896e47614SIlya Bakulin uint32_t cmd6_time; 139fd7371f7SEmmanuel Vadot uint32_t timings; /* Mask of bus timings supported */ 140fd7371f7SEmmanuel Vadot uint32_t vccq_120; /* Mask of bus timings at VCCQ of 1.2 V */ 141fd7371f7SEmmanuel Vadot uint32_t vccq_180; /* Mask of bus timings at VCCQ of 1.8 V */ 14296e47614SIlya Bakulin /* MMC partitions support */ 14396e47614SIlya Bakulin struct sdda_part *part[MMC_PART_MAX]; 14496e47614SIlya Bakulin uint8_t part_curr; /* Partition currently switched to */ 14596e47614SIlya Bakulin uint8_t part_requested; /* What partition we're currently switching to */ 14696e47614SIlya Bakulin uint32_t part_time; /* Partition switch timeout [us] */ 14796e47614SIlya Bakulin off_t enh_base; /* Enhanced user data area slice base ... */ 14896e47614SIlya Bakulin off_t enh_size; /* ... and size [bytes] */ 14996e47614SIlya Bakulin int log_count; 15096e47614SIlya Bakulin struct timeval log_time; 151a94a63f0SWarner Losh }; 152a94a63f0SWarner Losh 1531a22fb3fSIlya Bakulin static const char *mmc_errmsg[] = 1541a22fb3fSIlya Bakulin { 1551a22fb3fSIlya Bakulin "None", 1561a22fb3fSIlya Bakulin "Timeout", 1571a22fb3fSIlya Bakulin "Bad CRC", 1581a22fb3fSIlya Bakulin "Fifo", 1591a22fb3fSIlya Bakulin "Failed", 1601a22fb3fSIlya Bakulin "Invalid", 1611a22fb3fSIlya Bakulin "NO MEMORY" 1621a22fb3fSIlya Bakulin }; 1631a22fb3fSIlya Bakulin 164a94a63f0SWarner Losh #define ccb_bp ppriv_ptr1 165a94a63f0SWarner Losh 166a94a63f0SWarner Losh static disk_strategy_t sddastrategy; 167a94a63f0SWarner Losh static periph_init_t sddainit; 168a94a63f0SWarner Losh static void sddaasync(void *callback_arg, u_int32_t code, 169a94a63f0SWarner Losh struct cam_path *path, void *arg); 170a94a63f0SWarner Losh static periph_ctor_t sddaregister; 171a94a63f0SWarner Losh static periph_dtor_t sddacleanup; 172a94a63f0SWarner Losh static periph_start_t sddastart; 173a94a63f0SWarner Losh static periph_oninv_t sddaoninvalidate; 174a94a63f0SWarner Losh static void sddadone(struct cam_periph *periph, 175a94a63f0SWarner Losh union ccb *done_ccb); 176a94a63f0SWarner Losh static int sddaerror(union ccb *ccb, u_int32_t cam_flags, 177a94a63f0SWarner Losh u_int32_t sense_flags); 178a94a63f0SWarner Losh 1791a22fb3fSIlya Bakulin static int mmc_handle_reply(union ccb *ccb); 180a94a63f0SWarner Losh static uint16_t get_rca(struct cam_periph *periph); 181a94a63f0SWarner Losh static void sdda_start_init(void *context, union ccb *start_ccb); 182a94a63f0SWarner Losh static void sdda_start_init_task(void *context, int pending); 18396e47614SIlya Bakulin static void sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *start_ccb); 18496e47614SIlya Bakulin static uint32_t sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb); 185d670d951SIlya Bakulin static int mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca); 18696e47614SIlya Bakulin static inline uint32_t mmc_get_sector_size(struct cam_periph *periph) {return MMC_SECTOR_SIZE;} 18796e47614SIlya Bakulin 188bf286853SEmmanuel Vadot static SYSCTL_NODE(_kern_cam, OID_AUTO, sdda, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 189bf286853SEmmanuel Vadot "CAM Direct Access Disk driver"); 190bf286853SEmmanuel Vadot 191bf286853SEmmanuel Vadot static int sdda_mmcsd_compat = 1; 192bf286853SEmmanuel Vadot SYSCTL_INT(_kern_cam_sdda, OID_AUTO, mmcsd_compat, CTLFLAG_RDTUN, 193bf286853SEmmanuel Vadot &sdda_mmcsd_compat, 1, "Enable creation of mmcsd aliases."); 194bf286853SEmmanuel Vadot 19596e47614SIlya Bakulin /* TODO: actually issue GET_TRAN_SETTINGS to get R/O status */ 19696e47614SIlya Bakulin static inline bool sdda_get_read_only(struct cam_periph *periph, union ccb *start_ccb) 19796e47614SIlya Bakulin { 19896e47614SIlya Bakulin 19996e47614SIlya Bakulin return (false); 20096e47614SIlya Bakulin } 20196e47614SIlya Bakulin 20296e47614SIlya Bakulin static uint32_t mmc_get_spec_vers(struct cam_periph *periph); 20396e47614SIlya Bakulin static uint64_t mmc_get_media_size(struct cam_periph *periph); 20496e47614SIlya Bakulin static uint32_t mmc_get_cmd6_timeout(struct cam_periph *periph); 20596e47614SIlya Bakulin static void sdda_add_part(struct cam_periph *periph, u_int type, 20696e47614SIlya Bakulin const char *name, u_int cnt, off_t media_size, bool ro); 207a94a63f0SWarner Losh 208a94a63f0SWarner Losh static struct periph_driver sddadriver = 209a94a63f0SWarner Losh { 210a94a63f0SWarner Losh sddainit, "sdda", 211a94a63f0SWarner Losh TAILQ_HEAD_INITIALIZER(sddadriver.units), /* generation */ 0 212a94a63f0SWarner Losh }; 213a94a63f0SWarner Losh 214a94a63f0SWarner Losh PERIPHDRIVER_DECLARE(sdda, sddadriver); 215a94a63f0SWarner Losh 216a94a63f0SWarner Losh static MALLOC_DEFINE(M_SDDA, "sd_da", "sd_da buffers"); 217a94a63f0SWarner Losh 218a94a63f0SWarner Losh static const int exp[8] = { 219a94a63f0SWarner Losh 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 220a94a63f0SWarner Losh }; 221a94a63f0SWarner Losh 222a94a63f0SWarner Losh static const int mant[16] = { 223a94a63f0SWarner Losh 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 224a94a63f0SWarner Losh }; 225a94a63f0SWarner Losh 226a94a63f0SWarner Losh static const int cur_min[8] = { 227a94a63f0SWarner Losh 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 228a94a63f0SWarner Losh }; 229a94a63f0SWarner Losh 230a94a63f0SWarner Losh static const int cur_max[8] = { 231a94a63f0SWarner Losh 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 232a94a63f0SWarner Losh }; 233a94a63f0SWarner Losh 234a94a63f0SWarner Losh static uint16_t 235a94a63f0SWarner Losh get_rca(struct cam_periph *periph) { 236a94a63f0SWarner Losh return periph->path->device->mmc_ident_data.card_rca; 237a94a63f0SWarner Losh } 238a94a63f0SWarner Losh 2391a22fb3fSIlya Bakulin /* 2401a22fb3fSIlya Bakulin * Figure out if CCB execution resulted in error. 2411a22fb3fSIlya Bakulin * Look at both CAM-level errors and on MMC protocol errors. 242*b6b885c4SIlya Bakulin * 243*b6b885c4SIlya Bakulin * Return value is always MMC error. 2441a22fb3fSIlya Bakulin */ 2451a22fb3fSIlya Bakulin static int 2461a22fb3fSIlya Bakulin mmc_handle_reply(union ccb *ccb) 2471a22fb3fSIlya Bakulin { 2481a22fb3fSIlya Bakulin KASSERT(ccb->ccb_h.func_code == XPT_MMC_IO, 2491a22fb3fSIlya Bakulin ("ccb %p: cannot handle non-XPT_MMC_IO errors, got func_code=%d", 2501a22fb3fSIlya Bakulin ccb, ccb->ccb_h.func_code)); 2511a22fb3fSIlya Bakulin 252*b6b885c4SIlya Bakulin /* CAM-level error should always correspond to MMC-level error */ 253*b6b885c4SIlya Bakulin if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) && 254*b6b885c4SIlya Bakulin (ccb->mmcio.cmd.error != MMC_ERR_NONE)) 255*b6b885c4SIlya Bakulin panic("CCB status is OK but MMC error != MMC_ERR_NONE"); 256*b6b885c4SIlya Bakulin 257*b6b885c4SIlya 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 } 264*b6b885c4SIlya Bakulin return (ccb->mmcio.cmd.error); 2651a22fb3fSIlya Bakulin } 2661a22fb3fSIlya Bakulin 267a94a63f0SWarner Losh static uint32_t 268a94a63f0SWarner Losh mmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 269a94a63f0SWarner Losh { 270a94a63f0SWarner Losh const int i = (bit_len / 32) - (start / 32) - 1; 271a94a63f0SWarner Losh const int shift = start & 31; 272a94a63f0SWarner Losh uint32_t retval = bits[i] >> shift; 273a94a63f0SWarner Losh if (size + shift > 32) 274a94a63f0SWarner Losh retval |= bits[i - 1] << (32 - shift); 275a94a63f0SWarner Losh return (retval & ((1llu << size) - 1)); 276a94a63f0SWarner Losh } 277a94a63f0SWarner Losh 278a94a63f0SWarner Losh static void 279a94a63f0SWarner Losh mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 280a94a63f0SWarner Losh { 281a94a63f0SWarner Losh int v; 282a94a63f0SWarner Losh int m; 283a94a63f0SWarner Losh int e; 284a94a63f0SWarner Losh 285a94a63f0SWarner Losh memset(csd, 0, sizeof(*csd)); 286a94a63f0SWarner Losh csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 287a94a63f0SWarner Losh if (v == 0) { 288a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 115, 4); 289a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 112, 3); 290a94a63f0SWarner Losh csd->tacc = (exp[e] * mant[m] + 9) / 10; 291a94a63f0SWarner Losh csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 292a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 99, 4); 293a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 96, 3); 294a94a63f0SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 295a94a63f0SWarner Losh csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 296a94a63f0SWarner Losh csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 297a94a63f0SWarner Losh csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 298a94a63f0SWarner Losh csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 299a94a63f0SWarner Losh csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 300a94a63f0SWarner Losh csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 301a94a63f0SWarner Losh csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 302a94a63f0SWarner Losh csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 303a94a63f0SWarner Losh csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 304a94a63f0SWarner Losh csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 305a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 62, 12); 306a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 47, 3); 307a94a63f0SWarner Losh csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 308a94a63f0SWarner Losh csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 309a94a63f0SWarner Losh csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 310a94a63f0SWarner Losh csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 311a94a63f0SWarner Losh csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 312a94a63f0SWarner Losh csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 313a94a63f0SWarner Losh csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 314a94a63f0SWarner Losh csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 315a94a63f0SWarner Losh } else if (v == 1) { 316a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 115, 4); 317a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 112, 3); 318a94a63f0SWarner Losh csd->tacc = (exp[e] * mant[m] + 9) / 10; 319a94a63f0SWarner Losh csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 320a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 99, 4); 321a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 96, 3); 322a94a63f0SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 323a94a63f0SWarner Losh csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 324a94a63f0SWarner Losh csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 325a94a63f0SWarner Losh csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 326a94a63f0SWarner Losh csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 327a94a63f0SWarner Losh csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 328a94a63f0SWarner Losh csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 329a94a63f0SWarner Losh csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) * 330a94a63f0SWarner Losh 512 * 1024; 331a94a63f0SWarner Losh csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 332a94a63f0SWarner Losh csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 333a94a63f0SWarner Losh csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 334a94a63f0SWarner Losh csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 335a94a63f0SWarner Losh csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 336a94a63f0SWarner Losh csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 337a94a63f0SWarner Losh csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 338a94a63f0SWarner Losh } else 339a94a63f0SWarner Losh panic("unknown SD CSD version"); 340a94a63f0SWarner Losh } 341a94a63f0SWarner Losh 342a94a63f0SWarner Losh static void 343a94a63f0SWarner Losh mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 344a94a63f0SWarner Losh { 345a94a63f0SWarner Losh int m; 346a94a63f0SWarner Losh int e; 347a94a63f0SWarner Losh 348a94a63f0SWarner Losh memset(csd, 0, sizeof(*csd)); 349a94a63f0SWarner Losh csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 350a94a63f0SWarner Losh csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 351a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 115, 4); 352a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 112, 3); 353a94a63f0SWarner Losh csd->tacc = exp[e] * mant[m] + 9 / 10; 354a94a63f0SWarner Losh csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 355a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 99, 4); 356a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 96, 3); 357a94a63f0SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 358a94a63f0SWarner Losh csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 359a94a63f0SWarner Losh csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 360a94a63f0SWarner Losh csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 361a94a63f0SWarner Losh csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 362a94a63f0SWarner Losh csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 363a94a63f0SWarner Losh csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 364a94a63f0SWarner Losh csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 365a94a63f0SWarner Losh csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 366a94a63f0SWarner Losh csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 367a94a63f0SWarner Losh csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 368a94a63f0SWarner Losh m = mmc_get_bits(raw_csd, 128, 62, 12); 369a94a63f0SWarner Losh e = mmc_get_bits(raw_csd, 128, 47, 3); 370a94a63f0SWarner Losh csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 371a94a63f0SWarner Losh csd->erase_blk_en = 0; 372a94a63f0SWarner Losh csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 373a94a63f0SWarner Losh (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 374a94a63f0SWarner Losh csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 375a94a63f0SWarner Losh csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 376a94a63f0SWarner Losh csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 377a94a63f0SWarner Losh csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 378a94a63f0SWarner Losh csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 379a94a63f0SWarner Losh } 380a94a63f0SWarner Losh 381a94a63f0SWarner Losh static void 382a94a63f0SWarner Losh mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 383a94a63f0SWarner Losh { 384a94a63f0SWarner Losh int i; 385a94a63f0SWarner Losh 386a94a63f0SWarner Losh /* There's no version info, so we take it on faith */ 387a94a63f0SWarner Losh memset(cid, 0, sizeof(*cid)); 388a94a63f0SWarner Losh cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 389a94a63f0SWarner Losh cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 390a94a63f0SWarner Losh for (i = 0; i < 5; i++) 391a94a63f0SWarner Losh cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 392a94a63f0SWarner Losh cid->pnm[5] = 0; 393a94a63f0SWarner Losh cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 394a94a63f0SWarner Losh cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 395a94a63f0SWarner Losh cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 396a94a63f0SWarner Losh cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 397a94a63f0SWarner Losh } 398a94a63f0SWarner Losh 399a94a63f0SWarner Losh static void 400a94a63f0SWarner Losh mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid) 401a94a63f0SWarner Losh { 402a94a63f0SWarner Losh int i; 403a94a63f0SWarner Losh 404a94a63f0SWarner Losh /* There's no version info, so we take it on faith */ 405a94a63f0SWarner Losh memset(cid, 0, sizeof(*cid)); 406a94a63f0SWarner Losh cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 407a94a63f0SWarner Losh cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 408a94a63f0SWarner Losh for (i = 0; i < 6; i++) 409a94a63f0SWarner Losh cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 410a94a63f0SWarner Losh cid->pnm[6] = 0; 411a94a63f0SWarner Losh cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 412a94a63f0SWarner Losh cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 413a94a63f0SWarner Losh cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 414a94a63f0SWarner Losh cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997; 415a94a63f0SWarner Losh } 416a94a63f0SWarner Losh 417a94a63f0SWarner Losh static void 418a94a63f0SWarner Losh mmc_format_card_id_string(struct sdda_softc *sc, struct mmc_params *mmcp) 419a94a63f0SWarner Losh { 420a94a63f0SWarner Losh char oidstr[8]; 421a94a63f0SWarner Losh uint8_t c1; 422a94a63f0SWarner Losh uint8_t c2; 423a94a63f0SWarner Losh 424a94a63f0SWarner Losh /* 425a94a63f0SWarner Losh * Format a card ID string for use by the mmcsd driver, it's what 426a94a63f0SWarner Losh * appears between the <> in the following: 427a94a63f0SWarner Losh * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0 428a94a63f0SWarner Losh * 22.5MHz/4bit/128-block 429a94a63f0SWarner Losh * 430a94a63f0SWarner Losh * Also format just the card serial number, which the mmcsd driver will 431a94a63f0SWarner Losh * use as the disk->d_ident string. 432a94a63f0SWarner Losh * 433a94a63f0SWarner Losh * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 434a94a63f0SWarner Losh * and our max formatted length is currently 55 bytes if every field 435a94a63f0SWarner Losh * contains the largest value. 436a94a63f0SWarner Losh * 437a94a63f0SWarner Losh * Sometimes the oid is two printable ascii chars; when it's not, 438a94a63f0SWarner Losh * format it as 0xnnnn instead. 439a94a63f0SWarner Losh */ 440a94a63f0SWarner Losh c1 = (sc->cid.oid >> 8) & 0x0ff; 441a94a63f0SWarner Losh c2 = sc->cid.oid & 0x0ff; 442a94a63f0SWarner Losh if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 443a94a63f0SWarner Losh snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 444a94a63f0SWarner Losh else 445a94a63f0SWarner Losh snprintf(oidstr, sizeof(oidstr), "0x%04x", sc->cid.oid); 446a94a63f0SWarner Losh snprintf(sc->card_sn_string, sizeof(sc->card_sn_string), 447a94a63f0SWarner Losh "%08X", sc->cid.psn); 448a94a63f0SWarner Losh snprintf(sc->card_id_string, sizeof(sc->card_id_string), 449a94a63f0SWarner Losh "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s", 450a94a63f0SWarner Losh mmcp->card_features & CARD_FEATURE_MMC ? "MMC" : "SD", 451a94a63f0SWarner Losh mmcp->card_features & CARD_FEATURE_SDHC ? "HC" : "", 452a94a63f0SWarner Losh sc->cid.pnm, sc->cid.prv >> 4, sc->cid.prv & 0x0f, 453a94a63f0SWarner Losh sc->cid.psn, sc->cid.mdt_month, sc->cid.mdt_year, 454a94a63f0SWarner Losh sc->cid.mid, oidstr); 455a94a63f0SWarner Losh } 456a94a63f0SWarner Losh 457a94a63f0SWarner Losh static int 458a94a63f0SWarner Losh sddaopen(struct disk *dp) 459a94a63f0SWarner Losh { 46096e47614SIlya Bakulin struct sdda_part *part; 461a94a63f0SWarner Losh struct cam_periph *periph; 462a94a63f0SWarner Losh struct sdda_softc *softc; 463a94a63f0SWarner Losh int error; 464a94a63f0SWarner Losh 46596e47614SIlya Bakulin part = (struct sdda_part *)dp->d_drv1; 46696e47614SIlya Bakulin softc = part->sc; 46796e47614SIlya Bakulin periph = softc->periph; 46899e7a4adSScott Long if (cam_periph_acquire(periph) != 0) { 469a94a63f0SWarner Losh return(ENXIO); 470a94a63f0SWarner Losh } 471a94a63f0SWarner Losh 472a94a63f0SWarner Losh cam_periph_lock(periph); 473a94a63f0SWarner Losh if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 474a94a63f0SWarner Losh cam_periph_unlock(periph); 475a94a63f0SWarner Losh cam_periph_release(periph); 476a94a63f0SWarner Losh return (error); 477a94a63f0SWarner Losh } 478a94a63f0SWarner Losh 47902c474b4SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaopen\n")); 480a94a63f0SWarner Losh 48196e47614SIlya Bakulin part->flags |= SDDA_FLAG_OPEN; 482a94a63f0SWarner Losh 483a94a63f0SWarner Losh cam_periph_unhold(periph); 484a94a63f0SWarner Losh cam_periph_unlock(periph); 485a94a63f0SWarner Losh return (0); 486a94a63f0SWarner Losh } 487a94a63f0SWarner Losh 488a94a63f0SWarner Losh static int 489a94a63f0SWarner Losh sddaclose(struct disk *dp) 490a94a63f0SWarner Losh { 49196e47614SIlya Bakulin struct sdda_part *part; 492a94a63f0SWarner Losh struct cam_periph *periph; 493a94a63f0SWarner Losh struct sdda_softc *softc; 494a94a63f0SWarner Losh 49596e47614SIlya Bakulin part = (struct sdda_part *)dp->d_drv1; 49696e47614SIlya Bakulin softc = part->sc; 49796e47614SIlya Bakulin periph = softc->periph; 49896e47614SIlya Bakulin part->flags &= ~SDDA_FLAG_OPEN; 499a94a63f0SWarner Losh 500a94a63f0SWarner Losh cam_periph_lock(periph); 501a94a63f0SWarner Losh 50202c474b4SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaclose\n")); 503a94a63f0SWarner Losh 504a94a63f0SWarner Losh while (softc->refcount != 0) 505a94a63f0SWarner Losh cam_periph_sleep(periph, &softc->refcount, PRIBIO, "sddaclose", 1); 506a94a63f0SWarner Losh cam_periph_unlock(periph); 507a94a63f0SWarner Losh cam_periph_release(periph); 508a94a63f0SWarner Losh return (0); 509a94a63f0SWarner Losh } 510a94a63f0SWarner Losh 511a94a63f0SWarner Losh static void 512a94a63f0SWarner Losh sddaschedule(struct cam_periph *periph) 513a94a63f0SWarner Losh { 514a94a63f0SWarner Losh struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 51596e47614SIlya Bakulin struct sdda_part *part; 51696e47614SIlya Bakulin struct bio *bp; 51796e47614SIlya Bakulin int i; 518a94a63f0SWarner Losh 519a94a63f0SWarner Losh /* Check if we have more work to do. */ 52096e47614SIlya Bakulin /* Find partition that has outstanding commands. Prefer current partition. */ 52196e47614SIlya Bakulin bp = bioq_first(&softc->part[softc->part_curr]->bio_queue); 52296e47614SIlya Bakulin if (bp == NULL) { 52396e47614SIlya Bakulin for (i = 0; i < MMC_PART_MAX; i++) { 52496e47614SIlya Bakulin if ((part = softc->part[i]) != NULL && 52596e47614SIlya Bakulin (bp = bioq_first(&softc->part[i]->bio_queue)) != NULL) 52696e47614SIlya Bakulin break; 52796e47614SIlya Bakulin } 52896e47614SIlya Bakulin } 52996e47614SIlya Bakulin if (bp != NULL) { 530a94a63f0SWarner Losh xpt_schedule(periph, CAM_PRIORITY_NORMAL); 531a94a63f0SWarner Losh } 532a94a63f0SWarner Losh } 533a94a63f0SWarner Losh 534a94a63f0SWarner Losh /* 535a94a63f0SWarner Losh * Actually translate the requested transfer into one the physical driver 536a94a63f0SWarner Losh * can understand. The transfer is described by a buf and will include 537a94a63f0SWarner Losh * only one physical transfer. 538a94a63f0SWarner Losh */ 539a94a63f0SWarner Losh static void 540a94a63f0SWarner Losh sddastrategy(struct bio *bp) 541a94a63f0SWarner Losh { 542a94a63f0SWarner Losh struct cam_periph *periph; 54396e47614SIlya Bakulin struct sdda_part *part; 544a94a63f0SWarner Losh struct sdda_softc *softc; 545a94a63f0SWarner Losh 54696e47614SIlya Bakulin part = (struct sdda_part *)bp->bio_disk->d_drv1; 54796e47614SIlya Bakulin softc = part->sc; 54896e47614SIlya Bakulin periph = softc->periph; 549a94a63f0SWarner Losh 550a94a63f0SWarner Losh cam_periph_lock(periph); 551a94a63f0SWarner Losh 552a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastrategy(%p)\n", bp)); 553a94a63f0SWarner Losh 554a94a63f0SWarner Losh /* 555a94a63f0SWarner Losh * If the device has been made invalid, error out 556a94a63f0SWarner Losh */ 557a94a63f0SWarner Losh if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 558a94a63f0SWarner Losh cam_periph_unlock(periph); 559a94a63f0SWarner Losh biofinish(bp, NULL, ENXIO); 560a94a63f0SWarner Losh return; 561a94a63f0SWarner Losh } 562a94a63f0SWarner Losh 563a94a63f0SWarner Losh /* 564a94a63f0SWarner Losh * Place it in the queue of disk activities for this disk 565a94a63f0SWarner Losh */ 56696e47614SIlya Bakulin bioq_disksort(&part->bio_queue, bp); 567a94a63f0SWarner Losh 568a94a63f0SWarner Losh /* 569a94a63f0SWarner Losh * Schedule ourselves for performing the work. 570a94a63f0SWarner Losh */ 571a94a63f0SWarner Losh sddaschedule(periph); 572a94a63f0SWarner Losh cam_periph_unlock(periph); 573a94a63f0SWarner Losh 574a94a63f0SWarner Losh return; 575a94a63f0SWarner Losh } 576a94a63f0SWarner Losh 577a94a63f0SWarner Losh static void 578a94a63f0SWarner Losh sddainit(void) 579a94a63f0SWarner Losh { 580a94a63f0SWarner Losh cam_status status; 581a94a63f0SWarner Losh 582a94a63f0SWarner Losh /* 583a94a63f0SWarner Losh * Install a global async callback. This callback will 584a94a63f0SWarner Losh * receive async callbacks like "new device found". 585a94a63f0SWarner Losh */ 586a94a63f0SWarner Losh status = xpt_register_async(AC_FOUND_DEVICE, sddaasync, NULL, NULL); 587a94a63f0SWarner Losh 588a94a63f0SWarner Losh if (status != CAM_REQ_CMP) { 589a94a63f0SWarner Losh printf("sdda: Failed to attach master async callback " 590a94a63f0SWarner Losh "due to status 0x%x!\n", status); 591a94a63f0SWarner Losh } 592a94a63f0SWarner Losh } 593a94a63f0SWarner Losh 594a94a63f0SWarner Losh /* 595a94a63f0SWarner Losh * Callback from GEOM, called when it has finished cleaning up its 596a94a63f0SWarner Losh * resources. 597a94a63f0SWarner Losh */ 598a94a63f0SWarner Losh static void 599a94a63f0SWarner Losh sddadiskgonecb(struct disk *dp) 600a94a63f0SWarner Losh { 601a94a63f0SWarner Losh struct cam_periph *periph; 60296e47614SIlya Bakulin struct sdda_part *part; 603a94a63f0SWarner Losh 60496e47614SIlya Bakulin part = (struct sdda_part *)dp->d_drv1; 60596e47614SIlya Bakulin periph = part->sc->periph; 606a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddadiskgonecb\n")); 607a94a63f0SWarner Losh 608a94a63f0SWarner Losh cam_periph_release(periph); 609a94a63f0SWarner Losh } 610a94a63f0SWarner Losh 611a94a63f0SWarner Losh static void 612a94a63f0SWarner Losh sddaoninvalidate(struct cam_periph *periph) 613a94a63f0SWarner Losh { 614a94a63f0SWarner Losh struct sdda_softc *softc; 61596e47614SIlya Bakulin struct sdda_part *part; 616a94a63f0SWarner Losh 617a94a63f0SWarner Losh softc = (struct sdda_softc *)periph->softc; 618a94a63f0SWarner Losh 619a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaoninvalidate\n")); 620a94a63f0SWarner Losh 621a94a63f0SWarner Losh /* 622a94a63f0SWarner Losh * De-register any async callbacks. 623a94a63f0SWarner Losh */ 624a94a63f0SWarner Losh xpt_register_async(0, sddaasync, periph, periph->path); 625a94a63f0SWarner Losh 626a94a63f0SWarner Losh /* 627a94a63f0SWarner Losh * Return all queued I/O with ENXIO. 628a94a63f0SWarner Losh * XXX Handle any transactions queued to the card 629a94a63f0SWarner Losh * with XPT_ABORT_CCB. 630a94a63f0SWarner Losh */ 631a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush start\n")); 63296e47614SIlya Bakulin for (int i = 0; i < MMC_PART_MAX; i++) { 63396e47614SIlya Bakulin if ((part = softc->part[i]) != NULL) { 63496e47614SIlya Bakulin bioq_flush(&part->bio_queue, NULL, ENXIO); 63596e47614SIlya Bakulin disk_gone(part->disk); 63696e47614SIlya Bakulin } 63796e47614SIlya Bakulin } 638a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush end\n")); 639a94a63f0SWarner Losh 640a94a63f0SWarner Losh } 641a94a63f0SWarner Losh 642a94a63f0SWarner Losh static void 643a94a63f0SWarner Losh sddacleanup(struct cam_periph *periph) 644a94a63f0SWarner Losh { 645a94a63f0SWarner Losh struct sdda_softc *softc; 64696e47614SIlya Bakulin struct sdda_part *part; 64796e47614SIlya Bakulin int i; 648a94a63f0SWarner Losh 649a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddacleanup\n")); 650a94a63f0SWarner Losh softc = (struct sdda_softc *)periph->softc; 651a94a63f0SWarner Losh 652a94a63f0SWarner Losh cam_periph_unlock(periph); 653a94a63f0SWarner Losh 65496e47614SIlya Bakulin for (i = 0; i < MMC_PART_MAX; i++) { 65596e47614SIlya Bakulin if ((part = softc->part[i]) != NULL) { 65696e47614SIlya Bakulin disk_destroy(part->disk); 65796e47614SIlya Bakulin free(part, M_DEVBUF); 65896e47614SIlya Bakulin softc->part[i] = NULL; 65996e47614SIlya Bakulin } 66096e47614SIlya Bakulin } 661a94a63f0SWarner Losh free(softc, M_DEVBUF); 662a94a63f0SWarner Losh cam_periph_lock(periph); 663a94a63f0SWarner Losh } 664a94a63f0SWarner Losh 665a94a63f0SWarner Losh static void 666a94a63f0SWarner Losh sddaasync(void *callback_arg, u_int32_t code, 667a94a63f0SWarner Losh struct cam_path *path, void *arg) 668a94a63f0SWarner Losh { 669a94a63f0SWarner Losh struct ccb_getdev cgd; 670a94a63f0SWarner Losh struct cam_periph *periph; 671a94a63f0SWarner Losh struct sdda_softc *softc; 672a94a63f0SWarner Losh 673a94a63f0SWarner Losh periph = (struct cam_periph *)callback_arg; 674a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddaasync(code=%d)\n", code)); 675a94a63f0SWarner Losh switch (code) { 676a94a63f0SWarner Losh case AC_FOUND_DEVICE: 677a94a63f0SWarner Losh { 678a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_FOUND_DEVICE\n")); 679a94a63f0SWarner Losh struct ccb_getdev *cgd; 680a94a63f0SWarner Losh cam_status status; 681a94a63f0SWarner Losh 682a94a63f0SWarner Losh cgd = (struct ccb_getdev *)arg; 683a94a63f0SWarner Losh if (cgd == NULL) 684a94a63f0SWarner Losh break; 685a94a63f0SWarner Losh 686a94a63f0SWarner Losh if (cgd->protocol != PROTO_MMCSD) 687a94a63f0SWarner Losh break; 688a94a63f0SWarner Losh 689a94a63f0SWarner Losh if (!(path->device->mmc_ident_data.card_features & CARD_FEATURE_MEMORY)) { 690a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("No memory on the card!\n")); 691a94a63f0SWarner Losh break; 692a94a63f0SWarner Losh } 693a94a63f0SWarner Losh 694a94a63f0SWarner Losh /* 695a94a63f0SWarner Losh * Allocate a peripheral instance for 696a94a63f0SWarner Losh * this device and start the probe 697a94a63f0SWarner Losh * process. 698a94a63f0SWarner Losh */ 699a94a63f0SWarner Losh status = cam_periph_alloc(sddaregister, sddaoninvalidate, 700a94a63f0SWarner Losh sddacleanup, sddastart, 701a94a63f0SWarner Losh "sdda", CAM_PERIPH_BIO, 702a94a63f0SWarner Losh path, sddaasync, 703a94a63f0SWarner Losh AC_FOUND_DEVICE, cgd); 704a94a63f0SWarner Losh 705a94a63f0SWarner Losh if (status != CAM_REQ_CMP 706a94a63f0SWarner Losh && status != CAM_REQ_INPROG) 707a94a63f0SWarner Losh printf("sddaasync: Unable to attach to new device " 708a94a63f0SWarner Losh "due to status 0x%x\n", status); 709a94a63f0SWarner Losh break; 710a94a63f0SWarner Losh } 711a94a63f0SWarner Losh case AC_GETDEV_CHANGED: 712a94a63f0SWarner Losh { 713a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_GETDEV_CHANGED\n")); 714a94a63f0SWarner Losh softc = (struct sdda_softc *)periph->softc; 715a94a63f0SWarner Losh xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 716a94a63f0SWarner Losh cgd.ccb_h.func_code = XPT_GDEV_TYPE; 717a94a63f0SWarner Losh xpt_action((union ccb *)&cgd); 718a94a63f0SWarner Losh cam_periph_async(periph, code, path, arg); 719a94a63f0SWarner Losh break; 720a94a63f0SWarner Losh } 721a94a63f0SWarner Losh case AC_ADVINFO_CHANGED: 722a94a63f0SWarner Losh { 723a94a63f0SWarner Losh uintptr_t buftype; 72496e47614SIlya Bakulin int i; 72596e47614SIlya Bakulin 726a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_ADVINFO_CHANGED\n")); 727a94a63f0SWarner Losh buftype = (uintptr_t)arg; 728a94a63f0SWarner Losh if (buftype == CDAI_TYPE_PHYS_PATH) { 729a94a63f0SWarner Losh struct sdda_softc *softc; 73096e47614SIlya Bakulin struct sdda_part *part; 731a94a63f0SWarner Losh 732a94a63f0SWarner Losh softc = periph->softc; 73396e47614SIlya Bakulin for (i = 0; i < MMC_PART_MAX; i++) { 73496e47614SIlya Bakulin if ((part = softc->part[i]) != NULL) { 73596e47614SIlya Bakulin disk_attr_changed(part->disk, "GEOM::physpath", 736a94a63f0SWarner Losh M_NOWAIT); 737a94a63f0SWarner Losh } 738a94a63f0SWarner Losh } 73996e47614SIlya Bakulin } 74096e47614SIlya Bakulin break; 741a94a63f0SWarner Losh } 742a94a63f0SWarner Losh default: 743a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> default?!\n")); 744a94a63f0SWarner Losh cam_periph_async(periph, code, path, arg); 745a94a63f0SWarner Losh break; 746a94a63f0SWarner Losh } 747a94a63f0SWarner Losh } 748a94a63f0SWarner Losh 749a94a63f0SWarner Losh static int 750a94a63f0SWarner Losh sddagetattr(struct bio *bp) 751a94a63f0SWarner Losh { 752a94a63f0SWarner Losh struct cam_periph *periph; 75396e47614SIlya Bakulin struct sdda_softc *softc; 75496e47614SIlya Bakulin struct sdda_part *part; 75596e47614SIlya Bakulin int ret; 756a94a63f0SWarner Losh 75796e47614SIlya Bakulin part = (struct sdda_part *)bp->bio_disk->d_drv1; 75896e47614SIlya Bakulin softc = part->sc; 75996e47614SIlya Bakulin periph = softc->periph; 760a94a63f0SWarner Losh cam_periph_lock(periph); 761a94a63f0SWarner Losh ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 762a94a63f0SWarner Losh periph->path); 763a94a63f0SWarner Losh cam_periph_unlock(periph); 764a94a63f0SWarner Losh if (ret == 0) 765a94a63f0SWarner Losh bp->bio_completed = bp->bio_length; 76696e47614SIlya Bakulin return (ret); 767a94a63f0SWarner Losh } 768a94a63f0SWarner Losh 769a94a63f0SWarner Losh static cam_status 770a94a63f0SWarner Losh sddaregister(struct cam_periph *periph, void *arg) 771a94a63f0SWarner Losh { 772a94a63f0SWarner Losh struct sdda_softc *softc; 773a94a63f0SWarner Losh struct ccb_getdev *cgd; 774a94a63f0SWarner Losh union ccb *request_ccb; /* CCB representing the probe request */ 775a94a63f0SWarner Losh 776a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaregister\n")); 777a94a63f0SWarner Losh cgd = (struct ccb_getdev *)arg; 778a94a63f0SWarner Losh if (cgd == NULL) { 779a94a63f0SWarner Losh printf("sddaregister: no getdev CCB, can't register device\n"); 780a94a63f0SWarner Losh return (CAM_REQ_CMP_ERR); 781a94a63f0SWarner Losh } 782a94a63f0SWarner Losh 783a94a63f0SWarner Losh softc = (struct sdda_softc *)malloc(sizeof(*softc), M_DEVBUF, 784a94a63f0SWarner Losh M_NOWAIT|M_ZERO); 785a94a63f0SWarner Losh if (softc == NULL) { 786a94a63f0SWarner Losh printf("sddaregister: Unable to probe new device. " 787a94a63f0SWarner Losh "Unable to allocate softc\n"); 788a94a63f0SWarner Losh return (CAM_REQ_CMP_ERR); 789a94a63f0SWarner Losh } 790a94a63f0SWarner Losh 791a94a63f0SWarner Losh softc->state = SDDA_STATE_INIT; 792a94a63f0SWarner Losh softc->mmcdata = 793a94a63f0SWarner Losh (struct mmc_data *)malloc(sizeof(struct mmc_data), M_DEVBUF, M_NOWAIT|M_ZERO); 7940660cfa0SIlya Bakulin if (softc->mmcdata == NULL) { 7950660cfa0SIlya Bakulin printf("sddaregister: Unable to probe new device. " 7960660cfa0SIlya Bakulin "Unable to allocate mmcdata\n"); 7971e5d7335SBjoern A. Zeeb free(softc, M_DEVBUF); 7980660cfa0SIlya Bakulin return (CAM_REQ_CMP_ERR); 7990660cfa0SIlya Bakulin } 800a94a63f0SWarner Losh periph->softc = softc; 80196e47614SIlya Bakulin softc->periph = periph; 802a94a63f0SWarner Losh 803a94a63f0SWarner Losh request_ccb = (union ccb*) arg; 804a94a63f0SWarner Losh xpt_schedule(periph, CAM_PRIORITY_XPT); 805a94a63f0SWarner Losh TASK_INIT(&softc->start_init_task, 0, sdda_start_init_task, periph); 806a94a63f0SWarner Losh taskqueue_enqueue(taskqueue_thread, &softc->start_init_task); 807a94a63f0SWarner Losh 808a94a63f0SWarner Losh return (CAM_REQ_CMP); 809a94a63f0SWarner Losh } 810a94a63f0SWarner Losh 811a94a63f0SWarner Losh static int 812a94a63f0SWarner Losh mmc_exec_app_cmd(struct cam_periph *periph, union ccb *ccb, 813a94a63f0SWarner Losh struct mmc_command *cmd) { 814a94a63f0SWarner Losh int err; 815a94a63f0SWarner Losh 816a94a63f0SWarner Losh /* Send APP_CMD first */ 817a94a63f0SWarner Losh memset(&ccb->mmcio.cmd, 0, sizeof(struct mmc_command)); 818a94a63f0SWarner Losh memset(&ccb->mmcio.stop, 0, sizeof(struct mmc_command)); 819a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio, 820a94a63f0SWarner Losh /*retries*/ 0, 821a94a63f0SWarner Losh /*cbfcnp*/ NULL, 822a94a63f0SWarner Losh /*flags*/ CAM_DIR_NONE, 823a94a63f0SWarner Losh /*mmc_opcode*/ MMC_APP_CMD, 824a94a63f0SWarner Losh /*mmc_arg*/ get_rca(periph) << 16, 825a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_AC, 826a94a63f0SWarner Losh /*mmc_data*/ NULL, 827a94a63f0SWarner Losh /*timeout*/ 0); 828a94a63f0SWarner Losh 8291a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 8301a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 831a94a63f0SWarner Losh if (err != 0) 8321a22fb3fSIlya Bakulin return (err); 833a94a63f0SWarner Losh if (!(ccb->mmcio.cmd.resp[0] & R1_APP_CMD)) 8341a22fb3fSIlya Bakulin return (EIO); 835a94a63f0SWarner Losh 836a94a63f0SWarner Losh /* Now exec actual command */ 837a94a63f0SWarner Losh int flags = 0; 838a94a63f0SWarner Losh if (cmd->data != NULL) { 839a94a63f0SWarner Losh ccb->mmcio.cmd.data = cmd->data; 840a94a63f0SWarner Losh if (cmd->data->flags & MMC_DATA_READ) 841a94a63f0SWarner Losh flags |= CAM_DIR_IN; 842a94a63f0SWarner Losh if (cmd->data->flags & MMC_DATA_WRITE) 843a94a63f0SWarner Losh flags |= CAM_DIR_OUT; 844a94a63f0SWarner Losh } else flags = CAM_DIR_NONE; 845a94a63f0SWarner Losh 846a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio, 847a94a63f0SWarner Losh /*retries*/ 0, 848a94a63f0SWarner Losh /*cbfcnp*/ NULL, 849a94a63f0SWarner Losh /*flags*/ flags, 850a94a63f0SWarner Losh /*mmc_opcode*/ cmd->opcode, 851a94a63f0SWarner Losh /*mmc_arg*/ cmd->arg, 852a94a63f0SWarner Losh /*mmc_flags*/ cmd->flags, 853a94a63f0SWarner Losh /*mmc_data*/ cmd->data, 854a94a63f0SWarner Losh /*timeout*/ 0); 855a94a63f0SWarner Losh 8561a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 8571a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 8581a22fb3fSIlya Bakulin if (err != 0) 8591a22fb3fSIlya Bakulin return (err); 860a94a63f0SWarner Losh memcpy(cmd->resp, ccb->mmcio.cmd.resp, sizeof(cmd->resp)); 861a94a63f0SWarner Losh cmd->error = ccb->mmcio.cmd.error; 8621a22fb3fSIlya Bakulin 8631a22fb3fSIlya Bakulin return (0); 864a94a63f0SWarner Losh } 865a94a63f0SWarner Losh 866a94a63f0SWarner Losh static int 867a94a63f0SWarner Losh mmc_app_get_scr(struct cam_periph *periph, union ccb *ccb, uint32_t *rawscr) { 868a94a63f0SWarner Losh int err; 869a94a63f0SWarner Losh struct mmc_command cmd; 870a94a63f0SWarner Losh struct mmc_data d; 871a94a63f0SWarner Losh 872a94a63f0SWarner Losh memset(&cmd, 0, sizeof(cmd)); 8734c4200c6SIlya Bakulin memset(&d, 0, sizeof(d)); 874a94a63f0SWarner Losh 875a94a63f0SWarner Losh memset(rawscr, 0, 8); 876a94a63f0SWarner Losh cmd.opcode = ACMD_SEND_SCR; 877a94a63f0SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 878a94a63f0SWarner Losh cmd.arg = 0; 879a94a63f0SWarner Losh 880a94a63f0SWarner Losh d.data = rawscr; 881a94a63f0SWarner Losh d.len = 8; 882a94a63f0SWarner Losh d.flags = MMC_DATA_READ; 883a94a63f0SWarner Losh cmd.data = &d; 884a94a63f0SWarner Losh 885a94a63f0SWarner Losh err = mmc_exec_app_cmd(periph, ccb, &cmd); 886a94a63f0SWarner Losh rawscr[0] = be32toh(rawscr[0]); 887a94a63f0SWarner Losh rawscr[1] = be32toh(rawscr[1]); 888a94a63f0SWarner Losh return (err); 889a94a63f0SWarner Losh } 890a94a63f0SWarner Losh 891a94a63f0SWarner Losh static int 892a94a63f0SWarner Losh mmc_send_ext_csd(struct cam_periph *periph, union ccb *ccb, 893a94a63f0SWarner Losh uint8_t *rawextcsd, size_t buf_len) { 894a94a63f0SWarner Losh int err; 895a94a63f0SWarner Losh struct mmc_data d; 896a94a63f0SWarner Losh 897a94a63f0SWarner Losh KASSERT(buf_len == 512, ("Buffer for ext csd must be 512 bytes")); 8980660cfa0SIlya Bakulin memset(&d, 0, sizeof(d)); 899a94a63f0SWarner Losh d.data = rawextcsd; 900a94a63f0SWarner Losh d.len = buf_len; 901a94a63f0SWarner Losh d.flags = MMC_DATA_READ; 902a94a63f0SWarner Losh memset(d.data, 0, d.len); 903a94a63f0SWarner Losh 904a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio, 905a94a63f0SWarner Losh /*retries*/ 0, 906a94a63f0SWarner Losh /*cbfcnp*/ NULL, 907a94a63f0SWarner Losh /*flags*/ CAM_DIR_IN, 908a94a63f0SWarner Losh /*mmc_opcode*/ MMC_SEND_EXT_CSD, 909a94a63f0SWarner Losh /*mmc_arg*/ 0, 910a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC, 911a94a63f0SWarner Losh /*mmc_data*/ &d, 912a94a63f0SWarner Losh /*timeout*/ 0); 913a94a63f0SWarner Losh 9141a22fb3fSIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 9151a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 91696e47614SIlya Bakulin return (err); 917a94a63f0SWarner Losh } 918a94a63f0SWarner Losh 919a94a63f0SWarner Losh static void 920a94a63f0SWarner Losh mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 921a94a63f0SWarner Losh { 922a94a63f0SWarner Losh unsigned int scr_struct; 923a94a63f0SWarner Losh 924a94a63f0SWarner Losh memset(scr, 0, sizeof(*scr)); 925a94a63f0SWarner Losh 926a94a63f0SWarner Losh scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 927a94a63f0SWarner Losh if (scr_struct != 0) { 928a94a63f0SWarner Losh printf("Unrecognised SCR structure version %d\n", 929a94a63f0SWarner Losh scr_struct); 930a94a63f0SWarner Losh return; 931a94a63f0SWarner Losh } 932a94a63f0SWarner Losh scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 933a94a63f0SWarner Losh scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 934a94a63f0SWarner Losh } 935a94a63f0SWarner Losh 93696e47614SIlya Bakulin static inline void 93796e47614SIlya Bakulin mmc_switch_fill_mmcio(union ccb *ccb, 93896e47614SIlya Bakulin uint8_t set, uint8_t index, uint8_t value, u_int timeout) 939a94a63f0SWarner Losh { 940a94a63f0SWarner Losh int arg = (MMC_SWITCH_FUNC_WR << 24) | 941a94a63f0SWarner Losh (index << 16) | 942a94a63f0SWarner Losh (value << 8) | 943a94a63f0SWarner Losh set; 94496e47614SIlya Bakulin 945a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio, 946a94a63f0SWarner Losh /*retries*/ 0, 947a94a63f0SWarner Losh /*cbfcnp*/ NULL, 948a94a63f0SWarner Losh /*flags*/ CAM_DIR_NONE, 949a94a63f0SWarner Losh /*mmc_opcode*/ MMC_SWITCH_FUNC, 950a94a63f0SWarner Losh /*mmc_arg*/ arg, 951a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1B | MMC_CMD_AC, 952a94a63f0SWarner Losh /*mmc_data*/ NULL, 95396e47614SIlya Bakulin /*timeout*/ timeout); 95496e47614SIlya Bakulin } 955a94a63f0SWarner Losh 95696e47614SIlya Bakulin static int 957d670d951SIlya Bakulin mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca) 958d670d951SIlya Bakulin { 9591a22fb3fSIlya Bakulin int flags, err; 960d670d951SIlya Bakulin 961d670d951SIlya Bakulin flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 962d670d951SIlya Bakulin cam_fill_mmcio(&ccb->mmcio, 963d670d951SIlya Bakulin /*retries*/ 0, 964d670d951SIlya Bakulin /*cbfcnp*/ NULL, 965d670d951SIlya Bakulin /*flags*/ CAM_DIR_IN, 966d670d951SIlya Bakulin /*mmc_opcode*/ MMC_SELECT_CARD, 967d670d951SIlya Bakulin /*mmc_arg*/ rca << 16, 968d670d951SIlya Bakulin /*mmc_flags*/ flags, 969d670d951SIlya Bakulin /*mmc_data*/ NULL, 970d670d951SIlya Bakulin /*timeout*/ 0); 971d670d951SIlya Bakulin 972d670d951SIlya Bakulin cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 9731a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 9741a22fb3fSIlya Bakulin return (err); 975d670d951SIlya Bakulin } 976d670d951SIlya Bakulin 977d670d951SIlya Bakulin static int 97896e47614SIlya Bakulin mmc_switch(struct cam_periph *periph, union ccb *ccb, 97996e47614SIlya Bakulin uint8_t set, uint8_t index, uint8_t value, u_int timeout) 98096e47614SIlya Bakulin { 9811a22fb3fSIlya Bakulin int err; 98296e47614SIlya Bakulin 98396e47614SIlya Bakulin mmc_switch_fill_mmcio(ccb, set, index, value, timeout); 984a94a63f0SWarner Losh cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 9851a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 9861a22fb3fSIlya Bakulin return (err); 987a94a63f0SWarner Losh } 988a94a63f0SWarner Losh 98996e47614SIlya Bakulin static uint32_t 99096e47614SIlya Bakulin mmc_get_spec_vers(struct cam_periph *periph) { 99196e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 99296e47614SIlya Bakulin 99396e47614SIlya Bakulin return (softc->csd.spec_vers); 99496e47614SIlya Bakulin } 99596e47614SIlya Bakulin 99696e47614SIlya Bakulin static uint64_t 99796e47614SIlya Bakulin mmc_get_media_size(struct cam_periph *periph) { 99896e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 99996e47614SIlya Bakulin 100096e47614SIlya Bakulin return (softc->mediasize); 100196e47614SIlya Bakulin } 100296e47614SIlya Bakulin 100396e47614SIlya Bakulin static uint32_t 100496e47614SIlya Bakulin mmc_get_cmd6_timeout(struct cam_periph *periph) 100596e47614SIlya Bakulin { 100696e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 100796e47614SIlya Bakulin 100896e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 6) 100996e47614SIlya Bakulin return (softc->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME] * 10); 101096e47614SIlya Bakulin return (500 * 1000); 101196e47614SIlya Bakulin } 101296e47614SIlya Bakulin 1013a94a63f0SWarner Losh static int 1014a94a63f0SWarner Losh mmc_sd_switch(struct cam_periph *periph, union ccb *ccb, 1015a94a63f0SWarner Losh uint8_t mode, uint8_t grp, uint8_t value, 1016a94a63f0SWarner Losh uint8_t *res) { 1017a94a63f0SWarner Losh struct mmc_data mmc_d; 1018d670d951SIlya Bakulin uint32_t arg; 10191a22fb3fSIlya Bakulin int err; 1020a94a63f0SWarner Losh 1021a94a63f0SWarner Losh memset(res, 0, 64); 10220660cfa0SIlya Bakulin memset(&mmc_d, 0, sizeof(mmc_d)); 1023a94a63f0SWarner Losh mmc_d.len = 64; 1024a94a63f0SWarner Losh mmc_d.data = res; 1025a94a63f0SWarner Losh mmc_d.flags = MMC_DATA_READ; 1026a94a63f0SWarner Losh 1027d670d951SIlya Bakulin arg = mode << 31; /* 0 - check, 1 - set */ 1028d670d951SIlya Bakulin arg |= 0x00FFFFFF; 1029d670d951SIlya Bakulin arg &= ~(0xF << (grp * 4)); 1030d670d951SIlya Bakulin arg |= value << (grp * 4); 1031d670d951SIlya Bakulin 1032a94a63f0SWarner Losh cam_fill_mmcio(&ccb->mmcio, 1033a94a63f0SWarner Losh /*retries*/ 0, 1034a94a63f0SWarner Losh /*cbfcnp*/ NULL, 1035a94a63f0SWarner Losh /*flags*/ CAM_DIR_IN, 1036a94a63f0SWarner Losh /*mmc_opcode*/ SD_SWITCH_FUNC, 1037d670d951SIlya Bakulin /*mmc_arg*/ arg, 1038a94a63f0SWarner Losh /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC, 1039a94a63f0SWarner Losh /*mmc_data*/ &mmc_d, 1040a94a63f0SWarner Losh /*timeout*/ 0); 1041a94a63f0SWarner Losh 1042a94a63f0SWarner Losh cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL); 10431a22fb3fSIlya Bakulin err = mmc_handle_reply(ccb); 10441a22fb3fSIlya Bakulin return (err); 1045a94a63f0SWarner Losh } 1046a94a63f0SWarner Losh 1047a94a63f0SWarner Losh static int 1048a94a63f0SWarner Losh mmc_set_timing(struct cam_periph *periph, 1049a94a63f0SWarner Losh union ccb *ccb, 1050a94a63f0SWarner Losh enum mmc_bus_timing timing) 1051a94a63f0SWarner Losh { 1052a94a63f0SWarner Losh u_char switch_res[64]; 1053a94a63f0SWarner Losh int err; 1054a94a63f0SWarner Losh uint8_t value; 105596e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 1056a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data; 1057a94a63f0SWarner Losh 1058a94a63f0SWarner Losh CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, 1059a94a63f0SWarner Losh ("mmc_set_timing(timing=%d)", timing)); 1060a94a63f0SWarner Losh switch (timing) { 1061a94a63f0SWarner Losh case bus_timing_normal: 1062a94a63f0SWarner Losh value = 0; 1063a94a63f0SWarner Losh break; 1064a94a63f0SWarner Losh case bus_timing_hs: 1065a94a63f0SWarner Losh value = 1; 1066a94a63f0SWarner Losh break; 1067a94a63f0SWarner Losh default: 1068a94a63f0SWarner Losh return (MMC_ERR_INVALID); 1069a94a63f0SWarner Losh } 1070a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) { 1071a94a63f0SWarner Losh err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL, 107296e47614SIlya Bakulin EXT_CSD_HS_TIMING, value, softc->cmd6_time); 1073a94a63f0SWarner Losh } else { 1074a94a63f0SWarner Losh err = mmc_sd_switch(periph, ccb, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, value, switch_res); 1075a94a63f0SWarner Losh } 1076a94a63f0SWarner Losh 1077a94a63f0SWarner Losh /* Set high-speed timing on the host */ 1078a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts; 1079a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 1080a94a63f0SWarner Losh ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1081a94a63f0SWarner Losh ccb->ccb_h.flags = CAM_DIR_NONE; 1082a94a63f0SWarner Losh ccb->ccb_h.retry_count = 0; 1083a94a63f0SWarner Losh ccb->ccb_h.timeout = 100; 1084a94a63f0SWarner Losh ccb->ccb_h.cbfcnp = NULL; 1085a94a63f0SWarner Losh cts->ios.timing = timing; 1086a94a63f0SWarner Losh cts->ios_valid = MMC_BT; 1087a94a63f0SWarner Losh xpt_action(ccb); 1088a94a63f0SWarner Losh 1089a94a63f0SWarner Losh return (err); 1090a94a63f0SWarner Losh } 1091a94a63f0SWarner Losh 1092a94a63f0SWarner Losh static void 1093a94a63f0SWarner Losh sdda_start_init_task(void *context, int pending) { 1094a94a63f0SWarner Losh union ccb *new_ccb; 1095a94a63f0SWarner Losh struct cam_periph *periph; 1096a94a63f0SWarner Losh 1097a94a63f0SWarner Losh periph = (struct cam_periph *)context; 1098a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init_task\n")); 1099a94a63f0SWarner Losh new_ccb = xpt_alloc_ccb(); 1100a94a63f0SWarner Losh xpt_setup_ccb(&new_ccb->ccb_h, periph->path, 1101a94a63f0SWarner Losh CAM_PRIORITY_NONE); 1102a94a63f0SWarner Losh 1103a94a63f0SWarner Losh cam_periph_lock(periph); 1104f2df51ecSEmmanuel Vadot cam_periph_hold(periph, PRIBIO|PCATCH); 1105a94a63f0SWarner Losh sdda_start_init(context, new_ccb); 1106f2df51ecSEmmanuel Vadot cam_periph_unhold(periph); 1107a94a63f0SWarner Losh cam_periph_unlock(periph); 1108a94a63f0SWarner Losh xpt_free_ccb(new_ccb); 1109a94a63f0SWarner Losh } 1110a94a63f0SWarner Losh 1111a94a63f0SWarner Losh static void 1112a94a63f0SWarner Losh sdda_set_bus_width(struct cam_periph *periph, union ccb *ccb, int width) { 111396e47614SIlya Bakulin struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 1114a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data; 1115a94a63f0SWarner Losh int err; 1116a94a63f0SWarner Losh 1117a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_set_bus_width\n")); 1118a94a63f0SWarner Losh 1119a94a63f0SWarner Losh /* First set for the card, then for the host */ 1120a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) { 1121a94a63f0SWarner Losh uint8_t value; 1122a94a63f0SWarner Losh switch (width) { 1123a94a63f0SWarner Losh case bus_width_1: 1124a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_1; 1125a94a63f0SWarner Losh break; 1126a94a63f0SWarner Losh case bus_width_4: 1127a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_4; 1128a94a63f0SWarner Losh break; 1129a94a63f0SWarner Losh case bus_width_8: 1130a94a63f0SWarner Losh value = EXT_CSD_BUS_WIDTH_8; 1131a94a63f0SWarner Losh break; 1132a94a63f0SWarner Losh default: 1133a94a63f0SWarner Losh panic("Invalid bus width %d", width); 1134a94a63f0SWarner Losh } 1135a94a63f0SWarner Losh err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL, 113696e47614SIlya Bakulin EXT_CSD_BUS_WIDTH, value, softc->cmd6_time); 1137a94a63f0SWarner Losh } else { 1138a94a63f0SWarner Losh /* For SD cards we send ACMD6 with the required bus width in arg */ 1139a94a63f0SWarner Losh struct mmc_command cmd; 1140a94a63f0SWarner Losh memset(&cmd, 0, sizeof(struct mmc_command)); 1141a94a63f0SWarner Losh cmd.opcode = ACMD_SET_BUS_WIDTH; 1142a94a63f0SWarner Losh cmd.arg = width; 1143a94a63f0SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1144a94a63f0SWarner Losh err = mmc_exec_app_cmd(periph, ccb, &cmd); 1145a94a63f0SWarner Losh } 1146a94a63f0SWarner Losh 1147a94a63f0SWarner Losh if (err != MMC_ERR_NONE) { 1148a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Error %d when setting bus width on the card\n", err)); 1149a94a63f0SWarner Losh return; 1150a94a63f0SWarner Losh } 1151a94a63f0SWarner Losh /* Now card is done, set the host to the same width */ 1152a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts; 1153a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 1154a94a63f0SWarner Losh ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1155a94a63f0SWarner Losh ccb->ccb_h.flags = CAM_DIR_NONE; 1156a94a63f0SWarner Losh ccb->ccb_h.retry_count = 0; 1157a94a63f0SWarner Losh ccb->ccb_h.timeout = 100; 1158a94a63f0SWarner Losh ccb->ccb_h.cbfcnp = NULL; 1159a94a63f0SWarner Losh cts->ios.bus_width = width; 1160a94a63f0SWarner Losh cts->ios_valid = MMC_BW; 1161a94a63f0SWarner Losh xpt_action(ccb); 1162a94a63f0SWarner Losh } 1163a94a63f0SWarner Losh 116496e47614SIlya Bakulin static inline const char 116596e47614SIlya Bakulin *part_type(u_int type) 116696e47614SIlya Bakulin { 116796e47614SIlya Bakulin 116896e47614SIlya Bakulin switch (type) { 116996e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_RPMB: 117096e47614SIlya Bakulin return ("RPMB"); 117196e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_DEFAULT: 117296e47614SIlya Bakulin return ("default"); 117396e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_BOOT0: 117496e47614SIlya Bakulin return ("boot0"); 117596e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_BOOT1: 117696e47614SIlya Bakulin return ("boot1"); 117796e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP0: 117896e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP1: 117996e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP2: 118096e47614SIlya Bakulin case EXT_CSD_PART_CONFIG_ACC_GP3: 118196e47614SIlya Bakulin return ("general purpose"); 118296e47614SIlya Bakulin default: 118396e47614SIlya Bakulin return ("(unknown type)"); 1184a94a63f0SWarner Losh } 1185a94a63f0SWarner Losh } 1186a94a63f0SWarner Losh 118796e47614SIlya Bakulin static inline const char 118896e47614SIlya Bakulin *bus_width_str(enum mmc_bus_width w) 118996e47614SIlya Bakulin { 119096e47614SIlya Bakulin 119196e47614SIlya Bakulin switch (w) { 119296e47614SIlya Bakulin case bus_width_1: 119396e47614SIlya Bakulin return ("1-bit"); 119496e47614SIlya Bakulin case bus_width_4: 119596e47614SIlya Bakulin return ("4-bit"); 119696e47614SIlya Bakulin case bus_width_8: 119796e47614SIlya Bakulin return ("8-bit"); 119896e47614SIlya Bakulin } 119996e47614SIlya Bakulin } 120096e47614SIlya Bakulin 120196e47614SIlya Bakulin static uint32_t 120296e47614SIlya Bakulin sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb) 120396e47614SIlya Bakulin { 120496e47614SIlya Bakulin struct ccb_trans_settings_mmc *cts; 120596e47614SIlya Bakulin 120696e47614SIlya Bakulin cts = &ccb->cts.proto_specific.mmc; 120796e47614SIlya Bakulin 120896e47614SIlya Bakulin ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 120996e47614SIlya Bakulin ccb->ccb_h.flags = CAM_DIR_NONE; 121096e47614SIlya Bakulin ccb->ccb_h.retry_count = 0; 121196e47614SIlya Bakulin ccb->ccb_h.timeout = 100; 121296e47614SIlya Bakulin ccb->ccb_h.cbfcnp = NULL; 121396e47614SIlya Bakulin xpt_action(ccb); 121496e47614SIlya Bakulin 121596e47614SIlya Bakulin if (ccb->ccb_h.status != CAM_REQ_CMP) 121696e47614SIlya Bakulin panic("Cannot get host caps"); 121796e47614SIlya Bakulin return (cts->host_caps); 121896e47614SIlya Bakulin } 121996e47614SIlya Bakulin 12205d20e651SIlya Bakulin static uint32_t 12215d20e651SIlya Bakulin sdda_get_max_data(struct cam_periph *periph, union ccb *ccb) 12225d20e651SIlya Bakulin { 12235d20e651SIlya Bakulin struct ccb_trans_settings_mmc *cts; 12245d20e651SIlya Bakulin 12255d20e651SIlya Bakulin cts = &ccb->cts.proto_specific.mmc; 12265d20e651SIlya Bakulin memset(cts, 0, sizeof(struct ccb_trans_settings_mmc)); 12275d20e651SIlya Bakulin 12285d20e651SIlya Bakulin ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 12295d20e651SIlya Bakulin ccb->ccb_h.flags = CAM_DIR_NONE; 12305d20e651SIlya Bakulin ccb->ccb_h.retry_count = 0; 12315d20e651SIlya Bakulin ccb->ccb_h.timeout = 100; 12325d20e651SIlya Bakulin ccb->ccb_h.cbfcnp = NULL; 12335d20e651SIlya Bakulin xpt_action(ccb); 12345d20e651SIlya Bakulin 12355d20e651SIlya Bakulin if (ccb->ccb_h.status != CAM_REQ_CMP) 12365d20e651SIlya Bakulin panic("Cannot get host max data"); 12375d20e651SIlya Bakulin KASSERT(cts->host_max_data != 0, ("host_max_data == 0?!")); 12385d20e651SIlya Bakulin return (cts->host_max_data); 12395d20e651SIlya Bakulin } 12405d20e651SIlya Bakulin 1241a94a63f0SWarner Losh static void 124296e47614SIlya Bakulin sdda_start_init(void *context, union ccb *start_ccb) 124396e47614SIlya Bakulin { 124496e47614SIlya Bakulin struct cam_periph *periph = (struct cam_periph *)context; 124596e47614SIlya Bakulin struct ccb_trans_settings_mmc *cts; 124696e47614SIlya Bakulin uint32_t host_caps; 124796e47614SIlya Bakulin uint32_t sec_count; 1248a94a63f0SWarner Losh int err; 124996e47614SIlya Bakulin int host_f_max; 1250fd7371f7SEmmanuel Vadot uint8_t card_type; 1251a94a63f0SWarner Losh 1252a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init\n")); 1253a94a63f0SWarner Losh /* periph was held for us when this task was enqueued */ 1254a94a63f0SWarner Losh if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1255a94a63f0SWarner Losh cam_periph_release(periph); 1256a94a63f0SWarner Losh return; 1257a94a63f0SWarner Losh } 1258a94a63f0SWarner Losh 1259a94a63f0SWarner Losh struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 1260a94a63f0SWarner Losh //struct ccb_mmcio *mmcio = &start_ccb->mmcio; 1261a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data; 1262a94a63f0SWarner Losh struct cam_ed *device = periph->path->device; 1263a94a63f0SWarner Losh 1264a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) { 1265a94a63f0SWarner Losh mmc_decode_csd_mmc(mmcp->card_csd, &softc->csd); 1266a94a63f0SWarner Losh mmc_decode_cid_mmc(mmcp->card_cid, &softc->cid); 126796e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 4) { 1268a94a63f0SWarner Losh err = mmc_send_ext_csd(periph, start_ccb, 1269a94a63f0SWarner Losh (uint8_t *)&softc->raw_ext_csd, 1270a94a63f0SWarner Losh sizeof(softc->raw_ext_csd)); 127196e47614SIlya Bakulin if (err != 0) { 127296e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 127396e47614SIlya Bakulin ("Cannot read EXT_CSD, err %d", err)); 127496e47614SIlya Bakulin return; 127596e47614SIlya Bakulin } 127696e47614SIlya Bakulin } 1277a94a63f0SWarner Losh } else { 1278a94a63f0SWarner Losh mmc_decode_csd_sd(mmcp->card_csd, &softc->csd); 1279a94a63f0SWarner Losh mmc_decode_cid_sd(mmcp->card_cid, &softc->cid); 1280a94a63f0SWarner Losh } 1281a94a63f0SWarner Losh 1282a94a63f0SWarner Losh softc->sector_count = softc->csd.capacity / 512; 1283a94a63f0SWarner Losh softc->mediasize = softc->csd.capacity; 128496e47614SIlya Bakulin softc->cmd6_time = mmc_get_cmd6_timeout(periph); 1285a94a63f0SWarner Losh 1286a94a63f0SWarner Losh /* MMC >= 4.x have EXT_CSD that has its own opinion about capacity */ 128796e47614SIlya Bakulin if (mmc_get_spec_vers(periph) >= 4) { 128896e47614SIlya Bakulin sec_count = softc->raw_ext_csd[EXT_CSD_SEC_CNT] + 1289a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1290a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1291a94a63f0SWarner Losh (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1292a94a63f0SWarner Losh if (sec_count != 0) { 1293a94a63f0SWarner Losh softc->sector_count = sec_count; 1294a94a63f0SWarner Losh softc->mediasize = softc->sector_count * 512; 1295a94a63f0SWarner Losh /* FIXME: there should be a better name for this option...*/ 1296a94a63f0SWarner Losh mmcp->card_features |= CARD_FEATURE_SDHC; 1297a94a63f0SWarner Losh } 1298a94a63f0SWarner Losh } 1299a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 1300a94a63f0SWarner Losh ("Capacity: %"PRIu64", sectors: %"PRIu64"\n", 1301a94a63f0SWarner Losh softc->mediasize, 1302a94a63f0SWarner Losh softc->sector_count)); 1303a94a63f0SWarner Losh mmc_format_card_id_string(softc, mmcp); 1304a94a63f0SWarner Losh 1305a94a63f0SWarner Losh /* Update info for CAM */ 1306a94a63f0SWarner Losh device->serial_num_len = strlen(softc->card_sn_string); 130796e47614SIlya Bakulin device->serial_num = (u_int8_t *)malloc((device->serial_num_len + 1), 1308a94a63f0SWarner Losh M_CAMXPT, M_NOWAIT); 1309a94a63f0SWarner Losh strlcpy(device->serial_num, softc->card_sn_string, device->serial_num_len); 1310a94a63f0SWarner Losh 1311a94a63f0SWarner Losh device->device_id_len = strlen(softc->card_id_string); 131296e47614SIlya Bakulin device->device_id = (u_int8_t *)malloc((device->device_id_len + 1), 1313a94a63f0SWarner Losh M_CAMXPT, M_NOWAIT); 1314a94a63f0SWarner Losh strlcpy(device->device_id, softc->card_id_string, device->device_id_len); 1315a94a63f0SWarner Losh 1316a94a63f0SWarner Losh strlcpy(mmcp->model, softc->card_id_string, sizeof(mmcp->model)); 1317a94a63f0SWarner Losh 1318a94a63f0SWarner Losh /* Set the clock frequency that the card can handle */ 1319a94a63f0SWarner Losh cts = &start_ccb->cts.proto_specific.mmc; 1320a94a63f0SWarner Losh 1321a94a63f0SWarner Losh /* First, get the host's max freq */ 1322a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1323a94a63f0SWarner Losh start_ccb->ccb_h.flags = CAM_DIR_NONE; 1324a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0; 1325a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 100; 1326a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = NULL; 1327a94a63f0SWarner Losh xpt_action(start_ccb); 1328a94a63f0SWarner Losh 1329a94a63f0SWarner Losh if (start_ccb->ccb_h.status != CAM_REQ_CMP) 1330a94a63f0SWarner Losh panic("Cannot get max host freq"); 133196e47614SIlya Bakulin host_f_max = cts->host_f_max; 133296e47614SIlya Bakulin host_caps = cts->host_caps; 1333a94a63f0SWarner Losh if (cts->ios.bus_width != bus_width_1) 1334a94a63f0SWarner Losh panic("Bus width in ios is not 1-bit"); 1335a94a63f0SWarner Losh 1336a94a63f0SWarner Losh /* Now check if the card supports High-speed */ 1337a94a63f0SWarner Losh softc->card_f_max = softc->csd.tran_speed; 1338a94a63f0SWarner Losh 1339a94a63f0SWarner Losh if (host_caps & MMC_CAP_HSPEED) { 1340a94a63f0SWarner Losh /* Find out if the card supports High speed timing */ 1341a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_SD20) { 1342a94a63f0SWarner Losh /* Get and decode SCR */ 13434c4200c6SIlya Bakulin uint32_t rawscr[2]; 1344a94a63f0SWarner Losh uint8_t res[64]; 13454c4200c6SIlya Bakulin if (mmc_app_get_scr(periph, start_ccb, rawscr)) { 1346a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Cannot get SCR\n")); 1347a94a63f0SWarner Losh goto finish_hs_tests; 1348a94a63f0SWarner Losh } 13494c4200c6SIlya Bakulin mmc_app_decode_scr(rawscr, &softc->scr); 1350a94a63f0SWarner Losh 1351a94a63f0SWarner Losh if ((softc->scr.sda_vsn >= 1) && (softc->csd.ccc & (1<<10))) { 1352a94a63f0SWarner Losh mmc_sd_switch(periph, start_ccb, SD_SWITCH_MODE_CHECK, 1353a94a63f0SWarner Losh SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, res); 1354a94a63f0SWarner Losh if (res[13] & 2) { 1355a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS\n")); 1356a94a63f0SWarner Losh softc->card_f_max = SD_HS_MAX; 1357a94a63f0SWarner Losh } 1358d670d951SIlya Bakulin 1359d670d951SIlya Bakulin /* 1360d670d951SIlya Bakulin * We deselect then reselect the card here. Some cards 1361d670d951SIlya Bakulin * become unselected and timeout with the above two 1362d670d951SIlya Bakulin * commands, although the state tables / diagrams in the 1363d670d951SIlya Bakulin * standard suggest they go back to the transfer state. 1364d670d951SIlya Bakulin * Other cards don't become deselected, and if we 1365d670d951SIlya Bakulin * attempt to blindly re-select them, we get timeout 1366d670d951SIlya Bakulin * errors from some controllers. So we deselect then 1367d670d951SIlya Bakulin * reselect to handle all situations. 1368d670d951SIlya Bakulin */ 1369d670d951SIlya Bakulin mmc_select_card(periph, start_ccb, 0); 1370d670d951SIlya Bakulin mmc_select_card(periph, start_ccb, get_rca(periph)); 1371a94a63f0SWarner Losh } else { 1372a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Not trying the switch\n")); 1373a94a63f0SWarner Losh goto finish_hs_tests; 1374a94a63f0SWarner Losh } 1375a94a63f0SWarner Losh } 1376a94a63f0SWarner Losh 137796e47614SIlya Bakulin if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) { 1378fd7371f7SEmmanuel Vadot card_type = softc->raw_ext_csd[EXT_CSD_CARD_TYPE]; 1379fd7371f7SEmmanuel Vadot if (card_type & EXT_CSD_CARD_TYPE_HS_52) 1380a94a63f0SWarner Losh softc->card_f_max = MMC_TYPE_HS_52_MAX; 1381fd7371f7SEmmanuel Vadot else if (card_type & EXT_CSD_CARD_TYPE_HS_26) 1382a94a63f0SWarner Losh softc->card_f_max = MMC_TYPE_HS_26_MAX; 1383fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 && 1384fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_120) != 0) { 1385fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_ddr52); 1386fd7371f7SEmmanuel Vadot setbit(&softc->vccq_120, bus_timing_mmc_ddr52); 1387fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.2V\n")); 1388fd7371f7SEmmanuel Vadot } 1389fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 && 1390fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_180) != 0) { 1391fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_ddr52); 1392fd7371f7SEmmanuel Vadot setbit(&softc->vccq_180, bus_timing_mmc_ddr52); 1393fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports DDR52 at 1.8V\n")); 1394fd7371f7SEmmanuel Vadot } 1395fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) != 0 && 1396fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_120) != 0) { 1397fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_hs200); 1398fd7371f7SEmmanuel Vadot setbit(&softc->vccq_120, bus_timing_mmc_hs200); 1399fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.2V\n")); 1400fd7371f7SEmmanuel Vadot } 1401fd7371f7SEmmanuel Vadot if ((card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) != 0 && 1402fd7371f7SEmmanuel Vadot (host_caps & MMC_CAP_SIGNALING_180) != 0) { 1403fd7371f7SEmmanuel Vadot setbit(&softc->timings, bus_timing_mmc_hs200); 1404fd7371f7SEmmanuel Vadot setbit(&softc->vccq_180, bus_timing_mmc_hs200); 1405fd7371f7SEmmanuel Vadot CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS200 at 1.8V\n")); 1406fd7371f7SEmmanuel Vadot } 1407a94a63f0SWarner Losh } 1408a94a63f0SWarner Losh } 1409a94a63f0SWarner Losh int f_max; 1410a94a63f0SWarner Losh finish_hs_tests: 1411a94a63f0SWarner Losh f_max = min(host_f_max, softc->card_f_max); 1412a94a63f0SWarner 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)); 1413a94a63f0SWarner Losh 1414d670d951SIlya Bakulin /* Enable high-speed timing on the card */ 1415d670d951SIlya Bakulin if (f_max > 25000000) { 1416d670d951SIlya Bakulin err = mmc_set_timing(periph, start_ccb, bus_timing_hs); 1417d670d951SIlya Bakulin if (err != MMC_ERR_NONE) { 1418d670d951SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("Cannot switch card to high-speed mode")); 1419d670d951SIlya Bakulin f_max = 25000000; 1420d670d951SIlya Bakulin } 1421d670d951SIlya Bakulin } 1422fd7371f7SEmmanuel Vadot /* If possible, set lower-level signaling */ 1423fd7371f7SEmmanuel Vadot enum mmc_bus_timing timing; 1424fd7371f7SEmmanuel Vadot /* FIXME: MMCCAM supports max. bus_timing_mmc_ddr52 at the moment. */ 1425fd7371f7SEmmanuel Vadot for (timing = bus_timing_mmc_ddr52; timing > bus_timing_normal; timing--) { 1426fd7371f7SEmmanuel Vadot if (isset(&softc->vccq_120, timing)) { 1427fd7371f7SEmmanuel Vadot /* Set VCCQ = 1.2V */ 1428fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1429fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE; 1430fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0; 1431fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100; 1432fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL; 1433fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_120; 1434fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ; 1435fd7371f7SEmmanuel Vadot xpt_action(start_ccb); 1436fd7371f7SEmmanuel Vadot break; 1437fd7371f7SEmmanuel Vadot } else if (isset(&softc->vccq_180, timing)) { 1438fd7371f7SEmmanuel Vadot /* Set VCCQ = 1.8V */ 1439fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1440fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE; 1441fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0; 1442fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100; 1443fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL; 1444fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_180; 1445fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ; 1446fd7371f7SEmmanuel Vadot xpt_action(start_ccb); 1447fd7371f7SEmmanuel Vadot break; 1448fd7371f7SEmmanuel Vadot } else { 1449fd7371f7SEmmanuel Vadot /* Set VCCQ = 3.3V */ 1450fd7371f7SEmmanuel Vadot start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1451fd7371f7SEmmanuel Vadot start_ccb->ccb_h.flags = CAM_DIR_NONE; 1452fd7371f7SEmmanuel Vadot start_ccb->ccb_h.retry_count = 0; 1453fd7371f7SEmmanuel Vadot start_ccb->ccb_h.timeout = 100; 1454fd7371f7SEmmanuel Vadot start_ccb->ccb_h.cbfcnp = NULL; 1455fd7371f7SEmmanuel Vadot cts->ios.vccq = vccq_330; 1456fd7371f7SEmmanuel Vadot cts->ios_valid = MMC_VCCQ; 1457fd7371f7SEmmanuel Vadot xpt_action(start_ccb); 1458fd7371f7SEmmanuel Vadot break; 1459fd7371f7SEmmanuel Vadot } 1460fd7371f7SEmmanuel Vadot } 1461fd7371f7SEmmanuel Vadot 1462d670d951SIlya Bakulin /* Set frequency on the controller */ 1463a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1464a94a63f0SWarner Losh start_ccb->ccb_h.flags = CAM_DIR_NONE; 1465a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0; 1466a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 100; 1467a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = NULL; 1468a94a63f0SWarner Losh cts->ios.clock = f_max; 1469a94a63f0SWarner Losh cts->ios_valid = MMC_CLK; 1470a94a63f0SWarner Losh xpt_action(start_ccb); 1471a94a63f0SWarner Losh 1472a94a63f0SWarner Losh /* Set bus width */ 1473a94a63f0SWarner Losh enum mmc_bus_width desired_bus_width = bus_width_1; 1474a94a63f0SWarner Losh enum mmc_bus_width max_host_bus_width = 1475a94a63f0SWarner Losh (host_caps & MMC_CAP_8_BIT_DATA ? bus_width_8 : 1476a94a63f0SWarner Losh host_caps & MMC_CAP_4_BIT_DATA ? bus_width_4 : bus_width_1); 1477a94a63f0SWarner Losh enum mmc_bus_width max_card_bus_width = bus_width_1; 1478a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_SD20 && 1479a94a63f0SWarner Losh softc->scr.bus_widths & SD_SCR_BUS_WIDTH_4) 1480a94a63f0SWarner Losh max_card_bus_width = bus_width_4; 1481a94a63f0SWarner Losh /* 1482a94a63f0SWarner Losh * Unlike SD, MMC cards don't have any information about supported bus width... 1483a94a63f0SWarner Losh * So we need to perform read/write test to find out the width. 1484a94a63f0SWarner Losh */ 1485a94a63f0SWarner Losh /* TODO: figure out bus width for MMC; use 8-bit for now (to test on BBB) */ 1486a94a63f0SWarner Losh if (mmcp->card_features & CARD_FEATURE_MMC) 1487a94a63f0SWarner Losh max_card_bus_width = bus_width_8; 1488a94a63f0SWarner Losh 1489a94a63f0SWarner Losh desired_bus_width = min(max_host_bus_width, max_card_bus_width); 1490a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 1491a94a63f0SWarner Losh ("Set bus width to %s (min of host %s and card %s)\n", 1492a94a63f0SWarner Losh bus_width_str(desired_bus_width), 1493a94a63f0SWarner Losh bus_width_str(max_host_bus_width), 1494a94a63f0SWarner Losh bus_width_str(max_card_bus_width))); 1495a94a63f0SWarner Losh sdda_set_bus_width(periph, start_ccb, desired_bus_width); 1496a94a63f0SWarner Losh 1497a94a63f0SWarner Losh softc->state = SDDA_STATE_NORMAL; 149896e47614SIlya Bakulin 1499e70d59c0SEmmanuel Vadot cam_periph_unhold(periph); 150096e47614SIlya Bakulin /* MMC partitions support */ 150196e47614SIlya Bakulin if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) { 150296e47614SIlya Bakulin sdda_process_mmc_partitions(periph, start_ccb); 150396e47614SIlya Bakulin } else if (mmcp->card_features & CARD_FEATURE_SD20) { 150496e47614SIlya Bakulin /* For SD[HC] cards, just add one partition that is the whole card */ 150596e47614SIlya Bakulin sdda_add_part(periph, 0, "sdda", 150696e47614SIlya Bakulin periph->unit_number, 150796e47614SIlya Bakulin mmc_get_media_size(periph), 150896e47614SIlya Bakulin sdda_get_read_only(periph, start_ccb)); 150996e47614SIlya Bakulin softc->part_curr = 0; 151096e47614SIlya Bakulin } 1511e70d59c0SEmmanuel Vadot cam_periph_hold(periph, PRIBIO|PCATCH); 151296e47614SIlya Bakulin 151396e47614SIlya Bakulin xpt_announce_periph(periph, softc->card_id_string); 151496e47614SIlya Bakulin /* 151596e47614SIlya Bakulin * Add async callbacks for bus reset and bus device reset calls. 151696e47614SIlya Bakulin * I don't bother checking if this fails as, in most cases, 151796e47614SIlya Bakulin * the system will function just fine without them and the only 151896e47614SIlya Bakulin * alternative would be to not attach the device on failure. 151996e47614SIlya Bakulin */ 152096e47614SIlya Bakulin xpt_register_async(AC_LOST_DEVICE | AC_GETDEV_CHANGED | 152196e47614SIlya Bakulin AC_ADVINFO_CHANGED, sddaasync, periph, periph->path); 152296e47614SIlya Bakulin } 152396e47614SIlya Bakulin 152496e47614SIlya Bakulin static void 152596e47614SIlya Bakulin sdda_add_part(struct cam_periph *periph, u_int type, const char *name, 152696e47614SIlya Bakulin u_int cnt, off_t media_size, bool ro) 152796e47614SIlya Bakulin { 152896e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc; 152996e47614SIlya Bakulin struct sdda_part *part; 153096e47614SIlya Bakulin struct ccb_pathinq cpi; 153196e47614SIlya Bakulin 153296e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 153396e47614SIlya Bakulin ("Partition type '%s', size %ju %s\n", 153496e47614SIlya Bakulin part_type(type), 153596e47614SIlya Bakulin media_size, 153696e47614SIlya Bakulin ro ? "(read-only)" : "")); 153796e47614SIlya Bakulin 153896e47614SIlya Bakulin part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF, 153996e47614SIlya Bakulin M_WAITOK | M_ZERO); 154096e47614SIlya Bakulin 154196e47614SIlya Bakulin part->cnt = cnt; 154296e47614SIlya Bakulin part->type = type; 154396e47614SIlya Bakulin part->ro = ro; 154496e47614SIlya Bakulin part->sc = sc; 154596e47614SIlya Bakulin snprintf(part->name, sizeof(part->name), name, periph->unit_number); 154696e47614SIlya Bakulin 154796e47614SIlya Bakulin /* 154896e47614SIlya Bakulin * Due to the nature of RPMB partition it doesn't make much sense 154996e47614SIlya Bakulin * to add it as a disk. It would be more appropriate to create a 155096e47614SIlya Bakulin * userland tool to operate on the partition or leverage the existing 155196e47614SIlya Bakulin * tools from sysutils/mmc-utils. 155296e47614SIlya Bakulin */ 155396e47614SIlya Bakulin if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) { 155496e47614SIlya Bakulin /* TODO: Create device, assign IOCTL handler */ 155596e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 155696e47614SIlya Bakulin ("Don't know what to do with RPMB partitions yet\n")); 155796e47614SIlya Bakulin return; 155896e47614SIlya Bakulin } 155996e47614SIlya Bakulin 156096e47614SIlya Bakulin bioq_init(&part->bio_queue); 156196e47614SIlya Bakulin 156296e47614SIlya Bakulin bzero(&cpi, sizeof(cpi)); 156396e47614SIlya Bakulin xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 156496e47614SIlya Bakulin cpi.ccb_h.func_code = XPT_PATH_INQ; 156596e47614SIlya Bakulin xpt_action((union ccb *)&cpi); 156696e47614SIlya Bakulin 156796e47614SIlya Bakulin /* 156896e47614SIlya Bakulin * Register this media as a disk 156996e47614SIlya Bakulin */ 157096e47614SIlya Bakulin (void)cam_periph_hold(periph, PRIBIO); 157196e47614SIlya Bakulin cam_periph_unlock(periph); 157296e47614SIlya Bakulin 157396e47614SIlya Bakulin part->disk = disk_alloc(); 157496e47614SIlya Bakulin part->disk->d_rotation_rate = DISK_RR_NON_ROTATING; 157596e47614SIlya Bakulin part->disk->d_devstat = devstat_new_entry(part->name, 157696e47614SIlya Bakulin cnt, 512, 157796e47614SIlya Bakulin DEVSTAT_ALL_SUPPORTED, 157896e47614SIlya Bakulin DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport), 157996e47614SIlya Bakulin DEVSTAT_PRIORITY_DISK); 158096e47614SIlya Bakulin 158196e47614SIlya Bakulin part->disk->d_open = sddaopen; 158296e47614SIlya Bakulin part->disk->d_close = sddaclose; 158396e47614SIlya Bakulin part->disk->d_strategy = sddastrategy; 158496e47614SIlya Bakulin part->disk->d_getattr = sddagetattr; 158596e47614SIlya Bakulin // sc->disk->d_dump = sddadump; 158696e47614SIlya Bakulin part->disk->d_gone = sddadiskgonecb; 158796e47614SIlya Bakulin part->disk->d_name = part->name; 158896e47614SIlya Bakulin part->disk->d_drv1 = part; 15895d20e651SIlya Bakulin part->disk->d_maxsize = 15905d20e651SIlya Bakulin MIN(MAXPHYS, sdda_get_max_data(periph, 15915d20e651SIlya Bakulin (union ccb *)&cpi) * mmc_get_sector_size(periph)); 159296e47614SIlya Bakulin part->disk->d_unit = cnt; 159396e47614SIlya Bakulin part->disk->d_flags = 0; 159496e47614SIlya Bakulin strlcpy(part->disk->d_descr, sc->card_id_string, 159596e47614SIlya Bakulin MIN(sizeof(part->disk->d_descr), sizeof(sc->card_id_string))); 159696e47614SIlya Bakulin strlcpy(part->disk->d_ident, sc->card_sn_string, 159796e47614SIlya Bakulin MIN(sizeof(part->disk->d_ident), sizeof(sc->card_sn_string))); 159896e47614SIlya Bakulin part->disk->d_hba_vendor = cpi.hba_vendor; 159996e47614SIlya Bakulin part->disk->d_hba_device = cpi.hba_device; 160096e47614SIlya Bakulin part->disk->d_hba_subvendor = cpi.hba_subvendor; 160196e47614SIlya Bakulin part->disk->d_hba_subdevice = cpi.hba_subdevice; 1602b5961be1SEdward Tomasz Napierala snprintf(part->disk->d_attachment, sizeof(part->disk->d_attachment), 1603b5961be1SEdward Tomasz Napierala "%s%d", cpi.dev_name, cpi.unit_number); 160496e47614SIlya Bakulin 160596e47614SIlya Bakulin part->disk->d_sectorsize = mmc_get_sector_size(periph); 160696e47614SIlya Bakulin part->disk->d_mediasize = media_size; 160796e47614SIlya Bakulin part->disk->d_stripesize = 0; 160896e47614SIlya Bakulin part->disk->d_fwsectors = 0; 160996e47614SIlya Bakulin part->disk->d_fwheads = 0; 161096e47614SIlya Bakulin 1611bf286853SEmmanuel Vadot if (sdda_mmcsd_compat) 1612bf286853SEmmanuel Vadot disk_add_alias(part->disk, "mmcsd"); 1613bf286853SEmmanuel Vadot 161496e47614SIlya Bakulin /* 161596e47614SIlya Bakulin * Acquire a reference to the periph before we register with GEOM. 161696e47614SIlya Bakulin * We'll release this reference once GEOM calls us back (via 161796e47614SIlya Bakulin * sddadiskgonecb()) telling us that our provider has been freed. 161896e47614SIlya Bakulin */ 161996e47614SIlya Bakulin if (cam_periph_acquire(periph) != 0) { 162096e47614SIlya Bakulin xpt_print(periph->path, "%s: lost periph during " 162196e47614SIlya Bakulin "registration!\n", __func__); 162296e47614SIlya Bakulin cam_periph_lock(periph); 162396e47614SIlya Bakulin return; 162496e47614SIlya Bakulin } 162596e47614SIlya Bakulin disk_create(part->disk, DISK_VERSION); 162696e47614SIlya Bakulin cam_periph_lock(periph); 162796e47614SIlya Bakulin cam_periph_unhold(periph); 162896e47614SIlya Bakulin } 162996e47614SIlya Bakulin 163096e47614SIlya Bakulin /* 163196e47614SIlya Bakulin * For MMC cards, process EXT_CSD and add partitions that are supported by 163296e47614SIlya Bakulin * this device. 163396e47614SIlya Bakulin */ 163496e47614SIlya Bakulin static void 163596e47614SIlya Bakulin sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *ccb) 163696e47614SIlya Bakulin { 163796e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc; 163896e47614SIlya Bakulin struct mmc_params *mmcp = &periph->path->device->mmc_ident_data; 163996e47614SIlya Bakulin off_t erase_size, sector_size, size, wp_size; 164096e47614SIlya Bakulin int i; 164196e47614SIlya Bakulin const uint8_t *ext_csd; 164296e47614SIlya Bakulin uint8_t rev; 164396e47614SIlya Bakulin bool comp, ro; 164496e47614SIlya Bakulin 164596e47614SIlya Bakulin ext_csd = sc->raw_ext_csd; 164696e47614SIlya Bakulin 164796e47614SIlya Bakulin /* 164896e47614SIlya Bakulin * Enhanced user data area and general purpose partitions are only 164996e47614SIlya Bakulin * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB 165096e47614SIlya Bakulin * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later. 165196e47614SIlya Bakulin */ 165296e47614SIlya Bakulin rev = ext_csd[EXT_CSD_REV]; 165396e47614SIlya Bakulin 165496e47614SIlya Bakulin /* 165596e47614SIlya Bakulin * Ignore user-creatable enhanced user data area and general purpose 165696e47614SIlya Bakulin * partitions partitions as long as partitioning hasn't been finished. 165796e47614SIlya Bakulin */ 165896e47614SIlya Bakulin comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0; 165996e47614SIlya Bakulin 166096e47614SIlya Bakulin /* 166196e47614SIlya Bakulin * Add enhanced user data area slice, unless it spans the entirety of 166296e47614SIlya Bakulin * the user data area. The enhanced area is of a multiple of high 166396e47614SIlya Bakulin * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) * 166496e47614SIlya Bakulin * 512 KB) and its offset given in either sectors or bytes, depending 166596e47614SIlya Bakulin * on whether it's a high capacity device or not. 166696e47614SIlya Bakulin * NB: The slicer and its slices need to be registered before adding 166796e47614SIlya Bakulin * the disk for the corresponding user data area as re-tasting is 166896e47614SIlya Bakulin * racy. 166996e47614SIlya Bakulin */ 167096e47614SIlya Bakulin sector_size = mmc_get_sector_size(periph); 167196e47614SIlya Bakulin size = ext_csd[EXT_CSD_ENH_SIZE_MULT] + 167296e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + 167396e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16); 167496e47614SIlya Bakulin if (rev >= 4 && comp == TRUE && size > 0 && 167596e47614SIlya Bakulin (ext_csd[EXT_CSD_PART_SUPPORT] & 167696e47614SIlya Bakulin EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 && 167796e47614SIlya Bakulin (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) { 167896e47614SIlya Bakulin erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 167996e47614SIlya Bakulin MMC_SECTOR_SIZE; 168096e47614SIlya Bakulin wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 168196e47614SIlya Bakulin size *= erase_size * wp_size; 168296e47614SIlya Bakulin if (size != mmc_get_media_size(periph) * sector_size) { 168396e47614SIlya Bakulin sc->enh_size = size; 168496e47614SIlya Bakulin sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] + 168596e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) + 168696e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) + 168796e47614SIlya Bakulin (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) * 168896e47614SIlya Bakulin ((mmcp->card_features & CARD_FEATURE_SDHC) ? 1: MMC_SECTOR_SIZE); 168996e47614SIlya Bakulin } else 169096e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 169196e47614SIlya Bakulin ("enhanced user data area spans entire device")); 169296e47614SIlya Bakulin } 169396e47614SIlya Bakulin 169496e47614SIlya Bakulin /* 169596e47614SIlya Bakulin * Add default partition. This may be the only one or the user 169696e47614SIlya Bakulin * data area in case partitions are supported. 169796e47614SIlya Bakulin */ 169896e47614SIlya Bakulin ro = sdda_get_read_only(periph, ccb); 169996e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "sdda", 170096e47614SIlya Bakulin periph->unit_number, mmc_get_media_size(periph), ro); 170196e47614SIlya Bakulin sc->part_curr = EXT_CSD_PART_CONFIG_ACC_DEFAULT; 170296e47614SIlya Bakulin 170396e47614SIlya Bakulin if (mmc_get_spec_vers(periph) < 3) 170496e47614SIlya Bakulin return; 170596e47614SIlya Bakulin 170696e47614SIlya Bakulin /* Belatedly announce enhanced user data slice. */ 170796e47614SIlya Bakulin if (sc->enh_size != 0) { 170896e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 170996e47614SIlya Bakulin ("enhanced user data area off 0x%jx size %ju bytes\n", 171096e47614SIlya Bakulin sc->enh_base, sc->enh_size)); 171196e47614SIlya Bakulin } 171296e47614SIlya Bakulin 171396e47614SIlya Bakulin /* 171496e47614SIlya Bakulin * Determine partition switch timeout (provided in units of 10 ms) 171596e47614SIlya Bakulin * and ensure it's at least 300 ms as some eMMC chips lie. 171696e47614SIlya Bakulin */ 171796e47614SIlya Bakulin sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000, 171896e47614SIlya Bakulin 300 * 1000); 171996e47614SIlya Bakulin 172096e47614SIlya Bakulin /* Add boot partitions, which are of a fixed multiple of 128 KB. */ 172196e47614SIlya Bakulin size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 172296e47614SIlya Bakulin if (size > 0 && (sdda_get_host_caps(periph, ccb) & MMC_CAP_BOOT_NOACC) == 0) { 172396e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT0, 172496e47614SIlya Bakulin SDDA_FMT_BOOT, 0, size, 172596e47614SIlya Bakulin ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 172696e47614SIlya Bakulin EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0)); 172796e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT1, 172896e47614SIlya Bakulin SDDA_FMT_BOOT, 1, size, 172996e47614SIlya Bakulin ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] & 173096e47614SIlya Bakulin EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0)); 173196e47614SIlya Bakulin } 173296e47614SIlya Bakulin 173396e47614SIlya Bakulin /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */ 173496e47614SIlya Bakulin size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE; 173596e47614SIlya Bakulin if (rev >= 5 && size > 0) 173696e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_RPMB, 173796e47614SIlya Bakulin SDDA_FMT_RPMB, 0, size, ro); 173896e47614SIlya Bakulin 173996e47614SIlya Bakulin if (rev <= 3 || comp == FALSE) 174096e47614SIlya Bakulin return; 174196e47614SIlya Bakulin 174296e47614SIlya Bakulin /* 174396e47614SIlya Bakulin * Add general purpose partitions, which are of a multiple of high 174496e47614SIlya Bakulin * capacity write protect groups, too. 174596e47614SIlya Bakulin */ 174696e47614SIlya Bakulin if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) { 174796e47614SIlya Bakulin erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 * 174896e47614SIlya Bakulin MMC_SECTOR_SIZE; 174996e47614SIlya Bakulin wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 175096e47614SIlya Bakulin for (i = 0; i < MMC_PART_GP_MAX; i++) { 175196e47614SIlya Bakulin size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] + 175296e47614SIlya Bakulin (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) + 175396e47614SIlya Bakulin (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16); 175496e47614SIlya Bakulin if (size == 0) 175596e47614SIlya Bakulin continue; 175696e47614SIlya Bakulin sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_GP0 + i, 175796e47614SIlya Bakulin SDDA_FMT_GP, i, size * erase_size * wp_size, ro); 175896e47614SIlya Bakulin } 175996e47614SIlya Bakulin } 176096e47614SIlya Bakulin } 176196e47614SIlya Bakulin 176296e47614SIlya Bakulin /* 176396e47614SIlya Bakulin * We cannot just call mmc_switch() since it will sleep, and we are in 176496e47614SIlya Bakulin * GEOM context and cannot sleep. Instead, create an MMCIO request to switch 176596e47614SIlya Bakulin * partitions and send it to h/w, and upon completion resume processing 176696e47614SIlya Bakulin * the I/O queue. 176796e47614SIlya Bakulin * This function cannot fail, instead check switch errors in sddadone(). 176896e47614SIlya Bakulin */ 176996e47614SIlya Bakulin static void 177015f4848aSAndriy Gapon sdda_init_switch_part(struct cam_periph *periph, union ccb *start_ccb, 177115f4848aSAndriy Gapon uint8_t part) 177215f4848aSAndriy Gapon { 177396e47614SIlya Bakulin struct sdda_softc *sc = (struct sdda_softc *)periph->softc; 177496e47614SIlya Bakulin uint8_t value; 177596e47614SIlya Bakulin 177615f4848aSAndriy Gapon KASSERT(part < MMC_PART_MAX, ("%s: invalid partition index", __func__)); 177796e47614SIlya Bakulin sc->part_requested = part; 177896e47614SIlya Bakulin 177996e47614SIlya Bakulin value = (sc->raw_ext_csd[EXT_CSD_PART_CONFIG] & 178096e47614SIlya Bakulin ~EXT_CSD_PART_CONFIG_ACC_MASK) | part; 178196e47614SIlya Bakulin 178296e47614SIlya Bakulin mmc_switch_fill_mmcio(start_ccb, EXT_CSD_CMD_SET_NORMAL, 178396e47614SIlya Bakulin EXT_CSD_PART_CONFIG, value, sc->part_time); 178496e47614SIlya Bakulin start_ccb->ccb_h.cbfcnp = sddadone; 178596e47614SIlya Bakulin 178696e47614SIlya Bakulin sc->outstanding_cmds++; 178796e47614SIlya Bakulin cam_periph_unlock(periph); 178896e47614SIlya Bakulin xpt_action(start_ccb); 178996e47614SIlya Bakulin cam_periph_lock(periph); 1790a94a63f0SWarner Losh } 1791a94a63f0SWarner Losh 1792a94a63f0SWarner Losh /* Called with periph lock held! */ 1793a94a63f0SWarner Losh static void 1794a94a63f0SWarner Losh sddastart(struct cam_periph *periph, union ccb *start_ccb) 1795a94a63f0SWarner Losh { 179696e47614SIlya Bakulin struct bio *bp; 1797a94a63f0SWarner Losh struct sdda_softc *softc = (struct sdda_softc *)periph->softc; 179896e47614SIlya Bakulin struct sdda_part *part; 1799a94a63f0SWarner Losh struct mmc_params *mmcp = &periph->path->device->mmc_ident_data; 180015f4848aSAndriy Gapon uint8_t part_index; 1801a94a63f0SWarner Losh 1802a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastart\n")); 1803a94a63f0SWarner Losh 1804a94a63f0SWarner Losh if (softc->state != SDDA_STATE_NORMAL) { 180596e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("device is not in SDDA_STATE_NORMAL yet\n")); 1806a94a63f0SWarner Losh xpt_release_ccb(start_ccb); 1807a94a63f0SWarner Losh return; 1808a94a63f0SWarner Losh } 1809a94a63f0SWarner Losh 181096e47614SIlya Bakulin /* Find partition that has outstanding commands. Prefer current partition. */ 18114dfdaf4dSAndriy Gapon part_index = softc->part_curr; 181296e47614SIlya Bakulin part = softc->part[softc->part_curr]; 181396e47614SIlya Bakulin bp = bioq_first(&part->bio_queue); 181496e47614SIlya Bakulin if (bp == NULL) { 181596e47614SIlya Bakulin for (part_index = 0; part_index < MMC_PART_MAX; part_index++) { 181696e47614SIlya Bakulin if ((part = softc->part[part_index]) != NULL && 181796e47614SIlya Bakulin (bp = bioq_first(&softc->part[part_index]->bio_queue)) != NULL) 181896e47614SIlya Bakulin break; 181996e47614SIlya Bakulin } 182096e47614SIlya Bakulin } 1821a94a63f0SWarner Losh if (bp == NULL) { 1822a94a63f0SWarner Losh xpt_release_ccb(start_ccb); 1823a94a63f0SWarner Losh return; 1824a94a63f0SWarner Losh } 182596e47614SIlya Bakulin if (part_index != softc->part_curr) { 182696e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, 182796e47614SIlya Bakulin ("Partition %d -> %d\n", softc->part_curr, part_index)); 182896e47614SIlya Bakulin /* 182996e47614SIlya Bakulin * According to section "6.2.2 Command restrictions" of the eMMC 183096e47614SIlya Bakulin * specification v5.1, CMD19/CMD21 aren't allowed to be used with 183196e47614SIlya Bakulin * RPMB partitions. So we pause re-tuning along with triggering 183296e47614SIlya Bakulin * it up-front to decrease the likelihood of re-tuning becoming 183396e47614SIlya Bakulin * necessary while accessing an RPMB partition. Consequently, an 183496e47614SIlya Bakulin * RPMB partition should immediately be switched away from again 183596e47614SIlya Bakulin * after an access in order to allow for re-tuning to take place 183696e47614SIlya Bakulin * anew. 183796e47614SIlya Bakulin */ 183896e47614SIlya Bakulin /* TODO: pause retune if switching to RPMB partition */ 183996e47614SIlya Bakulin softc->state = SDDA_STATE_PART_SWITCH; 184096e47614SIlya Bakulin sdda_init_switch_part(periph, start_ccb, part_index); 184196e47614SIlya Bakulin return; 184296e47614SIlya Bakulin } 184396e47614SIlya Bakulin 184496e47614SIlya Bakulin bioq_remove(&part->bio_queue, bp); 1845a94a63f0SWarner Losh 1846a94a63f0SWarner Losh switch (bp->bio_cmd) { 1847a94a63f0SWarner Losh case BIO_WRITE: 1848a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_WRITE\n")); 184996e47614SIlya Bakulin part->flags |= SDDA_FLAG_DIRTY; 1850a94a63f0SWarner Losh /* FALLTHROUGH */ 1851a94a63f0SWarner Losh case BIO_READ: 1852a94a63f0SWarner Losh { 185396e47614SIlya Bakulin struct ccb_mmcio *mmcio; 1854a94a63f0SWarner Losh uint64_t blockno = bp->bio_pblkno; 1855a94a63f0SWarner Losh uint16_t count = bp->bio_bcount / 512; 1856a94a63f0SWarner Losh uint16_t opcode; 1857a94a63f0SWarner Losh 185896e47614SIlya Bakulin if (bp->bio_cmd == BIO_READ) 185996e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_READ\n")); 186096e47614SIlya Bakulin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 186196e47614SIlya Bakulin ("Block %"PRIu64" cnt %u\n", blockno, count)); 1862a94a63f0SWarner Losh 1863a94a63f0SWarner Losh /* Construct new MMC command */ 1864a94a63f0SWarner Losh if (bp->bio_cmd == BIO_READ) { 1865a94a63f0SWarner Losh if (count > 1) 1866a94a63f0SWarner Losh opcode = MMC_READ_MULTIPLE_BLOCK; 1867a94a63f0SWarner Losh else 1868a94a63f0SWarner Losh opcode = MMC_READ_SINGLE_BLOCK; 1869a94a63f0SWarner Losh } else { 1870a94a63f0SWarner Losh if (count > 1) 1871a94a63f0SWarner Losh opcode = MMC_WRITE_MULTIPLE_BLOCK; 1872a94a63f0SWarner Losh else 1873a94a63f0SWarner Losh opcode = MMC_WRITE_BLOCK; 1874a94a63f0SWarner Losh } 1875a94a63f0SWarner Losh 1876a94a63f0SWarner Losh start_ccb->ccb_h.func_code = XPT_MMC_IO; 1877a94a63f0SWarner Losh start_ccb->ccb_h.flags = (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : CAM_DIR_OUT); 1878a94a63f0SWarner Losh start_ccb->ccb_h.retry_count = 0; 1879a94a63f0SWarner Losh start_ccb->ccb_h.timeout = 15 * 1000; 1880a94a63f0SWarner Losh start_ccb->ccb_h.cbfcnp = sddadone; 1881a94a63f0SWarner Losh 1882a94a63f0SWarner Losh mmcio = &start_ccb->mmcio; 1883a94a63f0SWarner Losh mmcio->cmd.opcode = opcode; 1884a94a63f0SWarner Losh mmcio->cmd.arg = blockno; 1885a94a63f0SWarner Losh if (!(mmcp->card_features & CARD_FEATURE_SDHC)) 1886a94a63f0SWarner Losh mmcio->cmd.arg <<= 9; 1887a94a63f0SWarner Losh 1888a94a63f0SWarner Losh mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1889a94a63f0SWarner Losh mmcio->cmd.data = softc->mmcdata; 18900660cfa0SIlya Bakulin memset(mmcio->cmd.data, 0, sizeof(struct mmc_data)); 1891a94a63f0SWarner Losh mmcio->cmd.data->data = bp->bio_data; 1892a94a63f0SWarner Losh mmcio->cmd.data->len = 512 * count; 1893a94a63f0SWarner Losh mmcio->cmd.data->flags = (bp->bio_cmd == BIO_READ ? MMC_DATA_READ : MMC_DATA_WRITE); 1894a94a63f0SWarner Losh /* Direct h/w to issue CMD12 upon completion */ 1895a94a63f0SWarner Losh if (count > 1) { 18963f1cfdb1SIlya Bakulin mmcio->cmd.data->flags |= MMC_DATA_MULTI; 1897a94a63f0SWarner Losh mmcio->stop.opcode = MMC_STOP_TRANSMISSION; 1898a94a63f0SWarner Losh mmcio->stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 1899a94a63f0SWarner Losh mmcio->stop.arg = 0; 1900a94a63f0SWarner Losh } 1901a94a63f0SWarner Losh 1902a94a63f0SWarner Losh break; 1903a94a63f0SWarner Losh } 1904a94a63f0SWarner Losh case BIO_FLUSH: 1905a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_FLUSH\n")); 1906a94a63f0SWarner Losh sddaschedule(periph); 1907a94a63f0SWarner Losh break; 1908a94a63f0SWarner Losh case BIO_DELETE: 1909a94a63f0SWarner Losh CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_DELETE\n")); 1910a94a63f0SWarner Losh sddaschedule(periph); 1911a94a63f0SWarner Losh break; 1912d176b803SScott Long default: 1913d176b803SScott Long biofinish(bp, NULL, EOPNOTSUPP); 1914d176b803SScott Long xpt_release_ccb(start_ccb); 1915d176b803SScott Long return; 1916a94a63f0SWarner Losh } 1917a94a63f0SWarner Losh start_ccb->ccb_h.ccb_bp = bp; 1918a94a63f0SWarner Losh softc->outstanding_cmds++; 1919a94a63f0SWarner Losh softc->refcount++; 1920a94a63f0SWarner Losh cam_periph_unlock(periph); 1921a94a63f0SWarner Losh xpt_action(start_ccb); 1922a94a63f0SWarner Losh cam_periph_lock(periph); 1923a94a63f0SWarner Losh 1924a94a63f0SWarner Losh /* May have more work to do, so ensure we stay scheduled */ 1925a94a63f0SWarner Losh sddaschedule(periph); 1926a94a63f0SWarner Losh } 1927a94a63f0SWarner Losh 1928a94a63f0SWarner Losh static void 1929a94a63f0SWarner Losh sddadone(struct cam_periph *periph, union ccb *done_ccb) 1930a94a63f0SWarner Losh { 193196e47614SIlya Bakulin struct bio *bp; 1932a94a63f0SWarner Losh struct sdda_softc *softc; 1933a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1934a94a63f0SWarner Losh struct cam_path *path; 193596e47614SIlya Bakulin uint32_t card_status; 193696e47614SIlya Bakulin int error = 0; 1937a94a63f0SWarner Losh 1938a94a63f0SWarner Losh softc = (struct sdda_softc *)periph->softc; 1939a94a63f0SWarner Losh mmcio = &done_ccb->mmcio; 1940a94a63f0SWarner Losh path = done_ccb->ccb_h.path; 1941a94a63f0SWarner Losh 1942a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddadone\n")); 1943a94a63f0SWarner Losh // cam_periph_lock(periph); 1944a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1945a94a63f0SWarner Losh CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Error!!!\n")); 1946a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1947a94a63f0SWarner Losh cam_release_devq(path, 1948a94a63f0SWarner Losh /*relsim_flags*/0, 1949a94a63f0SWarner Losh /*reduction*/0, 1950a94a63f0SWarner Losh /*timeout*/0, 1951a94a63f0SWarner Losh /*getcount_only*/0); 1952a94a63f0SWarner Losh error = 5; /* EIO */ 1953a94a63f0SWarner Losh } else { 1954a94a63f0SWarner Losh if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1955a94a63f0SWarner Losh panic("REQ_CMP with QFRZN"); 1956a94a63f0SWarner Losh error = 0; 1957a94a63f0SWarner Losh } 1958a94a63f0SWarner Losh 195996e47614SIlya Bakulin card_status = mmcio->cmd.resp[0]; 196096e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE, 196196e47614SIlya Bakulin ("Card status: %08x\n", R1_STATUS(card_status))); 196296e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE, 196396e47614SIlya Bakulin ("Current state: %d\n", R1_CURRENT_STATE(card_status))); 196496e47614SIlya Bakulin 196596e47614SIlya Bakulin /* Process result of switching MMC partitions */ 196696e47614SIlya Bakulin if (softc->state == SDDA_STATE_PART_SWITCH) { 196796e47614SIlya Bakulin CAM_DEBUG(path, CAM_DEBUG_TRACE, 1968fd38fa39SAndriy Gapon ("Completing partition switch to %d\n", 1969fd38fa39SAndriy Gapon softc->part_requested)); 197096e47614SIlya Bakulin softc->outstanding_cmds--; 197196e47614SIlya Bakulin /* Complete partition switch */ 197296e47614SIlya Bakulin softc->state = SDDA_STATE_NORMAL; 197396e47614SIlya Bakulin if (error != MMC_ERR_NONE) { 197496e47614SIlya Bakulin /* TODO: Unpause retune if accessing RPMB */ 197596e47614SIlya Bakulin xpt_release_ccb(done_ccb); 197696e47614SIlya Bakulin xpt_schedule(periph, CAM_PRIORITY_NORMAL); 197796e47614SIlya Bakulin return; 197896e47614SIlya Bakulin } 197996e47614SIlya Bakulin 198096e47614SIlya Bakulin softc->raw_ext_csd[EXT_CSD_PART_CONFIG] = 198196e47614SIlya Bakulin (softc->raw_ext_csd[EXT_CSD_PART_CONFIG] & 198296e47614SIlya Bakulin ~EXT_CSD_PART_CONFIG_ACC_MASK) | softc->part_requested; 198396e47614SIlya Bakulin /* TODO: Unpause retune if accessing RPMB */ 198496e47614SIlya Bakulin softc->part_curr = softc->part_requested; 198596e47614SIlya Bakulin xpt_release_ccb(done_ccb); 198696e47614SIlya Bakulin 198796e47614SIlya Bakulin /* Return to processing BIO requests */ 198896e47614SIlya Bakulin xpt_schedule(periph, CAM_PRIORITY_NORMAL); 198996e47614SIlya Bakulin return; 199096e47614SIlya Bakulin } 1991a94a63f0SWarner Losh 1992a94a63f0SWarner Losh bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1993a94a63f0SWarner Losh bp->bio_error = error; 1994a94a63f0SWarner Losh if (error != 0) { 1995a94a63f0SWarner Losh bp->bio_resid = bp->bio_bcount; 1996a94a63f0SWarner Losh bp->bio_flags |= BIO_ERROR; 1997a94a63f0SWarner Losh } else { 1998a94a63f0SWarner Losh /* XXX: How many bytes remaining? */ 1999a94a63f0SWarner Losh bp->bio_resid = 0; 2000a94a63f0SWarner Losh if (bp->bio_resid > 0) 2001a94a63f0SWarner Losh bp->bio_flags |= BIO_ERROR; 2002a94a63f0SWarner Losh } 2003a94a63f0SWarner Losh 2004a94a63f0SWarner Losh softc->outstanding_cmds--; 2005a94a63f0SWarner Losh xpt_release_ccb(done_ccb); 2006d9a7a61bSWarner Losh /* 200796e47614SIlya Bakulin * Release the periph refcount taken in sddastart() for each CCB. 2008d9a7a61bSWarner Losh */ 200996e47614SIlya Bakulin KASSERT(softc->refcount >= 1, ("sddadone softc %p refcount %d", softc, softc->refcount)); 2010d9a7a61bSWarner Losh softc->refcount--; 2011a94a63f0SWarner Losh biodone(bp); 2012a94a63f0SWarner Losh } 2013a94a63f0SWarner Losh 2014a94a63f0SWarner Losh static int 2015a94a63f0SWarner Losh sddaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2016a94a63f0SWarner Losh { 2017553484aeSWarner Losh return(cam_periph_error(ccb, cam_flags, sense_flags)); 2018a94a63f0SWarner Losh } 2019a94a63f0SWarner Losh #endif /* _KERNEL */ 2020