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 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);
70 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
72 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
74 case XPT_MMC_SET_TRAN_SETTINGS:
75 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
77 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
79 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
82 panic("Unsupported ccb func %x\n", mmc_sim->ccb->ccb_h.func_code);
86 xpt_done(mmc_sim->ccb);
92 mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb)
94 struct mmc_sim *mmc_sim;
95 struct ccb_trans_settings_mmc mmc;
98 mmc_sim = cam_sim_softc(sim);
100 if (mmc_sim == NULL) {
101 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
106 mtx_assert(&mmc_sim->mtx, MA_OWNED);
108 if (mmc_sim->ccb != NULL) {
109 ccb->ccb_h.status = CAM_BUSY;
114 switch (ccb->ccb_h.func_code) {
116 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc);
118 ccb->ccb_h.status = CAM_REQ_INVALID;
120 mmc_path_inq(&ccb->cpi, "Deglitch Networks",
121 sim, mmc.host_max_data);
124 case XPT_GET_TRAN_SETTINGS:
126 struct ccb_trans_settings *cts = &ccb->cts;
128 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
130 ccb->ccb_h.status = CAM_REQ_INVALID;
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;
141 case XPT_MMC_GET_TRAN_SETTINGS:
143 ccb->ccb_h.status = CAM_SIM_QUEUED;
145 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
150 case XPT_SET_TRAN_SETTINGS:
152 struct ccb_trans_settings *cts = &ccb->cts;
154 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
156 ccb->ccb_h.status = CAM_REQ_INVALID;
158 ccb->ccb_h.status = CAM_REQ_CMP;
161 case XPT_MMC_SET_TRAN_SETTINGS:
163 ccb->ccb_h.status = CAM_SIM_QUEUED;
165 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
171 ccb->ccb_h.status = CAM_REQ_CMP;
175 ccb->ccb_h.status = CAM_REQ_INVALID;
176 rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb);
178 ccb->ccb_h.status = CAM_SIM_QUEUED;
184 ccb->ccb_h.status = CAM_REQ_INVALID;
192 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim)
197 if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
201 snprintf(mmc_sim->name, sizeof(mmc_sim->name), "%s_sim", name);
202 mtx_init(&mmc_sim->mtx, mmc_sim->name, NULL, MTX_DEF);
203 mmc_sim->sim = cam_sim_alloc(mmc_cam_sim_default_action,
204 mmc_cam_default_poll,
205 mmc_sim->name, mmc_sim, device_get_unit(dev),
206 &mmc_sim->mtx, 1, 1, mmc_sim->devq);
208 if (mmc_sim->sim == NULL) {
209 cam_simq_free(mmc_sim->devq);
210 device_printf(dev, "cannot allocate CAM SIM\n");
214 mtx_lock(&mmc_sim->mtx);
215 if (xpt_bus_register(mmc_sim->sim, dev, 0) != 0) {
216 device_printf(dev, "cannot register SCSI pass-through bus\n");
217 cam_sim_free(mmc_sim->sim, FALSE);
218 cam_simq_free(mmc_sim->devq);
219 mtx_unlock(&mmc_sim->mtx);
223 mtx_unlock(&mmc_sim->mtx);
224 TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim);
229 mmc_cam_sim_free(mmc_sim);
234 mmc_cam_sim_free(struct mmc_sim *mmc_sim)
237 if (mmc_sim->sim != NULL) {
238 mtx_lock(&mmc_sim->mtx);
239 xpt_bus_deregister(cam_sim_path(mmc_sim->sim));
240 cam_sim_free(mmc_sim->sim, FALSE);
241 mtx_unlock(&mmc_sim->mtx);
244 if (mmc_sim->devq != NULL)
245 cam_simq_free(mmc_sim->devq);
249 mmc_cam_sim_discover(struct mmc_sim *mmc_sim)
252 mmccam_start_discovery(mmc_sim->sim);