1898b0535SWarner Losh /*- 28b8a9b1dSJustin T. Gibbs * Common functions for CAM "type" (peripheral) drivers. 38b8a9b1dSJustin T. Gibbs * 4bec9534dSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5bec9534dSPedro F. Giffuni * 68b8a9b1dSJustin T. Gibbs * Copyright (c) 1997, 1998 Justin T. Gibbs. 7501468a5SKenneth D. Merry * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry. 88b8a9b1dSJustin T. Gibbs * All rights reserved. 98b8a9b1dSJustin T. Gibbs * 108b8a9b1dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 118b8a9b1dSJustin T. Gibbs * modification, are permitted provided that the following conditions 128b8a9b1dSJustin T. Gibbs * are met: 138b8a9b1dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 148b8a9b1dSJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 158b8a9b1dSJustin T. Gibbs * without modification, immediately at the beginning of the file. 168b8a9b1dSJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 178b8a9b1dSJustin T. Gibbs * derived from this software without specific prior written permission. 188b8a9b1dSJustin T. Gibbs * 198b8a9b1dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 208b8a9b1dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 218b8a9b1dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 228b8a9b1dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 238b8a9b1dSJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 248b8a9b1dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 258b8a9b1dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 268b8a9b1dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 278b8a9b1dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 288b8a9b1dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 298b8a9b1dSJustin T. Gibbs * SUCH DAMAGE. 308b8a9b1dSJustin T. Gibbs */ 318b8a9b1dSJustin T. Gibbs 329c963d87SDavid E. O'Brien #include <sys/cdefs.h> 339c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$"); 349c963d87SDavid E. O'Brien 358b8a9b1dSJustin T. Gibbs #include <sys/param.h> 368b8a9b1dSJustin T. Gibbs #include <sys/systm.h> 378b8a9b1dSJustin T. Gibbs #include <sys/types.h> 388b8a9b1dSJustin T. Gibbs #include <sys/malloc.h> 39362abc44STai-hwa Liang #include <sys/kernel.h> 409626b608SPoul-Henning Kamp #include <sys/bio.h> 415cf3cd10SWarner Losh #include <sys/conf.h> 42773e541eSWarner Losh #include <sys/devctl.h> 43f34fa851SJohn Baldwin #include <sys/lock.h> 44f34fa851SJohn Baldwin #include <sys/mutex.h> 458b8a9b1dSJustin T. Gibbs #include <sys/buf.h> 468b8a9b1dSJustin T. Gibbs #include <sys/proc.h> 478b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h> 483501942bSJustin T. Gibbs #include <sys/sbuf.h> 49b059686aSAlexander Motin #include <sys/sysctl.h> 508b8a9b1dSJustin T. Gibbs #include <vm/vm.h> 518b8a9b1dSJustin T. Gibbs #include <vm/vm_extern.h> 528b8a9b1dSJustin T. Gibbs 538b8a9b1dSJustin T. Gibbs #include <cam/cam.h> 548b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h> 5552c9ce25SScott Long #include <cam/cam_queue.h> 568b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h> 57447b3557SJohn Baldwin #include <cam/cam_xpt_internal.h> 588b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h> 598b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h> 602b83592fSScott Long #include <cam/cam_sim.h> 618b8a9b1dSJustin T. Gibbs 628b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h> 638b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h> 648b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h> 658b8a9b1dSJustin T. Gibbs 668b8a9b1dSJustin T. Gibbs static u_int camperiphnextunit(struct periph_driver *p_drv, 67501468a5SKenneth D. Merry u_int newunit, int wired, 68501468a5SKenneth D. Merry path_id_t pathid, target_id_t target, 69501468a5SKenneth D. Merry lun_id_t lun); 708b8a9b1dSJustin T. Gibbs static u_int camperiphunit(struct periph_driver *p_drv, 71501468a5SKenneth D. Merry path_id_t pathid, target_id_t target, 72501468a5SKenneth D. Merry lun_id_t lun); 738b8a9b1dSJustin T. Gibbs static void camperiphdone(struct cam_periph *periph, 748b8a9b1dSJustin T. Gibbs union ccb *done_ccb); 758b8a9b1dSJustin T. Gibbs static void camperiphfree(struct cam_periph *periph); 763393f8daSKenneth D. Merry static int camperiphscsistatuserror(union ccb *ccb, 770191d9b3SAlexander Motin union ccb **orig_ccb, 783393f8daSKenneth D. Merry cam_flags camflags, 793393f8daSKenneth D. Merry u_int32_t sense_flags, 803393f8daSKenneth D. Merry int *openings, 813393f8daSKenneth D. Merry u_int32_t *relsim_flags, 8283c5d981SAlexander Motin u_int32_t *timeout, 830181d54bSAlexander Motin u_int32_t *action, 8483c5d981SAlexander Motin const char **action_string); 853393f8daSKenneth D. Merry static int camperiphscsisenseerror(union ccb *ccb, 860191d9b3SAlexander Motin union ccb **orig_ccb, 873393f8daSKenneth D. Merry cam_flags camflags, 883393f8daSKenneth D. Merry u_int32_t sense_flags, 893393f8daSKenneth D. Merry int *openings, 903393f8daSKenneth D. Merry u_int32_t *relsim_flags, 9183c5d981SAlexander Motin u_int32_t *timeout, 920181d54bSAlexander Motin u_int32_t *action, 9383c5d981SAlexander Motin const char **action_string); 94a30ecd42SScott Long static void cam_periph_devctl_notify(union ccb *ccb); 958b8a9b1dSJustin T. Gibbs 960b7c27b9SPeter Wemm static int nperiph_drivers; 9783c5d981SAlexander Motin static int initialized = 0; 980b7c27b9SPeter Wemm struct periph_driver **periph_drivers; 990b7c27b9SPeter Wemm 100d745c852SEd Schouten static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers"); 101362abc44STai-hwa Liang 10273cf209fSMatt Jacob static int periph_selto_delay = 1000; 10373cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay); 10473cf209fSMatt Jacob static int periph_noresrc_delay = 500; 10573cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay); 10673cf209fSMatt Jacob static int periph_busy_delay = 500; 10773cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay); 10873cf209fSMatt Jacob 109b059686aSAlexander Motin static u_int periph_mapmem_thresh = 65536; 110b059686aSAlexander Motin SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN, 111b059686aSAlexander Motin &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping"); 11273cf209fSMatt Jacob 1130b7c27b9SPeter Wemm void 1140b7c27b9SPeter Wemm periphdriver_register(void *data) 1150b7c27b9SPeter Wemm { 11683c5d981SAlexander Motin struct periph_driver *drv = (struct periph_driver *)data; 1170b7c27b9SPeter Wemm struct periph_driver **newdrivers, **old; 1180b7c27b9SPeter Wemm int ndrivers; 1190b7c27b9SPeter Wemm 1205719711fSEdward Tomasz Napierala again: 1210b7c27b9SPeter Wemm ndrivers = nperiph_drivers + 2; 1220dd50e9bSScott Long newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH, 1230dd50e9bSScott Long M_WAITOK); 1245719711fSEdward Tomasz Napierala xpt_lock_buses(); 1255719711fSEdward Tomasz Napierala if (ndrivers != nperiph_drivers + 2) { 1265719711fSEdward Tomasz Napierala /* 1275719711fSEdward Tomasz Napierala * Lost race against itself; go around. 1285719711fSEdward Tomasz Napierala */ 1295719711fSEdward Tomasz Napierala xpt_unlock_buses(); 1305719711fSEdward Tomasz Napierala free(newdrivers, M_CAMPERIPH); 1315719711fSEdward Tomasz Napierala goto again; 1325719711fSEdward Tomasz Napierala } 1330b7c27b9SPeter Wemm if (periph_drivers) 1340b7c27b9SPeter Wemm bcopy(periph_drivers, newdrivers, 135623db360SKenneth D. Merry sizeof(*newdrivers) * nperiph_drivers); 13683c5d981SAlexander Motin newdrivers[nperiph_drivers] = drv; 1370b7c27b9SPeter Wemm newdrivers[nperiph_drivers + 1] = NULL; 1380b7c27b9SPeter Wemm old = periph_drivers; 1390b7c27b9SPeter Wemm periph_drivers = newdrivers; 1405719711fSEdward Tomasz Napierala nperiph_drivers++; 1415719711fSEdward Tomasz Napierala xpt_unlock_buses(); 1420b7c27b9SPeter Wemm if (old) 1430dd50e9bSScott Long free(old, M_CAMPERIPH); 14483c5d981SAlexander Motin /* If driver marked as early or it is late now, initialize it. */ 14583c5d981SAlexander Motin if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) || 14683c5d981SAlexander Motin initialized > 1) 14783c5d981SAlexander Motin (*drv->init)(); 14883c5d981SAlexander Motin } 14983c5d981SAlexander Motin 15094173c3cSAlexander Motin int 15194173c3cSAlexander Motin periphdriver_unregister(void *data) 15294173c3cSAlexander Motin { 15394173c3cSAlexander Motin struct periph_driver *drv = (struct periph_driver *)data; 15494173c3cSAlexander Motin int error, n; 15594173c3cSAlexander Motin 15694173c3cSAlexander Motin /* If driver marked as early or it is late now, deinitialize it. */ 15794173c3cSAlexander Motin if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) || 15894173c3cSAlexander Motin initialized > 1) { 15994173c3cSAlexander Motin if (drv->deinit == NULL) { 16094173c3cSAlexander Motin printf("CAM periph driver '%s' doesn't have deinit.\n", 16194173c3cSAlexander Motin drv->driver_name); 16294173c3cSAlexander Motin return (EOPNOTSUPP); 16394173c3cSAlexander Motin } 16494173c3cSAlexander Motin error = drv->deinit(); 16594173c3cSAlexander Motin if (error != 0) 16694173c3cSAlexander Motin return (error); 16794173c3cSAlexander Motin } 16894173c3cSAlexander Motin 16994173c3cSAlexander Motin xpt_lock_buses(); 17094173c3cSAlexander Motin for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++) 17194173c3cSAlexander Motin ; 17294173c3cSAlexander Motin KASSERT(n < nperiph_drivers, 17394173c3cSAlexander Motin ("Periph driver '%s' was not registered", drv->driver_name)); 17494173c3cSAlexander Motin for (; n + 1 < nperiph_drivers; n++) 17594173c3cSAlexander Motin periph_drivers[n] = periph_drivers[n + 1]; 17694173c3cSAlexander Motin periph_drivers[n + 1] = NULL; 17794173c3cSAlexander Motin nperiph_drivers--; 17894173c3cSAlexander Motin xpt_unlock_buses(); 17994173c3cSAlexander Motin return (0); 18094173c3cSAlexander Motin } 18194173c3cSAlexander Motin 18283c5d981SAlexander Motin void 18383c5d981SAlexander Motin periphdriver_init(int level) 18483c5d981SAlexander Motin { 18583c5d981SAlexander Motin int i, early; 18683c5d981SAlexander Motin 18783c5d981SAlexander Motin initialized = max(initialized, level); 18883c5d981SAlexander Motin for (i = 0; periph_drivers[i] != NULL; i++) { 18983c5d981SAlexander Motin early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2; 19083c5d981SAlexander Motin if (early == initialized) 19183c5d981SAlexander Motin (*periph_drivers[i]->init)(); 19283c5d981SAlexander Motin } 1930b7c27b9SPeter Wemm } 1940b7c27b9SPeter Wemm 1958b8a9b1dSJustin T. Gibbs cam_status 196ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor, 197ee9c90c7SKenneth D. Merry periph_oninv_t *periph_oninvalidate, 198ee9c90c7SKenneth D. Merry periph_dtor_t *periph_dtor, periph_start_t *periph_start, 199ee9c90c7SKenneth D. Merry char *name, cam_periph_type type, struct cam_path *path, 200ee9c90c7SKenneth D. Merry ac_callback_t *ac_callback, ac_code code, void *arg) 2018b8a9b1dSJustin T. Gibbs { 2028b8a9b1dSJustin T. Gibbs struct periph_driver **p_drv; 2032b83592fSScott Long struct cam_sim *sim; 2048b8a9b1dSJustin T. Gibbs struct cam_periph *periph; 2058b8a9b1dSJustin T. Gibbs struct cam_periph *cur_periph; 2068b8a9b1dSJustin T. Gibbs path_id_t path_id; 2078b8a9b1dSJustin T. Gibbs target_id_t target_id; 2088b8a9b1dSJustin T. Gibbs lun_id_t lun_id; 2098b8a9b1dSJustin T. Gibbs cam_status status; 2108b8a9b1dSJustin T. Gibbs u_int init_level; 2118b8a9b1dSJustin T. Gibbs 2128b8a9b1dSJustin T. Gibbs init_level = 0; 2138b8a9b1dSJustin T. Gibbs /* 2148b8a9b1dSJustin T. Gibbs * Handle Hot-Plug scenarios. If there is already a peripheral 2158b8a9b1dSJustin T. Gibbs * of our type assigned to this path, we are likely waiting for 2168b8a9b1dSJustin T. Gibbs * final close on an old, invalidated, peripheral. If this is 2178b8a9b1dSJustin T. Gibbs * the case, queue up a deferred call to the peripheral's async 21874c91ec5SJustin T. Gibbs * handler. If it looks like a mistaken re-allocation, complain. 2198b8a9b1dSJustin T. Gibbs */ 2208b8a9b1dSJustin T. Gibbs if ((periph = cam_periph_find(path, name)) != NULL) { 2218b8a9b1dSJustin T. Gibbs if ((periph->flags & CAM_PERIPH_INVALID) != 0 2228b8a9b1dSJustin T. Gibbs && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) { 2238b8a9b1dSJustin T. Gibbs periph->flags |= CAM_PERIPH_NEW_DEV_FOUND; 2248b8a9b1dSJustin T. Gibbs periph->deferred_callback = ac_callback; 2258b8a9b1dSJustin T. Gibbs periph->deferred_ac = code; 2268b8a9b1dSJustin T. Gibbs return (CAM_REQ_INPROG); 2278b8a9b1dSJustin T. Gibbs } else { 2288b8a9b1dSJustin T. Gibbs printf("cam_periph_alloc: attempt to re-allocate " 2298900f4b8SKenneth D. Merry "valid device %s%d rejected flags %#x " 2308900f4b8SKenneth D. Merry "refcount %d\n", periph->periph_name, 2318900f4b8SKenneth D. Merry periph->unit_number, periph->flags, 2328900f4b8SKenneth D. Merry periph->refcount); 2338b8a9b1dSJustin T. Gibbs } 2348b8a9b1dSJustin T. Gibbs return (CAM_REQ_INVALID); 2358b8a9b1dSJustin T. Gibbs } 2368b8a9b1dSJustin T. Gibbs 237362abc44STai-hwa Liang periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH, 2388900f4b8SKenneth D. Merry M_NOWAIT|M_ZERO); 2398b8a9b1dSJustin T. Gibbs 2408b8a9b1dSJustin T. Gibbs if (periph == NULL) 2418b8a9b1dSJustin T. Gibbs return (CAM_RESRC_UNAVAIL); 2428b8a9b1dSJustin T. Gibbs 2438b8a9b1dSJustin T. Gibbs init_level++; 2448b8a9b1dSJustin T. Gibbs 2452b83592fSScott Long sim = xpt_path_sim(path); 2468b8a9b1dSJustin T. Gibbs path_id = xpt_path_path_id(path); 2478b8a9b1dSJustin T. Gibbs target_id = xpt_path_target_id(path); 2488b8a9b1dSJustin T. Gibbs lun_id = xpt_path_lun_id(path); 2498b8a9b1dSJustin T. Gibbs periph->periph_start = periph_start; 2508b8a9b1dSJustin T. Gibbs periph->periph_dtor = periph_dtor; 251ee9c90c7SKenneth D. Merry periph->periph_oninval = periph_oninvalidate; 2528b8a9b1dSJustin T. Gibbs periph->type = type; 2538b8a9b1dSJustin T. Gibbs periph->periph_name = name; 254227d67aaSAlexander Motin periph->scheduled_priority = CAM_PRIORITY_NONE; 2558b8a9b1dSJustin T. Gibbs periph->immediate_priority = CAM_PRIORITY_NONE; 256d38c0e53SAlexander Motin periph->refcount = 1; /* Dropped by invalidation. */ 2572b83592fSScott Long periph->sim = sim; 2588b8a9b1dSJustin T. Gibbs SLIST_INIT(&periph->ccb_list); 2598b8a9b1dSJustin T. Gibbs status = xpt_create_path(&path, periph, path_id, target_id, lun_id); 2608b8a9b1dSJustin T. Gibbs if (status != CAM_REQ_CMP) 2618b8a9b1dSJustin T. Gibbs goto failure; 2628b8a9b1dSJustin T. Gibbs periph->path = path; 2638b8a9b1dSJustin T. Gibbs 264f1e2546aSMatt Jacob xpt_lock_buses(); 265f1e2546aSMatt Jacob for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 266f1e2546aSMatt Jacob if (strcmp((*p_drv)->driver_name, name) == 0) 267f1e2546aSMatt Jacob break; 268f1e2546aSMatt Jacob } 269f1e2546aSMatt Jacob if (*p_drv == NULL) { 270f1e2546aSMatt Jacob printf("cam_periph_alloc: invalid periph name '%s'\n", name); 2719e0d30e2SEdward Tomasz Napierala xpt_unlock_buses(); 272f1e2546aSMatt Jacob xpt_free_path(periph->path); 273f1e2546aSMatt Jacob free(periph, M_CAMPERIPH); 274f1e2546aSMatt Jacob return (CAM_REQ_INVALID); 275f1e2546aSMatt Jacob } 276f1e2546aSMatt Jacob periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id); 2778b8a9b1dSJustin T. Gibbs cur_periph = TAILQ_FIRST(&(*p_drv)->units); 2788b8a9b1dSJustin T. Gibbs while (cur_periph != NULL 2798b8a9b1dSJustin T. Gibbs && cur_periph->unit_number < periph->unit_number) 2808b8a9b1dSJustin T. Gibbs cur_periph = TAILQ_NEXT(cur_periph, unit_links); 281f1e2546aSMatt Jacob if (cur_periph != NULL) { 282dbfe5dd3SWarner Losh KASSERT(cur_periph->unit_number != periph->unit_number, 283dbfe5dd3SWarner Losh ("duplicate units on periph list")); 2848b8a9b1dSJustin T. Gibbs TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links); 285f1e2546aSMatt Jacob } else { 2868b8a9b1dSJustin T. Gibbs TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links); 2878b8a9b1dSJustin T. Gibbs (*p_drv)->generation++; 2888b8a9b1dSJustin T. Gibbs } 289f1e2546aSMatt Jacob xpt_unlock_buses(); 290f1e2546aSMatt Jacob 291f1e2546aSMatt Jacob init_level++; 292f1e2546aSMatt Jacob 293f1e2546aSMatt Jacob status = xpt_add_periph(periph); 294f1e2546aSMatt Jacob if (status != CAM_REQ_CMP) 295f1e2546aSMatt Jacob goto failure; 2968b8a9b1dSJustin T. Gibbs 2978b8a9b1dSJustin T. Gibbs init_level++; 29822c7d606SAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n")); 2998b8a9b1dSJustin T. Gibbs 3008b8a9b1dSJustin T. Gibbs status = periph_ctor(periph, arg); 3018b8a9b1dSJustin T. Gibbs 3028b8a9b1dSJustin T. Gibbs if (status == CAM_REQ_CMP) 3038b8a9b1dSJustin T. Gibbs init_level++; 3048b8a9b1dSJustin T. Gibbs 3058b8a9b1dSJustin T. Gibbs failure: 3068b8a9b1dSJustin T. Gibbs switch (init_level) { 3078b8a9b1dSJustin T. Gibbs case 4: 3088b8a9b1dSJustin T. Gibbs /* Initialized successfully */ 3098b8a9b1dSJustin T. Gibbs break; 3108b8a9b1dSJustin T. Gibbs case 3: 31122c7d606SAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n")); 312a29779e8SAlexander Motin xpt_remove_periph(periph); 3137379c88fSPoul-Henning Kamp /* FALLTHROUGH */ 3148b8a9b1dSJustin T. Gibbs case 2: 315f1e2546aSMatt Jacob xpt_lock_buses(); 316f1e2546aSMatt Jacob TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links); 317f1e2546aSMatt Jacob xpt_unlock_buses(); 3188b8a9b1dSJustin T. Gibbs xpt_free_path(periph->path); 3197379c88fSPoul-Henning Kamp /* FALLTHROUGH */ 3208b8a9b1dSJustin T. Gibbs case 1: 321362abc44STai-hwa Liang free(periph, M_CAMPERIPH); 3227379c88fSPoul-Henning Kamp /* FALLTHROUGH */ 3238b8a9b1dSJustin T. Gibbs case 0: 3248b8a9b1dSJustin T. Gibbs /* No cleanup to perform. */ 3258b8a9b1dSJustin T. Gibbs break; 3268b8a9b1dSJustin T. Gibbs default: 327905cb85dSKenneth D. Merry panic("%s: Unknown init level", __func__); 3288b8a9b1dSJustin T. Gibbs } 3298b8a9b1dSJustin T. Gibbs return(status); 3308b8a9b1dSJustin T. Gibbs } 3318b8a9b1dSJustin T. Gibbs 3328b8a9b1dSJustin T. Gibbs /* 3338b8a9b1dSJustin T. Gibbs * Find a peripheral structure with the specified path, target, lun, 3348b8a9b1dSJustin T. Gibbs * and (optionally) type. If the name is NULL, this function will return 3358b8a9b1dSJustin T. Gibbs * the first peripheral driver that matches the specified path. 3368b8a9b1dSJustin T. Gibbs */ 3378b8a9b1dSJustin T. Gibbs struct cam_periph * 3388b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name) 3398b8a9b1dSJustin T. Gibbs { 3408b8a9b1dSJustin T. Gibbs struct periph_driver **p_drv; 3418b8a9b1dSJustin T. Gibbs struct cam_periph *periph; 3428b8a9b1dSJustin T. Gibbs 34377dc25ccSScott Long xpt_lock_buses(); 3440b7c27b9SPeter Wemm for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 3458b8a9b1dSJustin T. Gibbs if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0)) 3468b8a9b1dSJustin T. Gibbs continue; 3478b8a9b1dSJustin T. Gibbs 34837d40066SPoul-Henning Kamp TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) { 3498b8a9b1dSJustin T. Gibbs if (xpt_path_comp(periph->path, path) == 0) { 35077dc25ccSScott Long xpt_unlock_buses(); 351227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 3528b8a9b1dSJustin T. Gibbs return(periph); 3538b8a9b1dSJustin T. Gibbs } 3548b8a9b1dSJustin T. Gibbs } 35577dc25ccSScott Long if (name != NULL) { 35677dc25ccSScott Long xpt_unlock_buses(); 3578b8a9b1dSJustin T. Gibbs return(NULL); 3588b8a9b1dSJustin T. Gibbs } 35977dc25ccSScott Long } 36077dc25ccSScott Long xpt_unlock_buses(); 3618b8a9b1dSJustin T. Gibbs return(NULL); 3628b8a9b1dSJustin T. Gibbs } 3638b8a9b1dSJustin T. Gibbs 3643501942bSJustin T. Gibbs /* 3658900f4b8SKenneth D. Merry * Find peripheral driver instances attached to the specified path. 3663501942bSJustin T. Gibbs */ 3673501942bSJustin T. Gibbs int 3683501942bSJustin T. Gibbs cam_periph_list(struct cam_path *path, struct sbuf *sb) 3693501942bSJustin T. Gibbs { 3708900f4b8SKenneth D. Merry struct sbuf local_sb; 3713501942bSJustin T. Gibbs struct periph_driver **p_drv; 3723501942bSJustin T. Gibbs struct cam_periph *periph; 3733501942bSJustin T. Gibbs int count; 3748900f4b8SKenneth D. Merry int sbuf_alloc_len; 3753501942bSJustin T. Gibbs 3768900f4b8SKenneth D. Merry sbuf_alloc_len = 16; 3778900f4b8SKenneth D. Merry retry: 3788900f4b8SKenneth D. Merry sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN); 3793501942bSJustin T. Gibbs count = 0; 3803501942bSJustin T. Gibbs xpt_lock_buses(); 3813501942bSJustin T. Gibbs for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 3823501942bSJustin T. Gibbs TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) { 3833501942bSJustin T. Gibbs if (xpt_path_comp(periph->path, path) != 0) 3843501942bSJustin T. Gibbs continue; 3853501942bSJustin T. Gibbs 3868900f4b8SKenneth D. Merry if (sbuf_len(&local_sb) != 0) 3878900f4b8SKenneth D. Merry sbuf_cat(&local_sb, ","); 3883501942bSJustin T. Gibbs 3898900f4b8SKenneth D. Merry sbuf_printf(&local_sb, "%s%d", periph->periph_name, 3903501942bSJustin T. Gibbs periph->unit_number); 3918900f4b8SKenneth D. Merry 3928900f4b8SKenneth D. Merry if (sbuf_error(&local_sb) == ENOMEM) { 3938900f4b8SKenneth D. Merry sbuf_alloc_len *= 2; 3948900f4b8SKenneth D. Merry xpt_unlock_buses(); 3958900f4b8SKenneth D. Merry sbuf_delete(&local_sb); 3968900f4b8SKenneth D. Merry goto retry; 3978900f4b8SKenneth D. Merry } 3983501942bSJustin T. Gibbs count++; 3993501942bSJustin T. Gibbs } 4003501942bSJustin T. Gibbs } 4013501942bSJustin T. Gibbs xpt_unlock_buses(); 4028900f4b8SKenneth D. Merry sbuf_finish(&local_sb); 40307f7e4c8SAlexander Motin if (sbuf_len(sb) != 0) 40407f7e4c8SAlexander Motin sbuf_cat(sb, ","); 40507f7e4c8SAlexander Motin sbuf_cat(sb, sbuf_data(&local_sb)); 4068900f4b8SKenneth D. Merry sbuf_delete(&local_sb); 4073501942bSJustin T. Gibbs return (count); 4083501942bSJustin T. Gibbs } 4093501942bSJustin T. Gibbs 41099e7a4adSScott Long int 4118b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph) 4128b8a9b1dSJustin T. Gibbs { 41399e7a4adSScott Long int status; 4148b8a9b1dSJustin T. Gibbs 4158b8a9b1dSJustin T. Gibbs if (periph == NULL) 41699e7a4adSScott Long return (EINVAL); 4178b8a9b1dSJustin T. Gibbs 41899e7a4adSScott Long status = ENOENT; 4192b83592fSScott Long xpt_lock_buses(); 4208900f4b8SKenneth D. Merry if ((periph->flags & CAM_PERIPH_INVALID) == 0) { 4218b8a9b1dSJustin T. Gibbs periph->refcount++; 42299e7a4adSScott Long status = 0; 4238900f4b8SKenneth D. Merry } 4242b83592fSScott Long xpt_unlock_buses(); 4258b8a9b1dSJustin T. Gibbs 4268900f4b8SKenneth D. Merry return (status); 4278900f4b8SKenneth D. Merry } 4288900f4b8SKenneth D. Merry 4298900f4b8SKenneth D. Merry void 430c33e4029SAlexander Motin cam_periph_doacquire(struct cam_periph *periph) 431c33e4029SAlexander Motin { 432c33e4029SAlexander Motin 433c33e4029SAlexander Motin xpt_lock_buses(); 434c33e4029SAlexander Motin KASSERT(periph->refcount >= 1, 435c33e4029SAlexander Motin ("cam_periph_doacquire() with refcount == %d", periph->refcount)); 436c33e4029SAlexander Motin periph->refcount++; 437c33e4029SAlexander Motin xpt_unlock_buses(); 438c33e4029SAlexander Motin } 439c33e4029SAlexander Motin 440c33e4029SAlexander Motin void 4418900f4b8SKenneth D. Merry cam_periph_release_locked_buses(struct cam_periph *periph) 4428900f4b8SKenneth D. Merry { 443dcdf6e74SAlexander Motin 444227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 445dcdf6e74SAlexander Motin KASSERT(periph->refcount >= 1, ("periph->refcount >= 1")); 446d38c0e53SAlexander Motin if (--periph->refcount == 0) 4478900f4b8SKenneth D. Merry camperiphfree(periph); 4488900f4b8SKenneth D. Merry } 4498b8a9b1dSJustin T. Gibbs 4508b8a9b1dSJustin T. Gibbs void 45124ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph) 4528b8a9b1dSJustin T. Gibbs { 4538b8a9b1dSJustin T. Gibbs 4548b8a9b1dSJustin T. Gibbs if (periph == NULL) 4558b8a9b1dSJustin T. Gibbs return; 4568b8a9b1dSJustin T. Gibbs 4572b83592fSScott Long xpt_lock_buses(); 4588900f4b8SKenneth D. Merry cam_periph_release_locked_buses(periph); 4592b83592fSScott Long xpt_unlock_buses(); 46024ebf566SEdward Tomasz Napierala } 4618b8a9b1dSJustin T. Gibbs 46224ebf566SEdward Tomasz Napierala void 46324ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph) 46424ebf566SEdward Tomasz Napierala { 465227d67aaSAlexander Motin struct mtx *mtx; 46624ebf566SEdward Tomasz Napierala 46724ebf566SEdward Tomasz Napierala if (periph == NULL) 46824ebf566SEdward Tomasz Napierala return; 46924ebf566SEdward Tomasz Napierala 470227d67aaSAlexander Motin cam_periph_assert(periph, MA_NOTOWNED); 471227d67aaSAlexander Motin mtx = cam_periph_mtx(periph); 472227d67aaSAlexander Motin mtx_lock(mtx); 47324ebf566SEdward Tomasz Napierala cam_periph_release_locked(periph); 474227d67aaSAlexander Motin mtx_unlock(mtx); 4758b8a9b1dSJustin T. Gibbs } 4768b8a9b1dSJustin T. Gibbs 4779385e92bSWarner Losh /* 4789385e92bSWarner Losh * hold/unhold act as mutual exclusion for sections of the code that 4799385e92bSWarner Losh * need to sleep and want to make sure that other sections that 4809385e92bSWarner Losh * will interfere are held off. This only protects exclusive sections 4819385e92bSWarner Losh * from each other. 4829385e92bSWarner Losh */ 4832b83592fSScott Long int 4842b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority) 4852b83592fSScott Long { 4862b83592fSScott Long int error; 4872b83592fSScott Long 4882b83592fSScott Long /* 4892b83592fSScott Long * Increment the reference count on the peripheral 4902b83592fSScott Long * while we wait for our lock attempt to succeed 4912b83592fSScott Long * to ensure the peripheral doesn't disappear out 4922b83592fSScott Long * from user us while we sleep. 4932b83592fSScott Long */ 4942b83592fSScott Long 49599e7a4adSScott Long if (cam_periph_acquire(periph) != 0) 4962b83592fSScott Long return (ENXIO); 4972b83592fSScott Long 498227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 4992b83592fSScott Long while ((periph->flags & CAM_PERIPH_LOCKED) != 0) { 5002b83592fSScott Long periph->flags |= CAM_PERIPH_LOCK_WANTED; 501227d67aaSAlexander Motin if ((error = cam_periph_sleep(periph, periph, priority, 5021fa738c2SJohn Baldwin "caplck", 0)) != 0) { 50324ebf566SEdward Tomasz Napierala cam_periph_release_locked(periph); 5042b83592fSScott Long return (error); 5052b83592fSScott Long } 506aed9c88cSAlexander Motin if (periph->flags & CAM_PERIPH_INVALID) { 507aed9c88cSAlexander Motin cam_periph_release_locked(periph); 508aed9c88cSAlexander Motin return (ENXIO); 509aed9c88cSAlexander Motin } 5102b83592fSScott Long } 5112b83592fSScott Long 5122b83592fSScott Long periph->flags |= CAM_PERIPH_LOCKED; 5132b83592fSScott Long return (0); 5142b83592fSScott Long } 5152b83592fSScott Long 5162b83592fSScott Long void 5172b83592fSScott Long cam_periph_unhold(struct cam_periph *periph) 5182b83592fSScott Long { 5192b83592fSScott Long 520227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 5212b83592fSScott Long 5222b83592fSScott Long periph->flags &= ~CAM_PERIPH_LOCKED; 5232b83592fSScott Long if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) { 5242b83592fSScott Long periph->flags &= ~CAM_PERIPH_LOCK_WANTED; 5252b83592fSScott Long wakeup(periph); 5262b83592fSScott Long } 5272b83592fSScott Long 52824ebf566SEdward Tomasz Napierala cam_periph_release_locked(periph); 5292b83592fSScott Long } 5302b83592fSScott Long 5318b8a9b1dSJustin T. Gibbs /* 5328b8a9b1dSJustin T. Gibbs * Look for the next unit number that is not currently in use for this 5338b8a9b1dSJustin T. Gibbs * peripheral type starting at "newunit". Also exclude unit numbers that 5348b8a9b1dSJustin T. Gibbs * are reserved by for future "hardwiring" unless we already know that this 5358b8a9b1dSJustin T. Gibbs * is a potential wired device. Only assume that the device is "wired" the 5368b8a9b1dSJustin T. Gibbs * first time through the loop since after that we'll be looking at unit 5378b8a9b1dSJustin T. Gibbs * numbers that did not match a wiring entry. 5388b8a9b1dSJustin T. Gibbs */ 5398b8a9b1dSJustin T. Gibbs static u_int 540501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired, 541501468a5SKenneth D. Merry path_id_t pathid, target_id_t target, lun_id_t lun) 5428b8a9b1dSJustin T. Gibbs { 5438b8a9b1dSJustin T. Gibbs struct cam_periph *periph; 5442398f0cdSPeter Wemm char *periph_name; 5452398f0cdSPeter Wemm int i, val, dunit, r; 5462398f0cdSPeter Wemm const char *dname, *strval; 5478b8a9b1dSJustin T. Gibbs 5488b8a9b1dSJustin T. Gibbs periph_name = p_drv->driver_name; 5498b8a9b1dSJustin T. Gibbs for (;;newunit++) { 5508b8a9b1dSJustin T. Gibbs for (periph = TAILQ_FIRST(&p_drv->units); 5518b8a9b1dSJustin T. Gibbs periph != NULL && periph->unit_number != newunit; 5528b8a9b1dSJustin T. Gibbs periph = TAILQ_NEXT(periph, unit_links)) 5538b8a9b1dSJustin T. Gibbs ; 5548b8a9b1dSJustin T. Gibbs 5558b8a9b1dSJustin T. Gibbs if (periph != NULL && periph->unit_number == newunit) { 5568b8a9b1dSJustin T. Gibbs if (wired != 0) { 557f0d9af51SMatt Jacob xpt_print(periph->path, "Duplicate Wired " 558f0d9af51SMatt Jacob "Device entry!\n"); 559f0d9af51SMatt Jacob xpt_print(periph->path, "Second device (%s " 560f0d9af51SMatt Jacob "device at scbus%d target %d lun %d) will " 561f0d9af51SMatt Jacob "not be wired\n", periph_name, pathid, 562f0d9af51SMatt Jacob target, lun); 5638b8a9b1dSJustin T. Gibbs wired = 0; 5648b8a9b1dSJustin T. Gibbs } 5658b8a9b1dSJustin T. Gibbs continue; 5668b8a9b1dSJustin T. Gibbs } 56775f51904SPeter Wemm if (wired) 56875f51904SPeter Wemm break; 5698b8a9b1dSJustin T. Gibbs 5708b8a9b1dSJustin T. Gibbs /* 57100f79c97SWarner Losh * Don't allow the mere presence of any attributes of a device 57200f79c97SWarner Losh * means that it is for a wired down entry. Instead, insist that 57300f79c97SWarner Losh * one of the matching criteria from camperiphunit be present 57400f79c97SWarner Losh * for the device. 5758b8a9b1dSJustin T. Gibbs */ 5762398f0cdSPeter Wemm i = 0; 5772398f0cdSPeter Wemm dname = periph_name; 5782398f0cdSPeter Wemm for (;;) { 5792398f0cdSPeter Wemm r = resource_find_dev(&i, dname, &dunit, NULL, NULL); 5802398f0cdSPeter Wemm if (r != 0) 5812398f0cdSPeter Wemm break; 582bee0133fSWarner Losh 583bee0133fSWarner Losh if (newunit != dunit) 58475f51904SPeter Wemm continue; 585*710a519eSWarner Losh if (resource_int_value(dname, dunit, "lun", &val) == 0 || 586*710a519eSWarner Losh resource_int_value(dname, dunit, "target", &val) == 0 || 587bee0133fSWarner Losh resource_string_value(dname, dunit, "at", &strval) == 0) 5888b8a9b1dSJustin T. Gibbs break; 5898b8a9b1dSJustin T. Gibbs } 5902398f0cdSPeter Wemm if (r != 0) 5918b8a9b1dSJustin T. Gibbs break; 5928b8a9b1dSJustin T. Gibbs } 5938b8a9b1dSJustin T. Gibbs return (newunit); 5948b8a9b1dSJustin T. Gibbs } 5958b8a9b1dSJustin T. Gibbs 5968b8a9b1dSJustin T. Gibbs static u_int 5978b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid, 5988b8a9b1dSJustin T. Gibbs target_id_t target, lun_id_t lun) 5998b8a9b1dSJustin T. Gibbs { 6008b8a9b1dSJustin T. Gibbs u_int unit; 601c1b81613SPeter Wemm int wired, i, val, dunit; 6022398f0cdSPeter Wemm const char *dname, *strval; 6032398f0cdSPeter Wemm char pathbuf[32], *periph_name; 6048b8a9b1dSJustin T. Gibbs 60575f51904SPeter Wemm periph_name = p_drv->driver_name; 60675f51904SPeter Wemm snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid); 607c1b81613SPeter Wemm unit = 0; 6082398f0cdSPeter Wemm i = 0; 6092398f0cdSPeter Wemm dname = periph_name; 610c1b81613SPeter Wemm for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0; 611c1b81613SPeter Wemm wired = 0) { 61275f51904SPeter Wemm if (resource_string_value(dname, dunit, "at", &strval) == 0) { 61375f51904SPeter Wemm if (strcmp(strval, pathbuf) != 0) 6148b8a9b1dSJustin T. Gibbs continue; 615c1b81613SPeter Wemm wired++; 6168b8a9b1dSJustin T. Gibbs } 61775f51904SPeter Wemm if (resource_int_value(dname, dunit, "target", &val) == 0) { 61875f51904SPeter Wemm if (val != target) 6198b8a9b1dSJustin T. Gibbs continue; 620c1b81613SPeter Wemm wired++; 6218b8a9b1dSJustin T. Gibbs } 62275f51904SPeter Wemm if (resource_int_value(dname, dunit, "lun", &val) == 0) { 62375f51904SPeter Wemm if (val != lun) 6248b8a9b1dSJustin T. Gibbs continue; 625c1b81613SPeter Wemm wired++; 6268b8a9b1dSJustin T. Gibbs } 627c1b81613SPeter Wemm if (wired != 0) { 62875f51904SPeter Wemm unit = dunit; 6298b8a9b1dSJustin T. Gibbs break; 6308b8a9b1dSJustin T. Gibbs } 6318b8a9b1dSJustin T. Gibbs } 6328b8a9b1dSJustin T. Gibbs 6338b8a9b1dSJustin T. Gibbs /* 6348b8a9b1dSJustin T. Gibbs * Either start from 0 looking for the next unit or from 63575f51904SPeter Wemm * the unit number given in the resource config. This way, 6368b8a9b1dSJustin T. Gibbs * if we have wildcard matches, we don't return the same 6378b8a9b1dSJustin T. Gibbs * unit number twice. 6388b8a9b1dSJustin T. Gibbs */ 639c1b81613SPeter Wemm unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun); 6408b8a9b1dSJustin T. Gibbs 6418b8a9b1dSJustin T. Gibbs return (unit); 6428b8a9b1dSJustin T. Gibbs } 6438b8a9b1dSJustin T. Gibbs 6448b8a9b1dSJustin T. Gibbs void 6458b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph) 6468b8a9b1dSJustin T. Gibbs { 6478b8a9b1dSJustin T. Gibbs 648227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 649ee9c90c7SKenneth D. Merry /* 6506fda2c54SWarner Losh * We only tear down the device the first time a peripheral is 65177dc25ccSScott Long * invalidated. 652ee9c90c7SKenneth D. Merry */ 653d38c0e53SAlexander Motin if ((periph->flags & CAM_PERIPH_INVALID) != 0) 654d38c0e53SAlexander Motin return; 655ee9c90c7SKenneth D. Merry 656d38c0e53SAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n")); 6575d01277fSScott Long if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) { 6585d01277fSScott Long struct sbuf sb; 659da0d7209SScott Long char buffer[160]; 6605d01277fSScott Long 661da0d7209SScott Long sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN); 6625d01277fSScott Long xpt_denounce_periph_sbuf(periph, &sb); 6635d01277fSScott Long sbuf_finish(&sb); 6645d01277fSScott Long sbuf_putbuf(&sb); 6655d01277fSScott Long } 6668b8a9b1dSJustin T. Gibbs periph->flags |= CAM_PERIPH_INVALID; 6678b8a9b1dSJustin T. Gibbs periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND; 668d38c0e53SAlexander Motin if (periph->periph_oninval != NULL) 669d38c0e53SAlexander Motin periph->periph_oninval(periph); 670d38c0e53SAlexander Motin cam_periph_release_locked(periph); 6718b8a9b1dSJustin T. Gibbs } 6728b8a9b1dSJustin T. Gibbs 6738b8a9b1dSJustin T. Gibbs static void 6748b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph) 6758b8a9b1dSJustin T. Gibbs { 6768b8a9b1dSJustin T. Gibbs struct periph_driver **p_drv; 67759fe7664SKenneth D. Merry struct periph_driver *drv; 6788b8a9b1dSJustin T. Gibbs 679227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 680227d67aaSAlexander Motin KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating", 681227d67aaSAlexander Motin periph->periph_name, periph->unit_number)); 6820b7c27b9SPeter Wemm for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 6838b8a9b1dSJustin T. Gibbs if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0) 6848b8a9b1dSJustin T. Gibbs break; 6858b8a9b1dSJustin T. Gibbs } 686661658a6SScott Long if (*p_drv == NULL) { 687661658a6SScott Long printf("camperiphfree: attempt to free non-existant periph\n"); 688661658a6SScott Long return; 689661658a6SScott Long } 69059fe7664SKenneth D. Merry /* 69159fe7664SKenneth D. Merry * Cache a pointer to the periph_driver structure. If a 69259fe7664SKenneth D. Merry * periph_driver is added or removed from the array (see 69359fe7664SKenneth D. Merry * periphdriver_register()) while we drop the toplogy lock 69459fe7664SKenneth D. Merry * below, p_drv may change. This doesn't protect against this 69559fe7664SKenneth D. Merry * particular periph_driver going away. That will require full 69659fe7664SKenneth D. Merry * reference counting in the periph_driver infrastructure. 69759fe7664SKenneth D. Merry */ 69859fe7664SKenneth D. Merry drv = *p_drv; 6998b8a9b1dSJustin T. Gibbs 700ea37f519SKenneth D. Merry /* 70133a38f74SKenneth D. Merry * We need to set this flag before dropping the topology lock, to 70233a38f74SKenneth D. Merry * let anyone who is traversing the list that this peripheral is 70333a38f74SKenneth D. Merry * about to be freed, and there will be no more reference count 70433a38f74SKenneth D. Merry * checks. 70533a38f74SKenneth D. Merry */ 70633a38f74SKenneth D. Merry periph->flags |= CAM_PERIPH_FREE; 70733a38f74SKenneth D. Merry 70833a38f74SKenneth D. Merry /* 709ea37f519SKenneth D. Merry * The peripheral destructor semantics dictate calling with only the 710ea37f519SKenneth D. Merry * SIM mutex held. Since it might sleep, it should not be called 711ea37f519SKenneth D. Merry * with the topology lock held. 712ea37f519SKenneth D. Merry */ 713f3548746SScott Long xpt_unlock_buses(); 7148b8a9b1dSJustin T. Gibbs 715ea37f519SKenneth D. Merry /* 716ea37f519SKenneth D. Merry * We need to call the peripheral destructor prior to removing the 717ea37f519SKenneth D. Merry * peripheral from the list. Otherwise, we risk running into a 718ea37f519SKenneth D. Merry * scenario where the peripheral unit number may get reused 719ea37f519SKenneth D. Merry * (because it has been removed from the list), but some resources 720ea37f519SKenneth D. Merry * used by the peripheral are still hanging around. In particular, 721ea37f519SKenneth D. Merry * the devfs nodes used by some peripherals like the pass(4) driver 722ea37f519SKenneth D. Merry * aren't fully cleaned up until the destructor is run. If the 723ea37f519SKenneth D. Merry * unit number is reused before the devfs instance is fully gone, 724ea37f519SKenneth D. Merry * devfs will panic. 725ea37f519SKenneth D. Merry */ 726f3548746SScott Long if (periph->periph_dtor != NULL) 727f3548746SScott Long periph->periph_dtor(periph); 728ea37f519SKenneth D. Merry 729ea37f519SKenneth D. Merry /* 73081490edaSWarner Losh * The peripheral list is protected by the topology lock. We have to 73181490edaSWarner Losh * remove the periph from the drv list before we call deferred_ac. The 73281490edaSWarner Losh * AC_FOUND_DEVICE callback won't create a new periph if it's still there. 733ea37f519SKenneth D. Merry */ 734ea37f519SKenneth D. Merry xpt_lock_buses(); 735ea37f519SKenneth D. Merry 73659fe7664SKenneth D. Merry TAILQ_REMOVE(&drv->units, periph, unit_links); 73759fe7664SKenneth D. Merry drv->generation++; 738ea37f519SKenneth D. Merry 739a29779e8SAlexander Motin xpt_remove_periph(periph); 740ea37f519SKenneth D. Merry 741ea37f519SKenneth D. Merry xpt_unlock_buses(); 742de6a705eSMarius Strobl if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) 7438d36a71bSAlexander Motin xpt_print(periph->path, "Periph destroyed\n"); 744de6a705eSMarius Strobl else 74522c7d606SAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n")); 7468b8a9b1dSJustin T. Gibbs 7478b8a9b1dSJustin T. Gibbs if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) { 7488b8a9b1dSJustin T. Gibbs union ccb ccb; 7498b8a9b1dSJustin T. Gibbs void *arg; 7508b8a9b1dSJustin T. Gibbs 751076686feSEdward Tomasz Napierala memset(&ccb, 0, sizeof(ccb)); 7528b8a9b1dSJustin T. Gibbs switch (periph->deferred_ac) { 7538b8a9b1dSJustin T. Gibbs case AC_FOUND_DEVICE: 7548b8a9b1dSJustin T. Gibbs ccb.ccb_h.func_code = XPT_GDEV_TYPE; 755bbfa4aa1SAlexander Motin xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 7568b8a9b1dSJustin T. Gibbs xpt_action(&ccb); 7578b8a9b1dSJustin T. Gibbs arg = &ccb; 7588b8a9b1dSJustin T. Gibbs break; 7598b8a9b1dSJustin T. Gibbs case AC_PATH_REGISTERED: 760762a7f4fSWarner Losh xpt_path_inq(&ccb.cpi, periph->path); 7618b8a9b1dSJustin T. Gibbs arg = &ccb; 7628b8a9b1dSJustin T. Gibbs break; 7638b8a9b1dSJustin T. Gibbs default: 7648b8a9b1dSJustin T. Gibbs arg = NULL; 7658b8a9b1dSJustin T. Gibbs break; 7668b8a9b1dSJustin T. Gibbs } 7678b8a9b1dSJustin T. Gibbs periph->deferred_callback(NULL, periph->deferred_ac, 7688b8a9b1dSJustin T. Gibbs periph->path, arg); 7698b8a9b1dSJustin T. Gibbs } 7708b8a9b1dSJustin T. Gibbs xpt_free_path(periph->path); 771362abc44STai-hwa Liang free(periph, M_CAMPERIPH); 772f3548746SScott Long xpt_lock_buses(); 7738b8a9b1dSJustin T. Gibbs } 7748b8a9b1dSJustin T. Gibbs 7758b8a9b1dSJustin T. Gibbs /* 7768b8a9b1dSJustin T. Gibbs * Map user virtual pointers into kernel virtual address space, so we can 77795fbded6SScott Long * access the memory. This is now a generic function that centralizes most 77895fbded6SScott Long * of the sanity checks on the data flags, if any. 779cd853791SKonstantin Belousov * This also only works for up to maxphys memory. Since we use 7808b8a9b1dSJustin T. Gibbs * buffers to map stuff in and out, we're limited to the buffer size. 7818b8a9b1dSJustin T. Gibbs */ 7828b8a9b1dSJustin T. Gibbs int 783de239312SAlexander Motin cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo, 784de239312SAlexander Motin u_int maxmap) 7858b8a9b1dSJustin T. Gibbs { 786b059686aSAlexander Motin int numbufs, i; 7878b8a9b1dSJustin T. Gibbs u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 7888b8a9b1dSJustin T. Gibbs u_int32_t lengths[CAM_PERIPH_MAXMAPS]; 7898b8a9b1dSJustin T. Gibbs u_int32_t dirs[CAM_PERIPH_MAXMAPS]; 7908b8a9b1dSJustin T. Gibbs 791b059686aSAlexander Motin bzero(mapinfo, sizeof(*mapinfo)); 792de239312SAlexander Motin if (maxmap == 0) 793de239312SAlexander Motin maxmap = DFLTPHYS; /* traditional default */ 794cd853791SKonstantin Belousov else if (maxmap > maxphys) 795cd853791SKonstantin Belousov maxmap = maxphys; /* for safety */ 7968b8a9b1dSJustin T. Gibbs switch(ccb->ccb_h.func_code) { 7978b8a9b1dSJustin T. Gibbs case XPT_DEV_MATCH: 7988b8a9b1dSJustin T. Gibbs if (ccb->cdm.match_buf_len == 0) { 7998b8a9b1dSJustin T. Gibbs printf("cam_periph_mapmem: invalid match buffer " 8008b8a9b1dSJustin T. Gibbs "length 0\n"); 8018b8a9b1dSJustin T. Gibbs return(EINVAL); 8028b8a9b1dSJustin T. Gibbs } 8038b8a9b1dSJustin T. Gibbs if (ccb->cdm.pattern_buf_len > 0) { 8048b8a9b1dSJustin T. Gibbs data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 8058b8a9b1dSJustin T. Gibbs lengths[0] = ccb->cdm.pattern_buf_len; 8068b8a9b1dSJustin T. Gibbs dirs[0] = CAM_DIR_OUT; 8078b8a9b1dSJustin T. Gibbs data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 8088b8a9b1dSJustin T. Gibbs lengths[1] = ccb->cdm.match_buf_len; 8098b8a9b1dSJustin T. Gibbs dirs[1] = CAM_DIR_IN; 8108b8a9b1dSJustin T. Gibbs numbufs = 2; 8118b8a9b1dSJustin T. Gibbs } else { 8128b8a9b1dSJustin T. Gibbs data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 8138b8a9b1dSJustin T. Gibbs lengths[0] = ccb->cdm.match_buf_len; 8148b8a9b1dSJustin T. Gibbs dirs[0] = CAM_DIR_IN; 8158b8a9b1dSJustin T. Gibbs numbufs = 1; 8168b8a9b1dSJustin T. Gibbs } 81752c9ce25SScott Long /* 81852c9ce25SScott Long * This request will not go to the hardware, no reason 819cd853791SKonstantin Belousov * to be so strict. vmapbuf() is able to map up to maxphys. 82052c9ce25SScott Long */ 821cd853791SKonstantin Belousov maxmap = maxphys; 8228b8a9b1dSJustin T. Gibbs break; 8238b8a9b1dSJustin T. Gibbs case XPT_SCSI_IO: 82487cfaf0eSJustin T. Gibbs case XPT_CONT_TARGET_IO: 8258b8a9b1dSJustin T. Gibbs if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 8268b8a9b1dSJustin T. Gibbs return(0); 82795fbded6SScott Long if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 82895fbded6SScott Long return (EINVAL); 8298b8a9b1dSJustin T. Gibbs data_ptrs[0] = &ccb->csio.data_ptr; 83079d49a06SKenneth D. Merry lengths[0] = ccb->csio.dxfer_len; 8318b8a9b1dSJustin T. Gibbs dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 8328b8a9b1dSJustin T. Gibbs numbufs = 1; 8338b8a9b1dSJustin T. Gibbs break; 83452c9ce25SScott Long case XPT_ATA_IO: 83552c9ce25SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 83652c9ce25SScott Long return(0); 83795fbded6SScott Long if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 83895fbded6SScott Long return (EINVAL); 83952c9ce25SScott Long data_ptrs[0] = &ccb->ataio.data_ptr; 84052c9ce25SScott Long lengths[0] = ccb->ataio.dxfer_len; 84152c9ce25SScott Long dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 84252c9ce25SScott Long numbufs = 1; 84352c9ce25SScott Long break; 844a94a63f0SWarner Losh case XPT_MMC_IO: 845a94a63f0SWarner Losh if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 846a94a63f0SWarner Losh return(0); 847a94a63f0SWarner Losh /* Two mappings: one for cmd->data and one for cmd->data->data */ 848a94a63f0SWarner Losh data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data; 849a94a63f0SWarner Losh lengths[0] = sizeof(struct mmc_data *); 850a94a63f0SWarner Losh dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 851a94a63f0SWarner Losh data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data; 852a94a63f0SWarner Losh lengths[1] = ccb->mmcio.cmd.data->len; 853a94a63f0SWarner Losh dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK; 854a94a63f0SWarner Losh numbufs = 2; 855a94a63f0SWarner Losh break; 85606e79492SKenneth D. Merry case XPT_SMP_IO: 85706e79492SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 85806e79492SKenneth D. Merry lengths[0] = ccb->smpio.smp_request_len; 85906e79492SKenneth D. Merry dirs[0] = CAM_DIR_OUT; 86006e79492SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 86106e79492SKenneth D. Merry lengths[1] = ccb->smpio.smp_response_len; 86206e79492SKenneth D. Merry dirs[1] = CAM_DIR_IN; 86306e79492SKenneth D. Merry numbufs = 2; 86406e79492SKenneth D. Merry break; 865df424515SWarner Losh case XPT_NVME_IO: 866df424515SWarner Losh case XPT_NVME_ADMIN: 867df424515SWarner Losh if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 868df424515SWarner Losh return (0); 869df424515SWarner Losh if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 870df424515SWarner Losh return (EINVAL); 871df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 872df424515SWarner Losh lengths[0] = ccb->nvmeio.dxfer_len; 873df424515SWarner Losh dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 874df424515SWarner Losh numbufs = 1; 875df424515SWarner Losh break; 8763501942bSJustin T. Gibbs case XPT_DEV_ADVINFO: 8773501942bSJustin T. Gibbs if (ccb->cdai.bufsiz == 0) 8787c103ddeSKenneth D. Merry return (0); 8797c103ddeSKenneth D. Merry 8803501942bSJustin T. Gibbs data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 8813501942bSJustin T. Gibbs lengths[0] = ccb->cdai.bufsiz; 88206e79492SKenneth D. Merry dirs[0] = CAM_DIR_IN; 88306e79492SKenneth D. Merry numbufs = 1; 8847c103ddeSKenneth D. Merry 8857c103ddeSKenneth D. Merry /* 8867c103ddeSKenneth D. Merry * This request will not go to the hardware, no reason 887cd853791SKonstantin Belousov * to be so strict. vmapbuf() is able to map up to maxphys. 8887c103ddeSKenneth D. Merry */ 889cd853791SKonstantin Belousov maxmap = maxphys; 89006e79492SKenneth D. Merry break; 8918b8a9b1dSJustin T. Gibbs default: 8928b8a9b1dSJustin T. Gibbs return(EINVAL); 8938b8a9b1dSJustin T. Gibbs break; /* NOTREACHED */ 8948b8a9b1dSJustin T. Gibbs } 8958b8a9b1dSJustin T. Gibbs 8968b8a9b1dSJustin T. Gibbs /* 89779d49a06SKenneth D. Merry * Check the transfer length and permissions first, so we don't 89879d49a06SKenneth D. Merry * have to unmap any previously mapped buffers. 8998b8a9b1dSJustin T. Gibbs */ 9008b8a9b1dSJustin T. Gibbs for (i = 0; i < numbufs; i++) { 901c389a786SAlexander Motin if (lengths[i] > maxmap) { 902c389a786SAlexander Motin printf("cam_periph_mapmem: attempt to map %lu bytes, " 903c389a786SAlexander Motin "which is greater than %lu\n", 904c389a786SAlexander Motin (long)(lengths[i]), (u_long)maxmap); 905c389a786SAlexander Motin return (E2BIG); 906c389a786SAlexander Motin } 90779d49a06SKenneth D. Merry } 90879d49a06SKenneth D. Merry 90979d49a06SKenneth D. Merry /* 910cef367e6SEitan Adler * This keeps the kernel stack of current thread from getting 91195fbded6SScott Long * swapped. In low-memory situations where the kernel stack might 91295fbded6SScott Long * otherwise get swapped out, this holds it and allows the thread 91395fbded6SScott Long * to make progress and release the kernel mapped pages sooner. 91495fbded6SScott Long * 91579d49a06SKenneth D. Merry * XXX KDM should I use P_NOSWAP instead? 91679d49a06SKenneth D. Merry */ 9170cbbb7bfSPeter Wemm PHOLD(curproc); 91879d49a06SKenneth D. Merry 91979d49a06SKenneth D. Merry for (i = 0; i < numbufs; i++) { 920b059686aSAlexander Motin /* Save the user's data address. */ 921b059686aSAlexander Motin mapinfo->orig[i] = *data_ptrs[i]; 922b059686aSAlexander Motin 923b059686aSAlexander Motin /* 924b059686aSAlexander Motin * For small buffers use malloc+copyin/copyout instead of 925b059686aSAlexander Motin * mapping to KVA to avoid expensive TLB shootdowns. For 926b059686aSAlexander Motin * small allocations malloc is backed by UMA, and so much 927b059686aSAlexander Motin * cheaper on SMP systems. 928b059686aSAlexander Motin */ 9299093e27cSAlexander Motin if (lengths[i] <= periph_mapmem_thresh && 930b059686aSAlexander Motin ccb->ccb_h.func_code != XPT_MMC_IO) { 931b059686aSAlexander Motin *data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH, 932b059686aSAlexander Motin M_WAITOK); 933b059686aSAlexander Motin if (dirs[i] != CAM_DIR_IN) { 934b059686aSAlexander Motin if (copyin(mapinfo->orig[i], *data_ptrs[i], 935b059686aSAlexander Motin lengths[i]) != 0) { 936b059686aSAlexander Motin free(*data_ptrs[i], M_CAMPERIPH); 937b059686aSAlexander Motin *data_ptrs[i] = mapinfo->orig[i]; 938b059686aSAlexander Motin goto fail; 939b059686aSAlexander Motin } 940b059686aSAlexander Motin } else 941b059686aSAlexander Motin bzero(*data_ptrs[i], lengths[i]); 942b059686aSAlexander Motin continue; 943b059686aSAlexander Motin } 944b059686aSAlexander Motin 9458b8a9b1dSJustin T. Gibbs /* 9468b8a9b1dSJustin T. Gibbs * Get the buffer. 9478b8a9b1dSJustin T. Gibbs */ 948756a5412SGleb Smirnoff mapinfo->bp[i] = uma_zalloc(pbuf_zone, M_WAITOK); 9498b8a9b1dSJustin T. Gibbs 95021144e3bSPoul-Henning Kamp /* set the direction */ 951b059686aSAlexander Motin mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ? 952b059686aSAlexander Motin BIO_WRITE : BIO_READ; 9538b8a9b1dSJustin T. Gibbs 954450a2e2aSWarner Losh /* Map the buffer into kernel memory. */ 95544ca4575SBrooks Davis if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i], 1) < 0) { 956756a5412SGleb Smirnoff uma_zfree(pbuf_zone, mapinfo->bp[i]); 957b059686aSAlexander Motin goto fail; 9582d5c7e45SMatthew Dillon } 9598b8a9b1dSJustin T. Gibbs 9608b8a9b1dSJustin T. Gibbs /* set our pointer to the new mapped area */ 9618b8a9b1dSJustin T. Gibbs *data_ptrs[i] = mapinfo->bp[i]->b_data; 9628b8a9b1dSJustin T. Gibbs } 9638b8a9b1dSJustin T. Gibbs 964a62525f3SMatt Jacob /* 965a62525f3SMatt Jacob * Now that we've gotten this far, change ownership to the kernel 966a62525f3SMatt Jacob * of the buffers so that we don't run afoul of returning to user 967a62525f3SMatt Jacob * space with locks (on the buffer) held. 968a62525f3SMatt Jacob */ 969a62525f3SMatt Jacob for (i = 0; i < numbufs; i++) { 970b059686aSAlexander Motin if (mapinfo->bp[i]) 971a62525f3SMatt Jacob BUF_KERNPROC(mapinfo->bp[i]); 972a62525f3SMatt Jacob } 973a62525f3SMatt Jacob 974b059686aSAlexander Motin mapinfo->num_bufs_used = numbufs; 9758b8a9b1dSJustin T. Gibbs return(0); 976b059686aSAlexander Motin 977b059686aSAlexander Motin fail: 978b059686aSAlexander Motin for (i--; i >= 0; i--) { 979b059686aSAlexander Motin if (mapinfo->bp[i]) { 980b059686aSAlexander Motin vunmapbuf(mapinfo->bp[i]); 981b059686aSAlexander Motin uma_zfree(pbuf_zone, mapinfo->bp[i]); 982b059686aSAlexander Motin } else 983b059686aSAlexander Motin free(*data_ptrs[i], M_CAMPERIPH); 984b059686aSAlexander Motin *data_ptrs[i] = mapinfo->orig[i]; 985b059686aSAlexander Motin } 986b059686aSAlexander Motin PRELE(curproc); 987b059686aSAlexander Motin return(EACCES); 9888b8a9b1dSJustin T. Gibbs } 9898b8a9b1dSJustin T. Gibbs 9908b8a9b1dSJustin T. Gibbs /* 9918b8a9b1dSJustin T. Gibbs * Unmap memory segments mapped into kernel virtual address space by 9928b8a9b1dSJustin T. Gibbs * cam_periph_mapmem(). 9938b8a9b1dSJustin T. Gibbs */ 9948b8a9b1dSJustin T. Gibbs void 9958b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo) 9968b8a9b1dSJustin T. Gibbs { 9978b8a9b1dSJustin T. Gibbs int numbufs, i; 9988b8a9b1dSJustin T. Gibbs u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 999b059686aSAlexander Motin u_int32_t lengths[CAM_PERIPH_MAXMAPS]; 1000b059686aSAlexander Motin u_int32_t dirs[CAM_PERIPH_MAXMAPS]; 10018b8a9b1dSJustin T. Gibbs 10028b8a9b1dSJustin T. Gibbs if (mapinfo->num_bufs_used <= 0) { 100395fbded6SScott Long /* nothing to free and the process wasn't held. */ 10048b8a9b1dSJustin T. Gibbs return; 10058b8a9b1dSJustin T. Gibbs } 10068b8a9b1dSJustin T. Gibbs 10078b8a9b1dSJustin T. Gibbs switch (ccb->ccb_h.func_code) { 10088b8a9b1dSJustin T. Gibbs case XPT_DEV_MATCH: 1009b059686aSAlexander Motin if (ccb->cdm.pattern_buf_len > 0) { 10108b8a9b1dSJustin T. Gibbs data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1011b059686aSAlexander Motin lengths[0] = ccb->cdm.pattern_buf_len; 1012b059686aSAlexander Motin dirs[0] = CAM_DIR_OUT; 10138b8a9b1dSJustin T. Gibbs data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1014b059686aSAlexander Motin lengths[1] = ccb->cdm.match_buf_len; 1015b059686aSAlexander Motin dirs[1] = CAM_DIR_IN; 1016b059686aSAlexander Motin numbufs = 2; 1017b059686aSAlexander Motin } else { 1018b059686aSAlexander Motin data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1019b059686aSAlexander Motin lengths[0] = ccb->cdm.match_buf_len; 1020b059686aSAlexander Motin dirs[0] = CAM_DIR_IN; 1021b059686aSAlexander Motin numbufs = 1; 10228b8a9b1dSJustin T. Gibbs } 10238b8a9b1dSJustin T. Gibbs break; 10248b8a9b1dSJustin T. Gibbs case XPT_SCSI_IO: 10259911ecf9SJustin T. Gibbs case XPT_CONT_TARGET_IO: 10268b8a9b1dSJustin T. Gibbs data_ptrs[0] = &ccb->csio.data_ptr; 1027b059686aSAlexander Motin lengths[0] = ccb->csio.dxfer_len; 1028b059686aSAlexander Motin dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1029b059686aSAlexander Motin numbufs = 1; 10308b8a9b1dSJustin T. Gibbs break; 103152c9ce25SScott Long case XPT_ATA_IO: 103252c9ce25SScott Long data_ptrs[0] = &ccb->ataio.data_ptr; 1033b059686aSAlexander Motin lengths[0] = ccb->ataio.dxfer_len; 1034b059686aSAlexander Motin dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1035b059686aSAlexander Motin numbufs = 1; 1036b059686aSAlexander Motin break; 1037b059686aSAlexander Motin case XPT_MMC_IO: 1038b059686aSAlexander Motin data_ptrs[0] = (u_int8_t **)&ccb->mmcio.cmd.data; 1039b059686aSAlexander Motin lengths[0] = sizeof(struct mmc_data *); 1040b059686aSAlexander Motin dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1041b059686aSAlexander Motin data_ptrs[1] = (u_int8_t **)&ccb->mmcio.cmd.data->data; 1042b059686aSAlexander Motin lengths[1] = ccb->mmcio.cmd.data->len; 1043b059686aSAlexander Motin dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK; 1044b059686aSAlexander Motin numbufs = 2; 104552c9ce25SScott Long break; 104606e79492SKenneth D. Merry case XPT_SMP_IO: 104706e79492SKenneth D. Merry data_ptrs[0] = &ccb->smpio.smp_request; 1048b059686aSAlexander Motin lengths[0] = ccb->smpio.smp_request_len; 1049b059686aSAlexander Motin dirs[0] = CAM_DIR_OUT; 105006e79492SKenneth D. Merry data_ptrs[1] = &ccb->smpio.smp_response; 1051b059686aSAlexander Motin lengths[1] = ccb->smpio.smp_response_len; 1052b059686aSAlexander Motin dirs[1] = CAM_DIR_IN; 1053b059686aSAlexander Motin numbufs = 2; 105406e79492SKenneth D. Merry break; 1055df424515SWarner Losh case XPT_NVME_IO: 1056df424515SWarner Losh case XPT_NVME_ADMIN: 1057df424515SWarner Losh data_ptrs[0] = &ccb->nvmeio.data_ptr; 1058b059686aSAlexander Motin lengths[0] = ccb->nvmeio.dxfer_len; 1059b059686aSAlexander Motin dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1060b059686aSAlexander Motin numbufs = 1; 1061b059686aSAlexander Motin break; 1062b059686aSAlexander Motin case XPT_DEV_ADVINFO: 1063b059686aSAlexander Motin data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1064b059686aSAlexander Motin lengths[0] = ccb->cdai.bufsiz; 1065b059686aSAlexander Motin dirs[0] = CAM_DIR_IN; 1066b059686aSAlexander Motin numbufs = 1; 1067df424515SWarner Losh break; 10688b8a9b1dSJustin T. Gibbs default: 10698b8a9b1dSJustin T. Gibbs /* allow ourselves to be swapped once again */ 10700cbbb7bfSPeter Wemm PRELE(curproc); 10718b8a9b1dSJustin T. Gibbs return; 10728b8a9b1dSJustin T. Gibbs break; /* NOTREACHED */ 10738b8a9b1dSJustin T. Gibbs } 10748b8a9b1dSJustin T. Gibbs 10758b8a9b1dSJustin T. Gibbs for (i = 0; i < numbufs; i++) { 1076b059686aSAlexander Motin if (mapinfo->bp[i]) { 10778b8a9b1dSJustin T. Gibbs /* unmap the buffer */ 10788b8a9b1dSJustin T. Gibbs vunmapbuf(mapinfo->bp[i]); 10798b8a9b1dSJustin T. Gibbs 10808b8a9b1dSJustin T. Gibbs /* release the buffer */ 1081756a5412SGleb Smirnoff uma_zfree(pbuf_zone, mapinfo->bp[i]); 1082b059686aSAlexander Motin } else { 1083b059686aSAlexander Motin if (dirs[i] != CAM_DIR_OUT) { 1084b059686aSAlexander Motin copyout(*data_ptrs[i], mapinfo->orig[i], 1085b059686aSAlexander Motin lengths[i]); 1086b059686aSAlexander Motin } 1087b059686aSAlexander Motin free(*data_ptrs[i], M_CAMPERIPH); 1088b059686aSAlexander Motin } 1089b059686aSAlexander Motin 1090b059686aSAlexander Motin /* Set the user's pointer back to the original value */ 1091b059686aSAlexander Motin *data_ptrs[i] = mapinfo->orig[i]; 10928b8a9b1dSJustin T. Gibbs } 10938b8a9b1dSJustin T. Gibbs 10948b8a9b1dSJustin T. Gibbs /* allow ourselves to be swapped once again */ 10950cbbb7bfSPeter Wemm PRELE(curproc); 10968b8a9b1dSJustin T. Gibbs } 10978b8a9b1dSJustin T. Gibbs 10988b8a9b1dSJustin T. Gibbs int 1099571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr, 11008b8a9b1dSJustin T. Gibbs int (*error_routine)(union ccb *ccb, 11018b8a9b1dSJustin T. Gibbs cam_flags camflags, 11028b8a9b1dSJustin T. Gibbs u_int32_t sense_flags)) 11038b8a9b1dSJustin T. Gibbs { 11048b8a9b1dSJustin T. Gibbs union ccb *ccb; 11058b8a9b1dSJustin T. Gibbs int error; 11068b8a9b1dSJustin T. Gibbs int found; 11078b8a9b1dSJustin T. Gibbs 11088b8a9b1dSJustin T. Gibbs error = found = 0; 11098b8a9b1dSJustin T. Gibbs 11108b8a9b1dSJustin T. Gibbs switch(cmd){ 11118b8a9b1dSJustin T. Gibbs case CAMGETPASSTHRU: 1112bbfa4aa1SAlexander Motin ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 11138b8a9b1dSJustin T. Gibbs xpt_setup_ccb(&ccb->ccb_h, 11148b8a9b1dSJustin T. Gibbs ccb->ccb_h.path, 1115bbfa4aa1SAlexander Motin CAM_PRIORITY_NORMAL); 11168b8a9b1dSJustin T. Gibbs ccb->ccb_h.func_code = XPT_GDEVLIST; 11178b8a9b1dSJustin T. Gibbs 11188b8a9b1dSJustin T. Gibbs /* 11198b8a9b1dSJustin T. Gibbs * Basically, the point of this is that we go through 11208b8a9b1dSJustin T. Gibbs * getting the list of devices, until we find a passthrough 11218b8a9b1dSJustin T. Gibbs * device. In the current version of the CAM code, the 11228b8a9b1dSJustin T. Gibbs * only way to determine what type of device we're dealing 11238b8a9b1dSJustin T. Gibbs * with is by its name. 11248b8a9b1dSJustin T. Gibbs */ 11258b8a9b1dSJustin T. Gibbs while (found == 0) { 11268b8a9b1dSJustin T. Gibbs ccb->cgdl.index = 0; 11278b8a9b1dSJustin T. Gibbs ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS; 11288b8a9b1dSJustin T. Gibbs while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) { 11298b8a9b1dSJustin T. Gibbs /* we want the next device in the list */ 11308b8a9b1dSJustin T. Gibbs xpt_action(ccb); 11318b8a9b1dSJustin T. Gibbs if (strncmp(ccb->cgdl.periph_name, 11328b8a9b1dSJustin T. Gibbs "pass", 4) == 0){ 11338b8a9b1dSJustin T. Gibbs found = 1; 11348b8a9b1dSJustin T. Gibbs break; 11358b8a9b1dSJustin T. Gibbs } 11368b8a9b1dSJustin T. Gibbs } 11378b8a9b1dSJustin T. Gibbs if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) && 11388b8a9b1dSJustin T. Gibbs (found == 0)) { 11398b8a9b1dSJustin T. Gibbs ccb->cgdl.periph_name[0] = '\0'; 11408b8a9b1dSJustin T. Gibbs ccb->cgdl.unit_number = 0; 11418b8a9b1dSJustin T. Gibbs break; 11428b8a9b1dSJustin T. Gibbs } 11438b8a9b1dSJustin T. Gibbs } 11448b8a9b1dSJustin T. Gibbs 11458b8a9b1dSJustin T. Gibbs /* copy the result back out */ 11468b8a9b1dSJustin T. Gibbs bcopy(ccb, addr, sizeof(union ccb)); 11478b8a9b1dSJustin T. Gibbs 11488b8a9b1dSJustin T. Gibbs /* and release the ccb */ 11498b8a9b1dSJustin T. Gibbs xpt_release_ccb(ccb); 11508b8a9b1dSJustin T. Gibbs 11518b8a9b1dSJustin T. Gibbs break; 11528b8a9b1dSJustin T. Gibbs default: 11538b8a9b1dSJustin T. Gibbs error = ENOTTY; 11548b8a9b1dSJustin T. Gibbs break; 11558b8a9b1dSJustin T. Gibbs } 11568b8a9b1dSJustin T. Gibbs return(error); 11578b8a9b1dSJustin T. Gibbs } 11588b8a9b1dSJustin T. Gibbs 1159227d67aaSAlexander Motin static void 1160991b5d26SMark Johnston cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb) 1161991b5d26SMark Johnston { 1162991b5d26SMark Johnston 1163991b5d26SMark Johnston panic("%s: already done with ccb %p", __func__, done_ccb); 1164991b5d26SMark Johnston } 1165991b5d26SMark Johnston 1166991b5d26SMark Johnston static void 1167227d67aaSAlexander Motin cam_periph_done(struct cam_periph *periph, union ccb *done_ccb) 1168227d67aaSAlexander Motin { 1169227d67aaSAlexander Motin 1170227d67aaSAlexander Motin /* Caller will release the CCB */ 1171991b5d26SMark Johnston xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED); 1172991b5d26SMark Johnston done_ccb->ccb_h.cbfcnp = cam_periph_done_panic; 1173227d67aaSAlexander Motin wakeup(&done_ccb->ccb_h.cbfcnp); 1174227d67aaSAlexander Motin } 1175227d67aaSAlexander Motin 1176991b5d26SMark Johnston static void 1177991b5d26SMark Johnston cam_periph_ccbwait(union ccb *ccb) 1178991b5d26SMark Johnston { 1179991b5d26SMark Johnston 1180991b5d26SMark Johnston if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 1181991b5d26SMark Johnston while (ccb->ccb_h.cbfcnp != cam_periph_done_panic) 1182991b5d26SMark Johnston xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp, 1183991b5d26SMark Johnston PRIBIO, "cbwait", 0); 1184991b5d26SMark Johnston } 1185991b5d26SMark Johnston KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX && 1186991b5d26SMark Johnston (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG, 1187991b5d26SMark Johnston ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, " 1188991b5d26SMark Johnston "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code, 1189991b5d26SMark Johnston ccb->ccb_h.status, ccb->ccb_h.pinfo.index)); 1190991b5d26SMark Johnston } 1191991b5d26SMark Johnston 119276314772SScott Long /* 119376314772SScott Long * Dispatch a CCB and wait for it to complete. If the CCB has set a 119476314772SScott Long * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost. 119576314772SScott Long */ 11968b8a9b1dSJustin T. Gibbs int 11978b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb, 11988b8a9b1dSJustin T. Gibbs int (*error_routine)(union ccb *ccb, 11998b8a9b1dSJustin T. Gibbs cam_flags camflags, 12008b8a9b1dSJustin T. Gibbs u_int32_t sense_flags), 12018b8a9b1dSJustin T. Gibbs cam_flags camflags, u_int32_t sense_flags, 12028b8a9b1dSJustin T. Gibbs struct devstat *ds) 12038b8a9b1dSJustin T. Gibbs { 1204fd8be18fSKenneth D. Merry struct bintime *starttime; 1205fd8be18fSKenneth D. Merry struct bintime ltime; 12068b8a9b1dSJustin T. Gibbs int error; 12075cf3cd10SWarner Losh bool must_poll; 1208f93a843cSWarner Losh uint32_t timeout = 1; 12098b8a9b1dSJustin T. Gibbs 1210fd8be18fSKenneth D. Merry starttime = NULL; 1211227d67aaSAlexander Motin xpt_path_assert(ccb->ccb_h.path, MA_OWNED); 1212991b5d26SMark Johnston KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0, 1213991b5d26SMark Johnston ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb, 1214991b5d26SMark Johnston ccb->ccb_h.func_code, ccb->ccb_h.flags)); 12158b8a9b1dSJustin T. Gibbs 12168b8a9b1dSJustin T. Gibbs /* 12178b8a9b1dSJustin T. Gibbs * If the user has supplied a stats structure, and if we understand 12188b8a9b1dSJustin T. Gibbs * this particular type of ccb, record the transaction start. 12198b8a9b1dSJustin T. Gibbs */ 1220d2f3208dSWarner Losh if (ds != NULL && 1221d2f3208dSWarner Losh (ccb->ccb_h.func_code == XPT_SCSI_IO || 1222d2f3208dSWarner Losh ccb->ccb_h.func_code == XPT_ATA_IO || 1223d2f3208dSWarner Losh ccb->ccb_h.func_code == XPT_NVME_IO)) { 1224fd8be18fSKenneth D. Merry starttime = <ime; 1225fd8be18fSKenneth D. Merry binuptime(starttime); 1226fd8be18fSKenneth D. Merry devstat_start_transaction(ds, starttime); 1227fd8be18fSKenneth D. Merry } 12288b8a9b1dSJustin T. Gibbs 12295cf3cd10SWarner Losh /* 12305cf3cd10SWarner Losh * We must poll the I/O while we're dumping. The scheduler is normally 12315cf3cd10SWarner Losh * stopped for dumping, except when we call doadump from ddb. While the 12325cf3cd10SWarner Losh * scheduler is running in this case, we still need to poll the I/O to 12335cf3cd10SWarner Losh * avoid sleeping waiting for the ccb to complete. 123404e814aeSScott Long * 1235045f8bc8SWarner Losh * A panic triggered dump stops the scheduler, any callback from the 1236045f8bc8SWarner Losh * shutdown_post_sync event will run with the scheduler stopped, but 1237045f8bc8SWarner Losh * before we're officially dumping. To avoid hanging in adashutdown 1238045f8bc8SWarner Losh * initiated commands (or other similar situations), we have to test for 1239045f8bc8SWarner Losh * either SCHEDULER_STOPPED() here as well. 1240045f8bc8SWarner Losh * 1241045f8bc8SWarner Losh * To avoid locking problems, dumping/polling callers must call 124204e814aeSScott Long * without a periph lock held. 12435cf3cd10SWarner Losh */ 1244045f8bc8SWarner Losh must_poll = dumping || SCHEDULER_STOPPED(); 1245227d67aaSAlexander Motin ccb->ccb_h.cbfcnp = cam_periph_done; 12468b8a9b1dSJustin T. Gibbs 1247f93a843cSWarner Losh /* 1248f93a843cSWarner Losh * If we're polling, then we need to ensure that we have ample resources 124976314772SScott Long * in the periph. cam_periph_error can reschedule the ccb by calling 125076314772SScott Long * xpt_action and returning ERESTART, so we have to effect the polling 125176314772SScott Long * in the do loop below. 1252f93a843cSWarner Losh */ 12535cf3cd10SWarner Losh if (must_poll) { 1254447b3557SJohn Baldwin if (cam_sim_pollable(ccb->ccb_h.path->bus->sim)) 1255f93a843cSWarner Losh timeout = xpt_poll_setup(ccb); 1256447b3557SJohn Baldwin else 1257447b3557SJohn Baldwin timeout = 0; 1258f93a843cSWarner Losh } 1259f93a843cSWarner Losh 1260f93a843cSWarner Losh if (timeout == 0) { 1261f93a843cSWarner Losh ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1262f93a843cSWarner Losh error = EBUSY; 1263f93a843cSWarner Losh } else { 1264f93a843cSWarner Losh xpt_action(ccb); 12658b8a9b1dSJustin T. Gibbs do { 12665cf3cd10SWarner Losh if (must_poll) { 1267f93a843cSWarner Losh xpt_pollwait(ccb, timeout); 1268f93a843cSWarner Losh timeout = ccb->ccb_h.timeout * 10; 12695cf3cd10SWarner Losh } else { 12705cf3cd10SWarner Losh cam_periph_ccbwait(ccb); 1271f93a843cSWarner Losh } 12728b8a9b1dSJustin T. Gibbs if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 12738b8a9b1dSJustin T. Gibbs error = 0; 1274991b5d26SMark Johnston else if (error_routine != NULL) { 1275991b5d26SMark Johnston ccb->ccb_h.cbfcnp = cam_periph_done; 12768b8a9b1dSJustin T. Gibbs error = (*error_routine)(ccb, camflags, sense_flags); 1277991b5d26SMark Johnston } else 12788b8a9b1dSJustin T. Gibbs error = 0; 12798b8a9b1dSJustin T. Gibbs } while (error == ERESTART); 1280f93a843cSWarner Losh } 1281f93a843cSWarner Losh 128283c5d981SAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 12838b8a9b1dSJustin T. Gibbs cam_release_devq(ccb->ccb_h.path, 12848b8a9b1dSJustin T. Gibbs /* relsim_flags */0, 12858b8a9b1dSJustin T. Gibbs /* openings */0, 12868b8a9b1dSJustin T. Gibbs /* timeout */0, 12878b8a9b1dSJustin T. Gibbs /* getcount_only */ FALSE); 128883c5d981SAlexander Motin ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 128983c5d981SAlexander Motin } 12908b8a9b1dSJustin T. Gibbs 1291379ad358SAlexander Motin if (ds != NULL) { 1292d2f3208dSWarner Losh uint32_t bytes; 1293d2f3208dSWarner Losh devstat_tag_type tag; 1294d2f3208dSWarner Losh bool valid = true; 1295d2f3208dSWarner Losh 1296379ad358SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1297d2f3208dSWarner Losh bytes = ccb->csio.dxfer_len - ccb->csio.resid; 1298d2f3208dSWarner Losh tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3); 1299379ad358SAlexander Motin } else if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1300d2f3208dSWarner Losh bytes = ccb->ataio.dxfer_len - ccb->ataio.resid; 1301d2f3208dSWarner Losh tag = (devstat_tag_type)0; 1302d2f3208dSWarner Losh } else if (ccb->ccb_h.func_code == XPT_NVME_IO) { 1303d2f3208dSWarner Losh bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */ 1304d2f3208dSWarner Losh tag = (devstat_tag_type)0; 1305d2f3208dSWarner Losh } else { 1306d2f3208dSWarner Losh valid = false; 1307379ad358SAlexander Motin } 1308d2f3208dSWarner Losh if (valid) 1309d2f3208dSWarner Losh devstat_end_transaction(ds, bytes, tag, 1310d2f3208dSWarner Losh ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ? 1311d2f3208dSWarner Losh DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1312d2f3208dSWarner Losh DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime); 1313379ad358SAlexander Motin } 13148b8a9b1dSJustin T. Gibbs 13158b8a9b1dSJustin T. Gibbs return(error); 13168b8a9b1dSJustin T. Gibbs } 13178b8a9b1dSJustin T. Gibbs 131887cfaf0eSJustin T. Gibbs void 131987cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path) 132087cfaf0eSJustin T. Gibbs { 1321cccf4220SAlexander Motin struct ccb_hdr ccb_h; 132287cfaf0eSJustin T. Gibbs 13230d4f3c31SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n")); 1324076686feSEdward Tomasz Napierala memset(&ccb_h, 0, sizeof(ccb_h)); 1325cccf4220SAlexander Motin xpt_setup_ccb(&ccb_h, path, /*priority*/1); 1326cccf4220SAlexander Motin ccb_h.func_code = XPT_NOOP; 1327cccf4220SAlexander Motin ccb_h.flags = CAM_DEV_QFREEZE; 1328cccf4220SAlexander Motin xpt_action((union ccb *)&ccb_h); 132987cfaf0eSJustin T. Gibbs } 133087cfaf0eSJustin T. Gibbs 13318b8a9b1dSJustin T. Gibbs u_int32_t 13328b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags, 133383c5d981SAlexander Motin u_int32_t openings, u_int32_t arg, 13348b8a9b1dSJustin T. Gibbs int getcount_only) 13358b8a9b1dSJustin T. Gibbs { 13368b8a9b1dSJustin T. Gibbs struct ccb_relsim crs; 13378b8a9b1dSJustin T. Gibbs 13380d4f3c31SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n", 13390d4f3c31SAlexander Motin relsim_flags, openings, arg, getcount_only)); 1340076686feSEdward Tomasz Napierala memset(&crs, 0, sizeof(crs)); 1341bbfa4aa1SAlexander Motin xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 13428b8a9b1dSJustin T. Gibbs crs.ccb_h.func_code = XPT_REL_SIMQ; 13438b8a9b1dSJustin T. Gibbs crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0; 13448b8a9b1dSJustin T. Gibbs crs.release_flags = relsim_flags; 13458b8a9b1dSJustin T. Gibbs crs.openings = openings; 134683c5d981SAlexander Motin crs.release_timeout = arg; 13478b8a9b1dSJustin T. Gibbs xpt_action((union ccb *)&crs); 13488b8a9b1dSJustin T. Gibbs return (crs.qfrozen_cnt); 13498b8a9b1dSJustin T. Gibbs } 13508b8a9b1dSJustin T. Gibbs 13518b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0 135283c5d981SAlexander Motin static void 135383c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb) 135483c5d981SAlexander Motin { 13550191d9b3SAlexander Motin union ccb *saved_ccb; 135683c5d981SAlexander Motin cam_status status; 135783c5d981SAlexander Motin struct scsi_start_stop_unit *scsi_cmd; 135879fab7d4SAlexander Motin int error = 0, error_code, sense_key, asc, ascq; 135913aa56fcSEdward Tomasz Napierala u_int16_t done_flags; 136083c5d981SAlexander Motin 13610191d9b3SAlexander Motin scsi_cmd = (struct scsi_start_stop_unit *) 13620191d9b3SAlexander Motin &done_ccb->csio.cdb_io.cdb_bytes; 136383c5d981SAlexander Motin status = done_ccb->ccb_h.status; 13648b8a9b1dSJustin T. Gibbs 13650191d9b3SAlexander Motin if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1366e7493b28SAlexander Motin if (scsi_extract_sense_ccb(done_ccb, 1367e7493b28SAlexander Motin &error_code, &sense_key, &asc, &ascq)) { 136865afe1f4SKenneth D. Merry /* 13698b8a9b1dSJustin T. Gibbs * If the error is "invalid field in CDB", 13708b8a9b1dSJustin T. Gibbs * and the load/eject flag is set, turn the 13718b8a9b1dSJustin T. Gibbs * flag off and try again. This is just in 13728b8a9b1dSJustin T. Gibbs * case the drive in question barfs on the 13738b8a9b1dSJustin T. Gibbs * load eject flag. The CAM code should set 13748b8a9b1dSJustin T. Gibbs * the load/eject flag by default for 13758b8a9b1dSJustin T. Gibbs * removable media. 13768b8a9b1dSJustin T. Gibbs */ 13778b8a9b1dSJustin T. Gibbs if ((scsi_cmd->opcode == START_STOP_UNIT) && 13788b8a9b1dSJustin T. Gibbs ((scsi_cmd->how & SSS_LOEJ) != 0) && 13790191d9b3SAlexander Motin (asc == 0x24) && (ascq == 0x00)) { 13808b8a9b1dSJustin T. Gibbs scsi_cmd->how &= ~SSS_LOEJ; 13810191d9b3SAlexander Motin if (status & CAM_DEV_QFRZN) { 13820191d9b3SAlexander Motin cam_release_devq(done_ccb->ccb_h.path, 13830191d9b3SAlexander Motin 0, 0, 0, 0); 13840191d9b3SAlexander Motin done_ccb->ccb_h.status &= 13850191d9b3SAlexander Motin ~CAM_DEV_QFRZN; 13860191d9b3SAlexander Motin } 13878b8a9b1dSJustin T. Gibbs xpt_action(done_ccb); 13880191d9b3SAlexander Motin goto out; 13890191d9b3SAlexander Motin } 13900191d9b3SAlexander Motin } 139179fab7d4SAlexander Motin error = cam_periph_error(done_ccb, 0, 139279fab7d4SAlexander Motin SF_RETRY_UA | SF_NO_PRINT); 139379fab7d4SAlexander Motin if (error == ERESTART) 13940191d9b3SAlexander Motin goto out; 13950191d9b3SAlexander Motin if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) { 13960191d9b3SAlexander Motin cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0); 13970191d9b3SAlexander Motin done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 13980191d9b3SAlexander Motin } 13990191d9b3SAlexander Motin } else { 14000191d9b3SAlexander Motin /* 14010191d9b3SAlexander Motin * If we have successfully taken a device from the not 14020191d9b3SAlexander Motin * ready to ready state, re-scan the device and re-get 14030191d9b3SAlexander Motin * the inquiry information. Many devices (mostly disks) 14040191d9b3SAlexander Motin * don't properly report their inquiry information unless 14050191d9b3SAlexander Motin * they are spun up. 14060191d9b3SAlexander Motin */ 14070191d9b3SAlexander Motin if (scsi_cmd->opcode == START_STOP_UNIT) 14080191d9b3SAlexander Motin xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL); 14090191d9b3SAlexander Motin } 141065afe1f4SKenneth D. Merry 1411bae3729bSAlexander Motin /* If we tried long wait and still failed, remember that. */ 1412bae3729bSAlexander Motin if ((periph->flags & CAM_PERIPH_RECOVERY_WAIT) && 1413bae3729bSAlexander Motin (done_ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY)) { 1414bae3729bSAlexander Motin periph->flags &= ~CAM_PERIPH_RECOVERY_WAIT; 1415bae3729bSAlexander Motin if (error != 0 && done_ccb->ccb_h.retry_count == 0) 1416bae3729bSAlexander Motin periph->flags |= CAM_PERIPH_RECOVERY_WAIT_FAILED; 1417bae3729bSAlexander Motin } 1418bae3729bSAlexander Motin 14198b8a9b1dSJustin T. Gibbs /* 142079fab7d4SAlexander Motin * After recovery action(s) completed, return to the original CCB. 142179fab7d4SAlexander Motin * If the recovery CCB has failed, considering its own possible 142279fab7d4SAlexander Motin * retries and recovery, assume we are back in state where we have 142379fab7d4SAlexander Motin * been originally, but without recovery hopes left. In such case, 142479fab7d4SAlexander Motin * after the final attempt below, we cancel any further retries, 142579fab7d4SAlexander Motin * blocking by that also any new recovery attempts for this CCB, 142679fab7d4SAlexander Motin * and the result will be the final one returned to the CCB owher. 14278b8a9b1dSJustin T. Gibbs */ 142813aa56fcSEdward Tomasz Napierala 142913aa56fcSEdward Tomasz Napierala /* 143013aa56fcSEdward Tomasz Napierala * Copy the CCB back, preserving the alloc_flags field. Things 143113aa56fcSEdward Tomasz Napierala * will crash horribly if the CCBs are not of the same size. 143213aa56fcSEdward Tomasz Napierala */ 14330191d9b3SAlexander Motin saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr; 143413aa56fcSEdward Tomasz Napierala KASSERT(saved_ccb->ccb_h.func_code == XPT_SCSI_IO, 143513aa56fcSEdward Tomasz Napierala ("%s: saved_ccb func_code %#x != XPT_SCSI_IO", 143613aa56fcSEdward Tomasz Napierala __func__, saved_ccb->ccb_h.func_code)); 143713aa56fcSEdward Tomasz Napierala KASSERT(done_ccb->ccb_h.func_code == XPT_SCSI_IO, 143813aa56fcSEdward Tomasz Napierala ("%s: done_ccb func_code %#x != XPT_SCSI_IO", 143913aa56fcSEdward Tomasz Napierala __func__, done_ccb->ccb_h.func_code)); 144013aa56fcSEdward Tomasz Napierala done_flags = done_ccb->ccb_h.alloc_flags; 144113aa56fcSEdward Tomasz Napierala bcopy(saved_ccb, done_ccb, sizeof(struct ccb_scsiio)); 144213aa56fcSEdward Tomasz Napierala done_ccb->ccb_h.alloc_flags = done_flags; 144383c5d981SAlexander Motin xpt_free_ccb(saved_ccb); 14440191d9b3SAlexander Motin if (done_ccb->ccb_h.cbfcnp != camperiphdone) 144560a899a0SKenneth D. Merry periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 144679fab7d4SAlexander Motin if (error != 0) 144779fab7d4SAlexander Motin done_ccb->ccb_h.retry_count = 0; 14488b8a9b1dSJustin T. Gibbs xpt_action(done_ccb); 14498b8a9b1dSJustin T. Gibbs 14500191d9b3SAlexander Motin out: 14510191d9b3SAlexander Motin /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */ 14520191d9b3SAlexander Motin cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0); 1453d84c90a6SAlexander Motin } 14548b8a9b1dSJustin T. Gibbs 14558b8a9b1dSJustin T. Gibbs /* 145687cfaf0eSJustin T. Gibbs * Generic Async Event handler. Peripheral drivers usually 145787cfaf0eSJustin T. Gibbs * filter out the events that require personal attention, 145887cfaf0eSJustin T. Gibbs * and leave the rest to this function. 145987cfaf0eSJustin T. Gibbs */ 146087cfaf0eSJustin T. Gibbs void 146187cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code, 146287cfaf0eSJustin T. Gibbs struct cam_path *path, void *arg) 146387cfaf0eSJustin T. Gibbs { 146487cfaf0eSJustin T. Gibbs switch (code) { 146587cfaf0eSJustin T. Gibbs case AC_LOST_DEVICE: 146687cfaf0eSJustin T. Gibbs cam_periph_invalidate(periph); 146787cfaf0eSJustin T. Gibbs break; 146887cfaf0eSJustin T. Gibbs default: 146987cfaf0eSJustin T. Gibbs break; 147087cfaf0eSJustin T. Gibbs } 147187cfaf0eSJustin T. Gibbs } 147287cfaf0eSJustin T. Gibbs 147387cfaf0eSJustin T. Gibbs void 147487cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle) 147587cfaf0eSJustin T. Gibbs { 147687cfaf0eSJustin T. Gibbs struct ccb_getdevstats cgds; 147787cfaf0eSJustin T. Gibbs 1478076686feSEdward Tomasz Napierala memset(&cgds, 0, sizeof(cgds)); 1479bbfa4aa1SAlexander Motin xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 148087cfaf0eSJustin T. Gibbs cgds.ccb_h.func_code = XPT_GDEV_STATS; 148187cfaf0eSJustin T. Gibbs xpt_action((union ccb *)&cgds); 148287cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle); 148387cfaf0eSJustin T. Gibbs } 148487cfaf0eSJustin T. Gibbs 148587cfaf0eSJustin T. Gibbs void 148687cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph, 148787cfaf0eSJustin T. Gibbs struct timeval* event_time, u_int duration_ms) 148887cfaf0eSJustin T. Gibbs { 148987cfaf0eSJustin T. Gibbs struct timeval delta; 149087cfaf0eSJustin T. Gibbs struct timeval duration_tv; 149187cfaf0eSJustin T. Gibbs 1492f8ff57d2SAlexander Motin if (!timevalisset(event_time)) 1493f8ff57d2SAlexander Motin return; 1494f8ff57d2SAlexander Motin 149587cfaf0eSJustin T. Gibbs microtime(&delta); 149687cfaf0eSJustin T. Gibbs timevalsub(&delta, event_time); 149787cfaf0eSJustin T. Gibbs duration_tv.tv_sec = duration_ms / 1000; 149887cfaf0eSJustin T. Gibbs duration_tv.tv_usec = (duration_ms % 1000) * 1000; 149987cfaf0eSJustin T. Gibbs if (timevalcmp(&delta, &duration_tv, <)) { 150087cfaf0eSJustin T. Gibbs timevalsub(&duration_tv, &delta); 150187cfaf0eSJustin T. Gibbs 150287cfaf0eSJustin T. Gibbs duration_ms = duration_tv.tv_sec * 1000; 150387cfaf0eSJustin T. Gibbs duration_ms += duration_tv.tv_usec / 1000; 150487cfaf0eSJustin T. Gibbs cam_freeze_devq(periph->path); 150587cfaf0eSJustin T. Gibbs cam_release_devq(periph->path, 150687cfaf0eSJustin T. Gibbs RELSIM_RELEASE_AFTER_TIMEOUT, 150787cfaf0eSJustin T. Gibbs /*reduction*/0, 150887cfaf0eSJustin T. Gibbs /*timeout*/duration_ms, 150987cfaf0eSJustin T. Gibbs /*getcount_only*/0); 151087cfaf0eSJustin T. Gibbs } 151187cfaf0eSJustin T. Gibbs 151287cfaf0eSJustin T. Gibbs } 151387cfaf0eSJustin T. Gibbs 15143393f8daSKenneth D. Merry static int 15150191d9b3SAlexander Motin camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb, 15160191d9b3SAlexander Motin cam_flags camflags, u_int32_t sense_flags, 15173393f8daSKenneth D. Merry int *openings, u_int32_t *relsim_flags, 15180181d54bSAlexander Motin u_int32_t *timeout, u_int32_t *action, const char **action_string) 15198b8a9b1dSJustin T. Gibbs { 152099bad9caSAlexander Motin struct cam_periph *periph; 15218b8a9b1dSJustin T. Gibbs int error; 15228b8a9b1dSJustin T. Gibbs 15238b8a9b1dSJustin T. Gibbs switch (ccb->csio.scsi_status) { 15248b8a9b1dSJustin T. Gibbs case SCSI_STATUS_OK: 15258b8a9b1dSJustin T. Gibbs case SCSI_STATUS_COND_MET: 15268b8a9b1dSJustin T. Gibbs case SCSI_STATUS_INTERMED: 15278b8a9b1dSJustin T. Gibbs case SCSI_STATUS_INTERMED_COND_MET: 15288b8a9b1dSJustin T. Gibbs error = 0; 15298b8a9b1dSJustin T. Gibbs break; 15308b8a9b1dSJustin T. Gibbs case SCSI_STATUS_CMD_TERMINATED: 15318b8a9b1dSJustin T. Gibbs case SCSI_STATUS_CHECK_COND: 15320191d9b3SAlexander Motin error = camperiphscsisenseerror(ccb, orig_ccb, 15333393f8daSKenneth D. Merry camflags, 15348b8a9b1dSJustin T. Gibbs sense_flags, 15353393f8daSKenneth D. Merry openings, 15363393f8daSKenneth D. Merry relsim_flags, 153783c5d981SAlexander Motin timeout, 15380181d54bSAlexander Motin action, 153983c5d981SAlexander Motin action_string); 15408b8a9b1dSJustin T. Gibbs break; 15418b8a9b1dSJustin T. Gibbs case SCSI_STATUS_QUEUE_FULL: 15428b8a9b1dSJustin T. Gibbs { 15438b8a9b1dSJustin T. Gibbs /* no decrement */ 154482815562SJustin T. Gibbs struct ccb_getdevstats cgds; 15458b8a9b1dSJustin T. Gibbs 15468b8a9b1dSJustin T. Gibbs /* 15478b8a9b1dSJustin T. Gibbs * First off, find out what the current 15488b8a9b1dSJustin T. Gibbs * transaction counts are. 15498b8a9b1dSJustin T. Gibbs */ 1550076686feSEdward Tomasz Napierala memset(&cgds, 0, sizeof(cgds)); 155182815562SJustin T. Gibbs xpt_setup_ccb(&cgds.ccb_h, 15528b8a9b1dSJustin T. Gibbs ccb->ccb_h.path, 1553bbfa4aa1SAlexander Motin CAM_PRIORITY_NORMAL); 155482815562SJustin T. Gibbs cgds.ccb_h.func_code = XPT_GDEV_STATS; 155582815562SJustin T. Gibbs xpt_action((union ccb *)&cgds); 15568b8a9b1dSJustin T. Gibbs 15578b8a9b1dSJustin T. Gibbs /* 15588b8a9b1dSJustin T. Gibbs * If we were the only transaction active, treat 15598b8a9b1dSJustin T. Gibbs * the QUEUE FULL as if it were a BUSY condition. 15608b8a9b1dSJustin T. Gibbs */ 156182815562SJustin T. Gibbs if (cgds.dev_active != 0) { 156282815562SJustin T. Gibbs int total_openings; 156382815562SJustin T. Gibbs 15648b8a9b1dSJustin T. Gibbs /* 15658b8a9b1dSJustin T. Gibbs * Reduce the number of openings to 15668b8a9b1dSJustin T. Gibbs * be 1 less than the amount it took 15678b8a9b1dSJustin T. Gibbs * to get a queue full bounded by the 15688b8a9b1dSJustin T. Gibbs * minimum allowed tag count for this 15698b8a9b1dSJustin T. Gibbs * device. 15708b8a9b1dSJustin T. Gibbs */ 15713393f8daSKenneth D. Merry total_openings = cgds.dev_active + cgds.dev_openings; 15723393f8daSKenneth D. Merry *openings = cgds.dev_active; 15733393f8daSKenneth D. Merry if (*openings < cgds.mintags) 15743393f8daSKenneth D. Merry *openings = cgds.mintags; 15753393f8daSKenneth D. Merry if (*openings < total_openings) 15763393f8daSKenneth D. Merry *relsim_flags = RELSIM_ADJUST_OPENINGS; 15778b8a9b1dSJustin T. Gibbs else { 15788b8a9b1dSJustin T. Gibbs /* 15798b8a9b1dSJustin T. Gibbs * Some devices report queue full for 15808b8a9b1dSJustin T. Gibbs * temporary resource shortages. For 15818b8a9b1dSJustin T. Gibbs * this reason, we allow a minimum 15828b8a9b1dSJustin T. Gibbs * tag count to be entered via a 15838b8a9b1dSJustin T. Gibbs * quirk entry to prevent the queue 15848b8a9b1dSJustin T. Gibbs * count on these devices from falling 15858b8a9b1dSJustin T. Gibbs * to a pessimisticly low value. We 15868b8a9b1dSJustin T. Gibbs * still wait for the next successful 15878b8a9b1dSJustin T. Gibbs * completion, however, before queueing 15888b8a9b1dSJustin T. Gibbs * more transactions to the device. 15898b8a9b1dSJustin T. Gibbs */ 15903393f8daSKenneth D. Merry *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT; 15918b8a9b1dSJustin T. Gibbs } 15923393f8daSKenneth D. Merry *timeout = 0; 15938b8a9b1dSJustin T. Gibbs error = ERESTART; 15940181d54bSAlexander Motin *action &= ~SSQ_PRINT_SENSE; 15958b8a9b1dSJustin T. Gibbs break; 15968b8a9b1dSJustin T. Gibbs } 15978b8a9b1dSJustin T. Gibbs /* FALLTHROUGH */ 15988b8a9b1dSJustin T. Gibbs } 15998b8a9b1dSJustin T. Gibbs case SCSI_STATUS_BUSY: 16008b8a9b1dSJustin T. Gibbs /* 16018b8a9b1dSJustin T. Gibbs * Restart the queue after either another 16028b8a9b1dSJustin T. Gibbs * command completes or a 1 second timeout. 16038b8a9b1dSJustin T. Gibbs */ 160499bad9caSAlexander Motin periph = xpt_path_periph(ccb->ccb_h.path); 160599bad9caSAlexander Motin if (periph->flags & CAM_PERIPH_INVALID) { 160699bad9caSAlexander Motin error = EIO; 160799bad9caSAlexander Motin *action_string = "Periph was invalidated"; 160899bad9caSAlexander Motin } else if ((sense_flags & SF_RETRY_BUSY) != 0 || 160999bad9caSAlexander Motin ccb->ccb_h.retry_count > 0) { 161099bad9caSAlexander Motin if ((sense_flags & SF_RETRY_BUSY) == 0) 161199bad9caSAlexander Motin ccb->ccb_h.retry_count--; 16128b8a9b1dSJustin T. Gibbs error = ERESTART; 16133393f8daSKenneth D. Merry *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT 16148b8a9b1dSJustin T. Gibbs | RELSIM_RELEASE_AFTER_CMDCMPLT; 16153393f8daSKenneth D. Merry *timeout = 1000; 1616af51b059SMatt Jacob } else { 1617af51b059SMatt Jacob error = EIO; 161899bad9caSAlexander Motin *action_string = "Retries exhausted"; 1619af51b059SMatt Jacob } 16208b8a9b1dSJustin T. Gibbs break; 16218b8a9b1dSJustin T. Gibbs case SCSI_STATUS_RESERV_CONFLICT: 16228b8a9b1dSJustin T. Gibbs default: 16238b8a9b1dSJustin T. Gibbs error = EIO; 16248b8a9b1dSJustin T. Gibbs break; 16258b8a9b1dSJustin T. Gibbs } 16263393f8daSKenneth D. Merry return (error); 16273393f8daSKenneth D. Merry } 16283393f8daSKenneth D. Merry 16293393f8daSKenneth D. Merry static int 16300191d9b3SAlexander Motin camperiphscsisenseerror(union ccb *ccb, union ccb **orig, 16310191d9b3SAlexander Motin cam_flags camflags, u_int32_t sense_flags, 16323393f8daSKenneth D. Merry int *openings, u_int32_t *relsim_flags, 16330181d54bSAlexander Motin u_int32_t *timeout, u_int32_t *action, const char **action_string) 16343393f8daSKenneth D. Merry { 16353393f8daSKenneth D. Merry struct cam_periph *periph; 163683c5d981SAlexander Motin union ccb *orig_ccb = ccb; 16370191d9b3SAlexander Motin int error, recoveryccb; 163813aa56fcSEdward Tomasz Napierala u_int16_t flags; 16393393f8daSKenneth D. Merry 16408532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 16418532d381SConrad Meyer if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL) 16428532d381SConrad Meyer biotrack(ccb->csio.bio, __func__); 16438532d381SConrad Meyer #endif 16448532d381SConrad Meyer 16453393f8daSKenneth D. Merry periph = xpt_path_periph(ccb->ccb_h.path); 16460191d9b3SAlexander Motin recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone); 16470191d9b3SAlexander Motin if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) { 16483393f8daSKenneth D. Merry /* 16493393f8daSKenneth D. Merry * If error recovery is already in progress, don't attempt 16503393f8daSKenneth D. Merry * to process this error, but requeue it unconditionally 16513393f8daSKenneth D. Merry * and attempt to process it once error recovery has 16523393f8daSKenneth D. Merry * completed. This failed command is probably related to 16533393f8daSKenneth D. Merry * the error that caused the currently active error recovery 16543393f8daSKenneth D. Merry * action so our current recovery efforts should also 16553393f8daSKenneth D. Merry * address this command. Be aware that the error recovery 16563393f8daSKenneth D. Merry * code assumes that only one recovery action is in progress 16573393f8daSKenneth D. Merry * on a particular peripheral instance at any given time 16583393f8daSKenneth D. Merry * (e.g. only one saved CCB for error recovery) so it is 16593393f8daSKenneth D. Merry * imperitive that we don't violate this assumption. 16603393f8daSKenneth D. Merry */ 16613393f8daSKenneth D. Merry error = ERESTART; 16620181d54bSAlexander Motin *action &= ~SSQ_PRINT_SENSE; 16633393f8daSKenneth D. Merry } else { 16643393f8daSKenneth D. Merry scsi_sense_action err_action; 16653393f8daSKenneth D. Merry struct ccb_getdev cgd; 16663393f8daSKenneth D. Merry 16673393f8daSKenneth D. Merry /* 16683393f8daSKenneth D. Merry * Grab the inquiry data for this device. 16693393f8daSKenneth D. Merry */ 1670076686feSEdward Tomasz Napierala memset(&cgd, 0, sizeof(cgd)); 1671bbfa4aa1SAlexander Motin xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL); 16723393f8daSKenneth D. Merry cgd.ccb_h.func_code = XPT_GDEV_TYPE; 16733393f8daSKenneth D. Merry xpt_action((union ccb *)&cgd); 16743393f8daSKenneth D. Merry 1675e7493b28SAlexander Motin err_action = scsi_error_action(&ccb->csio, &cgd.inq_data, 16763393f8daSKenneth D. Merry sense_flags); 16773393f8daSKenneth D. Merry error = err_action & SS_ERRMASK; 16783393f8daSKenneth D. Merry 16793393f8daSKenneth D. Merry /* 16800191d9b3SAlexander Motin * Do not autostart sequential access devices 16810191d9b3SAlexander Motin * to avoid unexpected tape loading. 16820191d9b3SAlexander Motin */ 16830191d9b3SAlexander Motin if ((err_action & SS_MASK) == SS_START && 16840191d9b3SAlexander Motin SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) { 16850191d9b3SAlexander Motin *action_string = "Will not autostart a " 16860191d9b3SAlexander Motin "sequential access device"; 16870191d9b3SAlexander Motin goto sense_error_done; 16880191d9b3SAlexander Motin } 16890191d9b3SAlexander Motin 16900191d9b3SAlexander Motin /* 16910191d9b3SAlexander Motin * Avoid recovery recursion if recovery action is the same. 16920191d9b3SAlexander Motin */ 16930191d9b3SAlexander Motin if ((err_action & SS_MASK) >= SS_START && recoveryccb) { 16940191d9b3SAlexander Motin if (((err_action & SS_MASK) == SS_START && 16950191d9b3SAlexander Motin ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) || 16960191d9b3SAlexander Motin ((err_action & SS_MASK) == SS_TUR && 16970191d9b3SAlexander Motin (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) { 16980191d9b3SAlexander Motin err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO; 16990191d9b3SAlexander Motin *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 17000191d9b3SAlexander Motin *timeout = 500; 17010191d9b3SAlexander Motin } 17020191d9b3SAlexander Motin } 17030191d9b3SAlexander Motin 17040191d9b3SAlexander Motin /* 17053393f8daSKenneth D. Merry * If the recovery action will consume a retry, 17063393f8daSKenneth D. Merry * make sure we actually have retries available. 17073393f8daSKenneth D. Merry */ 17083393f8daSKenneth D. Merry if ((err_action & SSQ_DECREMENT_COUNT) != 0) { 1709a07e846bSAlexander Motin if (ccb->ccb_h.retry_count > 0 && 1710a07e846bSAlexander Motin (periph->flags & CAM_PERIPH_INVALID) == 0) 17113393f8daSKenneth D. Merry ccb->ccb_h.retry_count--; 17123393f8daSKenneth D. Merry else { 171383c5d981SAlexander Motin *action_string = "Retries exhausted"; 17143393f8daSKenneth D. Merry goto sense_error_done; 17153393f8daSKenneth D. Merry } 17163393f8daSKenneth D. Merry } 17173393f8daSKenneth D. Merry 17183393f8daSKenneth D. Merry if ((err_action & SS_MASK) >= SS_START) { 17193393f8daSKenneth D. Merry /* 17203393f8daSKenneth D. Merry * Do common portions of commands that 17213393f8daSKenneth D. Merry * use recovery CCBs. 17223393f8daSKenneth D. Merry */ 172383c5d981SAlexander Motin orig_ccb = xpt_alloc_ccb_nowait(); 172483c5d981SAlexander Motin if (orig_ccb == NULL) { 172583c5d981SAlexander Motin *action_string = "Can't allocate recovery CCB"; 17263393f8daSKenneth D. Merry goto sense_error_done; 17273393f8daSKenneth D. Merry } 17281f1158b2SAlexander Motin /* 17291f1158b2SAlexander Motin * Clear freeze flag for original request here, as 17301f1158b2SAlexander Motin * this freeze will be dropped as part of ERESTART. 17311f1158b2SAlexander Motin */ 17321f1158b2SAlexander Motin ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 173313aa56fcSEdward Tomasz Napierala 173413aa56fcSEdward Tomasz Napierala KASSERT(ccb->ccb_h.func_code == XPT_SCSI_IO, 173513aa56fcSEdward Tomasz Napierala ("%s: ccb func_code %#x != XPT_SCSI_IO", 173613aa56fcSEdward Tomasz Napierala __func__, ccb->ccb_h.func_code)); 173713aa56fcSEdward Tomasz Napierala flags = orig_ccb->ccb_h.alloc_flags; 173813aa56fcSEdward Tomasz Napierala bcopy(ccb, orig_ccb, sizeof(struct ccb_scsiio)); 173913aa56fcSEdward Tomasz Napierala orig_ccb->ccb_h.alloc_flags = flags; 17403393f8daSKenneth D. Merry } 17413393f8daSKenneth D. Merry 17423393f8daSKenneth D. Merry switch (err_action & SS_MASK) { 17433393f8daSKenneth D. Merry case SS_NOP: 174483c5d981SAlexander Motin *action_string = "No recovery action needed"; 174500e54d14SKenneth D. Merry error = 0; 174600e54d14SKenneth D. Merry break; 17473393f8daSKenneth D. Merry case SS_RETRY: 174883c5d981SAlexander Motin *action_string = "Retrying command (per sense data)"; 17493393f8daSKenneth D. Merry error = ERESTART; 17508b8a9b1dSJustin T. Gibbs break; 17513393f8daSKenneth D. Merry case SS_FAIL: 175283c5d981SAlexander Motin *action_string = "Unretryable error"; 17533393f8daSKenneth D. Merry break; 17543393f8daSKenneth D. Merry case SS_START: 17553393f8daSKenneth D. Merry { 17563393f8daSKenneth D. Merry int le; 17573393f8daSKenneth D. Merry 17583393f8daSKenneth D. Merry /* 17593393f8daSKenneth D. Merry * Send a start unit command to the device, and 17603393f8daSKenneth D. Merry * then retry the command. 17613393f8daSKenneth D. Merry */ 176283c5d981SAlexander Motin *action_string = "Attempting to start unit"; 176383c5d981SAlexander Motin periph->flags |= CAM_PERIPH_RECOVERY_INPROG; 17643393f8daSKenneth D. Merry 17653393f8daSKenneth D. Merry /* 17663393f8daSKenneth D. Merry * Check for removable media and set 17673393f8daSKenneth D. Merry * load/eject flag appropriately. 17683393f8daSKenneth D. Merry */ 17693393f8daSKenneth D. Merry if (SID_IS_REMOVABLE(&cgd.inq_data)) 17703393f8daSKenneth D. Merry le = TRUE; 17713393f8daSKenneth D. Merry else 17723393f8daSKenneth D. Merry le = FALSE; 17733393f8daSKenneth D. Merry 17743393f8daSKenneth D. Merry scsi_start_stop(&ccb->csio, 17753393f8daSKenneth D. Merry /*retries*/1, 17763393f8daSKenneth D. Merry camperiphdone, 17773393f8daSKenneth D. Merry MSG_SIMPLE_Q_TAG, 17783393f8daSKenneth D. Merry /*start*/TRUE, 17793393f8daSKenneth D. Merry /*load/eject*/le, 17803393f8daSKenneth D. Merry /*immediate*/FALSE, 17813393f8daSKenneth D. Merry SSD_FULL_SIZE, 17823393f8daSKenneth D. Merry /*timeout*/50000); 17833393f8daSKenneth D. Merry break; 17843393f8daSKenneth D. Merry } 17853393f8daSKenneth D. Merry case SS_TUR: 17863393f8daSKenneth D. Merry { 17873393f8daSKenneth D. Merry /* 17883393f8daSKenneth D. Merry * Send a Test Unit Ready to the device. 17893393f8daSKenneth D. Merry * If the 'many' flag is set, we send 120 17903393f8daSKenneth D. Merry * test unit ready commands, one every half 17913393f8daSKenneth D. Merry * second. Otherwise, we just send one TUR. 17923393f8daSKenneth D. Merry * We only want to do this if the retry 17933393f8daSKenneth D. Merry * count has not been exhausted. 17943393f8daSKenneth D. Merry */ 17953393f8daSKenneth D. Merry int retries; 17963393f8daSKenneth D. Merry 1797bae3729bSAlexander Motin if ((err_action & SSQ_MANY) != 0 && (periph->flags & 1798bae3729bSAlexander Motin CAM_PERIPH_RECOVERY_WAIT_FAILED) == 0) { 1799bae3729bSAlexander Motin periph->flags |= CAM_PERIPH_RECOVERY_WAIT; 180083c5d981SAlexander Motin *action_string = "Polling device for readiness"; 18013393f8daSKenneth D. Merry retries = 120; 18023393f8daSKenneth D. Merry } else { 180383c5d981SAlexander Motin *action_string = "Testing device for readiness"; 18043393f8daSKenneth D. Merry retries = 1; 18053393f8daSKenneth D. Merry } 180683c5d981SAlexander Motin periph->flags |= CAM_PERIPH_RECOVERY_INPROG; 18073393f8daSKenneth D. Merry scsi_test_unit_ready(&ccb->csio, 18083393f8daSKenneth D. Merry retries, 18093393f8daSKenneth D. Merry camperiphdone, 18103393f8daSKenneth D. Merry MSG_SIMPLE_Q_TAG, 18113393f8daSKenneth D. Merry SSD_FULL_SIZE, 18123393f8daSKenneth D. Merry /*timeout*/5000); 18133393f8daSKenneth D. Merry 18143393f8daSKenneth D. Merry /* 18153393f8daSKenneth D. Merry * Accomplish our 500ms delay by deferring 18163393f8daSKenneth D. Merry * the release of our device queue appropriately. 18173393f8daSKenneth D. Merry */ 18183393f8daSKenneth D. Merry *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 18193393f8daSKenneth D. Merry *timeout = 500; 18203393f8daSKenneth D. Merry break; 18213393f8daSKenneth D. Merry } 18223393f8daSKenneth D. Merry default: 1823e3c29144SWarner Losh panic("Unhandled error action %x", err_action); 18243393f8daSKenneth D. Merry } 18253393f8daSKenneth D. Merry 18263393f8daSKenneth D. Merry if ((err_action & SS_MASK) >= SS_START) { 18273393f8daSKenneth D. Merry /* 1828bbfa4aa1SAlexander Motin * Drop the priority, so that the recovery 18293393f8daSKenneth D. Merry * CCB is the first to execute. Freeze the queue 18303393f8daSKenneth D. Merry * after this command is sent so that we can 18313393f8daSKenneth D. Merry * restore the old csio and have it queued in 18323393f8daSKenneth D. Merry * the proper order before we release normal 18333393f8daSKenneth D. Merry * transactions to the device. 18343393f8daSKenneth D. Merry */ 183583c5d981SAlexander Motin ccb->ccb_h.pinfo.priority--; 18363393f8daSKenneth D. Merry ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 183783c5d981SAlexander Motin ccb->ccb_h.saved_ccb_ptr = orig_ccb; 18383393f8daSKenneth D. Merry error = ERESTART; 18390191d9b3SAlexander Motin *orig = orig_ccb; 18403393f8daSKenneth D. Merry } 18413393f8daSKenneth D. Merry 18423393f8daSKenneth D. Merry sense_error_done: 18430181d54bSAlexander Motin *action = err_action; 18443393f8daSKenneth D. Merry } 18453393f8daSKenneth D. Merry return (error); 18463393f8daSKenneth D. Merry } 18473393f8daSKenneth D. Merry 18483393f8daSKenneth D. Merry /* 18493393f8daSKenneth D. Merry * Generic error handler. Peripheral drivers usually filter 18501ffe5851SPedro F. Giffuni * out the errors that they handle in a unique manner, then 18513393f8daSKenneth D. Merry * call this function. 18523393f8daSKenneth D. Merry */ 18533393f8daSKenneth D. Merry int 18543393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags, 1855553484aeSWarner Losh u_int32_t sense_flags) 18563393f8daSKenneth D. Merry { 18570181d54bSAlexander Motin struct cam_path *newpath; 18580181d54bSAlexander Motin union ccb *orig_ccb, *scan_ccb; 1859a07e846bSAlexander Motin struct cam_periph *periph; 18603393f8daSKenneth D. Merry const char *action_string; 18613393f8daSKenneth D. Merry cam_status status; 1862a30ecd42SScott Long int frozen, error, openings, devctl_err; 18630181d54bSAlexander Motin u_int32_t action, relsim_flags, timeout; 18643393f8daSKenneth D. Merry 18650181d54bSAlexander Motin action = SSQ_PRINT_SENSE; 1866a07e846bSAlexander Motin periph = xpt_path_periph(ccb->ccb_h.path); 18673393f8daSKenneth D. Merry action_string = NULL; 18683393f8daSKenneth D. Merry status = ccb->ccb_h.status; 18693393f8daSKenneth D. Merry frozen = (status & CAM_DEV_QFRZN) != 0; 18703393f8daSKenneth D. Merry status &= CAM_STATUS_MASK; 1871a30ecd42SScott Long devctl_err = openings = relsim_flags = timeout = 0; 18720191d9b3SAlexander Motin orig_ccb = ccb; 18733393f8daSKenneth D. Merry 1874a30ecd42SScott Long /* Filter the errors that should be reported via devctl */ 1875a30ecd42SScott Long switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 1876a30ecd42SScott Long case CAM_CMD_TIMEOUT: 1877a30ecd42SScott Long case CAM_REQ_ABORTED: 1878a30ecd42SScott Long case CAM_REQ_CMP_ERR: 1879a30ecd42SScott Long case CAM_REQ_TERMIO: 1880a30ecd42SScott Long case CAM_UNREC_HBA_ERROR: 1881a30ecd42SScott Long case CAM_DATA_RUN_ERR: 1882a30ecd42SScott Long case CAM_SCSI_STATUS_ERROR: 1883a30ecd42SScott Long case CAM_ATA_STATUS_ERROR: 1884a30ecd42SScott Long case CAM_SMP_STATUS_ERROR: 1885a30ecd42SScott Long devctl_err++; 1886a30ecd42SScott Long break; 1887a30ecd42SScott Long default: 1888a30ecd42SScott Long break; 1889a30ecd42SScott Long } 1890a30ecd42SScott Long 18913393f8daSKenneth D. Merry switch (status) { 18923393f8daSKenneth D. Merry case CAM_REQ_CMP: 18933393f8daSKenneth D. Merry error = 0; 18940181d54bSAlexander Motin action &= ~SSQ_PRINT_SENSE; 18953393f8daSKenneth D. Merry break; 18963393f8daSKenneth D. Merry case CAM_SCSI_STATUS_ERROR: 18970191d9b3SAlexander Motin error = camperiphscsistatuserror(ccb, &orig_ccb, 18980191d9b3SAlexander Motin camflags, sense_flags, &openings, &relsim_flags, 18990181d54bSAlexander Motin &timeout, &action, &action_string); 19003393f8daSKenneth D. Merry break; 19013393f8daSKenneth D. Merry case CAM_AUTOSENSE_FAIL: 1902c7ec4390SMatt Jacob error = EIO; /* we have to kill the command */ 1903c7ec4390SMatt Jacob break; 19048b8a9b1dSJustin T. Gibbs case CAM_UA_ABORT: 19058b8a9b1dSJustin T. Gibbs case CAM_UA_TERMIO: 19068b8a9b1dSJustin T. Gibbs case CAM_MSG_REJECT_REC: 19078b8a9b1dSJustin T. Gibbs /* XXX Don't know that these are correct */ 19088b8a9b1dSJustin T. Gibbs error = EIO; 19098b8a9b1dSJustin T. Gibbs break; 19108b8a9b1dSJustin T. Gibbs case CAM_SEL_TIMEOUT: 19113393f8daSKenneth D. Merry if ((camflags & CAM_RETRY_SELTO) != 0) { 1912a07e846bSAlexander Motin if (ccb->ccb_h.retry_count > 0 && 1913a07e846bSAlexander Motin (periph->flags & CAM_PERIPH_INVALID) == 0) { 19143393f8daSKenneth D. Merry ccb->ccb_h.retry_count--; 19153393f8daSKenneth D. Merry error = ERESTART; 19163393f8daSKenneth D. Merry 19173393f8daSKenneth D. Merry /* 191873cf209fSMatt Jacob * Wait a bit to give the device 19193393f8daSKenneth D. Merry * time to recover before we try again. 19203393f8daSKenneth D. Merry */ 19213393f8daSKenneth D. Merry relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 192273cf209fSMatt Jacob timeout = periph_selto_delay; 19233393f8daSKenneth D. Merry break; 19243393f8daSKenneth D. Merry } 1925a07e846bSAlexander Motin action_string = "Retries exhausted"; 19263393f8daSKenneth D. Merry } 19278900f4b8SKenneth D. Merry /* FALLTHROUGH */ 19288900f4b8SKenneth D. Merry case CAM_DEV_NOT_THERE: 19293393f8daSKenneth D. Merry error = ENXIO; 19300181d54bSAlexander Motin action = SSQ_LOST; 19318b8a9b1dSJustin T. Gibbs break; 19328b8a9b1dSJustin T. Gibbs case CAM_REQ_INVALID: 19338b8a9b1dSJustin T. Gibbs case CAM_PATH_INVALID: 19348b8a9b1dSJustin T. Gibbs case CAM_NO_HBA: 19358b8a9b1dSJustin T. Gibbs case CAM_PROVIDE_FAIL: 19368b8a9b1dSJustin T. Gibbs case CAM_REQ_TOO_BIG: 19379a014e6fSIan Dowse case CAM_LUN_INVALID: 19389a014e6fSIan Dowse case CAM_TID_INVALID: 1939357478a5SAlexander Motin case CAM_FUNC_NOTAVAIL: 19408b8a9b1dSJustin T. Gibbs error = EINVAL; 19418b8a9b1dSJustin T. Gibbs break; 19428b8a9b1dSJustin T. Gibbs case CAM_SCSI_BUS_RESET: 19438b8a9b1dSJustin T. Gibbs case CAM_BDR_SENT: 19443393f8daSKenneth D. Merry /* 19453393f8daSKenneth D. Merry * Commands that repeatedly timeout and cause these 19463393f8daSKenneth D. Merry * kinds of error recovery actions, should return 19473393f8daSKenneth D. Merry * CAM_CMD_TIMEOUT, which allows us to safely assume 19483393f8daSKenneth D. Merry * that this command was an innocent bystander to 19493393f8daSKenneth D. Merry * these events and should be unconditionally 19503393f8daSKenneth D. Merry * retried. 19513393f8daSKenneth D. Merry */ 19528b8a9b1dSJustin T. Gibbs case CAM_REQUEUE_REQ: 19530191d9b3SAlexander Motin /* Unconditional requeue if device is still there */ 19540191d9b3SAlexander Motin if (periph->flags & CAM_PERIPH_INVALID) { 19550191d9b3SAlexander Motin action_string = "Periph was invalidated"; 1956a07e846bSAlexander Motin error = EIO; 19570191d9b3SAlexander Motin } else if (sense_flags & SF_NO_RETRY) { 19580191d9b3SAlexander Motin error = EIO; 19590191d9b3SAlexander Motin action_string = "Retry was blocked"; 196084c4de2dSAlexander Motin } else { 19610191d9b3SAlexander Motin error = ERESTART; 19620181d54bSAlexander Motin action &= ~SSQ_PRINT_SENSE; 196384c4de2dSAlexander Motin } 19648b8a9b1dSJustin T. Gibbs break; 19658b8a9b1dSJustin T. Gibbs case CAM_RESRC_UNAVAIL: 196673cf209fSMatt Jacob /* Wait a bit for the resource shortage to abate. */ 196773cf209fSMatt Jacob timeout = periph_noresrc_delay; 196873cf209fSMatt Jacob /* FALLTHROUGH */ 19698b8a9b1dSJustin T. Gibbs case CAM_BUSY: 197073cf209fSMatt Jacob if (timeout == 0) { 197173cf209fSMatt Jacob /* Wait a bit for the busy condition to abate. */ 197273cf209fSMatt Jacob timeout = periph_busy_delay; 197373cf209fSMatt Jacob } 197473cf209fSMatt Jacob relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 197573cf209fSMatt Jacob /* FALLTHROUGH */ 19760191d9b3SAlexander Motin case CAM_ATA_STATUS_ERROR: 19770191d9b3SAlexander Motin case CAM_REQ_CMP_ERR: 19780191d9b3SAlexander Motin case CAM_CMD_TIMEOUT: 19790191d9b3SAlexander Motin case CAM_UNEXP_BUSFREE: 19800191d9b3SAlexander Motin case CAM_UNCOR_PARITY: 19810191d9b3SAlexander Motin case CAM_DATA_RUN_ERR: 19828b8a9b1dSJustin T. Gibbs default: 19830191d9b3SAlexander Motin if (periph->flags & CAM_PERIPH_INVALID) { 19840191d9b3SAlexander Motin error = EIO; 19850191d9b3SAlexander Motin action_string = "Periph was invalidated"; 19860191d9b3SAlexander Motin } else if (ccb->ccb_h.retry_count == 0) { 19878b8a9b1dSJustin T. Gibbs error = EIO; 198883c5d981SAlexander Motin action_string = "Retries exhausted"; 19890191d9b3SAlexander Motin } else if (sense_flags & SF_NO_RETRY) { 19900191d9b3SAlexander Motin error = EIO; 19910191d9b3SAlexander Motin action_string = "Retry was blocked"; 19920191d9b3SAlexander Motin } else { 19930191d9b3SAlexander Motin ccb->ccb_h.retry_count--; 19940191d9b3SAlexander Motin error = ERESTART; 19958b8a9b1dSJustin T. Gibbs } 19968b8a9b1dSJustin T. Gibbs break; 19978b8a9b1dSJustin T. Gibbs } 19988b8a9b1dSJustin T. Gibbs 19990191d9b3SAlexander Motin if ((sense_flags & SF_PRINT_ALWAYS) || 20000191d9b3SAlexander Motin CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) 20010181d54bSAlexander Motin action |= SSQ_PRINT_SENSE; 20020191d9b3SAlexander Motin else if (sense_flags & SF_NO_PRINT) 20030181d54bSAlexander Motin action &= ~SSQ_PRINT_SENSE; 20040181d54bSAlexander Motin if ((action & SSQ_PRINT_SENSE) != 0) 20050191d9b3SAlexander Motin cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL); 20060181d54bSAlexander Motin if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) { 20070f3e2159SAlexander Motin if (error != ERESTART) { 20083393f8daSKenneth D. Merry if (action_string == NULL) 200983c5d981SAlexander Motin action_string = "Unretryable error"; 201083c5d981SAlexander Motin xpt_print(ccb->ccb_h.path, "Error %d, %s\n", 201183c5d981SAlexander Motin error, action_string); 201283c5d981SAlexander Motin } else if (action_string != NULL) 2013f0d9af51SMatt Jacob xpt_print(ccb->ccb_h.path, "%s\n", action_string); 2014bc40691eSWarner Losh else { 2015bc40691eSWarner Losh xpt_print(ccb->ccb_h.path, 2016bc40691eSWarner Losh "Retrying command, %d more tries remain\n", 2017bc40691eSWarner Losh ccb->ccb_h.retry_count); 2018bc40691eSWarner Losh } 20190f3e2159SAlexander Motin } 20200f3e2159SAlexander Motin 2021cd375264SAlexander Motin if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0)) 2022a30ecd42SScott Long cam_periph_devctl_notify(orig_ccb); 2023a30ecd42SScott Long 20240181d54bSAlexander Motin if ((action & SSQ_LOST) != 0) { 20250191d9b3SAlexander Motin lun_id_t lun_id; 20260191d9b3SAlexander Motin 20270191d9b3SAlexander Motin /* 20280191d9b3SAlexander Motin * For a selection timeout, we consider all of the LUNs on 20290191d9b3SAlexander Motin * the target to be gone. If the status is CAM_DEV_NOT_THERE, 20300191d9b3SAlexander Motin * then we only get rid of the device(s) specified by the 20310191d9b3SAlexander Motin * path in the original CCB. 20320191d9b3SAlexander Motin */ 20330181d54bSAlexander Motin if (status == CAM_SEL_TIMEOUT) 20340191d9b3SAlexander Motin lun_id = CAM_LUN_WILDCARD; 20350181d54bSAlexander Motin else 20360181d54bSAlexander Motin lun_id = xpt_path_lun_id(ccb->ccb_h.path); 20370191d9b3SAlexander Motin 20380191d9b3SAlexander Motin /* Should we do more if we can't create the path?? */ 20390191d9b3SAlexander Motin if (xpt_create_path(&newpath, periph, 20400191d9b3SAlexander Motin xpt_path_path_id(ccb->ccb_h.path), 20410191d9b3SAlexander Motin xpt_path_target_id(ccb->ccb_h.path), 20420191d9b3SAlexander Motin lun_id) == CAM_REQ_CMP) { 20430191d9b3SAlexander Motin /* 20440191d9b3SAlexander Motin * Let peripheral drivers know that this 20450191d9b3SAlexander Motin * device has gone away. 20460191d9b3SAlexander Motin */ 20470191d9b3SAlexander Motin xpt_async(AC_LOST_DEVICE, newpath, NULL); 20480191d9b3SAlexander Motin xpt_free_path(newpath); 20490191d9b3SAlexander Motin } 20500181d54bSAlexander Motin } 20513631c638SAlexander Motin 20523631c638SAlexander Motin /* Broadcast UNIT ATTENTIONs to all periphs. */ 20530181d54bSAlexander Motin if ((action & SSQ_UA) != 0) 20543631c638SAlexander Motin xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb); 20550181d54bSAlexander Motin 20560181d54bSAlexander Motin /* Rescan target on "Reported LUNs data has changed" */ 20570181d54bSAlexander Motin if ((action & SSQ_RESCAN) != 0) { 20580181d54bSAlexander Motin if (xpt_create_path(&newpath, NULL, 20590181d54bSAlexander Motin xpt_path_path_id(ccb->ccb_h.path), 20600181d54bSAlexander Motin xpt_path_target_id(ccb->ccb_h.path), 2061e5736ac8SAlexander Motin CAM_LUN_WILDCARD) == CAM_REQ_CMP) { 20620181d54bSAlexander Motin scan_ccb = xpt_alloc_ccb_nowait(); 2063e5736ac8SAlexander Motin if (scan_ccb != NULL) { 20640181d54bSAlexander Motin scan_ccb->ccb_h.path = newpath; 2065e5736ac8SAlexander Motin scan_ccb->ccb_h.func_code = XPT_SCAN_TGT; 20660181d54bSAlexander Motin scan_ccb->crcn.flags = 0; 20670181d54bSAlexander Motin xpt_rescan(scan_ccb); 20683231e8bdSAlexander Motin } else { 2069e5736ac8SAlexander Motin xpt_print(newpath, 2070e5736ac8SAlexander Motin "Can't allocate CCB to rescan target\n"); 20713231e8bdSAlexander Motin xpt_free_path(newpath); 20723231e8bdSAlexander Motin } 20730181d54bSAlexander Motin } 20740191d9b3SAlexander Motin } 20750191d9b3SAlexander Motin 20760f3e2159SAlexander Motin /* Attempt a retry */ 20770f3e2159SAlexander Motin if (error == ERESTART || error == 0) { 20780f3e2159SAlexander Motin if (frozen != 0) 20790f3e2159SAlexander Motin ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 20800f3e2159SAlexander Motin if (error == ERESTART) 20810f3e2159SAlexander Motin xpt_action(ccb); 20820f3e2159SAlexander Motin if (frozen != 0) 20830f3e2159SAlexander Motin cam_release_devq(ccb->ccb_h.path, 20840f3e2159SAlexander Motin relsim_flags, 20850f3e2159SAlexander Motin openings, 20860f3e2159SAlexander Motin timeout, 20870f3e2159SAlexander Motin /*getcount_only*/0); 20883393f8daSKenneth D. Merry } 20898b8a9b1dSJustin T. Gibbs 20908b8a9b1dSJustin T. Gibbs return (error); 20918b8a9b1dSJustin T. Gibbs } 2092a30ecd42SScott Long 2093a30ecd42SScott Long #define CAM_PERIPH_DEVD_MSG_SIZE 256 2094a30ecd42SScott Long 2095a30ecd42SScott Long static void 2096a30ecd42SScott Long cam_periph_devctl_notify(union ccb *ccb) 2097a30ecd42SScott Long { 2098a30ecd42SScott Long struct cam_periph *periph; 2099a30ecd42SScott Long struct ccb_getdev *cgd; 2100a30ecd42SScott Long struct sbuf sb; 2101a30ecd42SScott Long int serr, sk, asc, ascq; 2102a30ecd42SScott Long char *sbmsg, *type; 2103a30ecd42SScott Long 2104a30ecd42SScott Long sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT); 2105a30ecd42SScott Long if (sbmsg == NULL) 2106a30ecd42SScott Long return; 2107a30ecd42SScott Long 2108a30ecd42SScott Long sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN); 2109a30ecd42SScott Long 2110a30ecd42SScott Long periph = xpt_path_periph(ccb->ccb_h.path); 2111a30ecd42SScott Long sbuf_printf(&sb, "device=%s%d ", periph->periph_name, 2112a30ecd42SScott Long periph->unit_number); 2113a30ecd42SScott Long 2114a30ecd42SScott Long sbuf_printf(&sb, "serial=\""); 2115a30ecd42SScott Long if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) { 2116a30ecd42SScott Long xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path, 2117a30ecd42SScott Long CAM_PRIORITY_NORMAL); 2118a30ecd42SScott Long cgd->ccb_h.func_code = XPT_GDEV_TYPE; 2119a30ecd42SScott Long xpt_action((union ccb *)cgd); 2120a30ecd42SScott Long 2121a30ecd42SScott Long if (cgd->ccb_h.status == CAM_REQ_CMP) 2122a30ecd42SScott Long sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len); 2123de482e7bSScott Long xpt_free_ccb((union ccb *)cgd); 2124a30ecd42SScott Long } 2125a30ecd42SScott Long sbuf_printf(&sb, "\" "); 2126a30ecd42SScott Long sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status); 2127a30ecd42SScott Long 2128a30ecd42SScott Long switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 2129a30ecd42SScott Long case CAM_CMD_TIMEOUT: 2130a30ecd42SScott Long sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout); 2131a30ecd42SScott Long type = "timeout"; 2132a30ecd42SScott Long break; 2133a30ecd42SScott Long case CAM_SCSI_STATUS_ERROR: 2134a30ecd42SScott Long sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status); 2135a30ecd42SScott Long if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq)) 2136a30ecd42SScott Long sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ", 2137a30ecd42SScott Long serr, sk, asc, ascq); 2138a30ecd42SScott Long type = "error"; 2139a30ecd42SScott Long break; 2140a30ecd42SScott Long case CAM_ATA_STATUS_ERROR: 2141a30ecd42SScott Long sbuf_printf(&sb, "RES=\""); 2142a30ecd42SScott Long ata_res_sbuf(&ccb->ataio.res, &sb); 2143a30ecd42SScott Long sbuf_printf(&sb, "\" "); 2144a30ecd42SScott Long type = "error"; 2145a30ecd42SScott Long break; 2146a30ecd42SScott Long default: 2147a30ecd42SScott Long type = "error"; 2148a30ecd42SScott Long break; 2149a30ecd42SScott Long } 2150a30ecd42SScott Long 2151a30ecd42SScott Long if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2152a30ecd42SScott Long sbuf_printf(&sb, "CDB=\""); 21534902e14dSAlexander Motin scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb); 2154a30ecd42SScott Long sbuf_printf(&sb, "\" "); 2155a30ecd42SScott Long } else if (ccb->ccb_h.func_code == XPT_ATA_IO) { 2156a30ecd42SScott Long sbuf_printf(&sb, "ACB=\""); 2157a30ecd42SScott Long ata_cmd_sbuf(&ccb->ataio.cmd, &sb); 2158a30ecd42SScott Long sbuf_printf(&sb, "\" "); 2159a30ecd42SScott Long } 2160a30ecd42SScott Long 2161a30ecd42SScott Long if (sbuf_finish(&sb) == 0) 2162a30ecd42SScott Long devctl_notify("CAM", "periph", type, sbuf_data(&sb)); 2163a30ecd42SScott Long sbuf_delete(&sb); 2164a30ecd42SScott Long free(sbmsg, M_CAMPERIPH); 2165a30ecd42SScott Long } 2166a30ecd42SScott Long 2167d38677d2SWarner Losh /* 2168d38677d2SWarner Losh * Sysctl to force an invalidation of the drive right now. Can be 2169d38677d2SWarner Losh * called with CTLFLAG_MPSAFE since we take periph lock. 2170d38677d2SWarner Losh */ 2171d38677d2SWarner Losh int 2172d38677d2SWarner Losh cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS) 2173d38677d2SWarner Losh { 2174d38677d2SWarner Losh struct cam_periph *periph; 2175d38677d2SWarner Losh int error, value; 2176d38677d2SWarner Losh 2177d38677d2SWarner Losh periph = arg1; 2178d38677d2SWarner Losh value = 0; 2179d38677d2SWarner Losh error = sysctl_handle_int(oidp, &value, 0, req); 2180d38677d2SWarner Losh if (error != 0 || req->newptr == NULL || value != 1) 2181d38677d2SWarner Losh return (error); 2182d38677d2SWarner Losh 2183d38677d2SWarner Losh cam_periph_lock(periph); 2184d38677d2SWarner Losh cam_periph_invalidate(periph); 2185d38677d2SWarner Losh cam_periph_unlock(periph); 2186d38677d2SWarner Losh 2187d38677d2SWarner Losh return (0); 2188d38677d2SWarner Losh } 2189