cam_sim.c revision 139743
1209878Snwhitehorn/*-
2209878Snwhitehorn * Common functions for SCSI Interface Modules (SIMs).
3209878Snwhitehorn *
4209878Snwhitehorn * Copyright (c) 1997 Justin T. Gibbs.
5209878Snwhitehorn * All rights reserved.
6209878Snwhitehorn *
7209878Snwhitehorn * Redistribution and use in source and binary forms, with or without
8209878Snwhitehorn * modification, are permitted provided that the following conditions
9209878Snwhitehorn * are met:
10209878Snwhitehorn * 1. Redistributions of source code must retain the above copyright
11209878Snwhitehorn *    notice, this list of conditions, and the following disclaimer,
12209878Snwhitehorn *    without modification, immediately at the beginning of the file.
13209878Snwhitehorn * 2. The name of the author may not be used to endorse or promote products
14209878Snwhitehorn *    derived from this software without specific prior written permission.
15209878Snwhitehorn *
16209878Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17209878Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18209878Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19209878Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20209878Snwhitehorn * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21209878Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22209878Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23209878Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24209878Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25209878Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26209878Snwhitehorn * SUCH DAMAGE.
27209878Snwhitehorn */
28209878Snwhitehorn
29209878Snwhitehorn#include <sys/cdefs.h>
30209878Snwhitehorn__FBSDID("$FreeBSD: head/sys/cam/cam_sim.c 139743 2005-01-05 22:34:37Z imp $");
31209878Snwhitehorn
32209878Snwhitehorn#include <sys/param.h>
33209878Snwhitehorn#include <sys/systm.h>
34209878Snwhitehorn#include <sys/malloc.h>
35209878Snwhitehorn
36231044Sandreast#include <cam/cam.h>
37209878Snwhitehorn#include <cam/cam_ccb.h>
38209878Snwhitehorn#include <cam/cam_sim.h>
39258502Sandreast#include <cam/cam_queue.h>
40209878Snwhitehorn
41209878Snwhitehorn#define CAM_PATH_ANY (u_int32_t)-1
42231044Sandreast
43209878Snwhitehornstruct cam_devq *
44209878Snwhitehorncam_simq_alloc(u_int32_t max_sim_transactions)
45209878Snwhitehorn{
46209878Snwhitehorn	return (cam_devq_alloc(/*size*/0, max_sim_transactions));
47209878Snwhitehorn}
48218824Snwhitehorn
49209878Snwhitehornvoid
50209878Snwhitehorncam_simq_free(struct cam_devq *devq)
51209878Snwhitehorn{
52209878Snwhitehorn	cam_devq_free(devq);
53209878Snwhitehorn}
54258502Sandreast
55258502Sandreaststruct cam_sim *
56258502Sandreastcam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
57258502Sandreast	      const char *sim_name, void *softc, u_int32_t unit,
58209878Snwhitehorn	      int max_dev_transactions,
59209878Snwhitehorn	      int max_tagged_dev_transactions, struct cam_devq *queue)
60231044Sandreast{
61209878Snwhitehorn	struct cam_sim *sim;
62209878Snwhitehorn
63258502Sandreast	/*
64258502Sandreast	 * If this is the xpt layer creating a sim, then it's OK
65231044Sandreast	 * to wait for an allocation.
66209878Snwhitehorn	 *
67209878Snwhitehorn	 * XXX Should we pass in a flag to indicate that wait is OK?
68209878Snwhitehorn	 */
69209878Snwhitehorn	if (strcmp(sim_name, "xpt") == 0)
70218824Snwhitehorn		sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
71209878Snwhitehorn					       M_DEVBUF, M_WAITOK);
72209878Snwhitehorn	else
73209878Snwhitehorn		sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
74209878Snwhitehorn					       M_DEVBUF, M_NOWAIT);
75209878Snwhitehorn
76209878Snwhitehorn	if (sim != NULL) {
77231044Sandreast		sim->sim_action = sim_action;
78209878Snwhitehorn		sim->sim_poll = sim_poll;
79209878Snwhitehorn		sim->sim_name = sim_name;
80258502Sandreast		sim->softc = softc;
81258502Sandreast		sim->path_id = CAM_PATH_ANY;
82258502Sandreast		sim->unit_number = unit;
83231044Sandreast		sim->bus_id = 0;	/* set in xpt_bus_register */
84209878Snwhitehorn		sim->max_tagged_dev_openings = max_tagged_dev_transactions;
85209878Snwhitehorn		sim->max_dev_openings = max_dev_transactions;
86209878Snwhitehorn		sim->flags = 0;
87209878Snwhitehorn		callout_handle_init(&sim->c_handle);
88209878Snwhitehorn		sim->devq = queue;
89218824Snwhitehorn	}
90209878Snwhitehorn
91209878Snwhitehorn	return (sim);
92209878Snwhitehorn}
93209878Snwhitehorn
94209878Snwhitehornvoid
95cam_sim_free(struct cam_sim *sim, int free_devq)
96{
97	if (free_devq)
98		cam_simq_free(sim->devq);
99	free(sim, M_DEVBUF);
100}
101
102void
103cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
104{
105	sim->path_id = path_id;
106}
107