1 /*- 2 * Copyright (c) 2020-2021 Emmanuel Vadot <manu@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/mutex.h> 36 37 #include <cam/cam.h> 38 #include <cam/cam_ccb.h> 39 #include <cam/cam_debug.h> 40 #include <cam/cam_sim.h> 41 #include <cam/cam_xpt_sim.h> 42 #include <cam/mmc/mmc_sim.h> 43 44 #include "mmc_sim_if.h" 45 46 static void 47 mmc_cam_default_poll(struct cam_sim *sim) 48 { 49 50 return; 51 } 52 53 static void 54 mmc_sim_task(void *arg, int pending) 55 { 56 struct mmc_sim *mmc_sim; 57 struct ccb_trans_settings *cts; 58 int rv; 59 60 mmc_sim = arg; 61 62 if (mmc_sim->ccb == NULL) 63 return; 64 65 cts = &mmc_sim->ccb->cts; 66 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 67 if (rv != 0) 68 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID; 69 else 70 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP; 71 72 xpt_done(mmc_sim->ccb); 73 mmc_sim->ccb = NULL; 74 } 75 76 77 static void 78 mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb) 79 { 80 struct mmc_sim *mmc_sim; 81 struct ccb_trans_settings_mmc mmc; 82 int rv; 83 84 mmc_sim = cam_sim_softc(sim); 85 86 if (mmc_sim == NULL) { 87 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 88 xpt_done(ccb); 89 return; 90 } 91 92 mtx_assert(&mmc_sim->mtx, MA_OWNED); 93 94 if (mmc_sim->ccb != NULL) { 95 ccb->ccb_h.status = CAM_BUSY; 96 xpt_done(ccb); 97 return; 98 } 99 100 switch (ccb->ccb_h.func_code) { 101 case XPT_PATH_INQ: 102 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc); 103 if (rv != 0) { 104 ccb->ccb_h.status = CAM_REQ_INVALID; 105 } else { 106 mmc_path_inq(&ccb->cpi, "Deglitch Networks", 107 sim, mmc.host_max_data); 108 } 109 break; 110 case XPT_GET_TRAN_SETTINGS: 111 case XPT_MMC_GET_TRAN_SETTINGS: 112 { 113 struct ccb_trans_settings *cts = &ccb->cts; 114 115 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 116 if (rv != 0) 117 ccb->ccb_h.status = CAM_REQ_INVALID; 118 else { 119 cts->protocol = PROTO_MMCSD; 120 cts->protocol_version = 1; 121 cts->transport = XPORT_MMCSD; 122 cts->transport_version = 1; 123 cts->xport_specific.valid = 0; 124 ccb->ccb_h.status = CAM_REQ_CMP; 125 } 126 break; 127 } 128 case XPT_SET_TRAN_SETTINGS: 129 { 130 struct ccb_trans_settings *cts = &ccb->cts; 131 132 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 133 if (rv != 0) 134 ccb->ccb_h.status = CAM_REQ_INVALID; 135 else 136 ccb->ccb_h.status = CAM_REQ_CMP; 137 break; 138 } 139 case XPT_MMC_SET_TRAN_SETTINGS: 140 { 141 ccb->ccb_h.status = CAM_SIM_QUEUED; 142 mmc_sim->ccb = ccb; 143 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task); 144 return; 145 /* NOTREACHED */ 146 break; 147 } 148 case XPT_RESET_BUS: 149 ccb->ccb_h.status = CAM_REQ_CMP; 150 break; 151 case XPT_MMC_IO: 152 { 153 rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb); 154 if (rv != 0) 155 ccb->ccb_h.status = CAM_SIM_QUEUED; 156 else 157 ccb->ccb_h.status = CAM_REQ_INVALID; 158 return; 159 /* NOTREACHED */ 160 break; 161 } 162 default: 163 ccb->ccb_h.status = CAM_REQ_INVALID; 164 break; 165 } 166 xpt_done(ccb); 167 return; 168 } 169 170 int 171 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim) 172 { 173 char sim_name[64], mtx_name[64]; 174 175 mmc_sim->dev = dev; 176 177 if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) { 178 goto fail; 179 } 180 181 snprintf(sim_name, sizeof(sim_name), "%s_sim", name); 182 snprintf(mtx_name, sizeof(mtx_name), "%s_mtx", name); 183 184 mtx_init(&mmc_sim->mtx, sim_name, NULL, MTX_DEF); 185 mmc_sim->sim = cam_sim_alloc_dev(mmc_cam_sim_default_action, 186 mmc_cam_default_poll, 187 name, mmc_sim, dev, 188 &mmc_sim->mtx, 1, 1, mmc_sim->devq); 189 190 if (mmc_sim->sim == NULL) { 191 cam_simq_free(mmc_sim->devq); 192 device_printf(dev, "cannot allocate CAM SIM\n"); 193 goto fail; 194 } 195 196 mtx_lock(&mmc_sim->mtx); 197 if (xpt_bus_register(mmc_sim->sim, dev, 0) != 0) { 198 device_printf(dev, "cannot register SCSI pass-through bus\n"); 199 cam_sim_free(mmc_sim->sim, FALSE); 200 cam_simq_free(mmc_sim->devq); 201 mtx_unlock(&mmc_sim->mtx); 202 goto fail; 203 } 204 205 mtx_unlock(&mmc_sim->mtx); 206 TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim); 207 208 return (0); 209 210 fail: 211 mmc_cam_sim_free(mmc_sim); 212 return (1); 213 } 214 215 void 216 mmc_cam_sim_free(struct mmc_sim *mmc_sim) 217 { 218 219 if (mmc_sim->sim != NULL) { 220 mtx_lock(&mmc_sim->mtx); 221 xpt_bus_deregister(cam_sim_path(mmc_sim->sim)); 222 cam_sim_free(mmc_sim->sim, FALSE); 223 mtx_unlock(&mmc_sim->mtx); 224 } 225 226 if (mmc_sim->devq != NULL) 227 cam_simq_free(mmc_sim->devq); 228 } 229 230 void 231 mmc_cam_sim_discover(struct mmc_sim *mmc_sim) 232 { 233 234 mmccam_start_discovery(mmc_sim->sim); 235 } 236