cam_sim.c revision 186185
1139743Simp/*- 239212Sgibbs * Common functions for SCSI Interface Modules (SIMs). 339212Sgibbs * 439212Sgibbs * Copyright (c) 1997 Justin T. Gibbs. 539212Sgibbs * All rights reserved. 639212Sgibbs * 739212Sgibbs * Redistribution and use in source and binary forms, with or without 839212Sgibbs * modification, are permitted provided that the following conditions 939212Sgibbs * are met: 1039212Sgibbs * 1. Redistributions of source code must retain the above copyright 1139212Sgibbs * notice, this list of conditions, and the following disclaimer, 1239212Sgibbs * without modification, immediately at the beginning of the file. 1339212Sgibbs * 2. The name of the author may not be used to endorse or promote products 1439212Sgibbs * derived from this software without specific prior written permission. 1539212Sgibbs * 1639212Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1739212Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1839212Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1939212Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2039212Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2139212Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2239212Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2339212Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2439212Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2539212Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2639212Sgibbs * SUCH DAMAGE. 2739212Sgibbs */ 2839212Sgibbs 29116161Sobrien#include <sys/cdefs.h> 30116161Sobrien__FBSDID("$FreeBSD: head/sys/cam/cam_sim.c 186185 2008-12-16 16:57:33Z trasz $"); 31116161Sobrien 3239212Sgibbs#include <sys/param.h> 3339212Sgibbs#include <sys/systm.h> 3439212Sgibbs#include <sys/malloc.h> 35147723Savatar#include <sys/kernel.h> 36168752Sscottl#include <sys/lock.h> 37168752Sscottl#include <sys/mutex.h> 3839212Sgibbs 3939212Sgibbs#include <cam/cam.h> 4039212Sgibbs#include <cam/cam_ccb.h> 4139212Sgibbs#include <cam/cam_sim.h> 4239212Sgibbs#include <cam/cam_queue.h> 4339212Sgibbs 4439212Sgibbs#define CAM_PATH_ANY (u_int32_t)-1 4539212Sgibbs 46147723SavatarMALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers"); 47147723Savatar 4839212Sgibbsstruct cam_devq * 4939212Sgibbscam_simq_alloc(u_int32_t max_sim_transactions) 5039212Sgibbs{ 5139212Sgibbs return (cam_devq_alloc(/*size*/0, max_sim_transactions)); 5239212Sgibbs} 5339212Sgibbs 5439212Sgibbsvoid 5539212Sgibbscam_simq_free(struct cam_devq *devq) 5639212Sgibbs{ 5739212Sgibbs cam_devq_free(devq); 5839212Sgibbs} 5939212Sgibbs 6039212Sgibbsstruct cam_sim * 6139212Sgibbscam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll, 6271507Sjhb const char *sim_name, void *softc, u_int32_t unit, 63168752Sscottl struct mtx *mtx, int max_dev_transactions, 6446581Sken int max_tagged_dev_transactions, struct cam_devq *queue) 6539212Sgibbs{ 6639212Sgibbs struct cam_sim *sim; 6739212Sgibbs 68168752Sscottl if (mtx == NULL) 69168752Sscottl return (NULL); 7039212Sgibbs 71168752Sscottl sim = (struct cam_sim *)malloc(sizeof(struct cam_sim), 72168752Sscottl M_CAMSIM, M_NOWAIT); 73168752Sscottl 74168752Sscottl if (sim == NULL) 75168752Sscottl return (NULL); 76168752Sscottl 77168752Sscottl sim->sim_action = sim_action; 78168752Sscottl sim->sim_poll = sim_poll; 79168752Sscottl sim->sim_name = sim_name; 80168752Sscottl sim->softc = softc; 81168752Sscottl sim->path_id = CAM_PATH_ANY; 82168752Sscottl sim->unit_number = unit; 83168752Sscottl sim->bus_id = 0; /* set in xpt_bus_register */ 84168752Sscottl sim->max_tagged_dev_openings = max_tagged_dev_transactions; 85168752Sscottl sim->max_dev_openings = max_dev_transactions; 86168752Sscottl sim->flags = 0; 87186185Strasz sim->refcount = 1; 88168752Sscottl sim->devq = queue; 89168752Sscottl sim->mtx = mtx; 90168752Sscottl if (mtx == &Giant) { 91168752Sscottl sim->flags |= 0; 92168752Sscottl callout_init(&sim->callout, 0); 93168752Sscottl } else { 94168752Sscottl sim->flags |= CAM_SIM_MPSAFE; 95168752Sscottl callout_init(&sim->callout, 1); 9639212Sgibbs } 9739212Sgibbs 98168752Sscottl SLIST_INIT(&sim->ccb_freeq); 99168864Sscottl TAILQ_INIT(&sim->sim_doneq); 100168752Sscottl 10139212Sgibbs return (sim); 10239212Sgibbs} 10339212Sgibbs 10439212Sgibbsvoid 10539212Sgibbscam_sim_free(struct cam_sim *sim, int free_devq) 10639212Sgibbs{ 107186185Strasz int error; 108186185Strasz 109186185Strasz sim->refcount--; 110186185Strasz if (sim->refcount > 0) { 111186185Strasz error = msleep(sim, sim->mtx, PRIBIO, "simfree", 0); 112186185Strasz KASSERT(error == 0, ("invalid error value for msleep(9)")); 113186185Strasz } 114186185Strasz 115186185Strasz KASSERT(sim->refcount == 0, ("sim->refcount == 0")); 116186185Strasz 11739212Sgibbs if (free_devq) 11839212Sgibbs cam_simq_free(sim->devq); 119147723Savatar free(sim, M_CAMSIM); 12039212Sgibbs} 12139212Sgibbs 12239212Sgibbsvoid 123186185Straszcam_sim_release(struct cam_sim *sim) 124186185Strasz{ 125186185Strasz KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 126186185Strasz 127186185Strasz sim->refcount--; 128186185Strasz if (sim->refcount <= 1) 129186185Strasz wakeup(sim); 130186185Strasz} 131186185Strasz 132186185Straszvoid 133186185Straszcam_sim_hold(struct cam_sim *sim) 134186185Strasz{ 135186185Strasz KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 136186185Strasz 137186185Strasz sim->refcount++; 138186185Strasz} 139186185Strasz 140186185Straszvoid 14139212Sgibbscam_sim_set_path(struct cam_sim *sim, u_int32_t path_id) 14239212Sgibbs{ 14339212Sgibbs sim->path_id = path_id; 14439212Sgibbs} 145