cam_sim.c revision 147723
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 147723 2005-07-01 15:21:30Z avatar $");
31116161Sobrien
3239212Sgibbs#include <sys/param.h>
3339212Sgibbs#include <sys/systm.h>
3439212Sgibbs#include <sys/malloc.h>
35147723Savatar#include <sys/kernel.h>
3639212Sgibbs
3739212Sgibbs#include <cam/cam.h>
3839212Sgibbs#include <cam/cam_ccb.h>
3939212Sgibbs#include <cam/cam_sim.h>
4039212Sgibbs#include <cam/cam_queue.h>
4139212Sgibbs
4239212Sgibbs#define CAM_PATH_ANY (u_int32_t)-1
4339212Sgibbs
44147723SavatarMALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
45147723Savatar
4639212Sgibbsstruct cam_devq *
4739212Sgibbscam_simq_alloc(u_int32_t max_sim_transactions)
4839212Sgibbs{
4939212Sgibbs	return (cam_devq_alloc(/*size*/0, max_sim_transactions));
5039212Sgibbs}
5139212Sgibbs
5239212Sgibbsvoid
5339212Sgibbscam_simq_free(struct cam_devq *devq)
5439212Sgibbs{
5539212Sgibbs	cam_devq_free(devq);
5639212Sgibbs}
5739212Sgibbs
5839212Sgibbsstruct cam_sim *
5939212Sgibbscam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
6071507Sjhb	      const char *sim_name, void *softc, u_int32_t unit,
6146581Sken	      int max_dev_transactions,
6246581Sken	      int max_tagged_dev_transactions, struct cam_devq *queue)
6339212Sgibbs{
6439212Sgibbs	struct cam_sim *sim;
6539212Sgibbs
6639212Sgibbs	/*
6739212Sgibbs	 * If this is the xpt layer creating a sim, then it's OK
6839212Sgibbs	 * to wait for an allocation.
6939212Sgibbs	 *
7039212Sgibbs	 * XXX Should we pass in a flag to indicate that wait is OK?
7139212Sgibbs	 */
7239212Sgibbs	if (strcmp(sim_name, "xpt") == 0)
7339212Sgibbs		sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
74147723Savatar					       M_CAMSIM, M_WAITOK);
7539212Sgibbs	else
7639212Sgibbs		sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
77147723Savatar					       M_CAMSIM, M_NOWAIT);
7839212Sgibbs
7939212Sgibbs	if (sim != NULL) {
8039212Sgibbs		sim->sim_action = sim_action;
8139212Sgibbs		sim->sim_poll = sim_poll;
8239212Sgibbs		sim->sim_name = sim_name;
8339212Sgibbs		sim->softc = softc;
8439212Sgibbs		sim->path_id = CAM_PATH_ANY;
8539212Sgibbs		sim->unit_number = unit;
8639212Sgibbs		sim->bus_id = 0;	/* set in xpt_bus_register */
8739212Sgibbs		sim->max_tagged_dev_openings = max_tagged_dev_transactions;
8839212Sgibbs		sim->max_dev_openings = max_dev_transactions;
8939212Sgibbs		sim->flags = 0;
9039212Sgibbs		callout_handle_init(&sim->c_handle);
9139212Sgibbs		sim->devq = queue;
9239212Sgibbs	}
9339212Sgibbs
9439212Sgibbs	return (sim);
9539212Sgibbs}
9639212Sgibbs
9739212Sgibbsvoid
9839212Sgibbscam_sim_free(struct cam_sim *sim, int free_devq)
9939212Sgibbs{
10039212Sgibbs	if (free_devq)
10139212Sgibbs		cam_simq_free(sim->devq);
102147723Savatar	free(sim, M_CAMSIM);
10339212Sgibbs}
10439212Sgibbs
10539212Sgibbsvoid
10639212Sgibbscam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
10739212Sgibbs{
10839212Sgibbs	sim->path_id = path_id;
10939212Sgibbs}
110