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 switch (mmc_sim->ccb->ccb_h.func_code) { 67 case XPT_MMC_GET_TRAN_SETTINGS: 68 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 69 if (rv != 0) 70 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID; 71 else 72 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP; 73 break; 74 case XPT_MMC_SET_TRAN_SETTINGS: 75 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 76 if (rv != 0) 77 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID; 78 else 79 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP; 80 break; 81 default: 82 panic("Unsupported ccb func %x\n", mmc_sim->ccb->ccb_h.func_code); 83 break; 84 } 85 86 xpt_done(mmc_sim->ccb); 87 mmc_sim->ccb = NULL; 88 } 89 90 91 static void 92 mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb) 93 { 94 struct mmc_sim *mmc_sim; 95 struct ccb_trans_settings_mmc mmc; 96 int rv; 97 98 mmc_sim = cam_sim_softc(sim); 99 100 if (mmc_sim == NULL) { 101 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 102 xpt_done(ccb); 103 return; 104 } 105 106 mtx_assert(&mmc_sim->mtx, MA_OWNED); 107 108 if (mmc_sim->ccb != NULL) { 109 ccb->ccb_h.status = CAM_BUSY; 110 xpt_done(ccb); 111 return; 112 } 113 114 switch (ccb->ccb_h.func_code) { 115 case XPT_PATH_INQ: 116 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc); 117 if (rv != 0) { 118 ccb->ccb_h.status = CAM_REQ_INVALID; 119 } else { 120 mmc_path_inq(&ccb->cpi, "Deglitch Networks", 121 sim, mmc.host_max_data); 122 } 123 break; 124 case XPT_GET_TRAN_SETTINGS: 125 { 126 struct ccb_trans_settings *cts = &ccb->cts; 127 128 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 129 if (rv != 0) 130 ccb->ccb_h.status = CAM_REQ_INVALID; 131 else { 132 cts->protocol = PROTO_MMCSD; 133 cts->protocol_version = 1; 134 cts->transport = XPORT_MMCSD; 135 cts->transport_version = 1; 136 cts->xport_specific.valid = 0; 137 ccb->ccb_h.status = CAM_REQ_CMP; 138 } 139 break; 140 } 141 case XPT_MMC_GET_TRAN_SETTINGS: 142 { 143 ccb->ccb_h.status = CAM_SIM_QUEUED; 144 mmc_sim->ccb = ccb; 145 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task); 146 return; 147 /* NOTREACHED */ 148 break; 149 } 150 case XPT_SET_TRAN_SETTINGS: 151 { 152 struct ccb_trans_settings *cts = &ccb->cts; 153 154 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc); 155 if (rv != 0) 156 ccb->ccb_h.status = CAM_REQ_INVALID; 157 else 158 ccb->ccb_h.status = CAM_REQ_CMP; 159 break; 160 } 161 case XPT_MMC_SET_TRAN_SETTINGS: 162 { 163 ccb->ccb_h.status = CAM_SIM_QUEUED; 164 mmc_sim->ccb = ccb; 165 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task); 166 return; 167 /* NOTREACHED */ 168 break; 169 } 170 case XPT_RESET_BUS: 171 ccb->ccb_h.status = CAM_REQ_CMP; 172 break; 173 case XPT_MMC_IO: 174 { 175 ccb->ccb_h.status = CAM_REQ_INVALID; 176 rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb); 177 if (rv != 0) 178 ccb->ccb_h.status = CAM_SIM_QUEUED; 179 return; 180 /* NOTREACHED */ 181 break; 182 } 183 default: 184 ccb->ccb_h.status = CAM_REQ_INVALID; 185 break; 186 } 187 xpt_done(ccb); 188 return; 189 } 190 191 int 192 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim) 193 { 194 char sim_name[64], mtx_name[64]; 195 196 mmc_sim->dev = dev; 197 198 if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) { 199 goto fail; 200 } 201 202 snprintf(sim_name, sizeof(sim_name), "%s_sim", name); 203 snprintf(mtx_name, sizeof(mtx_name), "%s_mtx", name); 204 205 mtx_init(&mmc_sim->mtx, sim_name, NULL, MTX_DEF); 206 mmc_sim->sim = cam_sim_alloc(mmc_cam_sim_default_action, 207 mmc_cam_default_poll, 208 name, mmc_sim, device_get_unit(dev), 209 &mmc_sim->mtx, 1, 1, mmc_sim->devq); 210 211 if (mmc_sim->sim == NULL) { 212 cam_simq_free(mmc_sim->devq); 213 device_printf(dev, "cannot allocate CAM SIM\n"); 214 goto fail; 215 } 216 217 mtx_lock(&mmc_sim->mtx); 218 if (xpt_bus_register(mmc_sim->sim, dev, 0) != 0) { 219 device_printf(dev, "cannot register SCSI pass-through bus\n"); 220 cam_sim_free(mmc_sim->sim, FALSE); 221 cam_simq_free(mmc_sim->devq); 222 mtx_unlock(&mmc_sim->mtx); 223 goto fail; 224 } 225 226 mtx_unlock(&mmc_sim->mtx); 227 TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim); 228 229 return (0); 230 231 fail: 232 mmc_cam_sim_free(mmc_sim); 233 return (1); 234 } 235 236 void 237 mmc_cam_sim_free(struct mmc_sim *mmc_sim) 238 { 239 240 if (mmc_sim->sim != NULL) { 241 mtx_lock(&mmc_sim->mtx); 242 xpt_bus_deregister(cam_sim_path(mmc_sim->sim)); 243 cam_sim_free(mmc_sim->sim, FALSE); 244 mtx_unlock(&mmc_sim->mtx); 245 } 246 247 if (mmc_sim->devq != NULL) 248 cam_simq_free(mmc_sim->devq); 249 } 250 251 void 252 mmc_cam_sim_discover(struct mmc_sim *mmc_sim) 253 { 254 255 mmccam_start_discovery(mmc_sim->sim); 256 } 257