1898b0535SWarner Losh /*- 2bec9534dSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3bec9534dSPedro F. Giffuni * 43393f8daSKenneth D. Merry * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs. 52a888f93SKenneth D. Merry * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 676babe50SJustin T. Gibbs * All rights reserved. 776babe50SJustin T. Gibbs * 876babe50SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 976babe50SJustin T. Gibbs * modification, are permitted provided that the following conditions 1076babe50SJustin T. Gibbs * are met: 1176babe50SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 1276babe50SJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 1376babe50SJustin T. Gibbs * without modification, immediately at the beginning of the file. 1476babe50SJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 1576babe50SJustin T. Gibbs * derived from this software without specific prior written permission. 1676babe50SJustin T. Gibbs * 1776babe50SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1876babe50SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1976babe50SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2076babe50SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2176babe50SJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2276babe50SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2376babe50SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2476babe50SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2576babe50SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2676babe50SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2776babe50SJustin T. Gibbs * SUCH DAMAGE. 2876babe50SJustin T. Gibbs */ 2976babe50SJustin T. Gibbs 30ee709e70SDavid E. O'Brien #include <sys/cdefs.h> 31ee709e70SDavid E. O'Brien __FBSDID("$FreeBSD$"); 32ee709e70SDavid E. O'Brien 3376babe50SJustin T. Gibbs #include <sys/param.h> 3476babe50SJustin T. Gibbs #include <sys/systm.h> 3576babe50SJustin T. Gibbs #include <sys/kernel.h> 36a9934668SKenneth D. Merry #include <sys/conf.h> 3776babe50SJustin T. Gibbs #include <sys/types.h> 389626b608SPoul-Henning Kamp #include <sys/bio.h> 39a9934668SKenneth D. Merry #include <sys/bus.h> 4076babe50SJustin T. Gibbs #include <sys/devicestat.h> 41a9934668SKenneth D. Merry #include <sys/errno.h> 42a9934668SKenneth D. Merry #include <sys/fcntl.h> 43a9934668SKenneth D. Merry #include <sys/malloc.h> 44f7312ca2SRobert Watson #include <sys/proc.h> 45a9934668SKenneth D. Merry #include <sys/poll.h> 46a9934668SKenneth D. Merry #include <sys/selinfo.h> 47a9934668SKenneth D. Merry #include <sys/sdt.h> 48416494d7SJustin T. Gibbs #include <sys/taskqueue.h> 49a9934668SKenneth D. Merry #include <vm/uma.h> 50a9934668SKenneth D. Merry #include <vm/vm.h> 51a9934668SKenneth D. Merry #include <vm/vm_extern.h> 52a9934668SKenneth D. Merry 53a9934668SKenneth D. Merry #include <machine/bus.h> 5476babe50SJustin T. Gibbs 5576babe50SJustin T. Gibbs #include <cam/cam.h> 5676babe50SJustin T. Gibbs #include <cam/cam_ccb.h> 5776babe50SJustin T. Gibbs #include <cam/cam_periph.h> 583393f8daSKenneth D. Merry #include <cam/cam_queue.h> 59a9934668SKenneth D. Merry #include <cam/cam_xpt.h> 6076babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h> 6176babe50SJustin T. Gibbs #include <cam/cam_debug.h> 6225a2902cSScott Long #include <cam/cam_compat.h> 63a9934668SKenneth D. Merry #include <cam/cam_xpt_periph.h> 6476babe50SJustin T. Gibbs 6576babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h> 6676babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h> 6776babe50SJustin T. Gibbs 6876babe50SJustin T. Gibbs typedef enum { 6976babe50SJustin T. Gibbs PASS_FLAG_OPEN = 0x01, 7076babe50SJustin T. Gibbs PASS_FLAG_LOCKED = 0x02, 71ea37f519SKenneth D. Merry PASS_FLAG_INVALID = 0x04, 72a9934668SKenneth D. Merry PASS_FLAG_INITIAL_PHYSPATH = 0x08, 73a9934668SKenneth D. Merry PASS_FLAG_ZONE_INPROG = 0x10, 74a9934668SKenneth D. Merry PASS_FLAG_ZONE_VALID = 0x20, 75a9934668SKenneth D. Merry PASS_FLAG_UNMAPPED_CAPABLE = 0x40, 76a9934668SKenneth D. Merry PASS_FLAG_ABANDONED_REF_SET = 0x80 7776babe50SJustin T. Gibbs } pass_flags; 7876babe50SJustin T. Gibbs 7976babe50SJustin T. Gibbs typedef enum { 8076babe50SJustin T. Gibbs PASS_STATE_NORMAL 8176babe50SJustin T. Gibbs } pass_state; 8276babe50SJustin T. Gibbs 8376babe50SJustin T. Gibbs typedef enum { 84a9934668SKenneth D. Merry PASS_CCB_BUFFER_IO, 85a9934668SKenneth D. Merry PASS_CCB_QUEUED_IO 8676babe50SJustin T. Gibbs } pass_ccb_types; 8776babe50SJustin T. Gibbs 8876babe50SJustin T. Gibbs #define ccb_type ppriv_field0 89a9934668SKenneth D. Merry #define ccb_ioreq ppriv_ptr1 90a9934668SKenneth D. Merry 91a9934668SKenneth D. Merry /* 92a9934668SKenneth D. Merry * The maximum number of memory segments we preallocate. 93a9934668SKenneth D. Merry */ 94a9934668SKenneth D. Merry #define PASS_MAX_SEGS 16 95a9934668SKenneth D. Merry 96a9934668SKenneth D. Merry typedef enum { 97a9934668SKenneth D. Merry PASS_IO_NONE = 0x00, 98a9934668SKenneth D. Merry PASS_IO_USER_SEG_MALLOC = 0x01, 99a9934668SKenneth D. Merry PASS_IO_KERN_SEG_MALLOC = 0x02, 100a9934668SKenneth D. Merry PASS_IO_ABANDONED = 0x04 101a9934668SKenneth D. Merry } pass_io_flags; 102a9934668SKenneth D. Merry 103a9934668SKenneth D. Merry struct pass_io_req { 104a9934668SKenneth D. Merry union ccb ccb; 105a9934668SKenneth D. Merry union ccb *alloced_ccb; 106a9934668SKenneth D. Merry union ccb *user_ccb_ptr; 107a9934668SKenneth D. Merry camq_entry user_periph_links; 108a9934668SKenneth D. Merry ccb_ppriv_area user_periph_priv; 109a9934668SKenneth D. Merry struct cam_periph_map_info mapinfo; 110a9934668SKenneth D. Merry pass_io_flags flags; 111a9934668SKenneth D. Merry ccb_flags data_flags; 112a9934668SKenneth D. Merry int num_user_segs; 113a9934668SKenneth D. Merry bus_dma_segment_t user_segs[PASS_MAX_SEGS]; 114a9934668SKenneth D. Merry int num_kern_segs; 115a9934668SKenneth D. Merry bus_dma_segment_t kern_segs[PASS_MAX_SEGS]; 116a9934668SKenneth D. Merry bus_dma_segment_t *user_segptr; 117a9934668SKenneth D. Merry bus_dma_segment_t *kern_segptr; 118a9934668SKenneth D. Merry int num_bufs; 119a9934668SKenneth D. Merry uint32_t dirs[CAM_PERIPH_MAXMAPS]; 120a9934668SKenneth D. Merry uint32_t lengths[CAM_PERIPH_MAXMAPS]; 121a9934668SKenneth D. Merry uint8_t *user_bufs[CAM_PERIPH_MAXMAPS]; 122a9934668SKenneth D. Merry uint8_t *kern_bufs[CAM_PERIPH_MAXMAPS]; 123a9934668SKenneth D. Merry struct bintime start_time; 124a9934668SKenneth D. Merry TAILQ_ENTRY(pass_io_req) links; 125a9934668SKenneth D. Merry }; 12676babe50SJustin T. Gibbs 12776babe50SJustin T. Gibbs struct pass_softc { 12876babe50SJustin T. Gibbs pass_state state; 12976babe50SJustin T. Gibbs pass_flags flags; 13076babe50SJustin T. Gibbs u_int8_t pd_type; 13176babe50SJustin T. Gibbs union ccb saved_ccb; 13286d45c7fSKenneth D. Merry int open_count; 133de239312SAlexander Motin u_int maxio; 134a9d2245eSPoul-Henning Kamp struct devstat *device_stats; 13589c9c53dSPoul-Henning Kamp struct cdev *dev; 136416494d7SJustin T. Gibbs struct cdev *alias_dev; 137416494d7SJustin T. Gibbs struct task add_physpath_task; 138a9934668SKenneth D. Merry struct task shutdown_kqueue_task; 139a9934668SKenneth D. Merry struct selinfo read_select; 140a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) incoming_queue; 141a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) active_queue; 142a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) abandoned_queue; 143a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) done_queue; 144a9934668SKenneth D. Merry struct cam_periph *periph; 145a9934668SKenneth D. Merry char zone_name[12]; 146a9934668SKenneth D. Merry char io_zone_name[12]; 147a9934668SKenneth D. Merry uma_zone_t pass_zone; 148a9934668SKenneth D. Merry uma_zone_t pass_io_zone; 149a9934668SKenneth D. Merry size_t io_zone_size; 15076babe50SJustin T. Gibbs }; 15176babe50SJustin T. Gibbs 15276babe50SJustin T. Gibbs static d_open_t passopen; 15376babe50SJustin T. Gibbs static d_close_t passclose; 15476babe50SJustin T. Gibbs static d_ioctl_t passioctl; 15525a2902cSScott Long static d_ioctl_t passdoioctl; 156a9934668SKenneth D. Merry static d_poll_t passpoll; 157a9934668SKenneth D. Merry static d_kqfilter_t passkqfilter; 158a9934668SKenneth D. Merry static void passreadfiltdetach(struct knote *kn); 159a9934668SKenneth D. Merry static int passreadfilt(struct knote *kn, long hint); 16076babe50SJustin T. Gibbs 16176babe50SJustin T. Gibbs static periph_init_t passinit; 16276babe50SJustin T. Gibbs static periph_ctor_t passregister; 163ee9c90c7SKenneth D. Merry static periph_oninv_t passoninvalidate; 16476babe50SJustin T. Gibbs static periph_dtor_t passcleanup; 165a9934668SKenneth D. Merry static periph_start_t passstart; 166a9934668SKenneth D. Merry static void pass_shutdown_kqueue(void *context, int pending); 167416494d7SJustin T. Gibbs static void pass_add_physpath(void *context, int pending); 16876babe50SJustin T. Gibbs static void passasync(void *callback_arg, u_int32_t code, 16976babe50SJustin T. Gibbs struct cam_path *path, void *arg); 170a9934668SKenneth D. Merry static void passdone(struct cam_periph *periph, 171a9934668SKenneth D. Merry union ccb *done_ccb); 172a9934668SKenneth D. Merry static int passcreatezone(struct cam_periph *periph); 173a9934668SKenneth D. Merry static void passiocleanup(struct pass_softc *softc, 174a9934668SKenneth D. Merry struct pass_io_req *io_req); 175a9934668SKenneth D. Merry static int passcopysglist(struct cam_periph *periph, 176a9934668SKenneth D. Merry struct pass_io_req *io_req, 177a9934668SKenneth D. Merry ccb_flags direction); 178a9934668SKenneth D. Merry static int passmemsetup(struct cam_periph *periph, 179a9934668SKenneth D. Merry struct pass_io_req *io_req); 180a9934668SKenneth D. Merry static int passmemdone(struct cam_periph *periph, 181a9934668SKenneth D. Merry struct pass_io_req *io_req); 18276babe50SJustin T. Gibbs static int passerror(union ccb *ccb, u_int32_t cam_flags, 18376babe50SJustin T. Gibbs u_int32_t sense_flags); 18476babe50SJustin T. Gibbs static int passsendccb(struct cam_periph *periph, union ccb *ccb, 18576babe50SJustin T. Gibbs union ccb *inccb); 18676babe50SJustin T. Gibbs 18776babe50SJustin T. Gibbs static struct periph_driver passdriver = 18876babe50SJustin T. Gibbs { 18976babe50SJustin T. Gibbs passinit, "pass", 19076babe50SJustin T. Gibbs TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 19176babe50SJustin T. Gibbs }; 19276babe50SJustin T. Gibbs 1930b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver); 19476babe50SJustin T. Gibbs 1954e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = { 196dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 197c552ebe1SKenneth D. Merry .d_flags = D_TRACKCLOSE, 1987ac40f5fSPoul-Henning Kamp .d_open = passopen, 1997ac40f5fSPoul-Henning Kamp .d_close = passclose, 2007ac40f5fSPoul-Henning Kamp .d_ioctl = passioctl, 201a9934668SKenneth D. Merry .d_poll = passpoll, 202a9934668SKenneth D. Merry .d_kqfilter = passkqfilter, 2037ac40f5fSPoul-Henning Kamp .d_name = "pass", 20476babe50SJustin T. Gibbs }; 20576babe50SJustin T. Gibbs 206a9934668SKenneth D. Merry static struct filterops passread_filtops = { 207a9934668SKenneth D. Merry .f_isfd = 1, 208a9934668SKenneth D. Merry .f_detach = passreadfiltdetach, 209a9934668SKenneth D. Merry .f_event = passreadfilt 210a9934668SKenneth D. Merry }; 211a9934668SKenneth D. Merry 212a9934668SKenneth D. Merry static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers"); 213a9934668SKenneth D. Merry 21476babe50SJustin T. Gibbs static void 21576babe50SJustin T. Gibbs passinit(void) 21676babe50SJustin T. Gibbs { 21776babe50SJustin T. Gibbs cam_status status; 21876babe50SJustin T. Gibbs 21976babe50SJustin T. Gibbs /* 22076babe50SJustin T. Gibbs * Install a global async callback. This callback will 22176babe50SJustin T. Gibbs * receive async callbacks like "new device found". 22276babe50SJustin T. Gibbs */ 22385d92640SScott Long status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL); 22476babe50SJustin T. Gibbs 22576babe50SJustin T. Gibbs if (status != CAM_REQ_CMP) { 22676babe50SJustin T. Gibbs printf("pass: Failed to attach master async callback " 22776babe50SJustin T. Gibbs "due to status 0x%x!\n", status); 22876babe50SJustin T. Gibbs } 22976babe50SJustin T. Gibbs 23076babe50SJustin T. Gibbs } 23176babe50SJustin T. Gibbs 23276babe50SJustin T. Gibbs static void 233a9934668SKenneth D. Merry passrejectios(struct cam_periph *periph) 234a9934668SKenneth D. Merry { 235a9934668SKenneth D. Merry struct pass_io_req *io_req, *io_req2; 236a9934668SKenneth D. Merry struct pass_softc *softc; 237a9934668SKenneth D. Merry 238a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 239a9934668SKenneth D. Merry 240a9934668SKenneth D. Merry /* 241a9934668SKenneth D. Merry * The user can no longer get status for I/O on the done queue, so 242a9934668SKenneth D. Merry * clean up all outstanding I/O on the done queue. 243a9934668SKenneth D. Merry */ 244a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 245a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 246a9934668SKenneth D. Merry passiocleanup(softc, io_req); 247a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 248a9934668SKenneth D. Merry } 249a9934668SKenneth D. Merry 250a9934668SKenneth D. Merry /* 251a9934668SKenneth D. Merry * The underlying device is gone, so we can't issue these I/Os. 252a9934668SKenneth D. Merry * The devfs node has been shut down, so we can't return status to 253a9934668SKenneth D. Merry * the user. Free any I/O left on the incoming queue. 254a9934668SKenneth D. Merry */ 255a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) { 256a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 257a9934668SKenneth D. Merry passiocleanup(softc, io_req); 258a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 259a9934668SKenneth D. Merry } 260a9934668SKenneth D. Merry 261a9934668SKenneth D. Merry /* 262a9934668SKenneth D. Merry * Normally we would put I/Os on the abandoned queue and acquire a 263a9934668SKenneth D. Merry * reference when we saw the final close. But, the device went 264a9934668SKenneth D. Merry * away and devfs may have moved everything off to deadfs by the 265a9934668SKenneth D. Merry * time the I/O done callback is called; as a result, we won't see 266a9934668SKenneth D. Merry * any more closes. So, if we have any active I/Os, we need to put 267a9934668SKenneth D. Merry * them on the abandoned queue. When the abandoned queue is empty, 268a9934668SKenneth D. Merry * we'll release the remaining reference (see below) to the peripheral. 269a9934668SKenneth D. Merry */ 270a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) { 271a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 272a9934668SKenneth D. Merry io_req->flags |= PASS_IO_ABANDONED; 273a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links); 274a9934668SKenneth D. Merry } 275a9934668SKenneth D. Merry 276a9934668SKenneth D. Merry /* 277a9934668SKenneth D. Merry * If we put any I/O on the abandoned queue, acquire a reference. 278a9934668SKenneth D. Merry */ 279a9934668SKenneth D. Merry if ((!TAILQ_EMPTY(&softc->abandoned_queue)) 280a9934668SKenneth D. Merry && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) { 281a9934668SKenneth D. Merry cam_periph_doacquire(periph); 282a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 283a9934668SKenneth D. Merry } 284a9934668SKenneth D. Merry } 285a9934668SKenneth D. Merry 286a9934668SKenneth D. Merry static void 287ea37f519SKenneth D. Merry passdevgonecb(void *arg) 288ea37f519SKenneth D. Merry { 289ea37f519SKenneth D. Merry struct cam_periph *periph; 290227d67aaSAlexander Motin struct mtx *mtx; 29186d45c7fSKenneth D. Merry struct pass_softc *softc; 29286d45c7fSKenneth D. Merry int i; 293ea37f519SKenneth D. Merry 294ea37f519SKenneth D. Merry periph = (struct cam_periph *)arg; 295227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 296227d67aaSAlexander Motin mtx_lock(mtx); 297ea37f519SKenneth D. Merry 298227d67aaSAlexander Motin softc = (struct pass_softc *)periph->softc; 29986d45c7fSKenneth D. Merry KASSERT(softc->open_count >= 0, ("Negative open count %d", 30086d45c7fSKenneth D. Merry softc->open_count)); 30186d45c7fSKenneth D. Merry 30286d45c7fSKenneth D. Merry /* 30386d45c7fSKenneth D. Merry * When we get this callback, we will get no more close calls from 30486d45c7fSKenneth D. Merry * devfs. So if we have any dangling opens, we need to release the 30586d45c7fSKenneth D. Merry * reference held for that particular context. 30686d45c7fSKenneth D. Merry */ 30786d45c7fSKenneth D. Merry for (i = 0; i < softc->open_count; i++) 30886d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 30986d45c7fSKenneth D. Merry 31086d45c7fSKenneth D. Merry softc->open_count = 0; 31186d45c7fSKenneth D. Merry 31286d45c7fSKenneth D. Merry /* 31386d45c7fSKenneth D. Merry * Release the reference held for the device node, it is gone now. 314a9934668SKenneth D. Merry * Accordingly, inform all queued I/Os of their fate. 31586d45c7fSKenneth D. Merry */ 31686d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 317a9934668SKenneth D. Merry passrejectios(periph); 31886d45c7fSKenneth D. Merry 31986d45c7fSKenneth D. Merry /* 320a9934668SKenneth D. Merry * We reference the SIM lock directly here, instead of using 32186d45c7fSKenneth D. Merry * cam_periph_unlock(). The reason is that the final call to 32286d45c7fSKenneth D. Merry * cam_periph_release_locked() above could result in the periph 32386d45c7fSKenneth D. Merry * getting freed. If that is the case, dereferencing the periph 32486d45c7fSKenneth D. Merry * with a cam_periph_unlock() call would cause a page fault. 32586d45c7fSKenneth D. Merry */ 326227d67aaSAlexander Motin mtx_unlock(mtx); 327a9934668SKenneth D. Merry 328a9934668SKenneth D. Merry /* 329a9934668SKenneth D. Merry * We have to remove our kqueue context from a thread because it 330a9934668SKenneth D. Merry * may sleep. It would be nice if we could get a callback from 331a9934668SKenneth D. Merry * kqueue when it is done cleaning up resources. 332a9934668SKenneth D. Merry */ 333a9934668SKenneth D. Merry taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task); 334ea37f519SKenneth D. Merry } 335ea37f519SKenneth D. Merry 336ea37f519SKenneth D. Merry static void 337ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph) 338ee9c90c7SKenneth D. Merry { 339ee9c90c7SKenneth D. Merry struct pass_softc *softc; 340ee9c90c7SKenneth D. Merry 341ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 342ee9c90c7SKenneth D. Merry 343ee9c90c7SKenneth D. Merry /* 344ee9c90c7SKenneth D. Merry * De-register any async callbacks. 345ee9c90c7SKenneth D. Merry */ 34685d92640SScott Long xpt_register_async(0, passasync, periph, periph->path); 347ee9c90c7SKenneth D. Merry 348ee9c90c7SKenneth D. Merry softc->flags |= PASS_FLAG_INVALID; 349ee9c90c7SKenneth D. Merry 350ee9c90c7SKenneth D. Merry /* 351ea37f519SKenneth D. Merry * Tell devfs this device has gone away, and ask for a callback 352ea37f519SKenneth D. Merry * when it has cleaned up its state. 353ea37f519SKenneth D. Merry */ 354ea37f519SKenneth D. Merry destroy_dev_sched_cb(softc->dev, passdevgonecb, periph); 355ee9c90c7SKenneth D. Merry } 356ee9c90c7SKenneth D. Merry 357ee9c90c7SKenneth D. Merry static void 35876babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph) 35976babe50SJustin T. Gibbs { 360ee9c90c7SKenneth D. Merry struct pass_softc *softc; 361ee9c90c7SKenneth D. Merry 362ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 363ee9c90c7SKenneth D. Merry 364a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 365a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->active_queue), 366a9934668SKenneth D. Merry ("%s called when there are commands on the active queue!\n", 367a9934668SKenneth D. Merry __func__)); 368a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->abandoned_queue), 369a9934668SKenneth D. Merry ("%s called when there are commands on the abandoned queue!\n", 370a9934668SKenneth D. Merry __func__)); 371a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->incoming_queue), 372a9934668SKenneth D. Merry ("%s called when there are commands on the incoming queue!\n", 373a9934668SKenneth D. Merry __func__)); 374a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->done_queue), 375a9934668SKenneth D. Merry ("%s called when there are commands on the done queue!\n", 376a9934668SKenneth D. Merry __func__)); 377a9934668SKenneth D. Merry 3785f3fed85SEdward Tomasz Napierala devstat_remove_entry(softc->device_stats); 379416494d7SJustin T. Gibbs 3805f3fed85SEdward Tomasz Napierala cam_periph_unlock(periph); 381a9934668SKenneth D. Merry 382a9934668SKenneth D. Merry /* 383a9934668SKenneth D. Merry * We call taskqueue_drain() for the physpath task to make sure it 384a9934668SKenneth D. Merry * is complete. We drop the lock because this can potentially 385a9934668SKenneth D. Merry * sleep. XXX KDM that is bad. Need a way to get a callback when 386a9934668SKenneth D. Merry * a taskqueue is drained. 387a9934668SKenneth D. Merry * 388a9934668SKenneth D. Merry * Note that we don't drain the kqueue shutdown task queue. This 389a9934668SKenneth D. Merry * is because we hold a reference on the periph for kqueue, and 390a9934668SKenneth D. Merry * release that reference from the kqueue shutdown task queue. So 391a9934668SKenneth D. Merry * we cannot come into this routine unless we've released that 392a9934668SKenneth D. Merry * reference. Also, because that could be the last reference, we 393a9934668SKenneth D. Merry * could be called from the cam_periph_release() call in 394a9934668SKenneth D. Merry * pass_shutdown_kqueue(). In that case, the taskqueue_drain() 395a9934668SKenneth D. Merry * would deadlock. It would be preferable if we had a way to 396a9934668SKenneth D. Merry * get a callback when a taskqueue is done. 397a9934668SKenneth D. Merry */ 398416494d7SJustin T. Gibbs taskqueue_drain(taskqueue_thread, &softc->add_physpath_task); 399416494d7SJustin T. Gibbs 4005f3fed85SEdward Tomasz Napierala cam_periph_lock(periph); 401416494d7SJustin T. Gibbs 402ee9c90c7SKenneth D. Merry free(softc, M_DEVBUF); 40376babe50SJustin T. Gibbs } 40476babe50SJustin T. Gibbs 40576babe50SJustin T. Gibbs static void 406a9934668SKenneth D. Merry pass_shutdown_kqueue(void *context, int pending) 407a9934668SKenneth D. Merry { 408a9934668SKenneth D. Merry struct cam_periph *periph; 409a9934668SKenneth D. Merry struct pass_softc *softc; 410a9934668SKenneth D. Merry 411a9934668SKenneth D. Merry periph = context; 412a9934668SKenneth D. Merry softc = periph->softc; 413a9934668SKenneth D. Merry 414a9934668SKenneth D. Merry knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0); 415a9934668SKenneth D. Merry knlist_destroy(&softc->read_select.si_note); 416a9934668SKenneth D. Merry 417a9934668SKenneth D. Merry /* 418a9934668SKenneth D. Merry * Release the reference we held for kqueue. 419a9934668SKenneth D. Merry */ 420a9934668SKenneth D. Merry cam_periph_release(periph); 421a9934668SKenneth D. Merry } 422a9934668SKenneth D. Merry 423a9934668SKenneth D. Merry static void 424416494d7SJustin T. Gibbs pass_add_physpath(void *context, int pending) 425416494d7SJustin T. Gibbs { 426416494d7SJustin T. Gibbs struct cam_periph *periph; 427416494d7SJustin T. Gibbs struct pass_softc *softc; 428a9934668SKenneth D. Merry struct mtx *mtx; 429416494d7SJustin T. Gibbs char *physpath; 430416494d7SJustin T. Gibbs 431416494d7SJustin T. Gibbs /* 432416494d7SJustin T. Gibbs * If we have one, create a devfs alias for our 433416494d7SJustin T. Gibbs * physical path. 434416494d7SJustin T. Gibbs */ 435416494d7SJustin T. Gibbs periph = context; 436416494d7SJustin T. Gibbs softc = periph->softc; 4376884b662SAlexander Motin physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK); 438a9934668SKenneth D. Merry mtx = cam_periph_mtx(periph); 439a9934668SKenneth D. Merry mtx_lock(mtx); 440a9934668SKenneth D. Merry 441a9934668SKenneth D. Merry if (periph->flags & CAM_PERIPH_INVALID) 4426884b662SAlexander Motin goto out; 443a9934668SKenneth D. Merry 444416494d7SJustin T. Gibbs if (xpt_getattr(physpath, MAXPATHLEN, 445416494d7SJustin T. Gibbs "GEOM::physpath", periph->path) == 0 446416494d7SJustin T. Gibbs && strlen(physpath) != 0) { 447416494d7SJustin T. Gibbs 448a9934668SKenneth D. Merry mtx_unlock(mtx); 449416494d7SJustin T. Gibbs make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev, 450416494d7SJustin T. Gibbs softc->dev, softc->alias_dev, physpath); 451a9934668SKenneth D. Merry mtx_lock(mtx); 452416494d7SJustin T. Gibbs } 453ea37f519SKenneth D. Merry 454a9934668SKenneth D. Merry out: 455ea37f519SKenneth D. Merry /* 456ea37f519SKenneth D. Merry * Now that we've made our alias, we no longer have to have a 457ea37f519SKenneth D. Merry * reference to the device. 458ea37f519SKenneth D. Merry */ 459a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0) 460ea37f519SKenneth D. Merry softc->flags |= PASS_FLAG_INITIAL_PHYSPATH; 4616884b662SAlexander Motin 462a9934668SKenneth D. Merry /* 463a9934668SKenneth D. Merry * We always acquire a reference to the periph before queueing this 464a9934668SKenneth D. Merry * task queue function, so it won't go away before we run. 465a9934668SKenneth D. Merry */ 466a9934668SKenneth D. Merry while (pending-- > 0) 467a9934668SKenneth D. Merry cam_periph_release_locked(periph); 468a9934668SKenneth D. Merry mtx_unlock(mtx); 469a9934668SKenneth D. Merry 4706884b662SAlexander Motin free(physpath, M_DEVBUF); 471416494d7SJustin T. Gibbs } 472416494d7SJustin T. Gibbs 473416494d7SJustin T. Gibbs static void 47476babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code, 47576babe50SJustin T. Gibbs struct cam_path *path, void *arg) 47676babe50SJustin T. Gibbs { 47776babe50SJustin T. Gibbs struct cam_periph *periph; 47876babe50SJustin T. Gibbs 47976babe50SJustin T. Gibbs periph = (struct cam_periph *)callback_arg; 48076babe50SJustin T. Gibbs 48176babe50SJustin T. Gibbs switch (code) { 48276babe50SJustin T. Gibbs case AC_FOUND_DEVICE: 48376babe50SJustin T. Gibbs { 48476babe50SJustin T. Gibbs struct ccb_getdev *cgd; 48576babe50SJustin T. Gibbs cam_status status; 48676babe50SJustin T. Gibbs 48776babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 488c5ff3b2fSMatt Jacob if (cgd == NULL) 489c5ff3b2fSMatt Jacob break; 49076babe50SJustin T. Gibbs 49176babe50SJustin T. Gibbs /* 49276babe50SJustin T. Gibbs * Allocate a peripheral instance for 49376babe50SJustin T. Gibbs * this device and start the probe 49476babe50SJustin T. Gibbs * process. 49576babe50SJustin T. Gibbs */ 496ee9c90c7SKenneth D. Merry status = cam_periph_alloc(passregister, passoninvalidate, 497a9934668SKenneth D. Merry passcleanup, passstart, "pass", 498227d67aaSAlexander Motin CAM_PERIPH_BIO, path, 499ee9c90c7SKenneth D. Merry passasync, AC_FOUND_DEVICE, cgd); 50076babe50SJustin T. Gibbs 50176babe50SJustin T. Gibbs if (status != CAM_REQ_CMP 5023393f8daSKenneth D. Merry && status != CAM_REQ_INPROG) { 5033393f8daSKenneth D. Merry const struct cam_status_entry *entry; 5043393f8daSKenneth D. Merry 5053393f8daSKenneth D. Merry entry = cam_fetch_status_entry(status); 5063393f8daSKenneth D. Merry 50776babe50SJustin T. Gibbs printf("passasync: Unable to attach new device " 5083393f8daSKenneth D. Merry "due to status %#x: %s\n", status, entry ? 5093393f8daSKenneth D. Merry entry->status_text : "Unknown"); 5103393f8daSKenneth D. Merry } 51176babe50SJustin T. Gibbs 51276babe50SJustin T. Gibbs break; 51376babe50SJustin T. Gibbs } 514416494d7SJustin T. Gibbs case AC_ADVINFO_CHANGED: 515416494d7SJustin T. Gibbs { 516416494d7SJustin T. Gibbs uintptr_t buftype; 517416494d7SJustin T. Gibbs 518416494d7SJustin T. Gibbs buftype = (uintptr_t)arg; 519416494d7SJustin T. Gibbs if (buftype == CDAI_TYPE_PHYS_PATH) { 520416494d7SJustin T. Gibbs struct pass_softc *softc; 521416494d7SJustin T. Gibbs 522416494d7SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 523a9934668SKenneth D. Merry /* 524a9934668SKenneth D. Merry * Acquire a reference to the periph before we 525a9934668SKenneth D. Merry * start the taskqueue, so that we don't run into 526a9934668SKenneth D. Merry * a situation where the periph goes away before 527a9934668SKenneth D. Merry * the task queue has a chance to run. 528a9934668SKenneth D. Merry */ 529*99e7a4adSScott Long if (cam_periph_acquire(periph) != 0) 530a9934668SKenneth D. Merry break; 531a9934668SKenneth D. Merry 532416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, 533416494d7SJustin T. Gibbs &softc->add_physpath_task); 534416494d7SJustin T. Gibbs } 535416494d7SJustin T. Gibbs break; 536416494d7SJustin T. Gibbs } 53776babe50SJustin T. Gibbs default: 538516871c6SJustin T. Gibbs cam_periph_async(periph, code, path, arg); 53976babe50SJustin T. Gibbs break; 54076babe50SJustin T. Gibbs } 54176babe50SJustin T. Gibbs } 54276babe50SJustin T. Gibbs 54376babe50SJustin T. Gibbs static cam_status 54476babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg) 54576babe50SJustin T. Gibbs { 54676babe50SJustin T. Gibbs struct pass_softc *softc; 54776babe50SJustin T. Gibbs struct ccb_getdev *cgd; 548b8b6b5d3SAlexander Motin struct ccb_pathinq cpi; 549ee198893SKonstantin Belousov struct make_dev_args args; 550ee198893SKonstantin Belousov int error, no_tags; 55176babe50SJustin T. Gibbs 55276babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 55376babe50SJustin T. Gibbs if (cgd == NULL) { 554ea37f519SKenneth D. Merry printf("%s: no getdev CCB, can't register device\n", __func__); 55576babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 55676babe50SJustin T. Gibbs } 55776babe50SJustin T. Gibbs 55876babe50SJustin T. Gibbs softc = (struct pass_softc *)malloc(sizeof(*softc), 55976babe50SJustin T. Gibbs M_DEVBUF, M_NOWAIT); 56076babe50SJustin T. Gibbs 56176babe50SJustin T. Gibbs if (softc == NULL) { 562ea37f519SKenneth D. Merry printf("%s: Unable to probe new device. " 563ea37f519SKenneth D. Merry "Unable to allocate softc\n", __func__); 56476babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 56576babe50SJustin T. Gibbs } 56676babe50SJustin T. Gibbs 56776babe50SJustin T. Gibbs bzero(softc, sizeof(*softc)); 56876babe50SJustin T. Gibbs softc->state = PASS_STATE_NORMAL; 569b8b6b5d3SAlexander Motin if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI) 57010b6172aSMatt Jacob softc->pd_type = SID_TYPE(&cgd->inq_data); 571b8b6b5d3SAlexander Motin else if (cgd->protocol == PROTO_SATAPM) 572b8b6b5d3SAlexander Motin softc->pd_type = T_ENCLOSURE; 573b8b6b5d3SAlexander Motin else 574b8b6b5d3SAlexander Motin softc->pd_type = T_DIRECT; 57576babe50SJustin T. Gibbs 57676babe50SJustin T. Gibbs periph->softc = softc; 577a9934668SKenneth D. Merry softc->periph = periph; 578a9934668SKenneth D. Merry TAILQ_INIT(&softc->incoming_queue); 579a9934668SKenneth D. Merry TAILQ_INIT(&softc->active_queue); 580a9934668SKenneth D. Merry TAILQ_INIT(&softc->abandoned_queue); 581a9934668SKenneth D. Merry TAILQ_INIT(&softc->done_queue); 582a9934668SKenneth D. Merry snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d", 583a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 584a9934668SKenneth D. Merry snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO", 585a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 586a9934668SKenneth D. Merry softc->io_zone_size = MAXPHYS; 587a9934668SKenneth D. Merry knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph)); 5883393f8daSKenneth D. Merry 589762a7f4fSWarner Losh xpt_path_inq(&cpi, periph->path); 590b8b6b5d3SAlexander Motin 591de239312SAlexander Motin if (cpi.maxio == 0) 592de239312SAlexander Motin softc->maxio = DFLTPHYS; /* traditional default */ 593de239312SAlexander Motin else if (cpi.maxio > MAXPHYS) 594de239312SAlexander Motin softc->maxio = MAXPHYS; /* for safety */ 595de239312SAlexander Motin else 596de239312SAlexander Motin softc->maxio = cpi.maxio; /* real value */ 597de239312SAlexander Motin 598a9934668SKenneth D. Merry if (cpi.hba_misc & PIM_UNMAPPED) 599a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE; 600a9934668SKenneth D. Merry 60176babe50SJustin T. Gibbs /* 60276babe50SJustin T. Gibbs * We pass in 0 for a blocksize, since we don't 60376babe50SJustin T. Gibbs * know what the blocksize of this device is, if 60476babe50SJustin T. Gibbs * it even has a blocksize. 60576babe50SJustin T. Gibbs */ 606edec59d9SAlexander Motin cam_periph_unlock(periph); 6073393f8daSKenneth D. Merry no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 608c81d2c74SMatt Jacob softc->device_stats = devstat_new_entry("pass", 609d3ce8327SEd Schouten periph->unit_number, 0, 6103393f8daSKenneth D. Merry DEVSTAT_NO_BLOCKSIZE 6113393f8daSKenneth D. Merry | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 61210b6172aSMatt Jacob softc->pd_type | 613b8b6b5d3SAlexander Motin XPORT_DEVSTAT_TYPE(cpi.transport) | 6142a888f93SKenneth D. Merry DEVSTAT_TYPE_PASS, 6152a888f93SKenneth D. Merry DEVSTAT_PRIORITY_PASS); 61673d26919SKenneth D. Merry 617ea37f519SKenneth D. Merry /* 618a9934668SKenneth D. Merry * Initialize the taskqueue handler for shutting down kqueue. 619a9934668SKenneth D. Merry */ 620a9934668SKenneth D. Merry TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0, 621a9934668SKenneth D. Merry pass_shutdown_kqueue, periph); 622a9934668SKenneth D. Merry 623a9934668SKenneth D. Merry /* 624a9934668SKenneth D. Merry * Acquire a reference to the periph that we can release once we've 625a9934668SKenneth D. Merry * cleaned up the kqueue. 626a9934668SKenneth D. Merry */ 627*99e7a4adSScott Long if (cam_periph_acquire(periph) != 0) { 628a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 629a9934668SKenneth D. Merry "registration!\n", __func__); 630a9934668SKenneth D. Merry cam_periph_lock(periph); 631a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 632a9934668SKenneth D. Merry } 633a9934668SKenneth D. Merry 634a9934668SKenneth D. Merry /* 635ea37f519SKenneth D. Merry * Acquire a reference to the periph before we create the devfs 636ea37f519SKenneth D. Merry * instance for it. We'll release this reference once the devfs 637ea37f519SKenneth D. Merry * instance has been freed. 638ea37f519SKenneth D. Merry */ 639*99e7a4adSScott Long if (cam_periph_acquire(periph) != 0) { 640ea37f519SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 641ea37f519SKenneth D. Merry "registration!\n", __func__); 64286d45c7fSKenneth D. Merry cam_periph_lock(periph); 643ea37f519SKenneth D. Merry return (CAM_REQ_CMP_ERR); 644ea37f519SKenneth D. Merry } 645ea37f519SKenneth D. Merry 64673d26919SKenneth D. Merry /* Register the device */ 647ee198893SKonstantin Belousov make_dev_args_init(&args); 648ee198893SKonstantin Belousov args.mda_devsw = &pass_cdevsw; 649ee198893SKonstantin Belousov args.mda_unit = periph->unit_number; 650ee198893SKonstantin Belousov args.mda_uid = UID_ROOT; 651ee198893SKonstantin Belousov args.mda_gid = GID_OPERATOR; 652ee198893SKonstantin Belousov args.mda_mode = 0600; 653ee198893SKonstantin Belousov args.mda_si_drv1 = periph; 654ee198893SKonstantin Belousov error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 655ee198893SKonstantin Belousov periph->unit_number); 656ee198893SKonstantin Belousov if (error != 0) { 657ee198893SKonstantin Belousov cam_periph_lock(periph); 658ee198893SKonstantin Belousov cam_periph_release_locked(periph); 659ee198893SKonstantin Belousov return (CAM_REQ_CMP_ERR); 660ee198893SKonstantin Belousov } 661ea37f519SKenneth D. Merry 662ea37f519SKenneth D. Merry /* 663a9934668SKenneth D. Merry * Hold a reference to the periph before we create the physical 664a9934668SKenneth D. Merry * path alias so it can't go away. 665ea37f519SKenneth D. Merry */ 666*99e7a4adSScott Long if (cam_periph_acquire(periph) != 0) { 667a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 668a9934668SKenneth D. Merry "registration!\n", __func__); 669a9934668SKenneth D. Merry cam_periph_lock(periph); 670a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 671a9934668SKenneth D. Merry } 672ea37f519SKenneth D. Merry 673edec59d9SAlexander Motin cam_periph_lock(periph); 67473d26919SKenneth D. Merry 675416494d7SJustin T. Gibbs TASK_INIT(&softc->add_physpath_task, /*priority*/0, 676416494d7SJustin T. Gibbs pass_add_physpath, periph); 677416494d7SJustin T. Gibbs 67876babe50SJustin T. Gibbs /* 679416494d7SJustin T. Gibbs * See if physical path information is already available. 68076babe50SJustin T. Gibbs */ 681416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task); 682416494d7SJustin T. Gibbs 683416494d7SJustin T. Gibbs /* 684416494d7SJustin T. Gibbs * Add an async callback so that we get notified if 685416494d7SJustin T. Gibbs * this device goes away or its physical path 686416494d7SJustin T. Gibbs * (stored in the advanced info data of the EDT) has 687416494d7SJustin T. Gibbs * changed. 688416494d7SJustin T. Gibbs */ 689416494d7SJustin T. Gibbs xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 690416494d7SJustin T. Gibbs passasync, periph, periph->path); 69176babe50SJustin T. Gibbs 69276babe50SJustin T. Gibbs if (bootverbose) 69376babe50SJustin T. Gibbs xpt_announce_periph(periph, NULL); 69476babe50SJustin T. Gibbs 69576babe50SJustin T. Gibbs return(CAM_REQ_CMP); 69676babe50SJustin T. Gibbs } 69776babe50SJustin T. Gibbs 69876babe50SJustin T. Gibbs static int 69989c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td) 70076babe50SJustin T. Gibbs { 70176babe50SJustin T. Gibbs struct cam_periph *periph; 70276babe50SJustin T. Gibbs struct pass_softc *softc; 703e2a5fdf9SNate Lawson int error; 70476babe50SJustin T. Gibbs 705e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 706*99e7a4adSScott Long if (cam_periph_acquire(periph) != 0) 70776babe50SJustin T. Gibbs return (ENXIO); 70876babe50SJustin T. Gibbs 7092b83592fSScott Long cam_periph_lock(periph); 7102b83592fSScott Long 71176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 71276babe50SJustin T. Gibbs 713ee9c90c7SKenneth D. Merry if (softc->flags & PASS_FLAG_INVALID) { 714c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7152b83592fSScott Long cam_periph_unlock(periph); 71676babe50SJustin T. Gibbs return(ENXIO); 717ee9c90c7SKenneth D. Merry } 71822b9c86cSKenneth D. Merry 71922b9c86cSKenneth D. Merry /* 720f5ef42beSRobert Watson * Don't allow access when we're running at a high securelevel. 72122b9c86cSKenneth D. Merry */ 722a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 1); 723f7312ca2SRobert Watson if (error) { 724c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7252b83592fSScott Long cam_periph_unlock(periph); 726f7312ca2SRobert Watson return(error); 72722b9c86cSKenneth D. Merry } 72876babe50SJustin T. Gibbs 72976babe50SJustin T. Gibbs /* 73066a0780eSKenneth D. Merry * Only allow read-write access. 73166a0780eSKenneth D. Merry */ 73222b9c86cSKenneth D. Merry if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 733c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7342b83592fSScott Long cam_periph_unlock(periph); 73566a0780eSKenneth D. Merry return(EPERM); 73622b9c86cSKenneth D. Merry } 73766a0780eSKenneth D. Merry 73866a0780eSKenneth D. Merry /* 73976babe50SJustin T. Gibbs * We don't allow nonblocking access. 74076babe50SJustin T. Gibbs */ 74176babe50SJustin T. Gibbs if ((flags & O_NONBLOCK) != 0) { 742f0d9af51SMatt Jacob xpt_print(periph->path, "can't do nonblocking access\n"); 743c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7442b83592fSScott Long cam_periph_unlock(periph); 74522b9c86cSKenneth D. Merry return(EINVAL); 74676babe50SJustin T. Gibbs } 74776babe50SJustin T. Gibbs 74886d45c7fSKenneth D. Merry softc->open_count++; 74986d45c7fSKenneth D. Merry 750835187bfSScott Long cam_periph_unlock(periph); 75176babe50SJustin T. Gibbs 75276babe50SJustin T. Gibbs return (error); 75376babe50SJustin T. Gibbs } 75476babe50SJustin T. Gibbs 75576babe50SJustin T. Gibbs static int 75689c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td) 75776babe50SJustin T. Gibbs { 75876babe50SJustin T. Gibbs struct cam_periph *periph; 75986d45c7fSKenneth D. Merry struct pass_softc *softc; 760227d67aaSAlexander Motin struct mtx *mtx; 76176babe50SJustin T. Gibbs 762e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 763227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 764227d67aaSAlexander Motin mtx_lock(mtx); 76576babe50SJustin T. Gibbs 76686d45c7fSKenneth D. Merry softc = periph->softc; 76786d45c7fSKenneth D. Merry softc->open_count--; 76886d45c7fSKenneth D. Merry 769a9934668SKenneth D. Merry if (softc->open_count == 0) { 770a9934668SKenneth D. Merry struct pass_io_req *io_req, *io_req2; 771a9934668SKenneth D. Merry 772a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 773a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 774a9934668SKenneth D. Merry passiocleanup(softc, io_req); 775a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 776a9934668SKenneth D. Merry } 777a9934668SKenneth D. Merry 778a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, 779a9934668SKenneth D. Merry io_req2) { 780a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 781a9934668SKenneth D. Merry passiocleanup(softc, io_req); 782a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 783a9934668SKenneth D. Merry } 784a9934668SKenneth D. Merry 785a9934668SKenneth D. Merry /* 786a9934668SKenneth D. Merry * If there are any active I/Os, we need to forcibly acquire a 787a9934668SKenneth D. Merry * reference to the peripheral so that we don't go away 788a9934668SKenneth D. Merry * before they complete. We'll release the reference when 789a9934668SKenneth D. Merry * the abandoned queue is empty. 790a9934668SKenneth D. Merry */ 791a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->active_queue); 792a9934668SKenneth D. Merry if ((io_req != NULL) 793a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) { 794a9934668SKenneth D. Merry cam_periph_doacquire(periph); 795a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 796a9934668SKenneth D. Merry } 797a9934668SKenneth D. Merry 798a9934668SKenneth D. Merry /* 799a9934668SKenneth D. Merry * Since the I/O in the active queue is not under our 800a9934668SKenneth D. Merry * control, just set a flag so that we can clean it up when 801a9934668SKenneth D. Merry * it completes and put it on the abandoned queue. This 802a9934668SKenneth D. Merry * will prevent our sending spurious completions in the 803a9934668SKenneth D. Merry * event that the device is opened again before these I/Os 804a9934668SKenneth D. Merry * complete. 805a9934668SKenneth D. Merry */ 806a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, 807a9934668SKenneth D. Merry io_req2) { 808a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 809a9934668SKenneth D. Merry io_req->flags |= PASS_IO_ABANDONED; 810a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, 811a9934668SKenneth D. Merry links); 812a9934668SKenneth D. Merry } 813a9934668SKenneth D. Merry } 814a9934668SKenneth D. Merry 81586d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 81686d45c7fSKenneth D. Merry 81786d45c7fSKenneth D. Merry /* 818227d67aaSAlexander Motin * We reference the lock directly here, instead of using 81986d45c7fSKenneth D. Merry * cam_periph_unlock(). The reason is that the call to 82086d45c7fSKenneth D. Merry * cam_periph_release_locked() above could result in the periph 82186d45c7fSKenneth D. Merry * getting freed. If that is the case, dereferencing the periph 82286d45c7fSKenneth D. Merry * with a cam_periph_unlock() call would cause a page fault. 82386d45c7fSKenneth D. Merry * 82486d45c7fSKenneth D. Merry * cam_periph_release() avoids this problem using the same method, 82586d45c7fSKenneth D. Merry * but we're manually acquiring and dropping the lock here to 82686d45c7fSKenneth D. Merry * protect the open count and avoid another lock acquisition and 82786d45c7fSKenneth D. Merry * release. 82886d45c7fSKenneth D. Merry */ 829227d67aaSAlexander Motin mtx_unlock(mtx); 83076babe50SJustin T. Gibbs 83176babe50SJustin T. Gibbs return (0); 83276babe50SJustin T. Gibbs } 83376babe50SJustin T. Gibbs 834a9934668SKenneth D. Merry 835a9934668SKenneth D. Merry static void 836a9934668SKenneth D. Merry passstart(struct cam_periph *periph, union ccb *start_ccb) 837a9934668SKenneth D. Merry { 838a9934668SKenneth D. Merry struct pass_softc *softc; 839a9934668SKenneth D. Merry 840a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 841a9934668SKenneth D. Merry 842a9934668SKenneth D. Merry switch (softc->state) { 843a9934668SKenneth D. Merry case PASS_STATE_NORMAL: { 844a9934668SKenneth D. Merry struct pass_io_req *io_req; 845a9934668SKenneth D. Merry 846a9934668SKenneth D. Merry /* 847a9934668SKenneth D. Merry * Check for any queued I/O requests that require an 848a9934668SKenneth D. Merry * allocated slot. 849a9934668SKenneth D. Merry */ 850a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->incoming_queue); 851a9934668SKenneth D. Merry if (io_req == NULL) { 852a9934668SKenneth D. Merry xpt_release_ccb(start_ccb); 853a9934668SKenneth D. Merry break; 854a9934668SKenneth D. Merry } 855a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 856a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 857a9934668SKenneth D. Merry /* 858a9934668SKenneth D. Merry * Merge the user's CCB into the allocated CCB. 859a9934668SKenneth D. Merry */ 860a9934668SKenneth D. Merry xpt_merge_ccb(start_ccb, &io_req->ccb); 861a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO; 862a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_ioreq = io_req; 863a9934668SKenneth D. Merry start_ccb->ccb_h.cbfcnp = passdone; 864a9934668SKenneth D. Merry io_req->alloced_ccb = start_ccb; 865a9934668SKenneth D. Merry binuptime(&io_req->start_time); 866a9934668SKenneth D. Merry devstat_start_transaction(softc->device_stats, 867a9934668SKenneth D. Merry &io_req->start_time); 868a9934668SKenneth D. Merry 869a9934668SKenneth D. Merry xpt_action(start_ccb); 870a9934668SKenneth D. Merry 871a9934668SKenneth D. Merry /* 872a9934668SKenneth D. Merry * If we have any more I/O waiting, schedule ourselves again. 873a9934668SKenneth D. Merry */ 874a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->incoming_queue)) 875a9934668SKenneth D. Merry xpt_schedule(periph, CAM_PRIORITY_NORMAL); 876a9934668SKenneth D. Merry break; 877a9934668SKenneth D. Merry } 878a9934668SKenneth D. Merry default: 879a9934668SKenneth D. Merry break; 880a9934668SKenneth D. Merry } 881a9934668SKenneth D. Merry } 882a9934668SKenneth D. Merry 883a9934668SKenneth D. Merry static void 884a9934668SKenneth D. Merry passdone(struct cam_periph *periph, union ccb *done_ccb) 885a9934668SKenneth D. Merry { 886a9934668SKenneth D. Merry struct pass_softc *softc; 887a9934668SKenneth D. Merry struct ccb_scsiio *csio; 888a9934668SKenneth D. Merry 889a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 890a9934668SKenneth D. Merry 891a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 892a9934668SKenneth D. Merry 893a9934668SKenneth D. Merry csio = &done_ccb->csio; 894a9934668SKenneth D. Merry switch (csio->ccb_h.ccb_type) { 895a9934668SKenneth D. Merry case PASS_CCB_QUEUED_IO: { 896a9934668SKenneth D. Merry struct pass_io_req *io_req; 897a9934668SKenneth D. Merry 898a9934668SKenneth D. Merry io_req = done_ccb->ccb_h.ccb_ioreq; 899a9934668SKenneth D. Merry #if 0 900a9934668SKenneth D. Merry xpt_print(periph->path, "%s: called for user CCB %p\n", 901a9934668SKenneth D. Merry __func__, io_req->user_ccb_ptr); 902a9934668SKenneth D. Merry #endif 903a9934668SKenneth D. Merry if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 904a9934668SKenneth D. Merry && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) 905a9934668SKenneth D. Merry && ((io_req->flags & PASS_IO_ABANDONED) == 0)) { 906a9934668SKenneth D. Merry int error; 907a9934668SKenneth D. Merry 908a9934668SKenneth D. Merry error = passerror(done_ccb, CAM_RETRY_SELTO, 909a9934668SKenneth D. Merry SF_RETRY_UA | SF_NO_PRINT); 910a9934668SKenneth D. Merry 911a9934668SKenneth D. Merry if (error == ERESTART) { 912a9934668SKenneth D. Merry /* 913a9934668SKenneth D. Merry * A retry was scheduled, so 914a9934668SKenneth D. Merry * just return. 915a9934668SKenneth D. Merry */ 916a9934668SKenneth D. Merry return; 917a9934668SKenneth D. Merry } 918a9934668SKenneth D. Merry } 919a9934668SKenneth D. Merry 920a9934668SKenneth D. Merry /* 921a9934668SKenneth D. Merry * Copy the allocated CCB contents back to the malloced CCB 922a9934668SKenneth D. Merry * so we can give status back to the user when he requests it. 923a9934668SKenneth D. Merry */ 924a9934668SKenneth D. Merry bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb)); 925a9934668SKenneth D. Merry 926a9934668SKenneth D. Merry /* 927a9934668SKenneth D. Merry * Log data/transaction completion with devstat(9). 928a9934668SKenneth D. Merry */ 929a9934668SKenneth D. Merry switch (done_ccb->ccb_h.func_code) { 930a9934668SKenneth D. Merry case XPT_SCSI_IO: 931a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 932a9934668SKenneth D. Merry done_ccb->csio.dxfer_len - done_ccb->csio.resid, 933a9934668SKenneth D. Merry done_ccb->csio.tag_action & 0x3, 934a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 935a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 936a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 937a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 938a9934668SKenneth D. Merry &io_req->start_time); 939a9934668SKenneth D. Merry break; 940a9934668SKenneth D. Merry case XPT_ATA_IO: 941a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 942a9934668SKenneth D. Merry done_ccb->ataio.dxfer_len - done_ccb->ataio.resid, 943e4cc6558SWarner Losh 0, /* Not used in ATA */ 944a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 945a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 946a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 947a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 948a9934668SKenneth D. Merry &io_req->start_time); 949a9934668SKenneth D. Merry break; 950a9934668SKenneth D. Merry case XPT_SMP_IO: 951a9934668SKenneth D. Merry /* 952a9934668SKenneth D. Merry * XXX KDM this isn't quite right, but there isn't 953a9934668SKenneth D. Merry * currently an easy way to represent a bidirectional 954a9934668SKenneth D. Merry * transfer in devstat. The only way to do it 955a9934668SKenneth D. Merry * and have the byte counts come out right would 956a9934668SKenneth D. Merry * mean that we would have to record two 957a9934668SKenneth D. Merry * transactions, one for the request and one for the 958a9934668SKenneth D. Merry * response. For now, so that we report something, 959a9934668SKenneth D. Merry * just treat the entire thing as a read. 960a9934668SKenneth D. Merry */ 961a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 962a9934668SKenneth D. Merry done_ccb->smpio.smp_request_len + 963a9934668SKenneth D. Merry done_ccb->smpio.smp_response_len, 964a9934668SKenneth D. Merry DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL, 965a9934668SKenneth D. Merry &io_req->start_time); 966a9934668SKenneth D. Merry break; 967a9934668SKenneth D. Merry default: 968a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 0, 969a9934668SKenneth D. Merry DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL, 970a9934668SKenneth D. Merry &io_req->start_time); 971a9934668SKenneth D. Merry break; 972a9934668SKenneth D. Merry } 973a9934668SKenneth D. Merry 974a9934668SKenneth D. Merry /* 975a9934668SKenneth D. Merry * In the normal case, take the completed I/O off of the 976a9934668SKenneth D. Merry * active queue and put it on the done queue. Notitfy the 977a9934668SKenneth D. Merry * user that we have a completed I/O. 978a9934668SKenneth D. Merry */ 979a9934668SKenneth D. Merry if ((io_req->flags & PASS_IO_ABANDONED) == 0) { 980a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 981a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 982a9934668SKenneth D. Merry selwakeuppri(&softc->read_select, PRIBIO); 983a9934668SKenneth D. Merry KNOTE_LOCKED(&softc->read_select.si_note, 0); 984a9934668SKenneth D. Merry } else { 985a9934668SKenneth D. Merry /* 986a9934668SKenneth D. Merry * In the case of an abandoned I/O (final close 987a9934668SKenneth D. Merry * without fetching the I/O), take it off of the 988a9934668SKenneth D. Merry * abandoned queue and free it. 989a9934668SKenneth D. Merry */ 990a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->abandoned_queue, io_req, links); 991a9934668SKenneth D. Merry passiocleanup(softc, io_req); 992a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 993a9934668SKenneth D. Merry 994a9934668SKenneth D. Merry /* 995a9934668SKenneth D. Merry * Release the done_ccb here, since we may wind up 996a9934668SKenneth D. Merry * freeing the peripheral when we decrement the 997a9934668SKenneth D. Merry * reference count below. 998a9934668SKenneth D. Merry */ 999a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1000a9934668SKenneth D. Merry 1001a9934668SKenneth D. Merry /* 1002a9934668SKenneth D. Merry * If the abandoned queue is empty, we can release 1003a9934668SKenneth D. Merry * our reference to the periph since we won't have 1004a9934668SKenneth D. Merry * any more completions coming. 1005a9934668SKenneth D. Merry */ 1006a9934668SKenneth D. Merry if ((TAILQ_EMPTY(&softc->abandoned_queue)) 1007a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) { 1008a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET; 1009a9934668SKenneth D. Merry cam_periph_release_locked(periph); 1010a9934668SKenneth D. Merry } 1011a9934668SKenneth D. Merry 1012a9934668SKenneth D. Merry /* 1013a9934668SKenneth D. Merry * We have already released the CCB, so we can 1014a9934668SKenneth D. Merry * return. 1015a9934668SKenneth D. Merry */ 1016a9934668SKenneth D. Merry return; 1017a9934668SKenneth D. Merry } 1018a9934668SKenneth D. Merry break; 1019a9934668SKenneth D. Merry } 1020a9934668SKenneth D. Merry } 1021a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1022a9934668SKenneth D. Merry } 1023a9934668SKenneth D. Merry 1024a9934668SKenneth D. Merry static int 1025a9934668SKenneth D. Merry passcreatezone(struct cam_periph *periph) 1026a9934668SKenneth D. Merry { 1027a9934668SKenneth D. Merry struct pass_softc *softc; 1028a9934668SKenneth D. Merry int error; 1029a9934668SKenneth D. Merry 1030a9934668SKenneth D. Merry error = 0; 1031a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1032a9934668SKenneth D. Merry 1033a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 1034a9934668SKenneth D. Merry KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 1035a9934668SKenneth D. Merry ("%s called when the pass(4) zone is valid!\n", __func__)); 1036a9934668SKenneth D. Merry KASSERT((softc->pass_zone == NULL), 1037a9934668SKenneth D. Merry ("%s called when the pass(4) zone is allocated!\n", __func__)); 1038a9934668SKenneth D. Merry 1039a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) { 1040a9934668SKenneth D. Merry 1041a9934668SKenneth D. Merry /* 1042a9934668SKenneth D. Merry * We're the first context through, so we need to create 1043a9934668SKenneth D. Merry * the pass(4) UMA zone for I/O requests. 1044a9934668SKenneth D. Merry */ 1045a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_INPROG; 1046a9934668SKenneth D. Merry 1047a9934668SKenneth D. Merry /* 1048a9934668SKenneth D. Merry * uma_zcreate() does a blocking (M_WAITOK) allocation, 1049a9934668SKenneth D. Merry * so we cannot hold a mutex while we call it. 1050a9934668SKenneth D. Merry */ 1051a9934668SKenneth D. Merry cam_periph_unlock(periph); 1052a9934668SKenneth D. Merry 1053a9934668SKenneth D. Merry softc->pass_zone = uma_zcreate(softc->zone_name, 1054a9934668SKenneth D. Merry sizeof(struct pass_io_req), NULL, NULL, NULL, NULL, 1055a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1056a9934668SKenneth D. Merry 1057a9934668SKenneth D. Merry softc->pass_io_zone = uma_zcreate(softc->io_zone_name, 1058a9934668SKenneth D. Merry softc->io_zone_size, NULL, NULL, NULL, NULL, 1059a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1060a9934668SKenneth D. Merry 1061a9934668SKenneth D. Merry cam_periph_lock(periph); 1062a9934668SKenneth D. Merry 1063a9934668SKenneth D. Merry if ((softc->pass_zone == NULL) 1064a9934668SKenneth D. Merry || (softc->pass_io_zone == NULL)) { 1065a9934668SKenneth D. Merry if (softc->pass_zone == NULL) 1066a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1067a9934668SKenneth D. Merry "IO Req UMA zone\n"); 1068a9934668SKenneth D. Merry else 1069a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1070a9934668SKenneth D. Merry "IO UMA zone\n"); 1071a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ZONE_INPROG; 1072a9934668SKenneth D. Merry goto bailout; 1073a9934668SKenneth D. Merry } 1074a9934668SKenneth D. Merry 1075a9934668SKenneth D. Merry /* 1076a9934668SKenneth D. Merry * Set the flags appropriately and notify any other waiters. 1077a9934668SKenneth D. Merry */ 1078a9934668SKenneth D. Merry softc->flags &= PASS_FLAG_ZONE_INPROG; 1079a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_VALID; 1080a9934668SKenneth D. Merry wakeup(&softc->pass_zone); 1081a9934668SKenneth D. Merry } else { 1082a9934668SKenneth D. Merry /* 1083a9934668SKenneth D. Merry * In this case, the UMA zone has not yet been created, but 1084a9934668SKenneth D. Merry * another context is in the process of creating it. We 1085a9934668SKenneth D. Merry * need to sleep until the creation is either done or has 1086a9934668SKenneth D. Merry * failed. 1087a9934668SKenneth D. Merry */ 1088a9934668SKenneth D. Merry while ((softc->flags & PASS_FLAG_ZONE_INPROG) 1089a9934668SKenneth D. Merry && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) { 1090a9934668SKenneth D. Merry error = msleep(&softc->pass_zone, 1091a9934668SKenneth D. Merry cam_periph_mtx(periph), PRIBIO, 1092a9934668SKenneth D. Merry "paszon", 0); 1093a9934668SKenneth D. Merry if (error != 0) 1094a9934668SKenneth D. Merry goto bailout; 1095a9934668SKenneth D. Merry } 1096a9934668SKenneth D. Merry /* 1097a9934668SKenneth D. Merry * If the zone creation failed, no luck for the user. 1098a9934668SKenneth D. Merry */ 1099a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){ 1100a9934668SKenneth D. Merry error = ENOMEM; 1101a9934668SKenneth D. Merry goto bailout; 1102a9934668SKenneth D. Merry } 1103a9934668SKenneth D. Merry } 1104a9934668SKenneth D. Merry bailout: 1105a9934668SKenneth D. Merry return (error); 1106a9934668SKenneth D. Merry } 1107a9934668SKenneth D. Merry 1108a9934668SKenneth D. Merry static void 1109a9934668SKenneth D. Merry passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req) 1110a9934668SKenneth D. Merry { 1111a9934668SKenneth D. Merry union ccb *ccb; 1112a9934668SKenneth D. Merry u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1113a9934668SKenneth D. Merry int i, numbufs; 1114a9934668SKenneth D. Merry 1115a9934668SKenneth D. Merry ccb = &io_req->ccb; 1116a9934668SKenneth D. Merry 1117a9934668SKenneth D. Merry switch (ccb->ccb_h.func_code) { 1118a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1119a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1120a9934668SKenneth D. Merry 1121a9934668SKenneth D. Merry if (numbufs == 1) { 1122a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1123a9934668SKenneth D. Merry } else { 1124a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1125a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1126a9934668SKenneth D. Merry } 1127a9934668SKenneth D. Merry break; 1128a9934668SKenneth D. Merry case XPT_SCSI_IO: 1129a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1130a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1131a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1132a9934668SKenneth D. Merry break; 1133a9934668SKenneth D. Merry case XPT_ATA_IO: 1134a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1135a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1136a9934668SKenneth D. Merry break; 1137a9934668SKenneth D. Merry case XPT_SMP_IO: 1138a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1139a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1140a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1141a9934668SKenneth D. Merry break; 1142a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1143a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1144a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1145a9934668SKenneth D. Merry break; 1146df424515SWarner Losh case XPT_NVME_IO: 1147df424515SWarner Losh case XPT_NVME_ADMIN: 1148df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1149df424515SWarner Losh numbufs = min(io_req->num_bufs, 1); 1150df424515SWarner Losh break; 1151a9934668SKenneth D. Merry default: 1152a9934668SKenneth D. Merry /* allow ourselves to be swapped once again */ 1153a9934668SKenneth D. Merry return; 1154a9934668SKenneth D. Merry break; /* NOTREACHED */ 1155a9934668SKenneth D. Merry } 1156a9934668SKenneth D. Merry 1157a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_USER_SEG_MALLOC) { 1158a9934668SKenneth D. Merry free(io_req->user_segptr, M_SCSIPASS); 1159a9934668SKenneth D. Merry io_req->user_segptr = NULL; 1160a9934668SKenneth D. Merry } 1161a9934668SKenneth D. Merry 1162a9934668SKenneth D. Merry /* 1163a9934668SKenneth D. Merry * We only want to free memory we malloced. 1164a9934668SKenneth D. Merry */ 1165a9934668SKenneth D. Merry if (io_req->data_flags == CAM_DATA_VADDR) { 1166a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1167a9934668SKenneth D. Merry if (io_req->kern_bufs[i] == NULL) 1168a9934668SKenneth D. Merry continue; 1169a9934668SKenneth D. Merry 1170a9934668SKenneth D. Merry free(io_req->kern_bufs[i], M_SCSIPASS); 1171a9934668SKenneth D. Merry io_req->kern_bufs[i] = NULL; 1172a9934668SKenneth D. Merry } 1173a9934668SKenneth D. Merry } else if (io_req->data_flags == CAM_DATA_SG) { 1174a9934668SKenneth D. Merry for (i = 0; i < io_req->num_kern_segs; i++) { 1175a9934668SKenneth D. Merry if ((uint8_t *)(uintptr_t) 1176a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr == NULL) 1177a9934668SKenneth D. Merry continue; 1178a9934668SKenneth D. Merry 1179a9934668SKenneth D. Merry uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t) 1180a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr); 1181a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 0; 1182a9934668SKenneth D. Merry } 1183a9934668SKenneth D. Merry } 1184a9934668SKenneth D. Merry 1185a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) { 1186a9934668SKenneth D. Merry free(io_req->kern_segptr, M_SCSIPASS); 1187a9934668SKenneth D. Merry io_req->kern_segptr = NULL; 1188a9934668SKenneth D. Merry } 1189a9934668SKenneth D. Merry 1190a9934668SKenneth D. Merry if (io_req->data_flags != CAM_DATA_PADDR) { 1191a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1192a9934668SKenneth D. Merry /* 1193a9934668SKenneth D. Merry * Restore the user's buffer pointers to their 1194a9934668SKenneth D. Merry * previous values. 1195a9934668SKenneth D. Merry */ 1196a9934668SKenneth D. Merry if (io_req->user_bufs[i] != NULL) 1197a9934668SKenneth D. Merry *data_ptrs[i] = io_req->user_bufs[i]; 1198a9934668SKenneth D. Merry } 1199a9934668SKenneth D. Merry } 1200a9934668SKenneth D. Merry 1201a9934668SKenneth D. Merry } 1202a9934668SKenneth D. Merry 1203a9934668SKenneth D. Merry static int 1204a9934668SKenneth D. Merry passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req, 1205a9934668SKenneth D. Merry ccb_flags direction) 1206a9934668SKenneth D. Merry { 1207a9934668SKenneth D. Merry bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy; 1208a9934668SKenneth D. Merry bus_dma_segment_t *user_sglist, *kern_sglist; 1209a9934668SKenneth D. Merry int i, j, error; 1210a9934668SKenneth D. Merry 1211a9934668SKenneth D. Merry error = 0; 1212a9934668SKenneth D. Merry kern_watermark = 0; 1213a9934668SKenneth D. Merry user_watermark = 0; 1214a9934668SKenneth D. Merry len_to_copy = 0; 1215a9934668SKenneth D. Merry len_copied = 0; 1216a9934668SKenneth D. Merry user_sglist = io_req->user_segptr; 1217a9934668SKenneth D. Merry kern_sglist = io_req->kern_segptr; 1218a9934668SKenneth D. Merry 1219a9934668SKenneth D. Merry for (i = 0, j = 0; i < io_req->num_user_segs && 1220a9934668SKenneth D. Merry j < io_req->num_kern_segs;) { 1221a9934668SKenneth D. Merry uint8_t *user_ptr, *kern_ptr; 1222a9934668SKenneth D. Merry 1223a9934668SKenneth D. Merry len_to_copy = min(user_sglist[i].ds_len -user_watermark, 1224a9934668SKenneth D. Merry kern_sglist[j].ds_len - kern_watermark); 1225a9934668SKenneth D. Merry 1226a9934668SKenneth D. Merry user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr; 1227a9934668SKenneth D. Merry user_ptr = user_ptr + user_watermark; 1228a9934668SKenneth D. Merry kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr; 1229a9934668SKenneth D. Merry kern_ptr = kern_ptr + kern_watermark; 1230a9934668SKenneth D. Merry 1231a9934668SKenneth D. Merry user_watermark += len_to_copy; 1232a9934668SKenneth D. Merry kern_watermark += len_to_copy; 1233a9934668SKenneth D. Merry 1234a9934668SKenneth D. Merry if (!useracc(user_ptr, len_to_copy, 1235a9934668SKenneth D. Merry (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) { 1236a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1237a9934668SKenneth D. Merry "S/G list element %p len %zu\n", __func__, 1238a9934668SKenneth D. Merry user_ptr, len_to_copy); 1239a9934668SKenneth D. Merry error = EFAULT; 1240a9934668SKenneth D. Merry goto bailout; 1241a9934668SKenneth D. Merry } 1242a9934668SKenneth D. Merry 1243a9934668SKenneth D. Merry if (direction == CAM_DIR_IN) { 1244a9934668SKenneth D. Merry error = copyout(kern_ptr, user_ptr, len_to_copy); 1245a9934668SKenneth D. Merry if (error != 0) { 1246a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyout of %u " 1247a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1248a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1249a9934668SKenneth D. Merry kern_ptr, user_ptr, error); 1250a9934668SKenneth D. Merry goto bailout; 1251a9934668SKenneth D. Merry } 1252a9934668SKenneth D. Merry } else { 1253a9934668SKenneth D. Merry error = copyin(user_ptr, kern_ptr, len_to_copy); 1254a9934668SKenneth D. Merry if (error != 0) { 1255a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyin of %u " 1256a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1257a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1258a9934668SKenneth D. Merry user_ptr, kern_ptr, error); 1259a9934668SKenneth D. Merry goto bailout; 1260a9934668SKenneth D. Merry } 1261a9934668SKenneth D. Merry } 1262a9934668SKenneth D. Merry 1263a9934668SKenneth D. Merry len_copied += len_to_copy; 1264a9934668SKenneth D. Merry 1265a9934668SKenneth D. Merry if (user_sglist[i].ds_len == user_watermark) { 1266a9934668SKenneth D. Merry i++; 1267a9934668SKenneth D. Merry user_watermark = 0; 1268a9934668SKenneth D. Merry } 1269a9934668SKenneth D. Merry 1270a9934668SKenneth D. Merry if (kern_sglist[j].ds_len == kern_watermark) { 1271a9934668SKenneth D. Merry j++; 1272a9934668SKenneth D. Merry kern_watermark = 0; 1273a9934668SKenneth D. Merry } 1274a9934668SKenneth D. Merry } 1275a9934668SKenneth D. Merry 1276a9934668SKenneth D. Merry bailout: 1277a9934668SKenneth D. Merry 1278a9934668SKenneth D. Merry return (error); 1279a9934668SKenneth D. Merry } 1280a9934668SKenneth D. Merry 1281a9934668SKenneth D. Merry static int 1282a9934668SKenneth D. Merry passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req) 1283a9934668SKenneth D. Merry { 1284a9934668SKenneth D. Merry union ccb *ccb; 1285a9934668SKenneth D. Merry struct pass_softc *softc; 1286a9934668SKenneth D. Merry int numbufs, i; 1287a9934668SKenneth D. Merry uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1288a9934668SKenneth D. Merry uint32_t lengths[CAM_PERIPH_MAXMAPS]; 1289a9934668SKenneth D. Merry uint32_t dirs[CAM_PERIPH_MAXMAPS]; 1290a9934668SKenneth D. Merry uint32_t num_segs; 1291a9934668SKenneth D. Merry uint16_t *seg_cnt_ptr; 1292a9934668SKenneth D. Merry size_t maxmap; 1293a9934668SKenneth D. Merry int error; 1294a9934668SKenneth D. Merry 1295a9934668SKenneth D. Merry cam_periph_assert(periph, MA_NOTOWNED); 1296a9934668SKenneth D. Merry 1297a9934668SKenneth D. Merry softc = periph->softc; 1298a9934668SKenneth D. Merry 1299a9934668SKenneth D. Merry error = 0; 1300a9934668SKenneth D. Merry ccb = &io_req->ccb; 1301a9934668SKenneth D. Merry maxmap = 0; 1302a9934668SKenneth D. Merry num_segs = 0; 1303a9934668SKenneth D. Merry seg_cnt_ptr = NULL; 1304a9934668SKenneth D. Merry 1305a9934668SKenneth D. Merry switch(ccb->ccb_h.func_code) { 1306a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1307a9934668SKenneth D. Merry if (ccb->cdm.match_buf_len == 0) { 1308a9934668SKenneth D. Merry printf("%s: invalid match buffer length 0\n", __func__); 1309a9934668SKenneth D. Merry return(EINVAL); 1310a9934668SKenneth D. Merry } 1311a9934668SKenneth D. Merry if (ccb->cdm.pattern_buf_len > 0) { 1312a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1313a9934668SKenneth D. Merry lengths[0] = ccb->cdm.pattern_buf_len; 1314a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1315a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1316a9934668SKenneth D. Merry lengths[1] = ccb->cdm.match_buf_len; 1317a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1318a9934668SKenneth D. Merry numbufs = 2; 1319a9934668SKenneth D. Merry } else { 1320a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1321a9934668SKenneth D. Merry lengths[0] = ccb->cdm.match_buf_len; 1322a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1323a9934668SKenneth D. Merry numbufs = 1; 1324a9934668SKenneth D. Merry } 1325a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1326a9934668SKenneth D. Merry break; 1327a9934668SKenneth D. Merry case XPT_SCSI_IO: 1328a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1329a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1330a9934668SKenneth D. Merry return(0); 1331a9934668SKenneth D. Merry 1332a9934668SKenneth D. Merry /* 1333a9934668SKenneth D. Merry * The user shouldn't be able to supply a bio. 1334a9934668SKenneth D. Merry */ 1335a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) 1336a9934668SKenneth D. Merry return (EINVAL); 1337a9934668SKenneth D. Merry 1338a9934668SKenneth D. Merry io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1339a9934668SKenneth D. Merry 1340a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1341a9934668SKenneth D. Merry lengths[0] = ccb->csio.dxfer_len; 1342a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1343a9934668SKenneth D. Merry num_segs = ccb->csio.sglist_cnt; 1344a9934668SKenneth D. Merry seg_cnt_ptr = &ccb->csio.sglist_cnt; 1345a9934668SKenneth D. Merry numbufs = 1; 1346a9934668SKenneth D. Merry maxmap = softc->maxio; 1347a9934668SKenneth D. Merry break; 1348a9934668SKenneth D. Merry case XPT_ATA_IO: 1349a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1350a9934668SKenneth D. Merry return(0); 1351a9934668SKenneth D. Merry 1352a9934668SKenneth D. Merry /* 1353a9934668SKenneth D. Merry * We only support a single virtual address for ATA I/O. 1354a9934668SKenneth D. Merry */ 1355a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 1356a9934668SKenneth D. Merry return (EINVAL); 1357a9934668SKenneth D. Merry 1358a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1359a9934668SKenneth D. Merry 1360a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1361a9934668SKenneth D. Merry lengths[0] = ccb->ataio.dxfer_len; 1362a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1363a9934668SKenneth D. Merry numbufs = 1; 1364a9934668SKenneth D. Merry maxmap = softc->maxio; 1365a9934668SKenneth D. Merry break; 1366a9934668SKenneth D. Merry case XPT_SMP_IO: 1367a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1368a9934668SKenneth D. Merry 1369a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1370a9934668SKenneth D. Merry lengths[0] = ccb->smpio.smp_request_len; 1371a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1372a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1373a9934668SKenneth D. Merry lengths[1] = ccb->smpio.smp_response_len; 1374a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1375a9934668SKenneth D. Merry numbufs = 2; 1376a9934668SKenneth D. Merry maxmap = softc->maxio; 1377a9934668SKenneth D. Merry break; 1378a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1379a9934668SKenneth D. Merry if (ccb->cdai.bufsiz == 0) 1380a9934668SKenneth D. Merry return (0); 1381a9934668SKenneth D. Merry 1382a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1383a9934668SKenneth D. Merry 1384a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1385a9934668SKenneth D. Merry lengths[0] = ccb->cdai.bufsiz; 1386a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1387a9934668SKenneth D. Merry numbufs = 1; 1388a9934668SKenneth D. Merry break; 1389df424515SWarner Losh case XPT_NVME_ADMIN: 1390df424515SWarner Losh case XPT_NVME_IO: 1391df424515SWarner Losh if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1392df424515SWarner Losh return (0); 1393df424515SWarner Losh 1394df424515SWarner Losh io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1395df424515SWarner Losh 1396df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1397df424515SWarner Losh lengths[0] = ccb->nvmeio.dxfer_len; 1398df424515SWarner Losh dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 139951977281SWarner Losh num_segs = ccb->nvmeio.sglist_cnt; 140051977281SWarner Losh seg_cnt_ptr = &ccb->nvmeio.sglist_cnt; 1401df424515SWarner Losh numbufs = 1; 1402df424515SWarner Losh maxmap = softc->maxio; 1403df424515SWarner Losh break; 1404a9934668SKenneth D. Merry default: 1405a9934668SKenneth D. Merry return(EINVAL); 1406a9934668SKenneth D. Merry break; /* NOTREACHED */ 1407a9934668SKenneth D. Merry } 1408a9934668SKenneth D. Merry 1409a9934668SKenneth D. Merry io_req->num_bufs = numbufs; 1410a9934668SKenneth D. Merry 1411a9934668SKenneth D. Merry /* 1412a9934668SKenneth D. Merry * If there is a maximum, check to make sure that the user's 1413a9934668SKenneth D. Merry * request fits within the limit. In general, we should only have 1414a9934668SKenneth D. Merry * a maximum length for requests that go to hardware. Otherwise it 1415a9934668SKenneth D. Merry * is whatever we're able to malloc. 1416a9934668SKenneth D. Merry */ 1417a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1418a9934668SKenneth D. Merry io_req->user_bufs[i] = *data_ptrs[i]; 1419a9934668SKenneth D. Merry io_req->dirs[i] = dirs[i]; 1420a9934668SKenneth D. Merry io_req->lengths[i] = lengths[i]; 1421a9934668SKenneth D. Merry 1422a9934668SKenneth D. Merry if (maxmap == 0) 1423a9934668SKenneth D. Merry continue; 1424a9934668SKenneth D. Merry 1425a9934668SKenneth D. Merry if (lengths[i] <= maxmap) 1426a9934668SKenneth D. Merry continue; 1427a9934668SKenneth D. Merry 1428a9934668SKenneth D. Merry xpt_print(periph->path, "%s: data length %u > max allowed %u " 1429a9934668SKenneth D. Merry "bytes\n", __func__, lengths[i], maxmap); 1430a9934668SKenneth D. Merry error = EINVAL; 1431a9934668SKenneth D. Merry goto bailout; 1432a9934668SKenneth D. Merry } 1433a9934668SKenneth D. Merry 1434a9934668SKenneth D. Merry switch (io_req->data_flags) { 1435a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1436a9934668SKenneth D. Merry /* Map or copy the buffer into kernel address space */ 1437a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1438a9934668SKenneth D. Merry uint8_t *tmp_buf; 1439a9934668SKenneth D. Merry 1440a9934668SKenneth D. Merry /* 1441a9934668SKenneth D. Merry * If for some reason no length is specified, we 1442a9934668SKenneth D. Merry * don't need to allocate anything. 1443a9934668SKenneth D. Merry */ 1444a9934668SKenneth D. Merry if (io_req->lengths[i] == 0) 1445a9934668SKenneth D. Merry continue; 1446a9934668SKenneth D. Merry 1447a9934668SKenneth D. Merry /* 1448a9934668SKenneth D. Merry * Make sure that the user's buffer is accessible 1449a9934668SKenneth D. Merry * to that process. 1450a9934668SKenneth D. Merry */ 1451a9934668SKenneth D. Merry if (!useracc(io_req->user_bufs[i], io_req->lengths[i], 1452a9934668SKenneth D. Merry (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE : 1453a9934668SKenneth D. Merry VM_PROT_READ)) { 1454a9934668SKenneth D. Merry xpt_print(periph->path, "%s: user address %p " 1455a9934668SKenneth D. Merry "length %u is not accessible\n", __func__, 1456a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1457a9934668SKenneth D. Merry error = EFAULT; 1458a9934668SKenneth D. Merry goto bailout; 1459a9934668SKenneth D. Merry } 1460a9934668SKenneth D. Merry 1461a9934668SKenneth D. Merry tmp_buf = malloc(lengths[i], M_SCSIPASS, 1462a9934668SKenneth D. Merry M_WAITOK | M_ZERO); 1463a9934668SKenneth D. Merry io_req->kern_bufs[i] = tmp_buf; 1464a9934668SKenneth D. Merry *data_ptrs[i] = tmp_buf; 1465a9934668SKenneth D. Merry 1466a9934668SKenneth D. Merry #if 0 1467a9934668SKenneth D. Merry xpt_print(periph->path, "%s: malloced %p len %u, user " 1468a9934668SKenneth D. Merry "buffer %p, operation: %s\n", __func__, 1469a9934668SKenneth D. Merry tmp_buf, lengths[i], io_req->user_bufs[i], 1470a9934668SKenneth D. Merry (dirs[i] == CAM_DIR_IN) ? "read" : "write"); 1471a9934668SKenneth D. Merry #endif 1472a9934668SKenneth D. Merry /* 1473a9934668SKenneth D. Merry * We only need to copy in if the user is writing. 1474a9934668SKenneth D. Merry */ 1475a9934668SKenneth D. Merry if (dirs[i] != CAM_DIR_OUT) 1476a9934668SKenneth D. Merry continue; 1477a9934668SKenneth D. Merry 1478a9934668SKenneth D. Merry error = copyin(io_req->user_bufs[i], 1479a9934668SKenneth D. Merry io_req->kern_bufs[i], lengths[i]); 1480a9934668SKenneth D. Merry if (error != 0) { 1481a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user " 1482a9934668SKenneth D. Merry "buffer from %p to %p failed with " 1483a9934668SKenneth D. Merry "error %d\n", __func__, 1484a9934668SKenneth D. Merry io_req->user_bufs[i], 1485a9934668SKenneth D. Merry io_req->kern_bufs[i], error); 1486a9934668SKenneth D. Merry goto bailout; 1487a9934668SKenneth D. Merry } 1488a9934668SKenneth D. Merry } 1489a9934668SKenneth D. Merry break; 1490a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1491a9934668SKenneth D. Merry /* Pass down the pointer as-is */ 1492a9934668SKenneth D. Merry break; 1493a9934668SKenneth D. Merry case CAM_DATA_SG: { 1494a9934668SKenneth D. Merry size_t sg_length, size_to_go, alloc_size; 1495a9934668SKenneth D. Merry uint32_t num_segs_needed; 1496a9934668SKenneth D. Merry 1497a9934668SKenneth D. Merry /* 1498a9934668SKenneth D. Merry * Copy the user S/G list in, and then copy in the 1499a9934668SKenneth D. Merry * individual segments. 1500a9934668SKenneth D. Merry */ 1501a9934668SKenneth D. Merry /* 1502a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1503a9934668SKenneth D. Merry */ 1504a9934668SKenneth D. Merry if (numbufs != 1) { 1505a9934668SKenneth D. Merry xpt_print(periph->path, "%s: cannot currently handle " 1506a9934668SKenneth D. Merry "more than one S/G list per CCB\n", __func__); 1507a9934668SKenneth D. Merry error = EINVAL; 1508a9934668SKenneth D. Merry goto bailout; 1509a9934668SKenneth D. Merry } 1510a9934668SKenneth D. Merry 1511a9934668SKenneth D. Merry /* 1512a9934668SKenneth D. Merry * We have to have at least one segment. 1513a9934668SKenneth D. Merry */ 1514a9934668SKenneth D. Merry if (num_segs == 0) { 1515a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG flag set, " 1516a9934668SKenneth D. Merry "but sglist_cnt=0!\n", __func__); 1517a9934668SKenneth D. Merry error = EINVAL; 1518a9934668SKenneth D. Merry goto bailout; 1519a9934668SKenneth D. Merry } 1520a9934668SKenneth D. Merry 1521a9934668SKenneth D. Merry /* 1522a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1523a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1524a9934668SKenneth D. Merry */ 1525a9934668SKenneth D. Merry if (lengths[0] == 0) { 1526a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1527a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1528a9934668SKenneth D. Merry error = EINVAL; 1529a9934668SKenneth D. Merry goto bailout; 1530a9934668SKenneth D. Merry } 1531a9934668SKenneth D. Merry 1532a9934668SKenneth D. Merry /* 1533a9934668SKenneth D. Merry * We allocate buffers in io_zone_size increments for an 1534a9934668SKenneth D. Merry * S/G list. This will generally be MAXPHYS. 1535a9934668SKenneth D. Merry */ 1536a9934668SKenneth D. Merry if (lengths[0] <= softc->io_zone_size) 1537a9934668SKenneth D. Merry num_segs_needed = 1; 1538a9934668SKenneth D. Merry else { 1539a9934668SKenneth D. Merry num_segs_needed = lengths[0] / softc->io_zone_size; 1540a9934668SKenneth D. Merry if ((lengths[0] % softc->io_zone_size) != 0) 1541a9934668SKenneth D. Merry num_segs_needed++; 1542a9934668SKenneth D. Merry } 1543a9934668SKenneth D. Merry 1544a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1545a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1546a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1547a9934668SKenneth D. Merry io_req->num_kern_segs = num_segs_needed; 1548a9934668SKenneth D. Merry 1549a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1550a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1551a9934668SKenneth D. Merry 1552a9934668SKenneth D. Merry /* 1553a9934668SKenneth D. Merry * If we have enough segments allocated by default to handle 1554a9934668SKenneth D. Merry * the length of the user's S/G list, 1555a9934668SKenneth D. Merry */ 1556a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1557a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1558a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1559a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1560a9934668SKenneth D. Merry } else 1561a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1562a9934668SKenneth D. Merry 1563a9934668SKenneth D. Merry if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) { 1564a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1565a9934668SKenneth D. Merry "S/G list at %p\n", __func__, *data_ptrs[0]); 1566a9934668SKenneth D. Merry error = EFAULT; 1567a9934668SKenneth D. Merry goto bailout; 1568a9934668SKenneth D. Merry } 1569a9934668SKenneth D. Merry 1570a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1571a9934668SKenneth D. Merry if (error != 0) { 1572a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1573a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1574a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1575a9934668SKenneth D. Merry error); 1576a9934668SKenneth D. Merry goto bailout; 1577a9934668SKenneth D. Merry } 1578a9934668SKenneth D. Merry 1579a9934668SKenneth D. Merry if (num_segs_needed > PASS_MAX_SEGS) { 1580a9934668SKenneth D. Merry io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) * 1581a9934668SKenneth D. Merry num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO); 1582a9934668SKenneth D. Merry io_req->flags |= PASS_IO_KERN_SEG_MALLOC; 1583a9934668SKenneth D. Merry } else { 1584a9934668SKenneth D. Merry io_req->kern_segptr = io_req->kern_segs; 1585a9934668SKenneth D. Merry } 1586a9934668SKenneth D. Merry 1587a9934668SKenneth D. Merry /* 1588a9934668SKenneth D. Merry * Allocate the kernel S/G list. 1589a9934668SKenneth D. Merry */ 1590a9934668SKenneth D. Merry for (size_to_go = lengths[0], i = 0; 1591a9934668SKenneth D. Merry size_to_go > 0 && i < num_segs_needed; 1592a9934668SKenneth D. Merry i++, size_to_go -= alloc_size) { 1593a9934668SKenneth D. Merry uint8_t *kern_ptr; 1594a9934668SKenneth D. Merry 1595a9934668SKenneth D. Merry alloc_size = min(size_to_go, softc->io_zone_size); 1596a9934668SKenneth D. Merry kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK); 1597a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 1598a9934668SKenneth D. Merry (bus_addr_t)(uintptr_t)kern_ptr; 1599a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_len = alloc_size; 1600a9934668SKenneth D. Merry } 1601a9934668SKenneth D. Merry if (size_to_go > 0) { 1602a9934668SKenneth D. Merry printf("%s: size_to_go = %zu, software error!\n", 1603a9934668SKenneth D. Merry __func__, size_to_go); 1604a9934668SKenneth D. Merry error = EINVAL; 1605a9934668SKenneth D. Merry goto bailout; 1606a9934668SKenneth D. Merry } 1607a9934668SKenneth D. Merry 1608a9934668SKenneth D. Merry *data_ptrs[0] = (uint8_t *)io_req->kern_segptr; 1609a9934668SKenneth D. Merry *seg_cnt_ptr = io_req->num_kern_segs; 1610a9934668SKenneth D. Merry 1611a9934668SKenneth D. Merry /* 1612a9934668SKenneth D. Merry * We only need to copy data here if the user is writing. 1613a9934668SKenneth D. Merry */ 1614a9934668SKenneth D. Merry if (dirs[0] == CAM_DIR_OUT) 1615a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, dirs[0]); 1616a9934668SKenneth D. Merry break; 1617a9934668SKenneth D. Merry } 1618a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: { 1619a9934668SKenneth D. Merry size_t sg_length; 1620a9934668SKenneth D. Merry 1621a9934668SKenneth D. Merry /* 1622a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1623a9934668SKenneth D. Merry */ 1624a9934668SKenneth D. Merry if (numbufs != 1) { 1625a9934668SKenneth D. Merry printf("%s: cannot currently handle more than one " 1626a9934668SKenneth D. Merry "S/G list per CCB\n", __func__); 1627a9934668SKenneth D. Merry error = EINVAL; 1628a9934668SKenneth D. Merry goto bailout; 1629a9934668SKenneth D. Merry } 1630a9934668SKenneth D. Merry 1631a9934668SKenneth D. Merry /* 1632a9934668SKenneth D. Merry * We have to have at least one segment. 1633a9934668SKenneth D. Merry */ 1634a9934668SKenneth D. Merry if (num_segs == 0) { 1635a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag " 1636a9934668SKenneth D. Merry "set, but sglist_cnt=0!\n", __func__); 1637a9934668SKenneth D. Merry error = EINVAL; 1638a9934668SKenneth D. Merry goto bailout; 1639a9934668SKenneth D. Merry } 1640a9934668SKenneth D. Merry 1641a9934668SKenneth D. Merry /* 1642a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1643a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1644a9934668SKenneth D. Merry */ 1645a9934668SKenneth D. Merry if (lengths[0] == 0) { 1646a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1647a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1648a9934668SKenneth D. Merry error = EINVAL; 1649a9934668SKenneth D. Merry goto bailout; 1650a9934668SKenneth D. Merry } 1651a9934668SKenneth D. Merry 1652a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1653a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1654a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1655a9934668SKenneth D. Merry io_req->num_kern_segs = io_req->num_user_segs; 1656a9934668SKenneth D. Merry 1657a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1658a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1659a9934668SKenneth D. Merry 1660a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1661a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1662a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1663a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1664a9934668SKenneth D. Merry } else 1665a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1666a9934668SKenneth D. Merry 1667a9934668SKenneth D. Merry io_req->kern_segptr = io_req->user_segptr; 1668a9934668SKenneth D. Merry 1669a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1670a9934668SKenneth D. Merry if (error != 0) { 1671a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1672a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1673a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1674a9934668SKenneth D. Merry error); 1675a9934668SKenneth D. Merry goto bailout; 1676a9934668SKenneth D. Merry } 1677a9934668SKenneth D. Merry break; 1678a9934668SKenneth D. Merry } 1679a9934668SKenneth D. Merry default: 1680a9934668SKenneth D. Merry case CAM_DATA_BIO: 1681a9934668SKenneth D. Merry /* 1682a9934668SKenneth D. Merry * A user shouldn't be attaching a bio to the CCB. It 1683a9934668SKenneth D. Merry * isn't a user-accessible structure. 1684a9934668SKenneth D. Merry */ 1685a9934668SKenneth D. Merry error = EINVAL; 1686a9934668SKenneth D. Merry break; 1687a9934668SKenneth D. Merry } 1688a9934668SKenneth D. Merry 1689a9934668SKenneth D. Merry bailout: 1690a9934668SKenneth D. Merry if (error != 0) 1691a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1692a9934668SKenneth D. Merry 1693a9934668SKenneth D. Merry return (error); 1694a9934668SKenneth D. Merry } 1695a9934668SKenneth D. Merry 1696a9934668SKenneth D. Merry static int 1697a9934668SKenneth D. Merry passmemdone(struct cam_periph *periph, struct pass_io_req *io_req) 1698a9934668SKenneth D. Merry { 1699a9934668SKenneth D. Merry struct pass_softc *softc; 1700a9934668SKenneth D. Merry int error; 1701a9934668SKenneth D. Merry int i; 1702a9934668SKenneth D. Merry 1703a9934668SKenneth D. Merry error = 0; 1704a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1705a9934668SKenneth D. Merry 1706a9934668SKenneth D. Merry switch (io_req->data_flags) { 1707a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1708a9934668SKenneth D. Merry /* 1709a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1710a9934668SKenneth D. Merry */ 1711a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1712a9934668SKenneth D. Merry if (io_req->dirs[i] != CAM_DIR_IN) 1713a9934668SKenneth D. Merry continue; 1714a9934668SKenneth D. Merry 1715a9934668SKenneth D. Merry error = copyout(io_req->kern_bufs[i], 1716a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1717a9934668SKenneth D. Merry if (error != 0) { 1718a9934668SKenneth D. Merry xpt_print(periph->path, "Unable to copy %u " 1719a9934668SKenneth D. Merry "bytes from %p to user address %p\n", 1720a9934668SKenneth D. Merry io_req->lengths[i], 1721a9934668SKenneth D. Merry io_req->kern_bufs[i], 1722a9934668SKenneth D. Merry io_req->user_bufs[i]); 1723a9934668SKenneth D. Merry goto bailout; 1724a9934668SKenneth D. Merry } 1725a9934668SKenneth D. Merry 1726a9934668SKenneth D. Merry } 1727a9934668SKenneth D. Merry break; 1728a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1729a9934668SKenneth D. Merry /* Do nothing. The pointer is a physical address already */ 1730a9934668SKenneth D. Merry break; 1731a9934668SKenneth D. Merry case CAM_DATA_SG: 1732a9934668SKenneth D. Merry /* 1733a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1734a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. 1735a9934668SKenneth D. Merry */ 1736a9934668SKenneth D. Merry if (io_req->dirs[0] == CAM_DIR_IN) 1737a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, io_req->dirs[0]); 1738a9934668SKenneth D. Merry break; 1739a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: 1740a9934668SKenneth D. Merry /* 1741a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. No need to 1742a9934668SKenneth D. Merry * copy. 1743a9934668SKenneth D. Merry */ 1744a9934668SKenneth D. Merry break; 1745a9934668SKenneth D. Merry default: 1746a9934668SKenneth D. Merry case CAM_DATA_BIO: 1747a9934668SKenneth D. Merry error = EINVAL; 1748a9934668SKenneth D. Merry break; 1749a9934668SKenneth D. Merry } 1750a9934668SKenneth D. Merry 1751a9934668SKenneth D. Merry bailout: 1752a9934668SKenneth D. Merry /* 1753a9934668SKenneth D. Merry * Reset the user's pointers to their original values and free 1754a9934668SKenneth D. Merry * allocated memory. 1755a9934668SKenneth D. Merry */ 1756a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1757a9934668SKenneth D. Merry 1758a9934668SKenneth D. Merry return (error); 1759a9934668SKenneth D. Merry } 1760a9934668SKenneth D. Merry 176176babe50SJustin T. Gibbs static int 176289c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 176376babe50SJustin T. Gibbs { 176425a2902cSScott Long int error; 176525a2902cSScott Long 176625a2902cSScott Long if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 1767f564de00SScott Long error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl); 176825a2902cSScott Long } 176925a2902cSScott Long return (error); 177025a2902cSScott Long } 177125a2902cSScott Long 177225a2902cSScott Long static int 177325a2902cSScott Long passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 177425a2902cSScott Long { 177576babe50SJustin T. Gibbs struct cam_periph *periph; 1776a9934668SKenneth D. Merry struct pass_softc *softc; 177776babe50SJustin T. Gibbs int error; 17788cff7eb8SAlexander Motin uint32_t priority; 177976babe50SJustin T. Gibbs 1780e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 17812b83592fSScott Long cam_periph_lock(periph); 1782a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 178376babe50SJustin T. Gibbs 178476babe50SJustin T. Gibbs error = 0; 178576babe50SJustin T. Gibbs 178676babe50SJustin T. Gibbs switch (cmd) { 178776babe50SJustin T. Gibbs 178876babe50SJustin T. Gibbs case CAMIOCOMMAND: 178976babe50SJustin T. Gibbs { 179076babe50SJustin T. Gibbs union ccb *inccb; 179176babe50SJustin T. Gibbs union ccb *ccb; 17929deea857SKenneth D. Merry int ccb_malloced; 179376babe50SJustin T. Gibbs 179476babe50SJustin T. Gibbs inccb = (union ccb *)addr; 17958fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 17968fc77fffSConrad Meyer if (inccb->ccb_h.func_code == XPT_SCSI_IO) 17978fc77fffSConrad Meyer inccb->csio.bio = NULL; 17988fc77fffSConrad Meyer #endif 17999deea857SKenneth D. Merry 1800a0bbf9e0SMark Johnston if (inccb->ccb_h.flags & CAM_UNLOCKED) { 1801a0bbf9e0SMark Johnston error = EINVAL; 1802a0bbf9e0SMark Johnston break; 1803a0bbf9e0SMark Johnston } 1804a0bbf9e0SMark Johnston 18059deea857SKenneth D. Merry /* 18069deea857SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 18079deea857SKenneth D. Merry * through the transport layer device. 18089deea857SKenneth D. Merry */ 18099deea857SKenneth D. Merry if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1810f0d9af51SMatt Jacob xpt_print(periph->path, "CCB function code %#x is " 1811f0d9af51SMatt Jacob "restricted to the XPT device\n", 1812f0d9af51SMatt Jacob inccb->ccb_h.func_code); 18139deea857SKenneth D. Merry error = ENODEV; 18149deea857SKenneth D. Merry break; 18159deea857SKenneth D. Merry } 18169deea857SKenneth D. Merry 18178cff7eb8SAlexander Motin /* Compatibility for RL/priority-unaware code. */ 18188cff7eb8SAlexander Motin priority = inccb->ccb_h.pinfo.priority; 1819cccf4220SAlexander Motin if (priority <= CAM_PRIORITY_OOB) 1820cccf4220SAlexander Motin priority += CAM_PRIORITY_OOB + 1; 18218cff7eb8SAlexander Motin 18229deea857SKenneth D. Merry /* 18239deea857SKenneth D. Merry * Non-immediate CCBs need a CCB from the per-device pool 18249deea857SKenneth D. Merry * of CCBs, which is scheduled by the transport layer. 18259deea857SKenneth D. Merry * Immediate CCBs and user-supplied CCBs should just be 18269deea857SKenneth D. Merry * malloced. 18279deea857SKenneth D. Merry */ 18289deea857SKenneth D. Merry if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 18299deea857SKenneth D. Merry && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 18308cff7eb8SAlexander Motin ccb = cam_periph_getccb(periph, priority); 18319deea857SKenneth D. Merry ccb_malloced = 0; 18329deea857SKenneth D. Merry } else { 18338008a935SScott Long ccb = xpt_alloc_ccb_nowait(); 18349deea857SKenneth D. Merry 18359deea857SKenneth D. Merry if (ccb != NULL) 18369deea857SKenneth D. Merry xpt_setup_ccb(&ccb->ccb_h, periph->path, 18378cff7eb8SAlexander Motin priority); 18389deea857SKenneth D. Merry ccb_malloced = 1; 18399deea857SKenneth D. Merry } 18409deea857SKenneth D. Merry 18419deea857SKenneth D. Merry if (ccb == NULL) { 1842f0d9af51SMatt Jacob xpt_print(periph->path, "unable to allocate CCB\n"); 18439deea857SKenneth D. Merry error = ENOMEM; 18449deea857SKenneth D. Merry break; 18459deea857SKenneth D. Merry } 184676babe50SJustin T. Gibbs 184776babe50SJustin T. Gibbs error = passsendccb(periph, ccb, inccb); 184876babe50SJustin T. Gibbs 18499deea857SKenneth D. Merry if (ccb_malloced) 18509deea857SKenneth D. Merry xpt_free_ccb(ccb); 18519deea857SKenneth D. Merry else 185276babe50SJustin T. Gibbs xpt_release_ccb(ccb); 185376babe50SJustin T. Gibbs 185476babe50SJustin T. Gibbs break; 185576babe50SJustin T. Gibbs } 1856a9934668SKenneth D. Merry case CAMIOQUEUE: 1857a9934668SKenneth D. Merry { 1858a9934668SKenneth D. Merry struct pass_io_req *io_req; 1859a9934668SKenneth D. Merry union ccb **user_ccb, *ccb; 1860a9934668SKenneth D. Merry xpt_opcode fc; 1861a9934668SKenneth D. Merry 1862a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) { 1863a9934668SKenneth D. Merry error = passcreatezone(periph); 1864a9934668SKenneth D. Merry if (error != 0) 1865a9934668SKenneth D. Merry goto bailout; 1866a9934668SKenneth D. Merry } 1867a9934668SKenneth D. Merry 1868a9934668SKenneth D. Merry /* 1869a9934668SKenneth D. Merry * We're going to do a blocking allocation for this I/O 1870a9934668SKenneth D. Merry * request, so we have to drop the lock. 1871a9934668SKenneth D. Merry */ 1872a9934668SKenneth D. Merry cam_periph_unlock(periph); 1873a9934668SKenneth D. Merry 1874a9934668SKenneth D. Merry io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO); 1875a9934668SKenneth D. Merry ccb = &io_req->ccb; 1876a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 1877a9934668SKenneth D. Merry 1878a9934668SKenneth D. Merry /* 1879a9934668SKenneth D. Merry * Unlike the CAMIOCOMMAND ioctl above, we only have a 1880a9934668SKenneth D. Merry * pointer to the user's CCB, so we have to copy the whole 1881a9934668SKenneth D. Merry * thing in to a buffer we have allocated (above) instead 1882a9934668SKenneth D. Merry * of allowing the ioctl code to malloc a buffer and copy 1883a9934668SKenneth D. Merry * it in. 1884a9934668SKenneth D. Merry * 1885a9934668SKenneth D. Merry * This is an advantage for this asynchronous interface, 1886a9934668SKenneth D. Merry * since we don't want the memory to get freed while the 1887a9934668SKenneth D. Merry * CCB is outstanding. 1888a9934668SKenneth D. Merry */ 1889a9934668SKenneth D. Merry #if 0 1890a9934668SKenneth D. Merry xpt_print(periph->path, "Copying user CCB %p to " 1891a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, ccb); 1892a9934668SKenneth D. Merry #endif 1893a9934668SKenneth D. Merry error = copyin(*user_ccb, ccb, sizeof(*ccb)); 1894a9934668SKenneth D. Merry if (error != 0) { 1895a9934668SKenneth D. Merry xpt_print(periph->path, "Copy of user CCB %p to " 1896a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 1897a9934668SKenneth D. Merry *user_ccb, ccb, error); 1898a0bbf9e0SMark Johnston goto camioqueue_error; 1899a9934668SKenneth D. Merry } 19008fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 19018fc77fffSConrad Meyer if (ccb->ccb_h.func_code == XPT_SCSI_IO) 19028fc77fffSConrad Meyer ccb->csio.bio = NULL; 19038fc77fffSConrad Meyer #endif 1904a9934668SKenneth D. Merry 1905a0bbf9e0SMark Johnston if (ccb->ccb_h.flags & CAM_UNLOCKED) { 1906a0bbf9e0SMark Johnston error = EINVAL; 1907a0bbf9e0SMark Johnston goto camioqueue_error; 1908a0bbf9e0SMark Johnston } 1909a0bbf9e0SMark Johnston 1910a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 1911a1a604caSAlexander Motin if (ccb->csio.cdb_len > IOCDBLEN) { 1912a1a604caSAlexander Motin error = EINVAL; 1913a0bbf9e0SMark Johnston goto camioqueue_error; 1914a1a604caSAlexander Motin } 1915a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, 1916a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len); 1917a0bbf9e0SMark Johnston if (error != 0) 1918a0bbf9e0SMark Johnston goto camioqueue_error; 1919a1a604caSAlexander Motin ccb->ccb_h.flags &= ~CAM_CDB_POINTER; 1920a1a604caSAlexander Motin } 1921a1a604caSAlexander Motin 1922a9934668SKenneth D. Merry /* 1923a9934668SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 1924a9934668SKenneth D. Merry * through the transport layer device. 1925a9934668SKenneth D. Merry */ 1926a9934668SKenneth D. Merry if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1927a9934668SKenneth D. Merry xpt_print(periph->path, "CCB function code %#x is " 1928a9934668SKenneth D. Merry "restricted to the XPT device\n", 1929a9934668SKenneth D. Merry ccb->ccb_h.func_code); 1930a9934668SKenneth D. Merry error = ENODEV; 1931a0bbf9e0SMark Johnston goto camioqueue_error; 1932a9934668SKenneth D. Merry } 1933a9934668SKenneth D. Merry 1934a9934668SKenneth D. Merry /* 1935a9934668SKenneth D. Merry * Save the user's CCB pointer as well as his linked list 1936a9934668SKenneth D. Merry * pointers and peripheral private area so that we can 1937a9934668SKenneth D. Merry * restore these later. 1938a9934668SKenneth D. Merry */ 1939a9934668SKenneth D. Merry io_req->user_ccb_ptr = *user_ccb; 1940a9934668SKenneth D. Merry io_req->user_periph_links = ccb->ccb_h.periph_links; 1941a9934668SKenneth D. Merry io_req->user_periph_priv = ccb->ccb_h.periph_priv; 1942a9934668SKenneth D. Merry 1943a9934668SKenneth D. Merry /* 1944a9934668SKenneth D. Merry * Now that we've saved the user's values, we can set our 1945a9934668SKenneth D. Merry * own peripheral private entry. 1946a9934668SKenneth D. Merry */ 1947a9934668SKenneth D. Merry ccb->ccb_h.ccb_ioreq = io_req; 1948a9934668SKenneth D. Merry 1949a9934668SKenneth D. Merry /* Compatibility for RL/priority-unaware code. */ 1950a9934668SKenneth D. Merry priority = ccb->ccb_h.pinfo.priority; 1951a9934668SKenneth D. Merry if (priority <= CAM_PRIORITY_OOB) 1952a9934668SKenneth D. Merry priority += CAM_PRIORITY_OOB + 1; 1953a9934668SKenneth D. Merry 1954a9934668SKenneth D. Merry /* 1955a9934668SKenneth D. Merry * Setup fields in the CCB like the path and the priority. 1956a9934668SKenneth D. Merry * The path in particular cannot be done in userland, since 1957a9934668SKenneth D. Merry * it is a pointer to a kernel data structure. 1958a9934668SKenneth D. Merry */ 1959a9934668SKenneth D. Merry xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority, 1960a9934668SKenneth D. Merry ccb->ccb_h.flags); 1961a9934668SKenneth D. Merry 1962a9934668SKenneth D. Merry /* 1963a9934668SKenneth D. Merry * Setup our done routine. There is no way for the user to 1964a9934668SKenneth D. Merry * have a valid pointer here. 1965a9934668SKenneth D. Merry */ 1966a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 1967a9934668SKenneth D. Merry 1968a9934668SKenneth D. Merry fc = ccb->ccb_h.func_code; 1969a9934668SKenneth D. Merry /* 1970a9934668SKenneth D. Merry * If this function code has memory that can be mapped in 1971a9934668SKenneth D. Merry * or out, we need to call passmemsetup(). 1972a9934668SKenneth D. Merry */ 1973a9934668SKenneth D. Merry if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) 1974a9934668SKenneth D. Merry || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH) 1975df424515SWarner Losh || (fc == XPT_DEV_ADVINFO) 1976df424515SWarner Losh || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 1977a9934668SKenneth D. Merry error = passmemsetup(periph, io_req); 1978a0bbf9e0SMark Johnston if (error != 0) 1979a0bbf9e0SMark Johnston goto camioqueue_error; 1980a9934668SKenneth D. Merry } else 1981a9934668SKenneth D. Merry io_req->mapinfo.num_bufs_used = 0; 1982a9934668SKenneth D. Merry 1983a9934668SKenneth D. Merry cam_periph_lock(periph); 1984a9934668SKenneth D. Merry 1985a9934668SKenneth D. Merry /* 1986a9934668SKenneth D. Merry * Everything goes on the incoming queue initially. 1987a9934668SKenneth D. Merry */ 1988a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links); 1989a9934668SKenneth D. Merry 1990a9934668SKenneth D. Merry /* 1991a9934668SKenneth D. Merry * If the CCB is queued, and is not a user CCB, then 1992a9934668SKenneth D. Merry * we need to allocate a slot for it. Call xpt_schedule() 1993a9934668SKenneth D. Merry * so that our start routine will get called when a CCB is 1994a9934668SKenneth D. Merry * available. 1995a9934668SKenneth D. Merry */ 1996a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) 1997a9934668SKenneth D. Merry && ((fc & XPT_FC_USER_CCB) == 0)) { 1998a9934668SKenneth D. Merry xpt_schedule(periph, priority); 1999a9934668SKenneth D. Merry break; 2000a9934668SKenneth D. Merry } 2001a9934668SKenneth D. Merry 2002a9934668SKenneth D. Merry /* 2003a9934668SKenneth D. Merry * At this point, the CCB in question is either an 2004a9934668SKenneth D. Merry * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB 2005a9934668SKenneth D. Merry * and therefore should be malloced, not allocated via a slot. 2006a9934668SKenneth D. Merry * Remove the CCB from the incoming queue and add it to the 2007a9934668SKenneth D. Merry * active queue. 2008a9934668SKenneth D. Merry */ 2009a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 2010a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 2011a9934668SKenneth D. Merry 2012a9934668SKenneth D. Merry xpt_action(ccb); 2013a9934668SKenneth D. Merry 2014a9934668SKenneth D. Merry /* 2015a9934668SKenneth D. Merry * If this is not a queued CCB (i.e. it is an immediate CCB), 2016a9934668SKenneth D. Merry * then it is already done. We need to put it on the done 2017a9934668SKenneth D. Merry * queue for the user to fetch. 2018a9934668SKenneth D. Merry */ 2019a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) == 0) { 2020a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 2021a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 2022a9934668SKenneth D. Merry } 2023a9934668SKenneth D. Merry break; 2024a0bbf9e0SMark Johnston 2025a0bbf9e0SMark Johnston camioqueue_error: 2026a0bbf9e0SMark Johnston uma_zfree(softc->pass_zone, io_req); 2027a0bbf9e0SMark Johnston cam_periph_lock(periph); 2028a0bbf9e0SMark Johnston break; 2029a9934668SKenneth D. Merry } 2030a9934668SKenneth D. Merry case CAMIOGET: 2031a9934668SKenneth D. Merry { 2032a9934668SKenneth D. Merry union ccb **user_ccb; 2033a9934668SKenneth D. Merry struct pass_io_req *io_req; 2034a9934668SKenneth D. Merry int old_error; 2035a9934668SKenneth D. Merry 2036a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 2037a9934668SKenneth D. Merry old_error = 0; 2038a9934668SKenneth D. Merry 2039a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->done_queue); 2040a9934668SKenneth D. Merry if (io_req == NULL) { 2041a9934668SKenneth D. Merry error = ENOENT; 2042a9934668SKenneth D. Merry break; 2043a9934668SKenneth D. Merry } 2044a9934668SKenneth D. Merry 2045a9934668SKenneth D. Merry /* 2046a9934668SKenneth D. Merry * Remove the I/O from the done queue. 2047a9934668SKenneth D. Merry */ 2048a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 2049a9934668SKenneth D. Merry 2050a9934668SKenneth D. Merry /* 2051a9934668SKenneth D. Merry * We have to drop the lock during the copyout because the 2052a9934668SKenneth D. Merry * copyout can result in VM faults that require sleeping. 2053a9934668SKenneth D. Merry */ 2054a9934668SKenneth D. Merry cam_periph_unlock(periph); 2055a9934668SKenneth D. Merry 2056a9934668SKenneth D. Merry /* 2057a9934668SKenneth D. Merry * Do any needed copies (e.g. for reads) and revert the 2058a9934668SKenneth D. Merry * pointers in the CCB back to the user's pointers. 2059a9934668SKenneth D. Merry */ 2060a9934668SKenneth D. Merry error = passmemdone(periph, io_req); 2061a9934668SKenneth D. Merry 2062a9934668SKenneth D. Merry old_error = error; 2063a9934668SKenneth D. Merry 2064a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_links = io_req->user_periph_links; 2065a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv; 2066a9934668SKenneth D. Merry 2067a9934668SKenneth D. Merry #if 0 2068a9934668SKenneth D. Merry xpt_print(periph->path, "Copying to user CCB %p from " 2069a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, &io_req->ccb); 2070a9934668SKenneth D. Merry #endif 2071a9934668SKenneth D. Merry 2072a9934668SKenneth D. Merry error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb)); 2073a9934668SKenneth D. Merry if (error != 0) { 2074a9934668SKenneth D. Merry xpt_print(periph->path, "Copy to user CCB %p from " 2075a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 2076a9934668SKenneth D. Merry *user_ccb, &io_req->ccb, error); 2077a9934668SKenneth D. Merry } 2078a9934668SKenneth D. Merry 2079a9934668SKenneth D. Merry /* 2080a9934668SKenneth D. Merry * Prefer the first error we got back, and make sure we 2081a9934668SKenneth D. Merry * don't overwrite bad status with good. 2082a9934668SKenneth D. Merry */ 2083a9934668SKenneth D. Merry if (old_error != 0) 2084a9934668SKenneth D. Merry error = old_error; 2085a9934668SKenneth D. Merry 2086a9934668SKenneth D. Merry cam_periph_lock(periph); 2087a9934668SKenneth D. Merry 2088a9934668SKenneth D. Merry /* 2089a9934668SKenneth D. Merry * At this point, if there was an error, we could potentially 2090a9934668SKenneth D. Merry * re-queue the I/O and try again. But why? The error 2091a9934668SKenneth D. Merry * would almost certainly happen again. We might as well 2092a9934668SKenneth D. Merry * not leak memory. 2093a9934668SKenneth D. Merry */ 2094a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 2095a9934668SKenneth D. Merry break; 2096a9934668SKenneth D. Merry } 209776babe50SJustin T. Gibbs default: 209876babe50SJustin T. Gibbs error = cam_periph_ioctl(periph, cmd, addr, passerror); 209976babe50SJustin T. Gibbs break; 210076babe50SJustin T. Gibbs } 210176babe50SJustin T. Gibbs 2102a9934668SKenneth D. Merry bailout: 21032b83592fSScott Long cam_periph_unlock(periph); 2104a9934668SKenneth D. Merry 210576babe50SJustin T. Gibbs return(error); 210676babe50SJustin T. Gibbs } 210776babe50SJustin T. Gibbs 2108a9934668SKenneth D. Merry static int 2109a9934668SKenneth D. Merry passpoll(struct cdev *dev, int poll_events, struct thread *td) 2110a9934668SKenneth D. Merry { 2111a9934668SKenneth D. Merry struct cam_periph *periph; 2112a9934668SKenneth D. Merry struct pass_softc *softc; 2113a9934668SKenneth D. Merry int revents; 2114a9934668SKenneth D. Merry 2115a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2116a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2117a9934668SKenneth D. Merry 2118a9934668SKenneth D. Merry revents = poll_events & (POLLOUT | POLLWRNORM); 2119a9934668SKenneth D. Merry if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 2120a9934668SKenneth D. Merry cam_periph_lock(periph); 2121a9934668SKenneth D. Merry 2122a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->done_queue)) { 2123a9934668SKenneth D. Merry revents |= poll_events & (POLLIN | POLLRDNORM); 2124a9934668SKenneth D. Merry } 2125a9934668SKenneth D. Merry cam_periph_unlock(periph); 2126a9934668SKenneth D. Merry if (revents == 0) 2127a9934668SKenneth D. Merry selrecord(td, &softc->read_select); 2128a9934668SKenneth D. Merry } 2129a9934668SKenneth D. Merry 2130a9934668SKenneth D. Merry return (revents); 2131a9934668SKenneth D. Merry } 2132a9934668SKenneth D. Merry 2133a9934668SKenneth D. Merry static int 2134a9934668SKenneth D. Merry passkqfilter(struct cdev *dev, struct knote *kn) 2135a9934668SKenneth D. Merry { 2136a9934668SKenneth D. Merry struct cam_periph *periph; 2137a9934668SKenneth D. Merry struct pass_softc *softc; 2138a9934668SKenneth D. Merry 2139a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2140a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2141a9934668SKenneth D. Merry 2142a9934668SKenneth D. Merry kn->kn_hook = (caddr_t)periph; 2143a9934668SKenneth D. Merry kn->kn_fop = &passread_filtops; 2144a9934668SKenneth D. Merry knlist_add(&softc->read_select.si_note, kn, 0); 2145a9934668SKenneth D. Merry 2146a9934668SKenneth D. Merry return (0); 2147a9934668SKenneth D. Merry } 2148a9934668SKenneth D. Merry 2149a9934668SKenneth D. Merry static void 2150a9934668SKenneth D. Merry passreadfiltdetach(struct knote *kn) 2151a9934668SKenneth D. Merry { 2152a9934668SKenneth D. Merry struct cam_periph *periph; 2153a9934668SKenneth D. Merry struct pass_softc *softc; 2154a9934668SKenneth D. Merry 2155a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2156a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2157a9934668SKenneth D. Merry 2158a9934668SKenneth D. Merry knlist_remove(&softc->read_select.si_note, kn, 0); 2159a9934668SKenneth D. Merry } 2160a9934668SKenneth D. Merry 2161a9934668SKenneth D. Merry static int 2162a9934668SKenneth D. Merry passreadfilt(struct knote *kn, long hint) 2163a9934668SKenneth D. Merry { 2164a9934668SKenneth D. Merry struct cam_periph *periph; 2165a9934668SKenneth D. Merry struct pass_softc *softc; 2166a9934668SKenneth D. Merry int retval; 2167a9934668SKenneth D. Merry 2168a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2169a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2170a9934668SKenneth D. Merry 2171a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 2172a9934668SKenneth D. Merry 2173a9934668SKenneth D. Merry if (TAILQ_EMPTY(&softc->done_queue)) 2174a9934668SKenneth D. Merry retval = 0; 2175a9934668SKenneth D. Merry else 2176a9934668SKenneth D. Merry retval = 1; 2177a9934668SKenneth D. Merry 2178a9934668SKenneth D. Merry return (retval); 2179a9934668SKenneth D. Merry } 2180a9934668SKenneth D. Merry 218176babe50SJustin T. Gibbs /* 218276babe50SJustin T. Gibbs * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 218376babe50SJustin T. Gibbs * should be the CCB that is copied in from the user. 218476babe50SJustin T. Gibbs */ 218576babe50SJustin T. Gibbs static int 218676babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 218776babe50SJustin T. Gibbs { 218876babe50SJustin T. Gibbs struct pass_softc *softc; 218976babe50SJustin T. Gibbs struct cam_periph_map_info mapinfo; 2190a1a604caSAlexander Motin uint8_t *cmd; 219195fbded6SScott Long xpt_opcode fc; 219295fbded6SScott Long int error; 219376babe50SJustin T. Gibbs 219476babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 219576babe50SJustin T. Gibbs 219676babe50SJustin T. Gibbs /* 219776babe50SJustin T. Gibbs * There are some fields in the CCB header that need to be 219876babe50SJustin T. Gibbs * preserved, the rest we get from the user. 219976babe50SJustin T. Gibbs */ 220076babe50SJustin T. Gibbs xpt_merge_ccb(ccb, inccb); 220176babe50SJustin T. Gibbs 2202a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 2203a1a604caSAlexander Motin cmd = __builtin_alloca(ccb->csio.cdb_len); 2204a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len); 2205a1a604caSAlexander Motin if (error) 2206a1a604caSAlexander Motin return (error); 2207a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_ptr = cmd; 2208a1a604caSAlexander Motin } 2209a1a604caSAlexander Motin 221076babe50SJustin T. Gibbs /* 2211a9934668SKenneth D. Merry */ 2212a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 2213a9934668SKenneth D. Merry 2214a9934668SKenneth D. Merry /* 221595fbded6SScott Long * Let cam_periph_mapmem do a sanity check on the data pointer format. 221695fbded6SScott Long * Even if no data transfer is needed, it's a cheap check and it 221795fbded6SScott Long * simplifies the code. 221876babe50SJustin T. Gibbs */ 221995fbded6SScott Long fc = ccb->ccb_h.func_code; 222095fbded6SScott Long if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO) 2221a3cf03a5SWarner Losh || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO) 2222a3cf03a5SWarner Losh || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 2223a3cf03a5SWarner Losh 222476babe50SJustin T. Gibbs bzero(&mapinfo, sizeof(mapinfo)); 222576babe50SJustin T. Gibbs 22262b83592fSScott Long /* 22272b83592fSScott Long * cam_periph_mapmem calls into proc and vm functions that can 22282b83592fSScott Long * sleep as well as trigger I/O, so we can't hold the lock. 22292b83592fSScott Long * Dropping it here is reasonably safe. 22302b83592fSScott Long */ 22312b83592fSScott Long cam_periph_unlock(periph); 2232de239312SAlexander Motin error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio); 22332b83592fSScott Long cam_periph_lock(periph); 223476babe50SJustin T. Gibbs 223576babe50SJustin T. Gibbs /* 223676babe50SJustin T. Gibbs * cam_periph_mapmem returned an error, we can't continue. 223776babe50SJustin T. Gibbs * Return the error to the user. 223876babe50SJustin T. Gibbs */ 223976babe50SJustin T. Gibbs if (error) 224076babe50SJustin T. Gibbs return(error); 224195fbded6SScott Long } else 224295fbded6SScott Long /* Ensure that the unmap call later on is a no-op. */ 224395fbded6SScott Long mapinfo.num_bufs_used = 0; 224476babe50SJustin T. Gibbs 224576babe50SJustin T. Gibbs /* 224676babe50SJustin T. Gibbs * If the user wants us to perform any error recovery, then honor 224776babe50SJustin T. Gibbs * that request. Otherwise, it's up to the user to perform any 224876babe50SJustin T. Gibbs * error recovery. 224976babe50SJustin T. Gibbs */ 22506953d22bSKenneth D. Merry cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 22516953d22bSKenneth D. Merry passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO, 22526953d22bSKenneth D. Merry /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 2253a9d2245eSPoul-Henning Kamp softc->device_stats); 225476babe50SJustin T. Gibbs 225576babe50SJustin T. Gibbs cam_periph_unmapmem(ccb, &mapinfo); 225676babe50SJustin T. Gibbs 225776babe50SJustin T. Gibbs ccb->ccb_h.cbfcnp = NULL; 225876babe50SJustin T. Gibbs ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 225976babe50SJustin T. Gibbs bcopy(ccb, inccb, sizeof(union ccb)); 226076babe50SJustin T. Gibbs 226183c5d981SAlexander Motin return(0); 226276babe50SJustin T. Gibbs } 226376babe50SJustin T. Gibbs 226476babe50SJustin T. Gibbs static int 226576babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 226676babe50SJustin T. Gibbs { 226776babe50SJustin T. Gibbs struct cam_periph *periph; 226876babe50SJustin T. Gibbs struct pass_softc *softc; 226976babe50SJustin T. Gibbs 227076babe50SJustin T. Gibbs periph = xpt_path_periph(ccb->ccb_h.path); 227176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 227276babe50SJustin T. Gibbs 2273553484aeSWarner Losh return(cam_periph_error(ccb, cam_flags, sense_flags)); 227476babe50SJustin T. Gibbs } 2275