xref: /freebsd/sys/cam/mmc/mmc_sim.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
34 
35 #include <cam/cam.h>
36 #include <cam/cam_ccb.h>
37 #include <cam/cam_debug.h>
38 #include <cam/cam_sim.h>
39 #include <cam/cam_xpt_sim.h>
40 #include <cam/mmc/mmc_sim.h>
41 
42 #include "mmc_sim_if.h"
43 
44 static void
45 mmc_cam_default_poll(struct cam_sim *sim)
46 {
47 	struct mmc_sim *mmc_sim;
48 
49 	mmc_sim = cam_sim_softc(sim);
50 	MMC_SIM_CAM_POLL(mmc_sim->dev);
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 	mtx_assert(&mmc_sim->mtx, MA_OWNED);
101 
102 	if (mmc_sim->ccb != NULL) {
103 		ccb->ccb_h.status = CAM_BUSY;
104 		xpt_done(ccb);
105 		return;
106 	}
107 
108 	switch (ccb->ccb_h.func_code) {
109 	case XPT_PATH_INQ:
110 		rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc);
111 		if (rv != 0) {
112 			ccb->ccb_h.status = CAM_REQ_INVALID;
113 		} else {
114 			mmc_path_inq(&ccb->cpi, "Deglitch Networks",
115 			    sim, mmc.host_max_data);
116 		}
117 		break;
118 	case XPT_GET_TRAN_SETTINGS:
119 	{
120 		struct ccb_trans_settings *cts = &ccb->cts;
121 
122 		rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
123 		if (rv != 0)
124 			ccb->ccb_h.status = CAM_REQ_INVALID;
125 		else {
126 			cts->protocol = PROTO_MMCSD;
127 			cts->protocol_version = 1;
128 			cts->transport = XPORT_MMCSD;
129 			cts->transport_version = 1;
130 			cts->xport_specific.valid = 0;
131 			ccb->ccb_h.status = CAM_REQ_CMP;
132 		}
133 		break;
134 	}
135 	case XPT_MMC_GET_TRAN_SETTINGS:
136 	{
137 		ccb->ccb_h.status = CAM_SIM_QUEUED;
138 		mmc_sim->ccb = ccb;
139 		taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
140 		return;
141 		/* NOTREACHED */
142 		break;
143 	}
144 	case XPT_SET_TRAN_SETTINGS:
145 	{
146 		struct ccb_trans_settings *cts = &ccb->cts;
147 
148 		rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
149 		if (rv != 0)
150 			ccb->ccb_h.status = CAM_REQ_INVALID;
151 		else
152 			ccb->ccb_h.status = CAM_REQ_CMP;
153 		break;
154 	}
155 	case XPT_MMC_SET_TRAN_SETTINGS:
156 	{
157 		ccb->ccb_h.status = CAM_SIM_QUEUED;
158 		mmc_sim->ccb = ccb;
159 		taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
160 		return;
161 		/* NOTREACHED */
162 		break;
163 	}
164 	case XPT_RESET_BUS:
165 		ccb->ccb_h.status = CAM_REQ_CMP;
166 		break;
167 	case XPT_MMC_IO:
168 	{
169 		rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb);
170 		if (rv != 0)
171 			ccb->ccb_h.status = CAM_SIM_QUEUED;
172 		return;
173 		/* NOTREACHED */
174 		break;
175 	}
176 	default:
177 		ccb->ccb_h.status = CAM_REQ_INVALID;
178 		break;
179 	}
180 	xpt_done(ccb);
181 	return;
182 }
183 
184 int
185 mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim)
186 {
187 	kobjop_desc_t kobj_desc;
188 	kobj_method_t *kobj_method;
189 
190 	mmc_sim->dev = dev;
191 
192 	if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
193 		goto fail;
194 	}
195 
196 	snprintf(mmc_sim->name, sizeof(mmc_sim->name), "%s_sim", name);
197 	mtx_init(&mmc_sim->mtx, mmc_sim->name, NULL, MTX_DEF);
198 
199 	/* Provide sim_poll hook only if the device has the poll method. */
200 	kobj_desc = &mmc_sim_cam_poll_desc;
201 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
202 	    kobj_desc);
203 	mmc_sim->sim = cam_sim_alloc(mmc_cam_sim_default_action,
204 	    kobj_method == &kobj_desc->deflt ? NULL : mmc_cam_default_poll,
205 	    mmc_sim->name, mmc_sim, device_get_unit(dev),
206 	    &mmc_sim->mtx, 1, 1, mmc_sim->devq);
207 
208 	if (mmc_sim->sim == NULL) {
209 		cam_simq_free(mmc_sim->devq);
210 		device_printf(dev, "cannot allocate CAM SIM\n");
211 		goto fail;
212 	}
213 
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);
220 		goto fail;
221 	}
222 
223 	mtx_unlock(&mmc_sim->mtx);
224 	TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim);
225 
226 	return (0);
227 
228 fail:
229 	mmc_cam_sim_free(mmc_sim);
230 	return (1);
231 }
232 
233 void
234 mmc_cam_sim_free(struct mmc_sim *mmc_sim)
235 {
236 
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);
242 	}
243 
244 	if (mmc_sim->devq != NULL)
245 		cam_simq_free(mmc_sim->devq);
246 }
247 
248 void
249 mmc_cam_sim_discover(struct mmc_sim *mmc_sim)
250 {
251 
252 	mmccam_start_discovery(mmc_sim->sim);
253 }
254