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)
49 struct mmc_sim *mmc_sim;
51 mmc_sim = cam_sim_softc(sim);
52 MMC_SIM_CAM_POLL(mmc_sim->dev);
56 mmc_sim_task(void *arg, int pending)
58 struct mmc_sim *mmc_sim;
59 struct ccb_trans_settings *cts;
64 if (mmc_sim->ccb == NULL)
67 cts = &mmc_sim->ccb->cts;
68 switch (mmc_sim->ccb->ccb_h.func_code) {
69 case XPT_MMC_GET_TRAN_SETTINGS:
70 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
72 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
74 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
76 case XPT_MMC_SET_TRAN_SETTINGS:
77 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
79 mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
81 mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
84 panic("Unsupported ccb func %x\n", mmc_sim->ccb->ccb_h.func_code);
88 xpt_done(mmc_sim->ccb);
94 mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb)
96 struct mmc_sim *mmc_sim;
97 struct ccb_trans_settings_mmc mmc;
100 mmc_sim = cam_sim_softc(sim);
102 mtx_assert(&mmc_sim->mtx, MA_OWNED);
104 if (mmc_sim->ccb != NULL) {
105 ccb->ccb_h.status = CAM_BUSY;
110 switch (ccb->ccb_h.func_code) {
112 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc);
114 ccb->ccb_h.status = CAM_REQ_INVALID;
116 mmc_path_inq(&ccb->cpi, "Deglitch Networks",
117 sim, mmc.host_max_data);
120 case XPT_GET_TRAN_SETTINGS:
122 struct ccb_trans_settings *cts = &ccb->cts;
124 rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
126 ccb->ccb_h.status = CAM_REQ_INVALID;
128 cts->protocol = PROTO_MMCSD;
129 cts->protocol_version = 1;
130 cts->transport = XPORT_MMCSD;
131 cts->transport_version = 1;
132 cts->xport_specific.valid = 0;
133 ccb->ccb_h.status = CAM_REQ_CMP;
137 case XPT_MMC_GET_TRAN_SETTINGS:
139 ccb->ccb_h.status = CAM_SIM_QUEUED;
141 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
146 case XPT_SET_TRAN_SETTINGS:
148 struct ccb_trans_settings *cts = &ccb->cts;
150 rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
152 ccb->ccb_h.status = CAM_REQ_INVALID;
154 ccb->ccb_h.status = CAM_REQ_CMP;
157 case XPT_MMC_SET_TRAN_SETTINGS:
159 ccb->ccb_h.status = CAM_SIM_QUEUED;
161 taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
167 ccb->ccb_h.status = CAM_REQ_CMP;
171 rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb);
173 ccb->ccb_h.status = CAM_SIM_QUEUED;
179 ccb->ccb_h.status = CAM_REQ_INVALID;
187 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim)
189 kobjop_desc_t kobj_desc;
190 kobj_method_t *kobj_method;
194 if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
198 snprintf(mmc_sim->name, sizeof(mmc_sim->name), "%s_sim", name);
199 mtx_init(&mmc_sim->mtx, mmc_sim->name, NULL, MTX_DEF);
201 /* Provide sim_poll hook only if the device has the poll method. */
202 kobj_desc = &mmc_sim_cam_poll_desc;
203 kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
205 mmc_sim->sim = cam_sim_alloc(mmc_cam_sim_default_action,
206 kobj_method == &kobj_desc->deflt ? NULL : mmc_cam_default_poll,
207 mmc_sim->name, mmc_sim, device_get_unit(dev),
208 &mmc_sim->mtx, 1, 1, mmc_sim->devq);
210 if (mmc_sim->sim == NULL) {
211 cam_simq_free(mmc_sim->devq);
212 device_printf(dev, "cannot allocate CAM SIM\n");
216 mtx_lock(&mmc_sim->mtx);
217 if (xpt_bus_register(mmc_sim->sim, dev, 0) != 0) {
218 device_printf(dev, "cannot register SCSI pass-through bus\n");
219 cam_sim_free(mmc_sim->sim, FALSE);
220 cam_simq_free(mmc_sim->devq);
221 mtx_unlock(&mmc_sim->mtx);
225 mtx_unlock(&mmc_sim->mtx);
226 TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim);
231 mmc_cam_sim_free(mmc_sim);
236 mmc_cam_sim_free(struct mmc_sim *mmc_sim)
239 if (mmc_sim->sim != NULL) {
240 mtx_lock(&mmc_sim->mtx);
241 xpt_bus_deregister(cam_sim_path(mmc_sim->sim));
242 cam_sim_free(mmc_sim->sim, FALSE);
243 mtx_unlock(&mmc_sim->mtx);
246 if (mmc_sim->devq != NULL)
247 cam_simq_free(mmc_sim->devq);
251 mmc_cam_sim_discover(struct mmc_sim *mmc_sim)
254 mmccam_start_discovery(mmc_sim->sim);