2 * Copyright (c) 2020-2021 Emmanuel Vadot <manu@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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>
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>
44 #include "mmc_sim_if.h"
47 mmc_cam_default_poll(struct cam_sim *sim)
54 mmc_sim_task(void *arg, int pending)
56 struct mmc_sim *mmc_sim;
57 struct ccb_trans_settings *cts;
62 if (mmc_sim->ccb == NULL)
65 cts = &mmc_sim->ccb->cts;
66 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
68 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
70 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
72 xpt_done(mmc_sim->ccb);
78 mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb)
80 struct mmc_sim *mmc_sim;
81 struct ccb_trans_settings_mmc mmc;
84 mmc_sim = cam_sim_softc(sim);
86 if (mmc_sim == NULL) {
87 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
92 mtx_assert(&mmc_sim->mtx, MA_OWNED);
94 if (mmc_sim->ccb != NULL) {
95 ccb->ccb_h.status = CAM_BUSY;
100 switch (ccb->ccb_h.func_code) {
102 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc);
104 ccb->ccb_h.status = CAM_REQ_INVALID;
106 mmc_path_inq(&ccb->cpi, "Deglitch Networks",
107 sim, mmc.host_max_data);
110 case XPT_GET_TRAN_SETTINGS:
111 case XPT_MMC_GET_TRAN_SETTINGS:
113 struct ccb_trans_settings *cts = &ccb->cts;
115 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
117 ccb->ccb_h.status = CAM_REQ_INVALID;
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;
128 case XPT_SET_TRAN_SETTINGS:
130 struct ccb_trans_settings *cts = &ccb->cts;
132 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
134 ccb->ccb_h.status = CAM_REQ_INVALID;
136 ccb->ccb_h.status = CAM_REQ_CMP;
139 case XPT_MMC_SET_TRAN_SETTINGS:
141 ccb->ccb_h.status = CAM_SIM_QUEUED;
143 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
149 ccb->ccb_h.status = CAM_REQ_CMP;
153 rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb);
155 ccb->ccb_h.status = CAM_SIM_QUEUED;
157 ccb->ccb_h.status = CAM_REQ_INVALID;
163 ccb->ccb_h.status = CAM_REQ_INVALID;
171 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim)
173 char sim_name[64], mtx_name[64];
177 if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
181 snprintf(sim_name, sizeof(sim_name), "%s_sim", name);
182 snprintf(mtx_name, sizeof(mtx_name), "%s_mtx", name);
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,
188 &mmc_sim->mtx, 1, 1, mmc_sim->devq);
190 if (mmc_sim->sim == NULL) {
191 cam_simq_free(mmc_sim->devq);
192 device_printf(dev, "cannot allocate CAM SIM\n");
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);
205 mtx_unlock(&mmc_sim->mtx);
206 TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim);
211 mmc_cam_sim_free(mmc_sim);
216 mmc_cam_sim_free(struct mmc_sim *mmc_sim)
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);
226 if (mmc_sim->devq != NULL)
227 cam_simq_free(mmc_sim->devq);
231 mmc_cam_sim_discover(struct mmc_sim *mmc_sim)
234 mmccam_start_discovery(mmc_sim->sim);