1898b0535SWarner Losh /*- 23393f8daSKenneth D. Merry * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs. 32a888f93SKenneth D. Merry * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 476babe50SJustin T. Gibbs * All rights reserved. 576babe50SJustin T. Gibbs * 676babe50SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 776babe50SJustin T. Gibbs * modification, are permitted provided that the following conditions 876babe50SJustin T. Gibbs * are met: 976babe50SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 1076babe50SJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 1176babe50SJustin T. Gibbs * without modification, immediately at the beginning of the file. 1276babe50SJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 1376babe50SJustin T. Gibbs * derived from this software without specific prior written permission. 1476babe50SJustin T. Gibbs * 1576babe50SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1676babe50SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1776babe50SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1876babe50SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 1976babe50SJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2076babe50SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2176babe50SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2276babe50SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2376babe50SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2476babe50SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2576babe50SJustin T. Gibbs * SUCH DAMAGE. 2676babe50SJustin T. Gibbs */ 2776babe50SJustin T. Gibbs 28ee709e70SDavid E. O'Brien #include <sys/cdefs.h> 29ee709e70SDavid E. O'Brien __FBSDID("$FreeBSD$"); 30ee709e70SDavid E. O'Brien 3176babe50SJustin T. Gibbs #include <sys/param.h> 3276babe50SJustin T. Gibbs #include <sys/systm.h> 3376babe50SJustin T. Gibbs #include <sys/kernel.h> 34a9934668SKenneth D. Merry #include <sys/conf.h> 3576babe50SJustin T. Gibbs #include <sys/types.h> 369626b608SPoul-Henning Kamp #include <sys/bio.h> 37a9934668SKenneth D. Merry #include <sys/bus.h> 3876babe50SJustin T. Gibbs #include <sys/devicestat.h> 39a9934668SKenneth D. Merry #include <sys/errno.h> 40a9934668SKenneth D. Merry #include <sys/fcntl.h> 41a9934668SKenneth D. Merry #include <sys/malloc.h> 42f7312ca2SRobert Watson #include <sys/proc.h> 43a9934668SKenneth D. Merry #include <sys/poll.h> 44a9934668SKenneth D. Merry #include <sys/selinfo.h> 45a9934668SKenneth D. Merry #include <sys/sdt.h> 46416494d7SJustin T. Gibbs #include <sys/taskqueue.h> 47a9934668SKenneth D. Merry #include <vm/uma.h> 48a9934668SKenneth D. Merry #include <vm/vm.h> 49a9934668SKenneth D. Merry #include <vm/vm_extern.h> 50a9934668SKenneth D. Merry 51a9934668SKenneth D. Merry #include <machine/bus.h> 5276babe50SJustin T. Gibbs 5376babe50SJustin T. Gibbs #include <cam/cam.h> 5476babe50SJustin T. Gibbs #include <cam/cam_ccb.h> 5576babe50SJustin T. Gibbs #include <cam/cam_periph.h> 563393f8daSKenneth D. Merry #include <cam/cam_queue.h> 57a9934668SKenneth D. Merry #include <cam/cam_xpt.h> 5876babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h> 5976babe50SJustin T. Gibbs #include <cam/cam_debug.h> 6025a2902cSScott Long #include <cam/cam_compat.h> 61a9934668SKenneth D. Merry #include <cam/cam_xpt_periph.h> 6276babe50SJustin T. Gibbs 6376babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h> 6476babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h> 6576babe50SJustin T. Gibbs 6676babe50SJustin T. Gibbs typedef enum { 6776babe50SJustin T. Gibbs PASS_FLAG_OPEN = 0x01, 6876babe50SJustin T. Gibbs PASS_FLAG_LOCKED = 0x02, 69ea37f519SKenneth D. Merry PASS_FLAG_INVALID = 0x04, 70a9934668SKenneth D. Merry PASS_FLAG_INITIAL_PHYSPATH = 0x08, 71a9934668SKenneth D. Merry PASS_FLAG_ZONE_INPROG = 0x10, 72a9934668SKenneth D. Merry PASS_FLAG_ZONE_VALID = 0x20, 73a9934668SKenneth D. Merry PASS_FLAG_UNMAPPED_CAPABLE = 0x40, 74a9934668SKenneth D. Merry PASS_FLAG_ABANDONED_REF_SET = 0x80 7576babe50SJustin T. Gibbs } pass_flags; 7676babe50SJustin T. Gibbs 7776babe50SJustin T. Gibbs typedef enum { 7876babe50SJustin T. Gibbs PASS_STATE_NORMAL 7976babe50SJustin T. Gibbs } pass_state; 8076babe50SJustin T. Gibbs 8176babe50SJustin T. Gibbs typedef enum { 82a9934668SKenneth D. Merry PASS_CCB_BUFFER_IO, 83a9934668SKenneth D. Merry PASS_CCB_QUEUED_IO 8476babe50SJustin T. Gibbs } pass_ccb_types; 8576babe50SJustin T. Gibbs 8676babe50SJustin T. Gibbs #define ccb_type ppriv_field0 87a9934668SKenneth D. Merry #define ccb_ioreq ppriv_ptr1 88a9934668SKenneth D. Merry 89a9934668SKenneth D. Merry /* 90a9934668SKenneth D. Merry * The maximum number of memory segments we preallocate. 91a9934668SKenneth D. Merry */ 92a9934668SKenneth D. Merry #define PASS_MAX_SEGS 16 93a9934668SKenneth D. Merry 94a9934668SKenneth D. Merry typedef enum { 95a9934668SKenneth D. Merry PASS_IO_NONE = 0x00, 96a9934668SKenneth D. Merry PASS_IO_USER_SEG_MALLOC = 0x01, 97a9934668SKenneth D. Merry PASS_IO_KERN_SEG_MALLOC = 0x02, 98a9934668SKenneth D. Merry PASS_IO_ABANDONED = 0x04 99a9934668SKenneth D. Merry } pass_io_flags; 100a9934668SKenneth D. Merry 101a9934668SKenneth D. Merry struct pass_io_req { 102a9934668SKenneth D. Merry union ccb ccb; 103a9934668SKenneth D. Merry union ccb *alloced_ccb; 104a9934668SKenneth D. Merry union ccb *user_ccb_ptr; 105a9934668SKenneth D. Merry camq_entry user_periph_links; 106a9934668SKenneth D. Merry ccb_ppriv_area user_periph_priv; 107a9934668SKenneth D. Merry struct cam_periph_map_info mapinfo; 108a9934668SKenneth D. Merry pass_io_flags flags; 109a9934668SKenneth D. Merry ccb_flags data_flags; 110a9934668SKenneth D. Merry int num_user_segs; 111a9934668SKenneth D. Merry bus_dma_segment_t user_segs[PASS_MAX_SEGS]; 112a9934668SKenneth D. Merry int num_kern_segs; 113a9934668SKenneth D. Merry bus_dma_segment_t kern_segs[PASS_MAX_SEGS]; 114a9934668SKenneth D. Merry bus_dma_segment_t *user_segptr; 115a9934668SKenneth D. Merry bus_dma_segment_t *kern_segptr; 116a9934668SKenneth D. Merry int num_bufs; 117a9934668SKenneth D. Merry uint32_t dirs[CAM_PERIPH_MAXMAPS]; 118a9934668SKenneth D. Merry uint32_t lengths[CAM_PERIPH_MAXMAPS]; 119a9934668SKenneth D. Merry uint8_t *user_bufs[CAM_PERIPH_MAXMAPS]; 120a9934668SKenneth D. Merry uint8_t *kern_bufs[CAM_PERIPH_MAXMAPS]; 121a9934668SKenneth D. Merry struct bintime start_time; 122a9934668SKenneth D. Merry TAILQ_ENTRY(pass_io_req) links; 123a9934668SKenneth D. Merry }; 12476babe50SJustin T. Gibbs 12576babe50SJustin T. Gibbs struct pass_softc { 12676babe50SJustin T. Gibbs pass_state state; 12776babe50SJustin T. Gibbs pass_flags flags; 12876babe50SJustin T. Gibbs u_int8_t pd_type; 12976babe50SJustin T. Gibbs union ccb saved_ccb; 13086d45c7fSKenneth D. Merry int open_count; 131de239312SAlexander Motin u_int maxio; 132a9d2245eSPoul-Henning Kamp struct devstat *device_stats; 13389c9c53dSPoul-Henning Kamp struct cdev *dev; 134416494d7SJustin T. Gibbs struct cdev *alias_dev; 135416494d7SJustin T. Gibbs struct task add_physpath_task; 136a9934668SKenneth D. Merry struct task shutdown_kqueue_task; 137a9934668SKenneth D. Merry struct selinfo read_select; 138a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) incoming_queue; 139a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) active_queue; 140a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) abandoned_queue; 141a9934668SKenneth D. Merry TAILQ_HEAD(, pass_io_req) done_queue; 142a9934668SKenneth D. Merry struct cam_periph *periph; 143a9934668SKenneth D. Merry char zone_name[12]; 144a9934668SKenneth D. Merry char io_zone_name[12]; 145a9934668SKenneth D. Merry uma_zone_t pass_zone; 146a9934668SKenneth D. Merry uma_zone_t pass_io_zone; 147a9934668SKenneth D. Merry size_t io_zone_size; 14876babe50SJustin T. Gibbs }; 14976babe50SJustin T. Gibbs 15076babe50SJustin T. Gibbs static d_open_t passopen; 15176babe50SJustin T. Gibbs static d_close_t passclose; 15276babe50SJustin T. Gibbs static d_ioctl_t passioctl; 15325a2902cSScott Long static d_ioctl_t passdoioctl; 154a9934668SKenneth D. Merry static d_poll_t passpoll; 155a9934668SKenneth D. Merry static d_kqfilter_t passkqfilter; 156a9934668SKenneth D. Merry static void passreadfiltdetach(struct knote *kn); 157a9934668SKenneth D. Merry static int passreadfilt(struct knote *kn, long hint); 15876babe50SJustin T. Gibbs 15976babe50SJustin T. Gibbs static periph_init_t passinit; 16076babe50SJustin T. Gibbs static periph_ctor_t passregister; 161ee9c90c7SKenneth D. Merry static periph_oninv_t passoninvalidate; 16276babe50SJustin T. Gibbs static periph_dtor_t passcleanup; 163a9934668SKenneth D. Merry static periph_start_t passstart; 164a9934668SKenneth D. Merry static void pass_shutdown_kqueue(void *context, int pending); 165416494d7SJustin T. Gibbs static void pass_add_physpath(void *context, int pending); 16676babe50SJustin T. Gibbs static void passasync(void *callback_arg, u_int32_t code, 16776babe50SJustin T. Gibbs struct cam_path *path, void *arg); 168a9934668SKenneth D. Merry static void passdone(struct cam_periph *periph, 169a9934668SKenneth D. Merry union ccb *done_ccb); 170a9934668SKenneth D. Merry static int passcreatezone(struct cam_periph *periph); 171a9934668SKenneth D. Merry static void passiocleanup(struct pass_softc *softc, 172a9934668SKenneth D. Merry struct pass_io_req *io_req); 173a9934668SKenneth D. Merry static int passcopysglist(struct cam_periph *periph, 174a9934668SKenneth D. Merry struct pass_io_req *io_req, 175a9934668SKenneth D. Merry ccb_flags direction); 176a9934668SKenneth D. Merry static int passmemsetup(struct cam_periph *periph, 177a9934668SKenneth D. Merry struct pass_io_req *io_req); 178a9934668SKenneth D. Merry static int passmemdone(struct cam_periph *periph, 179a9934668SKenneth D. Merry struct pass_io_req *io_req); 18076babe50SJustin T. Gibbs static int passerror(union ccb *ccb, u_int32_t cam_flags, 18176babe50SJustin T. Gibbs u_int32_t sense_flags); 18276babe50SJustin T. Gibbs static int passsendccb(struct cam_periph *periph, union ccb *ccb, 18376babe50SJustin T. Gibbs union ccb *inccb); 18476babe50SJustin T. Gibbs 18576babe50SJustin T. Gibbs static struct periph_driver passdriver = 18676babe50SJustin T. Gibbs { 18776babe50SJustin T. Gibbs passinit, "pass", 18876babe50SJustin T. Gibbs TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 18976babe50SJustin T. Gibbs }; 19076babe50SJustin T. Gibbs 1910b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver); 19276babe50SJustin T. Gibbs 1934e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = { 194dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 195c552ebe1SKenneth D. Merry .d_flags = D_TRACKCLOSE, 1967ac40f5fSPoul-Henning Kamp .d_open = passopen, 1977ac40f5fSPoul-Henning Kamp .d_close = passclose, 1987ac40f5fSPoul-Henning Kamp .d_ioctl = passioctl, 199a9934668SKenneth D. Merry .d_poll = passpoll, 200a9934668SKenneth D. Merry .d_kqfilter = passkqfilter, 2017ac40f5fSPoul-Henning Kamp .d_name = "pass", 20276babe50SJustin T. Gibbs }; 20376babe50SJustin T. Gibbs 204a9934668SKenneth D. Merry static struct filterops passread_filtops = { 205a9934668SKenneth D. Merry .f_isfd = 1, 206a9934668SKenneth D. Merry .f_detach = passreadfiltdetach, 207a9934668SKenneth D. Merry .f_event = passreadfilt 208a9934668SKenneth D. Merry }; 209a9934668SKenneth D. Merry 210a9934668SKenneth D. Merry static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers"); 211a9934668SKenneth D. Merry 21276babe50SJustin T. Gibbs static void 21376babe50SJustin T. Gibbs passinit(void) 21476babe50SJustin T. Gibbs { 21576babe50SJustin T. Gibbs cam_status status; 21676babe50SJustin T. Gibbs 21776babe50SJustin T. Gibbs /* 21876babe50SJustin T. Gibbs * Install a global async callback. This callback will 21976babe50SJustin T. Gibbs * receive async callbacks like "new device found". 22076babe50SJustin T. Gibbs */ 22185d92640SScott Long status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL); 22276babe50SJustin T. Gibbs 22376babe50SJustin T. Gibbs if (status != CAM_REQ_CMP) { 22476babe50SJustin T. Gibbs printf("pass: Failed to attach master async callback " 22576babe50SJustin T. Gibbs "due to status 0x%x!\n", status); 22676babe50SJustin T. Gibbs } 22776babe50SJustin T. Gibbs 22876babe50SJustin T. Gibbs } 22976babe50SJustin T. Gibbs 23076babe50SJustin T. Gibbs static void 231a9934668SKenneth D. Merry passrejectios(struct cam_periph *periph) 232a9934668SKenneth D. Merry { 233a9934668SKenneth D. Merry struct pass_io_req *io_req, *io_req2; 234a9934668SKenneth D. Merry struct pass_softc *softc; 235a9934668SKenneth D. Merry 236a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 237a9934668SKenneth D. Merry 238a9934668SKenneth D. Merry /* 239a9934668SKenneth D. Merry * The user can no longer get status for I/O on the done queue, so 240a9934668SKenneth D. Merry * clean up all outstanding I/O on the done queue. 241a9934668SKenneth D. Merry */ 242a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 243a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 244a9934668SKenneth D. Merry passiocleanup(softc, io_req); 245a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 246a9934668SKenneth D. Merry } 247a9934668SKenneth D. Merry 248a9934668SKenneth D. Merry /* 249a9934668SKenneth D. Merry * The underlying device is gone, so we can't issue these I/Os. 250a9934668SKenneth D. Merry * The devfs node has been shut down, so we can't return status to 251a9934668SKenneth D. Merry * the user. Free any I/O left on the incoming queue. 252a9934668SKenneth D. Merry */ 253a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) { 254a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 255a9934668SKenneth D. Merry passiocleanup(softc, io_req); 256a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 257a9934668SKenneth D. Merry } 258a9934668SKenneth D. Merry 259a9934668SKenneth D. Merry /* 260a9934668SKenneth D. Merry * Normally we would put I/Os on the abandoned queue and acquire a 261a9934668SKenneth D. Merry * reference when we saw the final close. But, the device went 262a9934668SKenneth D. Merry * away and devfs may have moved everything off to deadfs by the 263a9934668SKenneth D. Merry * time the I/O done callback is called; as a result, we won't see 264a9934668SKenneth D. Merry * any more closes. So, if we have any active I/Os, we need to put 265a9934668SKenneth D. Merry * them on the abandoned queue. When the abandoned queue is empty, 266a9934668SKenneth D. Merry * we'll release the remaining reference (see below) to the peripheral. 267a9934668SKenneth D. Merry */ 268a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) { 269a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 270a9934668SKenneth D. Merry io_req->flags |= PASS_IO_ABANDONED; 271a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links); 272a9934668SKenneth D. Merry } 273a9934668SKenneth D. Merry 274a9934668SKenneth D. Merry /* 275a9934668SKenneth D. Merry * If we put any I/O on the abandoned queue, acquire a reference. 276a9934668SKenneth D. Merry */ 277a9934668SKenneth D. Merry if ((!TAILQ_EMPTY(&softc->abandoned_queue)) 278a9934668SKenneth D. Merry && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) { 279a9934668SKenneth D. Merry cam_periph_doacquire(periph); 280a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 281a9934668SKenneth D. Merry } 282a9934668SKenneth D. Merry } 283a9934668SKenneth D. Merry 284a9934668SKenneth D. Merry static void 285ea37f519SKenneth D. Merry passdevgonecb(void *arg) 286ea37f519SKenneth D. Merry { 287ea37f519SKenneth D. Merry struct cam_periph *periph; 288227d67aaSAlexander Motin struct mtx *mtx; 28986d45c7fSKenneth D. Merry struct pass_softc *softc; 29086d45c7fSKenneth D. Merry int i; 291ea37f519SKenneth D. Merry 292ea37f519SKenneth D. Merry periph = (struct cam_periph *)arg; 293227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 294227d67aaSAlexander Motin mtx_lock(mtx); 295ea37f519SKenneth D. Merry 296227d67aaSAlexander Motin softc = (struct pass_softc *)periph->softc; 29786d45c7fSKenneth D. Merry KASSERT(softc->open_count >= 0, ("Negative open count %d", 29886d45c7fSKenneth D. Merry softc->open_count)); 29986d45c7fSKenneth D. Merry 30086d45c7fSKenneth D. Merry /* 30186d45c7fSKenneth D. Merry * When we get this callback, we will get no more close calls from 30286d45c7fSKenneth D. Merry * devfs. So if we have any dangling opens, we need to release the 30386d45c7fSKenneth D. Merry * reference held for that particular context. 30486d45c7fSKenneth D. Merry */ 30586d45c7fSKenneth D. Merry for (i = 0; i < softc->open_count; i++) 30686d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 30786d45c7fSKenneth D. Merry 30886d45c7fSKenneth D. Merry softc->open_count = 0; 30986d45c7fSKenneth D. Merry 31086d45c7fSKenneth D. Merry /* 31186d45c7fSKenneth D. Merry * Release the reference held for the device node, it is gone now. 312a9934668SKenneth D. Merry * Accordingly, inform all queued I/Os of their fate. 31386d45c7fSKenneth D. Merry */ 31486d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 315a9934668SKenneth D. Merry passrejectios(periph); 31686d45c7fSKenneth D. Merry 31786d45c7fSKenneth D. Merry /* 318a9934668SKenneth D. Merry * We reference the SIM lock directly here, instead of using 31986d45c7fSKenneth D. Merry * cam_periph_unlock(). The reason is that the final call to 32086d45c7fSKenneth D. Merry * cam_periph_release_locked() above could result in the periph 32186d45c7fSKenneth D. Merry * getting freed. If that is the case, dereferencing the periph 32286d45c7fSKenneth D. Merry * with a cam_periph_unlock() call would cause a page fault. 32386d45c7fSKenneth D. Merry */ 324227d67aaSAlexander Motin mtx_unlock(mtx); 325a9934668SKenneth D. Merry 326a9934668SKenneth D. Merry /* 327a9934668SKenneth D. Merry * We have to remove our kqueue context from a thread because it 328a9934668SKenneth D. Merry * may sleep. It would be nice if we could get a callback from 329a9934668SKenneth D. Merry * kqueue when it is done cleaning up resources. 330a9934668SKenneth D. Merry */ 331a9934668SKenneth D. Merry taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task); 332ea37f519SKenneth D. Merry } 333ea37f519SKenneth D. Merry 334ea37f519SKenneth D. Merry static void 335ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph) 336ee9c90c7SKenneth D. Merry { 337ee9c90c7SKenneth D. Merry struct pass_softc *softc; 338ee9c90c7SKenneth D. Merry 339ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 340ee9c90c7SKenneth D. Merry 341ee9c90c7SKenneth D. Merry /* 342ee9c90c7SKenneth D. Merry * De-register any async callbacks. 343ee9c90c7SKenneth D. Merry */ 34485d92640SScott Long xpt_register_async(0, passasync, periph, periph->path); 345ee9c90c7SKenneth D. Merry 346ee9c90c7SKenneth D. Merry softc->flags |= PASS_FLAG_INVALID; 347ee9c90c7SKenneth D. Merry 348ee9c90c7SKenneth D. Merry /* 349ea37f519SKenneth D. Merry * Tell devfs this device has gone away, and ask for a callback 350ea37f519SKenneth D. Merry * when it has cleaned up its state. 351ea37f519SKenneth D. Merry */ 352ea37f519SKenneth D. Merry destroy_dev_sched_cb(softc->dev, passdevgonecb, periph); 353ee9c90c7SKenneth D. Merry } 354ee9c90c7SKenneth D. Merry 355ee9c90c7SKenneth D. Merry static void 35676babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph) 35776babe50SJustin T. Gibbs { 358ee9c90c7SKenneth D. Merry struct pass_softc *softc; 359ee9c90c7SKenneth D. Merry 360ee9c90c7SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 361ee9c90c7SKenneth D. Merry 362a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 363a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->active_queue), 364a9934668SKenneth D. Merry ("%s called when there are commands on the active queue!\n", 365a9934668SKenneth D. Merry __func__)); 366a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->abandoned_queue), 367a9934668SKenneth D. Merry ("%s called when there are commands on the abandoned queue!\n", 368a9934668SKenneth D. Merry __func__)); 369a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->incoming_queue), 370a9934668SKenneth D. Merry ("%s called when there are commands on the incoming queue!\n", 371a9934668SKenneth D. Merry __func__)); 372a9934668SKenneth D. Merry KASSERT(TAILQ_EMPTY(&softc->done_queue), 373a9934668SKenneth D. Merry ("%s called when there are commands on the done queue!\n", 374a9934668SKenneth D. Merry __func__)); 375a9934668SKenneth D. Merry 3765f3fed85SEdward Tomasz Napierala devstat_remove_entry(softc->device_stats); 377416494d7SJustin T. Gibbs 3785f3fed85SEdward Tomasz Napierala cam_periph_unlock(periph); 379a9934668SKenneth D. Merry 380a9934668SKenneth D. Merry /* 381a9934668SKenneth D. Merry * We call taskqueue_drain() for the physpath task to make sure it 382a9934668SKenneth D. Merry * is complete. We drop the lock because this can potentially 383a9934668SKenneth D. Merry * sleep. XXX KDM that is bad. Need a way to get a callback when 384a9934668SKenneth D. Merry * a taskqueue is drained. 385a9934668SKenneth D. Merry * 386a9934668SKenneth D. Merry * Note that we don't drain the kqueue shutdown task queue. This 387a9934668SKenneth D. Merry * is because we hold a reference on the periph for kqueue, and 388a9934668SKenneth D. Merry * release that reference from the kqueue shutdown task queue. So 389a9934668SKenneth D. Merry * we cannot come into this routine unless we've released that 390a9934668SKenneth D. Merry * reference. Also, because that could be the last reference, we 391a9934668SKenneth D. Merry * could be called from the cam_periph_release() call in 392a9934668SKenneth D. Merry * pass_shutdown_kqueue(). In that case, the taskqueue_drain() 393a9934668SKenneth D. Merry * would deadlock. It would be preferable if we had a way to 394a9934668SKenneth D. Merry * get a callback when a taskqueue is done. 395a9934668SKenneth D. Merry */ 396416494d7SJustin T. Gibbs taskqueue_drain(taskqueue_thread, &softc->add_physpath_task); 397416494d7SJustin T. Gibbs 3985f3fed85SEdward Tomasz Napierala cam_periph_lock(periph); 399416494d7SJustin T. Gibbs 400ee9c90c7SKenneth D. Merry free(softc, M_DEVBUF); 40176babe50SJustin T. Gibbs } 40276babe50SJustin T. Gibbs 40376babe50SJustin T. Gibbs static void 404a9934668SKenneth D. Merry pass_shutdown_kqueue(void *context, int pending) 405a9934668SKenneth D. Merry { 406a9934668SKenneth D. Merry struct cam_periph *periph; 407a9934668SKenneth D. Merry struct pass_softc *softc; 408a9934668SKenneth D. Merry 409a9934668SKenneth D. Merry periph = context; 410a9934668SKenneth D. Merry softc = periph->softc; 411a9934668SKenneth D. Merry 412a9934668SKenneth D. Merry knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0); 413a9934668SKenneth D. Merry knlist_destroy(&softc->read_select.si_note); 414a9934668SKenneth D. Merry 415a9934668SKenneth D. Merry /* 416a9934668SKenneth D. Merry * Release the reference we held for kqueue. 417a9934668SKenneth D. Merry */ 418a9934668SKenneth D. Merry cam_periph_release(periph); 419a9934668SKenneth D. Merry } 420a9934668SKenneth D. Merry 421a9934668SKenneth D. Merry static void 422416494d7SJustin T. Gibbs pass_add_physpath(void *context, int pending) 423416494d7SJustin T. Gibbs { 424416494d7SJustin T. Gibbs struct cam_periph *periph; 425416494d7SJustin T. Gibbs struct pass_softc *softc; 426a9934668SKenneth D. Merry struct mtx *mtx; 427416494d7SJustin T. Gibbs char *physpath; 428416494d7SJustin T. Gibbs 429416494d7SJustin T. Gibbs /* 430416494d7SJustin T. Gibbs * If we have one, create a devfs alias for our 431416494d7SJustin T. Gibbs * physical path. 432416494d7SJustin T. Gibbs */ 433416494d7SJustin T. Gibbs periph = context; 434416494d7SJustin T. Gibbs softc = periph->softc; 4356884b662SAlexander Motin physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK); 436a9934668SKenneth D. Merry mtx = cam_periph_mtx(periph); 437a9934668SKenneth D. Merry mtx_lock(mtx); 438a9934668SKenneth D. Merry 439a9934668SKenneth D. Merry if (periph->flags & CAM_PERIPH_INVALID) 4406884b662SAlexander Motin goto out; 441a9934668SKenneth D. Merry 442416494d7SJustin T. Gibbs if (xpt_getattr(physpath, MAXPATHLEN, 443416494d7SJustin T. Gibbs "GEOM::physpath", periph->path) == 0 444416494d7SJustin T. Gibbs && strlen(physpath) != 0) { 445416494d7SJustin T. Gibbs 446a9934668SKenneth D. Merry mtx_unlock(mtx); 447416494d7SJustin T. Gibbs make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev, 448416494d7SJustin T. Gibbs softc->dev, softc->alias_dev, physpath); 449a9934668SKenneth D. Merry mtx_lock(mtx); 450416494d7SJustin T. Gibbs } 451ea37f519SKenneth D. Merry 452a9934668SKenneth D. Merry out: 453ea37f519SKenneth D. Merry /* 454ea37f519SKenneth D. Merry * Now that we've made our alias, we no longer have to have a 455ea37f519SKenneth D. Merry * reference to the device. 456ea37f519SKenneth D. Merry */ 457a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0) 458ea37f519SKenneth D. Merry softc->flags |= PASS_FLAG_INITIAL_PHYSPATH; 4596884b662SAlexander Motin 460a9934668SKenneth D. Merry /* 461a9934668SKenneth D. Merry * We always acquire a reference to the periph before queueing this 462a9934668SKenneth D. Merry * task queue function, so it won't go away before we run. 463a9934668SKenneth D. Merry */ 464a9934668SKenneth D. Merry while (pending-- > 0) 465a9934668SKenneth D. Merry cam_periph_release_locked(periph); 466a9934668SKenneth D. Merry mtx_unlock(mtx); 467a9934668SKenneth D. Merry 4686884b662SAlexander Motin free(physpath, M_DEVBUF); 469416494d7SJustin T. Gibbs } 470416494d7SJustin T. Gibbs 471416494d7SJustin T. Gibbs static void 47276babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code, 47376babe50SJustin T. Gibbs struct cam_path *path, void *arg) 47476babe50SJustin T. Gibbs { 47576babe50SJustin T. Gibbs struct cam_periph *periph; 47676babe50SJustin T. Gibbs 47776babe50SJustin T. Gibbs periph = (struct cam_periph *)callback_arg; 47876babe50SJustin T. Gibbs 47976babe50SJustin T. Gibbs switch (code) { 48076babe50SJustin T. Gibbs case AC_FOUND_DEVICE: 48176babe50SJustin T. Gibbs { 48276babe50SJustin T. Gibbs struct ccb_getdev *cgd; 48376babe50SJustin T. Gibbs cam_status status; 48476babe50SJustin T. Gibbs 48576babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 486c5ff3b2fSMatt Jacob if (cgd == NULL) 487c5ff3b2fSMatt Jacob break; 48876babe50SJustin T. Gibbs 48976babe50SJustin T. Gibbs /* 49076babe50SJustin T. Gibbs * Allocate a peripheral instance for 49176babe50SJustin T. Gibbs * this device and start the probe 49276babe50SJustin T. Gibbs * process. 49376babe50SJustin T. Gibbs */ 494ee9c90c7SKenneth D. Merry status = cam_periph_alloc(passregister, passoninvalidate, 495a9934668SKenneth D. Merry passcleanup, passstart, "pass", 496227d67aaSAlexander Motin CAM_PERIPH_BIO, path, 497ee9c90c7SKenneth D. Merry passasync, AC_FOUND_DEVICE, cgd); 49876babe50SJustin T. Gibbs 49976babe50SJustin T. Gibbs if (status != CAM_REQ_CMP 5003393f8daSKenneth D. Merry && status != CAM_REQ_INPROG) { 5013393f8daSKenneth D. Merry const struct cam_status_entry *entry; 5023393f8daSKenneth D. Merry 5033393f8daSKenneth D. Merry entry = cam_fetch_status_entry(status); 5043393f8daSKenneth D. Merry 50576babe50SJustin T. Gibbs printf("passasync: Unable to attach new device " 5063393f8daSKenneth D. Merry "due to status %#x: %s\n", status, entry ? 5073393f8daSKenneth D. Merry entry->status_text : "Unknown"); 5083393f8daSKenneth D. Merry } 50976babe50SJustin T. Gibbs 51076babe50SJustin T. Gibbs break; 51176babe50SJustin T. Gibbs } 512416494d7SJustin T. Gibbs case AC_ADVINFO_CHANGED: 513416494d7SJustin T. Gibbs { 514416494d7SJustin T. Gibbs uintptr_t buftype; 515416494d7SJustin T. Gibbs 516416494d7SJustin T. Gibbs buftype = (uintptr_t)arg; 517416494d7SJustin T. Gibbs if (buftype == CDAI_TYPE_PHYS_PATH) { 518416494d7SJustin T. Gibbs struct pass_softc *softc; 519a9934668SKenneth D. Merry cam_status status; 520416494d7SJustin T. Gibbs 521416494d7SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 522a9934668SKenneth D. Merry /* 523a9934668SKenneth D. Merry * Acquire a reference to the periph before we 524a9934668SKenneth D. Merry * start the taskqueue, so that we don't run into 525a9934668SKenneth D. Merry * a situation where the periph goes away before 526a9934668SKenneth D. Merry * the task queue has a chance to run. 527a9934668SKenneth D. Merry */ 528a9934668SKenneth D. Merry status = cam_periph_acquire(periph); 529a9934668SKenneth D. Merry if (status != CAM_REQ_CMP) 530a9934668SKenneth D. Merry break; 531a9934668SKenneth D. Merry 532416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, 533416494d7SJustin T. Gibbs &softc->add_physpath_task); 534416494d7SJustin T. Gibbs } 535416494d7SJustin T. Gibbs break; 536416494d7SJustin T. Gibbs } 53776babe50SJustin T. Gibbs default: 538516871c6SJustin T. Gibbs cam_periph_async(periph, code, path, arg); 53976babe50SJustin T. Gibbs break; 54076babe50SJustin T. Gibbs } 54176babe50SJustin T. Gibbs } 54276babe50SJustin T. Gibbs 54376babe50SJustin T. Gibbs static cam_status 54476babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg) 54576babe50SJustin T. Gibbs { 54676babe50SJustin T. Gibbs struct pass_softc *softc; 54776babe50SJustin T. Gibbs struct ccb_getdev *cgd; 548b8b6b5d3SAlexander Motin struct ccb_pathinq cpi; 549ee198893SKonstantin Belousov struct make_dev_args args; 550ee198893SKonstantin Belousov int error, no_tags; 55176babe50SJustin T. Gibbs 55276babe50SJustin T. Gibbs cgd = (struct ccb_getdev *)arg; 55376babe50SJustin T. Gibbs if (cgd == NULL) { 554ea37f519SKenneth D. Merry printf("%s: no getdev CCB, can't register device\n", __func__); 55576babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 55676babe50SJustin T. Gibbs } 55776babe50SJustin T. Gibbs 55876babe50SJustin T. Gibbs softc = (struct pass_softc *)malloc(sizeof(*softc), 55976babe50SJustin T. Gibbs M_DEVBUF, M_NOWAIT); 56076babe50SJustin T. Gibbs 56176babe50SJustin T. Gibbs if (softc == NULL) { 562ea37f519SKenneth D. Merry printf("%s: Unable to probe new device. " 563ea37f519SKenneth D. Merry "Unable to allocate softc\n", __func__); 56476babe50SJustin T. Gibbs return(CAM_REQ_CMP_ERR); 56576babe50SJustin T. Gibbs } 56676babe50SJustin T. Gibbs 56776babe50SJustin T. Gibbs bzero(softc, sizeof(*softc)); 56876babe50SJustin T. Gibbs softc->state = PASS_STATE_NORMAL; 569b8b6b5d3SAlexander Motin if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI) 57010b6172aSMatt Jacob softc->pd_type = SID_TYPE(&cgd->inq_data); 571b8b6b5d3SAlexander Motin else if (cgd->protocol == PROTO_SATAPM) 572b8b6b5d3SAlexander Motin softc->pd_type = T_ENCLOSURE; 573b8b6b5d3SAlexander Motin else 574b8b6b5d3SAlexander Motin softc->pd_type = T_DIRECT; 57576babe50SJustin T. Gibbs 57676babe50SJustin T. Gibbs periph->softc = softc; 577a9934668SKenneth D. Merry softc->periph = periph; 578a9934668SKenneth D. Merry TAILQ_INIT(&softc->incoming_queue); 579a9934668SKenneth D. Merry TAILQ_INIT(&softc->active_queue); 580a9934668SKenneth D. Merry TAILQ_INIT(&softc->abandoned_queue); 581a9934668SKenneth D. Merry TAILQ_INIT(&softc->done_queue); 582a9934668SKenneth D. Merry snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d", 583a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 584a9934668SKenneth D. Merry snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO", 585a9934668SKenneth D. Merry periph->periph_name, periph->unit_number); 586a9934668SKenneth D. Merry softc->io_zone_size = MAXPHYS; 587a9934668SKenneth D. Merry knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph)); 5883393f8daSKenneth D. Merry 589b8b6b5d3SAlexander Motin bzero(&cpi, sizeof(cpi)); 590b8b6b5d3SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 591b8b6b5d3SAlexander Motin cpi.ccb_h.func_code = XPT_PATH_INQ; 592b8b6b5d3SAlexander Motin xpt_action((union ccb *)&cpi); 593b8b6b5d3SAlexander Motin 594de239312SAlexander Motin if (cpi.maxio == 0) 595de239312SAlexander Motin softc->maxio = DFLTPHYS; /* traditional default */ 596de239312SAlexander Motin else if (cpi.maxio > MAXPHYS) 597de239312SAlexander Motin softc->maxio = MAXPHYS; /* for safety */ 598de239312SAlexander Motin else 599de239312SAlexander Motin softc->maxio = cpi.maxio; /* real value */ 600de239312SAlexander Motin 601a9934668SKenneth D. Merry if (cpi.hba_misc & PIM_UNMAPPED) 602a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE; 603a9934668SKenneth D. Merry 60476babe50SJustin T. Gibbs /* 60576babe50SJustin T. Gibbs * We pass in 0 for a blocksize, since we don't 60676babe50SJustin T. Gibbs * know what the blocksize of this device is, if 60776babe50SJustin T. Gibbs * it even has a blocksize. 60876babe50SJustin T. Gibbs */ 609edec59d9SAlexander Motin cam_periph_unlock(periph); 6103393f8daSKenneth D. Merry no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 611c81d2c74SMatt Jacob softc->device_stats = devstat_new_entry("pass", 612d3ce8327SEd Schouten periph->unit_number, 0, 6133393f8daSKenneth D. Merry DEVSTAT_NO_BLOCKSIZE 6143393f8daSKenneth D. Merry | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 61510b6172aSMatt Jacob softc->pd_type | 616b8b6b5d3SAlexander Motin XPORT_DEVSTAT_TYPE(cpi.transport) | 6172a888f93SKenneth D. Merry DEVSTAT_TYPE_PASS, 6182a888f93SKenneth D. Merry DEVSTAT_PRIORITY_PASS); 61973d26919SKenneth D. Merry 620ea37f519SKenneth D. Merry /* 621a9934668SKenneth D. Merry * Initialize the taskqueue handler for shutting down kqueue. 622a9934668SKenneth D. Merry */ 623a9934668SKenneth D. Merry TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0, 624a9934668SKenneth D. Merry pass_shutdown_kqueue, periph); 625a9934668SKenneth D. Merry 626a9934668SKenneth D. Merry /* 627a9934668SKenneth D. Merry * Acquire a reference to the periph that we can release once we've 628a9934668SKenneth D. Merry * cleaned up the kqueue. 629a9934668SKenneth D. Merry */ 630a9934668SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 631a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 632a9934668SKenneth D. Merry "registration!\n", __func__); 633a9934668SKenneth D. Merry cam_periph_lock(periph); 634a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 635a9934668SKenneth D. Merry } 636a9934668SKenneth D. Merry 637a9934668SKenneth D. Merry /* 638ea37f519SKenneth D. Merry * Acquire a reference to the periph before we create the devfs 639ea37f519SKenneth D. Merry * instance for it. We'll release this reference once the devfs 640ea37f519SKenneth D. Merry * instance has been freed. 641ea37f519SKenneth D. Merry */ 642ea37f519SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 643ea37f519SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 644ea37f519SKenneth D. Merry "registration!\n", __func__); 64586d45c7fSKenneth D. Merry cam_periph_lock(periph); 646ea37f519SKenneth D. Merry return (CAM_REQ_CMP_ERR); 647ea37f519SKenneth D. Merry } 648ea37f519SKenneth D. Merry 64973d26919SKenneth D. Merry /* Register the device */ 650ee198893SKonstantin Belousov make_dev_args_init(&args); 651ee198893SKonstantin Belousov args.mda_devsw = &pass_cdevsw; 652ee198893SKonstantin Belousov args.mda_unit = periph->unit_number; 653ee198893SKonstantin Belousov args.mda_uid = UID_ROOT; 654ee198893SKonstantin Belousov args.mda_gid = GID_OPERATOR; 655ee198893SKonstantin Belousov args.mda_mode = 0600; 656ee198893SKonstantin Belousov args.mda_si_drv1 = periph; 657ee198893SKonstantin Belousov error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 658ee198893SKonstantin Belousov periph->unit_number); 659ee198893SKonstantin Belousov if (error != 0) { 660ee198893SKonstantin Belousov cam_periph_lock(periph); 661ee198893SKonstantin Belousov cam_periph_release_locked(periph); 662ee198893SKonstantin Belousov return (CAM_REQ_CMP_ERR); 663ee198893SKonstantin Belousov } 664ea37f519SKenneth D. Merry 665ea37f519SKenneth D. Merry /* 666a9934668SKenneth D. Merry * Hold a reference to the periph before we create the physical 667a9934668SKenneth D. Merry * path alias so it can't go away. 668ea37f519SKenneth D. Merry */ 669a9934668SKenneth D. Merry if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 670a9934668SKenneth D. Merry xpt_print(periph->path, "%s: lost periph during " 671a9934668SKenneth D. Merry "registration!\n", __func__); 672a9934668SKenneth D. Merry cam_periph_lock(periph); 673a9934668SKenneth D. Merry return (CAM_REQ_CMP_ERR); 674a9934668SKenneth D. Merry } 675ea37f519SKenneth D. Merry 676edec59d9SAlexander Motin cam_periph_lock(periph); 67773d26919SKenneth D. Merry 678416494d7SJustin T. Gibbs TASK_INIT(&softc->add_physpath_task, /*priority*/0, 679416494d7SJustin T. Gibbs pass_add_physpath, periph); 680416494d7SJustin T. Gibbs 68176babe50SJustin T. Gibbs /* 682416494d7SJustin T. Gibbs * See if physical path information is already available. 68376babe50SJustin T. Gibbs */ 684416494d7SJustin T. Gibbs taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task); 685416494d7SJustin T. Gibbs 686416494d7SJustin T. Gibbs /* 687416494d7SJustin T. Gibbs * Add an async callback so that we get notified if 688416494d7SJustin T. Gibbs * this device goes away or its physical path 689416494d7SJustin T. Gibbs * (stored in the advanced info data of the EDT) has 690416494d7SJustin T. Gibbs * changed. 691416494d7SJustin T. Gibbs */ 692416494d7SJustin T. Gibbs xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 693416494d7SJustin T. Gibbs passasync, periph, periph->path); 69476babe50SJustin T. Gibbs 69576babe50SJustin T. Gibbs if (bootverbose) 69676babe50SJustin T. Gibbs xpt_announce_periph(periph, NULL); 69776babe50SJustin T. Gibbs 69876babe50SJustin T. Gibbs return(CAM_REQ_CMP); 69976babe50SJustin T. Gibbs } 70076babe50SJustin T. Gibbs 70176babe50SJustin T. Gibbs static int 70289c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td) 70376babe50SJustin T. Gibbs { 70476babe50SJustin T. Gibbs struct cam_periph *periph; 70576babe50SJustin T. Gibbs struct pass_softc *softc; 706e2a5fdf9SNate Lawson int error; 70776babe50SJustin T. Gibbs 708e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 7092b83592fSScott Long if (cam_periph_acquire(periph) != CAM_REQ_CMP) 71076babe50SJustin T. Gibbs return (ENXIO); 71176babe50SJustin T. Gibbs 7122b83592fSScott Long cam_periph_lock(periph); 7132b83592fSScott Long 71476babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 71576babe50SJustin T. Gibbs 716ee9c90c7SKenneth D. Merry if (softc->flags & PASS_FLAG_INVALID) { 717c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7182b83592fSScott Long cam_periph_unlock(periph); 71976babe50SJustin T. Gibbs return(ENXIO); 720ee9c90c7SKenneth D. Merry } 72122b9c86cSKenneth D. Merry 72222b9c86cSKenneth D. Merry /* 723f5ef42beSRobert Watson * Don't allow access when we're running at a high securelevel. 72422b9c86cSKenneth D. Merry */ 725a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 1); 726f7312ca2SRobert Watson if (error) { 727c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7282b83592fSScott Long cam_periph_unlock(periph); 729f7312ca2SRobert Watson return(error); 73022b9c86cSKenneth D. Merry } 73176babe50SJustin T. Gibbs 73276babe50SJustin T. Gibbs /* 73366a0780eSKenneth D. Merry * Only allow read-write access. 73466a0780eSKenneth D. Merry */ 73522b9c86cSKenneth D. Merry if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 736c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7372b83592fSScott Long cam_periph_unlock(periph); 73866a0780eSKenneth D. Merry return(EPERM); 73922b9c86cSKenneth D. Merry } 74066a0780eSKenneth D. Merry 74166a0780eSKenneth D. Merry /* 74276babe50SJustin T. Gibbs * We don't allow nonblocking access. 74376babe50SJustin T. Gibbs */ 74476babe50SJustin T. Gibbs if ((flags & O_NONBLOCK) != 0) { 745f0d9af51SMatt Jacob xpt_print(periph->path, "can't do nonblocking access\n"); 746c552ebe1SKenneth D. Merry cam_periph_release_locked(periph); 7472b83592fSScott Long cam_periph_unlock(periph); 74822b9c86cSKenneth D. Merry return(EINVAL); 74976babe50SJustin T. Gibbs } 75076babe50SJustin T. Gibbs 75186d45c7fSKenneth D. Merry softc->open_count++; 75286d45c7fSKenneth D. Merry 753835187bfSScott Long cam_periph_unlock(periph); 75476babe50SJustin T. Gibbs 75576babe50SJustin T. Gibbs return (error); 75676babe50SJustin T. Gibbs } 75776babe50SJustin T. Gibbs 75876babe50SJustin T. Gibbs static int 75989c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td) 76076babe50SJustin T. Gibbs { 76176babe50SJustin T. Gibbs struct cam_periph *periph; 76286d45c7fSKenneth D. Merry struct pass_softc *softc; 763227d67aaSAlexander Motin struct mtx *mtx; 76476babe50SJustin T. Gibbs 765e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 766227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 767227d67aaSAlexander Motin mtx_lock(mtx); 76876babe50SJustin T. Gibbs 76986d45c7fSKenneth D. Merry softc = periph->softc; 77086d45c7fSKenneth D. Merry softc->open_count--; 77186d45c7fSKenneth D. Merry 772a9934668SKenneth D. Merry if (softc->open_count == 0) { 773a9934668SKenneth D. Merry struct pass_io_req *io_req, *io_req2; 774a9934668SKenneth D. Merry 775a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 776a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 777a9934668SKenneth D. Merry passiocleanup(softc, io_req); 778a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 779a9934668SKenneth D. Merry } 780a9934668SKenneth D. Merry 781a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, 782a9934668SKenneth D. Merry io_req2) { 783a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 784a9934668SKenneth D. Merry passiocleanup(softc, io_req); 785a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 786a9934668SKenneth D. Merry } 787a9934668SKenneth D. Merry 788a9934668SKenneth D. Merry /* 789a9934668SKenneth D. Merry * If there are any active I/Os, we need to forcibly acquire a 790a9934668SKenneth D. Merry * reference to the peripheral so that we don't go away 791a9934668SKenneth D. Merry * before they complete. We'll release the reference when 792a9934668SKenneth D. Merry * the abandoned queue is empty. 793a9934668SKenneth D. Merry */ 794a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->active_queue); 795a9934668SKenneth D. Merry if ((io_req != NULL) 796a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) { 797a9934668SKenneth D. Merry cam_periph_doacquire(periph); 798a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 799a9934668SKenneth D. Merry } 800a9934668SKenneth D. Merry 801a9934668SKenneth D. Merry /* 802a9934668SKenneth D. Merry * Since the I/O in the active queue is not under our 803a9934668SKenneth D. Merry * control, just set a flag so that we can clean it up when 804a9934668SKenneth D. Merry * it completes and put it on the abandoned queue. This 805a9934668SKenneth D. Merry * will prevent our sending spurious completions in the 806a9934668SKenneth D. Merry * event that the device is opened again before these I/Os 807a9934668SKenneth D. Merry * complete. 808a9934668SKenneth D. Merry */ 809a9934668SKenneth D. Merry TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, 810a9934668SKenneth D. Merry io_req2) { 811a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 812a9934668SKenneth D. Merry io_req->flags |= PASS_IO_ABANDONED; 813a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, 814a9934668SKenneth D. Merry links); 815a9934668SKenneth D. Merry } 816a9934668SKenneth D. Merry } 817a9934668SKenneth D. Merry 81886d45c7fSKenneth D. Merry cam_periph_release_locked(periph); 81986d45c7fSKenneth D. Merry 82086d45c7fSKenneth D. Merry /* 821227d67aaSAlexander Motin * We reference the lock directly here, instead of using 82286d45c7fSKenneth D. Merry * cam_periph_unlock(). The reason is that the call to 82386d45c7fSKenneth D. Merry * cam_periph_release_locked() above could result in the periph 82486d45c7fSKenneth D. Merry * getting freed. If that is the case, dereferencing the periph 82586d45c7fSKenneth D. Merry * with a cam_periph_unlock() call would cause a page fault. 82686d45c7fSKenneth D. Merry * 82786d45c7fSKenneth D. Merry * cam_periph_release() avoids this problem using the same method, 82886d45c7fSKenneth D. Merry * but we're manually acquiring and dropping the lock here to 82986d45c7fSKenneth D. Merry * protect the open count and avoid another lock acquisition and 83086d45c7fSKenneth D. Merry * release. 83186d45c7fSKenneth D. Merry */ 832227d67aaSAlexander Motin mtx_unlock(mtx); 83376babe50SJustin T. Gibbs 83476babe50SJustin T. Gibbs return (0); 83576babe50SJustin T. Gibbs } 83676babe50SJustin T. Gibbs 837a9934668SKenneth D. Merry 838a9934668SKenneth D. Merry static void 839a9934668SKenneth D. Merry passstart(struct cam_periph *periph, union ccb *start_ccb) 840a9934668SKenneth D. Merry { 841a9934668SKenneth D. Merry struct pass_softc *softc; 842a9934668SKenneth D. Merry 843a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 844a9934668SKenneth D. Merry 845a9934668SKenneth D. Merry switch (softc->state) { 846a9934668SKenneth D. Merry case PASS_STATE_NORMAL: { 847a9934668SKenneth D. Merry struct pass_io_req *io_req; 848a9934668SKenneth D. Merry 849a9934668SKenneth D. Merry /* 850a9934668SKenneth D. Merry * Check for any queued I/O requests that require an 851a9934668SKenneth D. Merry * allocated slot. 852a9934668SKenneth D. Merry */ 853a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->incoming_queue); 854a9934668SKenneth D. Merry if (io_req == NULL) { 855a9934668SKenneth D. Merry xpt_release_ccb(start_ccb); 856a9934668SKenneth D. Merry break; 857a9934668SKenneth D. Merry } 858a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 859a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 860a9934668SKenneth D. Merry /* 861a9934668SKenneth D. Merry * Merge the user's CCB into the allocated CCB. 862a9934668SKenneth D. Merry */ 863a9934668SKenneth D. Merry xpt_merge_ccb(start_ccb, &io_req->ccb); 864a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO; 865a9934668SKenneth D. Merry start_ccb->ccb_h.ccb_ioreq = io_req; 866a9934668SKenneth D. Merry start_ccb->ccb_h.cbfcnp = passdone; 867a9934668SKenneth D. Merry io_req->alloced_ccb = start_ccb; 868a9934668SKenneth D. Merry binuptime(&io_req->start_time); 869a9934668SKenneth D. Merry devstat_start_transaction(softc->device_stats, 870a9934668SKenneth D. Merry &io_req->start_time); 871a9934668SKenneth D. Merry 872a9934668SKenneth D. Merry xpt_action(start_ccb); 873a9934668SKenneth D. Merry 874a9934668SKenneth D. Merry /* 875a9934668SKenneth D. Merry * If we have any more I/O waiting, schedule ourselves again. 876a9934668SKenneth D. Merry */ 877a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->incoming_queue)) 878a9934668SKenneth D. Merry xpt_schedule(periph, CAM_PRIORITY_NORMAL); 879a9934668SKenneth D. Merry break; 880a9934668SKenneth D. Merry } 881a9934668SKenneth D. Merry default: 882a9934668SKenneth D. Merry break; 883a9934668SKenneth D. Merry } 884a9934668SKenneth D. Merry } 885a9934668SKenneth D. Merry 886a9934668SKenneth D. Merry static void 887a9934668SKenneth D. Merry passdone(struct cam_periph *periph, union ccb *done_ccb) 888a9934668SKenneth D. Merry { 889a9934668SKenneth D. Merry struct pass_softc *softc; 890a9934668SKenneth D. Merry struct ccb_scsiio *csio; 891a9934668SKenneth D. Merry 892a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 893a9934668SKenneth D. Merry 894a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 895a9934668SKenneth D. Merry 896a9934668SKenneth D. Merry csio = &done_ccb->csio; 897a9934668SKenneth D. Merry switch (csio->ccb_h.ccb_type) { 898a9934668SKenneth D. Merry case PASS_CCB_QUEUED_IO: { 899a9934668SKenneth D. Merry struct pass_io_req *io_req; 900a9934668SKenneth D. Merry 901a9934668SKenneth D. Merry io_req = done_ccb->ccb_h.ccb_ioreq; 902a9934668SKenneth D. Merry #if 0 903a9934668SKenneth D. Merry xpt_print(periph->path, "%s: called for user CCB %p\n", 904a9934668SKenneth D. Merry __func__, io_req->user_ccb_ptr); 905a9934668SKenneth D. Merry #endif 906a9934668SKenneth D. Merry if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 907a9934668SKenneth D. Merry && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) 908a9934668SKenneth D. Merry && ((io_req->flags & PASS_IO_ABANDONED) == 0)) { 909a9934668SKenneth D. Merry int error; 910a9934668SKenneth D. Merry 911a9934668SKenneth D. Merry error = passerror(done_ccb, CAM_RETRY_SELTO, 912a9934668SKenneth D. Merry SF_RETRY_UA | SF_NO_PRINT); 913a9934668SKenneth D. Merry 914a9934668SKenneth D. Merry if (error == ERESTART) { 915a9934668SKenneth D. Merry /* 916a9934668SKenneth D. Merry * A retry was scheduled, so 917a9934668SKenneth D. Merry * just return. 918a9934668SKenneth D. Merry */ 919a9934668SKenneth D. Merry return; 920a9934668SKenneth D. Merry } 921a9934668SKenneth D. Merry } 922a9934668SKenneth D. Merry 923a9934668SKenneth D. Merry /* 924a9934668SKenneth D. Merry * Copy the allocated CCB contents back to the malloced CCB 925a9934668SKenneth D. Merry * so we can give status back to the user when he requests it. 926a9934668SKenneth D. Merry */ 927a9934668SKenneth D. Merry bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb)); 928a9934668SKenneth D. Merry 929a9934668SKenneth D. Merry /* 930a9934668SKenneth D. Merry * Log data/transaction completion with devstat(9). 931a9934668SKenneth D. Merry */ 932a9934668SKenneth D. Merry switch (done_ccb->ccb_h.func_code) { 933a9934668SKenneth D. Merry case XPT_SCSI_IO: 934a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 935a9934668SKenneth D. Merry done_ccb->csio.dxfer_len - done_ccb->csio.resid, 936a9934668SKenneth D. Merry done_ccb->csio.tag_action & 0x3, 937a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 938a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 939a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 940a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 941a9934668SKenneth D. Merry &io_req->start_time); 942a9934668SKenneth D. Merry break; 943a9934668SKenneth D. Merry case XPT_ATA_IO: 944a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 945a9934668SKenneth D. Merry done_ccb->ataio.dxfer_len - done_ccb->ataio.resid, 946e4cc6558SWarner Losh 0, /* Not used in ATA */ 947a9934668SKenneth D. Merry ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 948a9934668SKenneth D. Merry CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 949a9934668SKenneth D. Merry (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 950a9934668SKenneth D. Merry DEVSTAT_WRITE : DEVSTAT_READ, NULL, 951a9934668SKenneth D. Merry &io_req->start_time); 952a9934668SKenneth D. Merry break; 953a9934668SKenneth D. Merry case XPT_SMP_IO: 954a9934668SKenneth D. Merry /* 955a9934668SKenneth D. Merry * XXX KDM this isn't quite right, but there isn't 956a9934668SKenneth D. Merry * currently an easy way to represent a bidirectional 957a9934668SKenneth D. Merry * transfer in devstat. The only way to do it 958a9934668SKenneth D. Merry * and have the byte counts come out right would 959a9934668SKenneth D. Merry * mean that we would have to record two 960a9934668SKenneth D. Merry * transactions, one for the request and one for the 961a9934668SKenneth D. Merry * response. For now, so that we report something, 962a9934668SKenneth D. Merry * just treat the entire thing as a read. 963a9934668SKenneth D. Merry */ 964a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 965a9934668SKenneth D. Merry done_ccb->smpio.smp_request_len + 966a9934668SKenneth D. Merry done_ccb->smpio.smp_response_len, 967a9934668SKenneth D. Merry DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL, 968a9934668SKenneth D. Merry &io_req->start_time); 969a9934668SKenneth D. Merry break; 970a9934668SKenneth D. Merry default: 971a9934668SKenneth D. Merry devstat_end_transaction(softc->device_stats, 0, 972a9934668SKenneth D. Merry DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL, 973a9934668SKenneth D. Merry &io_req->start_time); 974a9934668SKenneth D. Merry break; 975a9934668SKenneth D. Merry } 976a9934668SKenneth D. Merry 977a9934668SKenneth D. Merry /* 978a9934668SKenneth D. Merry * In the normal case, take the completed I/O off of the 979a9934668SKenneth D. Merry * active queue and put it on the done queue. Notitfy the 980a9934668SKenneth D. Merry * user that we have a completed I/O. 981a9934668SKenneth D. Merry */ 982a9934668SKenneth D. Merry if ((io_req->flags & PASS_IO_ABANDONED) == 0) { 983a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 984a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 985a9934668SKenneth D. Merry selwakeuppri(&softc->read_select, PRIBIO); 986a9934668SKenneth D. Merry KNOTE_LOCKED(&softc->read_select.si_note, 0); 987a9934668SKenneth D. Merry } else { 988a9934668SKenneth D. Merry /* 989a9934668SKenneth D. Merry * In the case of an abandoned I/O (final close 990a9934668SKenneth D. Merry * without fetching the I/O), take it off of the 991a9934668SKenneth D. Merry * abandoned queue and free it. 992a9934668SKenneth D. Merry */ 993a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->abandoned_queue, io_req, links); 994a9934668SKenneth D. Merry passiocleanup(softc, io_req); 995a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 996a9934668SKenneth D. Merry 997a9934668SKenneth D. Merry /* 998a9934668SKenneth D. Merry * Release the done_ccb here, since we may wind up 999a9934668SKenneth D. Merry * freeing the peripheral when we decrement the 1000a9934668SKenneth D. Merry * reference count below. 1001a9934668SKenneth D. Merry */ 1002a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1003a9934668SKenneth D. Merry 1004a9934668SKenneth D. Merry /* 1005a9934668SKenneth D. Merry * If the abandoned queue is empty, we can release 1006a9934668SKenneth D. Merry * our reference to the periph since we won't have 1007a9934668SKenneth D. Merry * any more completions coming. 1008a9934668SKenneth D. Merry */ 1009a9934668SKenneth D. Merry if ((TAILQ_EMPTY(&softc->abandoned_queue)) 1010a9934668SKenneth D. Merry && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) { 1011a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET; 1012a9934668SKenneth D. Merry cam_periph_release_locked(periph); 1013a9934668SKenneth D. Merry } 1014a9934668SKenneth D. Merry 1015a9934668SKenneth D. Merry /* 1016a9934668SKenneth D. Merry * We have already released the CCB, so we can 1017a9934668SKenneth D. Merry * return. 1018a9934668SKenneth D. Merry */ 1019a9934668SKenneth D. Merry return; 1020a9934668SKenneth D. Merry } 1021a9934668SKenneth D. Merry break; 1022a9934668SKenneth D. Merry } 1023a9934668SKenneth D. Merry } 1024a9934668SKenneth D. Merry xpt_release_ccb(done_ccb); 1025a9934668SKenneth D. Merry } 1026a9934668SKenneth D. Merry 1027a9934668SKenneth D. Merry static int 1028a9934668SKenneth D. Merry passcreatezone(struct cam_periph *periph) 1029a9934668SKenneth D. Merry { 1030a9934668SKenneth D. Merry struct pass_softc *softc; 1031a9934668SKenneth D. Merry int error; 1032a9934668SKenneth D. Merry 1033a9934668SKenneth D. Merry error = 0; 1034a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1035a9934668SKenneth D. Merry 1036a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 1037a9934668SKenneth D. Merry KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 1038a9934668SKenneth D. Merry ("%s called when the pass(4) zone is valid!\n", __func__)); 1039a9934668SKenneth D. Merry KASSERT((softc->pass_zone == NULL), 1040a9934668SKenneth D. Merry ("%s called when the pass(4) zone is allocated!\n", __func__)); 1041a9934668SKenneth D. Merry 1042a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) { 1043a9934668SKenneth D. Merry 1044a9934668SKenneth D. Merry /* 1045a9934668SKenneth D. Merry * We're the first context through, so we need to create 1046a9934668SKenneth D. Merry * the pass(4) UMA zone for I/O requests. 1047a9934668SKenneth D. Merry */ 1048a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_INPROG; 1049a9934668SKenneth D. Merry 1050a9934668SKenneth D. Merry /* 1051a9934668SKenneth D. Merry * uma_zcreate() does a blocking (M_WAITOK) allocation, 1052a9934668SKenneth D. Merry * so we cannot hold a mutex while we call it. 1053a9934668SKenneth D. Merry */ 1054a9934668SKenneth D. Merry cam_periph_unlock(periph); 1055a9934668SKenneth D. Merry 1056a9934668SKenneth D. Merry softc->pass_zone = uma_zcreate(softc->zone_name, 1057a9934668SKenneth D. Merry sizeof(struct pass_io_req), NULL, NULL, NULL, NULL, 1058a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1059a9934668SKenneth D. Merry 1060a9934668SKenneth D. Merry softc->pass_io_zone = uma_zcreate(softc->io_zone_name, 1061a9934668SKenneth D. Merry softc->io_zone_size, NULL, NULL, NULL, NULL, 1062a9934668SKenneth D. Merry /*align*/ 0, /*flags*/ 0); 1063a9934668SKenneth D. Merry 1064a9934668SKenneth D. Merry cam_periph_lock(periph); 1065a9934668SKenneth D. Merry 1066a9934668SKenneth D. Merry if ((softc->pass_zone == NULL) 1067a9934668SKenneth D. Merry || (softc->pass_io_zone == NULL)) { 1068a9934668SKenneth D. Merry if (softc->pass_zone == NULL) 1069a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1070a9934668SKenneth D. Merry "IO Req UMA zone\n"); 1071a9934668SKenneth D. Merry else 1072a9934668SKenneth D. Merry xpt_print(periph->path, "unable to allocate " 1073a9934668SKenneth D. Merry "IO UMA zone\n"); 1074a9934668SKenneth D. Merry softc->flags &= ~PASS_FLAG_ZONE_INPROG; 1075a9934668SKenneth D. Merry goto bailout; 1076a9934668SKenneth D. Merry } 1077a9934668SKenneth D. Merry 1078a9934668SKenneth D. Merry /* 1079a9934668SKenneth D. Merry * Set the flags appropriately and notify any other waiters. 1080a9934668SKenneth D. Merry */ 1081a9934668SKenneth D. Merry softc->flags &= PASS_FLAG_ZONE_INPROG; 1082a9934668SKenneth D. Merry softc->flags |= PASS_FLAG_ZONE_VALID; 1083a9934668SKenneth D. Merry wakeup(&softc->pass_zone); 1084a9934668SKenneth D. Merry } else { 1085a9934668SKenneth D. Merry /* 1086a9934668SKenneth D. Merry * In this case, the UMA zone has not yet been created, but 1087a9934668SKenneth D. Merry * another context is in the process of creating it. We 1088a9934668SKenneth D. Merry * need to sleep until the creation is either done or has 1089a9934668SKenneth D. Merry * failed. 1090a9934668SKenneth D. Merry */ 1091a9934668SKenneth D. Merry while ((softc->flags & PASS_FLAG_ZONE_INPROG) 1092a9934668SKenneth D. Merry && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) { 1093a9934668SKenneth D. Merry error = msleep(&softc->pass_zone, 1094a9934668SKenneth D. Merry cam_periph_mtx(periph), PRIBIO, 1095a9934668SKenneth D. Merry "paszon", 0); 1096a9934668SKenneth D. Merry if (error != 0) 1097a9934668SKenneth D. Merry goto bailout; 1098a9934668SKenneth D. Merry } 1099a9934668SKenneth D. Merry /* 1100a9934668SKenneth D. Merry * If the zone creation failed, no luck for the user. 1101a9934668SKenneth D. Merry */ 1102a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){ 1103a9934668SKenneth D. Merry error = ENOMEM; 1104a9934668SKenneth D. Merry goto bailout; 1105a9934668SKenneth D. Merry } 1106a9934668SKenneth D. Merry } 1107a9934668SKenneth D. Merry bailout: 1108a9934668SKenneth D. Merry return (error); 1109a9934668SKenneth D. Merry } 1110a9934668SKenneth D. Merry 1111a9934668SKenneth D. Merry static void 1112a9934668SKenneth D. Merry passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req) 1113a9934668SKenneth D. Merry { 1114a9934668SKenneth D. Merry union ccb *ccb; 1115a9934668SKenneth D. Merry u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1116a9934668SKenneth D. Merry int i, numbufs; 1117a9934668SKenneth D. Merry 1118a9934668SKenneth D. Merry ccb = &io_req->ccb; 1119a9934668SKenneth D. Merry 1120a9934668SKenneth D. Merry switch (ccb->ccb_h.func_code) { 1121a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1122a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1123a9934668SKenneth D. Merry 1124a9934668SKenneth D. Merry if (numbufs == 1) { 1125a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1126a9934668SKenneth D. Merry } else { 1127a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1128a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1129a9934668SKenneth D. Merry } 1130a9934668SKenneth D. Merry break; 1131a9934668SKenneth D. Merry case XPT_SCSI_IO: 1132a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1133a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1134a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1135a9934668SKenneth D. Merry break; 1136a9934668SKenneth D. Merry case XPT_ATA_IO: 1137a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1138a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1139a9934668SKenneth D. Merry break; 1140a9934668SKenneth D. Merry case XPT_SMP_IO: 1141a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 2); 1142a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1143a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1144a9934668SKenneth D. Merry break; 1145a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1146a9934668SKenneth D. Merry numbufs = min(io_req->num_bufs, 1); 1147a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1148a9934668SKenneth D. Merry break; 1149df424515SWarner Losh case XPT_NVME_IO: 1150df424515SWarner Losh case XPT_NVME_ADMIN: 1151df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1152df424515SWarner Losh numbufs = min(io_req->num_bufs, 1); 1153df424515SWarner Losh break; 1154a9934668SKenneth D. Merry default: 1155a9934668SKenneth D. Merry /* allow ourselves to be swapped once again */ 1156a9934668SKenneth D. Merry return; 1157a9934668SKenneth D. Merry break; /* NOTREACHED */ 1158a9934668SKenneth D. Merry } 1159a9934668SKenneth D. Merry 1160a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_USER_SEG_MALLOC) { 1161a9934668SKenneth D. Merry free(io_req->user_segptr, M_SCSIPASS); 1162a9934668SKenneth D. Merry io_req->user_segptr = NULL; 1163a9934668SKenneth D. Merry } 1164a9934668SKenneth D. Merry 1165a9934668SKenneth D. Merry /* 1166a9934668SKenneth D. Merry * We only want to free memory we malloced. 1167a9934668SKenneth D. Merry */ 1168a9934668SKenneth D. Merry if (io_req->data_flags == CAM_DATA_VADDR) { 1169a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1170a9934668SKenneth D. Merry if (io_req->kern_bufs[i] == NULL) 1171a9934668SKenneth D. Merry continue; 1172a9934668SKenneth D. Merry 1173a9934668SKenneth D. Merry free(io_req->kern_bufs[i], M_SCSIPASS); 1174a9934668SKenneth D. Merry io_req->kern_bufs[i] = NULL; 1175a9934668SKenneth D. Merry } 1176a9934668SKenneth D. Merry } else if (io_req->data_flags == CAM_DATA_SG) { 1177a9934668SKenneth D. Merry for (i = 0; i < io_req->num_kern_segs; i++) { 1178a9934668SKenneth D. Merry if ((uint8_t *)(uintptr_t) 1179a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr == NULL) 1180a9934668SKenneth D. Merry continue; 1181a9934668SKenneth D. Merry 1182a9934668SKenneth D. Merry uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t) 1183a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr); 1184a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 0; 1185a9934668SKenneth D. Merry } 1186a9934668SKenneth D. Merry } 1187a9934668SKenneth D. Merry 1188a9934668SKenneth D. Merry if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) { 1189a9934668SKenneth D. Merry free(io_req->kern_segptr, M_SCSIPASS); 1190a9934668SKenneth D. Merry io_req->kern_segptr = NULL; 1191a9934668SKenneth D. Merry } 1192a9934668SKenneth D. Merry 1193a9934668SKenneth D. Merry if (io_req->data_flags != CAM_DATA_PADDR) { 1194a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1195a9934668SKenneth D. Merry /* 1196a9934668SKenneth D. Merry * Restore the user's buffer pointers to their 1197a9934668SKenneth D. Merry * previous values. 1198a9934668SKenneth D. Merry */ 1199a9934668SKenneth D. Merry if (io_req->user_bufs[i] != NULL) 1200a9934668SKenneth D. Merry *data_ptrs[i] = io_req->user_bufs[i]; 1201a9934668SKenneth D. Merry } 1202a9934668SKenneth D. Merry } 1203a9934668SKenneth D. Merry 1204a9934668SKenneth D. Merry } 1205a9934668SKenneth D. Merry 1206a9934668SKenneth D. Merry static int 1207a9934668SKenneth D. Merry passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req, 1208a9934668SKenneth D. Merry ccb_flags direction) 1209a9934668SKenneth D. Merry { 1210a9934668SKenneth D. Merry bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy; 1211a9934668SKenneth D. Merry bus_dma_segment_t *user_sglist, *kern_sglist; 1212a9934668SKenneth D. Merry int i, j, error; 1213a9934668SKenneth D. Merry 1214a9934668SKenneth D. Merry error = 0; 1215a9934668SKenneth D. Merry kern_watermark = 0; 1216a9934668SKenneth D. Merry user_watermark = 0; 1217a9934668SKenneth D. Merry len_to_copy = 0; 1218a9934668SKenneth D. Merry len_copied = 0; 1219a9934668SKenneth D. Merry user_sglist = io_req->user_segptr; 1220a9934668SKenneth D. Merry kern_sglist = io_req->kern_segptr; 1221a9934668SKenneth D. Merry 1222a9934668SKenneth D. Merry for (i = 0, j = 0; i < io_req->num_user_segs && 1223a9934668SKenneth D. Merry j < io_req->num_kern_segs;) { 1224a9934668SKenneth D. Merry uint8_t *user_ptr, *kern_ptr; 1225a9934668SKenneth D. Merry 1226a9934668SKenneth D. Merry len_to_copy = min(user_sglist[i].ds_len -user_watermark, 1227a9934668SKenneth D. Merry kern_sglist[j].ds_len - kern_watermark); 1228a9934668SKenneth D. Merry 1229a9934668SKenneth D. Merry user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr; 1230a9934668SKenneth D. Merry user_ptr = user_ptr + user_watermark; 1231a9934668SKenneth D. Merry kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr; 1232a9934668SKenneth D. Merry kern_ptr = kern_ptr + kern_watermark; 1233a9934668SKenneth D. Merry 1234a9934668SKenneth D. Merry user_watermark += len_to_copy; 1235a9934668SKenneth D. Merry kern_watermark += len_to_copy; 1236a9934668SKenneth D. Merry 1237a9934668SKenneth D. Merry if (!useracc(user_ptr, len_to_copy, 1238a9934668SKenneth D. Merry (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) { 1239a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1240a9934668SKenneth D. Merry "S/G list element %p len %zu\n", __func__, 1241a9934668SKenneth D. Merry user_ptr, len_to_copy); 1242a9934668SKenneth D. Merry error = EFAULT; 1243a9934668SKenneth D. Merry goto bailout; 1244a9934668SKenneth D. Merry } 1245a9934668SKenneth D. Merry 1246a9934668SKenneth D. Merry if (direction == CAM_DIR_IN) { 1247a9934668SKenneth D. Merry error = copyout(kern_ptr, user_ptr, len_to_copy); 1248a9934668SKenneth D. Merry if (error != 0) { 1249a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyout of %u " 1250a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1251a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1252a9934668SKenneth D. Merry kern_ptr, user_ptr, error); 1253a9934668SKenneth D. Merry goto bailout; 1254a9934668SKenneth D. Merry } 1255a9934668SKenneth D. Merry } else { 1256a9934668SKenneth D. Merry error = copyin(user_ptr, kern_ptr, len_to_copy); 1257a9934668SKenneth D. Merry if (error != 0) { 1258a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copyin of %u " 1259a9934668SKenneth D. Merry "bytes from %p to %p failed with " 1260a9934668SKenneth D. Merry "error %d\n", __func__, len_to_copy, 1261a9934668SKenneth D. Merry user_ptr, kern_ptr, error); 1262a9934668SKenneth D. Merry goto bailout; 1263a9934668SKenneth D. Merry } 1264a9934668SKenneth D. Merry } 1265a9934668SKenneth D. Merry 1266a9934668SKenneth D. Merry len_copied += len_to_copy; 1267a9934668SKenneth D. Merry 1268a9934668SKenneth D. Merry if (user_sglist[i].ds_len == user_watermark) { 1269a9934668SKenneth D. Merry i++; 1270a9934668SKenneth D. Merry user_watermark = 0; 1271a9934668SKenneth D. Merry } 1272a9934668SKenneth D. Merry 1273a9934668SKenneth D. Merry if (kern_sglist[j].ds_len == kern_watermark) { 1274a9934668SKenneth D. Merry j++; 1275a9934668SKenneth D. Merry kern_watermark = 0; 1276a9934668SKenneth D. Merry } 1277a9934668SKenneth D. Merry } 1278a9934668SKenneth D. Merry 1279a9934668SKenneth D. Merry bailout: 1280a9934668SKenneth D. Merry 1281a9934668SKenneth D. Merry return (error); 1282a9934668SKenneth D. Merry } 1283a9934668SKenneth D. Merry 1284a9934668SKenneth D. Merry static int 1285a9934668SKenneth D. Merry passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req) 1286a9934668SKenneth D. Merry { 1287a9934668SKenneth D. Merry union ccb *ccb; 1288a9934668SKenneth D. Merry struct pass_softc *softc; 1289a9934668SKenneth D. Merry int numbufs, i; 1290a9934668SKenneth D. Merry uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1291a9934668SKenneth D. Merry uint32_t lengths[CAM_PERIPH_MAXMAPS]; 1292a9934668SKenneth D. Merry uint32_t dirs[CAM_PERIPH_MAXMAPS]; 1293a9934668SKenneth D. Merry uint32_t num_segs; 1294a9934668SKenneth D. Merry uint16_t *seg_cnt_ptr; 1295a9934668SKenneth D. Merry size_t maxmap; 1296a9934668SKenneth D. Merry int error; 1297a9934668SKenneth D. Merry 1298a9934668SKenneth D. Merry cam_periph_assert(periph, MA_NOTOWNED); 1299a9934668SKenneth D. Merry 1300a9934668SKenneth D. Merry softc = periph->softc; 1301a9934668SKenneth D. Merry 1302a9934668SKenneth D. Merry error = 0; 1303a9934668SKenneth D. Merry ccb = &io_req->ccb; 1304a9934668SKenneth D. Merry maxmap = 0; 1305a9934668SKenneth D. Merry num_segs = 0; 1306a9934668SKenneth D. Merry seg_cnt_ptr = NULL; 1307a9934668SKenneth D. Merry 1308a9934668SKenneth D. Merry switch(ccb->ccb_h.func_code) { 1309a9934668SKenneth D. Merry case XPT_DEV_MATCH: 1310a9934668SKenneth D. Merry if (ccb->cdm.match_buf_len == 0) { 1311a9934668SKenneth D. Merry printf("%s: invalid match buffer length 0\n", __func__); 1312a9934668SKenneth D. Merry return(EINVAL); 1313a9934668SKenneth D. Merry } 1314a9934668SKenneth D. Merry if (ccb->cdm.pattern_buf_len > 0) { 1315a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1316a9934668SKenneth D. Merry lengths[0] = ccb->cdm.pattern_buf_len; 1317a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1318a9934668SKenneth D. Merry data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1319a9934668SKenneth D. Merry lengths[1] = ccb->cdm.match_buf_len; 1320a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1321a9934668SKenneth D. Merry numbufs = 2; 1322a9934668SKenneth D. Merry } else { 1323a9934668SKenneth D. Merry data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1324a9934668SKenneth D. Merry lengths[0] = ccb->cdm.match_buf_len; 1325a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1326a9934668SKenneth D. Merry numbufs = 1; 1327a9934668SKenneth D. Merry } 1328a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1329a9934668SKenneth D. Merry break; 1330a9934668SKenneth D. Merry case XPT_SCSI_IO: 1331a9934668SKenneth D. Merry case XPT_CONT_TARGET_IO: 1332a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1333a9934668SKenneth D. Merry return(0); 1334a9934668SKenneth D. Merry 1335a9934668SKenneth D. Merry /* 1336a9934668SKenneth D. Merry * The user shouldn't be able to supply a bio. 1337a9934668SKenneth D. Merry */ 1338a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) 1339a9934668SKenneth D. Merry return (EINVAL); 1340a9934668SKenneth D. Merry 1341a9934668SKenneth D. Merry io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1342a9934668SKenneth D. Merry 1343a9934668SKenneth D. Merry data_ptrs[0] = &ccb->csio.data_ptr; 1344a9934668SKenneth D. Merry lengths[0] = ccb->csio.dxfer_len; 1345a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1346a9934668SKenneth D. Merry num_segs = ccb->csio.sglist_cnt; 1347a9934668SKenneth D. Merry seg_cnt_ptr = &ccb->csio.sglist_cnt; 1348a9934668SKenneth D. Merry numbufs = 1; 1349a9934668SKenneth D. Merry maxmap = softc->maxio; 1350a9934668SKenneth D. Merry break; 1351a9934668SKenneth D. Merry case XPT_ATA_IO: 1352a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1353a9934668SKenneth D. Merry return(0); 1354a9934668SKenneth D. Merry 1355a9934668SKenneth D. Merry /* 1356a9934668SKenneth D. Merry * We only support a single virtual address for ATA I/O. 1357a9934668SKenneth D. Merry */ 1358a9934668SKenneth D. Merry if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 1359a9934668SKenneth D. Merry return (EINVAL); 1360a9934668SKenneth D. Merry 1361a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1362a9934668SKenneth D. Merry 1363a9934668SKenneth D. Merry data_ptrs[0] = &ccb->ataio.data_ptr; 1364a9934668SKenneth D. Merry lengths[0] = ccb->ataio.dxfer_len; 1365a9934668SKenneth D. Merry dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1366a9934668SKenneth D. Merry numbufs = 1; 1367a9934668SKenneth D. Merry maxmap = softc->maxio; 1368a9934668SKenneth D. Merry break; 1369a9934668SKenneth D. Merry case XPT_SMP_IO: 1370a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1371a9934668SKenneth D. Merry 1372a9934668SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1373a9934668SKenneth D. Merry lengths[0] = ccb->smpio.smp_request_len; 1374a9934668SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 1375a9934668SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1376a9934668SKenneth D. Merry lengths[1] = ccb->smpio.smp_response_len; 1377a9934668SKenneth D. Merry dirs[1] = CAM_DIR_IN; 1378a9934668SKenneth D. Merry numbufs = 2; 1379a9934668SKenneth D. Merry maxmap = softc->maxio; 1380a9934668SKenneth D. Merry break; 1381a9934668SKenneth D. Merry case XPT_DEV_ADVINFO: 1382a9934668SKenneth D. Merry if (ccb->cdai.bufsiz == 0) 1383a9934668SKenneth D. Merry return (0); 1384a9934668SKenneth D. Merry 1385a9934668SKenneth D. Merry io_req->data_flags = CAM_DATA_VADDR; 1386a9934668SKenneth D. Merry 1387a9934668SKenneth D. Merry data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1388a9934668SKenneth D. Merry lengths[0] = ccb->cdai.bufsiz; 1389a9934668SKenneth D. Merry dirs[0] = CAM_DIR_IN; 1390a9934668SKenneth D. Merry numbufs = 1; 1391a9934668SKenneth D. Merry break; 1392df424515SWarner Losh case XPT_NVME_ADMIN: 1393df424515SWarner Losh case XPT_NVME_IO: 1394df424515SWarner Losh if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1395df424515SWarner Losh return (0); 1396df424515SWarner Losh 1397df424515SWarner Losh io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1398df424515SWarner Losh 1399df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1400df424515SWarner Losh lengths[0] = ccb->nvmeio.dxfer_len; 1401df424515SWarner Losh dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1402*51977281SWarner Losh num_segs = ccb->nvmeio.sglist_cnt; 1403*51977281SWarner Losh seg_cnt_ptr = &ccb->nvmeio.sglist_cnt; 1404df424515SWarner Losh numbufs = 1; 1405df424515SWarner Losh maxmap = softc->maxio; 1406df424515SWarner Losh break; 1407a9934668SKenneth D. Merry default: 1408a9934668SKenneth D. Merry return(EINVAL); 1409a9934668SKenneth D. Merry break; /* NOTREACHED */ 1410a9934668SKenneth D. Merry } 1411a9934668SKenneth D. Merry 1412a9934668SKenneth D. Merry io_req->num_bufs = numbufs; 1413a9934668SKenneth D. Merry 1414a9934668SKenneth D. Merry /* 1415a9934668SKenneth D. Merry * If there is a maximum, check to make sure that the user's 1416a9934668SKenneth D. Merry * request fits within the limit. In general, we should only have 1417a9934668SKenneth D. Merry * a maximum length for requests that go to hardware. Otherwise it 1418a9934668SKenneth D. Merry * is whatever we're able to malloc. 1419a9934668SKenneth D. Merry */ 1420a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1421a9934668SKenneth D. Merry io_req->user_bufs[i] = *data_ptrs[i]; 1422a9934668SKenneth D. Merry io_req->dirs[i] = dirs[i]; 1423a9934668SKenneth D. Merry io_req->lengths[i] = lengths[i]; 1424a9934668SKenneth D. Merry 1425a9934668SKenneth D. Merry if (maxmap == 0) 1426a9934668SKenneth D. Merry continue; 1427a9934668SKenneth D. Merry 1428a9934668SKenneth D. Merry if (lengths[i] <= maxmap) 1429a9934668SKenneth D. Merry continue; 1430a9934668SKenneth D. Merry 1431a9934668SKenneth D. Merry xpt_print(periph->path, "%s: data length %u > max allowed %u " 1432a9934668SKenneth D. Merry "bytes\n", __func__, lengths[i], maxmap); 1433a9934668SKenneth D. Merry error = EINVAL; 1434a9934668SKenneth D. Merry goto bailout; 1435a9934668SKenneth D. Merry } 1436a9934668SKenneth D. Merry 1437a9934668SKenneth D. Merry switch (io_req->data_flags) { 1438a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1439a9934668SKenneth D. Merry /* Map or copy the buffer into kernel address space */ 1440a9934668SKenneth D. Merry for (i = 0; i < numbufs; i++) { 1441a9934668SKenneth D. Merry uint8_t *tmp_buf; 1442a9934668SKenneth D. Merry 1443a9934668SKenneth D. Merry /* 1444a9934668SKenneth D. Merry * If for some reason no length is specified, we 1445a9934668SKenneth D. Merry * don't need to allocate anything. 1446a9934668SKenneth D. Merry */ 1447a9934668SKenneth D. Merry if (io_req->lengths[i] == 0) 1448a9934668SKenneth D. Merry continue; 1449a9934668SKenneth D. Merry 1450a9934668SKenneth D. Merry /* 1451a9934668SKenneth D. Merry * Make sure that the user's buffer is accessible 1452a9934668SKenneth D. Merry * to that process. 1453a9934668SKenneth D. Merry */ 1454a9934668SKenneth D. Merry if (!useracc(io_req->user_bufs[i], io_req->lengths[i], 1455a9934668SKenneth D. Merry (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE : 1456a9934668SKenneth D. Merry VM_PROT_READ)) { 1457a9934668SKenneth D. Merry xpt_print(periph->path, "%s: user address %p " 1458a9934668SKenneth D. Merry "length %u is not accessible\n", __func__, 1459a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1460a9934668SKenneth D. Merry error = EFAULT; 1461a9934668SKenneth D. Merry goto bailout; 1462a9934668SKenneth D. Merry } 1463a9934668SKenneth D. Merry 1464a9934668SKenneth D. Merry tmp_buf = malloc(lengths[i], M_SCSIPASS, 1465a9934668SKenneth D. Merry M_WAITOK | M_ZERO); 1466a9934668SKenneth D. Merry io_req->kern_bufs[i] = tmp_buf; 1467a9934668SKenneth D. Merry *data_ptrs[i] = tmp_buf; 1468a9934668SKenneth D. Merry 1469a9934668SKenneth D. Merry #if 0 1470a9934668SKenneth D. Merry xpt_print(periph->path, "%s: malloced %p len %u, user " 1471a9934668SKenneth D. Merry "buffer %p, operation: %s\n", __func__, 1472a9934668SKenneth D. Merry tmp_buf, lengths[i], io_req->user_bufs[i], 1473a9934668SKenneth D. Merry (dirs[i] == CAM_DIR_IN) ? "read" : "write"); 1474a9934668SKenneth D. Merry #endif 1475a9934668SKenneth D. Merry /* 1476a9934668SKenneth D. Merry * We only need to copy in if the user is writing. 1477a9934668SKenneth D. Merry */ 1478a9934668SKenneth D. Merry if (dirs[i] != CAM_DIR_OUT) 1479a9934668SKenneth D. Merry continue; 1480a9934668SKenneth D. Merry 1481a9934668SKenneth D. Merry error = copyin(io_req->user_bufs[i], 1482a9934668SKenneth D. Merry io_req->kern_bufs[i], lengths[i]); 1483a9934668SKenneth D. Merry if (error != 0) { 1484a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user " 1485a9934668SKenneth D. Merry "buffer from %p to %p failed with " 1486a9934668SKenneth D. Merry "error %d\n", __func__, 1487a9934668SKenneth D. Merry io_req->user_bufs[i], 1488a9934668SKenneth D. Merry io_req->kern_bufs[i], error); 1489a9934668SKenneth D. Merry goto bailout; 1490a9934668SKenneth D. Merry } 1491a9934668SKenneth D. Merry } 1492a9934668SKenneth D. Merry break; 1493a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1494a9934668SKenneth D. Merry /* Pass down the pointer as-is */ 1495a9934668SKenneth D. Merry break; 1496a9934668SKenneth D. Merry case CAM_DATA_SG: { 1497a9934668SKenneth D. Merry size_t sg_length, size_to_go, alloc_size; 1498a9934668SKenneth D. Merry uint32_t num_segs_needed; 1499a9934668SKenneth D. Merry 1500a9934668SKenneth D. Merry /* 1501a9934668SKenneth D. Merry * Copy the user S/G list in, and then copy in the 1502a9934668SKenneth D. Merry * individual segments. 1503a9934668SKenneth D. Merry */ 1504a9934668SKenneth D. Merry /* 1505a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1506a9934668SKenneth D. Merry */ 1507a9934668SKenneth D. Merry if (numbufs != 1) { 1508a9934668SKenneth D. Merry xpt_print(periph->path, "%s: cannot currently handle " 1509a9934668SKenneth D. Merry "more than one S/G list per CCB\n", __func__); 1510a9934668SKenneth D. Merry error = EINVAL; 1511a9934668SKenneth D. Merry goto bailout; 1512a9934668SKenneth D. Merry } 1513a9934668SKenneth D. Merry 1514a9934668SKenneth D. Merry /* 1515a9934668SKenneth D. Merry * We have to have at least one segment. 1516a9934668SKenneth D. Merry */ 1517a9934668SKenneth D. Merry if (num_segs == 0) { 1518a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG flag set, " 1519a9934668SKenneth D. Merry "but sglist_cnt=0!\n", __func__); 1520a9934668SKenneth D. Merry error = EINVAL; 1521a9934668SKenneth D. Merry goto bailout; 1522a9934668SKenneth D. Merry } 1523a9934668SKenneth D. Merry 1524a9934668SKenneth D. Merry /* 1525a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1526a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1527a9934668SKenneth D. Merry */ 1528a9934668SKenneth D. Merry if (lengths[0] == 0) { 1529a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1530a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1531a9934668SKenneth D. Merry error = EINVAL; 1532a9934668SKenneth D. Merry goto bailout; 1533a9934668SKenneth D. Merry } 1534a9934668SKenneth D. Merry 1535a9934668SKenneth D. Merry /* 1536a9934668SKenneth D. Merry * We allocate buffers in io_zone_size increments for an 1537a9934668SKenneth D. Merry * S/G list. This will generally be MAXPHYS. 1538a9934668SKenneth D. Merry */ 1539a9934668SKenneth D. Merry if (lengths[0] <= softc->io_zone_size) 1540a9934668SKenneth D. Merry num_segs_needed = 1; 1541a9934668SKenneth D. Merry else { 1542a9934668SKenneth D. Merry num_segs_needed = lengths[0] / softc->io_zone_size; 1543a9934668SKenneth D. Merry if ((lengths[0] % softc->io_zone_size) != 0) 1544a9934668SKenneth D. Merry num_segs_needed++; 1545a9934668SKenneth D. Merry } 1546a9934668SKenneth D. Merry 1547a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1548a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1549a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1550a9934668SKenneth D. Merry io_req->num_kern_segs = num_segs_needed; 1551a9934668SKenneth D. Merry 1552a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1553a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1554a9934668SKenneth D. Merry 1555a9934668SKenneth D. Merry /* 1556a9934668SKenneth D. Merry * If we have enough segments allocated by default to handle 1557a9934668SKenneth D. Merry * the length of the user's S/G list, 1558a9934668SKenneth D. Merry */ 1559a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1560a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1561a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1562a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1563a9934668SKenneth D. Merry } else 1564a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1565a9934668SKenneth D. Merry 1566a9934668SKenneth D. Merry if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) { 1567a9934668SKenneth D. Merry xpt_print(periph->path, "%s: unable to access user " 1568a9934668SKenneth D. Merry "S/G list at %p\n", __func__, *data_ptrs[0]); 1569a9934668SKenneth D. Merry error = EFAULT; 1570a9934668SKenneth D. Merry goto bailout; 1571a9934668SKenneth D. Merry } 1572a9934668SKenneth D. Merry 1573a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1574a9934668SKenneth D. Merry if (error != 0) { 1575a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1576a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1577a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1578a9934668SKenneth D. Merry error); 1579a9934668SKenneth D. Merry goto bailout; 1580a9934668SKenneth D. Merry } 1581a9934668SKenneth D. Merry 1582a9934668SKenneth D. Merry if (num_segs_needed > PASS_MAX_SEGS) { 1583a9934668SKenneth D. Merry io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) * 1584a9934668SKenneth D. Merry num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO); 1585a9934668SKenneth D. Merry io_req->flags |= PASS_IO_KERN_SEG_MALLOC; 1586a9934668SKenneth D. Merry } else { 1587a9934668SKenneth D. Merry io_req->kern_segptr = io_req->kern_segs; 1588a9934668SKenneth D. Merry } 1589a9934668SKenneth D. Merry 1590a9934668SKenneth D. Merry /* 1591a9934668SKenneth D. Merry * Allocate the kernel S/G list. 1592a9934668SKenneth D. Merry */ 1593a9934668SKenneth D. Merry for (size_to_go = lengths[0], i = 0; 1594a9934668SKenneth D. Merry size_to_go > 0 && i < num_segs_needed; 1595a9934668SKenneth D. Merry i++, size_to_go -= alloc_size) { 1596a9934668SKenneth D. Merry uint8_t *kern_ptr; 1597a9934668SKenneth D. Merry 1598a9934668SKenneth D. Merry alloc_size = min(size_to_go, softc->io_zone_size); 1599a9934668SKenneth D. Merry kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK); 1600a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_addr = 1601a9934668SKenneth D. Merry (bus_addr_t)(uintptr_t)kern_ptr; 1602a9934668SKenneth D. Merry io_req->kern_segptr[i].ds_len = alloc_size; 1603a9934668SKenneth D. Merry } 1604a9934668SKenneth D. Merry if (size_to_go > 0) { 1605a9934668SKenneth D. Merry printf("%s: size_to_go = %zu, software error!\n", 1606a9934668SKenneth D. Merry __func__, size_to_go); 1607a9934668SKenneth D. Merry error = EINVAL; 1608a9934668SKenneth D. Merry goto bailout; 1609a9934668SKenneth D. Merry } 1610a9934668SKenneth D. Merry 1611a9934668SKenneth D. Merry *data_ptrs[0] = (uint8_t *)io_req->kern_segptr; 1612a9934668SKenneth D. Merry *seg_cnt_ptr = io_req->num_kern_segs; 1613a9934668SKenneth D. Merry 1614a9934668SKenneth D. Merry /* 1615a9934668SKenneth D. Merry * We only need to copy data here if the user is writing. 1616a9934668SKenneth D. Merry */ 1617a9934668SKenneth D. Merry if (dirs[0] == CAM_DIR_OUT) 1618a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, dirs[0]); 1619a9934668SKenneth D. Merry break; 1620a9934668SKenneth D. Merry } 1621a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: { 1622a9934668SKenneth D. Merry size_t sg_length; 1623a9934668SKenneth D. Merry 1624a9934668SKenneth D. Merry /* 1625a9934668SKenneth D. Merry * We shouldn't see this, but check just in case. 1626a9934668SKenneth D. Merry */ 1627a9934668SKenneth D. Merry if (numbufs != 1) { 1628a9934668SKenneth D. Merry printf("%s: cannot currently handle more than one " 1629a9934668SKenneth D. Merry "S/G list per CCB\n", __func__); 1630a9934668SKenneth D. Merry error = EINVAL; 1631a9934668SKenneth D. Merry goto bailout; 1632a9934668SKenneth D. Merry } 1633a9934668SKenneth D. Merry 1634a9934668SKenneth D. Merry /* 1635a9934668SKenneth D. Merry * We have to have at least one segment. 1636a9934668SKenneth D. Merry */ 1637a9934668SKenneth D. Merry if (num_segs == 0) { 1638a9934668SKenneth D. Merry xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag " 1639a9934668SKenneth D. Merry "set, but sglist_cnt=0!\n", __func__); 1640a9934668SKenneth D. Merry error = EINVAL; 1641a9934668SKenneth D. Merry goto bailout; 1642a9934668SKenneth D. Merry } 1643a9934668SKenneth D. Merry 1644a9934668SKenneth D. Merry /* 1645a9934668SKenneth D. Merry * Make sure the user specified the total length and didn't 1646a9934668SKenneth D. Merry * just leave it to us to decode the S/G list. 1647a9934668SKenneth D. Merry */ 1648a9934668SKenneth D. Merry if (lengths[0] == 0) { 1649a9934668SKenneth D. Merry xpt_print(periph->path, "%s: no dxfer_len specified, " 1650a9934668SKenneth D. Merry "but CAM_DATA_SG flag is set!\n", __func__); 1651a9934668SKenneth D. Merry error = EINVAL; 1652a9934668SKenneth D. Merry goto bailout; 1653a9934668SKenneth D. Merry } 1654a9934668SKenneth D. Merry 1655a9934668SKenneth D. Merry /* Figure out the size of the S/G list */ 1656a9934668SKenneth D. Merry sg_length = num_segs * sizeof(bus_dma_segment_t); 1657a9934668SKenneth D. Merry io_req->num_user_segs = num_segs; 1658a9934668SKenneth D. Merry io_req->num_kern_segs = io_req->num_user_segs; 1659a9934668SKenneth D. Merry 1660a9934668SKenneth D. Merry /* Save the user's S/G list pointer for later restoration */ 1661a9934668SKenneth D. Merry io_req->user_bufs[0] = *data_ptrs[0]; 1662a9934668SKenneth D. Merry 1663a9934668SKenneth D. Merry if (num_segs > PASS_MAX_SEGS) { 1664a9934668SKenneth D. Merry io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1665a9934668SKenneth D. Merry num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1666a9934668SKenneth D. Merry io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1667a9934668SKenneth D. Merry } else 1668a9934668SKenneth D. Merry io_req->user_segptr = io_req->user_segs; 1669a9934668SKenneth D. Merry 1670a9934668SKenneth D. Merry io_req->kern_segptr = io_req->user_segptr; 1671a9934668SKenneth D. Merry 1672a9934668SKenneth D. Merry error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1673a9934668SKenneth D. Merry if (error != 0) { 1674a9934668SKenneth D. Merry xpt_print(periph->path, "%s: copy of user S/G list " 1675a9934668SKenneth D. Merry "from %p to %p failed with error %d\n", 1676a9934668SKenneth D. Merry __func__, *data_ptrs[0], io_req->user_segptr, 1677a9934668SKenneth D. Merry error); 1678a9934668SKenneth D. Merry goto bailout; 1679a9934668SKenneth D. Merry } 1680a9934668SKenneth D. Merry break; 1681a9934668SKenneth D. Merry } 1682a9934668SKenneth D. Merry default: 1683a9934668SKenneth D. Merry case CAM_DATA_BIO: 1684a9934668SKenneth D. Merry /* 1685a9934668SKenneth D. Merry * A user shouldn't be attaching a bio to the CCB. It 1686a9934668SKenneth D. Merry * isn't a user-accessible structure. 1687a9934668SKenneth D. Merry */ 1688a9934668SKenneth D. Merry error = EINVAL; 1689a9934668SKenneth D. Merry break; 1690a9934668SKenneth D. Merry } 1691a9934668SKenneth D. Merry 1692a9934668SKenneth D. Merry bailout: 1693a9934668SKenneth D. Merry if (error != 0) 1694a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1695a9934668SKenneth D. Merry 1696a9934668SKenneth D. Merry return (error); 1697a9934668SKenneth D. Merry } 1698a9934668SKenneth D. Merry 1699a9934668SKenneth D. Merry static int 1700a9934668SKenneth D. Merry passmemdone(struct cam_periph *periph, struct pass_io_req *io_req) 1701a9934668SKenneth D. Merry { 1702a9934668SKenneth D. Merry struct pass_softc *softc; 1703a9934668SKenneth D. Merry union ccb *ccb; 1704a9934668SKenneth D. Merry int error; 1705a9934668SKenneth D. Merry int i; 1706a9934668SKenneth D. Merry 1707a9934668SKenneth D. Merry error = 0; 1708a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 1709a9934668SKenneth D. Merry ccb = &io_req->ccb; 1710a9934668SKenneth D. Merry 1711a9934668SKenneth D. Merry switch (io_req->data_flags) { 1712a9934668SKenneth D. Merry case CAM_DATA_VADDR: 1713a9934668SKenneth D. Merry /* 1714a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1715a9934668SKenneth D. Merry */ 1716a9934668SKenneth D. Merry for (i = 0; i < io_req->num_bufs; i++) { 1717a9934668SKenneth D. Merry if (io_req->dirs[i] != CAM_DIR_IN) 1718a9934668SKenneth D. Merry continue; 1719a9934668SKenneth D. Merry 1720a9934668SKenneth D. Merry error = copyout(io_req->kern_bufs[i], 1721a9934668SKenneth D. Merry io_req->user_bufs[i], io_req->lengths[i]); 1722a9934668SKenneth D. Merry if (error != 0) { 1723a9934668SKenneth D. Merry xpt_print(periph->path, "Unable to copy %u " 1724a9934668SKenneth D. Merry "bytes from %p to user address %p\n", 1725a9934668SKenneth D. Merry io_req->lengths[i], 1726a9934668SKenneth D. Merry io_req->kern_bufs[i], 1727a9934668SKenneth D. Merry io_req->user_bufs[i]); 1728a9934668SKenneth D. Merry goto bailout; 1729a9934668SKenneth D. Merry } 1730a9934668SKenneth D. Merry 1731a9934668SKenneth D. Merry } 1732a9934668SKenneth D. Merry break; 1733a9934668SKenneth D. Merry case CAM_DATA_PADDR: 1734a9934668SKenneth D. Merry /* Do nothing. The pointer is a physical address already */ 1735a9934668SKenneth D. Merry break; 1736a9934668SKenneth D. Merry case CAM_DATA_SG: 1737a9934668SKenneth D. Merry /* 1738a9934668SKenneth D. Merry * Copy back to the user buffer if this was a read. 1739a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. 1740a9934668SKenneth D. Merry */ 1741a9934668SKenneth D. Merry if (io_req->dirs[0] == CAM_DIR_IN) 1742a9934668SKenneth D. Merry error = passcopysglist(periph, io_req, io_req->dirs[0]); 1743a9934668SKenneth D. Merry break; 1744a9934668SKenneth D. Merry case CAM_DATA_SG_PADDR: 1745a9934668SKenneth D. Merry /* 1746a9934668SKenneth D. Merry * Restore the user's S/G list buffer pointer. No need to 1747a9934668SKenneth D. Merry * copy. 1748a9934668SKenneth D. Merry */ 1749a9934668SKenneth D. Merry break; 1750a9934668SKenneth D. Merry default: 1751a9934668SKenneth D. Merry case CAM_DATA_BIO: 1752a9934668SKenneth D. Merry error = EINVAL; 1753a9934668SKenneth D. Merry break; 1754a9934668SKenneth D. Merry } 1755a9934668SKenneth D. Merry 1756a9934668SKenneth D. Merry bailout: 1757a9934668SKenneth D. Merry /* 1758a9934668SKenneth D. Merry * Reset the user's pointers to their original values and free 1759a9934668SKenneth D. Merry * allocated memory. 1760a9934668SKenneth D. Merry */ 1761a9934668SKenneth D. Merry passiocleanup(softc, io_req); 1762a9934668SKenneth D. Merry 1763a9934668SKenneth D. Merry return (error); 1764a9934668SKenneth D. Merry } 1765a9934668SKenneth D. Merry 176676babe50SJustin T. Gibbs static int 176789c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 176876babe50SJustin T. Gibbs { 176925a2902cSScott Long int error; 177025a2902cSScott Long 177125a2902cSScott Long if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 1772f564de00SScott Long error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl); 177325a2902cSScott Long } 177425a2902cSScott Long return (error); 177525a2902cSScott Long } 177625a2902cSScott Long 177725a2902cSScott Long static int 177825a2902cSScott Long passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 177925a2902cSScott Long { 178076babe50SJustin T. Gibbs struct cam_periph *periph; 1781a9934668SKenneth D. Merry struct pass_softc *softc; 178276babe50SJustin T. Gibbs int error; 17838cff7eb8SAlexander Motin uint32_t priority; 178476babe50SJustin T. Gibbs 1785e2a5fdf9SNate Lawson periph = (struct cam_periph *)dev->si_drv1; 17862b83592fSScott Long cam_periph_lock(periph); 1787a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 178876babe50SJustin T. Gibbs 178976babe50SJustin T. Gibbs error = 0; 179076babe50SJustin T. Gibbs 179176babe50SJustin T. Gibbs switch (cmd) { 179276babe50SJustin T. Gibbs 179376babe50SJustin T. Gibbs case CAMIOCOMMAND: 179476babe50SJustin T. Gibbs { 179576babe50SJustin T. Gibbs union ccb *inccb; 179676babe50SJustin T. Gibbs union ccb *ccb; 17979deea857SKenneth D. Merry int ccb_malloced; 179876babe50SJustin T. Gibbs 179976babe50SJustin T. Gibbs inccb = (union ccb *)addr; 18008fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 18018fc77fffSConrad Meyer if (inccb->ccb_h.func_code == XPT_SCSI_IO) 18028fc77fffSConrad Meyer inccb->csio.bio = NULL; 18038fc77fffSConrad Meyer #endif 18049deea857SKenneth D. Merry 1805a0bbf9e0SMark Johnston if (inccb->ccb_h.flags & CAM_UNLOCKED) { 1806a0bbf9e0SMark Johnston error = EINVAL; 1807a0bbf9e0SMark Johnston break; 1808a0bbf9e0SMark Johnston } 1809a0bbf9e0SMark Johnston 18109deea857SKenneth D. Merry /* 18119deea857SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 18129deea857SKenneth D. Merry * through the transport layer device. 18139deea857SKenneth D. Merry */ 18149deea857SKenneth D. Merry if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1815f0d9af51SMatt Jacob xpt_print(periph->path, "CCB function code %#x is " 1816f0d9af51SMatt Jacob "restricted to the XPT device\n", 1817f0d9af51SMatt Jacob inccb->ccb_h.func_code); 18189deea857SKenneth D. Merry error = ENODEV; 18199deea857SKenneth D. Merry break; 18209deea857SKenneth D. Merry } 18219deea857SKenneth D. Merry 18228cff7eb8SAlexander Motin /* Compatibility for RL/priority-unaware code. */ 18238cff7eb8SAlexander Motin priority = inccb->ccb_h.pinfo.priority; 1824cccf4220SAlexander Motin if (priority <= CAM_PRIORITY_OOB) 1825cccf4220SAlexander Motin priority += CAM_PRIORITY_OOB + 1; 18268cff7eb8SAlexander Motin 18279deea857SKenneth D. Merry /* 18289deea857SKenneth D. Merry * Non-immediate CCBs need a CCB from the per-device pool 18299deea857SKenneth D. Merry * of CCBs, which is scheduled by the transport layer. 18309deea857SKenneth D. Merry * Immediate CCBs and user-supplied CCBs should just be 18319deea857SKenneth D. Merry * malloced. 18329deea857SKenneth D. Merry */ 18339deea857SKenneth D. Merry if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 18349deea857SKenneth D. Merry && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 18358cff7eb8SAlexander Motin ccb = cam_periph_getccb(periph, priority); 18369deea857SKenneth D. Merry ccb_malloced = 0; 18379deea857SKenneth D. Merry } else { 18388008a935SScott Long ccb = xpt_alloc_ccb_nowait(); 18399deea857SKenneth D. Merry 18409deea857SKenneth D. Merry if (ccb != NULL) 18419deea857SKenneth D. Merry xpt_setup_ccb(&ccb->ccb_h, periph->path, 18428cff7eb8SAlexander Motin priority); 18439deea857SKenneth D. Merry ccb_malloced = 1; 18449deea857SKenneth D. Merry } 18459deea857SKenneth D. Merry 18469deea857SKenneth D. Merry if (ccb == NULL) { 1847f0d9af51SMatt Jacob xpt_print(periph->path, "unable to allocate CCB\n"); 18489deea857SKenneth D. Merry error = ENOMEM; 18499deea857SKenneth D. Merry break; 18509deea857SKenneth D. Merry } 185176babe50SJustin T. Gibbs 185276babe50SJustin T. Gibbs error = passsendccb(periph, ccb, inccb); 185376babe50SJustin T. Gibbs 18549deea857SKenneth D. Merry if (ccb_malloced) 18559deea857SKenneth D. Merry xpt_free_ccb(ccb); 18569deea857SKenneth D. Merry else 185776babe50SJustin T. Gibbs xpt_release_ccb(ccb); 185876babe50SJustin T. Gibbs 185976babe50SJustin T. Gibbs break; 186076babe50SJustin T. Gibbs } 1861a9934668SKenneth D. Merry case CAMIOQUEUE: 1862a9934668SKenneth D. Merry { 1863a9934668SKenneth D. Merry struct pass_io_req *io_req; 1864a9934668SKenneth D. Merry union ccb **user_ccb, *ccb; 1865a9934668SKenneth D. Merry xpt_opcode fc; 1866a9934668SKenneth D. Merry 1867a9934668SKenneth D. Merry if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) { 1868a9934668SKenneth D. Merry error = passcreatezone(periph); 1869a9934668SKenneth D. Merry if (error != 0) 1870a9934668SKenneth D. Merry goto bailout; 1871a9934668SKenneth D. Merry } 1872a9934668SKenneth D. Merry 1873a9934668SKenneth D. Merry /* 1874a9934668SKenneth D. Merry * We're going to do a blocking allocation for this I/O 1875a9934668SKenneth D. Merry * request, so we have to drop the lock. 1876a9934668SKenneth D. Merry */ 1877a9934668SKenneth D. Merry cam_periph_unlock(periph); 1878a9934668SKenneth D. Merry 1879a9934668SKenneth D. Merry io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO); 1880a9934668SKenneth D. Merry ccb = &io_req->ccb; 1881a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 1882a9934668SKenneth D. Merry 1883a9934668SKenneth D. Merry /* 1884a9934668SKenneth D. Merry * Unlike the CAMIOCOMMAND ioctl above, we only have a 1885a9934668SKenneth D. Merry * pointer to the user's CCB, so we have to copy the whole 1886a9934668SKenneth D. Merry * thing in to a buffer we have allocated (above) instead 1887a9934668SKenneth D. Merry * of allowing the ioctl code to malloc a buffer and copy 1888a9934668SKenneth D. Merry * it in. 1889a9934668SKenneth D. Merry * 1890a9934668SKenneth D. Merry * This is an advantage for this asynchronous interface, 1891a9934668SKenneth D. Merry * since we don't want the memory to get freed while the 1892a9934668SKenneth D. Merry * CCB is outstanding. 1893a9934668SKenneth D. Merry */ 1894a9934668SKenneth D. Merry #if 0 1895a9934668SKenneth D. Merry xpt_print(periph->path, "Copying user CCB %p to " 1896a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, ccb); 1897a9934668SKenneth D. Merry #endif 1898a9934668SKenneth D. Merry error = copyin(*user_ccb, ccb, sizeof(*ccb)); 1899a9934668SKenneth D. Merry if (error != 0) { 1900a9934668SKenneth D. Merry xpt_print(periph->path, "Copy of user CCB %p to " 1901a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 1902a9934668SKenneth D. Merry *user_ccb, ccb, error); 1903a0bbf9e0SMark Johnston goto camioqueue_error; 1904a9934668SKenneth D. Merry } 19058fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 19068fc77fffSConrad Meyer if (ccb->ccb_h.func_code == XPT_SCSI_IO) 19078fc77fffSConrad Meyer ccb->csio.bio = NULL; 19088fc77fffSConrad Meyer #endif 1909a9934668SKenneth D. Merry 1910a0bbf9e0SMark Johnston if (ccb->ccb_h.flags & CAM_UNLOCKED) { 1911a0bbf9e0SMark Johnston error = EINVAL; 1912a0bbf9e0SMark Johnston goto camioqueue_error; 1913a0bbf9e0SMark Johnston } 1914a0bbf9e0SMark Johnston 1915a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 1916a1a604caSAlexander Motin if (ccb->csio.cdb_len > IOCDBLEN) { 1917a1a604caSAlexander Motin error = EINVAL; 1918a0bbf9e0SMark Johnston goto camioqueue_error; 1919a1a604caSAlexander Motin } 1920a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, 1921a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len); 1922a0bbf9e0SMark Johnston if (error != 0) 1923a0bbf9e0SMark Johnston goto camioqueue_error; 1924a1a604caSAlexander Motin ccb->ccb_h.flags &= ~CAM_CDB_POINTER; 1925a1a604caSAlexander Motin } 1926a1a604caSAlexander Motin 1927a9934668SKenneth D. Merry /* 1928a9934668SKenneth D. Merry * Some CCB types, like scan bus and scan lun can only go 1929a9934668SKenneth D. Merry * through the transport layer device. 1930a9934668SKenneth D. Merry */ 1931a9934668SKenneth D. Merry if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1932a9934668SKenneth D. Merry xpt_print(periph->path, "CCB function code %#x is " 1933a9934668SKenneth D. Merry "restricted to the XPT device\n", 1934a9934668SKenneth D. Merry ccb->ccb_h.func_code); 1935a9934668SKenneth D. Merry error = ENODEV; 1936a0bbf9e0SMark Johnston goto camioqueue_error; 1937a9934668SKenneth D. Merry } 1938a9934668SKenneth D. Merry 1939a9934668SKenneth D. Merry /* 1940a9934668SKenneth D. Merry * Save the user's CCB pointer as well as his linked list 1941a9934668SKenneth D. Merry * pointers and peripheral private area so that we can 1942a9934668SKenneth D. Merry * restore these later. 1943a9934668SKenneth D. Merry */ 1944a9934668SKenneth D. Merry io_req->user_ccb_ptr = *user_ccb; 1945a9934668SKenneth D. Merry io_req->user_periph_links = ccb->ccb_h.periph_links; 1946a9934668SKenneth D. Merry io_req->user_periph_priv = ccb->ccb_h.periph_priv; 1947a9934668SKenneth D. Merry 1948a9934668SKenneth D. Merry /* 1949a9934668SKenneth D. Merry * Now that we've saved the user's values, we can set our 1950a9934668SKenneth D. Merry * own peripheral private entry. 1951a9934668SKenneth D. Merry */ 1952a9934668SKenneth D. Merry ccb->ccb_h.ccb_ioreq = io_req; 1953a9934668SKenneth D. Merry 1954a9934668SKenneth D. Merry /* Compatibility for RL/priority-unaware code. */ 1955a9934668SKenneth D. Merry priority = ccb->ccb_h.pinfo.priority; 1956a9934668SKenneth D. Merry if (priority <= CAM_PRIORITY_OOB) 1957a9934668SKenneth D. Merry priority += CAM_PRIORITY_OOB + 1; 1958a9934668SKenneth D. Merry 1959a9934668SKenneth D. Merry /* 1960a9934668SKenneth D. Merry * Setup fields in the CCB like the path and the priority. 1961a9934668SKenneth D. Merry * The path in particular cannot be done in userland, since 1962a9934668SKenneth D. Merry * it is a pointer to a kernel data structure. 1963a9934668SKenneth D. Merry */ 1964a9934668SKenneth D. Merry xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority, 1965a9934668SKenneth D. Merry ccb->ccb_h.flags); 1966a9934668SKenneth D. Merry 1967a9934668SKenneth D. Merry /* 1968a9934668SKenneth D. Merry * Setup our done routine. There is no way for the user to 1969a9934668SKenneth D. Merry * have a valid pointer here. 1970a9934668SKenneth D. Merry */ 1971a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 1972a9934668SKenneth D. Merry 1973a9934668SKenneth D. Merry fc = ccb->ccb_h.func_code; 1974a9934668SKenneth D. Merry /* 1975a9934668SKenneth D. Merry * If this function code has memory that can be mapped in 1976a9934668SKenneth D. Merry * or out, we need to call passmemsetup(). 1977a9934668SKenneth D. Merry */ 1978a9934668SKenneth D. Merry if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) 1979a9934668SKenneth D. Merry || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH) 1980df424515SWarner Losh || (fc == XPT_DEV_ADVINFO) 1981df424515SWarner Losh || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 1982a9934668SKenneth D. Merry error = passmemsetup(periph, io_req); 1983a0bbf9e0SMark Johnston if (error != 0) 1984a0bbf9e0SMark Johnston goto camioqueue_error; 1985a9934668SKenneth D. Merry } else 1986a9934668SKenneth D. Merry io_req->mapinfo.num_bufs_used = 0; 1987a9934668SKenneth D. Merry 1988a9934668SKenneth D. Merry cam_periph_lock(periph); 1989a9934668SKenneth D. Merry 1990a9934668SKenneth D. Merry /* 1991a9934668SKenneth D. Merry * Everything goes on the incoming queue initially. 1992a9934668SKenneth D. Merry */ 1993a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links); 1994a9934668SKenneth D. Merry 1995a9934668SKenneth D. Merry /* 1996a9934668SKenneth D. Merry * If the CCB is queued, and is not a user CCB, then 1997a9934668SKenneth D. Merry * we need to allocate a slot for it. Call xpt_schedule() 1998a9934668SKenneth D. Merry * so that our start routine will get called when a CCB is 1999a9934668SKenneth D. Merry * available. 2000a9934668SKenneth D. Merry */ 2001a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) 2002a9934668SKenneth D. Merry && ((fc & XPT_FC_USER_CCB) == 0)) { 2003a9934668SKenneth D. Merry xpt_schedule(periph, priority); 2004a9934668SKenneth D. Merry break; 2005a9934668SKenneth D. Merry } 2006a9934668SKenneth D. Merry 2007a9934668SKenneth D. Merry /* 2008a9934668SKenneth D. Merry * At this point, the CCB in question is either an 2009a9934668SKenneth D. Merry * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB 2010a9934668SKenneth D. Merry * and therefore should be malloced, not allocated via a slot. 2011a9934668SKenneth D. Merry * Remove the CCB from the incoming queue and add it to the 2012a9934668SKenneth D. Merry * active queue. 2013a9934668SKenneth D. Merry */ 2014a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 2015a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 2016a9934668SKenneth D. Merry 2017a9934668SKenneth D. Merry xpt_action(ccb); 2018a9934668SKenneth D. Merry 2019a9934668SKenneth D. Merry /* 2020a9934668SKenneth D. Merry * If this is not a queued CCB (i.e. it is an immediate CCB), 2021a9934668SKenneth D. Merry * then it is already done. We need to put it on the done 2022a9934668SKenneth D. Merry * queue for the user to fetch. 2023a9934668SKenneth D. Merry */ 2024a9934668SKenneth D. Merry if ((fc & XPT_FC_QUEUED) == 0) { 2025a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->active_queue, io_req, links); 2026a9934668SKenneth D. Merry TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 2027a9934668SKenneth D. Merry } 2028a9934668SKenneth D. Merry break; 2029a0bbf9e0SMark Johnston 2030a0bbf9e0SMark Johnston camioqueue_error: 2031a0bbf9e0SMark Johnston uma_zfree(softc->pass_zone, io_req); 2032a0bbf9e0SMark Johnston cam_periph_lock(periph); 2033a0bbf9e0SMark Johnston break; 2034a9934668SKenneth D. Merry } 2035a9934668SKenneth D. Merry case CAMIOGET: 2036a9934668SKenneth D. Merry { 2037a9934668SKenneth D. Merry union ccb **user_ccb; 2038a9934668SKenneth D. Merry struct pass_io_req *io_req; 2039a9934668SKenneth D. Merry int old_error; 2040a9934668SKenneth D. Merry 2041a9934668SKenneth D. Merry user_ccb = (union ccb **)addr; 2042a9934668SKenneth D. Merry old_error = 0; 2043a9934668SKenneth D. Merry 2044a9934668SKenneth D. Merry io_req = TAILQ_FIRST(&softc->done_queue); 2045a9934668SKenneth D. Merry if (io_req == NULL) { 2046a9934668SKenneth D. Merry error = ENOENT; 2047a9934668SKenneth D. Merry break; 2048a9934668SKenneth D. Merry } 2049a9934668SKenneth D. Merry 2050a9934668SKenneth D. Merry /* 2051a9934668SKenneth D. Merry * Remove the I/O from the done queue. 2052a9934668SKenneth D. Merry */ 2053a9934668SKenneth D. Merry TAILQ_REMOVE(&softc->done_queue, io_req, links); 2054a9934668SKenneth D. Merry 2055a9934668SKenneth D. Merry /* 2056a9934668SKenneth D. Merry * We have to drop the lock during the copyout because the 2057a9934668SKenneth D. Merry * copyout can result in VM faults that require sleeping. 2058a9934668SKenneth D. Merry */ 2059a9934668SKenneth D. Merry cam_periph_unlock(periph); 2060a9934668SKenneth D. Merry 2061a9934668SKenneth D. Merry /* 2062a9934668SKenneth D. Merry * Do any needed copies (e.g. for reads) and revert the 2063a9934668SKenneth D. Merry * pointers in the CCB back to the user's pointers. 2064a9934668SKenneth D. Merry */ 2065a9934668SKenneth D. Merry error = passmemdone(periph, io_req); 2066a9934668SKenneth D. Merry 2067a9934668SKenneth D. Merry old_error = error; 2068a9934668SKenneth D. Merry 2069a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_links = io_req->user_periph_links; 2070a9934668SKenneth D. Merry io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv; 2071a9934668SKenneth D. Merry 2072a9934668SKenneth D. Merry #if 0 2073a9934668SKenneth D. Merry xpt_print(periph->path, "Copying to user CCB %p from " 2074a9934668SKenneth D. Merry "kernel address %p\n", *user_ccb, &io_req->ccb); 2075a9934668SKenneth D. Merry #endif 2076a9934668SKenneth D. Merry 2077a9934668SKenneth D. Merry error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb)); 2078a9934668SKenneth D. Merry if (error != 0) { 2079a9934668SKenneth D. Merry xpt_print(periph->path, "Copy to user CCB %p from " 2080a9934668SKenneth D. Merry "kernel address %p failed with error %d\n", 2081a9934668SKenneth D. Merry *user_ccb, &io_req->ccb, error); 2082a9934668SKenneth D. Merry } 2083a9934668SKenneth D. Merry 2084a9934668SKenneth D. Merry /* 2085a9934668SKenneth D. Merry * Prefer the first error we got back, and make sure we 2086a9934668SKenneth D. Merry * don't overwrite bad status with good. 2087a9934668SKenneth D. Merry */ 2088a9934668SKenneth D. Merry if (old_error != 0) 2089a9934668SKenneth D. Merry error = old_error; 2090a9934668SKenneth D. Merry 2091a9934668SKenneth D. Merry cam_periph_lock(periph); 2092a9934668SKenneth D. Merry 2093a9934668SKenneth D. Merry /* 2094a9934668SKenneth D. Merry * At this point, if there was an error, we could potentially 2095a9934668SKenneth D. Merry * re-queue the I/O and try again. But why? The error 2096a9934668SKenneth D. Merry * would almost certainly happen again. We might as well 2097a9934668SKenneth D. Merry * not leak memory. 2098a9934668SKenneth D. Merry */ 2099a9934668SKenneth D. Merry uma_zfree(softc->pass_zone, io_req); 2100a9934668SKenneth D. Merry break; 2101a9934668SKenneth D. Merry } 210276babe50SJustin T. Gibbs default: 210376babe50SJustin T. Gibbs error = cam_periph_ioctl(periph, cmd, addr, passerror); 210476babe50SJustin T. Gibbs break; 210576babe50SJustin T. Gibbs } 210676babe50SJustin T. Gibbs 2107a9934668SKenneth D. Merry bailout: 21082b83592fSScott Long cam_periph_unlock(periph); 2109a9934668SKenneth D. Merry 211076babe50SJustin T. Gibbs return(error); 211176babe50SJustin T. Gibbs } 211276babe50SJustin T. Gibbs 2113a9934668SKenneth D. Merry static int 2114a9934668SKenneth D. Merry passpoll(struct cdev *dev, int poll_events, struct thread *td) 2115a9934668SKenneth D. Merry { 2116a9934668SKenneth D. Merry struct cam_periph *periph; 2117a9934668SKenneth D. Merry struct pass_softc *softc; 2118a9934668SKenneth D. Merry int revents; 2119a9934668SKenneth D. Merry 2120a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2121a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2122a9934668SKenneth D. Merry 2123a9934668SKenneth D. Merry revents = poll_events & (POLLOUT | POLLWRNORM); 2124a9934668SKenneth D. Merry if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 2125a9934668SKenneth D. Merry cam_periph_lock(periph); 2126a9934668SKenneth D. Merry 2127a9934668SKenneth D. Merry if (!TAILQ_EMPTY(&softc->done_queue)) { 2128a9934668SKenneth D. Merry revents |= poll_events & (POLLIN | POLLRDNORM); 2129a9934668SKenneth D. Merry } 2130a9934668SKenneth D. Merry cam_periph_unlock(periph); 2131a9934668SKenneth D. Merry if (revents == 0) 2132a9934668SKenneth D. Merry selrecord(td, &softc->read_select); 2133a9934668SKenneth D. Merry } 2134a9934668SKenneth D. Merry 2135a9934668SKenneth D. Merry return (revents); 2136a9934668SKenneth D. Merry } 2137a9934668SKenneth D. Merry 2138a9934668SKenneth D. Merry static int 2139a9934668SKenneth D. Merry passkqfilter(struct cdev *dev, struct knote *kn) 2140a9934668SKenneth D. Merry { 2141a9934668SKenneth D. Merry struct cam_periph *periph; 2142a9934668SKenneth D. Merry struct pass_softc *softc; 2143a9934668SKenneth D. Merry 2144a9934668SKenneth D. Merry periph = (struct cam_periph *)dev->si_drv1; 2145a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2146a9934668SKenneth D. Merry 2147a9934668SKenneth D. Merry kn->kn_hook = (caddr_t)periph; 2148a9934668SKenneth D. Merry kn->kn_fop = &passread_filtops; 2149a9934668SKenneth D. Merry knlist_add(&softc->read_select.si_note, kn, 0); 2150a9934668SKenneth D. Merry 2151a9934668SKenneth D. Merry return (0); 2152a9934668SKenneth D. Merry } 2153a9934668SKenneth D. Merry 2154a9934668SKenneth D. Merry static void 2155a9934668SKenneth D. Merry passreadfiltdetach(struct knote *kn) 2156a9934668SKenneth D. Merry { 2157a9934668SKenneth D. Merry struct cam_periph *periph; 2158a9934668SKenneth D. Merry struct pass_softc *softc; 2159a9934668SKenneth D. Merry 2160a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2161a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2162a9934668SKenneth D. Merry 2163a9934668SKenneth D. Merry knlist_remove(&softc->read_select.si_note, kn, 0); 2164a9934668SKenneth D. Merry } 2165a9934668SKenneth D. Merry 2166a9934668SKenneth D. Merry static int 2167a9934668SKenneth D. Merry passreadfilt(struct knote *kn, long hint) 2168a9934668SKenneth D. Merry { 2169a9934668SKenneth D. Merry struct cam_periph *periph; 2170a9934668SKenneth D. Merry struct pass_softc *softc; 2171a9934668SKenneth D. Merry int retval; 2172a9934668SKenneth D. Merry 2173a9934668SKenneth D. Merry periph = (struct cam_periph *)kn->kn_hook; 2174a9934668SKenneth D. Merry softc = (struct pass_softc *)periph->softc; 2175a9934668SKenneth D. Merry 2176a9934668SKenneth D. Merry cam_periph_assert(periph, MA_OWNED); 2177a9934668SKenneth D. Merry 2178a9934668SKenneth D. Merry if (TAILQ_EMPTY(&softc->done_queue)) 2179a9934668SKenneth D. Merry retval = 0; 2180a9934668SKenneth D. Merry else 2181a9934668SKenneth D. Merry retval = 1; 2182a9934668SKenneth D. Merry 2183a9934668SKenneth D. Merry return (retval); 2184a9934668SKenneth D. Merry } 2185a9934668SKenneth D. Merry 218676babe50SJustin T. Gibbs /* 218776babe50SJustin T. Gibbs * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 218876babe50SJustin T. Gibbs * should be the CCB that is copied in from the user. 218976babe50SJustin T. Gibbs */ 219076babe50SJustin T. Gibbs static int 219176babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 219276babe50SJustin T. Gibbs { 219376babe50SJustin T. Gibbs struct pass_softc *softc; 219476babe50SJustin T. Gibbs struct cam_periph_map_info mapinfo; 2195a1a604caSAlexander Motin uint8_t *cmd; 219695fbded6SScott Long xpt_opcode fc; 219795fbded6SScott Long int error; 219876babe50SJustin T. Gibbs 219976babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 220076babe50SJustin T. Gibbs 220176babe50SJustin T. Gibbs /* 220276babe50SJustin T. Gibbs * There are some fields in the CCB header that need to be 220376babe50SJustin T. Gibbs * preserved, the rest we get from the user. 220476babe50SJustin T. Gibbs */ 220576babe50SJustin T. Gibbs xpt_merge_ccb(ccb, inccb); 220676babe50SJustin T. Gibbs 2207a1a604caSAlexander Motin if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 2208a1a604caSAlexander Motin cmd = __builtin_alloca(ccb->csio.cdb_len); 2209a1a604caSAlexander Motin error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len); 2210a1a604caSAlexander Motin if (error) 2211a1a604caSAlexander Motin return (error); 2212a1a604caSAlexander Motin ccb->csio.cdb_io.cdb_ptr = cmd; 2213a1a604caSAlexander Motin } 2214a1a604caSAlexander Motin 221576babe50SJustin T. Gibbs /* 2216a9934668SKenneth D. Merry */ 2217a9934668SKenneth D. Merry ccb->ccb_h.cbfcnp = passdone; 2218a9934668SKenneth D. Merry 2219a9934668SKenneth D. Merry /* 222095fbded6SScott Long * Let cam_periph_mapmem do a sanity check on the data pointer format. 222195fbded6SScott Long * Even if no data transfer is needed, it's a cheap check and it 222295fbded6SScott Long * simplifies the code. 222376babe50SJustin T. Gibbs */ 222495fbded6SScott Long fc = ccb->ccb_h.func_code; 222595fbded6SScott Long if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO) 2226a94a63f0SWarner Losh || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO)) { 222776babe50SJustin T. Gibbs bzero(&mapinfo, sizeof(mapinfo)); 222876babe50SJustin T. Gibbs 22292b83592fSScott Long /* 22302b83592fSScott Long * cam_periph_mapmem calls into proc and vm functions that can 22312b83592fSScott Long * sleep as well as trigger I/O, so we can't hold the lock. 22322b83592fSScott Long * Dropping it here is reasonably safe. 22332b83592fSScott Long */ 22342b83592fSScott Long cam_periph_unlock(periph); 2235de239312SAlexander Motin error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio); 22362b83592fSScott Long cam_periph_lock(periph); 223776babe50SJustin T. Gibbs 223876babe50SJustin T. Gibbs /* 223976babe50SJustin T. Gibbs * cam_periph_mapmem returned an error, we can't continue. 224076babe50SJustin T. Gibbs * Return the error to the user. 224176babe50SJustin T. Gibbs */ 224276babe50SJustin T. Gibbs if (error) 224376babe50SJustin T. Gibbs return(error); 224495fbded6SScott Long } else 224595fbded6SScott Long /* Ensure that the unmap call later on is a no-op. */ 224695fbded6SScott Long mapinfo.num_bufs_used = 0; 224776babe50SJustin T. Gibbs 224876babe50SJustin T. Gibbs /* 224976babe50SJustin T. Gibbs * If the user wants us to perform any error recovery, then honor 225076babe50SJustin T. Gibbs * that request. Otherwise, it's up to the user to perform any 225176babe50SJustin T. Gibbs * error recovery. 225276babe50SJustin T. Gibbs */ 22536953d22bSKenneth D. Merry cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 22546953d22bSKenneth D. Merry passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO, 22556953d22bSKenneth D. Merry /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 2256a9d2245eSPoul-Henning Kamp softc->device_stats); 225776babe50SJustin T. Gibbs 225876babe50SJustin T. Gibbs cam_periph_unmapmem(ccb, &mapinfo); 225976babe50SJustin T. Gibbs 226076babe50SJustin T. Gibbs ccb->ccb_h.cbfcnp = NULL; 226176babe50SJustin T. Gibbs ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 226276babe50SJustin T. Gibbs bcopy(ccb, inccb, sizeof(union ccb)); 226376babe50SJustin T. Gibbs 226483c5d981SAlexander Motin return(0); 226576babe50SJustin T. Gibbs } 226676babe50SJustin T. Gibbs 226776babe50SJustin T. Gibbs static int 226876babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 226976babe50SJustin T. Gibbs { 227076babe50SJustin T. Gibbs struct cam_periph *periph; 227176babe50SJustin T. Gibbs struct pass_softc *softc; 227276babe50SJustin T. Gibbs 227376babe50SJustin T. Gibbs periph = xpt_path_periph(ccb->ccb_h.path); 227476babe50SJustin T. Gibbs softc = (struct pass_softc *)periph->softc; 227576babe50SJustin T. Gibbs 227676babe50SJustin T. Gibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 227776babe50SJustin T. Gibbs &softc->saved_ccb)); 227876babe50SJustin T. Gibbs } 2279