cam_sim.c revision 168864
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 168864 2007-04-19 14:28:43Z scottl $");
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;
87168752Sscottl	sim->devq = queue;
88168752Sscottl	sim->mtx = mtx;
89168752Sscottl	if (mtx == &Giant) {
90168752Sscottl		sim->flags |= 0;
91168752Sscottl		callout_init(&sim->callout, 0);
92168752Sscottl	} else {
93168752Sscottl		sim->flags |= CAM_SIM_MPSAFE;
94168752Sscottl		callout_init(&sim->callout, 1);
9539212Sgibbs	}
9639212Sgibbs
97168752Sscottl	SLIST_INIT(&sim->ccb_freeq);
98168864Sscottl	TAILQ_INIT(&sim->sim_doneq);
99168752Sscottl
10039212Sgibbs	return (sim);
10139212Sgibbs}
10239212Sgibbs
10339212Sgibbsvoid
10439212Sgibbscam_sim_free(struct cam_sim *sim, int free_devq)
10539212Sgibbs{
10639212Sgibbs	if (free_devq)
10739212Sgibbs		cam_simq_free(sim->devq);
108147723Savatar	free(sim, M_CAMSIM);
10939212Sgibbs}
11039212Sgibbs
11139212Sgibbsvoid
11239212Sgibbscam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
11339212Sgibbs{
11439212Sgibbs	sim->path_id = path_id;
11539212Sgibbs}
116