1898b0535SWarner Losh /*- 28b8a9b1dSJustin T. Gibbs * Common functions for SCSI Interface Modules (SIMs). 38b8a9b1dSJustin T. Gibbs * 48b8a9b1dSJustin T. Gibbs * Copyright (c) 1997 Justin T. Gibbs. 58b8a9b1dSJustin T. Gibbs * All rights reserved. 68b8a9b1dSJustin T. Gibbs * 78b8a9b1dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 88b8a9b1dSJustin T. Gibbs * modification, are permitted provided that the following conditions 98b8a9b1dSJustin T. Gibbs * are met: 108b8a9b1dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 118b8a9b1dSJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 128b8a9b1dSJustin T. Gibbs * without modification, immediately at the beginning of the file. 138b8a9b1dSJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 148b8a9b1dSJustin T. Gibbs * derived from this software without specific prior written permission. 158b8a9b1dSJustin T. Gibbs * 168b8a9b1dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178b8a9b1dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188b8a9b1dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198b8a9b1dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 208b8a9b1dSJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218b8a9b1dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228b8a9b1dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238b8a9b1dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248b8a9b1dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258b8a9b1dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268b8a9b1dSJustin T. Gibbs * SUCH DAMAGE. 278b8a9b1dSJustin T. Gibbs */ 288b8a9b1dSJustin T. Gibbs 299c963d87SDavid E. O'Brien #include <sys/cdefs.h> 309c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$"); 319c963d87SDavid E. O'Brien 328b8a9b1dSJustin T. Gibbs #include <sys/param.h> 338b8a9b1dSJustin T. Gibbs #include <sys/systm.h> 348b8a9b1dSJustin T. Gibbs #include <sys/malloc.h> 358b8a9b1dSJustin T. Gibbs 368b8a9b1dSJustin T. Gibbs #include <cam/cam.h> 378b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h> 388b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h> 398b8a9b1dSJustin T. Gibbs #include <cam/cam_queue.h> 408b8a9b1dSJustin T. Gibbs 418b8a9b1dSJustin T. Gibbs #define CAM_PATH_ANY (u_int32_t)-1 428b8a9b1dSJustin T. Gibbs 438b8a9b1dSJustin T. Gibbs struct cam_devq * 448b8a9b1dSJustin T. Gibbs cam_simq_alloc(u_int32_t max_sim_transactions) 458b8a9b1dSJustin T. Gibbs { 468b8a9b1dSJustin T. Gibbs return (cam_devq_alloc(/*size*/0, max_sim_transactions)); 478b8a9b1dSJustin T. Gibbs } 488b8a9b1dSJustin T. Gibbs 498b8a9b1dSJustin T. Gibbs void 508b8a9b1dSJustin T. Gibbs cam_simq_free(struct cam_devq *devq) 518b8a9b1dSJustin T. Gibbs { 528b8a9b1dSJustin T. Gibbs cam_devq_free(devq); 538b8a9b1dSJustin T. Gibbs } 548b8a9b1dSJustin T. Gibbs 558b8a9b1dSJustin T. Gibbs struct cam_sim * 568b8a9b1dSJustin T. Gibbs cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll, 57e2138feeSJohn Baldwin const char *sim_name, void *softc, u_int32_t unit, 589deea857SKenneth D. Merry int max_dev_transactions, 599deea857SKenneth D. Merry int max_tagged_dev_transactions, struct cam_devq *queue) 608b8a9b1dSJustin T. Gibbs { 618b8a9b1dSJustin T. Gibbs struct cam_sim *sim; 628b8a9b1dSJustin T. Gibbs 638b8a9b1dSJustin T. Gibbs /* 648b8a9b1dSJustin T. Gibbs * If this is the xpt layer creating a sim, then it's OK 658b8a9b1dSJustin T. Gibbs * to wait for an allocation. 668b8a9b1dSJustin T. Gibbs * 678b8a9b1dSJustin T. Gibbs * XXX Should we pass in a flag to indicate that wait is OK? 688b8a9b1dSJustin T. Gibbs */ 698b8a9b1dSJustin T. Gibbs if (strcmp(sim_name, "xpt") == 0) 708b8a9b1dSJustin T. Gibbs sim = (struct cam_sim *)malloc(sizeof(struct cam_sim), 71a163d034SWarner Losh M_DEVBUF, M_WAITOK); 728b8a9b1dSJustin T. Gibbs else 738b8a9b1dSJustin T. Gibbs sim = (struct cam_sim *)malloc(sizeof(struct cam_sim), 748b8a9b1dSJustin T. Gibbs M_DEVBUF, M_NOWAIT); 758b8a9b1dSJustin T. Gibbs 768b8a9b1dSJustin T. Gibbs if (sim != NULL) { 778b8a9b1dSJustin T. Gibbs sim->sim_action = sim_action; 788b8a9b1dSJustin T. Gibbs sim->sim_poll = sim_poll; 798b8a9b1dSJustin T. Gibbs sim->sim_name = sim_name; 808b8a9b1dSJustin T. Gibbs sim->softc = softc; 818b8a9b1dSJustin T. Gibbs sim->path_id = CAM_PATH_ANY; 828b8a9b1dSJustin T. Gibbs sim->unit_number = unit; 838b8a9b1dSJustin T. Gibbs sim->bus_id = 0; /* set in xpt_bus_register */ 848b8a9b1dSJustin T. Gibbs sim->max_tagged_dev_openings = max_tagged_dev_transactions; 858b8a9b1dSJustin T. Gibbs sim->max_dev_openings = max_dev_transactions; 868b8a9b1dSJustin T. Gibbs sim->flags = 0; 878b8a9b1dSJustin T. Gibbs callout_handle_init(&sim->c_handle); 888b8a9b1dSJustin T. Gibbs sim->devq = queue; 898b8a9b1dSJustin T. Gibbs } 908b8a9b1dSJustin T. Gibbs 918b8a9b1dSJustin T. Gibbs return (sim); 928b8a9b1dSJustin T. Gibbs } 938b8a9b1dSJustin T. Gibbs 948b8a9b1dSJustin T. Gibbs void 958b8a9b1dSJustin T. Gibbs cam_sim_free(struct cam_sim *sim, int free_devq) 968b8a9b1dSJustin T. Gibbs { 978b8a9b1dSJustin T. Gibbs if (free_devq) 988b8a9b1dSJustin T. Gibbs cam_simq_free(sim->devq); 998b8a9b1dSJustin T. Gibbs free(sim, M_DEVBUF); 1008b8a9b1dSJustin T. Gibbs } 1018b8a9b1dSJustin T. Gibbs 1028b8a9b1dSJustin T. Gibbs void 1038b8a9b1dSJustin T. Gibbs cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id) 1048b8a9b1dSJustin T. Gibbs { 1058b8a9b1dSJustin T. Gibbs sim->path_id = path_id; 1068b8a9b1dSJustin T. Gibbs } 107