1898b0535SWarner Losh /*- 2*bec9534dSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*bec9534dSPedro 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; 521a9934668SKenneth D. Merry cam_status status; 522416494d7SJustin T. Gibbs 523416494d7SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 524a9934668SKenneth D. Merry /* 525a9934668SKenneth D. Merry * Acquire a reference to the periph before we 526a9934668SKenneth D. Merry * start the taskqueue, so that we don't run into 527a9934668SKenneth D. Merry * a situation where the periph goes away before 528a9934668SKenneth D. Merry * the task queue has a chance to run. 529a9934668SKenneth D. Merry */ 530a9934668SKenneth D. Merry status = cam_periph_acquire(periph); 531a9934668SKenneth D. Merry if (status != CAM_REQ_CMP) 532a9934668SKenneth D. Merry break; 533a9934668SKenneth D. Merry 534416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, 535416494d7SJustin T. Gibbs &softc->add_physpath_task); 536416494d7SJustin T. Gibbs } 537416494d7SJustin T. Gibbs break; 538416494d7SJustin T. Gibbs } 53976babe50SJustin T. Gibbs default: 540516871c6SJustin T. Gibbs cam_periph_async(periph, code, path, arg); 54176babe50SJustin T. Gibbs break; 54276babe50SJustin T. Gibbs } 54376babe50SJustin T. Gibbs } 54476babe50SJustin T. Gibbs 54576babe50SJustin T. Gibbs static cam_status 54676babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg) 54776babe50SJustin T. Gibbs { 54876babe50SJustin T. Gibbs struct pass_softc *softc; 54976babe50SJustin T. Gibbs struct ccb_getdev *cgd; 550b8b6b5d3SAlexander Motin struct ccb_pathinq cpi; 551ee198893SKonstantin Belousov struct make_dev_args args; 552ee198893SKonstantin Belousov int error, no_tags; 55376babe50SJustin T. Gibbs 55476babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 55576babe50SJustin T. Gibbs if (cgd == NULL) { 556ea37f519SKenneth D. Merry printf("%s: no getdev CCB, can't register device\n", __func__); 55776babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 55876babe50SJustin T. Gibbs } 55976babe50SJustin T. Gibbs 56076babe50SJustin T. Gibbs softc = (struct pass_softc *)malloc(sizeof(*softc), 56176babe50SJustin T. Gibbs M_DEVBUF, M_NOWAIT); 56276babe50SJustin T. Gibbs 56376babe50SJustin T. Gibbs if (softc == NULL) { 564ea37f519SKenneth D. Merry printf("%s: Unable to probe new device. " 565ea37f519SKenneth D. Merry "Unable to allocate softc\n", __func__); 56676babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 56776babe50SJustin T. Gibbs } 56876babe50SJustin T. Gibbs 56976babe50SJustin T. Gibbs bzero(softc, sizeof(*softc)); 57076babe50SJustin T. Gibbs softc->state = PASS_STATE_NORMAL; 571b8b6b5d3SAlexander Motin if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI) 57210b6172aSMatt Jacob softc->pd_type = SID_TYPE(&cgd->inq_data); 573b8b6b5d3SAlexander Motin else if (cgd->protocol == PROTO_SATAPM) 574b8b6b5d3SAlexander Motin softc->pd_type = T_ENCLOSURE; 575b8b6b5d3SAlexander Motin else 576b8b6b5d3SAlexander Motin softc->pd_type = T_DIRECT; 57776babe50SJustin T. Gibbs 57876babe50SJustin T. Gibbs periph->softc = softc; 579a9934668SKenneth D. Merry softc->periph = periph; 580a9934668SKenneth D. Merry TAILQ_INIT(&softc->incoming_queue); 581a9934668SKenneth D. Merry TAILQ_INIT(&softc->active_queue); 582a9934668SKenneth D. Merry TAILQ_INIT(&softc->abandoned_queue); 583a9934668SKenneth D. Merry TAILQ_INIT(&softc->done_queue); 584a9934668SKenneth D. Merry snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d", 585a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 586a9934668SKenneth D. Merry snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO", 587a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 588a9934668SKenneth D. Merry softc->io_zone_size = MAXPHYS; 589a9934668SKenneth D. Merry knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph)); 5903393f8daSKenneth D. Merry 591b8b6b5d3SAlexander Motin bzero(&cpi, sizeof(cpi)); 592b8b6b5d3SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 593b8b6b5d3SAlexander Motin cpi.ccb_h.func_code = XPT_PATH_INQ; 594b8b6b5d3SAlexander Motin xpt_action((union ccb *)&cpi); 595b8b6b5d3SAlexander Motin 596de239312SAlexander Motin if (cpi.maxio == 0) 597de239312SAlexander Motin softc->maxio = DFLTPHYS; /* traditional default */ 598de239312SAlexander Motin else if (cpi.maxio > MAXPHYS) 599de239312SAlexander Motin softc->maxio = MAXPHYS; /* for safety */ 600de239312SAlexander Motin else 601de239312SAlexander Motin softc->maxio = cpi.maxio; /* real value */ 602de239312SAlexander Motin 603a9934668SKenneth D. Merry if (cpi.hba_misc & PIM_UNMAPPED) 604a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE; 605a9934668SKenneth D. Merry 60676babe50SJustin T. Gibbs /* 60776babe50SJustin T. Gibbs * We pass in 0 for a blocksize, since we don't 60876babe50SJustin T. Gibbs * know what the blocksize of this device is, if 60976babe50SJustin T. Gibbs * it even has a blocksize. 61076babe50SJustin T. Gibbs */ 611edec59d9SAlexander Motin cam_periph_unlock(periph); 6123393f8daSKenneth D. Merry no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 613c81d2c74SMatt Jacob softc->device_stats = devstat_new_entry("pass", 614d3ce8327SEd Schouten periph->unit_number, 0, 6153393f8daSKenneth D. Merry DEVSTAT_NO_BLOCKSIZE 6163393f8daSKenneth D. Merry | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 61710b6172aSMatt Jacob softc->pd_type | 618b8b6b5d3SAlexander Motin XPORT_DEVSTAT_TYPE(cpi.transport) | 6192a888f93SKenneth D. Merry DEVSTAT_TYPE_PASS, 6202a888f93SKenneth D. Merry DEVSTAT_PRIORITY_PASS); 62173d26919SKenneth D. Merry 622ea37f519SKenneth D. Merry /* 623a9934668SKenneth D. Merry * Initialize the taskqueue handler for shutting down kqueue. 624a9934668SKenneth D. Merry */ 625a9934668SKenneth D. Merry TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0, 626a9934668SKenneth D. Merry pass_shutdown_kqueue, periph); 627a9934668SKenneth D. Merry 628a9934668SKenneth D. Merry /* 629a9934668SKenneth D. Merry * Acquire a reference to the periph that we can release once we've 630a9934668SKenneth D. Merry * cleaned up the kqueue. 631a9934668SKenneth D. Merry */ 632a9934668SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 633a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 634a9934668SKenneth D. Merry "registration!\n", __func__); 635a9934668SKenneth D. Merry cam_periph_lock(periph); 636a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 637a9934668SKenneth D. Merry } 638a9934668SKenneth D. Merry 639a9934668SKenneth D. Merry /* 640ea37f519SKenneth D. Merry * Acquire a reference to the periph before we create the devfs 641ea37f519SKenneth D. Merry * instance for it. We'll release this reference once the devfs 642ea37f519SKenneth D. Merry * instance has been freed. 643ea37f519SKenneth D. Merry */ 644ea37f519SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 645ea37f519SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 646ea37f519SKenneth D. Merry "registration!\n", __func__); 64786d45c7fSKenneth D. Merry cam_periph_lock(periph); 648ea37f519SKenneth D. Merry return (CAM_REQ_CMP_ERR); 649ea37f519SKenneth D. Merry } 650ea37f519SKenneth D. Merry 65173d26919SKenneth D. Merry /* Register the device */ 652ee198893SKonstantin Belousov make_dev_args_init(&args); 653ee198893SKonstantin Belousov args.mda_devsw = &pass_cdevsw; 654ee198893SKonstantin Belousov args.mda_unit = periph->unit_number; 655ee198893SKonstantin Belousov args.mda_uid = UID_ROOT; 656ee198893SKonstantin Belousov args.mda_gid = GID_OPERATOR; 657ee198893SKonstantin Belousov args.mda_mode = 0600; 658ee198893SKonstantin Belousov args.mda_si_drv1 = periph; 659ee198893SKonstantin Belousov error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 660ee198893SKonstantin Belousov periph->unit_number); 661ee198893SKonstantin Belousov if (error != 0) { 662ee198893SKonstantin Belousov cam_periph_lock(periph); 663ee198893SKonstantin Belousov cam_periph_release_locked(periph); 664ee198893SKonstantin Belousov return (CAM_REQ_CMP_ERR); 665ee198893SKonstantin Belousov } 666ea37f519SKenneth D. Merry 667ea37f519SKenneth D. Merry /* 668a9934668SKenneth D. Merry * Hold a reference to the periph before we create the physical 669a9934668SKenneth D. Merry * path alias so it can't go away. 670ea37f519SKenneth D. Merry */ 671a9934668SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 672a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 673a9934668SKenneth D. Merry "registration!\n", __func__); 674a9934668SKenneth D. Merry cam_periph_lock(periph); 675a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 676a9934668SKenneth D. Merry } 677ea37f519SKenneth D. Merry 678edec59d9SAlexander Motin cam_periph_lock(periph); 67973d26919SKenneth D. Merry 680416494d7SJustin T. Gibbs TASK_INIT(&softc->add_physpath_task, /*priority*/0, 681416494d7SJustin T. Gibbs pass_add_physpath, periph); 682416494d7SJustin T. Gibbs 68376babe50SJustin T. Gibbs /* 684416494d7SJustin T. Gibbs * See if physical path information is already available. 68576babe50SJustin T. Gibbs */ 686416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task); 687416494d7SJustin T. Gibbs 688416494d7SJustin T. Gibbs /* 689416494d7SJustin T. Gibbs * Add an async callback so that we get notified if 690416494d7SJustin T. Gibbs * this device goes away or its physical path 691416494d7SJustin T. Gibbs * (stored in the advanced info data of the EDT) has 692416494d7SJustin T. Gibbs * changed. 693416494d7SJustin T. Gibbs */ 694416494d7SJustin T. Gibbs xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 695416494d7SJustin T. Gibbs passasync, periph, periph->path); 69676babe50SJustin T. Gibbs 69776babe50SJustin T. Gibbs if (bootverbose) 69876babe50SJustin T. Gibbs xpt_announce_periph(periph, NULL); 69976babe50SJustin T. Gibbs 70076babe50SJustin T. Gibbs return(CAM_REQ_CMP); 70176babe50SJustin T. Gibbs } 70276babe50SJustin T. Gibbs 70376babe50SJustin T. Gibbs static int 70489c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td) 70576babe50SJustin T. Gibbs { 70676babe50SJustin T. Gibbs struct cam_periph *periph; 70776babe50SJustin T. Gibbs struct pass_softc *softc; 708e2a5fdf9SNate Lawson int error; 70976babe50SJustin T. Gibbs 710e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 7112b83592fSScott Long if (cam_periph_acquire(periph) != CAM_REQ_CMP) 71276babe50SJustin T. Gibbs return (ENXIO); 71376babe50SJustin T. Gibbs 7142b83592fSScott Long cam_periph_lock(periph); 7152b83592fSScott Long 71676babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 71776babe50SJustin T. Gibbs 718ee9c90c7SKenneth D. Merry if (softc->flags & PASS_FLAG_INVALID) { 719c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7202b83592fSScott Long cam_periph_unlock(periph); 72176babe50SJustin T. Gibbs return(ENXIO); 722ee9c90c7SKenneth D. Merry } 72322b9c86cSKenneth D. Merry 72422b9c86cSKenneth D. Merry /* 725f5ef42beSRobert Watson * Don't allow access when we're running at a high securelevel. 72622b9c86cSKenneth D. Merry */ 727a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 1); 728f7312ca2SRobert Watson if (error) { 729c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7302b83592fSScott Long cam_periph_unlock(periph); 731f7312ca2SRobert Watson return(error); 73222b9c86cSKenneth D. Merry } 73376babe50SJustin T. Gibbs 73476babe50SJustin T. Gibbs /* 73566a0780eSKenneth D. Merry * Only allow read-write access. 73666a0780eSKenneth D. Merry */ 73722b9c86cSKenneth D. Merry if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 738c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7392b83592fSScott Long cam_periph_unlock(periph); 74066a0780eSKenneth D. Merry return(EPERM); 74122b9c86cSKenneth D. Merry } 74266a0780eSKenneth D. Merry 74366a0780eSKenneth D. Merry /* 74476babe50SJustin T. Gibbs * We don't allow nonblocking access. 74576babe50SJustin T. Gibbs */ 74676babe50SJustin T. Gibbs if ((flags & O_NONBLOCK) != 0) { 747f0d9af51SMatt Jacob xpt_print(periph->path, "can't do nonblocking access\n"); 748c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7492b83592fSScott Long cam_periph_unlock(periph); 75022b9c86cSKenneth D. Merry return(EINVAL); 75176babe50SJustin T. Gibbs } 75276babe50SJustin T. Gibbs 75386d45c7fSKenneth D. Merry softc->open_count++; 75486d45c7fSKenneth D. Merry 755835187bfSScott Long cam_periph_unlock(periph); 75676babe50SJustin T. Gibbs 75776babe50SJustin T. Gibbs return (error); 75876babe50SJustin T. Gibbs } 75976babe50SJustin T. Gibbs 76076babe50SJustin T. Gibbs static int 76189c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td) 76276babe50SJustin T. Gibbs { 76376babe50SJustin T. Gibbs struct cam_periph *periph; 76486d45c7fSKenneth D. Merry struct pass_softc *softc; 765227d67aaSAlexander Motin struct mtx *mtx; 76676babe50SJustin T. Gibbs 767e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 768227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 769227d67aaSAlexander Motin mtx_lock(mtx); 77076babe50SJustin T. Gibbs 77186d45c7fSKenneth D. Merry softc = periph->softc; 77286d45c7fSKenneth D. Merry softc->open_count--; 77386d45c7fSKenneth D. Merry 774a9934668SKenneth D. Merry if (softc->open_count == 0) { 775a9934668SKenneth D. Merry struct pass_io_req *io_req, *io_req2; 776a9934668SKenneth D. Merry 777a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 778a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 779a9934668SKenneth D. Merry passiocleanup(softc, io_req); 780a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 781a9934668SKenneth D. Merry } 782a9934668SKenneth D. Merry 783a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, 784a9934668SKenneth D. Merry io_req2) { 785a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 786a9934668SKenneth D. Merry passiocleanup(softc, io_req); 787a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 788a9934668SKenneth D. Merry } 789a9934668SKenneth D. Merry 790a9934668SKenneth D. Merry /* 791a9934668SKenneth D. Merry * If there are any active I/Os, we need to forcibly acquire a 792a9934668SKenneth D. Merry * reference to the peripheral so that we don't go away 793a9934668SKenneth D. Merry * before they complete. We'll release the reference when 794a9934668SKenneth D. Merry * the abandoned queue is empty. 795a9934668SKenneth D. Merry */ 796a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->active_queue); 797a9934668SKenneth D. Merry if ((io_req != NULL) 798a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) { 799a9934668SKenneth D. Merry cam_periph_doacquire(periph); 800a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 801a9934668SKenneth D. Merry } 802a9934668SKenneth D. Merry 803a9934668SKenneth D. Merry /* 804a9934668SKenneth D. Merry * Since the I/O in the active queue is not under our 805a9934668SKenneth D. Merry * control, just set a flag so that we can clean it up when 806a9934668SKenneth D. Merry * it completes and put it on the abandoned queue. This 807a9934668SKenneth D. Merry * will prevent our sending spurious completions in the 808a9934668SKenneth D. Merry * event that the device is opened again before these I/Os 809a9934668SKenneth D. Merry * complete. 810a9934668SKenneth D. Merry */ 811a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, 812a9934668SKenneth D. Merry io_req2) { 813a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 814a9934668SKenneth D. Merry io_req->flags |= PASS_IO_ABANDONED; 815a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, 816a9934668SKenneth D. Merry links); 817a9934668SKenneth D. Merry } 818a9934668SKenneth D. Merry } 819a9934668SKenneth D. Merry 82086d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 82186d45c7fSKenneth D. Merry 82286d45c7fSKenneth D. Merry /* 823227d67aaSAlexander Motin * We reference the lock directly here, instead of using 82486d45c7fSKenneth D. Merry * cam_periph_unlock(). The reason is that the call to 82586d45c7fSKenneth D. Merry * cam_periph_release_locked() above could result in the periph 82686d45c7fSKenneth D. Merry * getting freed. If that is the case, dereferencing the periph 82786d45c7fSKenneth D. Merry * with a cam_periph_unlock() call would cause a page fault. 82886d45c7fSKenneth D. Merry * 82986d45c7fSKenneth D. Merry * cam_periph_release() avoids this problem using the same method, 83086d45c7fSKenneth D. Merry * but we're manually acquiring and dropping the lock here to 83186d45c7fSKenneth D. Merry * protect the open count and avoid another lock acquisition and 83286d45c7fSKenneth D. Merry * release. 83386d45c7fSKenneth D. Merry */ 834227d67aaSAlexander Motin mtx_unlock(mtx); 83576babe50SJustin T. Gibbs 83676babe50SJustin T. Gibbs return (0); 83776babe50SJustin T. Gibbs } 83876babe50SJustin T. Gibbs 839a9934668SKenneth D. Merry 840a9934668SKenneth D. Merry static void 841a9934668SKenneth D. Merry passstart(struct cam_periph *periph, union ccb *start_ccb) 842a9934668SKenneth D. Merry { 843a9934668SKenneth D. Merry struct pass_softc *softc; 844a9934668SKenneth D. Merry 845a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 846a9934668SKenneth D. Merry 847a9934668SKenneth D. Merry switch (softc->state) { 848a9934668SKenneth D. Merry case PASS_STATE_NORMAL: { 849a9934668SKenneth D. Merry struct pass_io_req *io_req; 850a9934668SKenneth D. Merry 851a9934668SKenneth D. Merry /* 852a9934668SKenneth D. Merry * Check for any queued I/O requests that require an 853a9934668SKenneth D. Merry * allocated slot. 854a9934668SKenneth D. Merry */ 855a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->incoming_queue); 856a9934668SKenneth D. Merry if (io_req == NULL) { 857a9934668SKenneth D. Merry xpt_release_ccb(start_ccb); 858a9934668SKenneth D. Merry break; 859a9934668SKenneth D. Merry } 860a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 861a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 862a9934668SKenneth D. Merry /* 863a9934668SKenneth D. Merry * Merge the user's CCB into the allocated CCB. 864a9934668SKenneth D. Merry */ 865a9934668SKenneth D. Merry xpt_merge_ccb(start_ccb, &io_req->ccb); 866a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO; 867a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_ioreq = io_req; 868a9934668SKenneth D. Merry start_ccb->ccb_h.cbfcnp = passdone; 869a9934668SKenneth D. Merry io_req->alloced_ccb = start_ccb; 870a9934668SKenneth D. Merry binuptime(&io_req->start_time); 871a9934668SKenneth D. Merry devstat_start_transaction(softc->device_stats, 872a9934668SKenneth D. Merry &io_req->start_time); 873a9934668SKenneth D. Merry 874a9934668SKenneth D. Merry xpt_action(start_ccb); 875a9934668SKenneth D. Merry 876a9934668SKenneth D. Merry /* 877a9934668SKenneth D. Merry * If we have any more I/O waiting, schedule ourselves again. 878a9934668SKenneth D. Merry */ 879a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->incoming_queue)) 880a9934668SKenneth D. Merry xpt_schedule(periph, CAM_PRIORITY_NORMAL); 881a9934668SKenneth D. Merry break; 882a9934668SKenneth D. Merry } 883a9934668SKenneth D. Merry default: 884a9934668SKenneth D. Merry break; 885a9934668SKenneth D. Merry } 886a9934668SKenneth D. Merry } 887a9934668SKenneth D. Merry 888a9934668SKenneth D. Merry static void 889a9934668SKenneth D. Merry passdone(struct cam_periph *periph, union ccb *done_ccb) 890a9934668SKenneth D. Merry { 891a9934668SKenneth D. Merry struct pass_softc *softc; 892a9934668SKenneth D. Merry struct ccb_scsiio *csio; 893a9934668SKenneth D. Merry 894a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 895a9934668SKenneth D. Merry 896a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 897a9934668SKenneth D. Merry 898a9934668SKenneth D. Merry csio = &done_ccb->csio; 899a9934668SKenneth D. Merry switch (csio->ccb_h.ccb_type) { 900a9934668SKenneth D. Merry case PASS_CCB_QUEUED_IO: { 901a9934668SKenneth D. Merry struct pass_io_req *io_req; 902a9934668SKenneth D. Merry 903a9934668SKenneth D. Merry io_req = done_ccb->ccb_h.ccb_ioreq; 904a9934668SKenneth D. Merry #if 0 905a9934668SKenneth D. Merry xpt_print(periph->path, "%s: called for user CCB %p\n", 906a9934668SKenneth D. Merry __func__, io_req->user_ccb_ptr); 907a9934668SKenneth D. Merry #endif 908a9934668SKenneth D. Merry if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 909a9934668SKenneth D. Merry && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) 910a9934668SKenneth D. Merry && ((io_req->flags & PASS_IO_ABANDONED) == 0)) { 911a9934668SKenneth D. Merry int error; 912a9934668SKenneth D. Merry 913a9934668SKenneth D. Merry error = passerror(done_ccb, CAM_RETRY_SELTO, 914a9934668SKenneth D. Merry SF_RETRY_UA | SF_NO_PRINT); 915a9934668SKenneth D. Merry 916a9934668SKenneth D. Merry if (error == ERESTART) { 917a9934668SKenneth D. Merry /* 918a9934668SKenneth D. Merry * A retry was scheduled, so 919a9934668SKenneth D. Merry * just return. 920a9934668SKenneth D. Merry */ 921a9934668SKenneth D. Merry return; 922a9934668SKenneth D. Merry } 923a9934668SKenneth D. Merry } 924a9934668SKenneth D. Merry 925a9934668SKenneth D. Merry /* 926a9934668SKenneth D. Merry * Copy the allocated CCB contents back to the malloced CCB 927a9934668SKenneth D. Merry * so we can give status back to the user when he requests it. 928a9934668SKenneth D. Merry */ 929a9934668SKenneth D. Merry bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb)); 930a9934668SKenneth D. Merry 931a9934668SKenneth D. Merry /* 932a9934668SKenneth D. Merry * Log data/transaction completion with devstat(9). 933a9934668SKenneth D. Merry */ 934a9934668SKenneth D. Merry switch (done_ccb->ccb_h.func_code) { 935a9934668SKenneth D. Merry case XPT_SCSI_IO: 936a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 937a9934668SKenneth D. Merry done_ccb->csio.dxfer_len - done_ccb->csio.resid, 938a9934668SKenneth D. Merry done_ccb->csio.tag_action & 0x3, 939a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 940a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 941a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 942a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 943a9934668SKenneth D. Merry &io_req->start_time); 944a9934668SKenneth D. Merry break; 945a9934668SKenneth D. Merry case XPT_ATA_IO: 946a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 947a9934668SKenneth D. Merry done_ccb->ataio.dxfer_len - done_ccb->ataio.resid, 948e4cc6558SWarner Losh 0, /* Not used in ATA */ 949a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 950a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 951a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 952a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 953a9934668SKenneth D. Merry &io_req->start_time); 954a9934668SKenneth D. Merry break; 955a9934668SKenneth D. Merry case XPT_SMP_IO: 956a9934668SKenneth D. Merry /* 957a9934668SKenneth D. Merry * XXX KDM this isn't quite right, but there isn't 958a9934668SKenneth D. Merry * currently an easy way to represent a bidirectional 959a9934668SKenneth D. Merry * transfer in devstat. The only way to do it 960a9934668SKenneth D. Merry * and have the byte counts come out right would 961a9934668SKenneth D. Merry * mean that we would have to record two 962a9934668SKenneth D. Merry * transactions, one for the request and one for the 963a9934668SKenneth D. Merry * response. For now, so that we report something, 964a9934668SKenneth D. Merry * just treat the entire thing as a read. 965a9934668SKenneth D. Merry */ 966a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 967a9934668SKenneth D. Merry done_ccb->smpio.smp_request_len + 968a9934668SKenneth D. Merry done_ccb->smpio.smp_response_len, 969a9934668SKenneth D. Merry DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL, 970a9934668SKenneth D. Merry &io_req->start_time); 971a9934668SKenneth D. Merry break; 972a9934668SKenneth D. Merry default: 973a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 0, 974a9934668SKenneth D. Merry DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL, 975a9934668SKenneth D. Merry &io_req->start_time); 976a9934668SKenneth D. Merry break; 977a9934668SKenneth D. Merry } 978a9934668SKenneth D. Merry 979a9934668SKenneth D. Merry /* 980a9934668SKenneth D. Merry * In the normal case, take the completed I/O off of the 981a9934668SKenneth D. Merry * active queue and put it on the done queue. Notitfy the 982a9934668SKenneth D. Merry * user that we have a completed I/O. 983a9934668SKenneth D. Merry */ 984a9934668SKenneth D. Merry if ((io_req->flags & PASS_IO_ABANDONED) == 0) { 985a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 986a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 987a9934668SKenneth D. Merry selwakeuppri(&softc->read_select, PRIBIO); 988a9934668SKenneth D. Merry KNOTE_LOCKED(&softc->read_select.si_note, 0); 989a9934668SKenneth D. Merry } else { 990a9934668SKenneth D. Merry /* 991a9934668SKenneth D. Merry * In the case of an abandoned I/O (final close 992a9934668SKenneth D. Merry * without fetching the I/O), take it off of the 993a9934668SKenneth D. Merry * abandoned queue and free it. 994a9934668SKenneth D. Merry */ 995a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->abandoned_queue, io_req, links); 996a9934668SKenneth D. Merry passiocleanup(softc, io_req); 997a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 998a9934668SKenneth D. Merry 999a9934668SKenneth D. Merry /* 1000a9934668SKenneth D. Merry * Release the done_ccb here, since we may wind up 1001a9934668SKenneth D. Merry * freeing the peripheral when we decrement the 1002a9934668SKenneth D. Merry * reference count below. 1003a9934668SKenneth D. Merry */ 1004a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1005a9934668SKenneth D. Merry 1006a9934668SKenneth D. Merry /* 1007a9934668SKenneth D. Merry * If the abandoned queue is empty, we can release 1008a9934668SKenneth D. Merry * our reference to the periph since we won't have 1009a9934668SKenneth D. Merry * any more completions coming. 1010a9934668SKenneth D. Merry */ 1011a9934668SKenneth D. Merry if ((TAILQ_EMPTY(&softc->abandoned_queue)) 1012a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) { 1013a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET; 1014a9934668SKenneth D. Merry cam_periph_release_locked(periph); 1015a9934668SKenneth D. Merry } 1016a9934668SKenneth D. Merry 1017a9934668SKenneth D. Merry /* 1018a9934668SKenneth D. Merry * We have already released the CCB, so we can 1019a9934668SKenneth D. Merry * return. 1020a9934668SKenneth D. Merry */ 1021a9934668SKenneth D. Merry return; 1022a9934668SKenneth D. Merry } 1023a9934668SKenneth D. Merry break; 1024a9934668SKenneth D. Merry } 1025a9934668SKenneth D. Merry } 1026a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1027a9934668SKenneth D. Merry } 1028a9934668SKenneth D. Merry 1029a9934668SKenneth D. Merry static int 1030a9934668SKenneth D. Merry passcreatezone(struct cam_periph *periph) 1031a9934668SKenneth D. Merry { 1032a9934668SKenneth D. Merry struct pass_softc *softc; 1033a9934668SKenneth D. Merry int error; 1034a9934668SKenneth D. Merry 1035a9934668SKenneth D. Merry error = 0; 1036a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1037a9934668SKenneth D. Merry 1038a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 1039a9934668SKenneth D. Merry KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 1040a9934668SKenneth D. Merry ("%s called when the pass(4) zone is valid!\n", __func__)); 1041a9934668SKenneth D. Merry KASSERT((softc->pass_zone == NULL), 1042a9934668SKenneth D. Merry ("%s called when the pass(4) zone is allocated!\n", __func__)); 1043a9934668SKenneth D. Merry 1044a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) { 1045a9934668SKenneth D. Merry 1046a9934668SKenneth D. Merry /* 1047a9934668SKenneth D. Merry * We're the first context through, so we need to create 1048a9934668SKenneth D. Merry * the pass(4) UMA zone for I/O requests. 1049a9934668SKenneth D. Merry */ 1050a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_INPROG; 1051a9934668SKenneth D. Merry 1052a9934668SKenneth D. Merry /* 1053a9934668SKenneth D. Merry * uma_zcreate() does a blocking (M_WAITOK) allocation, 1054a9934668SKenneth D. Merry * so we cannot hold a mutex while we call it. 1055a9934668SKenneth D. Merry */ 1056a9934668SKenneth D. Merry cam_periph_unlock(periph); 1057a9934668SKenneth D. Merry 1058a9934668SKenneth D. Merry softc->pass_zone = uma_zcreate(softc->zone_name, 1059a9934668SKenneth D. Merry sizeof(struct pass_io_req), NULL, NULL, NULL, NULL, 1060a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1061a9934668SKenneth D. Merry 1062a9934668SKenneth D. Merry softc->pass_io_zone = uma_zcreate(softc->io_zone_name, 1063a9934668SKenneth D. Merry softc->io_zone_size, NULL, NULL, NULL, NULL, 1064a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1065a9934668SKenneth D. Merry 1066a9934668SKenneth D. Merry cam_periph_lock(periph); 1067a9934668SKenneth D. Merry 1068a9934668SKenneth D. Merry if ((softc->pass_zone == NULL) 1069a9934668SKenneth D. Merry || (softc->pass_io_zone == NULL)) { 1070a9934668SKenneth D. Merry if (softc->pass_zone == NULL) 1071a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1072a9934668SKenneth D. Merry "IO Req UMA zone\n"); 1073a9934668SKenneth D. Merry else 1074a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1075a9934668SKenneth D. Merry "IO UMA zone\n"); 1076a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ZONE_INPROG; 1077a9934668SKenneth D. Merry goto bailout; 1078a9934668SKenneth D. Merry } 1079a9934668SKenneth D. Merry 1080a9934668SKenneth D. Merry /* 1081a9934668SKenneth D. Merry * Set the flags appropriately and notify any other waiters. 1082a9934668SKenneth D. Merry */ 1083a9934668SKenneth D. Merry softc->flags &= PASS_FLAG_ZONE_INPROG; 1084a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_VALID; 1085a9934668SKenneth D. Merry wakeup(&softc->pass_zone); 1086a9934668SKenneth D. Merry } else { 1087a9934668SKenneth D. Merry /* 1088a9934668SKenneth D. Merry * In this case, the UMA zone has not yet been created, but 1089a9934668SKenneth D. Merry * another context is in the process of creating it. We 1090a9934668SKenneth D. Merry * need to sleep until the creation is either done or has 1091a9934668SKenneth D. Merry * failed. 1092a9934668SKenneth D. Merry */ 1093a9934668SKenneth D. Merry while ((softc->flags & PASS_FLAG_ZONE_INPROG) 1094a9934668SKenneth D. Merry && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) { 1095a9934668SKenneth D. Merry error = msleep(&softc->pass_zone, 1096a9934668SKenneth D. Merry cam_periph_mtx(periph), PRIBIO, 1097a9934668SKenneth D. Merry "paszon", 0); 1098a9934668SKenneth D. Merry if (error != 0) 1099a9934668SKenneth D. Merry goto bailout; 1100a9934668SKenneth D. Merry } 1101a9934668SKenneth D. Merry /* 1102a9934668SKenneth D. Merry * If the zone creation failed, no luck for the user. 1103a9934668SKenneth D. Merry */ 1104a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){ 1105a9934668SKenneth D. Merry error = ENOMEM; 1106a9934668SKenneth D. Merry goto bailout; 1107a9934668SKenneth D. Merry } 1108a9934668SKenneth D. Merry } 1109a9934668SKenneth D. Merry bailout: 1110a9934668SKenneth D. Merry return (error); 1111a9934668SKenneth D. Merry } 1112a9934668SKenneth D. Merry 1113a9934668SKenneth D. Merry static void 1114a9934668SKenneth D. Merry passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req) 1115a9934668SKenneth D. Merry { 1116a9934668SKenneth D. Merry union ccb *ccb; 1117a9934668SKenneth D. Merry u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1118a9934668SKenneth D. Merry int i, numbufs; 1119a9934668SKenneth D. Merry 1120a9934668SKenneth D. Merry ccb = &io_req->ccb; 1121a9934668SKenneth D. Merry 1122a9934668SKenneth D. Merry switch (ccb->ccb_h.func_code) { 1123a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1124a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1125a9934668SKenneth D. Merry 1126a9934668SKenneth D. Merry if (numbufs == 1) { 1127a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1128a9934668SKenneth D. Merry } else { 1129a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1130a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1131a9934668SKenneth D. Merry } 1132a9934668SKenneth D. Merry break; 1133a9934668SKenneth D. Merry case XPT_SCSI_IO: 1134a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1135a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1136a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1137a9934668SKenneth D. Merry break; 1138a9934668SKenneth D. Merry case XPT_ATA_IO: 1139a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1140a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1141a9934668SKenneth D. Merry break; 1142a9934668SKenneth D. Merry case XPT_SMP_IO: 1143a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1144a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1145a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1146a9934668SKenneth D. Merry break; 1147a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1148a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1149a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1150a9934668SKenneth D. Merry break; 1151df424515SWarner Losh case XPT_NVME_IO: 1152df424515SWarner Losh case XPT_NVME_ADMIN: 1153df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1154df424515SWarner Losh numbufs = min(io_req->num_bufs, 1); 1155df424515SWarner Losh break; 1156a9934668SKenneth D. Merry default: 1157a9934668SKenneth D. Merry /* allow ourselves to be swapped once again */ 1158a9934668SKenneth D. Merry return; 1159a9934668SKenneth D. Merry break; /* NOTREACHED */ 1160a9934668SKenneth D. Merry } 1161a9934668SKenneth D. Merry 1162a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_USER_SEG_MALLOC) { 1163a9934668SKenneth D. Merry free(io_req->user_segptr, M_SCSIPASS); 1164a9934668SKenneth D. Merry io_req->user_segptr = NULL; 1165a9934668SKenneth D. Merry } 1166a9934668SKenneth D. Merry 1167a9934668SKenneth D. Merry /* 1168a9934668SKenneth D. Merry * We only want to free memory we malloced. 1169a9934668SKenneth D. Merry */ 1170a9934668SKenneth D. Merry if (io_req->data_flags == CAM_DATA_VADDR) { 1171a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1172a9934668SKenneth D. Merry if (io_req->kern_bufs[i] == NULL) 1173a9934668SKenneth D. Merry continue; 1174a9934668SKenneth D. Merry 1175a9934668SKenneth D. Merry free(io_req->kern_bufs[i], M_SCSIPASS); 1176a9934668SKenneth D. Merry io_req->kern_bufs[i] = NULL; 1177a9934668SKenneth D. Merry } 1178a9934668SKenneth D. Merry } else if (io_req->data_flags == CAM_DATA_SG) { 1179a9934668SKenneth D. Merry for (i = 0; i < io_req->num_kern_segs; i++) { 1180a9934668SKenneth D. Merry if ((uint8_t *)(uintptr_t) 1181a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr == NULL) 1182a9934668SKenneth D. Merry continue; 1183a9934668SKenneth D. Merry 1184a9934668SKenneth D. Merry uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t) 1185a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr); 1186a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 0; 1187a9934668SKenneth D. Merry } 1188a9934668SKenneth D. Merry } 1189a9934668SKenneth D. Merry 1190a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) { 1191a9934668SKenneth D. Merry free(io_req->kern_segptr, M_SCSIPASS); 1192a9934668SKenneth D. Merry io_req->kern_segptr = NULL; 1193a9934668SKenneth D. Merry } 1194a9934668SKenneth D. Merry 1195a9934668SKenneth D. Merry if (io_req->data_flags != CAM_DATA_PADDR) { 1196a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1197a9934668SKenneth D. Merry /* 1198a9934668SKenneth D. Merry * Restore the user's buffer pointers to their 1199a9934668SKenneth D. Merry * previous values. 1200a9934668SKenneth D. Merry */ 1201a9934668SKenneth D. Merry if (io_req->user_bufs[i] != NULL) 1202a9934668SKenneth D. Merry *data_ptrs[i] = io_req->user_bufs[i]; 1203a9934668SKenneth D. Merry } 1204a9934668SKenneth D. Merry } 1205a9934668SKenneth D. Merry 1206a9934668SKenneth D. Merry } 1207a9934668SKenneth D. Merry 1208a9934668SKenneth D. Merry static int 1209a9934668SKenneth D. Merry passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req, 1210a9934668SKenneth D. Merry ccb_flags direction) 1211a9934668SKenneth D. Merry { 1212a9934668SKenneth D. Merry bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy; 1213a9934668SKenneth D. Merry bus_dma_segment_t *user_sglist, *kern_sglist; 1214a9934668SKenneth D. Merry int i, j, error; 1215a9934668SKenneth D. Merry 1216a9934668SKenneth D. Merry error = 0; 1217a9934668SKenneth D. Merry kern_watermark = 0; 1218a9934668SKenneth D. Merry user_watermark = 0; 1219a9934668SKenneth D. Merry len_to_copy = 0; 1220a9934668SKenneth D. Merry len_copied = 0; 1221a9934668SKenneth D. Merry user_sglist = io_req->user_segptr; 1222a9934668SKenneth D. Merry kern_sglist = io_req->kern_segptr; 1223a9934668SKenneth D. Merry 1224a9934668SKenneth D. Merry for (i = 0, j = 0; i < io_req->num_user_segs && 1225a9934668SKenneth D. Merry j < io_req->num_kern_segs;) { 1226a9934668SKenneth D. Merry uint8_t *user_ptr, *kern_ptr; 1227a9934668SKenneth D. Merry 1228a9934668SKenneth D. Merry len_to_copy = min(user_sglist[i].ds_len -user_watermark, 1229a9934668SKenneth D. Merry kern_sglist[j].ds_len - kern_watermark); 1230a9934668SKenneth D. Merry 1231a9934668SKenneth D. Merry user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr; 1232a9934668SKenneth D. Merry user_ptr = user_ptr + user_watermark; 1233a9934668SKenneth D. Merry kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr; 1234a9934668SKenneth D. Merry kern_ptr = kern_ptr + kern_watermark; 1235a9934668SKenneth D. Merry 1236a9934668SKenneth D. Merry user_watermark += len_to_copy; 1237a9934668SKenneth D. Merry kern_watermark += len_to_copy; 1238a9934668SKenneth D. Merry 1239a9934668SKenneth D. Merry if (!useracc(user_ptr, len_to_copy, 1240a9934668SKenneth D. Merry (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) { 1241a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1242a9934668SKenneth D. Merry "S/G list element %p len %zu\n", __func__, 1243a9934668SKenneth D. Merry user_ptr, len_to_copy); 1244a9934668SKenneth D. Merry error = EFAULT; 1245a9934668SKenneth D. Merry goto bailout; 1246a9934668SKenneth D. Merry } 1247a9934668SKenneth D. Merry 1248a9934668SKenneth D. Merry if (direction == CAM_DIR_IN) { 1249a9934668SKenneth D. Merry error = copyout(kern_ptr, user_ptr, len_to_copy); 1250a9934668SKenneth D. Merry if (error != 0) { 1251a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyout of %u " 1252a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1253a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1254a9934668SKenneth D. Merry kern_ptr, user_ptr, error); 1255a9934668SKenneth D. Merry goto bailout; 1256a9934668SKenneth D. Merry } 1257a9934668SKenneth D. Merry } else { 1258a9934668SKenneth D. Merry error = copyin(user_ptr, kern_ptr, len_to_copy); 1259a9934668SKenneth D. Merry if (error != 0) { 1260a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyin of %u " 1261a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1262a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1263a9934668SKenneth D. Merry user_ptr, kern_ptr, error); 1264a9934668SKenneth D. Merry goto bailout; 1265a9934668SKenneth D. Merry } 1266a9934668SKenneth D. Merry } 1267a9934668SKenneth D. Merry 1268a9934668SKenneth D. Merry len_copied += len_to_copy; 1269a9934668SKenneth D. Merry 1270a9934668SKenneth D. Merry if (user_sglist[i].ds_len == user_watermark) { 1271a9934668SKenneth D. Merry i++; 1272a9934668SKenneth D. Merry user_watermark = 0; 1273a9934668SKenneth D. Merry } 1274a9934668SKenneth D. Merry 1275a9934668SKenneth D. Merry if (kern_sglist[j].ds_len == kern_watermark) { 1276a9934668SKenneth D. Merry j++; 1277a9934668SKenneth D. Merry kern_watermark = 0; 1278a9934668SKenneth D. Merry } 1279a9934668SKenneth D. Merry } 1280a9934668SKenneth D. Merry 1281a9934668SKenneth D. Merry bailout: 1282a9934668SKenneth D. Merry 1283a9934668SKenneth D. Merry return (error); 1284a9934668SKenneth D. Merry } 1285a9934668SKenneth D. Merry 1286a9934668SKenneth D. Merry static int 1287a9934668SKenneth D. Merry passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req) 1288a9934668SKenneth D. Merry { 1289a9934668SKenneth D. Merry union ccb *ccb; 1290a9934668SKenneth D. Merry struct pass_softc *softc; 1291a9934668SKenneth D. Merry int numbufs, i; 1292a9934668SKenneth D. Merry uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1293a9934668SKenneth D. Merry uint32_t lengths[CAM_PERIPH_MAXMAPS]; 1294a9934668SKenneth D. Merry uint32_t dirs[CAM_PERIPH_MAXMAPS]; 1295a9934668SKenneth D. Merry uint32_t num_segs; 1296a9934668SKenneth D. Merry uint16_t *seg_cnt_ptr; 1297a9934668SKenneth D. Merry size_t maxmap; 1298a9934668SKenneth D. Merry int error; 1299a9934668SKenneth D. Merry 1300a9934668SKenneth D. Merry cam_periph_assert(periph, MA_NOTOWNED); 1301a9934668SKenneth D. Merry 1302a9934668SKenneth D. Merry softc = periph->softc; 1303a9934668SKenneth D. Merry 1304a9934668SKenneth D. Merry error = 0; 1305a9934668SKenneth D. Merry ccb = &io_req->ccb; 1306a9934668SKenneth D. Merry maxmap = 0; 1307a9934668SKenneth D. Merry num_segs = 0; 1308a9934668SKenneth D. Merry seg_cnt_ptr = NULL; 1309a9934668SKenneth D. Merry 1310a9934668SKenneth D. Merry switch(ccb->ccb_h.func_code) { 1311a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1312a9934668SKenneth D. Merry if (ccb->cdm.match_buf_len == 0) { 1313a9934668SKenneth D. Merry printf("%s: invalid match buffer length 0\n", __func__); 1314a9934668SKenneth D. Merry return(EINVAL); 1315a9934668SKenneth D. Merry } 1316a9934668SKenneth D. Merry if (ccb->cdm.pattern_buf_len > 0) { 1317a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1318a9934668SKenneth D. Merry lengths[0] = ccb->cdm.pattern_buf_len; 1319a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1320a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1321a9934668SKenneth D. Merry lengths[1] = ccb->cdm.match_buf_len; 1322a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1323a9934668SKenneth D. Merry numbufs = 2; 1324a9934668SKenneth D. Merry } else { 1325a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1326a9934668SKenneth D. Merry lengths[0] = ccb->cdm.match_buf_len; 1327a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1328a9934668SKenneth D. Merry numbufs = 1; 1329a9934668SKenneth D. Merry } 1330a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1331a9934668SKenneth D. Merry break; 1332a9934668SKenneth D. Merry case XPT_SCSI_IO: 1333a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1334a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1335a9934668SKenneth D. Merry return(0); 1336a9934668SKenneth D. Merry 1337a9934668SKenneth D. Merry /* 1338a9934668SKenneth D. Merry * The user shouldn't be able to supply a bio. 1339a9934668SKenneth D. Merry */ 1340a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) 1341a9934668SKenneth D. Merry return (EINVAL); 1342a9934668SKenneth D. Merry 1343a9934668SKenneth D. Merry io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1344a9934668SKenneth D. Merry 1345a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1346a9934668SKenneth D. Merry lengths[0] = ccb->csio.dxfer_len; 1347a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1348a9934668SKenneth D. Merry num_segs = ccb->csio.sglist_cnt; 1349a9934668SKenneth D. Merry seg_cnt_ptr = &ccb->csio.sglist_cnt; 1350a9934668SKenneth D. Merry numbufs = 1; 1351a9934668SKenneth D. Merry maxmap = softc->maxio; 1352a9934668SKenneth D. Merry break; 1353a9934668SKenneth D. Merry case XPT_ATA_IO: 1354a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1355a9934668SKenneth D. Merry return(0); 1356a9934668SKenneth D. Merry 1357a9934668SKenneth D. Merry /* 1358a9934668SKenneth D. Merry * We only support a single virtual address for ATA I/O. 1359a9934668SKenneth D. Merry */ 1360a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 1361a9934668SKenneth D. Merry return (EINVAL); 1362a9934668SKenneth D. Merry 1363a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1364a9934668SKenneth D. Merry 1365a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1366a9934668SKenneth D. Merry lengths[0] = ccb->ataio.dxfer_len; 1367a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1368a9934668SKenneth D. Merry numbufs = 1; 1369a9934668SKenneth D. Merry maxmap = softc->maxio; 1370a9934668SKenneth D. Merry break; 1371a9934668SKenneth D. Merry case XPT_SMP_IO: 1372a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1373a9934668SKenneth D. Merry 1374a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1375a9934668SKenneth D. Merry lengths[0] = ccb->smpio.smp_request_len; 1376a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1377a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1378a9934668SKenneth D. Merry lengths[1] = ccb->smpio.smp_response_len; 1379a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1380a9934668SKenneth D. Merry numbufs = 2; 1381a9934668SKenneth D. Merry maxmap = softc->maxio; 1382a9934668SKenneth D. Merry break; 1383a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1384a9934668SKenneth D. Merry if (ccb->cdai.bufsiz == 0) 1385a9934668SKenneth D. Merry return (0); 1386a9934668SKenneth D. Merry 1387a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1388a9934668SKenneth D. Merry 1389a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1390a9934668SKenneth D. Merry lengths[0] = ccb->cdai.bufsiz; 1391a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1392a9934668SKenneth D. Merry numbufs = 1; 1393a9934668SKenneth D. Merry break; 1394df424515SWarner Losh case XPT_NVME_ADMIN: 1395df424515SWarner Losh case XPT_NVME_IO: 1396df424515SWarner Losh if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1397df424515SWarner Losh return (0); 1398df424515SWarner Losh 1399df424515SWarner Losh io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1400df424515SWarner Losh 1401df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1402df424515SWarner Losh lengths[0] = ccb->nvmeio.dxfer_len; 1403df424515SWarner Losh dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 140451977281SWarner Losh num_segs = ccb->nvmeio.sglist_cnt; 140551977281SWarner Losh seg_cnt_ptr = &ccb->nvmeio.sglist_cnt; 1406df424515SWarner Losh numbufs = 1; 1407df424515SWarner Losh maxmap = softc->maxio; 1408df424515SWarner Losh break; 1409a9934668SKenneth D. Merry default: 1410a9934668SKenneth D. Merry return(EINVAL); 1411a9934668SKenneth D. Merry break; /* NOTREACHED */ 1412a9934668SKenneth D. Merry } 1413a9934668SKenneth D. Merry 1414a9934668SKenneth D. Merry io_req->num_bufs = numbufs; 1415a9934668SKenneth D. Merry 1416a9934668SKenneth D. Merry /* 1417a9934668SKenneth D. Merry * If there is a maximum, check to make sure that the user's 1418a9934668SKenneth D. Merry * request fits within the limit. In general, we should only have 1419a9934668SKenneth D. Merry * a maximum length for requests that go to hardware. Otherwise it 1420a9934668SKenneth D. Merry * is whatever we're able to malloc. 1421a9934668SKenneth D. Merry */ 1422a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1423a9934668SKenneth D. Merry io_req->user_bufs[i] = *data_ptrs[i]; 1424a9934668SKenneth D. Merry io_req->dirs[i] = dirs[i]; 1425a9934668SKenneth D. Merry io_req->lengths[i] = lengths[i]; 1426a9934668SKenneth D. Merry 1427a9934668SKenneth D. Merry if (maxmap == 0) 1428a9934668SKenneth D. Merry continue; 1429a9934668SKenneth D. Merry 1430a9934668SKenneth D. Merry if (lengths[i] <= maxmap) 1431a9934668SKenneth D. Merry continue; 1432a9934668SKenneth D. Merry 1433a9934668SKenneth D. Merry xpt_print(periph->path, "%s: data length %u > max allowed %u " 1434a9934668SKenneth D. Merry "bytes\n", __func__, lengths[i], maxmap); 1435a9934668SKenneth D. Merry error = EINVAL; 1436a9934668SKenneth D. Merry goto bailout; 1437a9934668SKenneth D. Merry } 1438a9934668SKenneth D. Merry 1439a9934668SKenneth D. Merry switch (io_req->data_flags) { 1440a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1441a9934668SKenneth D. Merry /* Map or copy the buffer into kernel address space */ 1442a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1443a9934668SKenneth D. Merry uint8_t *tmp_buf; 1444a9934668SKenneth D. Merry 1445a9934668SKenneth D. Merry /* 1446a9934668SKenneth D. Merry * If for some reason no length is specified, we 1447a9934668SKenneth D. Merry * don't need to allocate anything. 1448a9934668SKenneth D. Merry */ 1449a9934668SKenneth D. Merry if (io_req->lengths[i] == 0) 1450a9934668SKenneth D. Merry continue; 1451a9934668SKenneth D. Merry 1452a9934668SKenneth D. Merry /* 1453a9934668SKenneth D. Merry * Make sure that the user's buffer is accessible 1454a9934668SKenneth D. Merry * to that process. 1455a9934668SKenneth D. Merry */ 1456a9934668SKenneth D. Merry if (!useracc(io_req->user_bufs[i], io_req->lengths[i], 1457a9934668SKenneth D. Merry (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE : 1458a9934668SKenneth D. Merry VM_PROT_READ)) { 1459a9934668SKenneth D. Merry xpt_print(periph->path, "%s: user address %p " 1460a9934668SKenneth D. Merry "length %u is not accessible\n", __func__, 1461a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1462a9934668SKenneth D. Merry error = EFAULT; 1463a9934668SKenneth D. Merry goto bailout; 1464a9934668SKenneth D. Merry } 1465a9934668SKenneth D. Merry 1466a9934668SKenneth D. Merry tmp_buf = malloc(lengths[i], M_SCSIPASS, 1467a9934668SKenneth D. Merry M_WAITOK | M_ZERO); 1468a9934668SKenneth D. Merry io_req->kern_bufs[i] = tmp_buf; 1469a9934668SKenneth D. Merry *data_ptrs[i] = tmp_buf; 1470a9934668SKenneth D. Merry 1471a9934668SKenneth D. Merry #if 0 1472a9934668SKenneth D. Merry xpt_print(periph->path, "%s: malloced %p len %u, user " 1473a9934668SKenneth D. Merry "buffer %p, operation: %s\n", __func__, 1474a9934668SKenneth D. Merry tmp_buf, lengths[i], io_req->user_bufs[i], 1475a9934668SKenneth D. Merry (dirs[i] == CAM_DIR_IN) ? "read" : "write"); 1476a9934668SKenneth D. Merry #endif 1477a9934668SKenneth D. Merry /* 1478a9934668SKenneth D. Merry * We only need to copy in if the user is writing. 1479a9934668SKenneth D. Merry */ 1480a9934668SKenneth D. Merry if (dirs[i] != CAM_DIR_OUT) 1481a9934668SKenneth D. Merry continue; 1482a9934668SKenneth D. Merry 1483a9934668SKenneth D. Merry error = copyin(io_req->user_bufs[i], 1484a9934668SKenneth D. Merry io_req->kern_bufs[i], lengths[i]); 1485a9934668SKenneth D. Merry if (error != 0) { 1486a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user " 1487a9934668SKenneth D. Merry "buffer from %p to %p failed with " 1488a9934668SKenneth D. Merry "error %d\n", __func__, 1489a9934668SKenneth D. Merry io_req->user_bufs[i], 1490a9934668SKenneth D. Merry io_req->kern_bufs[i], error); 1491a9934668SKenneth D. Merry goto bailout; 1492a9934668SKenneth D. Merry } 1493a9934668SKenneth D. Merry } 1494a9934668SKenneth D. Merry break; 1495a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1496a9934668SKenneth D. Merry /* Pass down the pointer as-is */ 1497a9934668SKenneth D. Merry break; 1498a9934668SKenneth D. Merry case CAM_DATA_SG: { 1499a9934668SKenneth D. Merry size_t sg_length, size_to_go, alloc_size; 1500a9934668SKenneth D. Merry uint32_t num_segs_needed; 1501a9934668SKenneth D. Merry 1502a9934668SKenneth D. Merry /* 1503a9934668SKenneth D. Merry * Copy the user S/G list in, and then copy in the 1504a9934668SKenneth D. Merry * individual segments. 1505a9934668SKenneth D. Merry */ 1506a9934668SKenneth D. Merry /* 1507a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1508a9934668SKenneth D. Merry */ 1509a9934668SKenneth D. Merry if (numbufs != 1) { 1510a9934668SKenneth D. Merry xpt_print(periph->path, "%s: cannot currently handle " 1511a9934668SKenneth D. Merry "more than one S/G list per CCB\n", __func__); 1512a9934668SKenneth D. Merry error = EINVAL; 1513a9934668SKenneth D. Merry goto bailout; 1514a9934668SKenneth D. Merry } 1515a9934668SKenneth D. Merry 1516a9934668SKenneth D. Merry /* 1517a9934668SKenneth D. Merry * We have to have at least one segment. 1518a9934668SKenneth D. Merry */ 1519a9934668SKenneth D. Merry if (num_segs == 0) { 1520a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG flag set, " 1521a9934668SKenneth D. Merry "but sglist_cnt=0!\n", __func__); 1522a9934668SKenneth D. Merry error = EINVAL; 1523a9934668SKenneth D. Merry goto bailout; 1524a9934668SKenneth D. Merry } 1525a9934668SKenneth D. Merry 1526a9934668SKenneth D. Merry /* 1527a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1528a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1529a9934668SKenneth D. Merry */ 1530a9934668SKenneth D. Merry if (lengths[0] == 0) { 1531a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1532a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1533a9934668SKenneth D. Merry error = EINVAL; 1534a9934668SKenneth D. Merry goto bailout; 1535a9934668SKenneth D. Merry } 1536a9934668SKenneth D. Merry 1537a9934668SKenneth D. Merry /* 1538a9934668SKenneth D. Merry * We allocate buffers in io_zone_size increments for an 1539a9934668SKenneth D. Merry * S/G list. This will generally be MAXPHYS. 1540a9934668SKenneth D. Merry */ 1541a9934668SKenneth D. Merry if (lengths[0] <= softc->io_zone_size) 1542a9934668SKenneth D. Merry num_segs_needed = 1; 1543a9934668SKenneth D. Merry else { 1544a9934668SKenneth D. Merry num_segs_needed = lengths[0] / softc->io_zone_size; 1545a9934668SKenneth D. Merry if ((lengths[0] % softc->io_zone_size) != 0) 1546a9934668SKenneth D. Merry num_segs_needed++; 1547a9934668SKenneth D. Merry } 1548a9934668SKenneth D. Merry 1549a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1550a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1551a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1552a9934668SKenneth D. Merry io_req->num_kern_segs = num_segs_needed; 1553a9934668SKenneth D. Merry 1554a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1555a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1556a9934668SKenneth D. Merry 1557a9934668SKenneth D. Merry /* 1558a9934668SKenneth D. Merry * If we have enough segments allocated by default to handle 1559a9934668SKenneth D. Merry * the length of the user's S/G list, 1560a9934668SKenneth D. Merry */ 1561a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1562a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1563a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1564a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1565a9934668SKenneth D. Merry } else 1566a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1567a9934668SKenneth D. Merry 1568a9934668SKenneth D. Merry if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) { 1569a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1570a9934668SKenneth D. Merry "S/G list at %p\n", __func__, *data_ptrs[0]); 1571a9934668SKenneth D. Merry error = EFAULT; 1572a9934668SKenneth D. Merry goto bailout; 1573a9934668SKenneth D. Merry } 1574a9934668SKenneth D. Merry 1575a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1576a9934668SKenneth D. Merry if (error != 0) { 1577a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1578a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1579a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1580a9934668SKenneth D. Merry error); 1581a9934668SKenneth D. Merry goto bailout; 1582a9934668SKenneth D. Merry } 1583a9934668SKenneth D. Merry 1584a9934668SKenneth D. Merry if (num_segs_needed > PASS_MAX_SEGS) { 1585a9934668SKenneth D. Merry io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) * 1586a9934668SKenneth D. Merry num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO); 1587a9934668SKenneth D. Merry io_req->flags |= PASS_IO_KERN_SEG_MALLOC; 1588a9934668SKenneth D. Merry } else { 1589a9934668SKenneth D. Merry io_req->kern_segptr = io_req->kern_segs; 1590a9934668SKenneth D. Merry } 1591a9934668SKenneth D. Merry 1592a9934668SKenneth D. Merry /* 1593a9934668SKenneth D. Merry * Allocate the kernel S/G list. 1594a9934668SKenneth D. Merry */ 1595a9934668SKenneth D. Merry for (size_to_go = lengths[0], i = 0; 1596a9934668SKenneth D. Merry size_to_go > 0 && i < num_segs_needed; 1597a9934668SKenneth D. Merry i++, size_to_go -= alloc_size) { 1598a9934668SKenneth D. Merry uint8_t *kern_ptr; 1599a9934668SKenneth D. Merry 1600a9934668SKenneth D. Merry alloc_size = min(size_to_go, softc->io_zone_size); 1601a9934668SKenneth D. Merry kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK); 1602a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 1603a9934668SKenneth D. Merry (bus_addr_t)(uintptr_t)kern_ptr; 1604a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_len = alloc_size; 1605a9934668SKenneth D. Merry } 1606a9934668SKenneth D. Merry if (size_to_go > 0) { 1607a9934668SKenneth D. Merry printf("%s: size_to_go = %zu, software error!\n", 1608a9934668SKenneth D. Merry __func__, size_to_go); 1609a9934668SKenneth D. Merry error = EINVAL; 1610a9934668SKenneth D. Merry goto bailout; 1611a9934668SKenneth D. Merry } 1612a9934668SKenneth D. Merry 1613a9934668SKenneth D. Merry *data_ptrs[0] = (uint8_t *)io_req->kern_segptr; 1614a9934668SKenneth D. Merry *seg_cnt_ptr = io_req->num_kern_segs; 1615a9934668SKenneth D. Merry 1616a9934668SKenneth D. Merry /* 1617a9934668SKenneth D. Merry * We only need to copy data here if the user is writing. 1618a9934668SKenneth D. Merry */ 1619a9934668SKenneth D. Merry if (dirs[0] == CAM_DIR_OUT) 1620a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, dirs[0]); 1621a9934668SKenneth D. Merry break; 1622a9934668SKenneth D. Merry } 1623a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: { 1624a9934668SKenneth D. Merry size_t sg_length; 1625a9934668SKenneth D. Merry 1626a9934668SKenneth D. Merry /* 1627a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1628a9934668SKenneth D. Merry */ 1629a9934668SKenneth D. Merry if (numbufs != 1) { 1630a9934668SKenneth D. Merry printf("%s: cannot currently handle more than one " 1631a9934668SKenneth D. Merry "S/G list per CCB\n", __func__); 1632a9934668SKenneth D. Merry error = EINVAL; 1633a9934668SKenneth D. Merry goto bailout; 1634a9934668SKenneth D. Merry } 1635a9934668SKenneth D. Merry 1636a9934668SKenneth D. Merry /* 1637a9934668SKenneth D. Merry * We have to have at least one segment. 1638a9934668SKenneth D. Merry */ 1639a9934668SKenneth D. Merry if (num_segs == 0) { 1640a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag " 1641a9934668SKenneth D. Merry "set, but sglist_cnt=0!\n", __func__); 1642a9934668SKenneth D. Merry error = EINVAL; 1643a9934668SKenneth D. Merry goto bailout; 1644a9934668SKenneth D. Merry } 1645a9934668SKenneth D. Merry 1646a9934668SKenneth D. Merry /* 1647a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1648a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1649a9934668SKenneth D. Merry */ 1650a9934668SKenneth D. Merry if (lengths[0] == 0) { 1651a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1652a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1653a9934668SKenneth D. Merry error = EINVAL; 1654a9934668SKenneth D. Merry goto bailout; 1655a9934668SKenneth D. Merry } 1656a9934668SKenneth D. Merry 1657a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1658a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1659a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1660a9934668SKenneth D. Merry io_req->num_kern_segs = io_req->num_user_segs; 1661a9934668SKenneth D. Merry 1662a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1663a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1664a9934668SKenneth D. Merry 1665a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1666a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1667a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1668a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1669a9934668SKenneth D. Merry } else 1670a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1671a9934668SKenneth D. Merry 1672a9934668SKenneth D. Merry io_req->kern_segptr = io_req->user_segptr; 1673a9934668SKenneth D. Merry 1674a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1675a9934668SKenneth D. Merry if (error != 0) { 1676a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1677a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1678a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1679a9934668SKenneth D. Merry error); 1680a9934668SKenneth D. Merry goto bailout; 1681a9934668SKenneth D. Merry } 1682a9934668SKenneth D. Merry break; 1683a9934668SKenneth D. Merry } 1684a9934668SKenneth D. Merry default: 1685a9934668SKenneth D. Merry case CAM_DATA_BIO: 1686a9934668SKenneth D. Merry /* 1687a9934668SKenneth D. Merry * A user shouldn't be attaching a bio to the CCB. It 1688a9934668SKenneth D. Merry * isn't a user-accessible structure. 1689a9934668SKenneth D. Merry */ 1690a9934668SKenneth D. Merry error = EINVAL; 1691a9934668SKenneth D. Merry break; 1692a9934668SKenneth D. Merry } 1693a9934668SKenneth D. Merry 1694a9934668SKenneth D. Merry bailout: 1695a9934668SKenneth D. Merry if (error != 0) 1696a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1697a9934668SKenneth D. Merry 1698a9934668SKenneth D. Merry return (error); 1699a9934668SKenneth D. Merry } 1700a9934668SKenneth D. Merry 1701a9934668SKenneth D. Merry static int 1702a9934668SKenneth D. Merry passmemdone(struct cam_periph *periph, struct pass_io_req *io_req) 1703a9934668SKenneth D. Merry { 1704a9934668SKenneth D. Merry struct pass_softc *softc; 1705a9934668SKenneth D. Merry union ccb *ccb; 1706a9934668SKenneth D. Merry int error; 1707a9934668SKenneth D. Merry int i; 1708a9934668SKenneth D. Merry 1709a9934668SKenneth D. Merry error = 0; 1710a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1711a9934668SKenneth D. Merry ccb = &io_req->ccb; 1712a9934668SKenneth D. Merry 1713a9934668SKenneth D. Merry switch (io_req->data_flags) { 1714a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1715a9934668SKenneth D. Merry /* 1716a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1717a9934668SKenneth D. Merry */ 1718a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1719a9934668SKenneth D. Merry if (io_req->dirs[i] != CAM_DIR_IN) 1720a9934668SKenneth D. Merry continue; 1721a9934668SKenneth D. Merry 1722a9934668SKenneth D. Merry error = copyout(io_req->kern_bufs[i], 1723a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1724a9934668SKenneth D. Merry if (error != 0) { 1725a9934668SKenneth D. Merry xpt_print(periph->path, "Unable to copy %u " 1726a9934668SKenneth D. Merry "bytes from %p to user address %p\n", 1727a9934668SKenneth D. Merry io_req->lengths[i], 1728a9934668SKenneth D. Merry io_req->kern_bufs[i], 1729a9934668SKenneth D. Merry io_req->user_bufs[i]); 1730a9934668SKenneth D. Merry goto bailout; 1731a9934668SKenneth D. Merry } 1732a9934668SKenneth D. Merry 1733a9934668SKenneth D. Merry } 1734a9934668SKenneth D. Merry break; 1735a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1736a9934668SKenneth D. Merry /* Do nothing. The pointer is a physical address already */ 1737a9934668SKenneth D. Merry break; 1738a9934668SKenneth D. Merry case CAM_DATA_SG: 1739a9934668SKenneth D. Merry /* 1740a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1741a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. 1742a9934668SKenneth D. Merry */ 1743a9934668SKenneth D. Merry if (io_req->dirs[0] == CAM_DIR_IN) 1744a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, io_req->dirs[0]); 1745a9934668SKenneth D. Merry break; 1746a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: 1747a9934668SKenneth D. Merry /* 1748a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. No need to 1749a9934668SKenneth D. Merry * copy. 1750a9934668SKenneth D. Merry */ 1751a9934668SKenneth D. Merry break; 1752a9934668SKenneth D. Merry default: 1753a9934668SKenneth D. Merry case CAM_DATA_BIO: 1754a9934668SKenneth D. Merry error = EINVAL; 1755a9934668SKenneth D. Merry break; 1756a9934668SKenneth D. Merry } 1757a9934668SKenneth D. Merry 1758a9934668SKenneth D. Merry bailout: 1759a9934668SKenneth D. Merry /* 1760a9934668SKenneth D. Merry * Reset the user's pointers to their original values and free 1761a9934668SKenneth D. Merry * allocated memory. 1762a9934668SKenneth D. Merry */ 1763a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1764a9934668SKenneth D. Merry 1765a9934668SKenneth D. Merry return (error); 1766a9934668SKenneth D. Merry } 1767a9934668SKenneth D. Merry 176876babe50SJustin T. Gibbs static int 176989c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 177076babe50SJustin T. Gibbs { 177125a2902cSScott Long int error; 177225a2902cSScott Long 177325a2902cSScott Long if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 1774f564de00SScott Long error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl); 177525a2902cSScott Long } 177625a2902cSScott Long return (error); 177725a2902cSScott Long } 177825a2902cSScott Long 177925a2902cSScott Long static int 178025a2902cSScott Long passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 178125a2902cSScott Long { 178276babe50SJustin T. Gibbs struct cam_periph *periph; 1783a9934668SKenneth D. Merry struct pass_softc *softc; 178476babe50SJustin T. Gibbs int error; 17858cff7eb8SAlexander Motin uint32_t priority; 178676babe50SJustin T. Gibbs 1787e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 17882b83592fSScott Long cam_periph_lock(periph); 1789a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 179076babe50SJustin T. Gibbs 179176babe50SJustin T. Gibbs error = 0; 179276babe50SJustin T. Gibbs 179376babe50SJustin T. Gibbs switch (cmd) { 179476babe50SJustin T. Gibbs 179576babe50SJustin T. Gibbs case CAMIOCOMMAND: 179676babe50SJustin T. Gibbs { 179776babe50SJustin T. Gibbs union ccb *inccb; 179876babe50SJustin T. Gibbs union ccb *ccb; 17999deea857SKenneth D. Merry int ccb_malloced; 180076babe50SJustin T. Gibbs 180176babe50SJustin T. Gibbs inccb = (union ccb *)addr; 18028fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 18038fc77fffSConrad Meyer if (inccb->ccb_h.func_code == XPT_SCSI_IO) 18048fc77fffSConrad Meyer inccb->csio.bio = NULL; 18058fc77fffSConrad Meyer #endif 18069deea857SKenneth D. Merry 1807a0bbf9e0SMark Johnston if (inccb->ccb_h.flags & CAM_UNLOCKED) { 1808a0bbf9e0SMark Johnston error = EINVAL; 1809a0bbf9e0SMark Johnston break; 1810a0bbf9e0SMark Johnston } 1811a0bbf9e0SMark Johnston 18129deea857SKenneth D. Merry /* 18139deea857SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 18149deea857SKenneth D. Merry * through the transport layer device. 18159deea857SKenneth D. Merry */ 18169deea857SKenneth D. Merry if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1817f0d9af51SMatt Jacob xpt_print(periph->path, "CCB function code %#x is " 1818f0d9af51SMatt Jacob "restricted to the XPT device\n", 1819f0d9af51SMatt Jacob inccb->ccb_h.func_code); 18209deea857SKenneth D. Merry error = ENODEV; 18219deea857SKenneth D. Merry break; 18229deea857SKenneth D. Merry } 18239deea857SKenneth D. Merry 18248cff7eb8SAlexander Motin /* Compatibility for RL/priority-unaware code. */ 18258cff7eb8SAlexander Motin priority = inccb->ccb_h.pinfo.priority; 1826cccf4220SAlexander Motin if (priority <= CAM_PRIORITY_OOB) 1827cccf4220SAlexander Motin priority += CAM_PRIORITY_OOB + 1; 18288cff7eb8SAlexander Motin 18299deea857SKenneth D. Merry /* 18309deea857SKenneth D. Merry * Non-immediate CCBs need a CCB from the per-device pool 18319deea857SKenneth D. Merry * of CCBs, which is scheduled by the transport layer. 18329deea857SKenneth D. Merry * Immediate CCBs and user-supplied CCBs should just be 18339deea857SKenneth D. Merry * malloced. 18349deea857SKenneth D. Merry */ 18359deea857SKenneth D. Merry if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 18369deea857SKenneth D. Merry && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 18378cff7eb8SAlexander Motin ccb = cam_periph_getccb(periph, priority); 18389deea857SKenneth D. Merry ccb_malloced = 0; 18399deea857SKenneth D. Merry } else { 18408008a935SScott Long ccb = xpt_alloc_ccb_nowait(); 18419deea857SKenneth D. Merry 18429deea857SKenneth D. Merry if (ccb != NULL) 18439deea857SKenneth D. Merry xpt_setup_ccb(&ccb->ccb_h, periph->path, 18448cff7eb8SAlexander Motin priority); 18459deea857SKenneth D. Merry ccb_malloced = 1; 18469deea857SKenneth D. Merry } 18479deea857SKenneth D. Merry 18489deea857SKenneth D. Merry if (ccb == NULL) { 1849f0d9af51SMatt Jacob xpt_print(periph->path, "unable to allocate CCB\n"); 18509deea857SKenneth D. Merry error = ENOMEM; 18519deea857SKenneth D. Merry break; 18529deea857SKenneth D. Merry } 185376babe50SJustin T. Gibbs 185476babe50SJustin T. Gibbs error = passsendccb(periph, ccb, inccb); 185576babe50SJustin T. Gibbs 18569deea857SKenneth D. Merry if (ccb_malloced) 18579deea857SKenneth D. Merry xpt_free_ccb(ccb); 18589deea857SKenneth D. Merry else 185976babe50SJustin T. Gibbs xpt_release_ccb(ccb); 186076babe50SJustin T. Gibbs 186176babe50SJustin T. Gibbs break; 186276babe50SJustin T. Gibbs } 1863a9934668SKenneth D. Merry case CAMIOQUEUE: 1864a9934668SKenneth D. Merry { 1865a9934668SKenneth D. Merry struct pass_io_req *io_req; 1866a9934668SKenneth D. Merry union ccb **user_ccb, *ccb; 1867a9934668SKenneth D. Merry xpt_opcode fc; 1868a9934668SKenneth D. Merry 1869a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) { 1870a9934668SKenneth D. Merry error = passcreatezone(periph); 1871a9934668SKenneth D. Merry if (error != 0) 1872a9934668SKenneth D. Merry goto bailout; 1873a9934668SKenneth D. Merry } 1874a9934668SKenneth D. Merry 1875a9934668SKenneth D. Merry /* 1876a9934668SKenneth D. Merry * We're going to do a blocking allocation for this I/O 1877a9934668SKenneth D. Merry * request, so we have to drop the lock. 1878a9934668SKenneth D. Merry */ 1879a9934668SKenneth D. Merry cam_periph_unlock(periph); 1880a9934668SKenneth D. Merry 1881a9934668SKenneth D. Merry io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO); 1882a9934668SKenneth D. Merry ccb = &io_req->ccb; 1883a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 1884a9934668SKenneth D. Merry 1885a9934668SKenneth D. Merry /* 1886a9934668SKenneth D. Merry * Unlike the CAMIOCOMMAND ioctl above, we only have a 1887a9934668SKenneth D. Merry * pointer to the user's CCB, so we have to copy the whole 1888a9934668SKenneth D. Merry * thing in to a buffer we have allocated (above) instead 1889a9934668SKenneth D. Merry * of allowing the ioctl code to malloc a buffer and copy 1890a9934668SKenneth D. Merry * it in. 1891a9934668SKenneth D. Merry * 1892a9934668SKenneth D. Merry * This is an advantage for this asynchronous interface, 1893a9934668SKenneth D. Merry * since we don't want the memory to get freed while the 1894a9934668SKenneth D. Merry * CCB is outstanding. 1895a9934668SKenneth D. Merry */ 1896a9934668SKenneth D. Merry #if 0 1897a9934668SKenneth D. Merry xpt_print(periph->path, "Copying user CCB %p to " 1898a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, ccb); 1899a9934668SKenneth D. Merry #endif 1900a9934668SKenneth D. Merry error = copyin(*user_ccb, ccb, sizeof(*ccb)); 1901a9934668SKenneth D. Merry if (error != 0) { 1902a9934668SKenneth D. Merry xpt_print(periph->path, "Copy of user CCB %p to " 1903a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 1904a9934668SKenneth D. Merry *user_ccb, ccb, error); 1905a0bbf9e0SMark Johnston goto camioqueue_error; 1906a9934668SKenneth D. Merry } 19078fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 19088fc77fffSConrad Meyer if (ccb->ccb_h.func_code == XPT_SCSI_IO) 19098fc77fffSConrad Meyer ccb->csio.bio = NULL; 19108fc77fffSConrad Meyer #endif 1911a9934668SKenneth D. Merry 1912a0bbf9e0SMark Johnston if (ccb->ccb_h.flags & CAM_UNLOCKED) { 1913a0bbf9e0SMark Johnston error = EINVAL; 1914a0bbf9e0SMark Johnston goto camioqueue_error; 1915a0bbf9e0SMark Johnston } 1916a0bbf9e0SMark Johnston 1917a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 1918a1a604caSAlexander Motin if (ccb->csio.cdb_len > IOCDBLEN) { 1919a1a604caSAlexander Motin error = EINVAL; 1920a0bbf9e0SMark Johnston goto camioqueue_error; 1921a1a604caSAlexander Motin } 1922a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, 1923a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len); 1924a0bbf9e0SMark Johnston if (error != 0) 1925a0bbf9e0SMark Johnston goto camioqueue_error; 1926a1a604caSAlexander Motin ccb->ccb_h.flags &= ~CAM_CDB_POINTER; 1927a1a604caSAlexander Motin } 1928a1a604caSAlexander Motin 1929a9934668SKenneth D. Merry /* 1930a9934668SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 1931a9934668SKenneth D. Merry * through the transport layer device. 1932a9934668SKenneth D. Merry */ 1933a9934668SKenneth D. Merry if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1934a9934668SKenneth D. Merry xpt_print(periph->path, "CCB function code %#x is " 1935a9934668SKenneth D. Merry "restricted to the XPT device\n", 1936a9934668SKenneth D. Merry ccb->ccb_h.func_code); 1937a9934668SKenneth D. Merry error = ENODEV; 1938a0bbf9e0SMark Johnston goto camioqueue_error; 1939a9934668SKenneth D. Merry } 1940a9934668SKenneth D. Merry 1941a9934668SKenneth D. Merry /* 1942a9934668SKenneth D. Merry * Save the user's CCB pointer as well as his linked list 1943a9934668SKenneth D. Merry * pointers and peripheral private area so that we can 1944a9934668SKenneth D. Merry * restore these later. 1945a9934668SKenneth D. Merry */ 1946a9934668SKenneth D. Merry io_req->user_ccb_ptr = *user_ccb; 1947a9934668SKenneth D. Merry io_req->user_periph_links = ccb->ccb_h.periph_links; 1948a9934668SKenneth D. Merry io_req->user_periph_priv = ccb->ccb_h.periph_priv; 1949a9934668SKenneth D. Merry 1950a9934668SKenneth D. Merry /* 1951a9934668SKenneth D. Merry * Now that we've saved the user's values, we can set our 1952a9934668SKenneth D. Merry * own peripheral private entry. 1953a9934668SKenneth D. Merry */ 1954a9934668SKenneth D. Merry ccb->ccb_h.ccb_ioreq = io_req; 1955a9934668SKenneth D. Merry 1956a9934668SKenneth D. Merry /* Compatibility for RL/priority-unaware code. */ 1957a9934668SKenneth D. Merry priority = ccb->ccb_h.pinfo.priority; 1958a9934668SKenneth D. Merry if (priority <= CAM_PRIORITY_OOB) 1959a9934668SKenneth D. Merry priority += CAM_PRIORITY_OOB + 1; 1960a9934668SKenneth D. Merry 1961a9934668SKenneth D. Merry /* 1962a9934668SKenneth D. Merry * Setup fields in the CCB like the path and the priority. 1963a9934668SKenneth D. Merry * The path in particular cannot be done in userland, since 1964a9934668SKenneth D. Merry * it is a pointer to a kernel data structure. 1965a9934668SKenneth D. Merry */ 1966a9934668SKenneth D. Merry xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority, 1967a9934668SKenneth D. Merry ccb->ccb_h.flags); 1968a9934668SKenneth D. Merry 1969a9934668SKenneth D. Merry /* 1970a9934668SKenneth D. Merry * Setup our done routine. There is no way for the user to 1971a9934668SKenneth D. Merry * have a valid pointer here. 1972a9934668SKenneth D. Merry */ 1973a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 1974a9934668SKenneth D. Merry 1975a9934668SKenneth D. Merry fc = ccb->ccb_h.func_code; 1976a9934668SKenneth D. Merry /* 1977a9934668SKenneth D. Merry * If this function code has memory that can be mapped in 1978a9934668SKenneth D. Merry * or out, we need to call passmemsetup(). 1979a9934668SKenneth D. Merry */ 1980a9934668SKenneth D. Merry if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) 1981a9934668SKenneth D. Merry || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH) 1982df424515SWarner Losh || (fc == XPT_DEV_ADVINFO) 1983df424515SWarner Losh || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 1984a9934668SKenneth D. Merry error = passmemsetup(periph, io_req); 1985a0bbf9e0SMark Johnston if (error != 0) 1986a0bbf9e0SMark Johnston goto camioqueue_error; 1987a9934668SKenneth D. Merry } else 1988a9934668SKenneth D. Merry io_req->mapinfo.num_bufs_used = 0; 1989a9934668SKenneth D. Merry 1990a9934668SKenneth D. Merry cam_periph_lock(periph); 1991a9934668SKenneth D. Merry 1992a9934668SKenneth D. Merry /* 1993a9934668SKenneth D. Merry * Everything goes on the incoming queue initially. 1994a9934668SKenneth D. Merry */ 1995a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links); 1996a9934668SKenneth D. Merry 1997a9934668SKenneth D. Merry /* 1998a9934668SKenneth D. Merry * If the CCB is queued, and is not a user CCB, then 1999a9934668SKenneth D. Merry * we need to allocate a slot for it. Call xpt_schedule() 2000a9934668SKenneth D. Merry * so that our start routine will get called when a CCB is 2001a9934668SKenneth D. Merry * available. 2002a9934668SKenneth D. Merry */ 2003a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) 2004a9934668SKenneth D. Merry && ((fc & XPT_FC_USER_CCB) == 0)) { 2005a9934668SKenneth D. Merry xpt_schedule(periph, priority); 2006a9934668SKenneth D. Merry break; 2007a9934668SKenneth D. Merry } 2008a9934668SKenneth D. Merry 2009a9934668SKenneth D. Merry /* 2010a9934668SKenneth D. Merry * At this point, the CCB in question is either an 2011a9934668SKenneth D. Merry * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB 2012a9934668SKenneth D. Merry * and therefore should be malloced, not allocated via a slot. 2013a9934668SKenneth D. Merry * Remove the CCB from the incoming queue and add it to the 2014a9934668SKenneth D. Merry * active queue. 2015a9934668SKenneth D. Merry */ 2016a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 2017a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 2018a9934668SKenneth D. Merry 2019a9934668SKenneth D. Merry xpt_action(ccb); 2020a9934668SKenneth D. Merry 2021a9934668SKenneth D. Merry /* 2022a9934668SKenneth D. Merry * If this is not a queued CCB (i.e. it is an immediate CCB), 2023a9934668SKenneth D. Merry * then it is already done. We need to put it on the done 2024a9934668SKenneth D. Merry * queue for the user to fetch. 2025a9934668SKenneth D. Merry */ 2026a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) == 0) { 2027a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 2028a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 2029a9934668SKenneth D. Merry } 2030a9934668SKenneth D. Merry break; 2031a0bbf9e0SMark Johnston 2032a0bbf9e0SMark Johnston camioqueue_error: 2033a0bbf9e0SMark Johnston uma_zfree(softc->pass_zone, io_req); 2034a0bbf9e0SMark Johnston cam_periph_lock(periph); 2035a0bbf9e0SMark Johnston break; 2036a9934668SKenneth D. Merry } 2037a9934668SKenneth D. Merry case CAMIOGET: 2038a9934668SKenneth D. Merry { 2039a9934668SKenneth D. Merry union ccb **user_ccb; 2040a9934668SKenneth D. Merry struct pass_io_req *io_req; 2041a9934668SKenneth D. Merry int old_error; 2042a9934668SKenneth D. Merry 2043a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 2044a9934668SKenneth D. Merry old_error = 0; 2045a9934668SKenneth D. Merry 2046a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->done_queue); 2047a9934668SKenneth D. Merry if (io_req == NULL) { 2048a9934668SKenneth D. Merry error = ENOENT; 2049a9934668SKenneth D. Merry break; 2050a9934668SKenneth D. Merry } 2051a9934668SKenneth D. Merry 2052a9934668SKenneth D. Merry /* 2053a9934668SKenneth D. Merry * Remove the I/O from the done queue. 2054a9934668SKenneth D. Merry */ 2055a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 2056a9934668SKenneth D. Merry 2057a9934668SKenneth D. Merry /* 2058a9934668SKenneth D. Merry * We have to drop the lock during the copyout because the 2059a9934668SKenneth D. Merry * copyout can result in VM faults that require sleeping. 2060a9934668SKenneth D. Merry */ 2061a9934668SKenneth D. Merry cam_periph_unlock(periph); 2062a9934668SKenneth D. Merry 2063a9934668SKenneth D. Merry /* 2064a9934668SKenneth D. Merry * Do any needed copies (e.g. for reads) and revert the 2065a9934668SKenneth D. Merry * pointers in the CCB back to the user's pointers. 2066a9934668SKenneth D. Merry */ 2067a9934668SKenneth D. Merry error = passmemdone(periph, io_req); 2068a9934668SKenneth D. Merry 2069a9934668SKenneth D. Merry old_error = error; 2070a9934668SKenneth D. Merry 2071a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_links = io_req->user_periph_links; 2072a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv; 2073a9934668SKenneth D. Merry 2074a9934668SKenneth D. Merry #if 0 2075a9934668SKenneth D. Merry xpt_print(periph->path, "Copying to user CCB %p from " 2076a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, &io_req->ccb); 2077a9934668SKenneth D. Merry #endif 2078a9934668SKenneth D. Merry 2079a9934668SKenneth D. Merry error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb)); 2080a9934668SKenneth D. Merry if (error != 0) { 2081a9934668SKenneth D. Merry xpt_print(periph->path, "Copy to user CCB %p from " 2082a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 2083a9934668SKenneth D. Merry *user_ccb, &io_req->ccb, error); 2084a9934668SKenneth D. Merry } 2085a9934668SKenneth D. Merry 2086a9934668SKenneth D. Merry /* 2087a9934668SKenneth D. Merry * Prefer the first error we got back, and make sure we 2088a9934668SKenneth D. Merry * don't overwrite bad status with good. 2089a9934668SKenneth D. Merry */ 2090a9934668SKenneth D. Merry if (old_error != 0) 2091a9934668SKenneth D. Merry error = old_error; 2092a9934668SKenneth D. Merry 2093a9934668SKenneth D. Merry cam_periph_lock(periph); 2094a9934668SKenneth D. Merry 2095a9934668SKenneth D. Merry /* 2096a9934668SKenneth D. Merry * At this point, if there was an error, we could potentially 2097a9934668SKenneth D. Merry * re-queue the I/O and try again. But why? The error 2098a9934668SKenneth D. Merry * would almost certainly happen again. We might as well 2099a9934668SKenneth D. Merry * not leak memory. 2100a9934668SKenneth D. Merry */ 2101a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 2102a9934668SKenneth D. Merry break; 2103a9934668SKenneth D. Merry } 210476babe50SJustin T. Gibbs default: 210576babe50SJustin T. Gibbs error = cam_periph_ioctl(periph, cmd, addr, passerror); 210676babe50SJustin T. Gibbs break; 210776babe50SJustin T. Gibbs } 210876babe50SJustin T. Gibbs 2109a9934668SKenneth D. Merry bailout: 21102b83592fSScott Long cam_periph_unlock(periph); 2111a9934668SKenneth D. Merry 211276babe50SJustin T. Gibbs return(error); 211376babe50SJustin T. Gibbs } 211476babe50SJustin T. Gibbs 2115a9934668SKenneth D. Merry static int 2116a9934668SKenneth D. Merry passpoll(struct cdev *dev, int poll_events, struct thread *td) 2117a9934668SKenneth D. Merry { 2118a9934668SKenneth D. Merry struct cam_periph *periph; 2119a9934668SKenneth D. Merry struct pass_softc *softc; 2120a9934668SKenneth D. Merry int revents; 2121a9934668SKenneth D. Merry 2122a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2123a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2124a9934668SKenneth D. Merry 2125a9934668SKenneth D. Merry revents = poll_events & (POLLOUT | POLLWRNORM); 2126a9934668SKenneth D. Merry if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 2127a9934668SKenneth D. Merry cam_periph_lock(periph); 2128a9934668SKenneth D. Merry 2129a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->done_queue)) { 2130a9934668SKenneth D. Merry revents |= poll_events & (POLLIN | POLLRDNORM); 2131a9934668SKenneth D. Merry } 2132a9934668SKenneth D. Merry cam_periph_unlock(periph); 2133a9934668SKenneth D. Merry if (revents == 0) 2134a9934668SKenneth D. Merry selrecord(td, &softc->read_select); 2135a9934668SKenneth D. Merry } 2136a9934668SKenneth D. Merry 2137a9934668SKenneth D. Merry return (revents); 2138a9934668SKenneth D. Merry } 2139a9934668SKenneth D. Merry 2140a9934668SKenneth D. Merry static int 2141a9934668SKenneth D. Merry passkqfilter(struct cdev *dev, struct knote *kn) 2142a9934668SKenneth D. Merry { 2143a9934668SKenneth D. Merry struct cam_periph *periph; 2144a9934668SKenneth D. Merry struct pass_softc *softc; 2145a9934668SKenneth D. Merry 2146a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2147a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2148a9934668SKenneth D. Merry 2149a9934668SKenneth D. Merry kn->kn_hook = (caddr_t)periph; 2150a9934668SKenneth D. Merry kn->kn_fop = &passread_filtops; 2151a9934668SKenneth D. Merry knlist_add(&softc->read_select.si_note, kn, 0); 2152a9934668SKenneth D. Merry 2153a9934668SKenneth D. Merry return (0); 2154a9934668SKenneth D. Merry } 2155a9934668SKenneth D. Merry 2156a9934668SKenneth D. Merry static void 2157a9934668SKenneth D. Merry passreadfiltdetach(struct knote *kn) 2158a9934668SKenneth D. Merry { 2159a9934668SKenneth D. Merry struct cam_periph *periph; 2160a9934668SKenneth D. Merry struct pass_softc *softc; 2161a9934668SKenneth D. Merry 2162a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2163a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2164a9934668SKenneth D. Merry 2165a9934668SKenneth D. Merry knlist_remove(&softc->read_select.si_note, kn, 0); 2166a9934668SKenneth D. Merry } 2167a9934668SKenneth D. Merry 2168a9934668SKenneth D. Merry static int 2169a9934668SKenneth D. Merry passreadfilt(struct knote *kn, long hint) 2170a9934668SKenneth D. Merry { 2171a9934668SKenneth D. Merry struct cam_periph *periph; 2172a9934668SKenneth D. Merry struct pass_softc *softc; 2173a9934668SKenneth D. Merry int retval; 2174a9934668SKenneth D. Merry 2175a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2176a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2177a9934668SKenneth D. Merry 2178a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 2179a9934668SKenneth D. Merry 2180a9934668SKenneth D. Merry if (TAILQ_EMPTY(&softc->done_queue)) 2181a9934668SKenneth D. Merry retval = 0; 2182a9934668SKenneth D. Merry else 2183a9934668SKenneth D. Merry retval = 1; 2184a9934668SKenneth D. Merry 2185a9934668SKenneth D. Merry return (retval); 2186a9934668SKenneth D. Merry } 2187a9934668SKenneth D. Merry 218876babe50SJustin T. Gibbs /* 218976babe50SJustin T. Gibbs * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 219076babe50SJustin T. Gibbs * should be the CCB that is copied in from the user. 219176babe50SJustin T. Gibbs */ 219276babe50SJustin T. Gibbs static int 219376babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 219476babe50SJustin T. Gibbs { 219576babe50SJustin T. Gibbs struct pass_softc *softc; 219676babe50SJustin T. Gibbs struct cam_periph_map_info mapinfo; 2197a1a604caSAlexander Motin uint8_t *cmd; 219895fbded6SScott Long xpt_opcode fc; 219995fbded6SScott Long int error; 220076babe50SJustin T. Gibbs 220176babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 220276babe50SJustin T. Gibbs 220376babe50SJustin T. Gibbs /* 220476babe50SJustin T. Gibbs * There are some fields in the CCB header that need to be 220576babe50SJustin T. Gibbs * preserved, the rest we get from the user. 220676babe50SJustin T. Gibbs */ 220776babe50SJustin T. Gibbs xpt_merge_ccb(ccb, inccb); 220876babe50SJustin T. Gibbs 2209a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 2210a1a604caSAlexander Motin cmd = __builtin_alloca(ccb->csio.cdb_len); 2211a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len); 2212a1a604caSAlexander Motin if (error) 2213a1a604caSAlexander Motin return (error); 2214a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_ptr = cmd; 2215a1a604caSAlexander Motin } 2216a1a604caSAlexander Motin 221776babe50SJustin T. Gibbs /* 2218a9934668SKenneth D. Merry */ 2219a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 2220a9934668SKenneth D. Merry 2221a9934668SKenneth D. Merry /* 222295fbded6SScott Long * Let cam_periph_mapmem do a sanity check on the data pointer format. 222395fbded6SScott Long * Even if no data transfer is needed, it's a cheap check and it 222495fbded6SScott Long * simplifies the code. 222576babe50SJustin T. Gibbs */ 222695fbded6SScott Long fc = ccb->ccb_h.func_code; 222795fbded6SScott Long if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO) 2228a3cf03a5SWarner Losh || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO) 2229a3cf03a5SWarner Losh || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 2230a3cf03a5SWarner Losh 223176babe50SJustin T. Gibbs bzero(&mapinfo, sizeof(mapinfo)); 223276babe50SJustin T. Gibbs 22332b83592fSScott Long /* 22342b83592fSScott Long * cam_periph_mapmem calls into proc and vm functions that can 22352b83592fSScott Long * sleep as well as trigger I/O, so we can't hold the lock. 22362b83592fSScott Long * Dropping it here is reasonably safe. 22372b83592fSScott Long */ 22382b83592fSScott Long cam_periph_unlock(periph); 2239de239312SAlexander Motin error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio); 22402b83592fSScott Long cam_periph_lock(periph); 224176babe50SJustin T. Gibbs 224276babe50SJustin T. Gibbs /* 224376babe50SJustin T. Gibbs * cam_periph_mapmem returned an error, we can't continue. 224476babe50SJustin T. Gibbs * Return the error to the user. 224576babe50SJustin T. Gibbs */ 224676babe50SJustin T. Gibbs if (error) 224776babe50SJustin T. Gibbs return(error); 224895fbded6SScott Long } else 224995fbded6SScott Long /* Ensure that the unmap call later on is a no-op. */ 225095fbded6SScott Long mapinfo.num_bufs_used = 0; 225176babe50SJustin T. Gibbs 225276babe50SJustin T. Gibbs /* 225376babe50SJustin T. Gibbs * If the user wants us to perform any error recovery, then honor 225476babe50SJustin T. Gibbs * that request. Otherwise, it's up to the user to perform any 225576babe50SJustin T. Gibbs * error recovery. 225676babe50SJustin T. Gibbs */ 22576953d22bSKenneth D. Merry cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 22586953d22bSKenneth D. Merry passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO, 22596953d22bSKenneth D. Merry /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 2260a9d2245eSPoul-Henning Kamp softc->device_stats); 226176babe50SJustin T. Gibbs 226276babe50SJustin T. Gibbs cam_periph_unmapmem(ccb, &mapinfo); 226376babe50SJustin T. Gibbs 226476babe50SJustin T. Gibbs ccb->ccb_h.cbfcnp = NULL; 226576babe50SJustin T. Gibbs ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 226676babe50SJustin T. Gibbs bcopy(ccb, inccb, sizeof(union ccb)); 226776babe50SJustin T. Gibbs 226883c5d981SAlexander Motin return(0); 226976babe50SJustin T. Gibbs } 227076babe50SJustin T. Gibbs 227176babe50SJustin T. Gibbs static int 227276babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 227376babe50SJustin T. Gibbs { 227476babe50SJustin T. Gibbs struct cam_periph *periph; 227576babe50SJustin T. Gibbs struct pass_softc *softc; 227676babe50SJustin T. Gibbs 227776babe50SJustin T. Gibbs periph = xpt_path_periph(ccb->ccb_h.path); 227876babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 227976babe50SJustin T. Gibbs 228076babe50SJustin T. Gibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 228176babe50SJustin T. Gibbs &softc->saved_ccb)); 228276babe50SJustin T. Gibbs } 2283