147bde792SEmmanuel Vadot /*-
2af2253f6SEmmanuel Vadot * Copyright (c) 2020-2021 Emmanuel Vadot <manu@FreeBSD.org>
347bde792SEmmanuel Vadot *
447bde792SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
547bde792SEmmanuel Vadot * modification, are permitted provided that the following conditions
647bde792SEmmanuel Vadot * are met:
747bde792SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
847bde792SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
947bde792SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
1047bde792SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
1147bde792SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
1247bde792SEmmanuel Vadot *
1347bde792SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1447bde792SEmmanuel Vadot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1547bde792SEmmanuel Vadot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1647bde792SEmmanuel Vadot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1747bde792SEmmanuel Vadot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
1847bde792SEmmanuel Vadot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1947bde792SEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2047bde792SEmmanuel Vadot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2147bde792SEmmanuel Vadot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2247bde792SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2347bde792SEmmanuel Vadot * SUCH DAMAGE.
2447bde792SEmmanuel Vadot */
2547bde792SEmmanuel Vadot
2647bde792SEmmanuel Vadot #include <sys/param.h>
2747bde792SEmmanuel Vadot #include <sys/kernel.h>
2847bde792SEmmanuel Vadot #include <sys/systm.h>
2947bde792SEmmanuel Vadot #include <sys/malloc.h>
3047bde792SEmmanuel Vadot #include <sys/mutex.h>
3147bde792SEmmanuel Vadot
3247bde792SEmmanuel Vadot #include <cam/cam.h>
3347bde792SEmmanuel Vadot #include <cam/cam_ccb.h>
3447bde792SEmmanuel Vadot #include <cam/cam_debug.h>
3547bde792SEmmanuel Vadot #include <cam/cam_sim.h>
3647bde792SEmmanuel Vadot #include <cam/cam_xpt_sim.h>
3747bde792SEmmanuel Vadot #include <cam/mmc/mmc_sim.h>
3847bde792SEmmanuel Vadot
3947bde792SEmmanuel Vadot #include "mmc_sim_if.h"
4047bde792SEmmanuel Vadot
4147bde792SEmmanuel Vadot static void
mmc_cam_default_poll(struct cam_sim * sim)4247bde792SEmmanuel Vadot mmc_cam_default_poll(struct cam_sim *sim)
4347bde792SEmmanuel Vadot {
44*44682688SAndriy Gapon struct mmc_sim *mmc_sim;
4547bde792SEmmanuel Vadot
46*44682688SAndriy Gapon mmc_sim = cam_sim_softc(sim);
47*44682688SAndriy Gapon MMC_SIM_CAM_POLL(mmc_sim->dev);
4847bde792SEmmanuel Vadot }
4947bde792SEmmanuel Vadot
5047bde792SEmmanuel Vadot static void
mmc_sim_task(void * arg,int pending)51af2253f6SEmmanuel Vadot mmc_sim_task(void *arg, int pending)
52af2253f6SEmmanuel Vadot {
53af2253f6SEmmanuel Vadot struct mmc_sim *mmc_sim;
54af2253f6SEmmanuel Vadot struct ccb_trans_settings *cts;
55af2253f6SEmmanuel Vadot int rv;
56af2253f6SEmmanuel Vadot
57af2253f6SEmmanuel Vadot mmc_sim = arg;
58af2253f6SEmmanuel Vadot
59af2253f6SEmmanuel Vadot if (mmc_sim->ccb == NULL)
60af2253f6SEmmanuel Vadot return;
61af2253f6SEmmanuel Vadot
62af2253f6SEmmanuel Vadot cts = &mmc_sim->ccb->cts;
633386347fSEmmanuel Vadot switch (mmc_sim->ccb->ccb_h.func_code) {
643386347fSEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS:
653386347fSEmmanuel Vadot rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
663386347fSEmmanuel Vadot if (rv != 0)
673386347fSEmmanuel Vadot mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
683386347fSEmmanuel Vadot else
693386347fSEmmanuel Vadot mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
703386347fSEmmanuel Vadot break;
713386347fSEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS:
72af2253f6SEmmanuel Vadot rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
73af2253f6SEmmanuel Vadot if (rv != 0)
74af2253f6SEmmanuel Vadot mmc_sim->ccb->ccb_h.status = CAM_REQ_INVALID;
75af2253f6SEmmanuel Vadot else
76af2253f6SEmmanuel Vadot mmc_sim->ccb->ccb_h.status = CAM_REQ_CMP;
773386347fSEmmanuel Vadot break;
783386347fSEmmanuel Vadot default:
793386347fSEmmanuel Vadot panic("Unsupported ccb func %x\n", mmc_sim->ccb->ccb_h.func_code);
803386347fSEmmanuel Vadot break;
813386347fSEmmanuel Vadot }
82af2253f6SEmmanuel Vadot
83af2253f6SEmmanuel Vadot xpt_done(mmc_sim->ccb);
84af2253f6SEmmanuel Vadot mmc_sim->ccb = NULL;
85af2253f6SEmmanuel Vadot }
86af2253f6SEmmanuel Vadot
87af2253f6SEmmanuel Vadot
88af2253f6SEmmanuel Vadot static void
mmc_cam_sim_default_action(struct cam_sim * sim,union ccb * ccb)8947bde792SEmmanuel Vadot mmc_cam_sim_default_action(struct cam_sim *sim, union ccb *ccb)
9047bde792SEmmanuel Vadot {
9147bde792SEmmanuel Vadot struct mmc_sim *mmc_sim;
9247bde792SEmmanuel Vadot struct ccb_trans_settings_mmc mmc;
9347bde792SEmmanuel Vadot int rv;
9447bde792SEmmanuel Vadot
9547bde792SEmmanuel Vadot mmc_sim = cam_sim_softc(sim);
9647bde792SEmmanuel Vadot
9747bde792SEmmanuel Vadot mtx_assert(&mmc_sim->mtx, MA_OWNED);
9847bde792SEmmanuel Vadot
99af2253f6SEmmanuel Vadot if (mmc_sim->ccb != NULL) {
100af2253f6SEmmanuel Vadot ccb->ccb_h.status = CAM_BUSY;
101af2253f6SEmmanuel Vadot xpt_done(ccb);
102af2253f6SEmmanuel Vadot return;
103af2253f6SEmmanuel Vadot }
104af2253f6SEmmanuel Vadot
10547bde792SEmmanuel Vadot switch (ccb->ccb_h.func_code) {
10647bde792SEmmanuel Vadot case XPT_PATH_INQ:
10747bde792SEmmanuel Vadot rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &mmc);
10847bde792SEmmanuel Vadot if (rv != 0) {
10947bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_INVALID;
11047bde792SEmmanuel Vadot } else {
11147bde792SEmmanuel Vadot mmc_path_inq(&ccb->cpi, "Deglitch Networks",
11247bde792SEmmanuel Vadot sim, mmc.host_max_data);
11347bde792SEmmanuel Vadot }
11447bde792SEmmanuel Vadot break;
11547bde792SEmmanuel Vadot case XPT_GET_TRAN_SETTINGS:
11647bde792SEmmanuel Vadot {
11747bde792SEmmanuel Vadot struct ccb_trans_settings *cts = &ccb->cts;
11847bde792SEmmanuel Vadot
11947bde792SEmmanuel Vadot rv = MMC_SIM_GET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
12047bde792SEmmanuel Vadot if (rv != 0)
12147bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_INVALID;
12247bde792SEmmanuel Vadot else {
12347bde792SEmmanuel Vadot cts->protocol = PROTO_MMCSD;
12447bde792SEmmanuel Vadot cts->protocol_version = 1;
12547bde792SEmmanuel Vadot cts->transport = XPORT_MMCSD;
12647bde792SEmmanuel Vadot cts->transport_version = 1;
12747bde792SEmmanuel Vadot cts->xport_specific.valid = 0;
12847bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_CMP;
12947bde792SEmmanuel Vadot }
13047bde792SEmmanuel Vadot break;
13147bde792SEmmanuel Vadot }
1323386347fSEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS:
1333386347fSEmmanuel Vadot {
1343386347fSEmmanuel Vadot ccb->ccb_h.status = CAM_SIM_QUEUED;
1353386347fSEmmanuel Vadot mmc_sim->ccb = ccb;
1363386347fSEmmanuel Vadot taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
1373386347fSEmmanuel Vadot return;
1383386347fSEmmanuel Vadot /* NOTREACHED */
1393386347fSEmmanuel Vadot break;
1403386347fSEmmanuel Vadot }
14147bde792SEmmanuel Vadot case XPT_SET_TRAN_SETTINGS:
14247bde792SEmmanuel Vadot {
14347bde792SEmmanuel Vadot struct ccb_trans_settings *cts = &ccb->cts;
14447bde792SEmmanuel Vadot
14547bde792SEmmanuel Vadot rv = MMC_SIM_SET_TRAN_SETTINGS(mmc_sim->dev, &cts->proto_specific.mmc);
14647bde792SEmmanuel Vadot if (rv != 0)
14747bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_INVALID;
14847bde792SEmmanuel Vadot else
14947bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_CMP;
15047bde792SEmmanuel Vadot break;
15147bde792SEmmanuel Vadot }
152af2253f6SEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS:
153af2253f6SEmmanuel Vadot {
154af2253f6SEmmanuel Vadot ccb->ccb_h.status = CAM_SIM_QUEUED;
155af2253f6SEmmanuel Vadot mmc_sim->ccb = ccb;
156af2253f6SEmmanuel Vadot taskqueue_enqueue(taskqueue_thread, &mmc_sim->sim_task);
157af2253f6SEmmanuel Vadot return;
158af2253f6SEmmanuel Vadot /* NOTREACHED */
159af2253f6SEmmanuel Vadot break;
160af2253f6SEmmanuel Vadot }
16147bde792SEmmanuel Vadot case XPT_RESET_BUS:
16247bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_CMP;
16347bde792SEmmanuel Vadot break;
16447bde792SEmmanuel Vadot case XPT_MMC_IO:
16547bde792SEmmanuel Vadot {
16647bde792SEmmanuel Vadot rv = MMC_SIM_CAM_REQUEST(mmc_sim->dev, ccb);
16747bde792SEmmanuel Vadot if (rv != 0)
168af2253f6SEmmanuel Vadot ccb->ccb_h.status = CAM_SIM_QUEUED;
16947bde792SEmmanuel Vadot return;
17047bde792SEmmanuel Vadot /* NOTREACHED */
17147bde792SEmmanuel Vadot break;
17247bde792SEmmanuel Vadot }
17347bde792SEmmanuel Vadot default:
17447bde792SEmmanuel Vadot ccb->ccb_h.status = CAM_REQ_INVALID;
17547bde792SEmmanuel Vadot break;
17647bde792SEmmanuel Vadot }
17747bde792SEmmanuel Vadot xpt_done(ccb);
17847bde792SEmmanuel Vadot return;
17947bde792SEmmanuel Vadot }
18047bde792SEmmanuel Vadot
18147bde792SEmmanuel Vadot int
mmc_cam_sim_alloc(device_t dev,const char * name,struct mmc_sim * mmc_sim)18247bde792SEmmanuel Vadot mmc_cam_sim_alloc(device_t dev, const char *name, struct mmc_sim *mmc_sim)
18347bde792SEmmanuel Vadot {
184*44682688SAndriy Gapon kobjop_desc_t kobj_desc;
185*44682688SAndriy Gapon kobj_method_t *kobj_method;
18647bde792SEmmanuel Vadot
18747bde792SEmmanuel Vadot mmc_sim->dev = dev;
18847bde792SEmmanuel Vadot
18947bde792SEmmanuel Vadot if ((mmc_sim->devq = cam_simq_alloc(1)) == NULL) {
19047bde792SEmmanuel Vadot goto fail;
19147bde792SEmmanuel Vadot }
19247bde792SEmmanuel Vadot
19318679ab1SAndriy Gapon snprintf(mmc_sim->name, sizeof(mmc_sim->name), "%s_sim", name);
19418679ab1SAndriy Gapon mtx_init(&mmc_sim->mtx, mmc_sim->name, NULL, MTX_DEF);
195*44682688SAndriy Gapon
196*44682688SAndriy Gapon /* Provide sim_poll hook only if the device has the poll method. */
197*44682688SAndriy Gapon kobj_desc = &mmc_sim_cam_poll_desc;
198*44682688SAndriy Gapon kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
199*44682688SAndriy Gapon kobj_desc);
200bd69852bSWarner Losh mmc_sim->sim = cam_sim_alloc(mmc_cam_sim_default_action,
201*44682688SAndriy Gapon kobj_method == &kobj_desc->deflt ? NULL : mmc_cam_default_poll,
20218679ab1SAndriy Gapon mmc_sim->name, mmc_sim, device_get_unit(dev),
20347bde792SEmmanuel Vadot &mmc_sim->mtx, 1, 1, mmc_sim->devq);
20447bde792SEmmanuel Vadot
20547bde792SEmmanuel Vadot if (mmc_sim->sim == NULL) {
20647bde792SEmmanuel Vadot cam_simq_free(mmc_sim->devq);
20747bde792SEmmanuel Vadot device_printf(dev, "cannot allocate CAM SIM\n");
20847bde792SEmmanuel Vadot goto fail;
20947bde792SEmmanuel Vadot }
21047bde792SEmmanuel Vadot
21147bde792SEmmanuel Vadot mtx_lock(&mmc_sim->mtx);
21247bde792SEmmanuel Vadot if (xpt_bus_register(mmc_sim->sim, dev, 0) != 0) {
21347bde792SEmmanuel Vadot device_printf(dev, "cannot register SCSI pass-through bus\n");
21447bde792SEmmanuel Vadot cam_sim_free(mmc_sim->sim, FALSE);
21547bde792SEmmanuel Vadot cam_simq_free(mmc_sim->devq);
21647bde792SEmmanuel Vadot mtx_unlock(&mmc_sim->mtx);
21747bde792SEmmanuel Vadot goto fail;
21847bde792SEmmanuel Vadot }
21947bde792SEmmanuel Vadot
22047bde792SEmmanuel Vadot mtx_unlock(&mmc_sim->mtx);
221af2253f6SEmmanuel Vadot TASK_INIT(&mmc_sim->sim_task, 0, mmc_sim_task, mmc_sim);
22247bde792SEmmanuel Vadot
22347bde792SEmmanuel Vadot return (0);
22447bde792SEmmanuel Vadot
22547bde792SEmmanuel Vadot fail:
22647bde792SEmmanuel Vadot mmc_cam_sim_free(mmc_sim);
22747bde792SEmmanuel Vadot return (1);
22847bde792SEmmanuel Vadot }
22947bde792SEmmanuel Vadot
23047bde792SEmmanuel Vadot void
mmc_cam_sim_free(struct mmc_sim * mmc_sim)23147bde792SEmmanuel Vadot mmc_cam_sim_free(struct mmc_sim *mmc_sim)
23247bde792SEmmanuel Vadot {
23347bde792SEmmanuel Vadot
23447bde792SEmmanuel Vadot if (mmc_sim->sim != NULL) {
23547bde792SEmmanuel Vadot mtx_lock(&mmc_sim->mtx);
23647bde792SEmmanuel Vadot xpt_bus_deregister(cam_sim_path(mmc_sim->sim));
23747bde792SEmmanuel Vadot cam_sim_free(mmc_sim->sim, FALSE);
23847bde792SEmmanuel Vadot mtx_unlock(&mmc_sim->mtx);
23947bde792SEmmanuel Vadot }
24047bde792SEmmanuel Vadot
24147bde792SEmmanuel Vadot if (mmc_sim->devq != NULL)
24247bde792SEmmanuel Vadot cam_simq_free(mmc_sim->devq);
24347bde792SEmmanuel Vadot }
24447bde792SEmmanuel Vadot
24547bde792SEmmanuel Vadot void
mmc_cam_sim_discover(struct mmc_sim * mmc_sim)24647bde792SEmmanuel Vadot mmc_cam_sim_discover(struct mmc_sim *mmc_sim)
24747bde792SEmmanuel Vadot {
24847bde792SEmmanuel Vadot
24947bde792SEmmanuel Vadot mmccam_start_discovery(mmc_sim->sim);
25047bde792SEmmanuel Vadot }
251