176babe50SJustin T. Gibbs /* 23393f8daSKenneth D. Merry * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs. 32a888f93SKenneth D. Merry * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 476babe50SJustin T. Gibbs * All rights reserved. 576babe50SJustin T. Gibbs * 676babe50SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 776babe50SJustin T. Gibbs * modification, are permitted provided that the following conditions 876babe50SJustin T. Gibbs * are met: 976babe50SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 1076babe50SJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 1176babe50SJustin T. Gibbs * without modification, immediately at the beginning of the file. 1276babe50SJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 1376babe50SJustin T. Gibbs * derived from this software without specific prior written permission. 1476babe50SJustin T. Gibbs * 1576babe50SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1676babe50SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1776babe50SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1876babe50SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 1976babe50SJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2076babe50SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2176babe50SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2276babe50SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2376babe50SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2476babe50SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2576babe50SJustin T. Gibbs * SUCH DAMAGE. 2676babe50SJustin T. Gibbs * 27c3aac50fSPeter Wemm * $FreeBSD$ 2876babe50SJustin T. Gibbs */ 2976babe50SJustin T. Gibbs 3076babe50SJustin T. Gibbs #include <sys/param.h> 3176babe50SJustin T. Gibbs #include <sys/systm.h> 3276babe50SJustin T. Gibbs #include <sys/kernel.h> 3376babe50SJustin T. Gibbs #include <sys/types.h> 349626b608SPoul-Henning Kamp #include <sys/bio.h> 3576babe50SJustin T. Gibbs #include <sys/malloc.h> 3676babe50SJustin T. Gibbs #include <sys/fcntl.h> 3776babe50SJustin T. Gibbs #include <sys/conf.h> 3876babe50SJustin T. Gibbs #include <sys/errno.h> 3976babe50SJustin T. Gibbs #include <sys/devicestat.h> 40f7312ca2SRobert Watson #include <sys/proc.h> 4176babe50SJustin T. Gibbs 4276babe50SJustin T. Gibbs #include <cam/cam.h> 4376babe50SJustin T. Gibbs #include <cam/cam_ccb.h> 4476babe50SJustin T. Gibbs #include <cam/cam_extend.h> 4576babe50SJustin T. Gibbs #include <cam/cam_periph.h> 463393f8daSKenneth D. Merry #include <cam/cam_queue.h> 4776babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h> 4876babe50SJustin T. Gibbs #include <cam/cam_debug.h> 4976babe50SJustin T. Gibbs 5076babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h> 5176babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h> 5276babe50SJustin T. Gibbs 5376babe50SJustin T. Gibbs typedef enum { 5476babe50SJustin T. Gibbs PASS_FLAG_OPEN = 0x01, 5576babe50SJustin T. Gibbs PASS_FLAG_LOCKED = 0x02, 5676babe50SJustin T. Gibbs PASS_FLAG_INVALID = 0x04 5776babe50SJustin T. Gibbs } pass_flags; 5876babe50SJustin T. Gibbs 5976babe50SJustin T. Gibbs typedef enum { 6076babe50SJustin T. Gibbs PASS_STATE_NORMAL 6176babe50SJustin T. Gibbs } pass_state; 6276babe50SJustin T. Gibbs 6376babe50SJustin T. Gibbs typedef enum { 6476babe50SJustin T. Gibbs PASS_CCB_BUFFER_IO, 6576babe50SJustin T. Gibbs PASS_CCB_WAITING 6676babe50SJustin T. Gibbs } pass_ccb_types; 6776babe50SJustin T. Gibbs 6876babe50SJustin T. Gibbs #define ccb_type ppriv_field0 6976babe50SJustin T. Gibbs #define ccb_bp ppriv_ptr1 7076babe50SJustin T. Gibbs 7176babe50SJustin T. Gibbs struct pass_softc { 7276babe50SJustin T. Gibbs pass_state state; 7376babe50SJustin T. Gibbs pass_flags flags; 7476babe50SJustin T. Gibbs u_int8_t pd_type; 7576babe50SJustin T. Gibbs union ccb saved_ccb; 7676babe50SJustin T. Gibbs struct devstat device_stats; 7773d26919SKenneth D. Merry dev_t dev; 7876babe50SJustin T. Gibbs }; 7976babe50SJustin T. Gibbs 8076babe50SJustin T. Gibbs #ifndef MIN 8176babe50SJustin T. Gibbs #define MIN(x,y) ((x<y) ? x : y) 8276babe50SJustin T. Gibbs #endif 8376babe50SJustin T. Gibbs 8476babe50SJustin T. Gibbs #define PASS_CDEV_MAJOR 31 8576babe50SJustin T. Gibbs 8676babe50SJustin T. Gibbs static d_open_t passopen; 8776babe50SJustin T. Gibbs static d_close_t passclose; 8876babe50SJustin T. Gibbs static d_ioctl_t passioctl; 8976babe50SJustin T. Gibbs 9076babe50SJustin T. Gibbs static periph_init_t passinit; 9176babe50SJustin T. Gibbs static periph_ctor_t passregister; 92ee9c90c7SKenneth D. Merry static periph_oninv_t passoninvalidate; 9376babe50SJustin T. Gibbs static periph_dtor_t passcleanup; 9476babe50SJustin T. Gibbs static periph_start_t passstart; 9576babe50SJustin T. Gibbs static void passasync(void *callback_arg, u_int32_t code, 9676babe50SJustin T. Gibbs struct cam_path *path, void *arg); 9776babe50SJustin T. Gibbs static void passdone(struct cam_periph *periph, 9876babe50SJustin T. Gibbs union ccb *done_ccb); 9976babe50SJustin T. Gibbs static int passerror(union ccb *ccb, u_int32_t cam_flags, 10076babe50SJustin T. Gibbs u_int32_t sense_flags); 10176babe50SJustin T. Gibbs static int passsendccb(struct cam_periph *periph, union ccb *ccb, 10276babe50SJustin T. Gibbs union ccb *inccb); 10376babe50SJustin T. Gibbs 10476babe50SJustin T. Gibbs static struct periph_driver passdriver = 10576babe50SJustin T. Gibbs { 10676babe50SJustin T. Gibbs passinit, "pass", 10776babe50SJustin T. Gibbs TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 10876babe50SJustin T. Gibbs }; 10976babe50SJustin T. Gibbs 1100b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver); 11176babe50SJustin T. Gibbs 1124e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = { 1134e2f199eSPoul-Henning Kamp /* open */ passopen, 1144e2f199eSPoul-Henning Kamp /* close */ passclose, 1153393f8daSKenneth D. Merry /* read */ noread, 1163393f8daSKenneth D. Merry /* write */ nowrite, 1174e2f199eSPoul-Henning Kamp /* ioctl */ passioctl, 1184e2f199eSPoul-Henning Kamp /* poll */ nopoll, 1194e2f199eSPoul-Henning Kamp /* mmap */ nommap, 1203393f8daSKenneth D. Merry /* strategy */ nostrategy, 1214e2f199eSPoul-Henning Kamp /* name */ "pass", 1224e2f199eSPoul-Henning Kamp /* maj */ PASS_CDEV_MAJOR, 1234e2f199eSPoul-Henning Kamp /* dump */ nodump, 1244e2f199eSPoul-Henning Kamp /* psize */ nopsize, 1254e2f199eSPoul-Henning Kamp /* flags */ 0, 12676babe50SJustin T. Gibbs }; 12776babe50SJustin T. Gibbs 12876babe50SJustin T. Gibbs static struct extend_array *passperiphs; 12976babe50SJustin T. Gibbs 13076babe50SJustin T. Gibbs static void 13176babe50SJustin T. Gibbs passinit(void) 13276babe50SJustin T. Gibbs { 13376babe50SJustin T. Gibbs cam_status status; 13476babe50SJustin T. Gibbs struct cam_path *path; 13576babe50SJustin T. Gibbs 13676babe50SJustin T. Gibbs /* 13776babe50SJustin T. Gibbs * Create our extend array for storing the devices we attach to. 13876babe50SJustin T. Gibbs */ 13976babe50SJustin T. Gibbs passperiphs = cam_extend_new(); 14076babe50SJustin T. Gibbs if (passperiphs == NULL) { 14176babe50SJustin T. Gibbs printf("passm: Failed to alloc extend array!\n"); 14276babe50SJustin T. Gibbs return; 14376babe50SJustin T. Gibbs } 14476babe50SJustin T. Gibbs 14576babe50SJustin T. Gibbs /* 14676babe50SJustin T. Gibbs * Install a global async callback. This callback will 14776babe50SJustin T. Gibbs * receive async callbacks like "new device found". 14876babe50SJustin T. Gibbs */ 14976babe50SJustin T. Gibbs status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 15076babe50SJustin T. Gibbs CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 15176babe50SJustin T. Gibbs 15276babe50SJustin T. Gibbs if (status == CAM_REQ_CMP) { 15376babe50SJustin T. Gibbs struct ccb_setasync csa; 15476babe50SJustin T. Gibbs 15576babe50SJustin T. Gibbs xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 15676babe50SJustin T. Gibbs csa.ccb_h.func_code = XPT_SASYNC_CB; 15776babe50SJustin T. Gibbs csa.event_enable = AC_FOUND_DEVICE; 15876babe50SJustin T. Gibbs csa.callback = passasync; 15976babe50SJustin T. Gibbs csa.callback_arg = NULL; 16076babe50SJustin T. Gibbs xpt_action((union ccb *)&csa); 16176babe50SJustin T. Gibbs status = csa.ccb_h.status; 16276babe50SJustin T. Gibbs xpt_free_path(path); 16376babe50SJustin T. Gibbs } 16476babe50SJustin T. Gibbs 16576babe50SJustin T. Gibbs if (status != CAM_REQ_CMP) { 16676babe50SJustin T. Gibbs printf("pass: Failed to attach master async callback " 16776babe50SJustin T. Gibbs "due to status 0x%x!\n", status); 16876babe50SJustin T. Gibbs } 16976babe50SJustin T. Gibbs 17076babe50SJustin T. Gibbs } 17176babe50SJustin T. Gibbs 17276babe50SJustin T. Gibbs static void 173ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph) 174ee9c90c7SKenneth D. Merry { 175ee9c90c7SKenneth D. Merry struct pass_softc *softc; 176ee9c90c7SKenneth D. Merry struct ccb_setasync csa; 177ee9c90c7SKenneth D. Merry 178ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 179ee9c90c7SKenneth D. Merry 180ee9c90c7SKenneth D. Merry /* 181ee9c90c7SKenneth D. Merry * De-register any async callbacks. 182ee9c90c7SKenneth D. Merry */ 183ee9c90c7SKenneth D. Merry xpt_setup_ccb(&csa.ccb_h, periph->path, 184ee9c90c7SKenneth D. Merry /* priority */ 5); 185ee9c90c7SKenneth D. Merry csa.ccb_h.func_code = XPT_SASYNC_CB; 186ee9c90c7SKenneth D. Merry csa.event_enable = 0; 187ee9c90c7SKenneth D. Merry csa.callback = passasync; 188ee9c90c7SKenneth D. Merry csa.callback_arg = periph; 189ee9c90c7SKenneth D. Merry xpt_action((union ccb *)&csa); 190ee9c90c7SKenneth D. Merry 191ee9c90c7SKenneth D. Merry softc->flags |= PASS_FLAG_INVALID; 192ee9c90c7SKenneth D. Merry 193ee9c90c7SKenneth D. Merry /* 1943393f8daSKenneth D. Merry * XXX Return all queued I/O with ENXIO. 195ee9c90c7SKenneth D. Merry * XXX Handle any transactions queued to the card 196ee9c90c7SKenneth D. Merry * with XPT_ABORT_CCB. 197ee9c90c7SKenneth D. Merry */ 198ee9c90c7SKenneth D. Merry 199ee9c90c7SKenneth D. Merry if (bootverbose) { 200ee9c90c7SKenneth D. Merry xpt_print_path(periph->path); 201ee9c90c7SKenneth D. Merry printf("lost device\n"); 202ee9c90c7SKenneth D. Merry } 203ee9c90c7SKenneth D. Merry 204ee9c90c7SKenneth D. Merry } 205ee9c90c7SKenneth D. Merry 206ee9c90c7SKenneth D. Merry static void 20776babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph) 20876babe50SJustin T. Gibbs { 209ee9c90c7SKenneth D. Merry struct pass_softc *softc; 210ee9c90c7SKenneth D. Merry 211ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 212ee9c90c7SKenneth D. Merry 213ee9c90c7SKenneth D. Merry devstat_remove_entry(&softc->device_stats); 214ee9c90c7SKenneth D. Merry 21573d26919SKenneth D. Merry destroy_dev(softc->dev); 21673d26919SKenneth D. Merry 21776babe50SJustin T. Gibbs cam_extend_release(passperiphs, periph->unit_number); 21876babe50SJustin T. Gibbs 21976babe50SJustin T. Gibbs if (bootverbose) { 22076babe50SJustin T. Gibbs xpt_print_path(periph->path); 22176babe50SJustin T. Gibbs printf("removing device entry\n"); 22276babe50SJustin T. Gibbs } 223ee9c90c7SKenneth D. Merry free(softc, M_DEVBUF); 22476babe50SJustin T. Gibbs } 22576babe50SJustin T. Gibbs 22676babe50SJustin T. Gibbs static void 22776babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code, 22876babe50SJustin T. Gibbs struct cam_path *path, void *arg) 22976babe50SJustin T. Gibbs { 23076babe50SJustin T. Gibbs struct cam_periph *periph; 23176babe50SJustin T. Gibbs 23276babe50SJustin T. Gibbs periph = (struct cam_periph *)callback_arg; 23376babe50SJustin T. Gibbs 23476babe50SJustin T. Gibbs switch (code) { 23576babe50SJustin T. Gibbs case AC_FOUND_DEVICE: 23676babe50SJustin T. Gibbs { 23776babe50SJustin T. Gibbs struct ccb_getdev *cgd; 23876babe50SJustin T. Gibbs cam_status status; 23976babe50SJustin T. Gibbs 24076babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 241c5ff3b2fSMatt Jacob if (cgd == NULL) 242c5ff3b2fSMatt Jacob break; 24376babe50SJustin T. Gibbs 24476babe50SJustin T. Gibbs /* 24576babe50SJustin T. Gibbs * Allocate a peripheral instance for 24676babe50SJustin T. Gibbs * this device and start the probe 24776babe50SJustin T. Gibbs * process. 24876babe50SJustin T. Gibbs */ 249ee9c90c7SKenneth D. Merry status = cam_periph_alloc(passregister, passoninvalidate, 250ee9c90c7SKenneth D. Merry passcleanup, passstart, "pass", 251ee9c90c7SKenneth D. Merry CAM_PERIPH_BIO, cgd->ccb_h.path, 252ee9c90c7SKenneth D. Merry passasync, AC_FOUND_DEVICE, cgd); 25376babe50SJustin T. Gibbs 25476babe50SJustin T. Gibbs if (status != CAM_REQ_CMP 2553393f8daSKenneth D. Merry && status != CAM_REQ_INPROG) { 2563393f8daSKenneth D. Merry const struct cam_status_entry *entry; 2573393f8daSKenneth D. Merry 2583393f8daSKenneth D. Merry entry = cam_fetch_status_entry(status); 2593393f8daSKenneth D. Merry 26076babe50SJustin T. Gibbs printf("passasync: Unable to attach new device " 2613393f8daSKenneth D. Merry "due to status %#x: %s\n", status, entry ? 2623393f8daSKenneth D. Merry entry->status_text : "Unknown"); 2633393f8daSKenneth D. Merry } 26476babe50SJustin T. Gibbs 26576babe50SJustin T. Gibbs break; 26676babe50SJustin T. Gibbs } 26776babe50SJustin T. Gibbs default: 268516871c6SJustin T. Gibbs cam_periph_async(periph, code, path, arg); 26976babe50SJustin T. Gibbs break; 27076babe50SJustin T. Gibbs } 27176babe50SJustin T. Gibbs } 27276babe50SJustin T. Gibbs 27376babe50SJustin T. Gibbs static cam_status 27476babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg) 27576babe50SJustin T. Gibbs { 27676babe50SJustin T. Gibbs struct pass_softc *softc; 27776babe50SJustin T. Gibbs struct ccb_setasync csa; 27876babe50SJustin T. Gibbs struct ccb_getdev *cgd; 2793393f8daSKenneth D. Merry int no_tags; 28076babe50SJustin T. Gibbs 28176babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 28276babe50SJustin T. Gibbs if (periph == NULL) { 28376babe50SJustin T. Gibbs printf("passregister: periph was NULL!!\n"); 28476babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 28576babe50SJustin T. Gibbs } 28676babe50SJustin T. Gibbs 28776babe50SJustin T. Gibbs if (cgd == NULL) { 28876babe50SJustin T. Gibbs printf("passregister: no getdev CCB, can't register device\n"); 28976babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 29076babe50SJustin T. Gibbs } 29176babe50SJustin T. Gibbs 29276babe50SJustin T. Gibbs softc = (struct pass_softc *)malloc(sizeof(*softc), 29376babe50SJustin T. Gibbs M_DEVBUF, M_NOWAIT); 29476babe50SJustin T. Gibbs 29576babe50SJustin T. Gibbs if (softc == NULL) { 29676babe50SJustin T. Gibbs printf("passregister: Unable to probe new device. " 29776babe50SJustin T. Gibbs "Unable to allocate softc\n"); 29876babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 29976babe50SJustin T. Gibbs } 30076babe50SJustin T. Gibbs 30176babe50SJustin T. Gibbs bzero(softc, sizeof(*softc)); 30276babe50SJustin T. Gibbs softc->state = PASS_STATE_NORMAL; 30310b6172aSMatt Jacob softc->pd_type = SID_TYPE(&cgd->inq_data); 30476babe50SJustin T. Gibbs 30576babe50SJustin T. Gibbs periph->softc = softc; 30676babe50SJustin T. Gibbs cam_extend_set(passperiphs, periph->unit_number, periph); 3073393f8daSKenneth D. Merry 30876babe50SJustin T. Gibbs /* 30976babe50SJustin T. Gibbs * We pass in 0 for a blocksize, since we don't 31076babe50SJustin T. Gibbs * know what the blocksize of this device is, if 31176babe50SJustin T. Gibbs * it even has a blocksize. 31276babe50SJustin T. Gibbs */ 3133393f8daSKenneth D. Merry no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 3143393f8daSKenneth D. Merry devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0, 3153393f8daSKenneth D. Merry DEVSTAT_NO_BLOCKSIZE 3163393f8daSKenneth D. Merry | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 31710b6172aSMatt Jacob softc->pd_type | 31876babe50SJustin T. Gibbs DEVSTAT_TYPE_IF_SCSI | 3192a888f93SKenneth D. Merry DEVSTAT_TYPE_PASS, 3202a888f93SKenneth D. Merry DEVSTAT_PRIORITY_PASS); 32173d26919SKenneth D. Merry 32273d26919SKenneth D. Merry /* Register the device */ 32373d26919SKenneth D. Merry softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT, 32473d26919SKenneth D. Merry GID_OPERATOR, 0600, "%s%d", periph->periph_name, 32573d26919SKenneth D. Merry periph->unit_number); 32673d26919SKenneth D. Merry 32776babe50SJustin T. Gibbs /* 32876babe50SJustin T. Gibbs * Add an async callback so that we get 32976babe50SJustin T. Gibbs * notified if this device goes away. 33076babe50SJustin T. Gibbs */ 33176babe50SJustin T. Gibbs xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 33276babe50SJustin T. Gibbs csa.ccb_h.func_code = XPT_SASYNC_CB; 33376babe50SJustin T. Gibbs csa.event_enable = AC_LOST_DEVICE; 33476babe50SJustin T. Gibbs csa.callback = passasync; 33576babe50SJustin T. Gibbs csa.callback_arg = periph; 33676babe50SJustin T. Gibbs xpt_action((union ccb *)&csa); 33776babe50SJustin T. Gibbs 33876babe50SJustin T. Gibbs if (bootverbose) 33976babe50SJustin T. Gibbs xpt_announce_periph(periph, NULL); 34076babe50SJustin T. Gibbs 34176babe50SJustin T. Gibbs return(CAM_REQ_CMP); 34276babe50SJustin T. Gibbs } 34376babe50SJustin T. Gibbs 34476babe50SJustin T. Gibbs static int 345b40ce416SJulian Elischer passopen(dev_t dev, int flags, int fmt, struct thread *td) 34676babe50SJustin T. Gibbs { 34776babe50SJustin T. Gibbs struct cam_periph *periph; 34876babe50SJustin T. Gibbs struct pass_softc *softc; 34976babe50SJustin T. Gibbs int unit, error; 350ee9c90c7SKenneth D. Merry int s; 35176babe50SJustin T. Gibbs 35276babe50SJustin T. Gibbs error = 0; /* default to no error */ 35376babe50SJustin T. Gibbs 35476babe50SJustin T. Gibbs /* unit = dkunit(dev); */ 35576babe50SJustin T. Gibbs /* XXX KDM fix this */ 35676babe50SJustin T. Gibbs unit = minor(dev) & 0xff; 35776babe50SJustin T. Gibbs 35876babe50SJustin T. Gibbs periph = cam_extend_get(passperiphs, unit); 35976babe50SJustin T. Gibbs 36076babe50SJustin T. Gibbs if (periph == NULL) 36176babe50SJustin T. Gibbs return (ENXIO); 36276babe50SJustin T. Gibbs 36376babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 36476babe50SJustin T. Gibbs 365ee9c90c7SKenneth D. Merry s = splsoftcam(); 366ee9c90c7SKenneth D. Merry if (softc->flags & PASS_FLAG_INVALID) { 367ee9c90c7SKenneth D. Merry splx(s); 36876babe50SJustin T. Gibbs return(ENXIO); 369ee9c90c7SKenneth D. Merry } 37022b9c86cSKenneth D. Merry 37122b9c86cSKenneth D. Merry /* 372f5ef42beSRobert Watson * Don't allow access when we're running at a high securelevel. 37322b9c86cSKenneth D. Merry */ 374a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 1); 375f7312ca2SRobert Watson if (error) { 376ee9c90c7SKenneth D. Merry splx(s); 377f7312ca2SRobert Watson return(error); 37822b9c86cSKenneth D. Merry } 37976babe50SJustin T. Gibbs 38076babe50SJustin T. Gibbs /* 38166a0780eSKenneth D. Merry * Only allow read-write access. 38266a0780eSKenneth D. Merry */ 38322b9c86cSKenneth D. Merry if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 38422b9c86cSKenneth D. Merry splx(s); 38566a0780eSKenneth D. Merry return(EPERM); 38622b9c86cSKenneth D. Merry } 38766a0780eSKenneth D. Merry 38866a0780eSKenneth D. Merry /* 38976babe50SJustin T. Gibbs * We don't allow nonblocking access. 39076babe50SJustin T. Gibbs */ 39176babe50SJustin T. Gibbs if ((flags & O_NONBLOCK) != 0) { 39222b9c86cSKenneth D. Merry xpt_print_path(periph->path); 39322b9c86cSKenneth D. Merry printf("can't do nonblocking accesss\n"); 39422b9c86cSKenneth D. Merry splx(s); 39522b9c86cSKenneth D. Merry return(EINVAL); 39676babe50SJustin T. Gibbs } 39776babe50SJustin T. Gibbs 39822b9c86cSKenneth D. Merry if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 39922b9c86cSKenneth D. Merry splx(s); 40076babe50SJustin T. Gibbs return (error); 40122b9c86cSKenneth D. Merry } 40222b9c86cSKenneth D. Merry 40322b9c86cSKenneth D. Merry splx(s); 40476babe50SJustin T. Gibbs 40576babe50SJustin T. Gibbs if ((softc->flags & PASS_FLAG_OPEN) == 0) { 40676babe50SJustin T. Gibbs if (cam_periph_acquire(periph) != CAM_REQ_CMP) 40776babe50SJustin T. Gibbs return(ENXIO); 40876babe50SJustin T. Gibbs softc->flags |= PASS_FLAG_OPEN; 40976babe50SJustin T. Gibbs } 41076babe50SJustin T. Gibbs 41176babe50SJustin T. Gibbs cam_periph_unlock(periph); 41276babe50SJustin T. Gibbs 41376babe50SJustin T. Gibbs return (error); 41476babe50SJustin T. Gibbs } 41576babe50SJustin T. Gibbs 41676babe50SJustin T. Gibbs static int 417b40ce416SJulian Elischer passclose(dev_t dev, int flag, int fmt, struct thread *td) 41876babe50SJustin T. Gibbs { 41976babe50SJustin T. Gibbs struct cam_periph *periph; 42076babe50SJustin T. Gibbs struct pass_softc *softc; 42176babe50SJustin T. Gibbs int unit, error; 42276babe50SJustin T. Gibbs 42376babe50SJustin T. Gibbs /* unit = dkunit(dev); */ 42476babe50SJustin T. Gibbs /* XXX KDM fix this */ 42576babe50SJustin T. Gibbs unit = minor(dev) & 0xff; 42676babe50SJustin T. Gibbs 42776babe50SJustin T. Gibbs periph = cam_extend_get(passperiphs, unit); 42876babe50SJustin T. Gibbs if (periph == NULL) 42976babe50SJustin T. Gibbs return (ENXIO); 43076babe50SJustin T. Gibbs 43176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 43276babe50SJustin T. Gibbs 43376babe50SJustin T. Gibbs if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 43476babe50SJustin T. Gibbs return (error); 43576babe50SJustin T. Gibbs 43676babe50SJustin T. Gibbs softc->flags &= ~PASS_FLAG_OPEN; 43776babe50SJustin T. Gibbs 43876babe50SJustin T. Gibbs cam_periph_unlock(periph); 43976babe50SJustin T. Gibbs cam_periph_release(periph); 44076babe50SJustin T. Gibbs 44176babe50SJustin T. Gibbs return (0); 44276babe50SJustin T. Gibbs } 44376babe50SJustin T. Gibbs 44476babe50SJustin T. Gibbs static void 44576babe50SJustin T. Gibbs passstart(struct cam_periph *periph, union ccb *start_ccb) 44676babe50SJustin T. Gibbs { 44776babe50SJustin T. Gibbs struct pass_softc *softc; 44876babe50SJustin T. Gibbs int s; 44976babe50SJustin T. Gibbs 45076babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 45176babe50SJustin T. Gibbs 45276babe50SJustin T. Gibbs switch (softc->state) { 45376babe50SJustin T. Gibbs case PASS_STATE_NORMAL: 45476babe50SJustin T. Gibbs s = splbio(); 45576babe50SJustin T. Gibbs start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 45676babe50SJustin T. Gibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 45776babe50SJustin T. Gibbs periph_links.sle); 45876babe50SJustin T. Gibbs periph->immediate_priority = CAM_PRIORITY_NONE; 45976babe50SJustin T. Gibbs splx(s); 46076babe50SJustin T. Gibbs wakeup(&periph->ccb_list); 46176babe50SJustin T. Gibbs break; 46276babe50SJustin T. Gibbs } 46376babe50SJustin T. Gibbs } 4643393f8daSKenneth D. Merry 46576babe50SJustin T. Gibbs static void 46676babe50SJustin T. Gibbs passdone(struct cam_periph *periph, union ccb *done_ccb) 46776babe50SJustin T. Gibbs { 46876babe50SJustin T. Gibbs struct pass_softc *softc; 46976babe50SJustin T. Gibbs struct ccb_scsiio *csio; 47076babe50SJustin T. Gibbs 47176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 47276babe50SJustin T. Gibbs csio = &done_ccb->csio; 47376babe50SJustin T. Gibbs switch (csio->ccb_h.ccb_type) { 47476babe50SJustin T. Gibbs case PASS_CCB_WAITING: 47576babe50SJustin T. Gibbs /* Caller will release the CCB */ 47676babe50SJustin T. Gibbs wakeup(&done_ccb->ccb_h.cbfcnp); 47776babe50SJustin T. Gibbs return; 47876babe50SJustin T. Gibbs } 47976babe50SJustin T. Gibbs xpt_release_ccb(done_ccb); 48076babe50SJustin T. Gibbs } 48176babe50SJustin T. Gibbs 48276babe50SJustin T. Gibbs static int 483b40ce416SJulian Elischer passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 48476babe50SJustin T. Gibbs { 48576babe50SJustin T. Gibbs struct cam_periph *periph; 48676babe50SJustin T. Gibbs struct pass_softc *softc; 48776babe50SJustin T. Gibbs u_int8_t unit; 48876babe50SJustin T. Gibbs int error; 48976babe50SJustin T. Gibbs 49076babe50SJustin T. Gibbs 49176babe50SJustin T. Gibbs /* unit = dkunit(dev); */ 49276babe50SJustin T. Gibbs /* XXX KDM fix this */ 49376babe50SJustin T. Gibbs unit = minor(dev) & 0xff; 49476babe50SJustin T. Gibbs 49576babe50SJustin T. Gibbs periph = cam_extend_get(passperiphs, unit); 49676babe50SJustin T. Gibbs 49776babe50SJustin T. Gibbs if (periph == NULL) 49876babe50SJustin T. Gibbs return(ENXIO); 49976babe50SJustin T. Gibbs 50076babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 50176babe50SJustin T. Gibbs 50276babe50SJustin T. Gibbs error = 0; 50376babe50SJustin T. Gibbs 50476babe50SJustin T. Gibbs switch (cmd) { 50576babe50SJustin T. Gibbs 50676babe50SJustin T. Gibbs case CAMIOCOMMAND: 50776babe50SJustin T. Gibbs { 50876babe50SJustin T. Gibbs union ccb *inccb; 50976babe50SJustin T. Gibbs union ccb *ccb; 5109deea857SKenneth D. Merry int ccb_malloced; 51176babe50SJustin T. Gibbs 51276babe50SJustin T. Gibbs inccb = (union ccb *)addr; 5139deea857SKenneth D. Merry 5149deea857SKenneth D. Merry /* 5159deea857SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 5169deea857SKenneth D. Merry * through the transport layer device. 5179deea857SKenneth D. Merry */ 5189deea857SKenneth D. Merry if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 5199deea857SKenneth D. Merry xpt_print_path(periph->path); 5209deea857SKenneth D. Merry printf("CCB function code %#x is restricted to the " 5219deea857SKenneth D. Merry "XPT device\n", inccb->ccb_h.func_code); 5229deea857SKenneth D. Merry error = ENODEV; 5239deea857SKenneth D. Merry break; 5249deea857SKenneth D. Merry } 5259deea857SKenneth D. Merry 5269deea857SKenneth D. Merry /* 5279deea857SKenneth D. Merry * Non-immediate CCBs need a CCB from the per-device pool 5289deea857SKenneth D. Merry * of CCBs, which is scheduled by the transport layer. 5299deea857SKenneth D. Merry * Immediate CCBs and user-supplied CCBs should just be 5309deea857SKenneth D. Merry * malloced. 5319deea857SKenneth D. Merry */ 5329deea857SKenneth D. Merry if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 5339deea857SKenneth D. Merry && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 5349deea857SKenneth D. Merry ccb = cam_periph_getccb(periph, 5359deea857SKenneth D. Merry inccb->ccb_h.pinfo.priority); 5369deea857SKenneth D. Merry ccb_malloced = 0; 5379deea857SKenneth D. Merry } else { 5389deea857SKenneth D. Merry ccb = xpt_alloc_ccb(); 5399deea857SKenneth D. Merry 5409deea857SKenneth D. Merry if (ccb != NULL) 5419deea857SKenneth D. Merry xpt_setup_ccb(&ccb->ccb_h, periph->path, 5429deea857SKenneth D. Merry inccb->ccb_h.pinfo.priority); 5439deea857SKenneth D. Merry ccb_malloced = 1; 5449deea857SKenneth D. Merry } 5459deea857SKenneth D. Merry 5469deea857SKenneth D. Merry if (ccb == NULL) { 5479deea857SKenneth D. Merry xpt_print_path(periph->path); 5489deea857SKenneth D. Merry printf("unable to allocate CCB\n"); 5499deea857SKenneth D. Merry error = ENOMEM; 5509deea857SKenneth D. Merry break; 5519deea857SKenneth D. Merry } 55276babe50SJustin T. Gibbs 55376babe50SJustin T. Gibbs error = passsendccb(periph, ccb, inccb); 55476babe50SJustin T. Gibbs 5559deea857SKenneth D. Merry if (ccb_malloced) 5569deea857SKenneth D. Merry xpt_free_ccb(ccb); 5579deea857SKenneth D. Merry else 55876babe50SJustin T. Gibbs xpt_release_ccb(ccb); 55976babe50SJustin T. Gibbs 56076babe50SJustin T. Gibbs break; 56176babe50SJustin T. Gibbs } 56276babe50SJustin T. Gibbs default: 56376babe50SJustin T. Gibbs error = cam_periph_ioctl(periph, cmd, addr, passerror); 56476babe50SJustin T. Gibbs break; 56576babe50SJustin T. Gibbs } 56676babe50SJustin T. Gibbs 56776babe50SJustin T. Gibbs return(error); 56876babe50SJustin T. Gibbs } 56976babe50SJustin T. Gibbs 57076babe50SJustin T. Gibbs /* 57176babe50SJustin T. Gibbs * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 57276babe50SJustin T. Gibbs * should be the CCB that is copied in from the user. 57376babe50SJustin T. Gibbs */ 57476babe50SJustin T. Gibbs static int 57576babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 57676babe50SJustin T. Gibbs { 57776babe50SJustin T. Gibbs struct pass_softc *softc; 57876babe50SJustin T. Gibbs struct cam_periph_map_info mapinfo; 57976babe50SJustin T. Gibbs int error, need_unmap; 58076babe50SJustin T. Gibbs 58176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 58276babe50SJustin T. Gibbs 58376babe50SJustin T. Gibbs need_unmap = 0; 58476babe50SJustin T. Gibbs 58576babe50SJustin T. Gibbs /* 58676babe50SJustin T. Gibbs * There are some fields in the CCB header that need to be 58776babe50SJustin T. Gibbs * preserved, the rest we get from the user. 58876babe50SJustin T. Gibbs */ 58976babe50SJustin T. Gibbs xpt_merge_ccb(ccb, inccb); 59076babe50SJustin T. Gibbs 59176babe50SJustin T. Gibbs /* 59276babe50SJustin T. Gibbs * There's no way for the user to have a completion 59376babe50SJustin T. Gibbs * function, so we put our own completion function in here. 59476babe50SJustin T. Gibbs */ 59576babe50SJustin T. Gibbs ccb->ccb_h.cbfcnp = passdone; 59676babe50SJustin T. Gibbs 59776babe50SJustin T. Gibbs /* 59876babe50SJustin T. Gibbs * We only attempt to map the user memory into kernel space 59976babe50SJustin T. Gibbs * if they haven't passed in a physical memory pointer, 60076babe50SJustin T. Gibbs * and if there is actually an I/O operation to perform. 60176babe50SJustin T. Gibbs * Right now cam_periph_mapmem() only supports SCSI and device 60276babe50SJustin T. Gibbs * match CCBs. For the SCSI CCBs, we only pass the CCB in if 60376babe50SJustin T. Gibbs * there's actually data to map. cam_periph_mapmem() will do the 60476babe50SJustin T. Gibbs * right thing, even if there isn't data to map, but since CCBs 60576babe50SJustin T. Gibbs * without data are a reasonably common occurance (e.g. test unit 60676babe50SJustin T. Gibbs * ready), it will save a few cycles if we check for it here. 60776babe50SJustin T. Gibbs */ 60876babe50SJustin T. Gibbs if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 60976babe50SJustin T. Gibbs && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 61076babe50SJustin T. Gibbs && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 61176babe50SJustin T. Gibbs || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 61276babe50SJustin T. Gibbs 61376babe50SJustin T. Gibbs bzero(&mapinfo, sizeof(mapinfo)); 61476babe50SJustin T. Gibbs 61576babe50SJustin T. Gibbs error = cam_periph_mapmem(ccb, &mapinfo); 61676babe50SJustin T. Gibbs 61776babe50SJustin T. Gibbs /* 61876babe50SJustin T. Gibbs * cam_periph_mapmem returned an error, we can't continue. 61976babe50SJustin T. Gibbs * Return the error to the user. 62076babe50SJustin T. Gibbs */ 62176babe50SJustin T. Gibbs if (error) 62276babe50SJustin T. Gibbs return(error); 62376babe50SJustin T. Gibbs 62476babe50SJustin T. Gibbs /* 62576babe50SJustin T. Gibbs * We successfully mapped the memory in, so we need to 62676babe50SJustin T. Gibbs * unmap it when the transaction is done. 62776babe50SJustin T. Gibbs */ 62876babe50SJustin T. Gibbs need_unmap = 1; 62976babe50SJustin T. Gibbs } 63076babe50SJustin T. Gibbs 63176babe50SJustin T. Gibbs /* 63276babe50SJustin T. Gibbs * If the user wants us to perform any error recovery, then honor 63376babe50SJustin T. Gibbs * that request. Otherwise, it's up to the user to perform any 63476babe50SJustin T. Gibbs * error recovery. 63576babe50SJustin T. Gibbs */ 63676babe50SJustin T. Gibbs error = cam_periph_runccb(ccb, 63776babe50SJustin T. Gibbs (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 63876babe50SJustin T. Gibbs passerror : NULL, 6393393f8daSKenneth D. Merry /* cam_flags */ CAM_RETRY_SELTO, 6403393f8daSKenneth D. Merry /* sense_flags */SF_RETRY_UA, 64176babe50SJustin T. Gibbs &softc->device_stats); 64276babe50SJustin T. Gibbs 64376babe50SJustin T. Gibbs if (need_unmap != 0) 64476babe50SJustin T. Gibbs cam_periph_unmapmem(ccb, &mapinfo); 64576babe50SJustin T. Gibbs 64676babe50SJustin T. Gibbs ccb->ccb_h.cbfcnp = NULL; 64776babe50SJustin T. Gibbs ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 64876babe50SJustin T. Gibbs bcopy(ccb, inccb, sizeof(union ccb)); 64976babe50SJustin T. Gibbs 65076babe50SJustin T. Gibbs return(error); 65176babe50SJustin T. Gibbs } 65276babe50SJustin T. Gibbs 65376babe50SJustin T. Gibbs static int 65476babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 65576babe50SJustin T. Gibbs { 65676babe50SJustin T. Gibbs struct cam_periph *periph; 65776babe50SJustin T. Gibbs struct pass_softc *softc; 65876babe50SJustin T. Gibbs 65976babe50SJustin T. Gibbs periph = xpt_path_periph(ccb->ccb_h.path); 66076babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 66176babe50SJustin T. Gibbs 66276babe50SJustin T. Gibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 66376babe50SJustin T. Gibbs &softc->saved_ccb)); 66476babe50SJustin T. Gibbs } 665