1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs * Common functions for SCSI Interface Modules (SIMs).
38b8a9b1dSJustin T. Gibbs *
44d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
5bec9534dSPedro F. Giffuni *
68b8a9b1dSJustin T. Gibbs * Copyright (c) 1997 Justin T. Gibbs.
78b8a9b1dSJustin T. Gibbs * All rights reserved.
88b8a9b1dSJustin T. Gibbs *
98b8a9b1dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without
108b8a9b1dSJustin T. Gibbs * modification, are permitted provided that the following conditions
118b8a9b1dSJustin T. Gibbs * are met:
128b8a9b1dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright
138b8a9b1dSJustin T. Gibbs * notice, this list of conditions, and the following disclaimer,
148b8a9b1dSJustin T. Gibbs * without modification, immediately at the beginning of the file.
158b8a9b1dSJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products
168b8a9b1dSJustin T. Gibbs * derived from this software without specific prior written permission.
178b8a9b1dSJustin T. Gibbs *
188b8a9b1dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
198b8a9b1dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208b8a9b1dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218b8a9b1dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
228b8a9b1dSJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238b8a9b1dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
248b8a9b1dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
258b8a9b1dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
268b8a9b1dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
278b8a9b1dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
288b8a9b1dSJustin T. Gibbs * SUCH DAMAGE.
298b8a9b1dSJustin T. Gibbs */
308b8a9b1dSJustin T. Gibbs
318b8a9b1dSJustin T. Gibbs #include <sys/param.h>
3200e7a553SWarner Losh #include <sys/bus.h>
33362abc44STai-hwa Liang #include <sys/kernel.h>
342b83592fSScott Long #include <sys/lock.h>
3500e7a553SWarner Losh #include <sys/malloc.h>
362b83592fSScott Long #include <sys/mutex.h>
378b8a9b1dSJustin T. Gibbs
388b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
398b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
408b8a9b1dSJustin T. Gibbs #include <cam/cam_queue.h>
4100e7a553SWarner Losh #include <cam/cam_sim.h>
42cd05a36eSAlexander Motin #include <cam/cam_xpt.h>
438b8a9b1dSJustin T. Gibbs
44*7af2f2c8SWarner Losh #define CAM_PATH_ANY (uint32_t)-1
458b8a9b1dSJustin T. Gibbs
46d745c852SEd Schouten static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
47362abc44STai-hwa Liang
48401ed17aSAlexander Motin static struct mtx cam_sim_free_mtx;
49401ed17aSAlexander Motin MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
50401ed17aSAlexander Motin
518b8a9b1dSJustin T. Gibbs struct cam_devq *
cam_simq_alloc(uint32_t max_sim_transactions)52*7af2f2c8SWarner Losh cam_simq_alloc(uint32_t max_sim_transactions)
538b8a9b1dSJustin T. Gibbs {
548b8a9b1dSJustin T. Gibbs return (cam_devq_alloc(/*size*/0, max_sim_transactions));
558b8a9b1dSJustin T. Gibbs }
568b8a9b1dSJustin T. Gibbs
578b8a9b1dSJustin T. Gibbs void
cam_simq_free(struct cam_devq * devq)588b8a9b1dSJustin T. Gibbs cam_simq_free(struct cam_devq *devq)
598b8a9b1dSJustin T. Gibbs {
608b8a9b1dSJustin T. Gibbs cam_devq_free(devq);
618b8a9b1dSJustin T. Gibbs }
628b8a9b1dSJustin T. Gibbs
63cb588059SWarner Losh
64cb588059SWarner Losh
65cb588059SWarner Losh /**
66cb588059SWarner Losh * @brief allocate a new sim and fill in the details
67cb588059SWarner Losh *
68cb588059SWarner Losh * A Storage Interface Module (SIM) is the interface between CAM and
69cb588059SWarner Losh * hardware. SIM receives CCBs from CAM via @p sim_action callback and
70cb588059SWarner Losh * translates them into DMA or other hardware transactions. During system
71cb588059SWarner Losh * dumps, it can be polled with the @p sim_poll callback. CCB processing is
72cb588059SWarner Losh * terminated by calling @c xpt_done().
73cb588059SWarner Losh *
74cb588059SWarner Losh * The @p mtx acts as a perimeter lock for the SIM. All calls into the SIM's
75cb588059SWarner Losh * @p sim_action are made with this lock held. It is also used to hold/release
76cb588059SWarner Losh * a SIM, managing its reference count. When the lock is NULL, the SIM is 100%
77cb588059SWarner Losh * responsible for locking (and the reference counting is done with a shared
78cb588059SWarner Losh * lock.
79cb588059SWarner Losh *
80cb588059SWarner Losh * The cam_devq passed in (@c queue) is used to arbitrate the number of
81cb588059SWarner Losh * outstanding transactions to the SIM. For HBAs that have global limits shared
82cb588059SWarner Losh * between the different buses, the same devq should be specified for each bus
83cb588059SWarner Losh * attached to the SIM.
84cb588059SWarner Losh *
85cb588059SWarner Losh * @param sim_action Function to call to process CCBs
86cb588059SWarner Losh * @param sim_poll Function to poll the hardware for completions
87cb588059SWarner Losh * @param sim_name Name of SIM class
88cb588059SWarner Losh * @param softc Software context associated with the SIM
89cb588059SWarner Losh * @param unit Unit number of SIM
90cb588059SWarner Losh * @param mtx Mutex to lock while interacting with the SIM, or NULL
91cb588059SWarner Losh * for a SIM that handle its own locking to enable multi
92cb588059SWarner Losh * queue support.
93cb588059SWarner Losh * @param max_dev_transactions Maximum number of concurrent untagged
94cb588059SWarner Losh * transactions possible
95cb588059SWarner Losh * @param max_tagged_dev_transactions Maximum number of concurrent tagged
96cb588059SWarner Losh * transactions possible.
97cb588059SWarner Losh * @param queue The cam_devq to use for this SIM.
98cb588059SWarner Losh */
998b8a9b1dSJustin T. Gibbs struct cam_sim *
cam_sim_alloc(sim_action_func sim_action,sim_poll_func sim_poll,const char * sim_name,void * softc,uint32_t unit,struct mtx * mtx,int max_dev_transactions,int max_tagged_dev_transactions,struct cam_devq * queue)1008b8a9b1dSJustin T. Gibbs cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
101*7af2f2c8SWarner Losh const char *sim_name, void *softc, uint32_t unit,
1022b83592fSScott Long struct mtx *mtx, int max_dev_transactions,
1039deea857SKenneth D. Merry int max_tagged_dev_transactions, struct cam_devq *queue)
1048b8a9b1dSJustin T. Gibbs {
1058b8a9b1dSJustin T. Gibbs struct cam_sim *sim;
1068b8a9b1dSJustin T. Gibbs
1072ce5eef6SMark Johnston sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT);
1082b83592fSScott Long if (sim == NULL)
1092b83592fSScott Long return (NULL);
1102b83592fSScott Long
1118b8a9b1dSJustin T. Gibbs sim->sim_action = sim_action;
1128b8a9b1dSJustin T. Gibbs sim->sim_poll = sim_poll;
1138b8a9b1dSJustin T. Gibbs sim->sim_name = sim_name;
1148b8a9b1dSJustin T. Gibbs sim->softc = softc;
1158b8a9b1dSJustin T. Gibbs sim->path_id = CAM_PATH_ANY;
1168b8a9b1dSJustin T. Gibbs sim->unit_number = unit;
1178b8a9b1dSJustin T. Gibbs sim->bus_id = 0; /* set in xpt_bus_register */
1188b8a9b1dSJustin T. Gibbs sim->max_tagged_dev_openings = max_tagged_dev_transactions;
1198b8a9b1dSJustin T. Gibbs sim->max_dev_openings = max_dev_transactions;
1208b8a9b1dSJustin T. Gibbs sim->flags = 0;
121fa6099fdSEdward Tomasz Napierala sim->refcount = 1;
1228b8a9b1dSJustin T. Gibbs sim->devq = queue;
1232b83592fSScott Long sim->mtx = mtx;
1248b8a9b1dSJustin T. Gibbs return (sim);
1258b8a9b1dSJustin T. Gibbs }
1268b8a9b1dSJustin T. Gibbs
127b3b15d92SWarner Losh /**
128b3b15d92SWarner Losh * @brief frees up the sim
129b3b15d92SWarner Losh *
130b3b15d92SWarner Losh * Frees up the CAM @c sim and optionally the devq. If a mutex is associated
131b3b15d92SWarner Losh * with the sim, it must be locked on entry. It will remain locked on
132b3b15d92SWarner Losh * return.
133b3b15d92SWarner Losh *
134b3b15d92SWarner Losh * This function will wait for all outstanding reference to the sim to clear
135b3b15d92SWarner Losh * before returning.
136b3b15d92SWarner Losh *
137b3b15d92SWarner Losh * @param sim The sim to free
138b3b15d92SWarner Losh * @param free_devq Free the devq associated with the sim at creation.
139b3b15d92SWarner Losh */
1408b8a9b1dSJustin T. Gibbs void
cam_sim_free(struct cam_sim * sim,int free_devq)1418b8a9b1dSJustin T. Gibbs cam_sim_free(struct cam_sim *sim, int free_devq)
1428b8a9b1dSJustin T. Gibbs {
1437a7ca53fSBjoern A. Zeeb struct mtx *mtx;
144c154feacSScott Long int error __diagused;
145fa6099fdSEdward Tomasz Napierala
1467a7ca53fSBjoern A. Zeeb if (sim->mtx == NULL) {
147401ed17aSAlexander Motin mtx = &cam_sim_free_mtx;
148401ed17aSAlexander Motin mtx_lock(mtx);
1497a7ca53fSBjoern A. Zeeb } else {
1507a7ca53fSBjoern A. Zeeb mtx = sim->mtx;
1517a7ca53fSBjoern A. Zeeb mtx_assert(mtx, MA_OWNED);
152401ed17aSAlexander Motin }
153a1975719SWarner Losh KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
154fa6099fdSEdward Tomasz Napierala sim->refcount--;
155fa6099fdSEdward Tomasz Napierala if (sim->refcount > 0) {
156401ed17aSAlexander Motin error = msleep(sim, mtx, PRIBIO, "simfree", 0);
157fa6099fdSEdward Tomasz Napierala KASSERT(error == 0, ("invalid error value for msleep(9)"));
158fa6099fdSEdward Tomasz Napierala }
159fa6099fdSEdward Tomasz Napierala KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
1607a7ca53fSBjoern A. Zeeb if (mtx == &cam_sim_free_mtx) /* sim->mtx == NULL */
161401ed17aSAlexander Motin mtx_unlock(mtx);
162fa6099fdSEdward Tomasz Napierala
1638b8a9b1dSJustin T. Gibbs if (free_devq)
1648b8a9b1dSJustin T. Gibbs cam_simq_free(sim->devq);
165362abc44STai-hwa Liang free(sim, M_CAMSIM);
1668b8a9b1dSJustin T. Gibbs }
1678b8a9b1dSJustin T. Gibbs
1688b8a9b1dSJustin T. Gibbs void
cam_sim_release(struct cam_sim * sim)169fa6099fdSEdward Tomasz Napierala cam_sim_release(struct cam_sim *sim)
170fa6099fdSEdward Tomasz Napierala {
1717a7ca53fSBjoern A. Zeeb struct mtx *mtx;
172fa6099fdSEdward Tomasz Napierala
1737a7ca53fSBjoern A. Zeeb if (sim->mtx == NULL)
174401ed17aSAlexander Motin mtx = &cam_sim_free_mtx;
1757a7ca53fSBjoern A. Zeeb else if (!mtx_owned(sim->mtx))
1767a7ca53fSBjoern A. Zeeb mtx = sim->mtx;
1777a7ca53fSBjoern A. Zeeb else
1787a7ca53fSBjoern A. Zeeb mtx = NULL; /* We hold the lock. */
1797a7ca53fSBjoern A. Zeeb if (mtx)
180401ed17aSAlexander Motin mtx_lock(mtx);
181227d67aaSAlexander Motin KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
182fa6099fdSEdward Tomasz Napierala sim->refcount--;
1837412d60dSEdward Tomasz Napierala if (sim->refcount == 0)
184fa6099fdSEdward Tomasz Napierala wakeup(sim);
185401ed17aSAlexander Motin if (mtx)
186401ed17aSAlexander Motin mtx_unlock(mtx);
187fa6099fdSEdward Tomasz Napierala }
188fa6099fdSEdward Tomasz Napierala
189fa6099fdSEdward Tomasz Napierala void
cam_sim_hold(struct cam_sim * sim)190fa6099fdSEdward Tomasz Napierala cam_sim_hold(struct cam_sim *sim)
191fa6099fdSEdward Tomasz Napierala {
1927a7ca53fSBjoern A. Zeeb struct mtx *mtx;
193fa6099fdSEdward Tomasz Napierala
1947a7ca53fSBjoern A. Zeeb if (sim->mtx == NULL)
195401ed17aSAlexander Motin mtx = &cam_sim_free_mtx;
1967a7ca53fSBjoern A. Zeeb else if (!mtx_owned(sim->mtx))
1977a7ca53fSBjoern A. Zeeb mtx = sim->mtx;
1987a7ca53fSBjoern A. Zeeb else
1997a7ca53fSBjoern A. Zeeb mtx = NULL; /* We hold the lock. */
2007a7ca53fSBjoern A. Zeeb if (mtx)
201401ed17aSAlexander Motin mtx_lock(mtx);
202227d67aaSAlexander Motin KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
203fa6099fdSEdward Tomasz Napierala sim->refcount++;
204401ed17aSAlexander Motin if (mtx)
205401ed17aSAlexander Motin mtx_unlock(mtx);
206fa6099fdSEdward Tomasz Napierala }
207fa6099fdSEdward Tomasz Napierala
208fa6099fdSEdward Tomasz Napierala void
cam_sim_set_path(struct cam_sim * sim,uint32_t path_id)209*7af2f2c8SWarner Losh cam_sim_set_path(struct cam_sim *sim, uint32_t path_id)
2108b8a9b1dSJustin T. Gibbs {
2118b8a9b1dSJustin T. Gibbs sim->path_id = path_id;
2128b8a9b1dSJustin T. Gibbs }
213