xref: /freebsd/sys/cam/mmc/mmc_sim.c (revision d93b4d32034df7cd70e80b496e8fe8c1bc57c629)
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 
195 	mmc_sim->dev = dev;
196 
197 	if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
198 		goto fail;
199 	}
200 
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);
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