1898b0535SWarner Losh /*- 28b8a9b1dSJustin T. Gibbs * Implementation of the Common Access Method Transport (XPT) layer. 38b8a9b1dSJustin T. Gibbs * 4c8bead2aSJustin T. Gibbs * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs. 559190eaaSKenneth D. Merry * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 68b8a9b1dSJustin T. Gibbs * All rights reserved. 78b8a9b1dSJustin T. Gibbs * 88b8a9b1dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 98b8a9b1dSJustin T. Gibbs * modification, are permitted provided that the following conditions 108b8a9b1dSJustin T. Gibbs * are met: 118b8a9b1dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 128b8a9b1dSJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 138b8a9b1dSJustin T. Gibbs * without modification, immediately at the beginning of the file. 148b8a9b1dSJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 158b8a9b1dSJustin T. Gibbs * derived from this software without specific prior written permission. 168b8a9b1dSJustin T. Gibbs * 178b8a9b1dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 188b8a9b1dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198b8a9b1dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208b8a9b1dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 218b8a9b1dSJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228b8a9b1dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238b8a9b1dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248b8a9b1dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258b8a9b1dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268b8a9b1dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278b8a9b1dSJustin T. Gibbs * SUCH DAMAGE. 288b8a9b1dSJustin T. Gibbs */ 299c963d87SDavid E. O'Brien 309c963d87SDavid E. O'Brien #include <sys/cdefs.h> 319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$"); 329c963d87SDavid E. O'Brien 338b8a9b1dSJustin T. Gibbs #include <sys/param.h> 349a94c9c5SJohn Baldwin #include <sys/bus.h> 358b8a9b1dSJustin T. Gibbs #include <sys/systm.h> 368b8a9b1dSJustin T. Gibbs #include <sys/types.h> 378b8a9b1dSJustin T. Gibbs #include <sys/malloc.h> 388b8a9b1dSJustin T. Gibbs #include <sys/kernel.h> 3987cfaf0eSJustin T. Gibbs #include <sys/time.h> 408b8a9b1dSJustin T. Gibbs #include <sys/conf.h> 418b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h> 425d754af7SMatt Jacob #include <sys/interrupt.h> 43227d67aaSAlexander Motin #include <sys/proc.h> 443393f8daSKenneth D. Merry #include <sys/sbuf.h> 45227d67aaSAlexander Motin #include <sys/smp.h> 462b83592fSScott Long #include <sys/taskqueue.h> 478b8a9b1dSJustin T. Gibbs 48ef3cf714SScott Long #include <sys/lock.h> 49ef3cf714SScott Long #include <sys/mutex.h> 503b87a552SMatt Jacob #include <sys/sysctl.h> 519e6461a2SMatt Jacob #include <sys/kthread.h> 52ef3cf714SScott Long 538b8a9b1dSJustin T. Gibbs #include <cam/cam.h> 548b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h> 558b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h> 5652c9ce25SScott Long #include <cam/cam_queue.h> 578b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h> 588b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h> 598b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h> 608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h> 6152c9ce25SScott Long #include <cam/cam_xpt_internal.h> 628b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h> 6325a2902cSScott Long #include <cam/cam_compat.h> 648b8a9b1dSJustin T. Gibbs 658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h> 668b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h> 678b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h> 68abef0e67SMarius Strobl 69abef0e67SMarius Strobl #include <machine/md_var.h> /* geometry translation */ 70f0d9af51SMatt Jacob #include <machine/stdarg.h> /* for xpt_print below */ 71abef0e67SMarius Strobl 728b8a9b1dSJustin T. Gibbs #include "opt_cam.h" 738b8a9b1dSJustin T. Gibbs 7452c9ce25SScott Long /* 7552c9ce25SScott Long * This is the maximum number of high powered commands (e.g. start unit) 7652c9ce25SScott Long * that can be outstanding at a particular time. 7752c9ce25SScott Long */ 7852c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER 7952c9ce25SScott Long #define CAM_MAX_HIGHPOWER 4 8052c9ce25SScott Long #endif 8152c9ce25SScott Long 828b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */ 83362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers"); 84596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices"); 85596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs"); 86596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths"); 878b8a9b1dSJustin T. Gibbs 882b83592fSScott Long /* Object for defering XPT actions to a taskqueue */ 892b83592fSScott Long struct xpt_task { 902b83592fSScott Long struct task task; 9184f82481SScott Long void *data1; 9284f82481SScott Long uintptr_t data2; 932b83592fSScott Long }; 942b83592fSScott Long 958b8a9b1dSJustin T. Gibbs struct xpt_softc { 96636870ffSWill Andrews uint32_t xpt_generation; 97636870ffSWill Andrews 982b83592fSScott Long /* number of high powered commands that can go through right now */ 99daa5487fSAlexander Motin struct mtx xpt_highpower_lock; 100ea541bfdSAlexander Motin STAILQ_HEAD(highpowerlist, cam_ed) highpowerq; 1012b83592fSScott Long int num_highpower; 1022b83592fSScott Long 1032b83592fSScott Long /* queue for handling async rescan requests. */ 1042b83592fSScott Long TAILQ_HEAD(, ccb_hdr) ccb_scanq; 10583c5d981SAlexander Motin int buses_to_config; 10683c5d981SAlexander Motin int buses_config_done; 1072b83592fSScott Long 1082b83592fSScott Long /* Registered busses */ 1092b83592fSScott Long TAILQ_HEAD(,cam_eb) xpt_busses; 1102b83592fSScott Long u_int bus_generation; 1112b83592fSScott Long 1122b83592fSScott Long struct intr_config_hook *xpt_config_hook; 1132b83592fSScott Long 11483c5d981SAlexander Motin int boot_delay; 11583c5d981SAlexander Motin struct callout boot_callout; 11683c5d981SAlexander Motin 1172b83592fSScott Long struct mtx xpt_topo_lock; 1182b83592fSScott Long struct mtx xpt_lock; 119227d67aaSAlexander Motin struct taskqueue *xpt_taskq; 1208b8a9b1dSJustin T. Gibbs }; 1218b8a9b1dSJustin T. Gibbs 1228b8a9b1dSJustin T. Gibbs typedef enum { 1238b8a9b1dSJustin T. Gibbs DM_RET_COPY = 0x01, 1248b8a9b1dSJustin T. Gibbs DM_RET_FLAG_MASK = 0x0f, 1258b8a9b1dSJustin T. Gibbs DM_RET_NONE = 0x00, 1268b8a9b1dSJustin T. Gibbs DM_RET_STOP = 0x10, 1278b8a9b1dSJustin T. Gibbs DM_RET_DESCEND = 0x20, 1288b8a9b1dSJustin T. Gibbs DM_RET_ERROR = 0x30, 1298b8a9b1dSJustin T. Gibbs DM_RET_ACTION_MASK = 0xf0 1308b8a9b1dSJustin T. Gibbs } dev_match_ret; 1318b8a9b1dSJustin T. Gibbs 1328b8a9b1dSJustin T. Gibbs typedef enum { 1338b8a9b1dSJustin T. Gibbs XPT_DEPTH_BUS, 1348b8a9b1dSJustin T. Gibbs XPT_DEPTH_TARGET, 1358b8a9b1dSJustin T. Gibbs XPT_DEPTH_DEVICE, 1368b8a9b1dSJustin T. Gibbs XPT_DEPTH_PERIPH 1378b8a9b1dSJustin T. Gibbs } xpt_traverse_depth; 1388b8a9b1dSJustin T. Gibbs 1398b8a9b1dSJustin T. Gibbs struct xpt_traverse_config { 1408b8a9b1dSJustin T. Gibbs xpt_traverse_depth depth; 1418b8a9b1dSJustin T. Gibbs void *tr_func; 1428b8a9b1dSJustin T. Gibbs void *tr_arg; 1438b8a9b1dSJustin T. Gibbs }; 1448b8a9b1dSJustin T. Gibbs 1458b8a9b1dSJustin T. Gibbs typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg); 1468b8a9b1dSJustin T. Gibbs typedef int xpt_targetfunc_t (struct cam_et *target, void *arg); 1478b8a9b1dSJustin T. Gibbs typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg); 1488b8a9b1dSJustin T. Gibbs typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg); 1498b8a9b1dSJustin T. Gibbs typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg); 1508b8a9b1dSJustin T. Gibbs 1518b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */ 1528b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc; 1538b8a9b1dSJustin T. Gibbs 1545719711fSEdward Tomasz Napierala MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF); 1555719711fSEdward Tomasz Napierala 15683c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN, 15783c5d981SAlexander Motin &xsoftc.boot_delay, 0, "Bus registration wait time"); 158636870ffSWill Andrews SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD, 159636870ffSWill Andrews &xsoftc.xpt_generation, 0, "CAM peripheral generation count"); 16083c5d981SAlexander Motin 161227d67aaSAlexander Motin struct cam_doneq { 162227d67aaSAlexander Motin struct mtx_padalign cam_doneq_mtx; 163227d67aaSAlexander Motin STAILQ_HEAD(, ccb_hdr) cam_doneq; 164227d67aaSAlexander Motin int cam_doneq_sleep; 165227d67aaSAlexander Motin }; 1668b8a9b1dSJustin T. Gibbs 167227d67aaSAlexander Motin static struct cam_doneq cam_doneqs[MAXCPU]; 168227d67aaSAlexander Motin static int cam_num_doneqs; 169227d67aaSAlexander Motin static struct proc *cam_proc; 170227d67aaSAlexander Motin 171227d67aaSAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN, 172227d67aaSAlexander Motin &cam_num_doneqs, 0, "Number of completion queues/threads"); 1738b8a9b1dSJustin T. Gibbs 1749a1c8571SNick Hibma struct cam_periph *xpt_periph; 1759a1c8571SNick Hibma 1768b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init; 1778b8a9b1dSJustin T. Gibbs 1788b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver = 1798b8a9b1dSJustin T. Gibbs { 1808b8a9b1dSJustin T. Gibbs xpt_periph_init, "xpt", 1811e637ba6SAlexander Motin TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0, 1821e637ba6SAlexander Motin CAM_PERIPH_DRV_EARLY 1838b8a9b1dSJustin T. Gibbs }; 1848b8a9b1dSJustin T. Gibbs 1850b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver); 1868b8a9b1dSJustin T. Gibbs 1878b8a9b1dSJustin T. Gibbs static d_open_t xptopen; 1888b8a9b1dSJustin T. Gibbs static d_close_t xptclose; 1898b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl; 19025a2902cSScott Long static d_ioctl_t xptdoioctl; 1918b8a9b1dSJustin T. Gibbs 1924e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = { 193dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1942b83592fSScott Long .d_flags = 0, 1957ac40f5fSPoul-Henning Kamp .d_open = xptopen, 1967ac40f5fSPoul-Henning Kamp .d_close = xptclose, 1977ac40f5fSPoul-Henning Kamp .d_ioctl = xptioctl, 1987ac40f5fSPoul-Henning Kamp .d_name = "xpt", 1998b8a9b1dSJustin T. Gibbs }; 2008b8a9b1dSJustin T. Gibbs 2018b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */ 2028b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath; 20382b361b1SMatt Jacob u_int32_t cam_dflags = CAM_DEBUG_FLAGS; 204af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN, 205f0f25b9cSAlexander Motin &cam_dflags, 0, "Enabled debug flags"); 206f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY; 207af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN, 208f0f25b9cSAlexander Motin &cam_debug_delay, 0, "Delay in us after each debug message"); 2098b8a9b1dSJustin T. Gibbs 2106d2a8f1cSPeter Wemm /* Our boot-time initialization hook */ 21174bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *); 21274bd1c10SNick Hibma 21374bd1c10SNick Hibma static moduledata_t cam_moduledata = { 21474bd1c10SNick Hibma "cam", 21574bd1c10SNick Hibma cam_module_event_handler, 21674bd1c10SNick Hibma NULL 21774bd1c10SNick Hibma }; 21874bd1c10SNick Hibma 2192b83592fSScott Long static int xpt_init(void *); 22074bd1c10SNick Hibma 22174bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 22274bd1c10SNick Hibma MODULE_VERSION(cam, 1); 22374bd1c10SNick Hibma 2248b8a9b1dSJustin T. Gibbs 2258b8a9b1dSJustin T. Gibbs static void xpt_async_bcast(struct async_list *async_head, 2268b8a9b1dSJustin T. Gibbs u_int32_t async_code, 2278b8a9b1dSJustin T. Gibbs struct cam_path *path, 2288b8a9b1dSJustin T. Gibbs void *async_arg); 229434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void); 230434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus); 231227d67aaSAlexander Motin static union ccb *xpt_get_ccb(struct cam_periph *periph); 232227d67aaSAlexander Motin static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph); 233227d67aaSAlexander Motin static void xpt_run_allocq(struct cam_periph *periph, int sleep); 234227d67aaSAlexander Motin static void xpt_run_allocq_task(void *context, int pending); 235cccf4220SAlexander Motin static void xpt_run_devq(struct cam_devq *devq); 2368b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout; 2372b83592fSScott Long static void xpt_release_simq_timeout(void *arg) __unused; 238227d67aaSAlexander Motin static void xpt_acquire_bus(struct cam_eb *bus); 239a5479bc5SJustin T. Gibbs static void xpt_release_bus(struct cam_eb *bus); 240daa5487fSAlexander Motin static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count); 241227d67aaSAlexander Motin static int xpt_release_devq_device(struct cam_ed *dev, u_int count, 242cccf4220SAlexander Motin int run_queue); 2438b8a9b1dSJustin T. Gibbs static struct cam_et* 2448b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id); 245227d67aaSAlexander Motin static void xpt_acquire_target(struct cam_et *target); 246f98d7a47SAlexander Motin static void xpt_release_target(struct cam_et *target); 2478b8a9b1dSJustin T. Gibbs static struct cam_eb* 2488b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id); 2498b8a9b1dSJustin T. Gibbs static struct cam_et* 2508b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t target_id); 2518b8a9b1dSJustin T. Gibbs static struct cam_ed* 2528b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id); 2538b8a9b1dSJustin T. Gibbs static void xpt_config(void *arg); 254227d67aaSAlexander Motin static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo, 255227d67aaSAlexander Motin u_int32_t new_priority); 2568b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc; 2578b8a9b1dSJustin T. Gibbs static void xptaction(struct cam_sim *sim, union ccb *work_ccb); 258434bbf6eSJustin T. Gibbs static void xptpoll(struct cam_sim *sim); 259227d67aaSAlexander Motin static void camisr_runqueue(void); 260227d67aaSAlexander Motin static void xpt_done_process(struct ccb_hdr *ccb_h); 261227d67aaSAlexander Motin static void xpt_done_td(void *); 2628b8a9b1dSJustin T. Gibbs static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns, 2633393f8daSKenneth D. Merry u_int num_patterns, struct cam_eb *bus); 2648b8a9b1dSJustin T. Gibbs static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns, 2653393f8daSKenneth D. Merry u_int num_patterns, 2663393f8daSKenneth D. Merry struct cam_ed *device); 2678b8a9b1dSJustin T. Gibbs static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns, 2683393f8daSKenneth D. Merry u_int num_patterns, 2698b8a9b1dSJustin T. Gibbs struct cam_periph *periph); 2708b8a9b1dSJustin T. Gibbs static xpt_busfunc_t xptedtbusfunc; 2718b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t xptedttargetfunc; 2728b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptedtdevicefunc; 2738b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t xptedtperiphfunc; 2748b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t xptplistpdrvfunc; 2758b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t xptplistperiphfunc; 2768b8a9b1dSJustin T. Gibbs static int xptedtmatch(struct ccb_dev_match *cdm); 2778b8a9b1dSJustin T. Gibbs static int xptperiphlistmatch(struct ccb_dev_match *cdm); 2788b8a9b1dSJustin T. Gibbs static int xptbustraverse(struct cam_eb *start_bus, 2798b8a9b1dSJustin T. Gibbs xpt_busfunc_t *tr_func, void *arg); 2808b8a9b1dSJustin T. Gibbs static int xpttargettraverse(struct cam_eb *bus, 2818b8a9b1dSJustin T. Gibbs struct cam_et *start_target, 2828b8a9b1dSJustin T. Gibbs xpt_targetfunc_t *tr_func, void *arg); 2838b8a9b1dSJustin T. Gibbs static int xptdevicetraverse(struct cam_et *target, 2848b8a9b1dSJustin T. Gibbs struct cam_ed *start_device, 2858b8a9b1dSJustin T. Gibbs xpt_devicefunc_t *tr_func, void *arg); 2868b8a9b1dSJustin T. Gibbs static int xptperiphtraverse(struct cam_ed *device, 2878b8a9b1dSJustin T. Gibbs struct cam_periph *start_periph, 2888b8a9b1dSJustin T. Gibbs xpt_periphfunc_t *tr_func, void *arg); 2898b8a9b1dSJustin T. Gibbs static int xptpdrvtraverse(struct periph_driver **start_pdrv, 2908b8a9b1dSJustin T. Gibbs xpt_pdrvfunc_t *tr_func, void *arg); 2918b8a9b1dSJustin T. Gibbs static int xptpdperiphtraverse(struct periph_driver **pdrv, 2928b8a9b1dSJustin T. Gibbs struct cam_periph *start_periph, 2938b8a9b1dSJustin T. Gibbs xpt_periphfunc_t *tr_func, 2948b8a9b1dSJustin T. Gibbs void *arg); 2958b8a9b1dSJustin T. Gibbs static xpt_busfunc_t xptdefbusfunc; 2968b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t xptdeftargetfunc; 2978b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptdefdevicefunc; 2988b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t xptdefperiphfunc; 29983c5d981SAlexander Motin static void xpt_finishconfig_task(void *context, int pending); 30052c9ce25SScott Long static void xpt_dev_async_default(u_int32_t async_code, 30152c9ce25SScott Long struct cam_eb *bus, 30252c9ce25SScott Long struct cam_et *target, 30352c9ce25SScott Long struct cam_ed *device, 30452c9ce25SScott Long void *async_arg); 30552c9ce25SScott Long static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus, 30652c9ce25SScott Long struct cam_et *target, 30752c9ce25SScott Long lun_id_t lun_id); 3088b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptsetasyncfunc; 3098b8a9b1dSJustin T. Gibbs static xpt_busfunc_t xptsetasyncbusfunc; 3108b8a9b1dSJustin T. Gibbs static cam_status xptregister(struct cam_periph *periph, 3118b8a9b1dSJustin T. Gibbs void *arg); 312cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device); 3138b8a9b1dSJustin T. Gibbs 3148b8a9b1dSJustin T. Gibbs static __inline int 315cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev) 31630a4094fSAlexander Motin { 31730a4094fSAlexander Motin int retval; 31830a4094fSAlexander Motin 319227d67aaSAlexander Motin mtx_assert(&devq->send_mtx, MA_OWNED); 32083c5d981SAlexander Motin if ((dev->ccbq.queue.entries > 0) && 32183c5d981SAlexander Motin (dev->ccbq.dev_openings > 0) && 322cccf4220SAlexander Motin (dev->ccbq.queue.qfrozen_cnt == 0)) { 32330a4094fSAlexander Motin /* 32430a4094fSAlexander Motin * The priority of a device waiting for controller 3256bccea7cSRebecca Cran * resources is that of the highest priority CCB 32630a4094fSAlexander Motin * enqueued. 32730a4094fSAlexander Motin */ 32830a4094fSAlexander Motin retval = 329cccf4220SAlexander Motin xpt_schedule_dev(&devq->send_queue, 330227d67aaSAlexander Motin &dev->devq_entry, 33183c5d981SAlexander Motin CAMQ_GET_PRIO(&dev->ccbq.queue)); 33230a4094fSAlexander Motin } else { 33330a4094fSAlexander Motin retval = 0; 33430a4094fSAlexander Motin } 33530a4094fSAlexander Motin return (retval); 33630a4094fSAlexander Motin } 33730a4094fSAlexander Motin 33830a4094fSAlexander Motin static __inline int 339cccf4220SAlexander Motin device_is_queued(struct cam_ed *device) 3408b8a9b1dSJustin T. Gibbs { 341227d67aaSAlexander Motin return (device->devq_entry.index != CAM_UNQUEUED_INDEX); 3428b8a9b1dSJustin T. Gibbs } 3438b8a9b1dSJustin T. Gibbs 3448b8a9b1dSJustin T. Gibbs static void 3458b8a9b1dSJustin T. Gibbs xpt_periph_init() 3468b8a9b1dSJustin T. Gibbs { 34773d26919SKenneth D. Merry make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0"); 3488b8a9b1dSJustin T. Gibbs } 3498b8a9b1dSJustin T. Gibbs 3508b8a9b1dSJustin T. Gibbs static int 35189c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 3528b8a9b1dSJustin T. Gibbs { 3538b8a9b1dSJustin T. Gibbs 3548b8a9b1dSJustin T. Gibbs /* 35566a0780eSKenneth D. Merry * Only allow read-write access. 35666a0780eSKenneth D. Merry */ 35766a0780eSKenneth D. Merry if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) 35866a0780eSKenneth D. Merry return(EPERM); 35966a0780eSKenneth D. Merry 36066a0780eSKenneth D. Merry /* 3618b8a9b1dSJustin T. Gibbs * We don't allow nonblocking access. 3628b8a9b1dSJustin T. Gibbs */ 3638b8a9b1dSJustin T. Gibbs if ((flags & O_NONBLOCK) != 0) { 3642b83592fSScott Long printf("%s: can't do nonblocking access\n", devtoname(dev)); 3658b8a9b1dSJustin T. Gibbs return(ENODEV); 3668b8a9b1dSJustin T. Gibbs } 3678b8a9b1dSJustin T. Gibbs 3688b8a9b1dSJustin T. Gibbs return(0); 3698b8a9b1dSJustin T. Gibbs } 3708b8a9b1dSJustin T. Gibbs 3718b8a9b1dSJustin T. Gibbs static int 37289c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 3738b8a9b1dSJustin T. Gibbs { 3748b8a9b1dSJustin T. Gibbs 3758b8a9b1dSJustin T. Gibbs return(0); 3768b8a9b1dSJustin T. Gibbs } 3778b8a9b1dSJustin T. Gibbs 3782b83592fSScott Long /* 3792b83592fSScott Long * Don't automatically grab the xpt softc lock here even though this is going 3802b83592fSScott Long * through the xpt device. The xpt device is really just a back door for 3812b83592fSScott Long * accessing other devices and SIMs, so the right thing to do is to grab 3822b83592fSScott Long * the appropriate SIM lock once the bus/SIM is located. 3832b83592fSScott Long */ 3848b8a9b1dSJustin T. Gibbs static int 38589c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 3868b8a9b1dSJustin T. Gibbs { 3872b83592fSScott Long int error; 3888b8a9b1dSJustin T. Gibbs 38925a2902cSScott Long if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 390f564de00SScott Long error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl); 39125a2902cSScott Long } 39225a2902cSScott Long return (error); 39325a2902cSScott Long } 39425a2902cSScott Long 39525a2902cSScott Long static int 39625a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 39725a2902cSScott Long { 39825a2902cSScott Long int error; 39925a2902cSScott Long 4008b8a9b1dSJustin T. Gibbs error = 0; 4018b8a9b1dSJustin T. Gibbs 4028b8a9b1dSJustin T. Gibbs switch(cmd) { 4038b8a9b1dSJustin T. Gibbs /* 4048b8a9b1dSJustin T. Gibbs * For the transport layer CAMIOCOMMAND ioctl, we really only want 4058b8a9b1dSJustin T. Gibbs * to accept CCB types that don't quite make sense to send through a 4068c7a96c5SScott Long * passthrough driver. XPT_PATH_INQ is an exception to this, as stated 4078c7a96c5SScott Long * in the CAM spec. 4088b8a9b1dSJustin T. Gibbs */ 4098b8a9b1dSJustin T. Gibbs case CAMIOCOMMAND: { 4108b8a9b1dSJustin T. Gibbs union ccb *ccb; 4118b8a9b1dSJustin T. Gibbs union ccb *inccb; 4122b83592fSScott Long struct cam_eb *bus; 4138b8a9b1dSJustin T. Gibbs 4148b8a9b1dSJustin T. Gibbs inccb = (union ccb *)addr; 4158b8a9b1dSJustin T. Gibbs 4162b83592fSScott Long bus = xpt_find_bus(inccb->ccb_h.path_id); 4170e85f214SMatt Jacob if (bus == NULL) 4180e85f214SMatt Jacob return (EINVAL); 4190e85f214SMatt Jacob 4200e85f214SMatt Jacob switch (inccb->ccb_h.func_code) { 4210e85f214SMatt Jacob case XPT_SCAN_BUS: 4220e85f214SMatt Jacob case XPT_RESET_BUS: 4230e85f214SMatt Jacob if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD || 4240e85f214SMatt Jacob inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 4250e85f214SMatt Jacob xpt_release_bus(bus); 4260e85f214SMatt Jacob return (EINVAL); 4270e85f214SMatt Jacob } 4280e85f214SMatt Jacob break; 4290e85f214SMatt Jacob case XPT_SCAN_TGT: 4300e85f214SMatt Jacob if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD || 4310e85f214SMatt Jacob inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 4320e85f214SMatt Jacob xpt_release_bus(bus); 4330e85f214SMatt Jacob return (EINVAL); 4340e85f214SMatt Jacob } 4350e85f214SMatt Jacob break; 4360e85f214SMatt Jacob default: 4372b83592fSScott Long break; 4382b83592fSScott Long } 4392b83592fSScott Long 4408b8a9b1dSJustin T. Gibbs switch(inccb->ccb_h.func_code) { 4418b8a9b1dSJustin T. Gibbs case XPT_SCAN_BUS: 4428b8a9b1dSJustin T. Gibbs case XPT_RESET_BUS: 4438c7a96c5SScott Long case XPT_PATH_INQ: 4448fcf57f5SJustin T. Gibbs case XPT_ENG_INQ: 4458b8a9b1dSJustin T. Gibbs case XPT_SCAN_LUN: 4460e85f214SMatt Jacob case XPT_SCAN_TGT: 4478b8a9b1dSJustin T. Gibbs 4488008a935SScott Long ccb = xpt_alloc_ccb(); 4492b83592fSScott Long 4508b8a9b1dSJustin T. Gibbs /* 4518b8a9b1dSJustin T. Gibbs * Create a path using the bus, target, and lun the 4528b8a9b1dSJustin T. Gibbs * user passed in. 4538b8a9b1dSJustin T. Gibbs */ 454e5dfa058SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, 4558b8a9b1dSJustin T. Gibbs inccb->ccb_h.path_id, 4568b8a9b1dSJustin T. Gibbs inccb->ccb_h.target_id, 4578b8a9b1dSJustin T. Gibbs inccb->ccb_h.target_lun) != 4588b8a9b1dSJustin T. Gibbs CAM_REQ_CMP){ 4598b8a9b1dSJustin T. Gibbs error = EINVAL; 4608b8a9b1dSJustin T. Gibbs xpt_free_ccb(ccb); 4618b8a9b1dSJustin T. Gibbs break; 4628b8a9b1dSJustin T. Gibbs } 4638b8a9b1dSJustin T. Gibbs /* Ensure all of our fields are correct */ 4648b8a9b1dSJustin T. Gibbs xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 4658b8a9b1dSJustin T. Gibbs inccb->ccb_h.pinfo.priority); 4668b8a9b1dSJustin T. Gibbs xpt_merge_ccb(ccb, inccb); 467227d67aaSAlexander Motin xpt_path_lock(ccb->ccb_h.path); 4688b8a9b1dSJustin T. Gibbs cam_periph_runccb(ccb, NULL, 0, 0, NULL); 469227d67aaSAlexander Motin xpt_path_unlock(ccb->ccb_h.path); 4708b8a9b1dSJustin T. Gibbs bcopy(ccb, inccb, sizeof(union ccb)); 4718b8a9b1dSJustin T. Gibbs xpt_free_path(ccb->ccb_h.path); 4728b8a9b1dSJustin T. Gibbs xpt_free_ccb(ccb); 4738b8a9b1dSJustin T. Gibbs break; 4748b8a9b1dSJustin T. Gibbs 4758b8a9b1dSJustin T. Gibbs case XPT_DEBUG: { 4768b8a9b1dSJustin T. Gibbs union ccb ccb; 4778b8a9b1dSJustin T. Gibbs 4788b8a9b1dSJustin T. Gibbs /* 479aa872be6SMatt Jacob * This is an immediate CCB, so it's okay to 4808b8a9b1dSJustin T. Gibbs * allocate it on the stack. 4818b8a9b1dSJustin T. Gibbs */ 4828b8a9b1dSJustin T. Gibbs 4838b8a9b1dSJustin T. Gibbs /* 4848b8a9b1dSJustin T. Gibbs * Create a path using the bus, target, and lun the 4858b8a9b1dSJustin T. Gibbs * user passed in. 4868b8a9b1dSJustin T. Gibbs */ 487e5dfa058SAlexander Motin if (xpt_create_path(&ccb.ccb_h.path, NULL, 4888b8a9b1dSJustin T. Gibbs inccb->ccb_h.path_id, 4898b8a9b1dSJustin T. Gibbs inccb->ccb_h.target_id, 4908b8a9b1dSJustin T. Gibbs inccb->ccb_h.target_lun) != 4918b8a9b1dSJustin T. Gibbs CAM_REQ_CMP){ 4928b8a9b1dSJustin T. Gibbs error = EINVAL; 4938b8a9b1dSJustin T. Gibbs break; 4948b8a9b1dSJustin T. Gibbs } 4958b8a9b1dSJustin T. Gibbs /* Ensure all of our fields are correct */ 4968b8a9b1dSJustin T. Gibbs xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path, 4978b8a9b1dSJustin T. Gibbs inccb->ccb_h.pinfo.priority); 4988b8a9b1dSJustin T. Gibbs xpt_merge_ccb(&ccb, inccb); 4998b8a9b1dSJustin T. Gibbs xpt_action(&ccb); 5008b8a9b1dSJustin T. Gibbs bcopy(&ccb, inccb, sizeof(union ccb)); 5018b8a9b1dSJustin T. Gibbs xpt_free_path(ccb.ccb_h.path); 5028b8a9b1dSJustin T. Gibbs break; 5038b8a9b1dSJustin T. Gibbs 5048b8a9b1dSJustin T. Gibbs } 5058b8a9b1dSJustin T. Gibbs case XPT_DEV_MATCH: { 5068b8a9b1dSJustin T. Gibbs struct cam_periph_map_info mapinfo; 50759190eaaSKenneth D. Merry struct cam_path *old_path; 5088b8a9b1dSJustin T. Gibbs 5098b8a9b1dSJustin T. Gibbs /* 5108b8a9b1dSJustin T. Gibbs * We can't deal with physical addresses for this 5118b8a9b1dSJustin T. Gibbs * type of transaction. 5128b8a9b1dSJustin T. Gibbs */ 513dd0b4fb6SKonstantin Belousov if ((inccb->ccb_h.flags & CAM_DATA_MASK) != 514dd0b4fb6SKonstantin Belousov CAM_DATA_VADDR) { 5158b8a9b1dSJustin T. Gibbs error = EINVAL; 5168b8a9b1dSJustin T. Gibbs break; 5178b8a9b1dSJustin T. Gibbs } 51859190eaaSKenneth D. Merry 51959190eaaSKenneth D. Merry /* 52059190eaaSKenneth D. Merry * Save this in case the caller had it set to 52159190eaaSKenneth D. Merry * something in particular. 52259190eaaSKenneth D. Merry */ 52359190eaaSKenneth D. Merry old_path = inccb->ccb_h.path; 52459190eaaSKenneth D. Merry 52559190eaaSKenneth D. Merry /* 52659190eaaSKenneth D. Merry * We really don't need a path for the matching 52759190eaaSKenneth D. Merry * code. The path is needed because of the 52859190eaaSKenneth D. Merry * debugging statements in xpt_action(). They 52959190eaaSKenneth D. Merry * assume that the CCB has a valid path. 53059190eaaSKenneth D. Merry */ 53159190eaaSKenneth D. Merry inccb->ccb_h.path = xpt_periph->path; 53259190eaaSKenneth D. Merry 5338b8a9b1dSJustin T. Gibbs bzero(&mapinfo, sizeof(mapinfo)); 5348b8a9b1dSJustin T. Gibbs 5358b8a9b1dSJustin T. Gibbs /* 5368b8a9b1dSJustin T. Gibbs * Map the pattern and match buffers into kernel 5378b8a9b1dSJustin T. Gibbs * virtual address space. 5388b8a9b1dSJustin T. Gibbs */ 539*de239312SAlexander Motin error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS); 5408b8a9b1dSJustin T. Gibbs 54159190eaaSKenneth D. Merry if (error) { 54259190eaaSKenneth D. Merry inccb->ccb_h.path = old_path; 5438b8a9b1dSJustin T. Gibbs break; 54459190eaaSKenneth D. Merry } 5458b8a9b1dSJustin T. Gibbs 5468b8a9b1dSJustin T. Gibbs /* 5478b8a9b1dSJustin T. Gibbs * This is an immediate CCB, we can send it on directly. 5488b8a9b1dSJustin T. Gibbs */ 5498b8a9b1dSJustin T. Gibbs xpt_action(inccb); 5508b8a9b1dSJustin T. Gibbs 5518b8a9b1dSJustin T. Gibbs /* 5528b8a9b1dSJustin T. Gibbs * Map the buffers back into user space. 5538b8a9b1dSJustin T. Gibbs */ 5548b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(inccb, &mapinfo); 5558b8a9b1dSJustin T. Gibbs 55659190eaaSKenneth D. Merry inccb->ccb_h.path = old_path; 55759190eaaSKenneth D. Merry 5588b8a9b1dSJustin T. Gibbs error = 0; 5598b8a9b1dSJustin T. Gibbs break; 5608b8a9b1dSJustin T. Gibbs } 5618b8a9b1dSJustin T. Gibbs default: 5628fcf57f5SJustin T. Gibbs error = ENOTSUP; 5638b8a9b1dSJustin T. Gibbs break; 5648b8a9b1dSJustin T. Gibbs } 565daddc001SScott Long xpt_release_bus(bus); 5668b8a9b1dSJustin T. Gibbs break; 5678b8a9b1dSJustin T. Gibbs } 5688b8a9b1dSJustin T. Gibbs /* 5698b8a9b1dSJustin T. Gibbs * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input, 5708b8a9b1dSJustin T. Gibbs * with the periphal driver name and unit name filled in. The other 5718b8a9b1dSJustin T. Gibbs * fields don't really matter as input. The passthrough driver name 5728b8a9b1dSJustin T. Gibbs * ("pass"), and unit number are passed back in the ccb. The current 5738b8a9b1dSJustin T. Gibbs * device generation number, and the index into the device peripheral 5748b8a9b1dSJustin T. Gibbs * driver list, and the status are also passed back. Note that 5758b8a9b1dSJustin T. Gibbs * since we do everything in one pass, unlike the XPT_GDEVLIST ccb, 5768b8a9b1dSJustin T. Gibbs * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is 5778b8a9b1dSJustin T. Gibbs * (or rather should be) impossible for the device peripheral driver 5788b8a9b1dSJustin T. Gibbs * list to change since we look at the whole thing in one pass, and 57977dc25ccSScott Long * we do it with lock protection. 5808b8a9b1dSJustin T. Gibbs * 5818b8a9b1dSJustin T. Gibbs */ 5828b8a9b1dSJustin T. Gibbs case CAMGETPASSTHRU: { 5838b8a9b1dSJustin T. Gibbs union ccb *ccb; 5848b8a9b1dSJustin T. Gibbs struct cam_periph *periph; 5858b8a9b1dSJustin T. Gibbs struct periph_driver **p_drv; 5868b8a9b1dSJustin T. Gibbs char *name; 5873393f8daSKenneth D. Merry u_int unit; 588621a60d4SKenneth D. Merry int base_periph_found; 5898b8a9b1dSJustin T. Gibbs 5908b8a9b1dSJustin T. Gibbs ccb = (union ccb *)addr; 5918b8a9b1dSJustin T. Gibbs unit = ccb->cgdl.unit_number; 5928b8a9b1dSJustin T. Gibbs name = ccb->cgdl.periph_name; 593621a60d4SKenneth D. Merry base_periph_found = 0; 594621a60d4SKenneth D. Merry 5958b8a9b1dSJustin T. Gibbs /* 5968b8a9b1dSJustin T. Gibbs * Sanity check -- make sure we don't get a null peripheral 5978b8a9b1dSJustin T. Gibbs * driver name. 5988b8a9b1dSJustin T. Gibbs */ 5998b8a9b1dSJustin T. Gibbs if (*ccb->cgdl.periph_name == '\0') { 6008b8a9b1dSJustin T. Gibbs error = EINVAL; 6018b8a9b1dSJustin T. Gibbs break; 6028b8a9b1dSJustin T. Gibbs } 6038b8a9b1dSJustin T. Gibbs 6048b8a9b1dSJustin T. Gibbs /* Keep the list from changing while we traverse it */ 6059a7c2696SAlexander Motin xpt_lock_buses(); 6068b8a9b1dSJustin T. Gibbs 6078b8a9b1dSJustin T. Gibbs /* first find our driver in the list of drivers */ 6080b7c27b9SPeter Wemm for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) 6098b8a9b1dSJustin T. Gibbs if (strcmp((*p_drv)->driver_name, name) == 0) 6108b8a9b1dSJustin T. Gibbs break; 6118b8a9b1dSJustin T. Gibbs 6128b8a9b1dSJustin T. Gibbs if (*p_drv == NULL) { 6139a7c2696SAlexander Motin xpt_unlock_buses(); 6148b8a9b1dSJustin T. Gibbs ccb->ccb_h.status = CAM_REQ_CMP_ERR; 6158b8a9b1dSJustin T. Gibbs ccb->cgdl.status = CAM_GDEVLIST_ERROR; 6168b8a9b1dSJustin T. Gibbs *ccb->cgdl.periph_name = '\0'; 6178b8a9b1dSJustin T. Gibbs ccb->cgdl.unit_number = 0; 6188b8a9b1dSJustin T. Gibbs error = ENOENT; 6198b8a9b1dSJustin T. Gibbs break; 6208b8a9b1dSJustin T. Gibbs } 6218b8a9b1dSJustin T. Gibbs 6228b8a9b1dSJustin T. Gibbs /* 6238b8a9b1dSJustin T. Gibbs * Run through every peripheral instance of this driver 6248b8a9b1dSJustin T. Gibbs * and check to see whether it matches the unit passed 6258b8a9b1dSJustin T. Gibbs * in by the user. If it does, get out of the loops and 6268b8a9b1dSJustin T. Gibbs * find the passthrough driver associated with that 6278b8a9b1dSJustin T. Gibbs * peripheral driver. 6288b8a9b1dSJustin T. Gibbs */ 6298b8a9b1dSJustin T. Gibbs for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL; 6308b8a9b1dSJustin T. Gibbs periph = TAILQ_NEXT(periph, unit_links)) { 6318b8a9b1dSJustin T. Gibbs 632a29779e8SAlexander Motin if (periph->unit_number == unit) 6338b8a9b1dSJustin T. Gibbs break; 6348b8a9b1dSJustin T. Gibbs } 6358b8a9b1dSJustin T. Gibbs /* 6368b8a9b1dSJustin T. Gibbs * If we found the peripheral driver that the user passed 6378b8a9b1dSJustin T. Gibbs * in, go through all of the peripheral drivers for that 6388b8a9b1dSJustin T. Gibbs * particular device and look for a passthrough driver. 6398b8a9b1dSJustin T. Gibbs */ 6408b8a9b1dSJustin T. Gibbs if (periph != NULL) { 6418b8a9b1dSJustin T. Gibbs struct cam_ed *device; 6428b8a9b1dSJustin T. Gibbs int i; 6438b8a9b1dSJustin T. Gibbs 644621a60d4SKenneth D. Merry base_periph_found = 1; 6458b8a9b1dSJustin T. Gibbs device = periph->path->device; 646fc2ffbe6SPoul-Henning Kamp for (i = 0, periph = SLIST_FIRST(&device->periphs); 6478b8a9b1dSJustin T. Gibbs periph != NULL; 648fc2ffbe6SPoul-Henning Kamp periph = SLIST_NEXT(periph, periph_links), i++) { 6498b8a9b1dSJustin T. Gibbs /* 6508b8a9b1dSJustin T. Gibbs * Check to see whether we have a 6518b8a9b1dSJustin T. Gibbs * passthrough device or not. 6528b8a9b1dSJustin T. Gibbs */ 6538b8a9b1dSJustin T. Gibbs if (strcmp(periph->periph_name, "pass") == 0) { 6548b8a9b1dSJustin T. Gibbs /* 6558b8a9b1dSJustin T. Gibbs * Fill in the getdevlist fields. 6568b8a9b1dSJustin T. Gibbs */ 6578b8a9b1dSJustin T. Gibbs strcpy(ccb->cgdl.periph_name, 6588b8a9b1dSJustin T. Gibbs periph->periph_name); 6598b8a9b1dSJustin T. Gibbs ccb->cgdl.unit_number = 6608b8a9b1dSJustin T. Gibbs periph->unit_number; 661fc2ffbe6SPoul-Henning Kamp if (SLIST_NEXT(periph, periph_links)) 6628b8a9b1dSJustin T. Gibbs ccb->cgdl.status = 6638b8a9b1dSJustin T. Gibbs CAM_GDEVLIST_MORE_DEVS; 6648b8a9b1dSJustin T. Gibbs else 6658b8a9b1dSJustin T. Gibbs ccb->cgdl.status = 6668b8a9b1dSJustin T. Gibbs CAM_GDEVLIST_LAST_DEVICE; 6678b8a9b1dSJustin T. Gibbs ccb->cgdl.generation = 6688b8a9b1dSJustin T. Gibbs device->generation; 6698b8a9b1dSJustin T. Gibbs ccb->cgdl.index = i; 6708b8a9b1dSJustin T. Gibbs /* 6718b8a9b1dSJustin T. Gibbs * Fill in some CCB header fields 6728b8a9b1dSJustin T. Gibbs * that the user may want. 6738b8a9b1dSJustin T. Gibbs */ 6748b8a9b1dSJustin T. Gibbs ccb->ccb_h.path_id = 6758b8a9b1dSJustin T. Gibbs periph->path->bus->path_id; 6768b8a9b1dSJustin T. Gibbs ccb->ccb_h.target_id = 6778b8a9b1dSJustin T. Gibbs periph->path->target->target_id; 6788b8a9b1dSJustin T. Gibbs ccb->ccb_h.target_lun = 6798b8a9b1dSJustin T. Gibbs periph->path->device->lun_id; 6808b8a9b1dSJustin T. Gibbs ccb->ccb_h.status = CAM_REQ_CMP; 6818b8a9b1dSJustin T. Gibbs break; 6828b8a9b1dSJustin T. Gibbs } 6838b8a9b1dSJustin T. Gibbs } 6848b8a9b1dSJustin T. Gibbs } 6858b8a9b1dSJustin T. Gibbs 6868b8a9b1dSJustin T. Gibbs /* 6878b8a9b1dSJustin T. Gibbs * If the periph is null here, one of two things has 6888b8a9b1dSJustin T. Gibbs * happened. The first possibility is that we couldn't 6898b8a9b1dSJustin T. Gibbs * find the unit number of the particular peripheral driver 6908b8a9b1dSJustin T. Gibbs * that the user is asking about. e.g. the user asks for 6918b8a9b1dSJustin T. Gibbs * the passthrough driver for "da11". We find the list of 6928b8a9b1dSJustin T. Gibbs * "da" peripherals all right, but there is no unit 11. 6938b8a9b1dSJustin T. Gibbs * The other possibility is that we went through the list 6948b8a9b1dSJustin T. Gibbs * of peripheral drivers attached to the device structure, 6958b8a9b1dSJustin T. Gibbs * but didn't find one with the name "pass". Either way, 6968b8a9b1dSJustin T. Gibbs * we return ENOENT, since we couldn't find something. 6978b8a9b1dSJustin T. Gibbs */ 6988b8a9b1dSJustin T. Gibbs if (periph == NULL) { 6998b8a9b1dSJustin T. Gibbs ccb->ccb_h.status = CAM_REQ_CMP_ERR; 7008b8a9b1dSJustin T. Gibbs ccb->cgdl.status = CAM_GDEVLIST_ERROR; 7018b8a9b1dSJustin T. Gibbs *ccb->cgdl.periph_name = '\0'; 7028b8a9b1dSJustin T. Gibbs ccb->cgdl.unit_number = 0; 7038b8a9b1dSJustin T. Gibbs error = ENOENT; 704621a60d4SKenneth D. Merry /* 705621a60d4SKenneth D. Merry * It is unfortunate that this is even necessary, 706621a60d4SKenneth D. Merry * but there are many, many clueless users out there. 707621a60d4SKenneth D. Merry * If this is true, the user is looking for the 708621a60d4SKenneth D. Merry * passthrough driver, but doesn't have one in his 709621a60d4SKenneth D. Merry * kernel. 710621a60d4SKenneth D. Merry */ 711621a60d4SKenneth D. Merry if (base_periph_found == 1) { 712621a60d4SKenneth D. Merry printf("xptioctl: pass driver is not in the " 713621a60d4SKenneth D. Merry "kernel\n"); 714935c968aSChristian Brueffer printf("xptioctl: put \"device pass\" in " 715621a60d4SKenneth D. Merry "your kernel config file\n"); 716621a60d4SKenneth D. Merry } 7178b8a9b1dSJustin T. Gibbs } 7189a7c2696SAlexander Motin xpt_unlock_buses(); 7198b8a9b1dSJustin T. Gibbs break; 7208b8a9b1dSJustin T. Gibbs } 7218b8a9b1dSJustin T. Gibbs default: 7228b8a9b1dSJustin T. Gibbs error = ENOTTY; 7238b8a9b1dSJustin T. Gibbs break; 7248b8a9b1dSJustin T. Gibbs } 7258b8a9b1dSJustin T. Gibbs 7268b8a9b1dSJustin T. Gibbs return(error); 7278b8a9b1dSJustin T. Gibbs } 7288b8a9b1dSJustin T. Gibbs 72974bd1c10SNick Hibma static int 73074bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg) 73174bd1c10SNick Hibma { 7322b83592fSScott Long int error; 7332b83592fSScott Long 7342b83592fSScott Long switch (what) { 7352b83592fSScott Long case MOD_LOAD: 7362b83592fSScott Long if ((error = xpt_init(NULL)) != 0) 7372b83592fSScott Long return (error); 7382b83592fSScott Long break; 7392b83592fSScott Long case MOD_UNLOAD: 74074bd1c10SNick Hibma return EBUSY; 7412b83592fSScott Long default: 7423e019deaSPoul-Henning Kamp return EOPNOTSUPP; 74374bd1c10SNick Hibma } 74474bd1c10SNick Hibma 74574bd1c10SNick Hibma return 0; 74674bd1c10SNick Hibma } 74774bd1c10SNick Hibma 74883c5d981SAlexander Motin static void 74983c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb) 75083c5d981SAlexander Motin { 75183c5d981SAlexander Motin 75283c5d981SAlexander Motin if (done_ccb->ccb_h.ppriv_ptr1 == NULL) { 75383c5d981SAlexander Motin xpt_free_path(done_ccb->ccb_h.path); 75483c5d981SAlexander Motin xpt_free_ccb(done_ccb); 75583c5d981SAlexander Motin } else { 75683c5d981SAlexander Motin done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1; 75783c5d981SAlexander Motin (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb); 75883c5d981SAlexander Motin } 75983c5d981SAlexander Motin xpt_release_boot(); 76083c5d981SAlexander Motin } 76183c5d981SAlexander Motin 7629e6461a2SMatt Jacob /* thread to handle bus rescans */ 7639e6461a2SMatt Jacob static void 7649e6461a2SMatt Jacob xpt_scanner_thread(void *dummy) 7659e6461a2SMatt Jacob { 7669e6461a2SMatt Jacob union ccb *ccb; 767227d67aaSAlexander Motin struct cam_path path; 7682b83592fSScott Long 7692b83592fSScott Long xpt_lock_buses(); 77083c5d981SAlexander Motin for (;;) { 7715a73cc12SKenneth D. Merry if (TAILQ_EMPTY(&xsoftc.ccb_scanq)) 7722b83592fSScott Long msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO, 7733710ae64SAlexander Motin "-", 0); 77483c5d981SAlexander Motin if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) { 77583c5d981SAlexander Motin TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 7762b83592fSScott Long xpt_unlock_buses(); 7772b83592fSScott Long 778227d67aaSAlexander Motin /* 779227d67aaSAlexander Motin * Since lock can be dropped inside and path freed 780227d67aaSAlexander Motin * by completion callback even before return here, 781227d67aaSAlexander Motin * take our own path copy for reference. 782227d67aaSAlexander Motin */ 783227d67aaSAlexander Motin xpt_copy_path(&path, ccb->ccb_h.path); 784227d67aaSAlexander Motin xpt_path_lock(&path); 78583c5d981SAlexander Motin xpt_action(ccb); 786227d67aaSAlexander Motin xpt_path_unlock(&path); 787227d67aaSAlexander Motin xpt_release_path(&path); 78883c5d981SAlexander Motin 78983c5d981SAlexander Motin xpt_lock_buses(); 7909e6461a2SMatt Jacob } 7919e6461a2SMatt Jacob } 7929e6461a2SMatt Jacob } 7939e6461a2SMatt Jacob 7949e6461a2SMatt Jacob void 7959e6461a2SMatt Jacob xpt_rescan(union ccb *ccb) 7969e6461a2SMatt Jacob { 7979e6461a2SMatt Jacob struct ccb_hdr *hdr; 7982b83592fSScott Long 79983c5d981SAlexander Motin /* Prepare request */ 8000e85f214SMatt Jacob if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD && 801411cadaeSAlexander Motin ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 80283c5d981SAlexander Motin ccb->ccb_h.func_code = XPT_SCAN_BUS; 8030e85f214SMatt Jacob else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 8040e85f214SMatt Jacob ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 8050e85f214SMatt Jacob ccb->ccb_h.func_code = XPT_SCAN_TGT; 8060e85f214SMatt Jacob else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 8070e85f214SMatt Jacob ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD) 80883c5d981SAlexander Motin ccb->ccb_h.func_code = XPT_SCAN_LUN; 8090e85f214SMatt Jacob else { 8100e85f214SMatt Jacob xpt_print(ccb->ccb_h.path, "illegal scan path\n"); 8110e85f214SMatt Jacob xpt_free_path(ccb->ccb_h.path); 8120e85f214SMatt Jacob xpt_free_ccb(ccb); 8130e85f214SMatt Jacob return; 8140e85f214SMatt Jacob } 81583c5d981SAlexander Motin ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp; 81683c5d981SAlexander Motin ccb->ccb_h.cbfcnp = xpt_rescan_done; 81783c5d981SAlexander Motin xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT); 81883c5d981SAlexander Motin /* Don't make duplicate entries for the same paths. */ 8192b83592fSScott Long xpt_lock_buses(); 82083c5d981SAlexander Motin if (ccb->ccb_h.ppriv_ptr1 == NULL) { 8212b83592fSScott Long TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) { 8229e6461a2SMatt Jacob if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) { 8235a73cc12SKenneth D. Merry wakeup(&xsoftc.ccb_scanq); 8242b83592fSScott Long xpt_unlock_buses(); 8259e6461a2SMatt Jacob xpt_print(ccb->ccb_h.path, "rescan already queued\n"); 8269e6461a2SMatt Jacob xpt_free_path(ccb->ccb_h.path); 8279e6461a2SMatt Jacob xpt_free_ccb(ccb); 8289e6461a2SMatt Jacob return; 8299e6461a2SMatt Jacob } 8309e6461a2SMatt Jacob } 83183c5d981SAlexander Motin } 8322b83592fSScott Long TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 83383c5d981SAlexander Motin xsoftc.buses_to_config++; 8342b83592fSScott Long wakeup(&xsoftc.ccb_scanq); 8352b83592fSScott Long xpt_unlock_buses(); 8369e6461a2SMatt Jacob } 8379e6461a2SMatt Jacob 8388b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */ 8392b83592fSScott Long static int 8409e6461a2SMatt Jacob xpt_init(void *dummy) 8418b8a9b1dSJustin T. Gibbs { 8428b8a9b1dSJustin T. Gibbs struct cam_sim *xpt_sim; 8438b8a9b1dSJustin T. Gibbs struct cam_path *path; 844434bbf6eSJustin T. Gibbs struct cam_devq *devq; 8458b8a9b1dSJustin T. Gibbs cam_status status; 846227d67aaSAlexander Motin int error, i; 8478b8a9b1dSJustin T. Gibbs 8482b83592fSScott Long TAILQ_INIT(&xsoftc.xpt_busses); 8492b83592fSScott Long TAILQ_INIT(&xsoftc.ccb_scanq); 8502b83592fSScott Long STAILQ_INIT(&xsoftc.highpowerq); 8512b83592fSScott Long xsoftc.num_highpower = CAM_MAX_HIGHPOWER; 8528b8a9b1dSJustin T. Gibbs 8532b83592fSScott Long mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF); 854daa5487fSAlexander Motin mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF); 855227d67aaSAlexander Motin xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK, 856227d67aaSAlexander Motin taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq); 857ef3cf714SScott Long 8586b156f61SSean Bruno #ifdef CAM_BOOT_DELAY 8596b156f61SSean Bruno /* 8606b156f61SSean Bruno * Override this value at compile time to assist our users 8616b156f61SSean Bruno * who don't use loader to boot a kernel. 8626b156f61SSean Bruno */ 8636b156f61SSean Bruno xsoftc.boot_delay = CAM_BOOT_DELAY; 8646b156f61SSean Bruno #endif 8658b8a9b1dSJustin T. Gibbs /* 8668b8a9b1dSJustin T. Gibbs * The xpt layer is, itself, the equivelent of a SIM. 8678b8a9b1dSJustin T. Gibbs * Allow 16 ccbs in the ccb pool for it. This should 8688b8a9b1dSJustin T. Gibbs * give decent parallelism when we probe busses and 8698b8a9b1dSJustin T. Gibbs * perform other XPT functions. 8708b8a9b1dSJustin T. Gibbs */ 871434bbf6eSJustin T. Gibbs devq = cam_simq_alloc(16); 872434bbf6eSJustin T. Gibbs xpt_sim = cam_sim_alloc(xptaction, 873434bbf6eSJustin T. Gibbs xptpoll, 874434bbf6eSJustin T. Gibbs "xpt", 875434bbf6eSJustin T. Gibbs /*softc*/NULL, 876434bbf6eSJustin T. Gibbs /*unit*/0, 8772b83592fSScott Long /*mtx*/&xsoftc.xpt_lock, 878434bbf6eSJustin T. Gibbs /*max_dev_transactions*/0, 879434bbf6eSJustin T. Gibbs /*max_tagged_dev_transactions*/0, 880434bbf6eSJustin T. Gibbs devq); 8812b83592fSScott Long if (xpt_sim == NULL) 8822b83592fSScott Long return (ENOMEM); 8838b8a9b1dSJustin T. Gibbs 8842b83592fSScott Long mtx_lock(&xsoftc.xpt_lock); 885b50569b7SScott Long if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) { 88683c5d981SAlexander Motin mtx_unlock(&xsoftc.xpt_lock); 887a2821e04SMatt Jacob printf("xpt_init: xpt_bus_register failed with status %#x," 888df826980SMatt Jacob " failing attach\n", status); 8892b83592fSScott Long return (EINVAL); 890df826980SMatt Jacob } 891daa5487fSAlexander Motin mtx_unlock(&xsoftc.xpt_lock); 8928b8a9b1dSJustin T. Gibbs 8938b8a9b1dSJustin T. Gibbs /* 8948b8a9b1dSJustin T. Gibbs * Looking at the XPT from the SIM layer, the XPT is 8958b8a9b1dSJustin T. Gibbs * the equivelent of a peripheral driver. Allocate 8968b8a9b1dSJustin T. Gibbs * a peripheral driver entry for us. 8978b8a9b1dSJustin T. Gibbs */ 8988b8a9b1dSJustin T. Gibbs if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID, 8998b8a9b1dSJustin T. Gibbs CAM_TARGET_WILDCARD, 9008b8a9b1dSJustin T. Gibbs CAM_LUN_WILDCARD)) != CAM_REQ_CMP) { 9018b8a9b1dSJustin T. Gibbs printf("xpt_init: xpt_create_path failed with status %#x," 9028b8a9b1dSJustin T. Gibbs " failing attach\n", status); 9032b83592fSScott Long return (EINVAL); 9048b8a9b1dSJustin T. Gibbs } 905daa5487fSAlexander Motin xpt_path_lock(path); 906ee9c90c7SKenneth D. Merry cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO, 9072b83592fSScott Long path, NULL, 0, xpt_sim); 908daa5487fSAlexander Motin xpt_path_unlock(path); 9098b8a9b1dSJustin T. Gibbs xpt_free_path(path); 910daa5487fSAlexander Motin 911227d67aaSAlexander Motin if (cam_num_doneqs < 1) 912227d67aaSAlexander Motin cam_num_doneqs = 1 + mp_ncpus / 6; 913227d67aaSAlexander Motin else if (cam_num_doneqs > MAXCPU) 914227d67aaSAlexander Motin cam_num_doneqs = MAXCPU; 915227d67aaSAlexander Motin for (i = 0; i < cam_num_doneqs; i++) { 916227d67aaSAlexander Motin mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL, 917227d67aaSAlexander Motin MTX_DEF); 918227d67aaSAlexander Motin STAILQ_INIT(&cam_doneqs[i].cam_doneq); 919227d67aaSAlexander Motin error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i], 920227d67aaSAlexander Motin &cam_proc, NULL, 0, 0, "cam", "doneq%d", i); 921227d67aaSAlexander Motin if (error != 0) { 922227d67aaSAlexander Motin cam_num_doneqs = i; 923227d67aaSAlexander Motin break; 924227d67aaSAlexander Motin } 925227d67aaSAlexander Motin } 926227d67aaSAlexander Motin if (cam_num_doneqs < 1) { 927227d67aaSAlexander Motin printf("xpt_init: Cannot init completion queues " 928227d67aaSAlexander Motin "- failing attach\n"); 929227d67aaSAlexander Motin return (ENOMEM); 930227d67aaSAlexander Motin } 9318b8a9b1dSJustin T. Gibbs /* 9328b8a9b1dSJustin T. Gibbs * Register a callback for when interrupts are enabled. 9338b8a9b1dSJustin T. Gibbs */ 9342b83592fSScott Long xsoftc.xpt_config_hook = 9358b8a9b1dSJustin T. Gibbs (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook), 9360dd50e9bSScott Long M_CAMXPT, M_NOWAIT | M_ZERO); 9372b83592fSScott Long if (xsoftc.xpt_config_hook == NULL) { 9388b8a9b1dSJustin T. Gibbs printf("xpt_init: Cannot malloc config hook " 9398b8a9b1dSJustin T. Gibbs "- failing attach\n"); 9402b83592fSScott Long return (ENOMEM); 9418b8a9b1dSJustin T. Gibbs } 9422b83592fSScott Long xsoftc.xpt_config_hook->ich_func = xpt_config; 9432b83592fSScott Long if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) { 9440dd50e9bSScott Long free (xsoftc.xpt_config_hook, M_CAMXPT); 9458b8a9b1dSJustin T. Gibbs printf("xpt_init: config_intrhook_establish failed " 9468b8a9b1dSJustin T. Gibbs "- failing attach\n"); 9478b8a9b1dSJustin T. Gibbs } 9488b8a9b1dSJustin T. Gibbs 9492b83592fSScott Long return (0); 9508b8a9b1dSJustin T. Gibbs } 9518b8a9b1dSJustin T. Gibbs 9528b8a9b1dSJustin T. Gibbs static cam_status 9538b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg) 9548b8a9b1dSJustin T. Gibbs { 9552b83592fSScott Long struct cam_sim *xpt_sim; 9562b83592fSScott Long 9578b8a9b1dSJustin T. Gibbs if (periph == NULL) { 9588b8a9b1dSJustin T. Gibbs printf("xptregister: periph was NULL!!\n"); 9598b8a9b1dSJustin T. Gibbs return(CAM_REQ_CMP_ERR); 9608b8a9b1dSJustin T. Gibbs } 9618b8a9b1dSJustin T. Gibbs 9622b83592fSScott Long xpt_sim = (struct cam_sim *)arg; 9632b83592fSScott Long xpt_sim->softc = periph; 9648b8a9b1dSJustin T. Gibbs xpt_periph = periph; 9652b83592fSScott Long periph->softc = NULL; 9668b8a9b1dSJustin T. Gibbs 9678b8a9b1dSJustin T. Gibbs return(CAM_REQ_CMP); 9688b8a9b1dSJustin T. Gibbs } 9698b8a9b1dSJustin T. Gibbs 9708b8a9b1dSJustin T. Gibbs int32_t 9718b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph) 9728b8a9b1dSJustin T. Gibbs { 9738b8a9b1dSJustin T. Gibbs struct cam_ed *device; 9748b8a9b1dSJustin T. Gibbs int32_t status; 9758b8a9b1dSJustin T. Gibbs 976227d67aaSAlexander Motin TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph); 9778b8a9b1dSJustin T. Gibbs device = periph->path->device; 9788b8a9b1dSJustin T. Gibbs status = CAM_REQ_CMP; 9798b8a9b1dSJustin T. Gibbs if (device != NULL) { 980227d67aaSAlexander Motin mtx_lock(&device->target->bus->eb_mtx); 9818b8a9b1dSJustin T. Gibbs device->generation++; 982227d67aaSAlexander Motin SLIST_INSERT_HEAD(&device->periphs, periph, periph_links); 983227d67aaSAlexander Motin mtx_unlock(&device->target->bus->eb_mtx); 984636870ffSWill Andrews atomic_add_32(&xsoftc.xpt_generation, 1); 9858b8a9b1dSJustin T. Gibbs } 9868b8a9b1dSJustin T. Gibbs 9878b8a9b1dSJustin T. Gibbs return (status); 9888b8a9b1dSJustin T. Gibbs } 9898b8a9b1dSJustin T. Gibbs 9908b8a9b1dSJustin T. Gibbs void 991a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph) 9928b8a9b1dSJustin T. Gibbs { 9938b8a9b1dSJustin T. Gibbs struct cam_ed *device; 9948b8a9b1dSJustin T. Gibbs 9958b8a9b1dSJustin T. Gibbs device = periph->path->device; 9968b8a9b1dSJustin T. Gibbs if (device != NULL) { 997227d67aaSAlexander Motin mtx_lock(&device->target->bus->eb_mtx); 9988b8a9b1dSJustin T. Gibbs device->generation++; 999227d67aaSAlexander Motin SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links); 1000227d67aaSAlexander Motin mtx_unlock(&device->target->bus->eb_mtx); 1001636870ffSWill Andrews atomic_add_32(&xsoftc.xpt_generation, 1); 10028b8a9b1dSJustin T. Gibbs } 10038b8a9b1dSJustin T. Gibbs } 10048b8a9b1dSJustin T. Gibbs 10053393f8daSKenneth D. Merry 10063393f8daSKenneth D. Merry void 10073393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string) 10083393f8daSKenneth D. Merry { 100957079b17SAlexander Motin struct cam_path *path = periph->path; 10103393f8daSKenneth D. Merry 1011227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 10128d36a71bSAlexander Motin periph->flags |= CAM_PERIPH_ANNOUNCED; 101368153f43SScott Long 1014abe83505SNathan Whitehorn printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n", 10153393f8daSKenneth D. Merry periph->periph_name, periph->unit_number, 10163393f8daSKenneth D. Merry path->bus->sim->sim_name, 10173393f8daSKenneth D. Merry path->bus->sim->unit_number, 10183393f8daSKenneth D. Merry path->bus->sim->bus_id, 1019ad413009SAlexander Motin path->bus->path_id, 10203393f8daSKenneth D. Merry path->target->target_id, 1021abe83505SNathan Whitehorn (uintmax_t)path->device->lun_id); 10223393f8daSKenneth D. Merry printf("%s%d: ", periph->periph_name, periph->unit_number); 102352c9ce25SScott Long if (path->device->protocol == PROTO_SCSI) 10243393f8daSKenneth D. Merry scsi_print_inquiry(&path->device->inq_data); 102552c9ce25SScott Long else if (path->device->protocol == PROTO_ATA || 102652c9ce25SScott Long path->device->protocol == PROTO_SATAPM) 102752c9ce25SScott Long ata_print_ident(&path->device->ident_data); 10283089bb2eSAlexander Motin else if (path->device->protocol == PROTO_SEMB) 10293089bb2eSAlexander Motin semb_print_ident( 10303089bb2eSAlexander Motin (struct sep_identify_data *)&path->device->ident_data); 103152c9ce25SScott Long else 103252c9ce25SScott Long printf("Unknown protocol device\n"); 1033aa93041dSAlexander Motin if (path->device->serial_num_len > 0) { 10343393f8daSKenneth D. Merry /* Don't wrap the screen - print only the first 60 chars */ 10353393f8daSKenneth D. Merry printf("%s%d: Serial Number %.60s\n", periph->periph_name, 10363393f8daSKenneth D. Merry periph->unit_number, path->device->serial_num); 10373393f8daSKenneth D. Merry } 103857079b17SAlexander Motin /* Announce transport details. */ 103957079b17SAlexander Motin (*(path->bus->xport->announce))(periph); 104057079b17SAlexander Motin /* Announce command queueing. */ 10413393f8daSKenneth D. Merry if (path->device->inq_flags & SID_CmdQue 10423393f8daSKenneth D. Merry || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) { 10430aacc535SAlexander Motin printf("%s%d: Command Queueing enabled\n", 10443393f8daSKenneth D. Merry periph->periph_name, periph->unit_number); 10453393f8daSKenneth D. Merry } 104657079b17SAlexander Motin /* Announce caller's details if they've passed in. */ 10473393f8daSKenneth D. Merry if (announce_string != NULL) 10483393f8daSKenneth D. Merry printf("%s%d: %s\n", periph->periph_name, 10493393f8daSKenneth D. Merry periph->unit_number, announce_string); 10503393f8daSKenneth D. Merry } 10518b8a9b1dSJustin T. Gibbs 10526fb5c84eSSteven Hartland void 10536fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string) 10546fb5c84eSSteven Hartland { 10556fb5c84eSSteven Hartland if (quirks != 0) { 10566fb5c84eSSteven Hartland printf("%s%d: quirks=0x%b\n", periph->periph_name, 10576fb5c84eSSteven Hartland periph->unit_number, quirks, bit_string); 10586fb5c84eSSteven Hartland } 10596fb5c84eSSteven Hartland } 10606fb5c84eSSteven Hartland 10618d36a71bSAlexander Motin void 10628d36a71bSAlexander Motin xpt_denounce_periph(struct cam_periph *periph) 10638d36a71bSAlexander Motin { 10648d36a71bSAlexander Motin struct cam_path *path = periph->path; 10658d36a71bSAlexander Motin 1066227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 1067abe83505SNathan Whitehorn printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n", 10688d36a71bSAlexander Motin periph->periph_name, periph->unit_number, 10698d36a71bSAlexander Motin path->bus->sim->sim_name, 10708d36a71bSAlexander Motin path->bus->sim->unit_number, 10718d36a71bSAlexander Motin path->bus->sim->bus_id, 10728d36a71bSAlexander Motin path->bus->path_id, 10738d36a71bSAlexander Motin path->target->target_id, 1074abe83505SNathan Whitehorn (uintmax_t)path->device->lun_id); 10758d36a71bSAlexander Motin printf("%s%d: ", periph->periph_name, periph->unit_number); 10768d36a71bSAlexander Motin if (path->device->protocol == PROTO_SCSI) 10778d36a71bSAlexander Motin scsi_print_inquiry_short(&path->device->inq_data); 10788d36a71bSAlexander Motin else if (path->device->protocol == PROTO_ATA || 10798d36a71bSAlexander Motin path->device->protocol == PROTO_SATAPM) 10808d36a71bSAlexander Motin ata_print_ident_short(&path->device->ident_data); 10818d36a71bSAlexander Motin else if (path->device->protocol == PROTO_SEMB) 10828d36a71bSAlexander Motin semb_print_ident_short( 10838d36a71bSAlexander Motin (struct sep_identify_data *)&path->device->ident_data); 10848d36a71bSAlexander Motin else 10858d36a71bSAlexander Motin printf("Unknown protocol device"); 10868d36a71bSAlexander Motin if (path->device->serial_num_len > 0) 10878d36a71bSAlexander Motin printf(" s/n %.60s", path->device->serial_num); 10888d36a71bSAlexander Motin printf(" detached\n"); 10898d36a71bSAlexander Motin } 10908d36a71bSAlexander Motin 10918d36a71bSAlexander Motin 10923501942bSJustin T. Gibbs int 10933501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path) 10943501942bSJustin T. Gibbs { 1095ccba7102SAlexander Motin int ret = -1, l; 10963501942bSJustin T. Gibbs struct ccb_dev_advinfo cdai; 1097ccba7102SAlexander Motin struct scsi_vpd_id_descriptor *idd; 10983501942bSJustin T. Gibbs 1099227d67aaSAlexander Motin xpt_path_assert(path, MA_OWNED); 11006884b662SAlexander Motin 11013501942bSJustin T. Gibbs memset(&cdai, 0, sizeof(cdai)); 11023501942bSJustin T. Gibbs xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL); 11033501942bSJustin T. Gibbs cdai.ccb_h.func_code = XPT_DEV_ADVINFO; 11043501942bSJustin T. Gibbs cdai.bufsiz = len; 11053501942bSJustin T. Gibbs 11063501942bSJustin T. Gibbs if (!strcmp(attr, "GEOM::ident")) 11073501942bSJustin T. Gibbs cdai.buftype = CDAI_TYPE_SERIAL_NUM; 11083501942bSJustin T. Gibbs else if (!strcmp(attr, "GEOM::physpath")) 11093501942bSJustin T. Gibbs cdai.buftype = CDAI_TYPE_PHYS_PATH; 111040f27d7cSAlexander Motin else if (strcmp(attr, "GEOM::lunid") == 0 || 111140f27d7cSAlexander Motin strcmp(attr, "GEOM::lunname") == 0) { 1112ccba7102SAlexander Motin cdai.buftype = CDAI_TYPE_SCSI_DEVID; 1113ccba7102SAlexander Motin cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN; 1114ccba7102SAlexander Motin } else 11153501942bSJustin T. Gibbs goto out; 11163501942bSJustin T. Gibbs 11173501942bSJustin T. Gibbs cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO); 11183501942bSJustin T. Gibbs if (cdai.buf == NULL) { 11193501942bSJustin T. Gibbs ret = ENOMEM; 11203501942bSJustin T. Gibbs goto out; 11213501942bSJustin T. Gibbs } 11223501942bSJustin T. Gibbs xpt_action((union ccb *)&cdai); /* can only be synchronous */ 11233501942bSJustin T. Gibbs if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0) 11243501942bSJustin T. Gibbs cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE); 11253501942bSJustin T. Gibbs if (cdai.provsiz == 0) 11263501942bSJustin T. Gibbs goto out; 1127ccba7102SAlexander Motin if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) { 112840f27d7cSAlexander Motin if (strcmp(attr, "GEOM::lunid") == 0) { 1129ccba7102SAlexander Motin idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf, 1130ccba7102SAlexander Motin cdai.provsiz, scsi_devid_is_lun_naa); 1131ccba7102SAlexander Motin if (idd == NULL) 1132ccba7102SAlexander Motin idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf, 1133ccba7102SAlexander Motin cdai.provsiz, scsi_devid_is_lun_eui64); 113440f27d7cSAlexander Motin } else 113540f27d7cSAlexander Motin idd = NULL; 1136ccba7102SAlexander Motin if (idd == NULL) 1137ccba7102SAlexander Motin idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf, 1138ccba7102SAlexander Motin cdai.provsiz, scsi_devid_is_lun_t10); 1139ccba7102SAlexander Motin if (idd == NULL) 1140ccba7102SAlexander Motin idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf, 1141ccba7102SAlexander Motin cdai.provsiz, scsi_devid_is_lun_name); 1142ccba7102SAlexander Motin if (idd == NULL) 1143ccba7102SAlexander Motin goto out; 1144ccba7102SAlexander Motin ret = 0; 1145fa91cabfSAlexander Motin if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) { 1146fa91cabfSAlexander Motin if (idd->length < len) { 1147fa91cabfSAlexander Motin for (l = 0; l < idd->length; l++) 1148fa91cabfSAlexander Motin buf[l] = idd->identifier[l] ? 1149fa91cabfSAlexander Motin idd->identifier[l] : ' '; 1150fa91cabfSAlexander Motin buf[l] = 0; 1151fa91cabfSAlexander Motin } else 1152fa91cabfSAlexander Motin ret = EFAULT; 1153fa91cabfSAlexander Motin } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) { 1154ccba7102SAlexander Motin l = strnlen(idd->identifier, idd->length); 1155ccba7102SAlexander Motin if (l < len) { 1156ccba7102SAlexander Motin bcopy(idd->identifier, buf, l); 1157ccba7102SAlexander Motin buf[l] = 0; 1158ccba7102SAlexander Motin } else 1159ccba7102SAlexander Motin ret = EFAULT; 1160ccba7102SAlexander Motin } else { 1161ccba7102SAlexander Motin if (idd->length * 2 < len) { 1162ccba7102SAlexander Motin for (l = 0; l < idd->length; l++) 1163ccba7102SAlexander Motin sprintf(buf + l * 2, "%02x", 1164ccba7102SAlexander Motin idd->identifier[l]); 1165ccba7102SAlexander Motin } else 1166ccba7102SAlexander Motin ret = EFAULT; 1167ccba7102SAlexander Motin } 1168ccba7102SAlexander Motin } else { 11693501942bSJustin T. Gibbs ret = 0; 11703501942bSJustin T. Gibbs if (strlcpy(buf, cdai.buf, len) >= len) 11713501942bSJustin T. Gibbs ret = EFAULT; 1172ccba7102SAlexander Motin } 11733501942bSJustin T. Gibbs 11743501942bSJustin T. Gibbs out: 11753501942bSJustin T. Gibbs if (cdai.buf != NULL) 11763501942bSJustin T. Gibbs free(cdai.buf, M_CAMXPT); 11773501942bSJustin T. Gibbs return ret; 11783501942bSJustin T. Gibbs } 11793501942bSJustin T. Gibbs 11808b8a9b1dSJustin T. Gibbs static dev_match_ret 11813393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns, 11828b8a9b1dSJustin T. Gibbs struct cam_eb *bus) 11838b8a9b1dSJustin T. Gibbs { 11848b8a9b1dSJustin T. Gibbs dev_match_ret retval; 11858b8a9b1dSJustin T. Gibbs int i; 11868b8a9b1dSJustin T. Gibbs 11878b8a9b1dSJustin T. Gibbs retval = DM_RET_NONE; 11888b8a9b1dSJustin T. Gibbs 11898b8a9b1dSJustin T. Gibbs /* 11908b8a9b1dSJustin T. Gibbs * If we aren't given something to match against, that's an error. 11918b8a9b1dSJustin T. Gibbs */ 11928b8a9b1dSJustin T. Gibbs if (bus == NULL) 11938b8a9b1dSJustin T. Gibbs return(DM_RET_ERROR); 11948b8a9b1dSJustin T. Gibbs 11958b8a9b1dSJustin T. Gibbs /* 11968b8a9b1dSJustin T. Gibbs * If there are no match entries, then this bus matches no 11978b8a9b1dSJustin T. Gibbs * matter what. 11988b8a9b1dSJustin T. Gibbs */ 11998b8a9b1dSJustin T. Gibbs if ((patterns == NULL) || (num_patterns == 0)) 12008b8a9b1dSJustin T. Gibbs return(DM_RET_DESCEND | DM_RET_COPY); 12018b8a9b1dSJustin T. Gibbs 12028b8a9b1dSJustin T. Gibbs for (i = 0; i < num_patterns; i++) { 12038b8a9b1dSJustin T. Gibbs struct bus_match_pattern *cur_pattern; 12048b8a9b1dSJustin T. Gibbs 12058b8a9b1dSJustin T. Gibbs /* 12068b8a9b1dSJustin T. Gibbs * If the pattern in question isn't for a bus node, we 12078b8a9b1dSJustin T. Gibbs * aren't interested. However, we do indicate to the 12088b8a9b1dSJustin T. Gibbs * calling routine that we should continue descending the 12098b8a9b1dSJustin T. Gibbs * tree, since the user wants to match against lower-level 12108b8a9b1dSJustin T. Gibbs * EDT elements. 12118b8a9b1dSJustin T. Gibbs */ 12128b8a9b1dSJustin T. Gibbs if (patterns[i].type != DEV_MATCH_BUS) { 12138b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 12148b8a9b1dSJustin T. Gibbs retval |= DM_RET_DESCEND; 12158b8a9b1dSJustin T. Gibbs continue; 12168b8a9b1dSJustin T. Gibbs } 12178b8a9b1dSJustin T. Gibbs 12188b8a9b1dSJustin T. Gibbs cur_pattern = &patterns[i].pattern.bus_pattern; 12198b8a9b1dSJustin T. Gibbs 12208b8a9b1dSJustin T. Gibbs /* 12218b8a9b1dSJustin T. Gibbs * If they want to match any bus node, we give them any 12228b8a9b1dSJustin T. Gibbs * device node. 12238b8a9b1dSJustin T. Gibbs */ 12248b8a9b1dSJustin T. Gibbs if (cur_pattern->flags == BUS_MATCH_ANY) { 12258b8a9b1dSJustin T. Gibbs /* set the copy flag */ 12268b8a9b1dSJustin T. Gibbs retval |= DM_RET_COPY; 12278b8a9b1dSJustin T. Gibbs 12288b8a9b1dSJustin T. Gibbs /* 12298b8a9b1dSJustin T. Gibbs * If we've already decided on an action, go ahead 12308b8a9b1dSJustin T. Gibbs * and return. 12318b8a9b1dSJustin T. Gibbs */ 12328b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 12338b8a9b1dSJustin T. Gibbs return(retval); 12348b8a9b1dSJustin T. Gibbs } 12358b8a9b1dSJustin T. Gibbs 12368b8a9b1dSJustin T. Gibbs /* 12378b8a9b1dSJustin T. Gibbs * Not sure why someone would do this... 12388b8a9b1dSJustin T. Gibbs */ 12398b8a9b1dSJustin T. Gibbs if (cur_pattern->flags == BUS_MATCH_NONE) 12408b8a9b1dSJustin T. Gibbs continue; 12418b8a9b1dSJustin T. Gibbs 12428b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & BUS_MATCH_PATH) != 0) 12438b8a9b1dSJustin T. Gibbs && (cur_pattern->path_id != bus->path_id)) 12448b8a9b1dSJustin T. Gibbs continue; 12458b8a9b1dSJustin T. Gibbs 12468b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0) 12478b8a9b1dSJustin T. Gibbs && (cur_pattern->bus_id != bus->sim->bus_id)) 12488b8a9b1dSJustin T. Gibbs continue; 12498b8a9b1dSJustin T. Gibbs 12508b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0) 12518b8a9b1dSJustin T. Gibbs && (cur_pattern->unit_number != bus->sim->unit_number)) 12528b8a9b1dSJustin T. Gibbs continue; 12538b8a9b1dSJustin T. Gibbs 12548b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & BUS_MATCH_NAME) != 0) 12558b8a9b1dSJustin T. Gibbs && (strncmp(cur_pattern->dev_name, bus->sim->sim_name, 12568b8a9b1dSJustin T. Gibbs DEV_IDLEN) != 0)) 12578b8a9b1dSJustin T. Gibbs continue; 12588b8a9b1dSJustin T. Gibbs 12598b8a9b1dSJustin T. Gibbs /* 12608b8a9b1dSJustin T. Gibbs * If we get to this point, the user definitely wants 12618b8a9b1dSJustin T. Gibbs * information on this bus. So tell the caller to copy the 12628b8a9b1dSJustin T. Gibbs * data out. 12638b8a9b1dSJustin T. Gibbs */ 12648b8a9b1dSJustin T. Gibbs retval |= DM_RET_COPY; 12658b8a9b1dSJustin T. Gibbs 12668b8a9b1dSJustin T. Gibbs /* 12678b8a9b1dSJustin T. Gibbs * If the return action has been set to descend, then we 12688b8a9b1dSJustin T. Gibbs * know that we've already seen a non-bus matching 12698b8a9b1dSJustin T. Gibbs * expression, therefore we need to further descend the tree. 12708b8a9b1dSJustin T. Gibbs * This won't change by continuing around the loop, so we 12718b8a9b1dSJustin T. Gibbs * go ahead and return. If we haven't seen a non-bus 12728b8a9b1dSJustin T. Gibbs * matching expression, we keep going around the loop until 12738b8a9b1dSJustin T. Gibbs * we exhaust the matching expressions. We'll set the stop 12748b8a9b1dSJustin T. Gibbs * flag once we fall out of the loop. 12758b8a9b1dSJustin T. Gibbs */ 12768b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 12778b8a9b1dSJustin T. Gibbs return(retval); 12788b8a9b1dSJustin T. Gibbs } 12798b8a9b1dSJustin T. Gibbs 12808b8a9b1dSJustin T. Gibbs /* 12818b8a9b1dSJustin T. Gibbs * If the return action hasn't been set to descend yet, that means 12828b8a9b1dSJustin T. Gibbs * we haven't seen anything other than bus matching patterns. So 12838b8a9b1dSJustin T. Gibbs * tell the caller to stop descending the tree -- the user doesn't 12848b8a9b1dSJustin T. Gibbs * want to match against lower level tree elements. 12858b8a9b1dSJustin T. Gibbs */ 12868b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 12878b8a9b1dSJustin T. Gibbs retval |= DM_RET_STOP; 12888b8a9b1dSJustin T. Gibbs 12898b8a9b1dSJustin T. Gibbs return(retval); 12908b8a9b1dSJustin T. Gibbs } 12918b8a9b1dSJustin T. Gibbs 12928b8a9b1dSJustin T. Gibbs static dev_match_ret 12933393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns, 12948b8a9b1dSJustin T. Gibbs struct cam_ed *device) 12958b8a9b1dSJustin T. Gibbs { 12968b8a9b1dSJustin T. Gibbs dev_match_ret retval; 12978b8a9b1dSJustin T. Gibbs int i; 12988b8a9b1dSJustin T. Gibbs 12998b8a9b1dSJustin T. Gibbs retval = DM_RET_NONE; 13008b8a9b1dSJustin T. Gibbs 13018b8a9b1dSJustin T. Gibbs /* 13028b8a9b1dSJustin T. Gibbs * If we aren't given something to match against, that's an error. 13038b8a9b1dSJustin T. Gibbs */ 13048b8a9b1dSJustin T. Gibbs if (device == NULL) 13058b8a9b1dSJustin T. Gibbs return(DM_RET_ERROR); 13068b8a9b1dSJustin T. Gibbs 13078b8a9b1dSJustin T. Gibbs /* 13088b8a9b1dSJustin T. Gibbs * If there are no match entries, then this device matches no 13098b8a9b1dSJustin T. Gibbs * matter what. 13108b8a9b1dSJustin T. Gibbs */ 131159e75884SColin Percival if ((patterns == NULL) || (num_patterns == 0)) 13128b8a9b1dSJustin T. Gibbs return(DM_RET_DESCEND | DM_RET_COPY); 13138b8a9b1dSJustin T. Gibbs 13148b8a9b1dSJustin T. Gibbs for (i = 0; i < num_patterns; i++) { 13158b8a9b1dSJustin T. Gibbs struct device_match_pattern *cur_pattern; 13163501942bSJustin T. Gibbs struct scsi_vpd_device_id *device_id_page; 13178b8a9b1dSJustin T. Gibbs 13188b8a9b1dSJustin T. Gibbs /* 13198b8a9b1dSJustin T. Gibbs * If the pattern in question isn't for a device node, we 13208b8a9b1dSJustin T. Gibbs * aren't interested. 13218b8a9b1dSJustin T. Gibbs */ 13228b8a9b1dSJustin T. Gibbs if (patterns[i].type != DEV_MATCH_DEVICE) { 13238b8a9b1dSJustin T. Gibbs if ((patterns[i].type == DEV_MATCH_PERIPH) 13248b8a9b1dSJustin T. Gibbs && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)) 13258b8a9b1dSJustin T. Gibbs retval |= DM_RET_DESCEND; 13268b8a9b1dSJustin T. Gibbs continue; 13278b8a9b1dSJustin T. Gibbs } 13288b8a9b1dSJustin T. Gibbs 13298b8a9b1dSJustin T. Gibbs cur_pattern = &patterns[i].pattern.device_pattern; 13308b8a9b1dSJustin T. Gibbs 13313501942bSJustin T. Gibbs /* Error out if mutually exclusive options are specified. */ 13323501942bSJustin T. Gibbs if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID)) 13333501942bSJustin T. Gibbs == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID)) 13343501942bSJustin T. Gibbs return(DM_RET_ERROR); 13353501942bSJustin T. Gibbs 13368b8a9b1dSJustin T. Gibbs /* 13378b8a9b1dSJustin T. Gibbs * If they want to match any device node, we give them any 13388b8a9b1dSJustin T. Gibbs * device node. 13398b8a9b1dSJustin T. Gibbs */ 13403501942bSJustin T. Gibbs if (cur_pattern->flags == DEV_MATCH_ANY) 13413501942bSJustin T. Gibbs goto copy_dev_node; 13428b8a9b1dSJustin T. Gibbs 13438b8a9b1dSJustin T. Gibbs /* 13448b8a9b1dSJustin T. Gibbs * Not sure why someone would do this... 13458b8a9b1dSJustin T. Gibbs */ 13468b8a9b1dSJustin T. Gibbs if (cur_pattern->flags == DEV_MATCH_NONE) 13478b8a9b1dSJustin T. Gibbs continue; 13488b8a9b1dSJustin T. Gibbs 13498b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & DEV_MATCH_PATH) != 0) 13508b8a9b1dSJustin T. Gibbs && (cur_pattern->path_id != device->target->bus->path_id)) 13518b8a9b1dSJustin T. Gibbs continue; 13528b8a9b1dSJustin T. Gibbs 13538b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0) 13548b8a9b1dSJustin T. Gibbs && (cur_pattern->target_id != device->target->target_id)) 13558b8a9b1dSJustin T. Gibbs continue; 13568b8a9b1dSJustin T. Gibbs 13578b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & DEV_MATCH_LUN) != 0) 13588b8a9b1dSJustin T. Gibbs && (cur_pattern->target_lun != device->lun_id)) 13598b8a9b1dSJustin T. Gibbs continue; 13608b8a9b1dSJustin T. Gibbs 13618b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0) 13628b8a9b1dSJustin T. Gibbs && (cam_quirkmatch((caddr_t)&device->inq_data, 13633501942bSJustin T. Gibbs (caddr_t)&cur_pattern->data.inq_pat, 13643501942bSJustin T. Gibbs 1, sizeof(cur_pattern->data.inq_pat), 13658b8a9b1dSJustin T. Gibbs scsi_static_inquiry_match) == NULL)) 13668b8a9b1dSJustin T. Gibbs continue; 13678b8a9b1dSJustin T. Gibbs 13683501942bSJustin T. Gibbs device_id_page = (struct scsi_vpd_device_id *)device->device_id; 13693501942bSJustin T. Gibbs if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0) 13703501942bSJustin T. Gibbs && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN 13713501942bSJustin T. Gibbs || scsi_devid_match((uint8_t *)device_id_page->desc_list, 13723501942bSJustin T. Gibbs device->device_id_len 13733501942bSJustin T. Gibbs - SVPD_DEVICE_ID_HDR_LEN, 13743501942bSJustin T. Gibbs cur_pattern->data.devid_pat.id, 13753501942bSJustin T. Gibbs cur_pattern->data.devid_pat.id_len) != 0)) 13763501942bSJustin T. Gibbs continue; 13773501942bSJustin T. Gibbs 13783501942bSJustin T. Gibbs copy_dev_node: 13798b8a9b1dSJustin T. Gibbs /* 13808b8a9b1dSJustin T. Gibbs * If we get to this point, the user definitely wants 13818b8a9b1dSJustin T. Gibbs * information on this device. So tell the caller to copy 13828b8a9b1dSJustin T. Gibbs * the data out. 13838b8a9b1dSJustin T. Gibbs */ 13848b8a9b1dSJustin T. Gibbs retval |= DM_RET_COPY; 13858b8a9b1dSJustin T. Gibbs 13868b8a9b1dSJustin T. Gibbs /* 13878b8a9b1dSJustin T. Gibbs * If the return action has been set to descend, then we 13888b8a9b1dSJustin T. Gibbs * know that we've already seen a peripheral matching 13898b8a9b1dSJustin T. Gibbs * expression, therefore we need to further descend the tree. 13908b8a9b1dSJustin T. Gibbs * This won't change by continuing around the loop, so we 13918b8a9b1dSJustin T. Gibbs * go ahead and return. If we haven't seen a peripheral 13928b8a9b1dSJustin T. Gibbs * matching expression, we keep going around the loop until 13938b8a9b1dSJustin T. Gibbs * we exhaust the matching expressions. We'll set the stop 13948b8a9b1dSJustin T. Gibbs * flag once we fall out of the loop. 13958b8a9b1dSJustin T. Gibbs */ 13968b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 13978b8a9b1dSJustin T. Gibbs return(retval); 13988b8a9b1dSJustin T. Gibbs } 13998b8a9b1dSJustin T. Gibbs 14008b8a9b1dSJustin T. Gibbs /* 14018b8a9b1dSJustin T. Gibbs * If the return action hasn't been set to descend yet, that means 14028b8a9b1dSJustin T. Gibbs * we haven't seen any peripheral matching patterns. So tell the 14038b8a9b1dSJustin T. Gibbs * caller to stop descending the tree -- the user doesn't want to 14048b8a9b1dSJustin T. Gibbs * match against lower level tree elements. 14058b8a9b1dSJustin T. Gibbs */ 14068b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 14078b8a9b1dSJustin T. Gibbs retval |= DM_RET_STOP; 14088b8a9b1dSJustin T. Gibbs 14098b8a9b1dSJustin T. Gibbs return(retval); 14108b8a9b1dSJustin T. Gibbs } 14118b8a9b1dSJustin T. Gibbs 14128b8a9b1dSJustin T. Gibbs /* 14138b8a9b1dSJustin T. Gibbs * Match a single peripheral against any number of match patterns. 14148b8a9b1dSJustin T. Gibbs */ 14158b8a9b1dSJustin T. Gibbs static dev_match_ret 14163393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns, 14178b8a9b1dSJustin T. Gibbs struct cam_periph *periph) 14188b8a9b1dSJustin T. Gibbs { 14198b8a9b1dSJustin T. Gibbs dev_match_ret retval; 14208b8a9b1dSJustin T. Gibbs int i; 14218b8a9b1dSJustin T. Gibbs 14228b8a9b1dSJustin T. Gibbs /* 14238b8a9b1dSJustin T. Gibbs * If we aren't given something to match against, that's an error. 14248b8a9b1dSJustin T. Gibbs */ 14258b8a9b1dSJustin T. Gibbs if (periph == NULL) 14268b8a9b1dSJustin T. Gibbs return(DM_RET_ERROR); 14278b8a9b1dSJustin T. Gibbs 14288b8a9b1dSJustin T. Gibbs /* 14298b8a9b1dSJustin T. Gibbs * If there are no match entries, then this peripheral matches no 14308b8a9b1dSJustin T. Gibbs * matter what. 14318b8a9b1dSJustin T. Gibbs */ 14328b8a9b1dSJustin T. Gibbs if ((patterns == NULL) || (num_patterns == 0)) 14338b8a9b1dSJustin T. Gibbs return(DM_RET_STOP | DM_RET_COPY); 14348b8a9b1dSJustin T. Gibbs 14358b8a9b1dSJustin T. Gibbs /* 14368b8a9b1dSJustin T. Gibbs * There aren't any nodes below a peripheral node, so there's no 14378b8a9b1dSJustin T. Gibbs * reason to descend the tree any further. 14388b8a9b1dSJustin T. Gibbs */ 14398b8a9b1dSJustin T. Gibbs retval = DM_RET_STOP; 14408b8a9b1dSJustin T. Gibbs 14418b8a9b1dSJustin T. Gibbs for (i = 0; i < num_patterns; i++) { 14428b8a9b1dSJustin T. Gibbs struct periph_match_pattern *cur_pattern; 14438b8a9b1dSJustin T. Gibbs 14448b8a9b1dSJustin T. Gibbs /* 14458b8a9b1dSJustin T. Gibbs * If the pattern in question isn't for a peripheral, we 14468b8a9b1dSJustin T. Gibbs * aren't interested. 14478b8a9b1dSJustin T. Gibbs */ 14488b8a9b1dSJustin T. Gibbs if (patterns[i].type != DEV_MATCH_PERIPH) 14498b8a9b1dSJustin T. Gibbs continue; 14508b8a9b1dSJustin T. Gibbs 14518b8a9b1dSJustin T. Gibbs cur_pattern = &patterns[i].pattern.periph_pattern; 14528b8a9b1dSJustin T. Gibbs 14538b8a9b1dSJustin T. Gibbs /* 14548b8a9b1dSJustin T. Gibbs * If they want to match on anything, then we will do so. 14558b8a9b1dSJustin T. Gibbs */ 14568b8a9b1dSJustin T. Gibbs if (cur_pattern->flags == PERIPH_MATCH_ANY) { 14578b8a9b1dSJustin T. Gibbs /* set the copy flag */ 14588b8a9b1dSJustin T. Gibbs retval |= DM_RET_COPY; 14598b8a9b1dSJustin T. Gibbs 14608b8a9b1dSJustin T. Gibbs /* 14618b8a9b1dSJustin T. Gibbs * We've already set the return action to stop, 14628b8a9b1dSJustin T. Gibbs * since there are no nodes below peripherals in 14638b8a9b1dSJustin T. Gibbs * the tree. 14648b8a9b1dSJustin T. Gibbs */ 14658b8a9b1dSJustin T. Gibbs return(retval); 14668b8a9b1dSJustin T. Gibbs } 14678b8a9b1dSJustin T. Gibbs 14688b8a9b1dSJustin T. Gibbs /* 14698b8a9b1dSJustin T. Gibbs * Not sure why someone would do this... 14708b8a9b1dSJustin T. Gibbs */ 14718b8a9b1dSJustin T. Gibbs if (cur_pattern->flags == PERIPH_MATCH_NONE) 14728b8a9b1dSJustin T. Gibbs continue; 14738b8a9b1dSJustin T. Gibbs 14748b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0) 14758b8a9b1dSJustin T. Gibbs && (cur_pattern->path_id != periph->path->bus->path_id)) 14768b8a9b1dSJustin T. Gibbs continue; 14778b8a9b1dSJustin T. Gibbs 14788b8a9b1dSJustin T. Gibbs /* 14798b8a9b1dSJustin T. Gibbs * For the target and lun id's, we have to make sure the 14808b8a9b1dSJustin T. Gibbs * target and lun pointers aren't NULL. The xpt peripheral 14818b8a9b1dSJustin T. Gibbs * has a wildcard target and device. 14828b8a9b1dSJustin T. Gibbs */ 14838b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0) 14848b8a9b1dSJustin T. Gibbs && ((periph->path->target == NULL) 14858b8a9b1dSJustin T. Gibbs ||(cur_pattern->target_id != periph->path->target->target_id))) 14868b8a9b1dSJustin T. Gibbs continue; 14878b8a9b1dSJustin T. Gibbs 14888b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0) 14898b8a9b1dSJustin T. Gibbs && ((periph->path->device == NULL) 14908b8a9b1dSJustin T. Gibbs || (cur_pattern->target_lun != periph->path->device->lun_id))) 14918b8a9b1dSJustin T. Gibbs continue; 14928b8a9b1dSJustin T. Gibbs 14938b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0) 14948b8a9b1dSJustin T. Gibbs && (cur_pattern->unit_number != periph->unit_number)) 14958b8a9b1dSJustin T. Gibbs continue; 14968b8a9b1dSJustin T. Gibbs 14978b8a9b1dSJustin T. Gibbs if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0) 14988b8a9b1dSJustin T. Gibbs && (strncmp(cur_pattern->periph_name, periph->periph_name, 14998b8a9b1dSJustin T. Gibbs DEV_IDLEN) != 0)) 15008b8a9b1dSJustin T. Gibbs continue; 15018b8a9b1dSJustin T. Gibbs 15028b8a9b1dSJustin T. Gibbs /* 15038b8a9b1dSJustin T. Gibbs * If we get to this point, the user definitely wants 15048b8a9b1dSJustin T. Gibbs * information on this peripheral. So tell the caller to 15058b8a9b1dSJustin T. Gibbs * copy the data out. 15068b8a9b1dSJustin T. Gibbs */ 15078b8a9b1dSJustin T. Gibbs retval |= DM_RET_COPY; 15088b8a9b1dSJustin T. Gibbs 15098b8a9b1dSJustin T. Gibbs /* 15108b8a9b1dSJustin T. Gibbs * The return action has already been set to stop, since 15118b8a9b1dSJustin T. Gibbs * peripherals don't have any nodes below them in the EDT. 15128b8a9b1dSJustin T. Gibbs */ 15138b8a9b1dSJustin T. Gibbs return(retval); 15148b8a9b1dSJustin T. Gibbs } 15158b8a9b1dSJustin T. Gibbs 15168b8a9b1dSJustin T. Gibbs /* 15178b8a9b1dSJustin T. Gibbs * If we get to this point, the peripheral that was passed in 15188b8a9b1dSJustin T. Gibbs * doesn't match any of the patterns. 15198b8a9b1dSJustin T. Gibbs */ 15208b8a9b1dSJustin T. Gibbs return(retval); 15218b8a9b1dSJustin T. Gibbs } 15228b8a9b1dSJustin T. Gibbs 15238b8a9b1dSJustin T. Gibbs static int 15248b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg) 15258b8a9b1dSJustin T. Gibbs { 15268b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 1527227d67aaSAlexander Motin struct cam_et *target; 15288b8a9b1dSJustin T. Gibbs dev_match_ret retval; 15298b8a9b1dSJustin T. Gibbs 15308b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 15318b8a9b1dSJustin T. Gibbs 15328b8a9b1dSJustin T. Gibbs /* 15338b8a9b1dSJustin T. Gibbs * If our position is for something deeper in the tree, that means 15348b8a9b1dSJustin T. Gibbs * that we've already seen this node. So, we keep going down. 15358b8a9b1dSJustin T. Gibbs */ 15368b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 15378b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.bus == bus) 15388b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 15398b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.target != NULL)) 15408b8a9b1dSJustin T. Gibbs retval = DM_RET_DESCEND; 15418b8a9b1dSJustin T. Gibbs else 15428b8a9b1dSJustin T. Gibbs retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus); 15438b8a9b1dSJustin T. Gibbs 15448b8a9b1dSJustin T. Gibbs /* 15458b8a9b1dSJustin T. Gibbs * If we got an error, bail out of the search. 15468b8a9b1dSJustin T. Gibbs */ 15478b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 15488b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 15498b8a9b1dSJustin T. Gibbs return(0); 15508b8a9b1dSJustin T. Gibbs } 15518b8a9b1dSJustin T. Gibbs 15528b8a9b1dSJustin T. Gibbs /* 15538b8a9b1dSJustin T. Gibbs * If the copy flag is set, copy this bus out. 15548b8a9b1dSJustin T. Gibbs */ 15558b8a9b1dSJustin T. Gibbs if (retval & DM_RET_COPY) { 15568b8a9b1dSJustin T. Gibbs int spaceleft, j; 15578b8a9b1dSJustin T. Gibbs 15588b8a9b1dSJustin T. Gibbs spaceleft = cdm->match_buf_len - (cdm->num_matches * 15598b8a9b1dSJustin T. Gibbs sizeof(struct dev_match_result)); 15608b8a9b1dSJustin T. Gibbs 15618b8a9b1dSJustin T. Gibbs /* 15628b8a9b1dSJustin T. Gibbs * If we don't have enough space to put in another 15638b8a9b1dSJustin T. Gibbs * match result, save our position and tell the 15648b8a9b1dSJustin T. Gibbs * user there are more devices to check. 15658b8a9b1dSJustin T. Gibbs */ 15668b8a9b1dSJustin T. Gibbs if (spaceleft < sizeof(struct dev_match_result)) { 15678b8a9b1dSJustin T. Gibbs bzero(&cdm->pos, sizeof(cdm->pos)); 15688b8a9b1dSJustin T. Gibbs cdm->pos.position_type = 15698b8a9b1dSJustin T. Gibbs CAM_DEV_POS_EDT | CAM_DEV_POS_BUS; 15708b8a9b1dSJustin T. Gibbs 15718b8a9b1dSJustin T. Gibbs cdm->pos.cookie.bus = bus; 15728b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_BUS_GENERATION]= 15732b83592fSScott Long xsoftc.bus_generation; 15748b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_MORE; 15758b8a9b1dSJustin T. Gibbs return(0); 15768b8a9b1dSJustin T. Gibbs } 15778b8a9b1dSJustin T. Gibbs j = cdm->num_matches; 15788b8a9b1dSJustin T. Gibbs cdm->num_matches++; 15798b8a9b1dSJustin T. Gibbs cdm->matches[j].type = DEV_MATCH_BUS; 15808b8a9b1dSJustin T. Gibbs cdm->matches[j].result.bus_result.path_id = bus->path_id; 15818b8a9b1dSJustin T. Gibbs cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id; 15828b8a9b1dSJustin T. Gibbs cdm->matches[j].result.bus_result.unit_number = 15838b8a9b1dSJustin T. Gibbs bus->sim->unit_number; 15848b8a9b1dSJustin T. Gibbs strncpy(cdm->matches[j].result.bus_result.dev_name, 15858b8a9b1dSJustin T. Gibbs bus->sim->sim_name, DEV_IDLEN); 15868b8a9b1dSJustin T. Gibbs } 15878b8a9b1dSJustin T. Gibbs 15888b8a9b1dSJustin T. Gibbs /* 15898b8a9b1dSJustin T. Gibbs * If the user is only interested in busses, there's no 15908b8a9b1dSJustin T. Gibbs * reason to descend to the next level in the tree. 15918b8a9b1dSJustin T. Gibbs */ 15928b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 15938b8a9b1dSJustin T. Gibbs return(1); 15948b8a9b1dSJustin T. Gibbs 15958b8a9b1dSJustin T. Gibbs /* 15968b8a9b1dSJustin T. Gibbs * If there is a target generation recorded, check it to 15978b8a9b1dSJustin T. Gibbs * make sure the target list hasn't changed. 15988b8a9b1dSJustin T. Gibbs */ 1599227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 16008b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 16018b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.bus == bus) 16028b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1603227d67aaSAlexander Motin && (cdm->pos.cookie.target != NULL)) { 1604227d67aaSAlexander Motin if ((cdm->pos.generations[CAM_TARGET_GENERATION] != 1605227d67aaSAlexander Motin bus->generation)) { 1606227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 1607227d67aaSAlexander Motin cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1608227d67aaSAlexander Motin return (0); 1609227d67aaSAlexander Motin } 1610227d67aaSAlexander Motin target = (struct cam_et *)cdm->pos.cookie.target; 1611227d67aaSAlexander Motin target->refcount++; 1612227d67aaSAlexander Motin } else 1613227d67aaSAlexander Motin target = NULL; 1614227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 1615227d67aaSAlexander Motin 1616227d67aaSAlexander Motin return (xpttargettraverse(bus, target, xptedttargetfunc, arg)); 16178b8a9b1dSJustin T. Gibbs } 16188b8a9b1dSJustin T. Gibbs 16198b8a9b1dSJustin T. Gibbs static int 16208b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg) 16218b8a9b1dSJustin T. Gibbs { 16228b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 1623227d67aaSAlexander Motin struct cam_eb *bus; 1624227d67aaSAlexander Motin struct cam_ed *device; 16258b8a9b1dSJustin T. Gibbs 16268b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 1627227d67aaSAlexander Motin bus = target->bus; 16288b8a9b1dSJustin T. Gibbs 16298b8a9b1dSJustin T. Gibbs /* 16308b8a9b1dSJustin T. Gibbs * If there is a device list generation recorded, check it to 16318b8a9b1dSJustin T. Gibbs * make sure the device list hasn't changed. 16328b8a9b1dSJustin T. Gibbs */ 1633227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 16348b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1635227d67aaSAlexander Motin && (cdm->pos.cookie.bus == bus) 16368b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 16378b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.target == target) 16388b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1639227d67aaSAlexander Motin && (cdm->pos.cookie.device != NULL)) { 1640227d67aaSAlexander Motin if (cdm->pos.generations[CAM_DEV_GENERATION] != 1641227d67aaSAlexander Motin target->generation) { 1642227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 16438b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 16448b8a9b1dSJustin T. Gibbs return(0); 16458b8a9b1dSJustin T. Gibbs } 1646227d67aaSAlexander Motin device = (struct cam_ed *)cdm->pos.cookie.device; 1647227d67aaSAlexander Motin device->refcount++; 1648227d67aaSAlexander Motin } else 1649227d67aaSAlexander Motin device = NULL; 1650227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 16518b8a9b1dSJustin T. Gibbs 1652227d67aaSAlexander Motin return (xptdevicetraverse(target, device, xptedtdevicefunc, arg)); 16538b8a9b1dSJustin T. Gibbs } 16548b8a9b1dSJustin T. Gibbs 16558b8a9b1dSJustin T. Gibbs static int 16568b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg) 16578b8a9b1dSJustin T. Gibbs { 1658227d67aaSAlexander Motin struct cam_eb *bus; 1659227d67aaSAlexander Motin struct cam_periph *periph; 16608b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 16618b8a9b1dSJustin T. Gibbs dev_match_ret retval; 16628b8a9b1dSJustin T. Gibbs 16638b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 1664227d67aaSAlexander Motin bus = device->target->bus; 16658b8a9b1dSJustin T. Gibbs 16668b8a9b1dSJustin T. Gibbs /* 16678b8a9b1dSJustin T. Gibbs * If our position is for something deeper in the tree, that means 16688b8a9b1dSJustin T. Gibbs * that we've already seen this node. So, we keep going down. 16698b8a9b1dSJustin T. Gibbs */ 16708b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE) 16718b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.device == device) 16728b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 16738b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.periph != NULL)) 16748b8a9b1dSJustin T. Gibbs retval = DM_RET_DESCEND; 16758b8a9b1dSJustin T. Gibbs else 16768b8a9b1dSJustin T. Gibbs retval = xptdevicematch(cdm->patterns, cdm->num_patterns, 16778b8a9b1dSJustin T. Gibbs device); 16788b8a9b1dSJustin T. Gibbs 16798b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 16808b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 16818b8a9b1dSJustin T. Gibbs return(0); 16828b8a9b1dSJustin T. Gibbs } 16838b8a9b1dSJustin T. Gibbs 16848b8a9b1dSJustin T. Gibbs /* 16858b8a9b1dSJustin T. Gibbs * If the copy flag is set, copy this device out. 16868b8a9b1dSJustin T. Gibbs */ 16878b8a9b1dSJustin T. Gibbs if (retval & DM_RET_COPY) { 16888b8a9b1dSJustin T. Gibbs int spaceleft, j; 16898b8a9b1dSJustin T. Gibbs 16908b8a9b1dSJustin T. Gibbs spaceleft = cdm->match_buf_len - (cdm->num_matches * 16918b8a9b1dSJustin T. Gibbs sizeof(struct dev_match_result)); 16928b8a9b1dSJustin T. Gibbs 16938b8a9b1dSJustin T. Gibbs /* 16948b8a9b1dSJustin T. Gibbs * If we don't have enough space to put in another 16958b8a9b1dSJustin T. Gibbs * match result, save our position and tell the 16968b8a9b1dSJustin T. Gibbs * user there are more devices to check. 16978b8a9b1dSJustin T. Gibbs */ 16988b8a9b1dSJustin T. Gibbs if (spaceleft < sizeof(struct dev_match_result)) { 16998b8a9b1dSJustin T. Gibbs bzero(&cdm->pos, sizeof(cdm->pos)); 17008b8a9b1dSJustin T. Gibbs cdm->pos.position_type = 17018b8a9b1dSJustin T. Gibbs CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 17028b8a9b1dSJustin T. Gibbs CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE; 17038b8a9b1dSJustin T. Gibbs 17048b8a9b1dSJustin T. Gibbs cdm->pos.cookie.bus = device->target->bus; 17058b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_BUS_GENERATION]= 17062b83592fSScott Long xsoftc.bus_generation; 17078b8a9b1dSJustin T. Gibbs cdm->pos.cookie.target = device->target; 17088b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_TARGET_GENERATION] = 17098b8a9b1dSJustin T. Gibbs device->target->bus->generation; 17108b8a9b1dSJustin T. Gibbs cdm->pos.cookie.device = device; 17118b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_DEV_GENERATION] = 17128b8a9b1dSJustin T. Gibbs device->target->generation; 17138b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_MORE; 17148b8a9b1dSJustin T. Gibbs return(0); 17158b8a9b1dSJustin T. Gibbs } 17168b8a9b1dSJustin T. Gibbs j = cdm->num_matches; 17178b8a9b1dSJustin T. Gibbs cdm->num_matches++; 17188b8a9b1dSJustin T. Gibbs cdm->matches[j].type = DEV_MATCH_DEVICE; 17198b8a9b1dSJustin T. Gibbs cdm->matches[j].result.device_result.path_id = 17208b8a9b1dSJustin T. Gibbs device->target->bus->path_id; 17218b8a9b1dSJustin T. Gibbs cdm->matches[j].result.device_result.target_id = 17228b8a9b1dSJustin T. Gibbs device->target->target_id; 17238b8a9b1dSJustin T. Gibbs cdm->matches[j].result.device_result.target_lun = 17248b8a9b1dSJustin T. Gibbs device->lun_id; 172552c9ce25SScott Long cdm->matches[j].result.device_result.protocol = 172652c9ce25SScott Long device->protocol; 17278b8a9b1dSJustin T. Gibbs bcopy(&device->inq_data, 17288b8a9b1dSJustin T. Gibbs &cdm->matches[j].result.device_result.inq_data, 17298b8a9b1dSJustin T. Gibbs sizeof(struct scsi_inquiry_data)); 173052c9ce25SScott Long bcopy(&device->ident_data, 173152c9ce25SScott Long &cdm->matches[j].result.device_result.ident_data, 173252c9ce25SScott Long sizeof(struct ata_params)); 17339deea857SKenneth D. Merry 17349deea857SKenneth D. Merry /* Let the user know whether this device is unconfigured */ 17359deea857SKenneth D. Merry if (device->flags & CAM_DEV_UNCONFIGURED) 17369deea857SKenneth D. Merry cdm->matches[j].result.device_result.flags = 17379deea857SKenneth D. Merry DEV_RESULT_UNCONFIGURED; 17389deea857SKenneth D. Merry else 17399deea857SKenneth D. Merry cdm->matches[j].result.device_result.flags = 17409deea857SKenneth D. Merry DEV_RESULT_NOFLAG; 17418b8a9b1dSJustin T. Gibbs } 17428b8a9b1dSJustin T. Gibbs 17438b8a9b1dSJustin T. Gibbs /* 17448b8a9b1dSJustin T. Gibbs * If the user isn't interested in peripherals, don't descend 17458b8a9b1dSJustin T. Gibbs * the tree any further. 17468b8a9b1dSJustin T. Gibbs */ 17478b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 17488b8a9b1dSJustin T. Gibbs return(1); 17498b8a9b1dSJustin T. Gibbs 17508b8a9b1dSJustin T. Gibbs /* 17518b8a9b1dSJustin T. Gibbs * If there is a peripheral list generation recorded, make sure 17528b8a9b1dSJustin T. Gibbs * it hasn't changed. 17538b8a9b1dSJustin T. Gibbs */ 1754227d67aaSAlexander Motin xpt_lock_buses(); 1755227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 17568b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1757227d67aaSAlexander Motin && (cdm->pos.cookie.bus == bus) 17588b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 17598b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.target == device->target) 17608b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 17618b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.device == device) 17628b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1763227d67aaSAlexander Motin && (cdm->pos.cookie.periph != NULL)) { 1764227d67aaSAlexander Motin if (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1765227d67aaSAlexander Motin device->generation) { 1766227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 1767227d67aaSAlexander Motin xpt_unlock_buses(); 1768227d67aaSAlexander Motin cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1769227d67aaSAlexander Motin return(0); 1770227d67aaSAlexander Motin } 1771227d67aaSAlexander Motin periph = (struct cam_periph *)cdm->pos.cookie.periph; 1772227d67aaSAlexander Motin periph->refcount++; 1773227d67aaSAlexander Motin } else 1774227d67aaSAlexander Motin periph = NULL; 1775227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 1776227d67aaSAlexander Motin xpt_unlock_buses(); 1777227d67aaSAlexander Motin 1778227d67aaSAlexander Motin return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg)); 17798b8a9b1dSJustin T. Gibbs } 17808b8a9b1dSJustin T. Gibbs 17818b8a9b1dSJustin T. Gibbs static int 17828b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg) 17838b8a9b1dSJustin T. Gibbs { 17848b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 17858b8a9b1dSJustin T. Gibbs dev_match_ret retval; 17868b8a9b1dSJustin T. Gibbs 17878b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 17888b8a9b1dSJustin T. Gibbs 17898b8a9b1dSJustin T. Gibbs retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 17908b8a9b1dSJustin T. Gibbs 17918b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 17928b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 17938b8a9b1dSJustin T. Gibbs return(0); 17948b8a9b1dSJustin T. Gibbs } 17958b8a9b1dSJustin T. Gibbs 17968b8a9b1dSJustin T. Gibbs /* 17978b8a9b1dSJustin T. Gibbs * If the copy flag is set, copy this peripheral out. 17988b8a9b1dSJustin T. Gibbs */ 17998b8a9b1dSJustin T. Gibbs if (retval & DM_RET_COPY) { 18008b8a9b1dSJustin T. Gibbs int spaceleft, j; 18018b8a9b1dSJustin T. Gibbs 18028b8a9b1dSJustin T. Gibbs spaceleft = cdm->match_buf_len - (cdm->num_matches * 18038b8a9b1dSJustin T. Gibbs sizeof(struct dev_match_result)); 18048b8a9b1dSJustin T. Gibbs 18058b8a9b1dSJustin T. Gibbs /* 18068b8a9b1dSJustin T. Gibbs * If we don't have enough space to put in another 18078b8a9b1dSJustin T. Gibbs * match result, save our position and tell the 18088b8a9b1dSJustin T. Gibbs * user there are more devices to check. 18098b8a9b1dSJustin T. Gibbs */ 18108b8a9b1dSJustin T. Gibbs if (spaceleft < sizeof(struct dev_match_result)) { 18118b8a9b1dSJustin T. Gibbs bzero(&cdm->pos, sizeof(cdm->pos)); 18128b8a9b1dSJustin T. Gibbs cdm->pos.position_type = 18138b8a9b1dSJustin T. Gibbs CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 18148b8a9b1dSJustin T. Gibbs CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE | 18158b8a9b1dSJustin T. Gibbs CAM_DEV_POS_PERIPH; 18168b8a9b1dSJustin T. Gibbs 18178b8a9b1dSJustin T. Gibbs cdm->pos.cookie.bus = periph->path->bus; 18188b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_BUS_GENERATION]= 18192b83592fSScott Long xsoftc.bus_generation; 18208b8a9b1dSJustin T. Gibbs cdm->pos.cookie.target = periph->path->target; 18218b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_TARGET_GENERATION] = 18228b8a9b1dSJustin T. Gibbs periph->path->bus->generation; 18238b8a9b1dSJustin T. Gibbs cdm->pos.cookie.device = periph->path->device; 18248b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_DEV_GENERATION] = 18258b8a9b1dSJustin T. Gibbs periph->path->target->generation; 18268b8a9b1dSJustin T. Gibbs cdm->pos.cookie.periph = periph; 18278b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_PERIPH_GENERATION] = 18288b8a9b1dSJustin T. Gibbs periph->path->device->generation; 18298b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_MORE; 18308b8a9b1dSJustin T. Gibbs return(0); 18318b8a9b1dSJustin T. Gibbs } 18328b8a9b1dSJustin T. Gibbs 18338b8a9b1dSJustin T. Gibbs j = cdm->num_matches; 18348b8a9b1dSJustin T. Gibbs cdm->num_matches++; 18358b8a9b1dSJustin T. Gibbs cdm->matches[j].type = DEV_MATCH_PERIPH; 18368b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.path_id = 18378b8a9b1dSJustin T. Gibbs periph->path->bus->path_id; 18388b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.target_id = 18398b8a9b1dSJustin T. Gibbs periph->path->target->target_id; 18408b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.target_lun = 18418b8a9b1dSJustin T. Gibbs periph->path->device->lun_id; 18428b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.unit_number = 18438b8a9b1dSJustin T. Gibbs periph->unit_number; 18448b8a9b1dSJustin T. Gibbs strncpy(cdm->matches[j].result.periph_result.periph_name, 18458b8a9b1dSJustin T. Gibbs periph->periph_name, DEV_IDLEN); 18468b8a9b1dSJustin T. Gibbs } 18478b8a9b1dSJustin T. Gibbs 18488b8a9b1dSJustin T. Gibbs return(1); 18498b8a9b1dSJustin T. Gibbs } 18508b8a9b1dSJustin T. Gibbs 18518b8a9b1dSJustin T. Gibbs static int 18528b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm) 18538b8a9b1dSJustin T. Gibbs { 1854227d67aaSAlexander Motin struct cam_eb *bus; 18558b8a9b1dSJustin T. Gibbs int ret; 18568b8a9b1dSJustin T. Gibbs 18578b8a9b1dSJustin T. Gibbs cdm->num_matches = 0; 18588b8a9b1dSJustin T. Gibbs 18598b8a9b1dSJustin T. Gibbs /* 18608b8a9b1dSJustin T. Gibbs * Check the bus list generation. If it has changed, the user 18618b8a9b1dSJustin T. Gibbs * needs to reset everything and start over. 18628b8a9b1dSJustin T. Gibbs */ 1863227d67aaSAlexander Motin xpt_lock_buses(); 18648b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1865227d67aaSAlexander Motin && (cdm->pos.cookie.bus != NULL)) { 1866227d67aaSAlexander Motin if (cdm->pos.generations[CAM_BUS_GENERATION] != 1867227d67aaSAlexander Motin xsoftc.bus_generation) { 1868227d67aaSAlexander Motin xpt_unlock_buses(); 18698b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 18708b8a9b1dSJustin T. Gibbs return(0); 18718b8a9b1dSJustin T. Gibbs } 1872227d67aaSAlexander Motin bus = (struct cam_eb *)cdm->pos.cookie.bus; 1873227d67aaSAlexander Motin bus->refcount++; 1874227d67aaSAlexander Motin } else 1875227d67aaSAlexander Motin bus = NULL; 1876227d67aaSAlexander Motin xpt_unlock_buses(); 18778b8a9b1dSJustin T. Gibbs 1878227d67aaSAlexander Motin ret = xptbustraverse(bus, xptedtbusfunc, cdm); 18798b8a9b1dSJustin T. Gibbs 18808b8a9b1dSJustin T. Gibbs /* 18818b8a9b1dSJustin T. Gibbs * If we get back 0, that means that we had to stop before fully 18828b8a9b1dSJustin T. Gibbs * traversing the EDT. It also means that one of the subroutines 18838b8a9b1dSJustin T. Gibbs * has set the status field to the proper value. If we get back 1, 18848b8a9b1dSJustin T. Gibbs * we've fully traversed the EDT and copied out any matching entries. 18858b8a9b1dSJustin T. Gibbs */ 18868b8a9b1dSJustin T. Gibbs if (ret == 1) 18878b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_LAST; 18888b8a9b1dSJustin T. Gibbs 18898b8a9b1dSJustin T. Gibbs return(ret); 18908b8a9b1dSJustin T. Gibbs } 18918b8a9b1dSJustin T. Gibbs 18928b8a9b1dSJustin T. Gibbs static int 18938b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg) 18948b8a9b1dSJustin T. Gibbs { 1895227d67aaSAlexander Motin struct cam_periph *periph; 18968b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 18978b8a9b1dSJustin T. Gibbs 18988b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 18998b8a9b1dSJustin T. Gibbs 1900227d67aaSAlexander Motin xpt_lock_buses(); 19018b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 19028b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.pdrv == pdrv) 19038b8a9b1dSJustin T. Gibbs && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1904227d67aaSAlexander Motin && (cdm->pos.cookie.periph != NULL)) { 1905227d67aaSAlexander Motin if (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1906227d67aaSAlexander Motin (*pdrv)->generation) { 1907227d67aaSAlexander Motin xpt_unlock_buses(); 19088b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 19098b8a9b1dSJustin T. Gibbs return(0); 19108b8a9b1dSJustin T. Gibbs } 1911227d67aaSAlexander Motin periph = (struct cam_periph *)cdm->pos.cookie.periph; 1912227d67aaSAlexander Motin periph->refcount++; 1913227d67aaSAlexander Motin } else 1914227d67aaSAlexander Motin periph = NULL; 1915227d67aaSAlexander Motin xpt_unlock_buses(); 19168b8a9b1dSJustin T. Gibbs 1917227d67aaSAlexander Motin return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg)); 19188b8a9b1dSJustin T. Gibbs } 19198b8a9b1dSJustin T. Gibbs 19208b8a9b1dSJustin T. Gibbs static int 19218b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg) 19228b8a9b1dSJustin T. Gibbs { 19238b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 19248b8a9b1dSJustin T. Gibbs dev_match_ret retval; 19258b8a9b1dSJustin T. Gibbs 19268b8a9b1dSJustin T. Gibbs cdm = (struct ccb_dev_match *)arg; 19278b8a9b1dSJustin T. Gibbs 19288b8a9b1dSJustin T. Gibbs retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 19298b8a9b1dSJustin T. Gibbs 19308b8a9b1dSJustin T. Gibbs if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 19318b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 19328b8a9b1dSJustin T. Gibbs return(0); 19338b8a9b1dSJustin T. Gibbs } 19348b8a9b1dSJustin T. Gibbs 19358b8a9b1dSJustin T. Gibbs /* 19368b8a9b1dSJustin T. Gibbs * If the copy flag is set, copy this peripheral out. 19378b8a9b1dSJustin T. Gibbs */ 19388b8a9b1dSJustin T. Gibbs if (retval & DM_RET_COPY) { 19398b8a9b1dSJustin T. Gibbs int spaceleft, j; 19408b8a9b1dSJustin T. Gibbs 19418b8a9b1dSJustin T. Gibbs spaceleft = cdm->match_buf_len - (cdm->num_matches * 19428b8a9b1dSJustin T. Gibbs sizeof(struct dev_match_result)); 19438b8a9b1dSJustin T. Gibbs 19448b8a9b1dSJustin T. Gibbs /* 19458b8a9b1dSJustin T. Gibbs * If we don't have enough space to put in another 19468b8a9b1dSJustin T. Gibbs * match result, save our position and tell the 19478b8a9b1dSJustin T. Gibbs * user there are more devices to check. 19488b8a9b1dSJustin T. Gibbs */ 19498b8a9b1dSJustin T. Gibbs if (spaceleft < sizeof(struct dev_match_result)) { 19508b8a9b1dSJustin T. Gibbs struct periph_driver **pdrv; 19518b8a9b1dSJustin T. Gibbs 19528b8a9b1dSJustin T. Gibbs pdrv = NULL; 19538b8a9b1dSJustin T. Gibbs bzero(&cdm->pos, sizeof(cdm->pos)); 19548b8a9b1dSJustin T. Gibbs cdm->pos.position_type = 19558b8a9b1dSJustin T. Gibbs CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR | 19568b8a9b1dSJustin T. Gibbs CAM_DEV_POS_PERIPH; 19578b8a9b1dSJustin T. Gibbs 19588b8a9b1dSJustin T. Gibbs /* 19598b8a9b1dSJustin T. Gibbs * This may look a bit non-sensical, but it is 19608b8a9b1dSJustin T. Gibbs * actually quite logical. There are very few 19618b8a9b1dSJustin T. Gibbs * peripheral drivers, and bloating every peripheral 19628b8a9b1dSJustin T. Gibbs * structure with a pointer back to its parent 19638b8a9b1dSJustin T. Gibbs * peripheral driver linker set entry would cost 19648b8a9b1dSJustin T. Gibbs * more in the long run than doing this quick lookup. 19658b8a9b1dSJustin T. Gibbs */ 19660b7c27b9SPeter Wemm for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) { 19678b8a9b1dSJustin T. Gibbs if (strcmp((*pdrv)->driver_name, 19688b8a9b1dSJustin T. Gibbs periph->periph_name) == 0) 19698b8a9b1dSJustin T. Gibbs break; 19708b8a9b1dSJustin T. Gibbs } 19718b8a9b1dSJustin T. Gibbs 197201910babSScott Long if (*pdrv == NULL) { 19738b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 19748b8a9b1dSJustin T. Gibbs return(0); 19758b8a9b1dSJustin T. Gibbs } 19768b8a9b1dSJustin T. Gibbs 19778b8a9b1dSJustin T. Gibbs cdm->pos.cookie.pdrv = pdrv; 19788b8a9b1dSJustin T. Gibbs /* 19798b8a9b1dSJustin T. Gibbs * The periph generation slot does double duty, as 19808b8a9b1dSJustin T. Gibbs * does the periph pointer slot. They are used for 19818b8a9b1dSJustin T. Gibbs * both edt and pdrv lookups and positioning. 19828b8a9b1dSJustin T. Gibbs */ 19838b8a9b1dSJustin T. Gibbs cdm->pos.cookie.periph = periph; 19848b8a9b1dSJustin T. Gibbs cdm->pos.generations[CAM_PERIPH_GENERATION] = 19858b8a9b1dSJustin T. Gibbs (*pdrv)->generation; 19868b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_MORE; 19878b8a9b1dSJustin T. Gibbs return(0); 19888b8a9b1dSJustin T. Gibbs } 19898b8a9b1dSJustin T. Gibbs 19908b8a9b1dSJustin T. Gibbs j = cdm->num_matches; 19918b8a9b1dSJustin T. Gibbs cdm->num_matches++; 19928b8a9b1dSJustin T. Gibbs cdm->matches[j].type = DEV_MATCH_PERIPH; 19938b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.path_id = 19948b8a9b1dSJustin T. Gibbs periph->path->bus->path_id; 19958b8a9b1dSJustin T. Gibbs 19968b8a9b1dSJustin T. Gibbs /* 19978b8a9b1dSJustin T. Gibbs * The transport layer peripheral doesn't have a target or 19988b8a9b1dSJustin T. Gibbs * lun. 19998b8a9b1dSJustin T. Gibbs */ 20008b8a9b1dSJustin T. Gibbs if (periph->path->target) 20018b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.target_id = 20028b8a9b1dSJustin T. Gibbs periph->path->target->target_id; 20038b8a9b1dSJustin T. Gibbs else 2004431d3a5bSAlexander Motin cdm->matches[j].result.periph_result.target_id = 2005431d3a5bSAlexander Motin CAM_TARGET_WILDCARD; 20068b8a9b1dSJustin T. Gibbs 20078b8a9b1dSJustin T. Gibbs if (periph->path->device) 20088b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.target_lun = 20098b8a9b1dSJustin T. Gibbs periph->path->device->lun_id; 20108b8a9b1dSJustin T. Gibbs else 2011431d3a5bSAlexander Motin cdm->matches[j].result.periph_result.target_lun = 2012431d3a5bSAlexander Motin CAM_LUN_WILDCARD; 20138b8a9b1dSJustin T. Gibbs 20148b8a9b1dSJustin T. Gibbs cdm->matches[j].result.periph_result.unit_number = 20158b8a9b1dSJustin T. Gibbs periph->unit_number; 20168b8a9b1dSJustin T. Gibbs strncpy(cdm->matches[j].result.periph_result.periph_name, 20178b8a9b1dSJustin T. Gibbs periph->periph_name, DEV_IDLEN); 20188b8a9b1dSJustin T. Gibbs } 20198b8a9b1dSJustin T. Gibbs 20208b8a9b1dSJustin T. Gibbs return(1); 20218b8a9b1dSJustin T. Gibbs } 20228b8a9b1dSJustin T. Gibbs 20238b8a9b1dSJustin T. Gibbs static int 20248b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm) 20258b8a9b1dSJustin T. Gibbs { 20268b8a9b1dSJustin T. Gibbs int ret; 20278b8a9b1dSJustin T. Gibbs 20288b8a9b1dSJustin T. Gibbs cdm->num_matches = 0; 20298b8a9b1dSJustin T. Gibbs 20308b8a9b1dSJustin T. Gibbs /* 20318b8a9b1dSJustin T. Gibbs * At this point in the edt traversal function, we check the bus 20328b8a9b1dSJustin T. Gibbs * list generation to make sure that no busses have been added or 20338b8a9b1dSJustin T. Gibbs * removed since the user last sent a XPT_DEV_MATCH ccb through. 20348b8a9b1dSJustin T. Gibbs * For the peripheral driver list traversal function, however, we 20358b8a9b1dSJustin T. Gibbs * don't have to worry about new peripheral driver types coming or 20368b8a9b1dSJustin T. Gibbs * going; they're in a linker set, and therefore can't change 20378b8a9b1dSJustin T. Gibbs * without a recompile. 20388b8a9b1dSJustin T. Gibbs */ 20398b8a9b1dSJustin T. Gibbs 20408b8a9b1dSJustin T. Gibbs if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 20418b8a9b1dSJustin T. Gibbs && (cdm->pos.cookie.pdrv != NULL)) 20428b8a9b1dSJustin T. Gibbs ret = xptpdrvtraverse( 20438b8a9b1dSJustin T. Gibbs (struct periph_driver **)cdm->pos.cookie.pdrv, 20448b8a9b1dSJustin T. Gibbs xptplistpdrvfunc, cdm); 20458b8a9b1dSJustin T. Gibbs else 20468b8a9b1dSJustin T. Gibbs ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm); 20478b8a9b1dSJustin T. Gibbs 20488b8a9b1dSJustin T. Gibbs /* 20498b8a9b1dSJustin T. Gibbs * If we get back 0, that means that we had to stop before fully 20508b8a9b1dSJustin T. Gibbs * traversing the peripheral driver tree. It also means that one of 20518b8a9b1dSJustin T. Gibbs * the subroutines has set the status field to the proper value. If 20528b8a9b1dSJustin T. Gibbs * we get back 1, we've fully traversed the EDT and copied out any 20538b8a9b1dSJustin T. Gibbs * matching entries. 20548b8a9b1dSJustin T. Gibbs */ 20558b8a9b1dSJustin T. Gibbs if (ret == 1) 20568b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_LAST; 20578b8a9b1dSJustin T. Gibbs 20588b8a9b1dSJustin T. Gibbs return(ret); 20598b8a9b1dSJustin T. Gibbs } 20608b8a9b1dSJustin T. Gibbs 20618b8a9b1dSJustin T. Gibbs static int 20628b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg) 20638b8a9b1dSJustin T. Gibbs { 20648b8a9b1dSJustin T. Gibbs struct cam_eb *bus, *next_bus; 20658b8a9b1dSJustin T. Gibbs int retval; 20668b8a9b1dSJustin T. Gibbs 20678b8a9b1dSJustin T. Gibbs retval = 1; 2068227d67aaSAlexander Motin if (start_bus) 2069227d67aaSAlexander Motin bus = start_bus; 2070227d67aaSAlexander Motin else { 20719a7c2696SAlexander Motin xpt_lock_buses(); 2072227d67aaSAlexander Motin bus = TAILQ_FIRST(&xsoftc.xpt_busses); 2073227d67aaSAlexander Motin if (bus == NULL) { 20749a7c2696SAlexander Motin xpt_unlock_buses(); 2075227d67aaSAlexander Motin return (retval); 2076227d67aaSAlexander Motin } 2077227d67aaSAlexander Motin bus->refcount++; 2078227d67aaSAlexander Motin xpt_unlock_buses(); 2079227d67aaSAlexander Motin } 2080227d67aaSAlexander Motin for (; bus != NULL; bus = next_bus) { 20818b8a9b1dSJustin T. Gibbs retval = tr_func(bus, arg); 2082227d67aaSAlexander Motin if (retval == 0) { 2083227d67aaSAlexander Motin xpt_release_bus(bus); 2084227d67aaSAlexander Motin break; 2085227d67aaSAlexander Motin } 20869a7c2696SAlexander Motin xpt_lock_buses(); 20878900f4b8SKenneth D. Merry next_bus = TAILQ_NEXT(bus, links); 2088227d67aaSAlexander Motin if (next_bus) 2089227d67aaSAlexander Motin next_bus->refcount++; 20909a7c2696SAlexander Motin xpt_unlock_buses(); 20918900f4b8SKenneth D. Merry xpt_release_bus(bus); 20928b8a9b1dSJustin T. Gibbs } 20938b8a9b1dSJustin T. Gibbs return(retval); 20948b8a9b1dSJustin T. Gibbs } 20958b8a9b1dSJustin T. Gibbs 20968b8a9b1dSJustin T. Gibbs static int 20978b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target, 20988b8a9b1dSJustin T. Gibbs xpt_targetfunc_t *tr_func, void *arg) 20998b8a9b1dSJustin T. Gibbs { 21008b8a9b1dSJustin T. Gibbs struct cam_et *target, *next_target; 21018b8a9b1dSJustin T. Gibbs int retval; 21028b8a9b1dSJustin T. Gibbs 21038b8a9b1dSJustin T. Gibbs retval = 1; 2104227d67aaSAlexander Motin if (start_target) 2105227d67aaSAlexander Motin target = start_target; 2106227d67aaSAlexander Motin else { 2107227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2108227d67aaSAlexander Motin target = TAILQ_FIRST(&bus->et_entries); 2109227d67aaSAlexander Motin if (target == NULL) { 2110227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 21118b8a9b1dSJustin T. Gibbs return (retval); 21128b8a9b1dSJustin T. Gibbs } 2113227d67aaSAlexander Motin target->refcount++; 2114227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2115227d67aaSAlexander Motin } 2116227d67aaSAlexander Motin for (; target != NULL; target = next_target) { 2117227d67aaSAlexander Motin retval = tr_func(target, arg); 2118227d67aaSAlexander Motin if (retval == 0) { 2119227d67aaSAlexander Motin xpt_release_target(target); 2120227d67aaSAlexander Motin break; 2121227d67aaSAlexander Motin } 2122227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2123227d67aaSAlexander Motin next_target = TAILQ_NEXT(target, links); 2124227d67aaSAlexander Motin if (next_target) 2125227d67aaSAlexander Motin next_target->refcount++; 2126227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2127227d67aaSAlexander Motin xpt_release_target(target); 2128227d67aaSAlexander Motin } 21298b8a9b1dSJustin T. Gibbs return(retval); 21308b8a9b1dSJustin T. Gibbs } 21318b8a9b1dSJustin T. Gibbs 21328b8a9b1dSJustin T. Gibbs static int 21338b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device, 21348b8a9b1dSJustin T. Gibbs xpt_devicefunc_t *tr_func, void *arg) 21358b8a9b1dSJustin T. Gibbs { 2136227d67aaSAlexander Motin struct cam_eb *bus; 21378b8a9b1dSJustin T. Gibbs struct cam_ed *device, *next_device; 21388b8a9b1dSJustin T. Gibbs int retval; 21398b8a9b1dSJustin T. Gibbs 21408b8a9b1dSJustin T. Gibbs retval = 1; 2141227d67aaSAlexander Motin bus = target->bus; 2142227d67aaSAlexander Motin if (start_device) 2143227d67aaSAlexander Motin device = start_device; 2144227d67aaSAlexander Motin else { 2145227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2146227d67aaSAlexander Motin device = TAILQ_FIRST(&target->ed_entries); 2147227d67aaSAlexander Motin if (device == NULL) { 2148227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 21498b8a9b1dSJustin T. Gibbs return (retval); 21508b8a9b1dSJustin T. Gibbs } 2151227d67aaSAlexander Motin device->refcount++; 2152227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2153227d67aaSAlexander Motin } 2154227d67aaSAlexander Motin for (; device != NULL; device = next_device) { 2155227d67aaSAlexander Motin mtx_lock(&device->device_mtx); 2156227d67aaSAlexander Motin retval = tr_func(device, arg); 2157227d67aaSAlexander Motin mtx_unlock(&device->device_mtx); 2158227d67aaSAlexander Motin if (retval == 0) { 2159227d67aaSAlexander Motin xpt_release_device(device); 2160227d67aaSAlexander Motin break; 2161227d67aaSAlexander Motin } 2162227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2163227d67aaSAlexander Motin next_device = TAILQ_NEXT(device, links); 2164227d67aaSAlexander Motin if (next_device) 2165227d67aaSAlexander Motin next_device->refcount++; 2166227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2167227d67aaSAlexander Motin xpt_release_device(device); 2168227d67aaSAlexander Motin } 21698b8a9b1dSJustin T. Gibbs return(retval); 21708b8a9b1dSJustin T. Gibbs } 21718b8a9b1dSJustin T. Gibbs 21728b8a9b1dSJustin T. Gibbs static int 21738b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph, 21748b8a9b1dSJustin T. Gibbs xpt_periphfunc_t *tr_func, void *arg) 21758b8a9b1dSJustin T. Gibbs { 2176227d67aaSAlexander Motin struct cam_eb *bus; 21778b8a9b1dSJustin T. Gibbs struct cam_periph *periph, *next_periph; 21788b8a9b1dSJustin T. Gibbs int retval; 21798b8a9b1dSJustin T. Gibbs 21808b8a9b1dSJustin T. Gibbs retval = 1; 21818b8a9b1dSJustin T. Gibbs 2182227d67aaSAlexander Motin bus = device->target->bus; 2183227d67aaSAlexander Motin if (start_periph) 2184227d67aaSAlexander Motin periph = start_periph; 2185227d67aaSAlexander Motin else { 21868900f4b8SKenneth D. Merry xpt_lock_buses(); 2187227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2188227d67aaSAlexander Motin periph = SLIST_FIRST(&device->periphs); 2189227d67aaSAlexander Motin while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0) 2190227d67aaSAlexander Motin periph = SLIST_NEXT(periph, periph_links); 2191227d67aaSAlexander Motin if (periph == NULL) { 2192227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 21938900f4b8SKenneth D. Merry xpt_unlock_buses(); 2194227d67aaSAlexander Motin return (retval); 2195227d67aaSAlexander Motin } 2196227d67aaSAlexander Motin periph->refcount++; 2197227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2198227d67aaSAlexander Motin xpt_unlock_buses(); 2199227d67aaSAlexander Motin } 2200227d67aaSAlexander Motin for (; periph != NULL; periph = next_periph) { 2201227d67aaSAlexander Motin retval = tr_func(periph, arg); 2202227d67aaSAlexander Motin if (retval == 0) { 22039813c936SAlexander Motin cam_periph_release_locked(periph); 2204227d67aaSAlexander Motin break; 2205227d67aaSAlexander Motin } 2206227d67aaSAlexander Motin xpt_lock_buses(); 2207227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 2208227d67aaSAlexander Motin next_periph = SLIST_NEXT(periph, periph_links); 2209227d67aaSAlexander Motin while (next_periph != NULL && 2210227d67aaSAlexander Motin (next_periph->flags & CAM_PERIPH_FREE) != 0) 2211840a5dd4SAlexander Motin next_periph = SLIST_NEXT(next_periph, periph_links); 2212227d67aaSAlexander Motin if (next_periph) 2213227d67aaSAlexander Motin next_periph->refcount++; 2214227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 2215227d67aaSAlexander Motin xpt_unlock_buses(); 2216227d67aaSAlexander Motin cam_periph_release_locked(periph); 2217227d67aaSAlexander Motin } 22188b8a9b1dSJustin T. Gibbs return(retval); 22198b8a9b1dSJustin T. Gibbs } 22208b8a9b1dSJustin T. Gibbs 22218b8a9b1dSJustin T. Gibbs static int 22228b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv, 22238b8a9b1dSJustin T. Gibbs xpt_pdrvfunc_t *tr_func, void *arg) 22248b8a9b1dSJustin T. Gibbs { 22258b8a9b1dSJustin T. Gibbs struct periph_driver **pdrv; 22268b8a9b1dSJustin T. Gibbs int retval; 22278b8a9b1dSJustin T. Gibbs 22288b8a9b1dSJustin T. Gibbs retval = 1; 22298b8a9b1dSJustin T. Gibbs 22308b8a9b1dSJustin T. Gibbs /* 22318b8a9b1dSJustin T. Gibbs * We don't traverse the peripheral driver list like we do the 22328b8a9b1dSJustin T. Gibbs * other lists, because it is a linker set, and therefore cannot be 22338b8a9b1dSJustin T. Gibbs * changed during runtime. If the peripheral driver list is ever 22348b8a9b1dSJustin T. Gibbs * re-done to be something other than a linker set (i.e. it can 22358b8a9b1dSJustin T. Gibbs * change while the system is running), the list traversal should 22368b8a9b1dSJustin T. Gibbs * be modified to work like the other traversal functions. 22378b8a9b1dSJustin T. Gibbs */ 22380b7c27b9SPeter Wemm for (pdrv = (start_pdrv ? start_pdrv : periph_drivers); 22398b8a9b1dSJustin T. Gibbs *pdrv != NULL; pdrv++) { 22408b8a9b1dSJustin T. Gibbs retval = tr_func(pdrv, arg); 22418b8a9b1dSJustin T. Gibbs 22428b8a9b1dSJustin T. Gibbs if (retval == 0) 22438b8a9b1dSJustin T. Gibbs return(retval); 22448b8a9b1dSJustin T. Gibbs } 22458b8a9b1dSJustin T. Gibbs 22468b8a9b1dSJustin T. Gibbs return(retval); 22478b8a9b1dSJustin T. Gibbs } 22488b8a9b1dSJustin T. Gibbs 22498b8a9b1dSJustin T. Gibbs static int 22508b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv, 22518b8a9b1dSJustin T. Gibbs struct cam_periph *start_periph, 22528b8a9b1dSJustin T. Gibbs xpt_periphfunc_t *tr_func, void *arg) 22538b8a9b1dSJustin T. Gibbs { 22548b8a9b1dSJustin T. Gibbs struct cam_periph *periph, *next_periph; 22558b8a9b1dSJustin T. Gibbs int retval; 22568b8a9b1dSJustin T. Gibbs 22578b8a9b1dSJustin T. Gibbs retval = 1; 22588b8a9b1dSJustin T. Gibbs 2259227d67aaSAlexander Motin if (start_periph) 2260227d67aaSAlexander Motin periph = start_periph; 2261227d67aaSAlexander Motin else { 2262f1e2546aSMatt Jacob xpt_lock_buses(); 2263227d67aaSAlexander Motin periph = TAILQ_FIRST(&(*pdrv)->units); 2264227d67aaSAlexander Motin while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0) 2265227d67aaSAlexander Motin periph = TAILQ_NEXT(periph, unit_links); 2266227d67aaSAlexander Motin if (periph == NULL) { 2267227d67aaSAlexander Motin xpt_unlock_buses(); 2268227d67aaSAlexander Motin return (retval); 22698900f4b8SKenneth D. Merry } 22708900f4b8SKenneth D. Merry periph->refcount++; 2271dcdf6e74SAlexander Motin xpt_unlock_buses(); 22728b8a9b1dSJustin T. Gibbs } 2273227d67aaSAlexander Motin for (; periph != NULL; periph = next_periph) { 2274227d67aaSAlexander Motin cam_periph_lock(periph); 2275227d67aaSAlexander Motin retval = tr_func(periph, arg); 2276227d67aaSAlexander Motin cam_periph_unlock(periph); 2277227d67aaSAlexander Motin if (retval == 0) { 2278227d67aaSAlexander Motin cam_periph_release(periph); 2279227d67aaSAlexander Motin break; 2280227d67aaSAlexander Motin } 2281227d67aaSAlexander Motin xpt_lock_buses(); 2282227d67aaSAlexander Motin next_periph = TAILQ_NEXT(periph, unit_links); 2283227d67aaSAlexander Motin while (next_periph != NULL && 2284227d67aaSAlexander Motin (next_periph->flags & CAM_PERIPH_FREE) != 0) 2285840a5dd4SAlexander Motin next_periph = TAILQ_NEXT(next_periph, unit_links); 2286227d67aaSAlexander Motin if (next_periph) 2287227d67aaSAlexander Motin next_periph->refcount++; 2288f1e2546aSMatt Jacob xpt_unlock_buses(); 2289227d67aaSAlexander Motin cam_periph_release(periph); 2290227d67aaSAlexander Motin } 22918b8a9b1dSJustin T. Gibbs return(retval); 22928b8a9b1dSJustin T. Gibbs } 22938b8a9b1dSJustin T. Gibbs 22948b8a9b1dSJustin T. Gibbs static int 22958b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg) 22968b8a9b1dSJustin T. Gibbs { 22978b8a9b1dSJustin T. Gibbs struct xpt_traverse_config *tr_config; 22988b8a9b1dSJustin T. Gibbs 22998b8a9b1dSJustin T. Gibbs tr_config = (struct xpt_traverse_config *)arg; 23008b8a9b1dSJustin T. Gibbs 23018b8a9b1dSJustin T. Gibbs if (tr_config->depth == XPT_DEPTH_BUS) { 23028b8a9b1dSJustin T. Gibbs xpt_busfunc_t *tr_func; 23038b8a9b1dSJustin T. Gibbs 23048b8a9b1dSJustin T. Gibbs tr_func = (xpt_busfunc_t *)tr_config->tr_func; 23058b8a9b1dSJustin T. Gibbs 23068b8a9b1dSJustin T. Gibbs return(tr_func(bus, tr_config->tr_arg)); 23078b8a9b1dSJustin T. Gibbs } else 23088b8a9b1dSJustin T. Gibbs return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg)); 23098b8a9b1dSJustin T. Gibbs } 23108b8a9b1dSJustin T. Gibbs 23118b8a9b1dSJustin T. Gibbs static int 23128b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg) 23138b8a9b1dSJustin T. Gibbs { 23148b8a9b1dSJustin T. Gibbs struct xpt_traverse_config *tr_config; 23158b8a9b1dSJustin T. Gibbs 23168b8a9b1dSJustin T. Gibbs tr_config = (struct xpt_traverse_config *)arg; 23178b8a9b1dSJustin T. Gibbs 23188b8a9b1dSJustin T. Gibbs if (tr_config->depth == XPT_DEPTH_TARGET) { 23198b8a9b1dSJustin T. Gibbs xpt_targetfunc_t *tr_func; 23208b8a9b1dSJustin T. Gibbs 23218b8a9b1dSJustin T. Gibbs tr_func = (xpt_targetfunc_t *)tr_config->tr_func; 23228b8a9b1dSJustin T. Gibbs 23238b8a9b1dSJustin T. Gibbs return(tr_func(target, tr_config->tr_arg)); 23248b8a9b1dSJustin T. Gibbs } else 23258b8a9b1dSJustin T. Gibbs return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg)); 23268b8a9b1dSJustin T. Gibbs } 23278b8a9b1dSJustin T. Gibbs 23288b8a9b1dSJustin T. Gibbs static int 23298b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg) 23308b8a9b1dSJustin T. Gibbs { 23318b8a9b1dSJustin T. Gibbs struct xpt_traverse_config *tr_config; 23328b8a9b1dSJustin T. Gibbs 23338b8a9b1dSJustin T. Gibbs tr_config = (struct xpt_traverse_config *)arg; 23348b8a9b1dSJustin T. Gibbs 23358b8a9b1dSJustin T. Gibbs if (tr_config->depth == XPT_DEPTH_DEVICE) { 23368b8a9b1dSJustin T. Gibbs xpt_devicefunc_t *tr_func; 23378b8a9b1dSJustin T. Gibbs 23388b8a9b1dSJustin T. Gibbs tr_func = (xpt_devicefunc_t *)tr_config->tr_func; 23398b8a9b1dSJustin T. Gibbs 23408b8a9b1dSJustin T. Gibbs return(tr_func(device, tr_config->tr_arg)); 23418b8a9b1dSJustin T. Gibbs } else 23428b8a9b1dSJustin T. Gibbs return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg)); 23438b8a9b1dSJustin T. Gibbs } 23448b8a9b1dSJustin T. Gibbs 23458b8a9b1dSJustin T. Gibbs static int 23468b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg) 23478b8a9b1dSJustin T. Gibbs { 23488b8a9b1dSJustin T. Gibbs struct xpt_traverse_config *tr_config; 23498b8a9b1dSJustin T. Gibbs xpt_periphfunc_t *tr_func; 23508b8a9b1dSJustin T. Gibbs 23518b8a9b1dSJustin T. Gibbs tr_config = (struct xpt_traverse_config *)arg; 23528b8a9b1dSJustin T. Gibbs 23538b8a9b1dSJustin T. Gibbs tr_func = (xpt_periphfunc_t *)tr_config->tr_func; 23548b8a9b1dSJustin T. Gibbs 23558b8a9b1dSJustin T. Gibbs /* 23568b8a9b1dSJustin T. Gibbs * Unlike the other default functions, we don't check for depth 23578b8a9b1dSJustin T. Gibbs * here. The peripheral driver level is the last level in the EDT, 23588b8a9b1dSJustin T. Gibbs * so if we're here, we should execute the function in question. 23598b8a9b1dSJustin T. Gibbs */ 23608b8a9b1dSJustin T. Gibbs return(tr_func(periph, tr_config->tr_arg)); 23618b8a9b1dSJustin T. Gibbs } 23628b8a9b1dSJustin T. Gibbs 23638b8a9b1dSJustin T. Gibbs /* 23648b8a9b1dSJustin T. Gibbs * Execute the given function for every bus in the EDT. 23658b8a9b1dSJustin T. Gibbs */ 23668b8a9b1dSJustin T. Gibbs static int 23678b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg) 23688b8a9b1dSJustin T. Gibbs { 23698b8a9b1dSJustin T. Gibbs struct xpt_traverse_config tr_config; 23708b8a9b1dSJustin T. Gibbs 23718b8a9b1dSJustin T. Gibbs tr_config.depth = XPT_DEPTH_BUS; 23728b8a9b1dSJustin T. Gibbs tr_config.tr_func = tr_func; 23738b8a9b1dSJustin T. Gibbs tr_config.tr_arg = arg; 23748b8a9b1dSJustin T. Gibbs 23758b8a9b1dSJustin T. Gibbs return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 23768b8a9b1dSJustin T. Gibbs } 23778b8a9b1dSJustin T. Gibbs 23788b8a9b1dSJustin T. Gibbs /* 23798b8a9b1dSJustin T. Gibbs * Execute the given function for every device in the EDT. 23808b8a9b1dSJustin T. Gibbs */ 23818b8a9b1dSJustin T. Gibbs static int 23828b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg) 23838b8a9b1dSJustin T. Gibbs { 23848b8a9b1dSJustin T. Gibbs struct xpt_traverse_config tr_config; 23858b8a9b1dSJustin T. Gibbs 23868b8a9b1dSJustin T. Gibbs tr_config.depth = XPT_DEPTH_DEVICE; 23878b8a9b1dSJustin T. Gibbs tr_config.tr_func = tr_func; 23888b8a9b1dSJustin T. Gibbs tr_config.tr_arg = arg; 23898b8a9b1dSJustin T. Gibbs 23908b8a9b1dSJustin T. Gibbs return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 23918b8a9b1dSJustin T. Gibbs } 23928b8a9b1dSJustin T. Gibbs 23938b8a9b1dSJustin T. Gibbs static int 23948b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg) 23958b8a9b1dSJustin T. Gibbs { 23968b8a9b1dSJustin T. Gibbs struct cam_path path; 23978b8a9b1dSJustin T. Gibbs struct ccb_getdev cgd; 23987685edecSAlexander Motin struct ccb_setasync *csa = (struct ccb_setasync *)arg; 23998b8a9b1dSJustin T. Gibbs 2400c8bead2aSJustin T. Gibbs /* 2401c8bead2aSJustin T. Gibbs * Don't report unconfigured devices (Wildcard devs, 2402c8bead2aSJustin T. Gibbs * devices only for target mode, device instances 2403c8bead2aSJustin T. Gibbs * that have been invalidated but are waiting for 2404c8bead2aSJustin T. Gibbs * their last reference count to be released). 2405c8bead2aSJustin T. Gibbs */ 2406c8bead2aSJustin T. Gibbs if ((device->flags & CAM_DEV_UNCONFIGURED) != 0) 2407c8bead2aSJustin T. Gibbs return (1); 2408c8bead2aSJustin T. Gibbs 24098b8a9b1dSJustin T. Gibbs xpt_compile_path(&path, 24108b8a9b1dSJustin T. Gibbs NULL, 24118b8a9b1dSJustin T. Gibbs device->target->bus->path_id, 24128b8a9b1dSJustin T. Gibbs device->target->target_id, 24138b8a9b1dSJustin T. Gibbs device->lun_id); 2414bbfa4aa1SAlexander Motin xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL); 24158b8a9b1dSJustin T. Gibbs cgd.ccb_h.func_code = XPT_GDEV_TYPE; 24168b8a9b1dSJustin T. Gibbs xpt_action((union ccb *)&cgd); 24177685edecSAlexander Motin csa->callback(csa->callback_arg, 24188b8a9b1dSJustin T. Gibbs AC_FOUND_DEVICE, 24198b8a9b1dSJustin T. Gibbs &path, &cgd); 24208b8a9b1dSJustin T. Gibbs xpt_release_path(&path); 24218b8a9b1dSJustin T. Gibbs 24228b8a9b1dSJustin T. Gibbs return(1); 24238b8a9b1dSJustin T. Gibbs } 2424c8bead2aSJustin T. Gibbs 24258b8a9b1dSJustin T. Gibbs static int 24268b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg) 24278b8a9b1dSJustin T. Gibbs { 24288b8a9b1dSJustin T. Gibbs struct cam_path path; 24298b8a9b1dSJustin T. Gibbs struct ccb_pathinq cpi; 24307685edecSAlexander Motin struct ccb_setasync *csa = (struct ccb_setasync *)arg; 24318b8a9b1dSJustin T. Gibbs 24328b8a9b1dSJustin T. Gibbs xpt_compile_path(&path, /*periph*/NULL, 24336dfc67e3SAlexander Motin bus->path_id, 24348b8a9b1dSJustin T. Gibbs CAM_TARGET_WILDCARD, 24358b8a9b1dSJustin T. Gibbs CAM_LUN_WILDCARD); 2436227d67aaSAlexander Motin xpt_path_lock(&path); 2437bbfa4aa1SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL); 24388b8a9b1dSJustin T. Gibbs cpi.ccb_h.func_code = XPT_PATH_INQ; 24398b8a9b1dSJustin T. Gibbs xpt_action((union ccb *)&cpi); 24407685edecSAlexander Motin csa->callback(csa->callback_arg, 24418b8a9b1dSJustin T. Gibbs AC_PATH_REGISTERED, 24428b8a9b1dSJustin T. Gibbs &path, &cpi); 2443227d67aaSAlexander Motin xpt_path_unlock(&path); 24448b8a9b1dSJustin T. Gibbs xpt_release_path(&path); 24458b8a9b1dSJustin T. Gibbs 24468b8a9b1dSJustin T. Gibbs return(1); 24478b8a9b1dSJustin T. Gibbs } 24488b8a9b1dSJustin T. Gibbs 24498b8a9b1dSJustin T. Gibbs void 24508b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb) 24518b8a9b1dSJustin T. Gibbs { 24529911ecf9SJustin T. Gibbs 24538b8a9b1dSJustin T. Gibbs CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n")); 24548b8a9b1dSJustin T. Gibbs 24558b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_INPROG; 245652c9ce25SScott Long (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb); 245752c9ce25SScott Long } 245852c9ce25SScott Long 245952c9ce25SScott Long void 246052c9ce25SScott Long xpt_action_default(union ccb *start_ccb) 246152c9ce25SScott Long { 2462da396db2SAlexander Motin struct cam_path *path; 2463227d67aaSAlexander Motin struct cam_sim *sim; 2464227d67aaSAlexander Motin int lock; 246552c9ce25SScott Long 2466da396db2SAlexander Motin path = start_ccb->ccb_h.path; 2467da396db2SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n")); 246852c9ce25SScott Long 24698b8a9b1dSJustin T. Gibbs switch (start_ccb->ccb_h.func_code) { 24708b8a9b1dSJustin T. Gibbs case XPT_SCSI_IO: 2471d05caa00SKenneth D. Merry { 24723393f8daSKenneth D. Merry struct cam_ed *device; 2473d05caa00SKenneth D. Merry 24748b8a9b1dSJustin T. Gibbs /* 24758b8a9b1dSJustin T. Gibbs * For the sake of compatibility with SCSI-1 24768b8a9b1dSJustin T. Gibbs * devices that may not understand the identify 24778b8a9b1dSJustin T. Gibbs * message, we include lun information in the 24788b8a9b1dSJustin T. Gibbs * second byte of all commands. SCSI-1 specifies 24798b8a9b1dSJustin T. Gibbs * that luns are a 3 bit value and reserves only 3 24808b8a9b1dSJustin T. Gibbs * bits for lun information in the CDB. Later 24818b8a9b1dSJustin T. Gibbs * revisions of the SCSI spec allow for more than 8 24828b8a9b1dSJustin T. Gibbs * luns, but have deprecated lun information in the 24838b8a9b1dSJustin T. Gibbs * CDB. So, if the lun won't fit, we must omit. 24848b8a9b1dSJustin T. Gibbs * 24858b8a9b1dSJustin T. Gibbs * Also be aware that during initial probing for devices, 24868b8a9b1dSJustin T. Gibbs * the inquiry information is unknown but initialized to 0. 24878b8a9b1dSJustin T. Gibbs * This means that this code will be exercised while probing 24888b8a9b1dSJustin T. Gibbs * devices with an ANSI revision greater than 2. 24898b8a9b1dSJustin T. Gibbs */ 2490da396db2SAlexander Motin device = path->device; 24913393f8daSKenneth D. Merry if (device->protocol_version <= SCSI_REV_2 24928b8a9b1dSJustin T. Gibbs && start_ccb->ccb_h.target_lun < 8 24938b8a9b1dSJustin T. Gibbs && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) { 24948b8a9b1dSJustin T. Gibbs 24958b8a9b1dSJustin T. Gibbs start_ccb->csio.cdb_io.cdb_bytes[1] |= 24968b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.target_lun << 5; 24978b8a9b1dSJustin T. Gibbs } 24988b8a9b1dSJustin T. Gibbs start_ccb->csio.scsi_status = SCSI_STATUS_OK; 2499d05caa00SKenneth D. Merry } 250007c6eac9SPoul-Henning Kamp /* FALLTHROUGH */ 25018b8a9b1dSJustin T. Gibbs case XPT_TARGET_IO: 25028b8a9b1dSJustin T. Gibbs case XPT_CONT_TARGET_IO: 25032cefde5fSJustin T. Gibbs start_ccb->csio.sense_resid = 0; 25042cefde5fSJustin T. Gibbs start_ccb->csio.resid = 0; 25052cefde5fSJustin T. Gibbs /* FALLTHROUGH */ 250652c9ce25SScott Long case XPT_ATA_IO: 2507de9ebb68SAlexander Motin if (start_ccb->ccb_h.func_code == XPT_ATA_IO) 250852c9ce25SScott Long start_ccb->ataio.resid = 0; 2509b882a6d3SMatt Jacob /* FALLTHROUGH */ 251010e1cf63SKenneth D. Merry case XPT_RESET_DEV: 25118b8a9b1dSJustin T. Gibbs case XPT_ENG_EXEC: 251206e79492SKenneth D. Merry case XPT_SMP_IO: 25132cefde5fSJustin T. Gibbs { 2514227d67aaSAlexander Motin struct cam_devq *devq; 25152cefde5fSJustin T. Gibbs 2516227d67aaSAlexander Motin devq = path->bus->sim->devq; 2517227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 2518227d67aaSAlexander Motin cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb); 2519227d67aaSAlexander Motin if (xpt_schedule_devq(devq, path->device) != 0) 2520227d67aaSAlexander Motin xpt_run_devq(devq); 2521227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 2522227d67aaSAlexander Motin break; 2523227d67aaSAlexander Motin } 2524227d67aaSAlexander Motin case XPT_CALC_GEOMETRY: 25258b8a9b1dSJustin T. Gibbs /* Filter out garbage */ 25268b8a9b1dSJustin T. Gibbs if (start_ccb->ccg.block_size == 0 25278b8a9b1dSJustin T. Gibbs || start_ccb->ccg.volume_size == 0) { 25288b8a9b1dSJustin T. Gibbs start_ccb->ccg.cylinders = 0; 25298b8a9b1dSJustin T. Gibbs start_ccb->ccg.heads = 0; 25308b8a9b1dSJustin T. Gibbs start_ccb->ccg.secs_per_track = 0; 25318b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 25328b8a9b1dSJustin T. Gibbs break; 25338b8a9b1dSJustin T. Gibbs } 2534abef0e67SMarius Strobl #if defined(PC98) || defined(__sparc64__) 25358b8a9b1dSJustin T. Gibbs /* 25368b8a9b1dSJustin T. Gibbs * In a PC-98 system, geometry translation depens on 25378b8a9b1dSJustin T. Gibbs * the "real" device geometry obtained from mode page 4. 25388b8a9b1dSJustin T. Gibbs * SCSI geometry translation is performed in the 25398b8a9b1dSJustin T. Gibbs * initialization routine of the SCSI BIOS and the result 25408b8a9b1dSJustin T. Gibbs * stored in host memory. If the translation is available 25418b8a9b1dSJustin T. Gibbs * in host memory, use it. If not, rely on the default 25428b8a9b1dSJustin T. Gibbs * translation the device driver performs. 2543abef0e67SMarius Strobl * For sparc64, we may need adjust the geometry of large 2544abef0e67SMarius Strobl * disks in order to fit the limitations of the 16-bit 2545abef0e67SMarius Strobl * fields of the VTOC8 disk label. 25468b8a9b1dSJustin T. Gibbs */ 25478b8a9b1dSJustin T. Gibbs if (scsi_da_bios_params(&start_ccb->ccg) != 0) { 25488b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 25498b8a9b1dSJustin T. Gibbs break; 25508b8a9b1dSJustin T. Gibbs } 25518b8a9b1dSJustin T. Gibbs #endif 2552227d67aaSAlexander Motin goto call_sim; 2553bb6087e5SJustin T. Gibbs case XPT_ABORT: 25542cefde5fSJustin T. Gibbs { 25552cefde5fSJustin T. Gibbs union ccb* abort_ccb; 25562cefde5fSJustin T. Gibbs 25572cefde5fSJustin T. Gibbs abort_ccb = start_ccb->cab.abort_ccb; 25582cefde5fSJustin T. Gibbs if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) { 25592cefde5fSJustin T. Gibbs 25602cefde5fSJustin T. Gibbs if (abort_ccb->ccb_h.pinfo.index >= 0) { 25612cefde5fSJustin T. Gibbs struct cam_ccbq *ccbq; 256283c5d981SAlexander Motin struct cam_ed *device; 25632cefde5fSJustin T. Gibbs 256483c5d981SAlexander Motin device = abort_ccb->ccb_h.path->device; 256583c5d981SAlexander Motin ccbq = &device->ccbq; 25662cefde5fSJustin T. Gibbs cam_ccbq_remove_ccb(ccbq, abort_ccb); 25672cefde5fSJustin T. Gibbs abort_ccb->ccb_h.status = 25682cefde5fSJustin T. Gibbs CAM_REQ_ABORTED|CAM_DEV_QFRZN; 25692cefde5fSJustin T. Gibbs xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 25702cefde5fSJustin T. Gibbs xpt_done(abort_ccb); 25712cefde5fSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 25722cefde5fSJustin T. Gibbs break; 25732cefde5fSJustin T. Gibbs } 25742cefde5fSJustin T. Gibbs if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX 25752cefde5fSJustin T. Gibbs && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) { 25762cefde5fSJustin T. Gibbs /* 25772cefde5fSJustin T. Gibbs * We've caught this ccb en route to 25782cefde5fSJustin T. Gibbs * the SIM. Flag it for abort and the 25792cefde5fSJustin T. Gibbs * SIM will do so just before starting 25802cefde5fSJustin T. Gibbs * real work on the CCB. 25812cefde5fSJustin T. Gibbs */ 25822cefde5fSJustin T. Gibbs abort_ccb->ccb_h.status = 25832cefde5fSJustin T. Gibbs CAM_REQ_ABORTED|CAM_DEV_QFRZN; 25842cefde5fSJustin T. Gibbs xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 25852cefde5fSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 25862cefde5fSJustin T. Gibbs break; 25872cefde5fSJustin T. Gibbs } 25882cefde5fSJustin T. Gibbs } 25892cefde5fSJustin T. Gibbs if (XPT_FC_IS_QUEUED(abort_ccb) 25902cefde5fSJustin T. Gibbs && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) { 25912cefde5fSJustin T. Gibbs /* 25922cefde5fSJustin T. Gibbs * It's already completed but waiting 25932cefde5fSJustin T. Gibbs * for our SWI to get to it. 25942cefde5fSJustin T. Gibbs */ 25952cefde5fSJustin T. Gibbs start_ccb->ccb_h.status = CAM_UA_ABORT; 25962cefde5fSJustin T. Gibbs break; 25972cefde5fSJustin T. Gibbs } 25982cefde5fSJustin T. Gibbs /* 25992cefde5fSJustin T. Gibbs * If we weren't able to take care of the abort request 26002cefde5fSJustin T. Gibbs * in the XPT, pass the request down to the SIM for processing. 26012cefde5fSJustin T. Gibbs */ 26022cefde5fSJustin T. Gibbs } 260307c6eac9SPoul-Henning Kamp /* FALLTHROUGH */ 26048b8a9b1dSJustin T. Gibbs case XPT_ACCEPT_TARGET_IO: 26058b8a9b1dSJustin T. Gibbs case XPT_EN_LUN: 26068b8a9b1dSJustin T. Gibbs case XPT_IMMED_NOTIFY: 26078b8a9b1dSJustin T. Gibbs case XPT_NOTIFY_ACK: 26088b8a9b1dSJustin T. Gibbs case XPT_RESET_BUS: 26092df76c16SMatt Jacob case XPT_IMMEDIATE_NOTIFY: 26102df76c16SMatt Jacob case XPT_NOTIFY_ACKNOWLEDGE: 26112df76c16SMatt Jacob case XPT_GET_SIM_KNOB: 26122df76c16SMatt Jacob case XPT_SET_SIM_KNOB: 2613227d67aaSAlexander Motin case XPT_GET_TRAN_SETTINGS: 2614227d67aaSAlexander Motin case XPT_SET_TRAN_SETTINGS: 261587cfaf0eSJustin T. Gibbs case XPT_PATH_INQ: 2616227d67aaSAlexander Motin call_sim: 2617da396db2SAlexander Motin sim = path->bus->sim; 2618227d67aaSAlexander Motin lock = (mtx_owned(sim->mtx) == 0); 2619227d67aaSAlexander Motin if (lock) 2620227d67aaSAlexander Motin CAM_SIM_LOCK(sim); 262187cfaf0eSJustin T. Gibbs (*(sim->sim_action))(sim, start_ccb); 2622227d67aaSAlexander Motin if (lock) 2623227d67aaSAlexander Motin CAM_SIM_UNLOCK(sim); 262487cfaf0eSJustin T. Gibbs break; 262587cfaf0eSJustin T. Gibbs case XPT_PATH_STATS: 2626da396db2SAlexander Motin start_ccb->cpis.last_reset = path->bus->last_reset; 262787cfaf0eSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 262887cfaf0eSJustin T. Gibbs break; 26298b8a9b1dSJustin T. Gibbs case XPT_GDEV_TYPE: 2630a5479bc5SJustin T. Gibbs { 263187cfaf0eSJustin T. Gibbs struct cam_ed *dev; 2632a5479bc5SJustin T. Gibbs 2633da396db2SAlexander Motin dev = path->device; 263487cfaf0eSJustin T. Gibbs if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 26358b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 26368b8a9b1dSJustin T. Gibbs } else { 26378b8a9b1dSJustin T. Gibbs struct ccb_getdev *cgd; 26388b8a9b1dSJustin T. Gibbs 26398b8a9b1dSJustin T. Gibbs cgd = &start_ccb->cgd; 264052c9ce25SScott Long cgd->protocol = dev->protocol; 26418b8a9b1dSJustin T. Gibbs cgd->inq_data = dev->inq_data; 264252c9ce25SScott Long cgd->ident_data = dev->ident_data; 264330a4094fSAlexander Motin cgd->inq_flags = dev->inq_flags; 26448b8a9b1dSJustin T. Gibbs cgd->ccb_h.status = CAM_REQ_CMP; 26458b8a9b1dSJustin T. Gibbs cgd->serial_num_len = dev->serial_num_len; 26468b8a9b1dSJustin T. Gibbs if ((dev->serial_num_len > 0) 26478b8a9b1dSJustin T. Gibbs && (dev->serial_num != NULL)) 26488b8a9b1dSJustin T. Gibbs bcopy(dev->serial_num, cgd->serial_num, 26498b8a9b1dSJustin T. Gibbs dev->serial_num_len); 26508b8a9b1dSJustin T. Gibbs } 26518b8a9b1dSJustin T. Gibbs break; 2652a5479bc5SJustin T. Gibbs } 265387cfaf0eSJustin T. Gibbs case XPT_GDEV_STATS: 265487cfaf0eSJustin T. Gibbs { 265587cfaf0eSJustin T. Gibbs struct cam_ed *dev; 265687cfaf0eSJustin T. Gibbs 2657da396db2SAlexander Motin dev = path->device; 265887cfaf0eSJustin T. Gibbs if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 265987cfaf0eSJustin T. Gibbs start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 266087cfaf0eSJustin T. Gibbs } else { 266187cfaf0eSJustin T. Gibbs struct ccb_getdevstats *cgds; 266287cfaf0eSJustin T. Gibbs struct cam_eb *bus; 266387cfaf0eSJustin T. Gibbs struct cam_et *tar; 2664959ec258SAlexander Motin struct cam_devq *devq; 266587cfaf0eSJustin T. Gibbs 266687cfaf0eSJustin T. Gibbs cgds = &start_ccb->cgds; 2667da396db2SAlexander Motin bus = path->bus; 2668da396db2SAlexander Motin tar = path->target; 2669959ec258SAlexander Motin devq = bus->sim->devq; 2670959ec258SAlexander Motin mtx_lock(&devq->send_mtx); 267187cfaf0eSJustin T. Gibbs cgds->dev_openings = dev->ccbq.dev_openings; 267287cfaf0eSJustin T. Gibbs cgds->dev_active = dev->ccbq.dev_active; 2673959ec258SAlexander Motin cgds->allocated = dev->ccbq.allocated; 2674959ec258SAlexander Motin cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq); 2675959ec258SAlexander Motin cgds->held = cgds->allocated - cgds->dev_active - 2676959ec258SAlexander Motin cgds->queued; 267787cfaf0eSJustin T. Gibbs cgds->last_reset = tar->last_reset; 267852c9ce25SScott Long cgds->maxtags = dev->maxtags; 267952c9ce25SScott Long cgds->mintags = dev->mintags; 268087cfaf0eSJustin T. Gibbs if (timevalcmp(&tar->last_reset, &bus->last_reset, <)) 268187cfaf0eSJustin T. Gibbs cgds->last_reset = bus->last_reset; 2682959ec258SAlexander Motin mtx_unlock(&devq->send_mtx); 268387cfaf0eSJustin T. Gibbs cgds->ccb_h.status = CAM_REQ_CMP; 268487cfaf0eSJustin T. Gibbs } 268587cfaf0eSJustin T. Gibbs break; 268687cfaf0eSJustin T. Gibbs } 26878b8a9b1dSJustin T. Gibbs case XPT_GDEVLIST: 26888b8a9b1dSJustin T. Gibbs { 26898b8a9b1dSJustin T. Gibbs struct cam_periph *nperiph; 26908b8a9b1dSJustin T. Gibbs struct periph_list *periph_head; 26918b8a9b1dSJustin T. Gibbs struct ccb_getdevlist *cgdl; 26923393f8daSKenneth D. Merry u_int i; 26938b8a9b1dSJustin T. Gibbs struct cam_ed *device; 26948b8a9b1dSJustin T. Gibbs int found; 26958b8a9b1dSJustin T. Gibbs 26968b8a9b1dSJustin T. Gibbs 26978b8a9b1dSJustin T. Gibbs found = 0; 26988b8a9b1dSJustin T. Gibbs 26998b8a9b1dSJustin T. Gibbs /* 27008b8a9b1dSJustin T. Gibbs * Don't want anyone mucking with our data. 27018b8a9b1dSJustin T. Gibbs */ 2702da396db2SAlexander Motin device = path->device; 27038b8a9b1dSJustin T. Gibbs periph_head = &device->periphs; 27048b8a9b1dSJustin T. Gibbs cgdl = &start_ccb->cgdl; 27058b8a9b1dSJustin T. Gibbs 27068b8a9b1dSJustin T. Gibbs /* 27078b8a9b1dSJustin T. Gibbs * Check and see if the list has changed since the user 27088b8a9b1dSJustin T. Gibbs * last requested a list member. If so, tell them that the 27098b8a9b1dSJustin T. Gibbs * list has changed, and therefore they need to start over 27108b8a9b1dSJustin T. Gibbs * from the beginning. 27118b8a9b1dSJustin T. Gibbs */ 27128b8a9b1dSJustin T. Gibbs if ((cgdl->index != 0) && 27138b8a9b1dSJustin T. Gibbs (cgdl->generation != device->generation)) { 27148b8a9b1dSJustin T. Gibbs cgdl->status = CAM_GDEVLIST_LIST_CHANGED; 27158b8a9b1dSJustin T. Gibbs break; 27168b8a9b1dSJustin T. Gibbs } 27178b8a9b1dSJustin T. Gibbs 27188b8a9b1dSJustin T. Gibbs /* 27198b8a9b1dSJustin T. Gibbs * Traverse the list of peripherals and attempt to find 27208b8a9b1dSJustin T. Gibbs * the requested peripheral. 27218b8a9b1dSJustin T. Gibbs */ 2722fc2ffbe6SPoul-Henning Kamp for (nperiph = SLIST_FIRST(periph_head), i = 0; 27238b8a9b1dSJustin T. Gibbs (nperiph != NULL) && (i <= cgdl->index); 2724fc2ffbe6SPoul-Henning Kamp nperiph = SLIST_NEXT(nperiph, periph_links), i++) { 27258b8a9b1dSJustin T. Gibbs if (i == cgdl->index) { 27268b8a9b1dSJustin T. Gibbs strncpy(cgdl->periph_name, 27278b8a9b1dSJustin T. Gibbs nperiph->periph_name, 27288b8a9b1dSJustin T. Gibbs DEV_IDLEN); 27298b8a9b1dSJustin T. Gibbs cgdl->unit_number = nperiph->unit_number; 27308b8a9b1dSJustin T. Gibbs found = 1; 27318b8a9b1dSJustin T. Gibbs } 27328b8a9b1dSJustin T. Gibbs } 27338b8a9b1dSJustin T. Gibbs if (found == 0) { 27348b8a9b1dSJustin T. Gibbs cgdl->status = CAM_GDEVLIST_ERROR; 27358b8a9b1dSJustin T. Gibbs break; 27368b8a9b1dSJustin T. Gibbs } 27378b8a9b1dSJustin T. Gibbs 27388b8a9b1dSJustin T. Gibbs if (nperiph == NULL) 27398b8a9b1dSJustin T. Gibbs cgdl->status = CAM_GDEVLIST_LAST_DEVICE; 27408b8a9b1dSJustin T. Gibbs else 27418b8a9b1dSJustin T. Gibbs cgdl->status = CAM_GDEVLIST_MORE_DEVS; 27428b8a9b1dSJustin T. Gibbs 27438b8a9b1dSJustin T. Gibbs cgdl->index++; 27448b8a9b1dSJustin T. Gibbs cgdl->generation = device->generation; 27458b8a9b1dSJustin T. Gibbs 27468b8a9b1dSJustin T. Gibbs cgdl->ccb_h.status = CAM_REQ_CMP; 27478b8a9b1dSJustin T. Gibbs break; 27488b8a9b1dSJustin T. Gibbs } 27498b8a9b1dSJustin T. Gibbs case XPT_DEV_MATCH: 27508b8a9b1dSJustin T. Gibbs { 27518b8a9b1dSJustin T. Gibbs dev_pos_type position_type; 27528b8a9b1dSJustin T. Gibbs struct ccb_dev_match *cdm; 27538b8a9b1dSJustin T. Gibbs 27548b8a9b1dSJustin T. Gibbs cdm = &start_ccb->cdm; 27558b8a9b1dSJustin T. Gibbs 27568b8a9b1dSJustin T. Gibbs /* 27578b8a9b1dSJustin T. Gibbs * There are two ways of getting at information in the EDT. 27588b8a9b1dSJustin T. Gibbs * The first way is via the primary EDT tree. It starts 27598b8a9b1dSJustin T. Gibbs * with a list of busses, then a list of targets on a bus, 27608b8a9b1dSJustin T. Gibbs * then devices/luns on a target, and then peripherals on a 27618b8a9b1dSJustin T. Gibbs * device/lun. The "other" way is by the peripheral driver 27628b8a9b1dSJustin T. Gibbs * lists. The peripheral driver lists are organized by 27638b8a9b1dSJustin T. Gibbs * peripheral driver. (obviously) So it makes sense to 27648b8a9b1dSJustin T. Gibbs * use the peripheral driver list if the user is looking 27658b8a9b1dSJustin T. Gibbs * for something like "da1", or all "da" devices. If the 27668b8a9b1dSJustin T. Gibbs * user is looking for something on a particular bus/target 27678b8a9b1dSJustin T. Gibbs * or lun, it's generally better to go through the EDT tree. 27688b8a9b1dSJustin T. Gibbs */ 27698b8a9b1dSJustin T. Gibbs 27708b8a9b1dSJustin T. Gibbs if (cdm->pos.position_type != CAM_DEV_POS_NONE) 27718b8a9b1dSJustin T. Gibbs position_type = cdm->pos.position_type; 27728b8a9b1dSJustin T. Gibbs else { 27733393f8daSKenneth D. Merry u_int i; 27748b8a9b1dSJustin T. Gibbs 27758b8a9b1dSJustin T. Gibbs position_type = CAM_DEV_POS_NONE; 27768b8a9b1dSJustin T. Gibbs 27778b8a9b1dSJustin T. Gibbs for (i = 0; i < cdm->num_patterns; i++) { 27788b8a9b1dSJustin T. Gibbs if ((cdm->patterns[i].type == DEV_MATCH_BUS) 27798b8a9b1dSJustin T. Gibbs ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){ 27808b8a9b1dSJustin T. Gibbs position_type = CAM_DEV_POS_EDT; 27818b8a9b1dSJustin T. Gibbs break; 27828b8a9b1dSJustin T. Gibbs } 27838b8a9b1dSJustin T. Gibbs } 27848b8a9b1dSJustin T. Gibbs 27858b8a9b1dSJustin T. Gibbs if (cdm->num_patterns == 0) 27868b8a9b1dSJustin T. Gibbs position_type = CAM_DEV_POS_EDT; 27878b8a9b1dSJustin T. Gibbs else if (position_type == CAM_DEV_POS_NONE) 27888b8a9b1dSJustin T. Gibbs position_type = CAM_DEV_POS_PDRV; 27898b8a9b1dSJustin T. Gibbs } 27908b8a9b1dSJustin T. Gibbs 27918b8a9b1dSJustin T. Gibbs switch(position_type & CAM_DEV_POS_TYPEMASK) { 27928b8a9b1dSJustin T. Gibbs case CAM_DEV_POS_EDT: 279307c6eac9SPoul-Henning Kamp xptedtmatch(cdm); 27948b8a9b1dSJustin T. Gibbs break; 27958b8a9b1dSJustin T. Gibbs case CAM_DEV_POS_PDRV: 279607c6eac9SPoul-Henning Kamp xptperiphlistmatch(cdm); 27978b8a9b1dSJustin T. Gibbs break; 27988b8a9b1dSJustin T. Gibbs default: 27998b8a9b1dSJustin T. Gibbs cdm->status = CAM_DEV_MATCH_ERROR; 28008b8a9b1dSJustin T. Gibbs break; 28018b8a9b1dSJustin T. Gibbs } 28028b8a9b1dSJustin T. Gibbs 28038b8a9b1dSJustin T. Gibbs if (cdm->status == CAM_DEV_MATCH_ERROR) 28048b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 28058b8a9b1dSJustin T. Gibbs else 28068b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 28078b8a9b1dSJustin T. Gibbs 28088b8a9b1dSJustin T. Gibbs break; 28098b8a9b1dSJustin T. Gibbs } 28108b8a9b1dSJustin T. Gibbs case XPT_SASYNC_CB: 28118b8a9b1dSJustin T. Gibbs { 281284f82481SScott Long struct ccb_setasync *csa; 281384f82481SScott Long struct async_node *cur_entry; 281484f82481SScott Long struct async_list *async_head; 281584f82481SScott Long u_int32_t added; 281684f82481SScott Long 281784f82481SScott Long csa = &start_ccb->csa; 281884f82481SScott Long added = csa->event_enable; 2819da396db2SAlexander Motin async_head = &path->device->asyncs; 282084f82481SScott Long 282184f82481SScott Long /* 282284f82481SScott Long * If there is already an entry for us, simply 282384f82481SScott Long * update it. 282484f82481SScott Long */ 282584f82481SScott Long cur_entry = SLIST_FIRST(async_head); 282684f82481SScott Long while (cur_entry != NULL) { 282784f82481SScott Long if ((cur_entry->callback_arg == csa->callback_arg) 282884f82481SScott Long && (cur_entry->callback == csa->callback)) 282984f82481SScott Long break; 283084f82481SScott Long cur_entry = SLIST_NEXT(cur_entry, links); 283184f82481SScott Long } 283284f82481SScott Long 283384f82481SScott Long if (cur_entry != NULL) { 283484f82481SScott Long /* 283584f82481SScott Long * If the request has no flags set, 283684f82481SScott Long * remove the entry. 283784f82481SScott Long */ 283884f82481SScott Long added &= ~cur_entry->event_enable; 283984f82481SScott Long if (csa->event_enable == 0) { 284084f82481SScott Long SLIST_REMOVE(async_head, cur_entry, 284184f82481SScott Long async_node, links); 2842da396db2SAlexander Motin xpt_release_device(path->device); 284384f82481SScott Long free(cur_entry, M_CAMXPT); 284484f82481SScott Long } else { 284584f82481SScott Long cur_entry->event_enable = csa->event_enable; 284684f82481SScott Long } 28477685edecSAlexander Motin csa->event_enable = added; 284884f82481SScott Long } else { 284984f82481SScott Long cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT, 285084f82481SScott Long M_NOWAIT); 285184f82481SScott Long if (cur_entry == NULL) { 285284f82481SScott Long csa->ccb_h.status = CAM_RESRC_UNAVAIL; 285384f82481SScott Long break; 285484f82481SScott Long } 285584f82481SScott Long cur_entry->event_enable = csa->event_enable; 2856227d67aaSAlexander Motin cur_entry->event_lock = 2857227d67aaSAlexander Motin mtx_owned(path->bus->sim->mtx) ? 1 : 0; 285884f82481SScott Long cur_entry->callback_arg = csa->callback_arg; 285984f82481SScott Long cur_entry->callback = csa->callback; 286084f82481SScott Long SLIST_INSERT_HEAD(async_head, cur_entry, links); 2861da396db2SAlexander Motin xpt_acquire_device(path->device); 286284f82481SScott Long } 28638b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 28648b8a9b1dSJustin T. Gibbs break; 28658b8a9b1dSJustin T. Gibbs } 28668b8a9b1dSJustin T. Gibbs case XPT_REL_SIMQ: 28678b8a9b1dSJustin T. Gibbs { 28688b8a9b1dSJustin T. Gibbs struct ccb_relsim *crs; 28698b8a9b1dSJustin T. Gibbs struct cam_ed *dev; 28708b8a9b1dSJustin T. Gibbs 28718b8a9b1dSJustin T. Gibbs crs = &start_ccb->crs; 2872da396db2SAlexander Motin dev = path->device; 28738b8a9b1dSJustin T. Gibbs if (dev == NULL) { 28748b8a9b1dSJustin T. Gibbs 28758b8a9b1dSJustin T. Gibbs crs->ccb_h.status = CAM_DEV_NOT_THERE; 28768b8a9b1dSJustin T. Gibbs break; 28778b8a9b1dSJustin T. Gibbs } 28788b8a9b1dSJustin T. Gibbs 28798b8a9b1dSJustin T. Gibbs if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) { 28808b8a9b1dSJustin T. Gibbs 28818b8a9b1dSJustin T. Gibbs /* Don't ever go below one opening */ 28828b8a9b1dSJustin T. Gibbs if (crs->openings > 0) { 28837dc3213dSAlexander Motin xpt_dev_ccbq_resize(path, crs->openings); 288479ccc199SJordan K. Hubbard if (bootverbose) { 2885da396db2SAlexander Motin xpt_print(path, 28867dc3213dSAlexander Motin "number of openings is now %d\n", 28878b8a9b1dSJustin T. Gibbs crs->openings); 28888b8a9b1dSJustin T. Gibbs } 28898b8a9b1dSJustin T. Gibbs } 28908b8a9b1dSJustin T. Gibbs } 28918b8a9b1dSJustin T. Gibbs 2892227d67aaSAlexander Motin mtx_lock(&dev->sim->devq->send_mtx); 28938b8a9b1dSJustin T. Gibbs if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) { 28948b8a9b1dSJustin T. Gibbs 28958b8a9b1dSJustin T. Gibbs if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 28968b8a9b1dSJustin T. Gibbs 28978b8a9b1dSJustin T. Gibbs /* 28988b8a9b1dSJustin T. Gibbs * Just extend the old timeout and decrement 28998b8a9b1dSJustin T. Gibbs * the freeze count so that a single timeout 29008b8a9b1dSJustin T. Gibbs * is sufficient for releasing the queue. 29018b8a9b1dSJustin T. Gibbs */ 29028b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 29032b83592fSScott Long callout_stop(&dev->callout); 29048b8a9b1dSJustin T. Gibbs } else { 29058b8a9b1dSJustin T. Gibbs 29068b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 29078b8a9b1dSJustin T. Gibbs } 29088b8a9b1dSJustin T. Gibbs 290985c9dd9dSSteven Hartland callout_reset_sbt(&dev->callout, 291085c9dd9dSSteven Hartland SBT_1MS * crs->release_timeout, 0, 291185c9dd9dSSteven Hartland xpt_release_devq_timeout, dev, 0); 29128b8a9b1dSJustin T. Gibbs 29138b8a9b1dSJustin T. Gibbs dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING; 29148b8a9b1dSJustin T. Gibbs 29158b8a9b1dSJustin T. Gibbs } 29168b8a9b1dSJustin T. Gibbs 29178b8a9b1dSJustin T. Gibbs if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) { 29188b8a9b1dSJustin T. Gibbs 29198b8a9b1dSJustin T. Gibbs if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) { 29208b8a9b1dSJustin T. Gibbs /* 29218b8a9b1dSJustin T. Gibbs * Decrement the freeze count so that a single 29228b8a9b1dSJustin T. Gibbs * completion is still sufficient to unfreeze 29238b8a9b1dSJustin T. Gibbs * the queue. 29248b8a9b1dSJustin T. Gibbs */ 29258b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 29268b8a9b1dSJustin T. Gibbs } else { 29278b8a9b1dSJustin T. Gibbs 29288b8a9b1dSJustin T. Gibbs dev->flags |= CAM_DEV_REL_ON_COMPLETE; 29298b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 29308b8a9b1dSJustin T. Gibbs } 29318b8a9b1dSJustin T. Gibbs } 29328b8a9b1dSJustin T. Gibbs 29338b8a9b1dSJustin T. Gibbs if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) { 29348b8a9b1dSJustin T. Gibbs 29358b8a9b1dSJustin T. Gibbs if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 29368b8a9b1dSJustin T. Gibbs || (dev->ccbq.dev_active == 0)) { 29378b8a9b1dSJustin T. Gibbs 29388b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 29398b8a9b1dSJustin T. Gibbs } else { 29408b8a9b1dSJustin T. Gibbs 29418b8a9b1dSJustin T. Gibbs dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY; 29428b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 29438b8a9b1dSJustin T. Gibbs } 29448b8a9b1dSJustin T. Gibbs } 2945227d67aaSAlexander Motin mtx_unlock(&dev->sim->devq->send_mtx); 29468b8a9b1dSJustin T. Gibbs 2947cccf4220SAlexander Motin if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) 2948cccf4220SAlexander Motin xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE); 2949cccf4220SAlexander Motin start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt; 29508b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 29518b8a9b1dSJustin T. Gibbs break; 29528b8a9b1dSJustin T. Gibbs } 29538b8a9b1dSJustin T. Gibbs case XPT_DEBUG: { 295480d6987cSAlexander Motin struct cam_path *oldpath; 295580d6987cSAlexander Motin 2956f0f25b9cSAlexander Motin /* Check that all request bits are supported. */ 295722c7d606SAlexander Motin if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) { 2958f0f25b9cSAlexander Motin start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2959f0f25b9cSAlexander Motin break; 2960f0f25b9cSAlexander Motin } 2961f0f25b9cSAlexander Motin 296280d6987cSAlexander Motin cam_dflags = CAM_DEBUG_NONE; 29638b8a9b1dSJustin T. Gibbs if (cam_dpath != NULL) { 296480d6987cSAlexander Motin oldpath = cam_dpath; 29658b8a9b1dSJustin T. Gibbs cam_dpath = NULL; 296680d6987cSAlexander Motin xpt_free_path(oldpath); 29678b8a9b1dSJustin T. Gibbs } 296880d6987cSAlexander Motin if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) { 2969e5dfa058SAlexander Motin if (xpt_create_path(&cam_dpath, NULL, 29708b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.path_id, 29718b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.target_id, 29728b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.target_lun) != 29738b8a9b1dSJustin T. Gibbs CAM_REQ_CMP) { 29748b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2975aa872be6SMatt Jacob } else { 297680d6987cSAlexander Motin cam_dflags = start_ccb->cdbg.flags; 29778b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 2978f0d9af51SMatt Jacob xpt_print(cam_dpath, "debugging flags now %x\n", 2979f0d9af51SMatt Jacob cam_dflags); 2980aa872be6SMatt Jacob } 298180d6987cSAlexander Motin } else 29828b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 29838b8a9b1dSJustin T. Gibbs break; 29848b8a9b1dSJustin T. Gibbs } 29858b8a9b1dSJustin T. Gibbs case XPT_NOOP: 298687cfaf0eSJustin T. Gibbs if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) 2987da396db2SAlexander Motin xpt_freeze_devq(path, 1); 29888b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_REQ_CMP; 29898b8a9b1dSJustin T. Gibbs break; 29908b8a9b1dSJustin T. Gibbs default: 29918b8a9b1dSJustin T. Gibbs case XPT_SDEV_TYPE: 29928b8a9b1dSJustin T. Gibbs case XPT_TERM_IO: 29938b8a9b1dSJustin T. Gibbs case XPT_ENG_INQ: 29948b8a9b1dSJustin T. Gibbs /* XXX Implement */ 29953501942bSJustin T. Gibbs printf("%s: CCB type %#x not supported\n", __func__, 29963501942bSJustin T. Gibbs start_ccb->ccb_h.func_code); 29978b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_PROVIDE_FAIL; 2998b882a6d3SMatt Jacob if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) { 2999b882a6d3SMatt Jacob xpt_done(start_ccb); 3000b882a6d3SMatt Jacob } 30018b8a9b1dSJustin T. Gibbs break; 30028b8a9b1dSJustin T. Gibbs } 30038b8a9b1dSJustin T. Gibbs } 30048b8a9b1dSJustin T. Gibbs 30058b8a9b1dSJustin T. Gibbs void 30068b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb) 30078b8a9b1dSJustin T. Gibbs { 30088b8a9b1dSJustin T. Gibbs u_int32_t timeout; 30098b8a9b1dSJustin T. Gibbs struct cam_sim *sim; 30108b8a9b1dSJustin T. Gibbs struct cam_devq *devq; 30118b8a9b1dSJustin T. Gibbs struct cam_ed *dev; 30128b8a9b1dSJustin T. Gibbs 30130069293bSAlexander Motin timeout = start_ccb->ccb_h.timeout * 10; 30148b8a9b1dSJustin T. Gibbs sim = start_ccb->ccb_h.path->bus->sim; 30158b8a9b1dSJustin T. Gibbs devq = sim->devq; 30168b8a9b1dSJustin T. Gibbs dev = start_ccb->ccb_h.path->device; 30178b8a9b1dSJustin T. Gibbs 3018227d67aaSAlexander Motin mtx_unlock(&dev->device_mtx); 30194a612489SAlexander Motin 30208b8a9b1dSJustin T. Gibbs /* 30218b8a9b1dSJustin T. Gibbs * Steal an opening so that no other queued requests 30228b8a9b1dSJustin T. Gibbs * can get it before us while we simulate interrupts. 30238b8a9b1dSJustin T. Gibbs */ 3024227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 30258b8a9b1dSJustin T. Gibbs dev->ccbq.dev_openings--; 3026227d67aaSAlexander Motin while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) && 3027227d67aaSAlexander Motin (--timeout > 0)) { 3028227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 30290069293bSAlexander Motin DELAY(100); 3030227d67aaSAlexander Motin CAM_SIM_LOCK(sim); 30318b8a9b1dSJustin T. Gibbs (*(sim->sim_poll))(sim); 3032227d67aaSAlexander Motin CAM_SIM_UNLOCK(sim); 3033227d67aaSAlexander Motin camisr_runqueue(); 3034227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 30358b8a9b1dSJustin T. Gibbs } 30368b8a9b1dSJustin T. Gibbs dev->ccbq.dev_openings++; 3037227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 30388b8a9b1dSJustin T. Gibbs 30398b8a9b1dSJustin T. Gibbs if (timeout != 0) { 30408b8a9b1dSJustin T. Gibbs xpt_action(start_ccb); 30418b8a9b1dSJustin T. Gibbs while(--timeout > 0) { 3042227d67aaSAlexander Motin CAM_SIM_LOCK(sim); 30438b8a9b1dSJustin T. Gibbs (*(sim->sim_poll))(sim); 3044227d67aaSAlexander Motin CAM_SIM_UNLOCK(sim); 3045227d67aaSAlexander Motin camisr_runqueue(); 30468b8a9b1dSJustin T. Gibbs if ((start_ccb->ccb_h.status & CAM_STATUS_MASK) 30478b8a9b1dSJustin T. Gibbs != CAM_REQ_INPROG) 30488b8a9b1dSJustin T. Gibbs break; 30490069293bSAlexander Motin DELAY(100); 30508b8a9b1dSJustin T. Gibbs } 30518b8a9b1dSJustin T. Gibbs if (timeout == 0) { 30528b8a9b1dSJustin T. Gibbs /* 30538b8a9b1dSJustin T. Gibbs * XXX Is it worth adding a sim_timeout entry 30548b8a9b1dSJustin T. Gibbs * point so we can attempt recovery? If 30558b8a9b1dSJustin T. Gibbs * this is only used for dumps, I don't think 30568b8a9b1dSJustin T. Gibbs * it is. 30578b8a9b1dSJustin T. Gibbs */ 30588b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_CMD_TIMEOUT; 30598b8a9b1dSJustin T. Gibbs } 30608b8a9b1dSJustin T. Gibbs } else { 30618b8a9b1dSJustin T. Gibbs start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 30628b8a9b1dSJustin T. Gibbs } 30634a612489SAlexander Motin 3064227d67aaSAlexander Motin mtx_lock(&dev->device_mtx); 30658b8a9b1dSJustin T. Gibbs } 30668b8a9b1dSJustin T. Gibbs 30678b8a9b1dSJustin T. Gibbs /* 306821cffce5SBryan Drewery * Schedule a peripheral driver to receive a ccb when its 30698b8a9b1dSJustin T. Gibbs * target device has space for more transactions. 30708b8a9b1dSJustin T. Gibbs */ 30718b8a9b1dSJustin T. Gibbs void 3072227d67aaSAlexander Motin xpt_schedule(struct cam_periph *periph, u_int32_t new_priority) 30738b8a9b1dSJustin T. Gibbs { 30748b8a9b1dSJustin T. Gibbs 3075227d67aaSAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n")); 3076227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 3077227d67aaSAlexander Motin if (new_priority < periph->scheduled_priority) { 3078227d67aaSAlexander Motin periph->scheduled_priority = new_priority; 3079227d67aaSAlexander Motin xpt_run_allocq(periph, 0); 30808b8a9b1dSJustin T. Gibbs } 30818b8a9b1dSJustin T. Gibbs } 30828b8a9b1dSJustin T. Gibbs 30838b8a9b1dSJustin T. Gibbs 30848b8a9b1dSJustin T. Gibbs /* 30858b8a9b1dSJustin T. Gibbs * Schedule a device to run on a given queue. 30868b8a9b1dSJustin T. Gibbs * If the device was inserted as a new entry on the queue, 30878b8a9b1dSJustin T. Gibbs * return 1 meaning the device queue should be run. If we 30888b8a9b1dSJustin T. Gibbs * were already queued, implying someone else has already 30898b8a9b1dSJustin T. Gibbs * started the queue, return 0 so the caller doesn't attempt 309077dc25ccSScott Long * to run the queue. 30918b8a9b1dSJustin T. Gibbs */ 3092227d67aaSAlexander Motin static int 30938b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo, 30948b8a9b1dSJustin T. Gibbs u_int32_t new_priority) 30958b8a9b1dSJustin T. Gibbs { 30968b8a9b1dSJustin T. Gibbs int retval; 30978b8a9b1dSJustin T. Gibbs u_int32_t old_priority; 30988b8a9b1dSJustin T. Gibbs 3099aa872be6SMatt Jacob CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n")); 31008b8a9b1dSJustin T. Gibbs 31018b8a9b1dSJustin T. Gibbs old_priority = pinfo->priority; 31028b8a9b1dSJustin T. Gibbs 31038b8a9b1dSJustin T. Gibbs /* 31048b8a9b1dSJustin T. Gibbs * Are we already queued? 31058b8a9b1dSJustin T. Gibbs */ 31068b8a9b1dSJustin T. Gibbs if (pinfo->index != CAM_UNQUEUED_INDEX) { 31078b8a9b1dSJustin T. Gibbs /* Simply reorder based on new priority */ 31088b8a9b1dSJustin T. Gibbs if (new_priority < old_priority) { 31098b8a9b1dSJustin T. Gibbs camq_change_priority(queue, pinfo->index, 31108b8a9b1dSJustin T. Gibbs new_priority); 3111aa872be6SMatt Jacob CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 31128b8a9b1dSJustin T. Gibbs ("changed priority to %d\n", 31138b8a9b1dSJustin T. Gibbs new_priority)); 311483c5d981SAlexander Motin retval = 1; 311583c5d981SAlexander Motin } else 31168b8a9b1dSJustin T. Gibbs retval = 0; 31178b8a9b1dSJustin T. Gibbs } else { 31188b8a9b1dSJustin T. Gibbs /* New entry on the queue */ 31198b8a9b1dSJustin T. Gibbs if (new_priority < old_priority) 31208b8a9b1dSJustin T. Gibbs pinfo->priority = new_priority; 31218b8a9b1dSJustin T. Gibbs 3122aa872be6SMatt Jacob CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 31238b8a9b1dSJustin T. Gibbs ("Inserting onto queue\n")); 31248bad620dSJustin T. Gibbs pinfo->generation = ++queue->generation; 31258b8a9b1dSJustin T. Gibbs camq_insert(queue, pinfo); 31268b8a9b1dSJustin T. Gibbs retval = 1; 31278b8a9b1dSJustin T. Gibbs } 31288b8a9b1dSJustin T. Gibbs return (retval); 31298b8a9b1dSJustin T. Gibbs } 31308b8a9b1dSJustin T. Gibbs 31318b8a9b1dSJustin T. Gibbs static void 3132227d67aaSAlexander Motin xpt_run_allocq_task(void *context, int pending) 31338b8a9b1dSJustin T. Gibbs { 3134227d67aaSAlexander Motin struct cam_periph *periph = context; 31358b8a9b1dSJustin T. Gibbs 3136227d67aaSAlexander Motin cam_periph_lock(periph); 3137227d67aaSAlexander Motin periph->flags &= ~CAM_PERIPH_RUN_TASK; 3138227d67aaSAlexander Motin xpt_run_allocq(periph, 1); 3139227d67aaSAlexander Motin cam_periph_unlock(periph); 3140227d67aaSAlexander Motin cam_periph_release(periph); 3141227d67aaSAlexander Motin } 3142227d67aaSAlexander Motin 3143227d67aaSAlexander Motin static void 3144227d67aaSAlexander Motin xpt_run_allocq(struct cam_periph *periph, int sleep) 3145227d67aaSAlexander Motin { 3146227d67aaSAlexander Motin struct cam_ed *device; 3147227d67aaSAlexander Motin union ccb *ccb; 3148227d67aaSAlexander Motin uint32_t prio; 3149227d67aaSAlexander Motin 3150227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 3151227d67aaSAlexander Motin if (periph->periph_allocating) 3152cccf4220SAlexander Motin return; 3153227d67aaSAlexander Motin periph->periph_allocating = 1; 3154227d67aaSAlexander Motin CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph)); 3155227d67aaSAlexander Motin device = periph->path->device; 3156227d67aaSAlexander Motin ccb = NULL; 3157227d67aaSAlexander Motin restart: 3158227d67aaSAlexander Motin while ((prio = min(periph->scheduled_priority, 3159227d67aaSAlexander Motin periph->immediate_priority)) != CAM_PRIORITY_NONE && 3160227d67aaSAlexander Motin (periph->periph_allocated - (ccb != NULL ? 1 : 0) < 3161227d67aaSAlexander Motin device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) { 3162cccf4220SAlexander Motin 3163227d67aaSAlexander Motin if (ccb == NULL && 3164227d67aaSAlexander Motin (ccb = xpt_get_ccb_nowait(periph)) == NULL) { 3165227d67aaSAlexander Motin if (sleep) { 3166227d67aaSAlexander Motin ccb = xpt_get_ccb(periph); 3167227d67aaSAlexander Motin goto restart; 3168227d67aaSAlexander Motin } 31698ec5ab3fSAlexander Motin if (periph->flags & CAM_PERIPH_RUN_TASK) 31708b8a9b1dSJustin T. Gibbs break; 3171c33e4029SAlexander Motin cam_periph_doacquire(periph); 3172227d67aaSAlexander Motin periph->flags |= CAM_PERIPH_RUN_TASK; 3173227d67aaSAlexander Motin taskqueue_enqueue(xsoftc.xpt_taskq, 3174227d67aaSAlexander Motin &periph->periph_run_task); 3175227d67aaSAlexander Motin break; 31768b8a9b1dSJustin T. Gibbs } 3177227d67aaSAlexander Motin xpt_setup_ccb(&ccb->ccb_h, periph->path, prio); 3178227d67aaSAlexander Motin if (prio == periph->immediate_priority) { 3179227d67aaSAlexander Motin periph->immediate_priority = CAM_PRIORITY_NONE; 3180227d67aaSAlexander Motin CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3181227d67aaSAlexander Motin ("waking cam_periph_getccb()\n")); 3182227d67aaSAlexander Motin SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h, 3183227d67aaSAlexander Motin periph_links.sle); 3184227d67aaSAlexander Motin wakeup(&periph->ccb_list); 3185227d67aaSAlexander Motin } else { 3186227d67aaSAlexander Motin periph->scheduled_priority = CAM_PRIORITY_NONE; 3187227d67aaSAlexander Motin CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3188227d67aaSAlexander Motin ("calling periph_start()\n")); 3189227d67aaSAlexander Motin periph->periph_start(periph, ccb); 3190227d67aaSAlexander Motin } 3191227d67aaSAlexander Motin ccb = NULL; 3192227d67aaSAlexander Motin } 3193227d67aaSAlexander Motin if (ccb != NULL) 3194227d67aaSAlexander Motin xpt_release_ccb(ccb); 3195227d67aaSAlexander Motin periph->periph_allocating = 0; 31968b8a9b1dSJustin T. Gibbs } 31978b8a9b1dSJustin T. Gibbs 319883c5d981SAlexander Motin static void 3199cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq) 32008b8a9b1dSJustin T. Gibbs { 3201de9ebb68SAlexander Motin char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 3202227d67aaSAlexander Motin int lock; 32038b8a9b1dSJustin T. Gibbs 3204cccf4220SAlexander Motin CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n")); 32058b8a9b1dSJustin T. Gibbs 3206cccf4220SAlexander Motin devq->send_queue.qfrozen_cnt++; 32078b8a9b1dSJustin T. Gibbs while ((devq->send_queue.entries > 0) 3208ec700f26SAlexander Motin && (devq->send_openings > 0) 3209cccf4220SAlexander Motin && (devq->send_queue.qfrozen_cnt <= 1)) { 32108b8a9b1dSJustin T. Gibbs struct cam_ed *device; 32118b8a9b1dSJustin T. Gibbs union ccb *work_ccb; 32128b8a9b1dSJustin T. Gibbs struct cam_sim *sim; 32138b8a9b1dSJustin T. Gibbs 3214227d67aaSAlexander Motin device = (struct cam_ed *)camq_remove(&devq->send_queue, 32155a526431SJustin T. Gibbs CAMQ_HEAD); 3216aa872be6SMatt Jacob CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 321750642f18SKenneth D. Merry ("running device %p\n", device)); 32188b8a9b1dSJustin T. Gibbs 32195a526431SJustin T. Gibbs work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 32208b8a9b1dSJustin T. Gibbs if (work_ccb == NULL) { 322157b89bbcSNate Lawson printf("device on run queue with no ccbs???\n"); 32228b8a9b1dSJustin T. Gibbs continue; 32238b8a9b1dSJustin T. Gibbs } 32248b8a9b1dSJustin T. Gibbs 32258b8a9b1dSJustin T. Gibbs if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) { 32268b8a9b1dSJustin T. Gibbs 3227daa5487fSAlexander Motin mtx_lock(&xsoftc.xpt_highpower_lock); 32282b83592fSScott Long if (xsoftc.num_highpower <= 0) { 32298b8a9b1dSJustin T. Gibbs /* 32308b8a9b1dSJustin T. Gibbs * We got a high power command, but we 32318b8a9b1dSJustin T. Gibbs * don't have any available slots. Freeze 32328b8a9b1dSJustin T. Gibbs * the device queue until we have a slot 32338b8a9b1dSJustin T. Gibbs * available. 32348b8a9b1dSJustin T. Gibbs */ 3235daa5487fSAlexander Motin xpt_freeze_devq_device(device, 1); 3236227d67aaSAlexander Motin STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device, 3237ea541bfdSAlexander Motin highpowerq_entry); 32388b8a9b1dSJustin T. Gibbs 3239daa5487fSAlexander Motin mtx_unlock(&xsoftc.xpt_highpower_lock); 32408b8a9b1dSJustin T. Gibbs continue; 32418b8a9b1dSJustin T. Gibbs } else { 32428b8a9b1dSJustin T. Gibbs /* 32438b8a9b1dSJustin T. Gibbs * Consume a high power slot while 32448b8a9b1dSJustin T. Gibbs * this ccb runs. 32458b8a9b1dSJustin T. Gibbs */ 32462b83592fSScott Long xsoftc.num_highpower--; 32478b8a9b1dSJustin T. Gibbs } 3248daa5487fSAlexander Motin mtx_unlock(&xsoftc.xpt_highpower_lock); 32498b8a9b1dSJustin T. Gibbs } 32508b8a9b1dSJustin T. Gibbs cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 32518b8a9b1dSJustin T. Gibbs cam_ccbq_send_ccb(&device->ccbq, work_ccb); 32528b8a9b1dSJustin T. Gibbs devq->send_openings--; 32538b8a9b1dSJustin T. Gibbs devq->send_active++; 3254cccf4220SAlexander Motin xpt_schedule_devq(devq, device); 3255227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 32568b8a9b1dSJustin T. Gibbs 3257cccf4220SAlexander Motin if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) { 32588b8a9b1dSJustin T. Gibbs /* 32598b8a9b1dSJustin T. Gibbs * The client wants to freeze the queue 32608b8a9b1dSJustin T. Gibbs * after this CCB is sent. 32618b8a9b1dSJustin T. Gibbs */ 326283c5d981SAlexander Motin xpt_freeze_devq(work_ccb->ccb_h.path, 1); 32638b8a9b1dSJustin T. Gibbs } 32648b8a9b1dSJustin T. Gibbs 3265a4eb4f16SMatt Jacob /* In Target mode, the peripheral driver knows best... */ 3266a4eb4f16SMatt Jacob if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) { 3267a4eb4f16SMatt Jacob if ((device->inq_flags & SID_CmdQue) != 0 3268a4eb4f16SMatt Jacob && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE) 32698b8a9b1dSJustin T. Gibbs work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID; 32708b8a9b1dSJustin T. Gibbs else 32718b8a9b1dSJustin T. Gibbs /* 3272a4eb4f16SMatt Jacob * Clear this in case of a retried CCB that 3273a4eb4f16SMatt Jacob * failed due to a rejected tag. 32748b8a9b1dSJustin T. Gibbs */ 32758b8a9b1dSJustin T. Gibbs work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 3276a4eb4f16SMatt Jacob } 32778b8a9b1dSJustin T. Gibbs 3278de9ebb68SAlexander Motin switch (work_ccb->ccb_h.func_code) { 3279de9ebb68SAlexander Motin case XPT_SCSI_IO: 3280de9ebb68SAlexander Motin CAM_DEBUG(work_ccb->ccb_h.path, 3281de9ebb68SAlexander Motin CAM_DEBUG_CDB,("%s. CDB: %s\n", 3282de9ebb68SAlexander Motin scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0], 3283de9ebb68SAlexander Motin &device->inq_data), 3284de9ebb68SAlexander Motin scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes, 3285de9ebb68SAlexander Motin cdb_str, sizeof(cdb_str)))); 3286de9ebb68SAlexander Motin break; 3287de9ebb68SAlexander Motin case XPT_ATA_IO: 3288de9ebb68SAlexander Motin CAM_DEBUG(work_ccb->ccb_h.path, 3289de9ebb68SAlexander Motin CAM_DEBUG_CDB,("%s. ACB: %s\n", 3290de9ebb68SAlexander Motin ata_op_string(&work_ccb->ataio.cmd), 3291de9ebb68SAlexander Motin ata_cmd_string(&work_ccb->ataio.cmd, 3292de9ebb68SAlexander Motin cdb_str, sizeof(cdb_str)))); 3293de9ebb68SAlexander Motin break; 3294de9ebb68SAlexander Motin default: 3295de9ebb68SAlexander Motin break; 3296de9ebb68SAlexander Motin } 3297de9ebb68SAlexander Motin 32988b8a9b1dSJustin T. Gibbs /* 3299227d67aaSAlexander Motin * Device queues can be shared among multiple SIM instances 3300227d67aaSAlexander Motin * that reside on different busses. Use the SIM from the 3301227d67aaSAlexander Motin * queued device, rather than the one from the calling bus. 33028b8a9b1dSJustin T. Gibbs */ 3303227d67aaSAlexander Motin sim = device->sim; 3304227d67aaSAlexander Motin lock = (mtx_owned(sim->mtx) == 0); 3305227d67aaSAlexander Motin if (lock) 3306227d67aaSAlexander Motin CAM_SIM_LOCK(sim); 33078b8a9b1dSJustin T. Gibbs (*(sim->sim_action))(sim, work_ccb); 3308227d67aaSAlexander Motin if (lock) 3309227d67aaSAlexander Motin CAM_SIM_UNLOCK(sim); 3310227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 33118b8a9b1dSJustin T. Gibbs } 3312cccf4220SAlexander Motin devq->send_queue.qfrozen_cnt--; 33138b8a9b1dSJustin T. Gibbs } 33148b8a9b1dSJustin T. Gibbs 33158b8a9b1dSJustin T. Gibbs /* 33168b8a9b1dSJustin T. Gibbs * This function merges stuff from the slave ccb into the master ccb, while 33178b8a9b1dSJustin T. Gibbs * keeping important fields in the master ccb constant. 33188b8a9b1dSJustin T. Gibbs */ 33198b8a9b1dSJustin T. Gibbs void 33208b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb) 33218b8a9b1dSJustin T. Gibbs { 332268153f43SScott Long 33238b8a9b1dSJustin T. Gibbs /* 33248b8a9b1dSJustin T. Gibbs * Pull fields that are valid for peripheral drivers to set 33258b8a9b1dSJustin T. Gibbs * into the master CCB along with the CCB "payload". 33268b8a9b1dSJustin T. Gibbs */ 33278b8a9b1dSJustin T. Gibbs master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count; 33288b8a9b1dSJustin T. Gibbs master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code; 33298b8a9b1dSJustin T. Gibbs master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout; 33308b8a9b1dSJustin T. Gibbs master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags; 33318b8a9b1dSJustin T. Gibbs bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1], 33328b8a9b1dSJustin T. Gibbs sizeof(union ccb) - sizeof(struct ccb_hdr)); 33338b8a9b1dSJustin T. Gibbs } 33348b8a9b1dSJustin T. Gibbs 33358b8a9b1dSJustin T. Gibbs void 33368b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority) 33378b8a9b1dSJustin T. Gibbs { 333868153f43SScott Long 33398b8a9b1dSJustin T. Gibbs CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n")); 33408b8a9b1dSJustin T. Gibbs ccb_h->pinfo.priority = priority; 33418b8a9b1dSJustin T. Gibbs ccb_h->path = path; 33428b8a9b1dSJustin T. Gibbs ccb_h->path_id = path->bus->path_id; 33438b8a9b1dSJustin T. Gibbs if (path->target) 33448b8a9b1dSJustin T. Gibbs ccb_h->target_id = path->target->target_id; 33458b8a9b1dSJustin T. Gibbs else 33468b8a9b1dSJustin T. Gibbs ccb_h->target_id = CAM_TARGET_WILDCARD; 33478b8a9b1dSJustin T. Gibbs if (path->device) { 33488b8a9b1dSJustin T. Gibbs ccb_h->target_lun = path->device->lun_id; 33498bad620dSJustin T. Gibbs ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation; 33508b8a9b1dSJustin T. Gibbs } else { 33518b8a9b1dSJustin T. Gibbs ccb_h->target_lun = CAM_TARGET_WILDCARD; 33528b8a9b1dSJustin T. Gibbs } 33538b8a9b1dSJustin T. Gibbs ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 33548b8a9b1dSJustin T. Gibbs ccb_h->flags = 0; 3355b5595753SNathan Whitehorn ccb_h->xflags = 0; 33568b8a9b1dSJustin T. Gibbs } 33578b8a9b1dSJustin T. Gibbs 33588b8a9b1dSJustin T. Gibbs /* Path manipulation functions */ 33598b8a9b1dSJustin T. Gibbs cam_status 33608b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph, 33618b8a9b1dSJustin T. Gibbs path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 33628b8a9b1dSJustin T. Gibbs { 33638b8a9b1dSJustin T. Gibbs struct cam_path *path; 33648b8a9b1dSJustin T. Gibbs cam_status status; 33658b8a9b1dSJustin T. Gibbs 3366596ee08fSAlexander Motin path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT); 33678b8a9b1dSJustin T. Gibbs 33688b8a9b1dSJustin T. Gibbs if (path == NULL) { 33698b8a9b1dSJustin T. Gibbs status = CAM_RESRC_UNAVAIL; 33708b8a9b1dSJustin T. Gibbs return(status); 33718b8a9b1dSJustin T. Gibbs } 33728b8a9b1dSJustin T. Gibbs status = xpt_compile_path(path, perph, path_id, target_id, lun_id); 33738b8a9b1dSJustin T. Gibbs if (status != CAM_REQ_CMP) { 3374596ee08fSAlexander Motin free(path, M_CAMPATH); 33758b8a9b1dSJustin T. Gibbs path = NULL; 33768b8a9b1dSJustin T. Gibbs } 33778b8a9b1dSJustin T. Gibbs *new_path_ptr = path; 33788b8a9b1dSJustin T. Gibbs return (status); 33798b8a9b1dSJustin T. Gibbs } 33808b8a9b1dSJustin T. Gibbs 33812b83592fSScott Long cam_status 33822b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr, 33832b83592fSScott Long struct cam_periph *periph, path_id_t path_id, 33842b83592fSScott Long target_id_t target_id, lun_id_t lun_id) 33852b83592fSScott Long { 33862b83592fSScott Long 3387227d67aaSAlexander Motin return (xpt_create_path(new_path_ptr, periph, path_id, target_id, 3388227d67aaSAlexander Motin lun_id)); 33892b83592fSScott Long } 33902b83592fSScott Long 339152c9ce25SScott Long cam_status 33928b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph, 33938b8a9b1dSJustin T. Gibbs path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 33948b8a9b1dSJustin T. Gibbs { 33958b8a9b1dSJustin T. Gibbs struct cam_eb *bus; 33968b8a9b1dSJustin T. Gibbs struct cam_et *target; 33978b8a9b1dSJustin T. Gibbs struct cam_ed *device; 33988b8a9b1dSJustin T. Gibbs cam_status status; 33998b8a9b1dSJustin T. Gibbs 34008b8a9b1dSJustin T. Gibbs status = CAM_REQ_CMP; /* Completed without error */ 34018b8a9b1dSJustin T. Gibbs target = NULL; /* Wildcarded */ 34028b8a9b1dSJustin T. Gibbs device = NULL; /* Wildcarded */ 3403a5479bc5SJustin T. Gibbs 3404a5479bc5SJustin T. Gibbs /* 3405a5479bc5SJustin T. Gibbs * We will potentially modify the EDT, so block interrupts 3406a5479bc5SJustin T. Gibbs * that may attempt to create cam paths. 3407a5479bc5SJustin T. Gibbs */ 34088b8a9b1dSJustin T. Gibbs bus = xpt_find_bus(path_id); 34098b8a9b1dSJustin T. Gibbs if (bus == NULL) { 34108b8a9b1dSJustin T. Gibbs status = CAM_PATH_INVALID; 3411c8bead2aSJustin T. Gibbs } else { 3412227d67aaSAlexander Motin xpt_lock_buses(); 3413227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 34148b8a9b1dSJustin T. Gibbs target = xpt_find_target(bus, target_id); 34158b8a9b1dSJustin T. Gibbs if (target == NULL) { 34168b8a9b1dSJustin T. Gibbs /* Create one */ 34178b8a9b1dSJustin T. Gibbs struct cam_et *new_target; 34188b8a9b1dSJustin T. Gibbs 34198b8a9b1dSJustin T. Gibbs new_target = xpt_alloc_target(bus, target_id); 34208b8a9b1dSJustin T. Gibbs if (new_target == NULL) { 34218b8a9b1dSJustin T. Gibbs status = CAM_RESRC_UNAVAIL; 34228b8a9b1dSJustin T. Gibbs } else { 34238b8a9b1dSJustin T. Gibbs target = new_target; 34248b8a9b1dSJustin T. Gibbs } 34258b8a9b1dSJustin T. Gibbs } 3426227d67aaSAlexander Motin xpt_unlock_buses(); 3427c8bead2aSJustin T. Gibbs if (target != NULL) { 34288b8a9b1dSJustin T. Gibbs device = xpt_find_device(target, lun_id); 34298b8a9b1dSJustin T. Gibbs if (device == NULL) { 34308b8a9b1dSJustin T. Gibbs /* Create one */ 34318b8a9b1dSJustin T. Gibbs struct cam_ed *new_device; 34328b8a9b1dSJustin T. Gibbs 343352c9ce25SScott Long new_device = 343452c9ce25SScott Long (*(bus->xport->alloc_device))(bus, 34358b8a9b1dSJustin T. Gibbs target, 34368b8a9b1dSJustin T. Gibbs lun_id); 34378b8a9b1dSJustin T. Gibbs if (new_device == NULL) { 34388b8a9b1dSJustin T. Gibbs status = CAM_RESRC_UNAVAIL; 34398b8a9b1dSJustin T. Gibbs } else { 34408b8a9b1dSJustin T. Gibbs device = new_device; 34418b8a9b1dSJustin T. Gibbs } 34428b8a9b1dSJustin T. Gibbs } 34438b8a9b1dSJustin T. Gibbs } 3444227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 34458b8a9b1dSJustin T. Gibbs } 34468b8a9b1dSJustin T. Gibbs 34478b8a9b1dSJustin T. Gibbs /* 34488b8a9b1dSJustin T. Gibbs * Only touch the user's data if we are successful. 34498b8a9b1dSJustin T. Gibbs */ 34508b8a9b1dSJustin T. Gibbs if (status == CAM_REQ_CMP) { 34518b8a9b1dSJustin T. Gibbs new_path->periph = perph; 34528b8a9b1dSJustin T. Gibbs new_path->bus = bus; 34538b8a9b1dSJustin T. Gibbs new_path->target = target; 34548b8a9b1dSJustin T. Gibbs new_path->device = device; 34558b8a9b1dSJustin T. Gibbs CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n")); 34568b8a9b1dSJustin T. Gibbs } else { 34578b8a9b1dSJustin T. Gibbs if (device != NULL) 3458f98d7a47SAlexander Motin xpt_release_device(device); 34598b8a9b1dSJustin T. Gibbs if (target != NULL) 3460f98d7a47SAlexander Motin xpt_release_target(target); 3461a5479bc5SJustin T. Gibbs if (bus != NULL) 3462a5479bc5SJustin T. Gibbs xpt_release_bus(bus); 34638b8a9b1dSJustin T. Gibbs } 34648b8a9b1dSJustin T. Gibbs return (status); 34658b8a9b1dSJustin T. Gibbs } 34668b8a9b1dSJustin T. Gibbs 3467227d67aaSAlexander Motin cam_status 3468227d67aaSAlexander Motin xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path) 3469227d67aaSAlexander Motin { 3470227d67aaSAlexander Motin struct cam_path *new_path; 3471227d67aaSAlexander Motin 3472227d67aaSAlexander Motin new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT); 3473227d67aaSAlexander Motin if (new_path == NULL) 3474227d67aaSAlexander Motin return(CAM_RESRC_UNAVAIL); 3475227d67aaSAlexander Motin xpt_copy_path(new_path, path); 3476227d67aaSAlexander Motin *new_path_ptr = new_path; 3477227d67aaSAlexander Motin return (CAM_REQ_CMP); 3478227d67aaSAlexander Motin } 3479227d67aaSAlexander Motin 3480227d67aaSAlexander Motin void 3481227d67aaSAlexander Motin xpt_copy_path(struct cam_path *new_path, struct cam_path *path) 3482227d67aaSAlexander Motin { 3483227d67aaSAlexander Motin 3484227d67aaSAlexander Motin *new_path = *path; 3485227d67aaSAlexander Motin if (path->bus != NULL) 3486227d67aaSAlexander Motin xpt_acquire_bus(path->bus); 3487227d67aaSAlexander Motin if (path->target != NULL) 3488227d67aaSAlexander Motin xpt_acquire_target(path->target); 3489227d67aaSAlexander Motin if (path->device != NULL) 3490227d67aaSAlexander Motin xpt_acquire_device(path->device); 3491227d67aaSAlexander Motin } 3492227d67aaSAlexander Motin 349352c9ce25SScott Long void 34948b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path) 34958b8a9b1dSJustin T. Gibbs { 34968b8a9b1dSJustin T. Gibbs CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n")); 34979dd03ecfSJustin T. Gibbs if (path->device != NULL) { 3498f98d7a47SAlexander Motin xpt_release_device(path->device); 34999dd03ecfSJustin T. Gibbs path->device = NULL; 35009dd03ecfSJustin T. Gibbs } 35019dd03ecfSJustin T. Gibbs if (path->target != NULL) { 3502f98d7a47SAlexander Motin xpt_release_target(path->target); 35039dd03ecfSJustin T. Gibbs path->target = NULL; 35049dd03ecfSJustin T. Gibbs } 35059dd03ecfSJustin T. Gibbs if (path->bus != NULL) { 35069dd03ecfSJustin T. Gibbs xpt_release_bus(path->bus); 35079dd03ecfSJustin T. Gibbs path->bus = NULL; 35089dd03ecfSJustin T. Gibbs } 35098b8a9b1dSJustin T. Gibbs } 35108b8a9b1dSJustin T. Gibbs 35118b8a9b1dSJustin T. Gibbs void 35128b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path) 35138b8a9b1dSJustin T. Gibbs { 351468153f43SScott Long 35158b8a9b1dSJustin T. Gibbs CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n")); 35168b8a9b1dSJustin T. Gibbs xpt_release_path(path); 3517596ee08fSAlexander Motin free(path, M_CAMPATH); 35188b8a9b1dSJustin T. Gibbs } 35198b8a9b1dSJustin T. Gibbs 352015975b7bSMatt Jacob void 352115975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref, 352215975b7bSMatt Jacob uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref) 352315975b7bSMatt Jacob { 352415975b7bSMatt Jacob 35259a7c2696SAlexander Motin xpt_lock_buses(); 352615975b7bSMatt Jacob if (bus_ref) { 352715975b7bSMatt Jacob if (path->bus) 352815975b7bSMatt Jacob *bus_ref = path->bus->refcount; 352915975b7bSMatt Jacob else 353015975b7bSMatt Jacob *bus_ref = 0; 353115975b7bSMatt Jacob } 353215975b7bSMatt Jacob if (periph_ref) { 353315975b7bSMatt Jacob if (path->periph) 353415975b7bSMatt Jacob *periph_ref = path->periph->refcount; 353515975b7bSMatt Jacob else 353615975b7bSMatt Jacob *periph_ref = 0; 353715975b7bSMatt Jacob } 3538dcdf6e74SAlexander Motin xpt_unlock_buses(); 353915975b7bSMatt Jacob if (target_ref) { 354015975b7bSMatt Jacob if (path->target) 354115975b7bSMatt Jacob *target_ref = path->target->refcount; 354215975b7bSMatt Jacob else 354315975b7bSMatt Jacob *target_ref = 0; 354415975b7bSMatt Jacob } 354515975b7bSMatt Jacob if (device_ref) { 354615975b7bSMatt Jacob if (path->device) 354715975b7bSMatt Jacob *device_ref = path->device->refcount; 354815975b7bSMatt Jacob else 354915975b7bSMatt Jacob *device_ref = 0; 355015975b7bSMatt Jacob } 355115975b7bSMatt Jacob } 35528b8a9b1dSJustin T. Gibbs 35538b8a9b1dSJustin T. Gibbs /* 35542cefde5fSJustin T. Gibbs * Return -1 for failure, 0 for exact match, 1 for match with wildcards 35552cefde5fSJustin T. Gibbs * in path1, 2 for match with wildcards in path2. 35568b8a9b1dSJustin T. Gibbs */ 35578b8a9b1dSJustin T. Gibbs int 35588b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2) 35598b8a9b1dSJustin T. Gibbs { 35608b8a9b1dSJustin T. Gibbs int retval = 0; 35618b8a9b1dSJustin T. Gibbs 35628b8a9b1dSJustin T. Gibbs if (path1->bus != path2->bus) { 35632cefde5fSJustin T. Gibbs if (path1->bus->path_id == CAM_BUS_WILDCARD) 35648b8a9b1dSJustin T. Gibbs retval = 1; 35652cefde5fSJustin T. Gibbs else if (path2->bus->path_id == CAM_BUS_WILDCARD) 35662cefde5fSJustin T. Gibbs retval = 2; 35678b8a9b1dSJustin T. Gibbs else 35688b8a9b1dSJustin T. Gibbs return (-1); 35698b8a9b1dSJustin T. Gibbs } 35708b8a9b1dSJustin T. Gibbs if (path1->target != path2->target) { 35712cefde5fSJustin T. Gibbs if (path1->target->target_id == CAM_TARGET_WILDCARD) { 35722cefde5fSJustin T. Gibbs if (retval == 0) 35738b8a9b1dSJustin T. Gibbs retval = 1; 35742cefde5fSJustin T. Gibbs } else if (path2->target->target_id == CAM_TARGET_WILDCARD) 35752cefde5fSJustin T. Gibbs retval = 2; 35768b8a9b1dSJustin T. Gibbs else 35778b8a9b1dSJustin T. Gibbs return (-1); 35788b8a9b1dSJustin T. Gibbs } 35798b8a9b1dSJustin T. Gibbs if (path1->device != path2->device) { 35802cefde5fSJustin T. Gibbs if (path1->device->lun_id == CAM_LUN_WILDCARD) { 35812cefde5fSJustin T. Gibbs if (retval == 0) 35828b8a9b1dSJustin T. Gibbs retval = 1; 35832cefde5fSJustin T. Gibbs } else if (path2->device->lun_id == CAM_LUN_WILDCARD) 35842cefde5fSJustin T. Gibbs retval = 2; 35858b8a9b1dSJustin T. Gibbs else 35868b8a9b1dSJustin T. Gibbs return (-1); 35878b8a9b1dSJustin T. Gibbs } 35888b8a9b1dSJustin T. Gibbs return (retval); 35898b8a9b1dSJustin T. Gibbs } 35908b8a9b1dSJustin T. Gibbs 35910d4f3c31SAlexander Motin int 35920d4f3c31SAlexander Motin xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev) 35930d4f3c31SAlexander Motin { 35940d4f3c31SAlexander Motin int retval = 0; 35950d4f3c31SAlexander Motin 35960d4f3c31SAlexander Motin if (path->bus != dev->target->bus) { 35970d4f3c31SAlexander Motin if (path->bus->path_id == CAM_BUS_WILDCARD) 35980d4f3c31SAlexander Motin retval = 1; 35990d4f3c31SAlexander Motin else if (dev->target->bus->path_id == CAM_BUS_WILDCARD) 36000d4f3c31SAlexander Motin retval = 2; 36010d4f3c31SAlexander Motin else 36020d4f3c31SAlexander Motin return (-1); 36030d4f3c31SAlexander Motin } 36040d4f3c31SAlexander Motin if (path->target != dev->target) { 36050d4f3c31SAlexander Motin if (path->target->target_id == CAM_TARGET_WILDCARD) { 36060d4f3c31SAlexander Motin if (retval == 0) 36070d4f3c31SAlexander Motin retval = 1; 36080d4f3c31SAlexander Motin } else if (dev->target->target_id == CAM_TARGET_WILDCARD) 36090d4f3c31SAlexander Motin retval = 2; 36100d4f3c31SAlexander Motin else 36110d4f3c31SAlexander Motin return (-1); 36120d4f3c31SAlexander Motin } 36130d4f3c31SAlexander Motin if (path->device != dev) { 36140d4f3c31SAlexander Motin if (path->device->lun_id == CAM_LUN_WILDCARD) { 36150d4f3c31SAlexander Motin if (retval == 0) 36160d4f3c31SAlexander Motin retval = 1; 36170d4f3c31SAlexander Motin } else if (dev->lun_id == CAM_LUN_WILDCARD) 36180d4f3c31SAlexander Motin retval = 2; 36190d4f3c31SAlexander Motin else 36200d4f3c31SAlexander Motin return (-1); 36210d4f3c31SAlexander Motin } 36220d4f3c31SAlexander Motin return (retval); 36230d4f3c31SAlexander Motin } 36240d4f3c31SAlexander Motin 36258b8a9b1dSJustin T. Gibbs void 36268b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path) 36278b8a9b1dSJustin T. Gibbs { 362868153f43SScott Long 36298b8a9b1dSJustin T. Gibbs if (path == NULL) 36308b8a9b1dSJustin T. Gibbs printf("(nopath): "); 36318b8a9b1dSJustin T. Gibbs else { 36328b8a9b1dSJustin T. Gibbs if (path->periph != NULL) 36338b8a9b1dSJustin T. Gibbs printf("(%s%d:", path->periph->periph_name, 36348b8a9b1dSJustin T. Gibbs path->periph->unit_number); 36358b8a9b1dSJustin T. Gibbs else 36368b8a9b1dSJustin T. Gibbs printf("(noperiph:"); 36378b8a9b1dSJustin T. Gibbs 36388b8a9b1dSJustin T. Gibbs if (path->bus != NULL) 36398b8a9b1dSJustin T. Gibbs printf("%s%d:%d:", path->bus->sim->sim_name, 36408b8a9b1dSJustin T. Gibbs path->bus->sim->unit_number, 36418b8a9b1dSJustin T. Gibbs path->bus->sim->bus_id); 36428b8a9b1dSJustin T. Gibbs else 36438b8a9b1dSJustin T. Gibbs printf("nobus:"); 36448b8a9b1dSJustin T. Gibbs 36458b8a9b1dSJustin T. Gibbs if (path->target != NULL) 36468b8a9b1dSJustin T. Gibbs printf("%d:", path->target->target_id); 36478b8a9b1dSJustin T. Gibbs else 36488b8a9b1dSJustin T. Gibbs printf("X:"); 36498b8a9b1dSJustin T. Gibbs 36508b8a9b1dSJustin T. Gibbs if (path->device != NULL) 3651abe83505SNathan Whitehorn printf("%jx): ", (uintmax_t)path->device->lun_id); 36528b8a9b1dSJustin T. Gibbs else 36538b8a9b1dSJustin T. Gibbs printf("X): "); 36548b8a9b1dSJustin T. Gibbs } 36558b8a9b1dSJustin T. Gibbs } 36568b8a9b1dSJustin T. Gibbs 3657f0d9af51SMatt Jacob void 36580d4f3c31SAlexander Motin xpt_print_device(struct cam_ed *device) 36590d4f3c31SAlexander Motin { 36600d4f3c31SAlexander Motin 36610d4f3c31SAlexander Motin if (device == NULL) 36620d4f3c31SAlexander Motin printf("(nopath): "); 36630d4f3c31SAlexander Motin else { 3664abe83505SNathan Whitehorn printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name, 36650d4f3c31SAlexander Motin device->sim->unit_number, 36660d4f3c31SAlexander Motin device->sim->bus_id, 36670d4f3c31SAlexander Motin device->target->target_id, 3668abe83505SNathan Whitehorn (uintmax_t)device->lun_id); 36690d4f3c31SAlexander Motin } 36700d4f3c31SAlexander Motin } 36710d4f3c31SAlexander Motin 36720d4f3c31SAlexander Motin void 3673f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...) 3674f0d9af51SMatt Jacob { 3675f0d9af51SMatt Jacob va_list ap; 3676f0d9af51SMatt Jacob xpt_print_path(path); 3677f0d9af51SMatt Jacob va_start(ap, fmt); 3678f0d9af51SMatt Jacob vprintf(fmt, ap); 3679f0d9af51SMatt Jacob va_end(ap); 3680f0d9af51SMatt Jacob } 3681f0d9af51SMatt Jacob 36823393f8daSKenneth D. Merry int 36833393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len) 36843393f8daSKenneth D. Merry { 36853393f8daSKenneth D. Merry struct sbuf sb; 36863393f8daSKenneth D. Merry 36873393f8daSKenneth D. Merry sbuf_new(&sb, str, str_len, 0); 36883393f8daSKenneth D. Merry 36893393f8daSKenneth D. Merry if (path == NULL) 36903393f8daSKenneth D. Merry sbuf_printf(&sb, "(nopath): "); 36913393f8daSKenneth D. Merry else { 36923393f8daSKenneth D. Merry if (path->periph != NULL) 36933393f8daSKenneth D. Merry sbuf_printf(&sb, "(%s%d:", path->periph->periph_name, 36943393f8daSKenneth D. Merry path->periph->unit_number); 36953393f8daSKenneth D. Merry else 36963393f8daSKenneth D. Merry sbuf_printf(&sb, "(noperiph:"); 36973393f8daSKenneth D. Merry 36983393f8daSKenneth D. Merry if (path->bus != NULL) 36993393f8daSKenneth D. Merry sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name, 37003393f8daSKenneth D. Merry path->bus->sim->unit_number, 37013393f8daSKenneth D. Merry path->bus->sim->bus_id); 37023393f8daSKenneth D. Merry else 37033393f8daSKenneth D. Merry sbuf_printf(&sb, "nobus:"); 37043393f8daSKenneth D. Merry 37053393f8daSKenneth D. Merry if (path->target != NULL) 37063393f8daSKenneth D. Merry sbuf_printf(&sb, "%d:", path->target->target_id); 37073393f8daSKenneth D. Merry else 37083393f8daSKenneth D. Merry sbuf_printf(&sb, "X:"); 37093393f8daSKenneth D. Merry 37103393f8daSKenneth D. Merry if (path->device != NULL) 3711abe83505SNathan Whitehorn sbuf_printf(&sb, "%jx): ", 3712abe83505SNathan Whitehorn (uintmax_t)path->device->lun_id); 37133393f8daSKenneth D. Merry else 37143393f8daSKenneth D. Merry sbuf_printf(&sb, "X): "); 37153393f8daSKenneth D. Merry } 37163393f8daSKenneth D. Merry sbuf_finish(&sb); 37173393f8daSKenneth D. Merry 37183393f8daSKenneth D. Merry return(sbuf_len(&sb)); 37193393f8daSKenneth D. Merry } 37203393f8daSKenneth D. Merry 37218b8a9b1dSJustin T. Gibbs path_id_t 37228b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path) 37238b8a9b1dSJustin T. Gibbs { 37248b8a9b1dSJustin T. Gibbs return(path->bus->path_id); 37258b8a9b1dSJustin T. Gibbs } 37268b8a9b1dSJustin T. Gibbs 37278b8a9b1dSJustin T. Gibbs target_id_t 37288b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path) 37298b8a9b1dSJustin T. Gibbs { 37308b8a9b1dSJustin T. Gibbs if (path->target != NULL) 37318b8a9b1dSJustin T. Gibbs return (path->target->target_id); 37328b8a9b1dSJustin T. Gibbs else 37338b8a9b1dSJustin T. Gibbs return (CAM_TARGET_WILDCARD); 37348b8a9b1dSJustin T. Gibbs } 37358b8a9b1dSJustin T. Gibbs 37368b8a9b1dSJustin T. Gibbs lun_id_t 37378b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path) 37388b8a9b1dSJustin T. Gibbs { 37398b8a9b1dSJustin T. Gibbs if (path->device != NULL) 37408b8a9b1dSJustin T. Gibbs return (path->device->lun_id); 37418b8a9b1dSJustin T. Gibbs else 37428b8a9b1dSJustin T. Gibbs return (CAM_LUN_WILDCARD); 37438b8a9b1dSJustin T. Gibbs } 37448b8a9b1dSJustin T. Gibbs 37458b8a9b1dSJustin T. Gibbs struct cam_sim * 37468b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path) 37478b8a9b1dSJustin T. Gibbs { 374868153f43SScott Long 37498b8a9b1dSJustin T. Gibbs return (path->bus->sim); 37508b8a9b1dSJustin T. Gibbs } 37518b8a9b1dSJustin T. Gibbs 37528b8a9b1dSJustin T. Gibbs struct cam_periph* 37538b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path) 37548b8a9b1dSJustin T. Gibbs { 375568153f43SScott Long 37568b8a9b1dSJustin T. Gibbs return (path->periph); 37578b8a9b1dSJustin T. Gibbs } 37588b8a9b1dSJustin T. Gibbs 37590d307e09SAlexander Motin int 37600d307e09SAlexander Motin xpt_path_legacy_ata_id(struct cam_path *path) 37610d307e09SAlexander Motin { 37620d307e09SAlexander Motin struct cam_eb *bus; 37630d307e09SAlexander Motin int bus_id; 37640d307e09SAlexander Motin 37650d307e09SAlexander Motin if ((strcmp(path->bus->sim->sim_name, "ata") != 0) && 37660d307e09SAlexander Motin strcmp(path->bus->sim->sim_name, "ahcich") != 0 && 37670d307e09SAlexander Motin strcmp(path->bus->sim->sim_name, "mvsch") != 0 && 37680d307e09SAlexander Motin strcmp(path->bus->sim->sim_name, "siisch") != 0) 37690d307e09SAlexander Motin return (-1); 37700d307e09SAlexander Motin 37710d307e09SAlexander Motin if (strcmp(path->bus->sim->sim_name, "ata") == 0 && 37720d307e09SAlexander Motin path->bus->sim->unit_number < 2) { 37730d307e09SAlexander Motin bus_id = path->bus->sim->unit_number; 37740d307e09SAlexander Motin } else { 37750d307e09SAlexander Motin bus_id = 2; 37760d307e09SAlexander Motin xpt_lock_buses(); 37770d307e09SAlexander Motin TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) { 37780d307e09SAlexander Motin if (bus == path->bus) 37790d307e09SAlexander Motin break; 37800d307e09SAlexander Motin if ((strcmp(bus->sim->sim_name, "ata") == 0 && 37810d307e09SAlexander Motin bus->sim->unit_number >= 2) || 37820d307e09SAlexander Motin strcmp(bus->sim->sim_name, "ahcich") == 0 || 37830d307e09SAlexander Motin strcmp(bus->sim->sim_name, "mvsch") == 0 || 37840d307e09SAlexander Motin strcmp(bus->sim->sim_name, "siisch") == 0) 37850d307e09SAlexander Motin bus_id++; 37860d307e09SAlexander Motin } 37870d307e09SAlexander Motin xpt_unlock_buses(); 37880d307e09SAlexander Motin } 3789ce6cf987SAlexander Motin if (path->target != NULL) { 3790ce6cf987SAlexander Motin if (path->target->target_id < 2) 37910d307e09SAlexander Motin return (bus_id * 2 + path->target->target_id); 37920d307e09SAlexander Motin else 3793ce6cf987SAlexander Motin return (-1); 3794ce6cf987SAlexander Motin } else 37950d307e09SAlexander Motin return (bus_id * 2); 37960d307e09SAlexander Motin } 37970d307e09SAlexander Motin 37988b8a9b1dSJustin T. Gibbs /* 37998b8a9b1dSJustin T. Gibbs * Release a CAM control block for the caller. Remit the cost of the structure 38008b8a9b1dSJustin T. Gibbs * to the device referenced by the path. If the this device had no 'credits' 38018b8a9b1dSJustin T. Gibbs * and peripheral drivers have registered async callbacks for this notification 38028b8a9b1dSJustin T. Gibbs * call them now. 38038b8a9b1dSJustin T. Gibbs */ 38048b8a9b1dSJustin T. Gibbs void 38058b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb) 38068b8a9b1dSJustin T. Gibbs { 38078b8a9b1dSJustin T. Gibbs struct cam_ed *device; 3808227d67aaSAlexander Motin struct cam_periph *periph; 380968153f43SScott Long 3810aa872be6SMatt Jacob CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n")); 3811227d67aaSAlexander Motin xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED); 3812227d67aaSAlexander Motin device = free_ccb->ccb_h.path->device; 3813227d67aaSAlexander Motin periph = free_ccb->ccb_h.path->periph; 38142b83592fSScott Long 38158b8a9b1dSJustin T. Gibbs xpt_free_ccb(free_ccb); 3816227d67aaSAlexander Motin periph->periph_allocated--; 3817227d67aaSAlexander Motin cam_ccbq_release_opening(&device->ccbq); 3818227d67aaSAlexander Motin xpt_run_allocq(periph, 0); 38198b8a9b1dSJustin T. Gibbs } 38208b8a9b1dSJustin T. Gibbs 38218b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */ 38228b8a9b1dSJustin T. Gibbs 382352c9ce25SScott Long static struct xpt_xport xport_default = { 382452c9ce25SScott Long .alloc_device = xpt_alloc_device_default, 382552c9ce25SScott Long .action = xpt_action_default, 382652c9ce25SScott Long .async = xpt_dev_async_default, 382752c9ce25SScott Long }; 382852c9ce25SScott Long 38298b8a9b1dSJustin T. Gibbs /* 38308b8a9b1dSJustin T. Gibbs * A sim structure, listing the SIM entry points and instance 38318b8a9b1dSJustin T. Gibbs * identification info is passed to xpt_bus_register to hook the SIM 38328b8a9b1dSJustin T. Gibbs * into the CAM framework. xpt_bus_register creates a cam_eb entry 38338b8a9b1dSJustin T. Gibbs * for this new bus and places it in the array of busses and assigns 38348b8a9b1dSJustin T. Gibbs * it a path_id. The path_id may be influenced by "hard wiring" 38358b8a9b1dSJustin T. Gibbs * information specified by the user. Once interrupt services are 383602caf36eSEdward Tomasz Napierala * available, the bus will be probed. 38378b8a9b1dSJustin T. Gibbs */ 38388b8a9b1dSJustin T. Gibbs int32_t 3839b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus) 38408b8a9b1dSJustin T. Gibbs { 38418b8a9b1dSJustin T. Gibbs struct cam_eb *new_bus; 3842434bbf6eSJustin T. Gibbs struct cam_eb *old_bus; 38438b8a9b1dSJustin T. Gibbs struct ccb_pathinq cpi; 384483c5d981SAlexander Motin struct cam_path *path; 384552c9ce25SScott Long cam_status status; 38468b8a9b1dSJustin T. Gibbs 38472b83592fSScott Long mtx_assert(sim->mtx, MA_OWNED); 384868153f43SScott Long 38498b8a9b1dSJustin T. Gibbs sim->bus_id = bus; 38508b8a9b1dSJustin T. Gibbs new_bus = (struct cam_eb *)malloc(sizeof(*new_bus), 3851227d67aaSAlexander Motin M_CAMXPT, M_NOWAIT|M_ZERO); 38528b8a9b1dSJustin T. Gibbs if (new_bus == NULL) { 38538b8a9b1dSJustin T. Gibbs /* Couldn't satisfy request */ 38548b8a9b1dSJustin T. Gibbs return (CAM_RESRC_UNAVAIL); 38558b8a9b1dSJustin T. Gibbs } 38568b8a9b1dSJustin T. Gibbs 3857227d67aaSAlexander Motin mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF); 3858434bbf6eSJustin T. Gibbs TAILQ_INIT(&new_bus->et_entries); 3859fa6099fdSEdward Tomasz Napierala cam_sim_hold(sim); 38608b8a9b1dSJustin T. Gibbs new_bus->sim = sim; 386187cfaf0eSJustin T. Gibbs timevalclear(&new_bus->last_reset); 3862434bbf6eSJustin T. Gibbs new_bus->flags = 0; 3863a5479bc5SJustin T. Gibbs new_bus->refcount = 1; /* Held until a bus_deregister event */ 3864434bbf6eSJustin T. Gibbs new_bus->generation = 0; 386552c9ce25SScott Long 38669a7c2696SAlexander Motin xpt_lock_buses(); 38676dfc67e3SAlexander Motin sim->path_id = new_bus->path_id = 38686dfc67e3SAlexander Motin xptpathid(sim->sim_name, sim->unit_number, sim->bus_id); 38692b83592fSScott Long old_bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3870434bbf6eSJustin T. Gibbs while (old_bus != NULL 3871434bbf6eSJustin T. Gibbs && old_bus->path_id < new_bus->path_id) 3872434bbf6eSJustin T. Gibbs old_bus = TAILQ_NEXT(old_bus, links); 3873434bbf6eSJustin T. Gibbs if (old_bus != NULL) 3874434bbf6eSJustin T. Gibbs TAILQ_INSERT_BEFORE(old_bus, new_bus, links); 3875434bbf6eSJustin T. Gibbs else 38762b83592fSScott Long TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links); 38772b83592fSScott Long xsoftc.bus_generation++; 38789a7c2696SAlexander Motin xpt_unlock_buses(); 38798b8a9b1dSJustin T. Gibbs 388052c9ce25SScott Long /* 388152c9ce25SScott Long * Set a default transport so that a PATH_INQ can be issued to 388252c9ce25SScott Long * the SIM. This will then allow for probing and attaching of 388352c9ce25SScott Long * a more appropriate transport. 388452c9ce25SScott Long */ 388552c9ce25SScott Long new_bus->xport = &xport_default; 38868b8a9b1dSJustin T. Gibbs 388732aa80a6SAlexander Motin status = xpt_create_path(&path, /*periph*/NULL, sim->path_id, 38888b8a9b1dSJustin T. Gibbs CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3889627995dcSAlexander Motin if (status != CAM_REQ_CMP) { 3890627995dcSAlexander Motin xpt_release_bus(new_bus); 3891627995dcSAlexander Motin free(path, M_CAMXPT); 3892627995dcSAlexander Motin return (CAM_RESRC_UNAVAIL); 3893627995dcSAlexander Motin } 389452c9ce25SScott Long 389583c5d981SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 38968b8a9b1dSJustin T. Gibbs cpi.ccb_h.func_code = XPT_PATH_INQ; 38978b8a9b1dSJustin T. Gibbs xpt_action((union ccb *)&cpi); 389852c9ce25SScott Long 389952c9ce25SScott Long if (cpi.ccb_h.status == CAM_REQ_CMP) { 390052c9ce25SScott Long switch (cpi.transport) { 390152c9ce25SScott Long case XPORT_SPI: 390252c9ce25SScott Long case XPORT_SAS: 390352c9ce25SScott Long case XPORT_FC: 390452c9ce25SScott Long case XPORT_USB: 390533ea30feSAlexander Motin case XPORT_ISCSI: 3906b5595753SNathan Whitehorn case XPORT_SRP: 390733ea30feSAlexander Motin case XPORT_PPB: 390852c9ce25SScott Long new_bus->xport = scsi_get_xport(); 390952c9ce25SScott Long break; 391052c9ce25SScott Long case XPORT_ATA: 391152c9ce25SScott Long case XPORT_SATA: 391252c9ce25SScott Long new_bus->xport = ata_get_xport(); 391352c9ce25SScott Long break; 391452c9ce25SScott Long default: 391552c9ce25SScott Long new_bus->xport = &xport_default; 391652c9ce25SScott Long break; 39178b8a9b1dSJustin T. Gibbs } 391852c9ce25SScott Long } 391952c9ce25SScott Long 392052c9ce25SScott Long /* Notify interested parties */ 392152c9ce25SScott Long if (sim->path_id != CAM_XPT_PATH_ID) { 392283c5d981SAlexander Motin 392383c5d981SAlexander Motin xpt_async(AC_PATH_REGISTERED, path, &cpi); 3924b01773b0SKenneth D. Merry if ((cpi.hba_misc & PIM_NOSCAN) == 0) { 3925b01773b0SKenneth D. Merry union ccb *scan_ccb; 3926b01773b0SKenneth D. Merry 392783c5d981SAlexander Motin /* Initiate bus rescan. */ 392883c5d981SAlexander Motin scan_ccb = xpt_alloc_ccb_nowait(); 3929e5736ac8SAlexander Motin if (scan_ccb != NULL) { 393083c5d981SAlexander Motin scan_ccb->ccb_h.path = path; 393183c5d981SAlexander Motin scan_ccb->ccb_h.func_code = XPT_SCAN_BUS; 393283c5d981SAlexander Motin scan_ccb->crcn.flags = 0; 393383c5d981SAlexander Motin xpt_rescan(scan_ccb); 39347f7aacb4SAlexander Motin } else { 3935b01773b0SKenneth D. Merry xpt_print(path, 3936b01773b0SKenneth D. Merry "Can't allocate CCB to scan bus\n"); 39377f7aacb4SAlexander Motin xpt_free_path(path); 39387f7aacb4SAlexander Motin } 3939b01773b0SKenneth D. Merry } else 3940b01773b0SKenneth D. Merry xpt_free_path(path); 3941e5736ac8SAlexander Motin } else 394283c5d981SAlexander Motin xpt_free_path(path); 39438b8a9b1dSJustin T. Gibbs return (CAM_SUCCESS); 39448b8a9b1dSJustin T. Gibbs } 39458b8a9b1dSJustin T. Gibbs 3946434bbf6eSJustin T. Gibbs int32_t 3947434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid) 39488b8a9b1dSJustin T. Gibbs { 3949434bbf6eSJustin T. Gibbs struct cam_path bus_path; 3950434bbf6eSJustin T. Gibbs cam_status status; 3951434bbf6eSJustin T. Gibbs 3952434bbf6eSJustin T. Gibbs status = xpt_compile_path(&bus_path, NULL, pathid, 3953434bbf6eSJustin T. Gibbs CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3954434bbf6eSJustin T. Gibbs if (status != CAM_REQ_CMP) 3955434bbf6eSJustin T. Gibbs return (status); 3956434bbf6eSJustin T. Gibbs 3957434bbf6eSJustin T. Gibbs xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 3958434bbf6eSJustin T. Gibbs xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 3959434bbf6eSJustin T. Gibbs 3960434bbf6eSJustin T. Gibbs /* Release the reference count held while registered. */ 3961434bbf6eSJustin T. Gibbs xpt_release_bus(bus_path.bus); 3962434bbf6eSJustin T. Gibbs xpt_release_path(&bus_path); 3963434bbf6eSJustin T. Gibbs 3964434bbf6eSJustin T. Gibbs return (CAM_REQ_CMP); 3965434bbf6eSJustin T. Gibbs } 3966434bbf6eSJustin T. Gibbs 3967434bbf6eSJustin T. Gibbs static path_id_t 3968434bbf6eSJustin T. Gibbs xptnextfreepathid(void) 3969434bbf6eSJustin T. Gibbs { 3970434bbf6eSJustin T. Gibbs struct cam_eb *bus; 3971434bbf6eSJustin T. Gibbs path_id_t pathid; 39722398f0cdSPeter Wemm const char *strval; 39738b8a9b1dSJustin T. Gibbs 39746dfc67e3SAlexander Motin mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED); 3975434bbf6eSJustin T. Gibbs pathid = 0; 39762b83592fSScott Long bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3977434bbf6eSJustin T. Gibbs retry: 3978434bbf6eSJustin T. Gibbs /* Find an unoccupied pathid */ 39799e6461a2SMatt Jacob while (bus != NULL && bus->path_id <= pathid) { 3980434bbf6eSJustin T. Gibbs if (bus->path_id == pathid) 3981434bbf6eSJustin T. Gibbs pathid++; 3982434bbf6eSJustin T. Gibbs bus = TAILQ_NEXT(bus, links); 3983434bbf6eSJustin T. Gibbs } 3984434bbf6eSJustin T. Gibbs 3985434bbf6eSJustin T. Gibbs /* 3986434bbf6eSJustin T. Gibbs * Ensure that this pathid is not reserved for 3987434bbf6eSJustin T. Gibbs * a bus that may be registered in the future. 3988434bbf6eSJustin T. Gibbs */ 398975f51904SPeter Wemm if (resource_string_value("scbus", pathid, "at", &strval) == 0) { 3990434bbf6eSJustin T. Gibbs ++pathid; 39918b8a9b1dSJustin T. Gibbs /* Start the search over */ 3992434bbf6eSJustin T. Gibbs goto retry; 39938b8a9b1dSJustin T. Gibbs } 3994434bbf6eSJustin T. Gibbs return (pathid); 39958b8a9b1dSJustin T. Gibbs } 39968b8a9b1dSJustin T. Gibbs 3997434bbf6eSJustin T. Gibbs static path_id_t 3998434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus) 39998b8a9b1dSJustin T. Gibbs { 40008b8a9b1dSJustin T. Gibbs path_id_t pathid; 400175f51904SPeter Wemm int i, dunit, val; 4002642f0c46SPeter Wemm char buf[32]; 40032398f0cdSPeter Wemm const char *dname; 40048b8a9b1dSJustin T. Gibbs 40058b8a9b1dSJustin T. Gibbs pathid = CAM_XPT_PATH_ID; 400675f51904SPeter Wemm snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit); 40076dfc67e3SAlexander Motin if (strcmp(buf, "xpt0") == 0 && sim_bus == 0) 40086dfc67e3SAlexander Motin return (pathid); 40092398f0cdSPeter Wemm i = 0; 40102398f0cdSPeter Wemm while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) { 40112398f0cdSPeter Wemm if (strcmp(dname, "scbus")) { 4012642f0c46SPeter Wemm /* Avoid a bit of foot shooting. */ 4013642f0c46SPeter Wemm continue; 4014642f0c46SPeter Wemm } 401575f51904SPeter Wemm if (dunit < 0) /* unwired?! */ 40168b8a9b1dSJustin T. Gibbs continue; 401775f51904SPeter Wemm if (resource_int_value("scbus", dunit, "bus", &val) == 0) { 401875f51904SPeter Wemm if (sim_bus == val) { 401975f51904SPeter Wemm pathid = dunit; 40208b8a9b1dSJustin T. Gibbs break; 40218b8a9b1dSJustin T. Gibbs } 40228b8a9b1dSJustin T. Gibbs } else if (sim_bus == 0) { 40238b8a9b1dSJustin T. Gibbs /* Unspecified matches bus 0 */ 402475f51904SPeter Wemm pathid = dunit; 40258b8a9b1dSJustin T. Gibbs break; 40268b8a9b1dSJustin T. Gibbs } else { 40278b8a9b1dSJustin T. Gibbs printf("Ambiguous scbus configuration for %s%d " 40288b8a9b1dSJustin T. Gibbs "bus %d, cannot wire down. The kernel " 40298b8a9b1dSJustin T. Gibbs "config entry for scbus%d should " 40308b8a9b1dSJustin T. Gibbs "specify a controller bus.\n" 40318b8a9b1dSJustin T. Gibbs "Scbus will be assigned dynamically.\n", 403275f51904SPeter Wemm sim_name, sim_unit, sim_bus, dunit); 40338b8a9b1dSJustin T. Gibbs break; 40348b8a9b1dSJustin T. Gibbs } 40358b8a9b1dSJustin T. Gibbs } 40368b8a9b1dSJustin T. Gibbs 4037434bbf6eSJustin T. Gibbs if (pathid == CAM_XPT_PATH_ID) 4038434bbf6eSJustin T. Gibbs pathid = xptnextfreepathid(); 40398b8a9b1dSJustin T. Gibbs return (pathid); 40408b8a9b1dSJustin T. Gibbs } 40418b8a9b1dSJustin T. Gibbs 404222c7d606SAlexander Motin static const char * 404322c7d606SAlexander Motin xpt_async_string(u_int32_t async_code) 404422c7d606SAlexander Motin { 404522c7d606SAlexander Motin 404622c7d606SAlexander Motin switch (async_code) { 404722c7d606SAlexander Motin case AC_BUS_RESET: return ("AC_BUS_RESET"); 404822c7d606SAlexander Motin case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL"); 404922c7d606SAlexander Motin case AC_SCSI_AEN: return ("AC_SCSI_AEN"); 405022c7d606SAlexander Motin case AC_SENT_BDR: return ("AC_SENT_BDR"); 405122c7d606SAlexander Motin case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED"); 405222c7d606SAlexander Motin case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED"); 405322c7d606SAlexander Motin case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE"); 405422c7d606SAlexander Motin case AC_LOST_DEVICE: return ("AC_LOST_DEVICE"); 405522c7d606SAlexander Motin case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG"); 405622c7d606SAlexander Motin case AC_INQ_CHANGED: return ("AC_INQ_CHANGED"); 405722c7d606SAlexander Motin case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED"); 405822c7d606SAlexander Motin case AC_CONTRACT: return ("AC_CONTRACT"); 405922c7d606SAlexander Motin case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED"); 40603631c638SAlexander Motin case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION"); 406122c7d606SAlexander Motin } 406222c7d606SAlexander Motin return ("AC_UNKNOWN"); 406322c7d606SAlexander Motin } 406422c7d606SAlexander Motin 4065227d67aaSAlexander Motin static int 4066227d67aaSAlexander Motin xpt_async_size(u_int32_t async_code) 40678b8a9b1dSJustin T. Gibbs { 40688b8a9b1dSJustin T. Gibbs 4069227d67aaSAlexander Motin switch (async_code) { 4070227d67aaSAlexander Motin case AC_BUS_RESET: return (0); 4071227d67aaSAlexander Motin case AC_UNSOL_RESEL: return (0); 4072227d67aaSAlexander Motin case AC_SCSI_AEN: return (0); 4073227d67aaSAlexander Motin case AC_SENT_BDR: return (0); 4074227d67aaSAlexander Motin case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq)); 4075227d67aaSAlexander Motin case AC_PATH_DEREGISTERED: return (0); 4076227d67aaSAlexander Motin case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev)); 4077227d67aaSAlexander Motin case AC_LOST_DEVICE: return (0); 4078227d67aaSAlexander Motin case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings)); 4079227d67aaSAlexander Motin case AC_INQ_CHANGED: return (0); 4080227d67aaSAlexander Motin case AC_GETDEV_CHANGED: return (0); 4081227d67aaSAlexander Motin case AC_CONTRACT: return (sizeof(struct ac_contract)); 4082227d67aaSAlexander Motin case AC_ADVINFO_CHANGED: return (-1); 4083227d67aaSAlexander Motin case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio)); 4084227d67aaSAlexander Motin } 4085227d67aaSAlexander Motin return (0); 4086227d67aaSAlexander Motin } 4087227d67aaSAlexander Motin 4088227d67aaSAlexander Motin static int 4089227d67aaSAlexander Motin xpt_async_process_dev(struct cam_ed *device, void *arg) 4090227d67aaSAlexander Motin { 4091227d67aaSAlexander Motin union ccb *ccb = arg; 4092227d67aaSAlexander Motin struct cam_path *path = ccb->ccb_h.path; 4093227d67aaSAlexander Motin void *async_arg = ccb->casync.async_arg_ptr; 4094227d67aaSAlexander Motin u_int32_t async_code = ccb->casync.async_code; 4095227d67aaSAlexander Motin int relock; 4096227d67aaSAlexander Motin 4097227d67aaSAlexander Motin if (path->device != device 4098227d67aaSAlexander Motin && path->device->lun_id != CAM_LUN_WILDCARD 4099227d67aaSAlexander Motin && device->lun_id != CAM_LUN_WILDCARD) 4100227d67aaSAlexander Motin return (1); 41018b8a9b1dSJustin T. Gibbs 4102a5479bc5SJustin T. Gibbs /* 4103227d67aaSAlexander Motin * The async callback could free the device. 4104227d67aaSAlexander Motin * If it is a broadcast async, it doesn't hold 4105227d67aaSAlexander Motin * device reference, so take our own reference. 4106a5479bc5SJustin T. Gibbs */ 4107227d67aaSAlexander Motin xpt_acquire_device(device); 41088b8a9b1dSJustin T. Gibbs 4109227d67aaSAlexander Motin /* 4110227d67aaSAlexander Motin * If async for specific device is to be delivered to 4111227d67aaSAlexander Motin * the wildcard client, take the specific device lock. 4112227d67aaSAlexander Motin * XXX: We may need a way for client to specify it. 4113227d67aaSAlexander Motin */ 4114227d67aaSAlexander Motin if ((device->lun_id == CAM_LUN_WILDCARD && 4115227d67aaSAlexander Motin path->device->lun_id != CAM_LUN_WILDCARD) || 4116227d67aaSAlexander Motin (device->target->target_id == CAM_TARGET_WILDCARD && 4117227d67aaSAlexander Motin path->target->target_id != CAM_TARGET_WILDCARD) || 4118227d67aaSAlexander Motin (device->target->bus->path_id == CAM_BUS_WILDCARD && 4119227d67aaSAlexander Motin path->target->bus->path_id != CAM_BUS_WILDCARD)) { 4120227d67aaSAlexander Motin mtx_unlock(&device->device_mtx); 4121227d67aaSAlexander Motin xpt_path_lock(path); 4122227d67aaSAlexander Motin relock = 1; 4123227d67aaSAlexander Motin } else 4124227d67aaSAlexander Motin relock = 0; 4125227d67aaSAlexander Motin 4126227d67aaSAlexander Motin (*(device->target->bus->xport->async))(async_code, 4127227d67aaSAlexander Motin device->target->bus, device->target, device, async_arg); 4128227d67aaSAlexander Motin xpt_async_bcast(&device->asyncs, async_code, path, async_arg); 4129227d67aaSAlexander Motin 4130227d67aaSAlexander Motin if (relock) { 4131227d67aaSAlexander Motin xpt_path_unlock(path); 4132227d67aaSAlexander Motin mtx_lock(&device->device_mtx); 4133227d67aaSAlexander Motin } 4134227d67aaSAlexander Motin xpt_release_device(device); 4135227d67aaSAlexander Motin return (1); 4136227d67aaSAlexander Motin } 4137227d67aaSAlexander Motin 4138227d67aaSAlexander Motin static int 4139227d67aaSAlexander Motin xpt_async_process_tgt(struct cam_et *target, void *arg) 4140227d67aaSAlexander Motin { 4141227d67aaSAlexander Motin union ccb *ccb = arg; 4142227d67aaSAlexander Motin struct cam_path *path = ccb->ccb_h.path; 4143227d67aaSAlexander Motin 4144227d67aaSAlexander Motin if (path->target != target 4145227d67aaSAlexander Motin && path->target->target_id != CAM_TARGET_WILDCARD 4146227d67aaSAlexander Motin && target->target_id != CAM_TARGET_WILDCARD) 4147227d67aaSAlexander Motin return (1); 4148227d67aaSAlexander Motin 4149227d67aaSAlexander Motin if (ccb->casync.async_code == AC_SENT_BDR) { 4150227d67aaSAlexander Motin /* Update our notion of when the last reset occurred */ 4151227d67aaSAlexander Motin microtime(&target->last_reset); 4152227d67aaSAlexander Motin } 4153227d67aaSAlexander Motin 4154227d67aaSAlexander Motin return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb)); 4155227d67aaSAlexander Motin } 4156227d67aaSAlexander Motin 4157227d67aaSAlexander Motin static void 4158227d67aaSAlexander Motin xpt_async_process(struct cam_periph *periph, union ccb *ccb) 4159227d67aaSAlexander Motin { 4160227d67aaSAlexander Motin struct cam_eb *bus; 4161227d67aaSAlexander Motin struct cam_path *path; 4162227d67aaSAlexander Motin void *async_arg; 4163227d67aaSAlexander Motin u_int32_t async_code; 4164227d67aaSAlexander Motin 4165227d67aaSAlexander Motin path = ccb->ccb_h.path; 4166227d67aaSAlexander Motin async_code = ccb->casync.async_code; 4167227d67aaSAlexander Motin async_arg = ccb->casync.async_arg_ptr; 4168227d67aaSAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO, 4169227d67aaSAlexander Motin ("xpt_async(%s)\n", xpt_async_string(async_code))); 41708b8a9b1dSJustin T. Gibbs bus = path->bus; 41718b8a9b1dSJustin T. Gibbs 41728b8a9b1dSJustin T. Gibbs if (async_code == AC_BUS_RESET) { 417387cfaf0eSJustin T. Gibbs /* Update our notion of when the last reset occurred */ 417487cfaf0eSJustin T. Gibbs microtime(&bus->last_reset); 41758b8a9b1dSJustin T. Gibbs } 41768b8a9b1dSJustin T. Gibbs 4177227d67aaSAlexander Motin xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb); 4178c8bead2aSJustin T. Gibbs 4179c8bead2aSJustin T. Gibbs /* 4180c8bead2aSJustin T. Gibbs * If this wasn't a fully wildcarded async, tell all 4181c8bead2aSJustin T. Gibbs * clients that want all async events. 4182c8bead2aSJustin T. Gibbs */ 4183227d67aaSAlexander Motin if (bus != xpt_periph->path->bus) { 4184227d67aaSAlexander Motin xpt_path_lock(xpt_periph->path); 4185227d67aaSAlexander Motin xpt_async_process_dev(xpt_periph->path->device, ccb); 4186227d67aaSAlexander Motin xpt_path_unlock(xpt_periph->path); 4187227d67aaSAlexander Motin } 4188227d67aaSAlexander Motin 4189227d67aaSAlexander Motin if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD) 4190227d67aaSAlexander Motin xpt_release_devq(path, 1, TRUE); 4191227d67aaSAlexander Motin else 4192227d67aaSAlexander Motin xpt_release_simq(path->bus->sim, TRUE); 4193227d67aaSAlexander Motin if (ccb->casync.async_arg_size > 0) 4194227d67aaSAlexander Motin free(async_arg, M_CAMXPT); 4195227d67aaSAlexander Motin xpt_free_path(path); 4196227d67aaSAlexander Motin xpt_free_ccb(ccb); 41978b8a9b1dSJustin T. Gibbs } 41988b8a9b1dSJustin T. Gibbs 41998b8a9b1dSJustin T. Gibbs static void 42008b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head, 42018b8a9b1dSJustin T. Gibbs u_int32_t async_code, 42028b8a9b1dSJustin T. Gibbs struct cam_path *path, void *async_arg) 42038b8a9b1dSJustin T. Gibbs { 42048b8a9b1dSJustin T. Gibbs struct async_node *cur_entry; 4205227d67aaSAlexander Motin int lock; 42068b8a9b1dSJustin T. Gibbs 42078b8a9b1dSJustin T. Gibbs cur_entry = SLIST_FIRST(async_head); 42088b8a9b1dSJustin T. Gibbs while (cur_entry != NULL) { 42098b8a9b1dSJustin T. Gibbs struct async_node *next_entry; 42108b8a9b1dSJustin T. Gibbs /* 42118b8a9b1dSJustin T. Gibbs * Grab the next list entry before we call the current 42128b8a9b1dSJustin T. Gibbs * entry's callback. This is because the callback function 42138b8a9b1dSJustin T. Gibbs * can delete its async callback entry. 42148b8a9b1dSJustin T. Gibbs */ 42158b8a9b1dSJustin T. Gibbs next_entry = SLIST_NEXT(cur_entry, links); 4216227d67aaSAlexander Motin if ((cur_entry->event_enable & async_code) != 0) { 4217227d67aaSAlexander Motin lock = cur_entry->event_lock; 4218227d67aaSAlexander Motin if (lock) 4219227d67aaSAlexander Motin CAM_SIM_LOCK(path->device->sim); 42208b8a9b1dSJustin T. Gibbs cur_entry->callback(cur_entry->callback_arg, 42218b8a9b1dSJustin T. Gibbs async_code, path, 42228b8a9b1dSJustin T. Gibbs async_arg); 4223227d67aaSAlexander Motin if (lock) 4224227d67aaSAlexander Motin CAM_SIM_UNLOCK(path->device->sim); 4225227d67aaSAlexander Motin } 42268b8a9b1dSJustin T. Gibbs cur_entry = next_entry; 42278b8a9b1dSJustin T. Gibbs } 42288b8a9b1dSJustin T. Gibbs } 42298b8a9b1dSJustin T. Gibbs 4230227d67aaSAlexander Motin void 4231227d67aaSAlexander Motin xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg) 4232227d67aaSAlexander Motin { 4233227d67aaSAlexander Motin union ccb *ccb; 4234227d67aaSAlexander Motin int size; 4235227d67aaSAlexander Motin 4236227d67aaSAlexander Motin ccb = xpt_alloc_ccb_nowait(); 4237227d67aaSAlexander Motin if (ccb == NULL) { 4238227d67aaSAlexander Motin xpt_print(path, "Can't allocate CCB to send %s\n", 4239227d67aaSAlexander Motin xpt_async_string(async_code)); 4240227d67aaSAlexander Motin return; 4241227d67aaSAlexander Motin } 4242227d67aaSAlexander Motin 4243227d67aaSAlexander Motin if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) { 4244227d67aaSAlexander Motin xpt_print(path, "Can't allocate path to send %s\n", 4245227d67aaSAlexander Motin xpt_async_string(async_code)); 4246227d67aaSAlexander Motin xpt_free_ccb(ccb); 4247227d67aaSAlexander Motin return; 4248227d67aaSAlexander Motin } 4249227d67aaSAlexander Motin ccb->ccb_h.path->periph = NULL; 4250227d67aaSAlexander Motin ccb->ccb_h.func_code = XPT_ASYNC; 4251227d67aaSAlexander Motin ccb->ccb_h.cbfcnp = xpt_async_process; 4252227d67aaSAlexander Motin ccb->ccb_h.flags |= CAM_UNLOCKED; 4253227d67aaSAlexander Motin ccb->casync.async_code = async_code; 4254227d67aaSAlexander Motin ccb->casync.async_arg_size = 0; 4255227d67aaSAlexander Motin size = xpt_async_size(async_code); 4256227d67aaSAlexander Motin if (size > 0 && async_arg != NULL) { 4257227d67aaSAlexander Motin ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT); 4258227d67aaSAlexander Motin if (ccb->casync.async_arg_ptr == NULL) { 4259227d67aaSAlexander Motin xpt_print(path, "Can't allocate argument to send %s\n", 4260227d67aaSAlexander Motin xpt_async_string(async_code)); 4261227d67aaSAlexander Motin xpt_free_path(ccb->ccb_h.path); 4262227d67aaSAlexander Motin xpt_free_ccb(ccb); 4263227d67aaSAlexander Motin return; 4264227d67aaSAlexander Motin } 4265227d67aaSAlexander Motin memcpy(ccb->casync.async_arg_ptr, async_arg, size); 4266227d67aaSAlexander Motin ccb->casync.async_arg_size = size; 4267738fd166SAlan Somers } else if (size < 0) { 4268738fd166SAlan Somers ccb->casync.async_arg_ptr = async_arg; 4269227d67aaSAlexander Motin ccb->casync.async_arg_size = size; 4270738fd166SAlan Somers } 4271227d67aaSAlexander Motin if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD) 4272227d67aaSAlexander Motin xpt_freeze_devq(path, 1); 4273227d67aaSAlexander Motin else 4274227d67aaSAlexander Motin xpt_freeze_simq(path->bus->sim, 1); 4275227d67aaSAlexander Motin xpt_done(ccb); 4276227d67aaSAlexander Motin } 4277227d67aaSAlexander Motin 42782f22d08dSJustin T. Gibbs static void 427952c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus, 428052c9ce25SScott Long struct cam_et *target, struct cam_ed *device, 428152c9ce25SScott Long void *async_arg) 42822f22d08dSJustin T. Gibbs { 4283227d67aaSAlexander Motin 4284227d67aaSAlexander Motin /* 4285227d67aaSAlexander Motin * We only need to handle events for real devices. 4286227d67aaSAlexander Motin */ 4287227d67aaSAlexander Motin if (target->target_id == CAM_TARGET_WILDCARD 4288227d67aaSAlexander Motin || device->lun_id == CAM_LUN_WILDCARD) 4289227d67aaSAlexander Motin return; 4290227d67aaSAlexander Motin 4291b882a6d3SMatt Jacob printf("%s called\n", __func__); 42922f22d08dSJustin T. Gibbs } 42932f22d08dSJustin T. Gibbs 4294daa5487fSAlexander Motin static uint32_t 4295daa5487fSAlexander Motin xpt_freeze_devq_device(struct cam_ed *dev, u_int count) 4296daa5487fSAlexander Motin { 4297daa5487fSAlexander Motin struct cam_devq *devq; 4298daa5487fSAlexander Motin uint32_t freeze; 4299daa5487fSAlexander Motin 4300daa5487fSAlexander Motin devq = dev->sim->devq; 4301daa5487fSAlexander Motin mtx_assert(&devq->send_mtx, MA_OWNED); 4302daa5487fSAlexander Motin CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, 4303daa5487fSAlexander Motin ("xpt_freeze_devq_device(%d) %u->%u\n", count, 4304daa5487fSAlexander Motin dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count)); 4305daa5487fSAlexander Motin freeze = (dev->ccbq.queue.qfrozen_cnt += count); 4306daa5487fSAlexander Motin /* Remove frozen device from sendq. */ 4307daa5487fSAlexander Motin if (device_is_queued(dev)) 4308daa5487fSAlexander Motin camq_remove(&devq->send_queue, dev->devq_entry.index); 4309daa5487fSAlexander Motin return (freeze); 4310daa5487fSAlexander Motin } 4311daa5487fSAlexander Motin 43128b8a9b1dSJustin T. Gibbs u_int32_t 4313cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count) 431483c5d981SAlexander Motin { 431583c5d981SAlexander Motin struct cam_ed *dev = path->device; 4316227d67aaSAlexander Motin struct cam_devq *devq; 4317227d67aaSAlexander Motin uint32_t freeze; 431883c5d981SAlexander Motin 4319227d67aaSAlexander Motin devq = dev->sim->devq; 4320227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4321daa5487fSAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count)); 4322daa5487fSAlexander Motin freeze = xpt_freeze_devq_device(dev, count); 4323227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 4324227d67aaSAlexander Motin return (freeze); 43258b8a9b1dSJustin T. Gibbs } 43268b8a9b1dSJustin T. Gibbs 43278b8a9b1dSJustin T. Gibbs u_int32_t 43288b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count) 43298b8a9b1dSJustin T. Gibbs { 4330227d67aaSAlexander Motin struct cam_devq *devq; 4331227d67aaSAlexander Motin uint32_t freeze; 4332ec700f26SAlexander Motin 4333227d67aaSAlexander Motin devq = sim->devq; 4334227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4335227d67aaSAlexander Motin freeze = (devq->send_queue.qfrozen_cnt += count); 4336227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 4337227d67aaSAlexander Motin return (freeze); 43388b8a9b1dSJustin T. Gibbs } 43398b8a9b1dSJustin T. Gibbs 43408b8a9b1dSJustin T. Gibbs static void 43418b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg) 43428b8a9b1dSJustin T. Gibbs { 4343227d67aaSAlexander Motin struct cam_ed *dev; 4344227d67aaSAlexander Motin struct cam_devq *devq; 43458b8a9b1dSJustin T. Gibbs 4346227d67aaSAlexander Motin dev = (struct cam_ed *)arg; 4347227d67aaSAlexander Motin CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n")); 4348227d67aaSAlexander Motin devq = dev->sim->devq; 4349227d67aaSAlexander Motin mtx_assert(&devq->send_mtx, MA_OWNED); 4350227d67aaSAlexander Motin if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE)) 4351227d67aaSAlexander Motin xpt_run_devq(devq); 43528b8a9b1dSJustin T. Gibbs } 43538b8a9b1dSJustin T. Gibbs 43548b8a9b1dSJustin T. Gibbs void 43552cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue) 43562cefde5fSJustin T. Gibbs { 4357227d67aaSAlexander Motin struct cam_ed *dev; 4358227d67aaSAlexander Motin struct cam_devq *devq; 435968153f43SScott Long 43600d4f3c31SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n", 43610d4f3c31SAlexander Motin count, run_queue)); 4362227d67aaSAlexander Motin dev = path->device; 4363227d67aaSAlexander Motin devq = dev->sim->devq; 4364227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4365227d67aaSAlexander Motin if (xpt_release_devq_device(dev, count, run_queue)) 4366227d67aaSAlexander Motin xpt_run_devq(dev->sim->devq); 4367227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 436883c5d981SAlexander Motin } 436983c5d981SAlexander Motin 4370227d67aaSAlexander Motin static int 4371cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue) 43728b8a9b1dSJustin T. Gibbs { 43738b8a9b1dSJustin T. Gibbs 4374227d67aaSAlexander Motin mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED); 43750d4f3c31SAlexander Motin CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, 43760d4f3c31SAlexander Motin ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue, 43770d4f3c31SAlexander Motin dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count)); 4378cccf4220SAlexander Motin if (count > dev->ccbq.queue.qfrozen_cnt) { 437983c5d981SAlexander Motin #ifdef INVARIANTS 4380cccf4220SAlexander Motin printf("xpt_release_devq(): requested %u > present %u\n", 4381cccf4220SAlexander Motin count, dev->ccbq.queue.qfrozen_cnt); 438283c5d981SAlexander Motin #endif 4383cccf4220SAlexander Motin count = dev->ccbq.queue.qfrozen_cnt; 438483c5d981SAlexander Motin } 4385cccf4220SAlexander Motin dev->ccbq.queue.qfrozen_cnt -= count; 4386cccf4220SAlexander Motin if (dev->ccbq.queue.qfrozen_cnt == 0) { 43878b8a9b1dSJustin T. Gibbs /* 43888b8a9b1dSJustin T. Gibbs * No longer need to wait for a successful 43898b8a9b1dSJustin T. Gibbs * command completion. 43908b8a9b1dSJustin T. Gibbs */ 43918b8a9b1dSJustin T. Gibbs dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 43928b8a9b1dSJustin T. Gibbs /* 43938b8a9b1dSJustin T. Gibbs * Remove any timeouts that might be scheduled 43948b8a9b1dSJustin T. Gibbs * to release this queue. 43958b8a9b1dSJustin T. Gibbs */ 43968b8a9b1dSJustin T. Gibbs if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 43972b83592fSScott Long callout_stop(&dev->callout); 43988b8a9b1dSJustin T. Gibbs dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING; 43998b8a9b1dSJustin T. Gibbs } 44008b8a9b1dSJustin T. Gibbs /* 44018b8a9b1dSJustin T. Gibbs * Now that we are unfrozen schedule the 44028b8a9b1dSJustin T. Gibbs * device so any pending transactions are 44038b8a9b1dSJustin T. Gibbs * run. 44048b8a9b1dSJustin T. Gibbs */ 4405227d67aaSAlexander Motin xpt_schedule_devq(dev->sim->devq, dev); 4406227d67aaSAlexander Motin } else 4407227d67aaSAlexander Motin run_queue = 0; 4408227d67aaSAlexander Motin return (run_queue); 440983c5d981SAlexander Motin } 44108b8a9b1dSJustin T. Gibbs 44118b8a9b1dSJustin T. Gibbs void 44128b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue) 44138b8a9b1dSJustin T. Gibbs { 4414227d67aaSAlexander Motin struct cam_devq *devq; 44158b8a9b1dSJustin T. Gibbs 4416227d67aaSAlexander Motin devq = sim->devq; 4417227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4418227d67aaSAlexander Motin if (devq->send_queue.qfrozen_cnt <= 0) { 441983c5d981SAlexander Motin #ifdef INVARIANTS 442083c5d981SAlexander Motin printf("xpt_release_simq: requested 1 > present %u\n", 4421227d67aaSAlexander Motin devq->send_queue.qfrozen_cnt); 442283c5d981SAlexander Motin #endif 442383c5d981SAlexander Motin } else 4424227d67aaSAlexander Motin devq->send_queue.qfrozen_cnt--; 4425227d67aaSAlexander Motin if (devq->send_queue.qfrozen_cnt == 0) { 44268b8a9b1dSJustin T. Gibbs /* 44278b8a9b1dSJustin T. Gibbs * If there is a timeout scheduled to release this 44288b8a9b1dSJustin T. Gibbs * sim queue, remove it. The queue frozen count is 44298b8a9b1dSJustin T. Gibbs * already at 0. 44308b8a9b1dSJustin T. Gibbs */ 44318b8a9b1dSJustin T. Gibbs if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){ 44322b83592fSScott Long callout_stop(&sim->callout); 44338b8a9b1dSJustin T. Gibbs sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING; 44348b8a9b1dSJustin T. Gibbs } 44358b8a9b1dSJustin T. Gibbs if (run_queue) { 44368b8a9b1dSJustin T. Gibbs /* 44378b8a9b1dSJustin T. Gibbs * Now that we are unfrozen run the send queue. 44388b8a9b1dSJustin T. Gibbs */ 4439cccf4220SAlexander Motin xpt_run_devq(sim->devq); 444077dc25ccSScott Long } 444177dc25ccSScott Long } 4442227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 44438b8a9b1dSJustin T. Gibbs } 44448b8a9b1dSJustin T. Gibbs 44452b83592fSScott Long /* 44462b83592fSScott Long * XXX Appears to be unused. 44472b83592fSScott Long */ 44488b8a9b1dSJustin T. Gibbs static void 44498b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg) 44508b8a9b1dSJustin T. Gibbs { 44518b8a9b1dSJustin T. Gibbs struct cam_sim *sim; 44528b8a9b1dSJustin T. Gibbs 44538b8a9b1dSJustin T. Gibbs sim = (struct cam_sim *)arg; 44548b8a9b1dSJustin T. Gibbs xpt_release_simq(sim, /* run_queue */ TRUE); 44558b8a9b1dSJustin T. Gibbs } 44568b8a9b1dSJustin T. Gibbs 44578b8a9b1dSJustin T. Gibbs void 4458a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb) 44598b8a9b1dSJustin T. Gibbs { 4460227d67aaSAlexander Motin struct cam_doneq *queue; 4461227d67aaSAlexander Motin int run, hash; 44628b8a9b1dSJustin T. Gibbs 44638b8a9b1dSJustin T. Gibbs CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n")); 4464227d67aaSAlexander Motin if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0) 4465227d67aaSAlexander Motin return; 4466227d67aaSAlexander Motin 4467227d67aaSAlexander Motin hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id + 4468227d67aaSAlexander Motin done_ccb->ccb_h.target_lun) % cam_num_doneqs; 4469227d67aaSAlexander Motin queue = &cam_doneqs[hash]; 4470227d67aaSAlexander Motin mtx_lock(&queue->cam_doneq_mtx); 4471227d67aaSAlexander Motin run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq)); 4472227d67aaSAlexander Motin STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe); 44738b8a9b1dSJustin T. Gibbs done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4474227d67aaSAlexander Motin mtx_unlock(&queue->cam_doneq_mtx); 4475227d67aaSAlexander Motin if (run) 4476227d67aaSAlexander Motin wakeup(&queue->cam_doneq); 44778b8a9b1dSJustin T. Gibbs } 44788b8a9b1dSJustin T. Gibbs 4479711f6613SAlexander Motin void 4480227d67aaSAlexander Motin xpt_done_direct(union ccb *done_ccb) 4481711f6613SAlexander Motin { 4482711f6613SAlexander Motin 4483227d67aaSAlexander Motin CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done_direct\n")); 4484227d67aaSAlexander Motin if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0) 4485227d67aaSAlexander Motin return; 4486711f6613SAlexander Motin 4487227d67aaSAlexander Motin xpt_done_process(&done_ccb->ccb_h); 4488711f6613SAlexander Motin } 4489711f6613SAlexander Motin 44908b8a9b1dSJustin T. Gibbs union ccb * 44918008a935SScott Long xpt_alloc_ccb() 44928b8a9b1dSJustin T. Gibbs { 44938b8a9b1dSJustin T. Gibbs union ccb *new_ccb; 44948b8a9b1dSJustin T. Gibbs 4495596ee08fSAlexander Motin new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK); 4496362abc44STai-hwa Liang return (new_ccb); 4497362abc44STai-hwa Liang } 4498362abc44STai-hwa Liang 4499362abc44STai-hwa Liang union ccb * 45008008a935SScott Long xpt_alloc_ccb_nowait() 4501362abc44STai-hwa Liang { 4502362abc44STai-hwa Liang union ccb *new_ccb; 4503362abc44STai-hwa Liang 4504596ee08fSAlexander Motin new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT); 45058b8a9b1dSJustin T. Gibbs return (new_ccb); 45068b8a9b1dSJustin T. Gibbs } 45078b8a9b1dSJustin T. Gibbs 45088b8a9b1dSJustin T. Gibbs void 45098b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb) 45108b8a9b1dSJustin T. Gibbs { 4511596ee08fSAlexander Motin free(free_ccb, M_CAMCCB); 45128b8a9b1dSJustin T. Gibbs } 45138b8a9b1dSJustin T. Gibbs 45148b8a9b1dSJustin T. Gibbs 45158b8a9b1dSJustin T. Gibbs 45168b8a9b1dSJustin T. Gibbs /* Private XPT functions */ 45178b8a9b1dSJustin T. Gibbs 45188b8a9b1dSJustin T. Gibbs /* 45198b8a9b1dSJustin T. Gibbs * Get a CAM control block for the caller. Charge the structure to the device 4520227d67aaSAlexander Motin * referenced by the path. If we don't have sufficient resources to allocate 4521227d67aaSAlexander Motin * more ccbs, we return NULL. 45228b8a9b1dSJustin T. Gibbs */ 45238b8a9b1dSJustin T. Gibbs static union ccb * 4524227d67aaSAlexander Motin xpt_get_ccb_nowait(struct cam_periph *periph) 45258b8a9b1dSJustin T. Gibbs { 45268b8a9b1dSJustin T. Gibbs union ccb *new_ccb; 45278b8a9b1dSJustin T. Gibbs 4528d3995fddSBenno Rice new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT); 4529227d67aaSAlexander Motin if (new_ccb == NULL) 45308b8a9b1dSJustin T. Gibbs return (NULL); 4531227d67aaSAlexander Motin periph->periph_allocated++; 4532227d67aaSAlexander Motin cam_ccbq_take_opening(&periph->path->device->ccbq); 45338b8a9b1dSJustin T. Gibbs return (new_ccb); 45348b8a9b1dSJustin T. Gibbs } 45358b8a9b1dSJustin T. Gibbs 4536227d67aaSAlexander Motin static union ccb * 4537227d67aaSAlexander Motin xpt_get_ccb(struct cam_periph *periph) 4538227d67aaSAlexander Motin { 4539227d67aaSAlexander Motin union ccb *new_ccb; 4540227d67aaSAlexander Motin 4541227d67aaSAlexander Motin cam_periph_unlock(periph); 4542d3995fddSBenno Rice new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK); 4543227d67aaSAlexander Motin cam_periph_lock(periph); 4544227d67aaSAlexander Motin periph->periph_allocated++; 4545227d67aaSAlexander Motin cam_ccbq_take_opening(&periph->path->device->ccbq); 4546227d67aaSAlexander Motin return (new_ccb); 4547227d67aaSAlexander Motin } 4548227d67aaSAlexander Motin 4549227d67aaSAlexander Motin union ccb * 4550227d67aaSAlexander Motin cam_periph_getccb(struct cam_periph *periph, u_int32_t priority) 4551227d67aaSAlexander Motin { 4552227d67aaSAlexander Motin struct ccb_hdr *ccb_h; 4553227d67aaSAlexander Motin 4554227d67aaSAlexander Motin CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n")); 4555227d67aaSAlexander Motin cam_periph_assert(periph, MA_OWNED); 4556227d67aaSAlexander Motin while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL || 4557227d67aaSAlexander Motin ccb_h->pinfo.priority != priority) { 4558227d67aaSAlexander Motin if (priority < periph->immediate_priority) { 4559227d67aaSAlexander Motin periph->immediate_priority = priority; 4560227d67aaSAlexander Motin xpt_run_allocq(periph, 0); 4561227d67aaSAlexander Motin } else 4562227d67aaSAlexander Motin cam_periph_sleep(periph, &periph->ccb_list, PRIBIO, 4563227d67aaSAlexander Motin "cgticb", 0); 4564227d67aaSAlexander Motin } 4565227d67aaSAlexander Motin SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle); 4566227d67aaSAlexander Motin return ((union ccb *)ccb_h); 4567227d67aaSAlexander Motin } 4568227d67aaSAlexander Motin 4569227d67aaSAlexander Motin static void 4570227d67aaSAlexander Motin xpt_acquire_bus(struct cam_eb *bus) 4571227d67aaSAlexander Motin { 4572227d67aaSAlexander Motin 4573227d67aaSAlexander Motin xpt_lock_buses(); 4574227d67aaSAlexander Motin bus->refcount++; 4575227d67aaSAlexander Motin xpt_unlock_buses(); 4576227d67aaSAlexander Motin } 4577227d67aaSAlexander Motin 4578a5479bc5SJustin T. Gibbs static void 4579a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus) 4580a5479bc5SJustin T. Gibbs { 4581a5479bc5SJustin T. Gibbs 45829a7c2696SAlexander Motin xpt_lock_buses(); 458315975b7bSMatt Jacob KASSERT(bus->refcount >= 1, ("bus->refcount >= 1")); 4584dcdf6e74SAlexander Motin if (--bus->refcount > 0) { 4585dcdf6e74SAlexander Motin xpt_unlock_buses(); 4586dcdf6e74SAlexander Motin return; 4587dcdf6e74SAlexander Motin } 45882b83592fSScott Long TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links); 45892b83592fSScott Long xsoftc.bus_generation++; 45909a7c2696SAlexander Motin xpt_unlock_buses(); 4591227d67aaSAlexander Motin KASSERT(TAILQ_EMPTY(&bus->et_entries), 4592227d67aaSAlexander Motin ("destroying bus, but target list is not empty")); 4593fa6099fdSEdward Tomasz Napierala cam_sim_release(bus->sim); 4594227d67aaSAlexander Motin mtx_destroy(&bus->eb_mtx); 4595362abc44STai-hwa Liang free(bus, M_CAMXPT); 4596a5479bc5SJustin T. Gibbs } 45978b8a9b1dSJustin T. Gibbs 45988b8a9b1dSJustin T. Gibbs static struct cam_et * 45998b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id) 46008b8a9b1dSJustin T. Gibbs { 4601dcdf6e74SAlexander Motin struct cam_et *cur_target, *target; 46028b8a9b1dSJustin T. Gibbs 4603227d67aaSAlexander Motin mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED); 4604227d67aaSAlexander Motin mtx_assert(&bus->eb_mtx, MA_OWNED); 46053501942bSJustin T. Gibbs target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, 46063501942bSJustin T. Gibbs M_NOWAIT|M_ZERO); 4607dcdf6e74SAlexander Motin if (target == NULL) 4608dcdf6e74SAlexander Motin return (NULL); 46098b8a9b1dSJustin T. Gibbs 4610434bbf6eSJustin T. Gibbs TAILQ_INIT(&target->ed_entries); 46118b8a9b1dSJustin T. Gibbs target->bus = bus; 46128b8a9b1dSJustin T. Gibbs target->target_id = target_id; 46138b8a9b1dSJustin T. Gibbs target->refcount = 1; 4614434bbf6eSJustin T. Gibbs target->generation = 0; 461582b361b1SMatt Jacob target->luns = NULL; 4616227d67aaSAlexander Motin mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF); 4617434bbf6eSJustin T. Gibbs timevalclear(&target->last_reset); 4618a5479bc5SJustin T. Gibbs /* 4619a5479bc5SJustin T. Gibbs * Hold a reference to our parent bus so it 4620a5479bc5SJustin T. Gibbs * will not go away before we do. 4621a5479bc5SJustin T. Gibbs */ 4622a5479bc5SJustin T. Gibbs bus->refcount++; 46238b8a9b1dSJustin T. Gibbs 46248b8a9b1dSJustin T. Gibbs /* Insertion sort into our bus's target list */ 46258b8a9b1dSJustin T. Gibbs cur_target = TAILQ_FIRST(&bus->et_entries); 46268b8a9b1dSJustin T. Gibbs while (cur_target != NULL && cur_target->target_id < target_id) 46278b8a9b1dSJustin T. Gibbs cur_target = TAILQ_NEXT(cur_target, links); 46288b8a9b1dSJustin T. Gibbs if (cur_target != NULL) { 46298b8a9b1dSJustin T. Gibbs TAILQ_INSERT_BEFORE(cur_target, target, links); 46308b8a9b1dSJustin T. Gibbs } else { 46318b8a9b1dSJustin T. Gibbs TAILQ_INSERT_TAIL(&bus->et_entries, target, links); 46328b8a9b1dSJustin T. Gibbs } 4633a5479bc5SJustin T. Gibbs bus->generation++; 46348b8a9b1dSJustin T. Gibbs return (target); 46358b8a9b1dSJustin T. Gibbs } 46368b8a9b1dSJustin T. Gibbs 4637a5479bc5SJustin T. Gibbs static void 4638227d67aaSAlexander Motin xpt_acquire_target(struct cam_et *target) 4639227d67aaSAlexander Motin { 4640227d67aaSAlexander Motin struct cam_eb *bus = target->bus; 4641227d67aaSAlexander Motin 4642227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 4643227d67aaSAlexander Motin target->refcount++; 4644227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4645227d67aaSAlexander Motin } 4646227d67aaSAlexander Motin 4647227d67aaSAlexander Motin static void 4648f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target) 46498b8a9b1dSJustin T. Gibbs { 4650227d67aaSAlexander Motin struct cam_eb *bus = target->bus; 4651a5479bc5SJustin T. Gibbs 4652227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 4653227d67aaSAlexander Motin if (--target->refcount > 0) { 4654227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4655dcdf6e74SAlexander Motin return; 4656227d67aaSAlexander Motin } 4657227d67aaSAlexander Motin TAILQ_REMOVE(&bus->et_entries, target, links); 4658227d67aaSAlexander Motin bus->generation++; 4659227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4660dcdf6e74SAlexander Motin KASSERT(TAILQ_EMPTY(&target->ed_entries), 4661227d67aaSAlexander Motin ("destroying target, but device list is not empty")); 4662227d67aaSAlexander Motin xpt_release_bus(bus); 4663227d67aaSAlexander Motin mtx_destroy(&target->luns_mtx); 466482b361b1SMatt Jacob if (target->luns) 466582b361b1SMatt Jacob free(target->luns, M_CAMXPT); 4666362abc44STai-hwa Liang free(target, M_CAMXPT); 466777dc25ccSScott Long } 46688b8a9b1dSJustin T. Gibbs 46698b8a9b1dSJustin T. Gibbs static struct cam_ed * 467052c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target, 467152c9ce25SScott Long lun_id_t lun_id) 467252c9ce25SScott Long { 4673dcdf6e74SAlexander Motin struct cam_ed *device; 467452c9ce25SScott Long 467552c9ce25SScott Long device = xpt_alloc_device(bus, target, lun_id); 467652c9ce25SScott Long if (device == NULL) 467752c9ce25SScott Long return (NULL); 467852c9ce25SScott Long 467952c9ce25SScott Long device->mintags = 1; 468052c9ce25SScott Long device->maxtags = 1; 468152c9ce25SScott Long return (device); 468252c9ce25SScott Long } 468352c9ce25SScott Long 4684227d67aaSAlexander Motin static void 4685227d67aaSAlexander Motin xpt_destroy_device(void *context, int pending) 4686227d67aaSAlexander Motin { 4687227d67aaSAlexander Motin struct cam_ed *device = context; 4688227d67aaSAlexander Motin 4689227d67aaSAlexander Motin mtx_lock(&device->device_mtx); 4690227d67aaSAlexander Motin mtx_destroy(&device->device_mtx); 4691227d67aaSAlexander Motin free(device, M_CAMDEV); 4692227d67aaSAlexander Motin } 4693227d67aaSAlexander Motin 469452c9ce25SScott Long struct cam_ed * 46958b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 46968b8a9b1dSJustin T. Gibbs { 4697dcdf6e74SAlexander Motin struct cam_ed *cur_device, *device; 46988b8a9b1dSJustin T. Gibbs struct cam_devq *devq; 4699a5479bc5SJustin T. Gibbs cam_status status; 47008b8a9b1dSJustin T. Gibbs 4701227d67aaSAlexander Motin mtx_assert(&bus->eb_mtx, MA_OWNED); 47028b8a9b1dSJustin T. Gibbs /* Make space for us in the device queue on our bus */ 47038b8a9b1dSJustin T. Gibbs devq = bus->sim->devq; 4704227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4705cccf4220SAlexander Motin status = cam_devq_resize(devq, devq->send_queue.array_size + 1); 4706227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 4707dcdf6e74SAlexander Motin if (status != CAM_REQ_CMP) 4708dcdf6e74SAlexander Motin return (NULL); 47098b8a9b1dSJustin T. Gibbs 47108b8a9b1dSJustin T. Gibbs device = (struct cam_ed *)malloc(sizeof(*device), 4711596ee08fSAlexander Motin M_CAMDEV, M_NOWAIT|M_ZERO); 4712dcdf6e74SAlexander Motin if (device == NULL) 4713dcdf6e74SAlexander Motin return (NULL); 47148b8a9b1dSJustin T. Gibbs 4715227d67aaSAlexander Motin cam_init_pinfo(&device->devq_entry); 47168b8a9b1dSJustin T. Gibbs device->target = target; 47178b8a9b1dSJustin T. Gibbs device->lun_id = lun_id; 47182b83592fSScott Long device->sim = bus->sim; 47198b8a9b1dSJustin T. Gibbs if (cam_ccbq_init(&device->ccbq, 47208b8a9b1dSJustin T. Gibbs bus->sim->max_dev_openings) != 0) { 4721596ee08fSAlexander Motin free(device, M_CAMDEV); 47228b8a9b1dSJustin T. Gibbs return (NULL); 47238b8a9b1dSJustin T. Gibbs } 4724434bbf6eSJustin T. Gibbs SLIST_INIT(&device->asyncs); 4725434bbf6eSJustin T. Gibbs SLIST_INIT(&device->periphs); 4726434bbf6eSJustin T. Gibbs device->generation = 0; 4727434bbf6eSJustin T. Gibbs device->flags = CAM_DEV_UNCONFIGURED; 4728434bbf6eSJustin T. Gibbs device->tag_delay_count = 0; 4729df8f9080SJustin T. Gibbs device->tag_saved_openings = 0; 4730434bbf6eSJustin T. Gibbs device->refcount = 1; 4731227d67aaSAlexander Motin mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF); 4732227d67aaSAlexander Motin callout_init_mtx(&device->callout, &devq->send_mtx, 0); 4733227d67aaSAlexander Motin TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device); 4734227d67aaSAlexander Motin /* 4735227d67aaSAlexander Motin * Hold a reference to our parent bus so it 4736227d67aaSAlexander Motin * will not go away before we do. 4737227d67aaSAlexander Motin */ 4738227d67aaSAlexander Motin target->refcount++; 4739434bbf6eSJustin T. Gibbs 4740dcdf6e74SAlexander Motin cur_device = TAILQ_FIRST(&target->ed_entries); 4741dcdf6e74SAlexander Motin while (cur_device != NULL && cur_device->lun_id < lun_id) 4742dcdf6e74SAlexander Motin cur_device = TAILQ_NEXT(cur_device, links); 4743dcdf6e74SAlexander Motin if (cur_device != NULL) 4744dcdf6e74SAlexander Motin TAILQ_INSERT_BEFORE(cur_device, device, links); 4745dcdf6e74SAlexander Motin else 4746dcdf6e74SAlexander Motin TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 4747dcdf6e74SAlexander Motin target->generation++; 47488b8a9b1dSJustin T. Gibbs return (device); 47498b8a9b1dSJustin T. Gibbs } 47508b8a9b1dSJustin T. Gibbs 4751f98d7a47SAlexander Motin void 4752f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device) 47538b8a9b1dSJustin T. Gibbs { 4754227d67aaSAlexander Motin struct cam_eb *bus = device->target->bus; 47558b8a9b1dSJustin T. Gibbs 4756227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 4757f98d7a47SAlexander Motin device->refcount++; 4758227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4759f98d7a47SAlexander Motin } 4760f98d7a47SAlexander Motin 4761f98d7a47SAlexander Motin void 4762f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device) 4763f98d7a47SAlexander Motin { 4764227d67aaSAlexander Motin struct cam_eb *bus = device->target->bus; 47658b8a9b1dSJustin T. Gibbs struct cam_devq *devq; 47668b8a9b1dSJustin T. Gibbs 4767227d67aaSAlexander Motin mtx_lock(&bus->eb_mtx); 4768227d67aaSAlexander Motin if (--device->refcount > 0) { 4769227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4770dcdf6e74SAlexander Motin return; 4771227d67aaSAlexander Motin } 4772227d67aaSAlexander Motin 4773227d67aaSAlexander Motin TAILQ_REMOVE(&device->target->ed_entries, device,links); 4774227d67aaSAlexander Motin device->target->generation++; 4775227d67aaSAlexander Motin mtx_unlock(&bus->eb_mtx); 4776227d67aaSAlexander Motin 4777227d67aaSAlexander Motin /* Release our slot in the devq */ 4778227d67aaSAlexander Motin devq = bus->sim->devq; 4779227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 4780227d67aaSAlexander Motin cam_devq_resize(devq, devq->send_queue.array_size - 1); 4781227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 4782dcdf6e74SAlexander Motin 4783dcdf6e74SAlexander Motin KASSERT(SLIST_EMPTY(&device->periphs), 4784227d67aaSAlexander Motin ("destroying device, but periphs list is not empty")); 4785227d67aaSAlexander Motin KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX, 4786227d67aaSAlexander Motin ("destroying device while still queued for ccbs")); 47872cefde5fSJustin T. Gibbs 47882cefde5fSJustin T. Gibbs if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) 47892b83592fSScott Long callout_stop(&device->callout); 47902cefde5fSJustin T. Gibbs 4791227d67aaSAlexander Motin xpt_release_target(device->target); 4792227d67aaSAlexander Motin 479320a7933fSAlexander Motin cam_ccbq_fini(&device->ccbq); 4794e6bd5983SKenneth D. Merry /* 4795e6bd5983SKenneth D. Merry * Free allocated memory. free(9) does nothing if the 4796e6bd5983SKenneth D. Merry * supplied pointer is NULL, so it is safe to call without 4797e6bd5983SKenneth D. Merry * checking. 4798e6bd5983SKenneth D. Merry */ 4799e6bd5983SKenneth D. Merry free(device->supported_vpds, M_CAMXPT); 4800e6bd5983SKenneth D. Merry free(device->device_id, M_CAMXPT); 48013bba3152SKenneth D. Merry free(device->ext_inq, M_CAMXPT); 4802e6bd5983SKenneth D. Merry free(device->physpath, M_CAMXPT); 4803e6bd5983SKenneth D. Merry free(device->rcap_buf, M_CAMXPT); 4804e6bd5983SKenneth D. Merry free(device->serial_num, M_CAMXPT); 4805227d67aaSAlexander Motin taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task); 48068b8a9b1dSJustin T. Gibbs } 48078b8a9b1dSJustin T. Gibbs 480852c9ce25SScott Long u_int32_t 48098b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings) 48108b8a9b1dSJustin T. Gibbs { 48118b8a9b1dSJustin T. Gibbs int result; 48128b8a9b1dSJustin T. Gibbs struct cam_ed *dev; 48138b8a9b1dSJustin T. Gibbs 48148b8a9b1dSJustin T. Gibbs dev = path->device; 4815227d67aaSAlexander Motin mtx_lock(&dev->sim->devq->send_mtx); 48168b8a9b1dSJustin T. Gibbs result = cam_ccbq_resize(&dev->ccbq, newopenings); 4817227d67aaSAlexander Motin mtx_unlock(&dev->sim->devq->send_mtx); 4818df8f9080SJustin T. Gibbs if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 4819df8f9080SJustin T. Gibbs || (dev->inq_flags & SID_CmdQue) != 0) 4820df8f9080SJustin T. Gibbs dev->tag_saved_openings = newopenings; 48218b8a9b1dSJustin T. Gibbs return (result); 48228b8a9b1dSJustin T. Gibbs } 48238b8a9b1dSJustin T. Gibbs 48248b8a9b1dSJustin T. Gibbs static struct cam_eb * 48258b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id) 48268b8a9b1dSJustin T. Gibbs { 48278b8a9b1dSJustin T. Gibbs struct cam_eb *bus; 48288b8a9b1dSJustin T. Gibbs 48299a7c2696SAlexander Motin xpt_lock_buses(); 48302b83592fSScott Long for (bus = TAILQ_FIRST(&xsoftc.xpt_busses); 48318b8a9b1dSJustin T. Gibbs bus != NULL; 48328b8a9b1dSJustin T. Gibbs bus = TAILQ_NEXT(bus, links)) { 4833a5479bc5SJustin T. Gibbs if (bus->path_id == path_id) { 4834a5479bc5SJustin T. Gibbs bus->refcount++; 48358b8a9b1dSJustin T. Gibbs break; 48368b8a9b1dSJustin T. Gibbs } 4837a5479bc5SJustin T. Gibbs } 48389a7c2696SAlexander Motin xpt_unlock_buses(); 48398b8a9b1dSJustin T. Gibbs return (bus); 48408b8a9b1dSJustin T. Gibbs } 48418b8a9b1dSJustin T. Gibbs 48428b8a9b1dSJustin T. Gibbs static struct cam_et * 48438b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t target_id) 48448b8a9b1dSJustin T. Gibbs { 48458b8a9b1dSJustin T. Gibbs struct cam_et *target; 48468b8a9b1dSJustin T. Gibbs 4847227d67aaSAlexander Motin mtx_assert(&bus->eb_mtx, MA_OWNED); 48488b8a9b1dSJustin T. Gibbs for (target = TAILQ_FIRST(&bus->et_entries); 48498b8a9b1dSJustin T. Gibbs target != NULL; 48508b8a9b1dSJustin T. Gibbs target = TAILQ_NEXT(target, links)) { 48518b8a9b1dSJustin T. Gibbs if (target->target_id == target_id) { 48528b8a9b1dSJustin T. Gibbs target->refcount++; 48538b8a9b1dSJustin T. Gibbs break; 48548b8a9b1dSJustin T. Gibbs } 48558b8a9b1dSJustin T. Gibbs } 48568b8a9b1dSJustin T. Gibbs return (target); 48578b8a9b1dSJustin T. Gibbs } 48588b8a9b1dSJustin T. Gibbs 48598b8a9b1dSJustin T. Gibbs static struct cam_ed * 48608b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id) 48618b8a9b1dSJustin T. Gibbs { 48628b8a9b1dSJustin T. Gibbs struct cam_ed *device; 48638b8a9b1dSJustin T. Gibbs 4864227d67aaSAlexander Motin mtx_assert(&target->bus->eb_mtx, MA_OWNED); 48658b8a9b1dSJustin T. Gibbs for (device = TAILQ_FIRST(&target->ed_entries); 48668b8a9b1dSJustin T. Gibbs device != NULL; 48678b8a9b1dSJustin T. Gibbs device = TAILQ_NEXT(device, links)) { 48688b8a9b1dSJustin T. Gibbs if (device->lun_id == lun_id) { 48698b8a9b1dSJustin T. Gibbs device->refcount++; 48708b8a9b1dSJustin T. Gibbs break; 48718b8a9b1dSJustin T. Gibbs } 48728b8a9b1dSJustin T. Gibbs } 48738b8a9b1dSJustin T. Gibbs return (device); 48748b8a9b1dSJustin T. Gibbs } 48758b8a9b1dSJustin T. Gibbs 487630a4094fSAlexander Motin void 4877f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path) 4878f0adc790SJustin T. Gibbs { 4879fd21cc5eSJustin T. Gibbs struct ccb_relsim crs; 4880fd21cc5eSJustin T. Gibbs struct cam_ed *device; 4881fd21cc5eSJustin T. Gibbs struct cam_sim *sim; 4882fd21cc5eSJustin T. Gibbs int newopenings; 4883fd21cc5eSJustin T. Gibbs 4884fd21cc5eSJustin T. Gibbs device = path->device; 4885fd21cc5eSJustin T. Gibbs sim = path->bus->sim; 4886fd21cc5eSJustin T. Gibbs device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 4887fd21cc5eSJustin T. Gibbs xpt_freeze_devq(path, /*count*/1); 4888fd21cc5eSJustin T. Gibbs device->inq_flags |= SID_CmdQue; 4889df8f9080SJustin T. Gibbs if (device->tag_saved_openings != 0) 4890df8f9080SJustin T. Gibbs newopenings = device->tag_saved_openings; 4891df8f9080SJustin T. Gibbs else 489252c9ce25SScott Long newopenings = min(device->maxtags, 4893df8f9080SJustin T. Gibbs sim->max_tagged_dev_openings); 4894f0adc790SJustin T. Gibbs xpt_dev_ccbq_resize(path, newopenings); 4895581b2e78SAlexander Motin xpt_async(AC_GETDEV_CHANGED, path, NULL); 4896bbfa4aa1SAlexander Motin xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 4897fd21cc5eSJustin T. Gibbs crs.ccb_h.func_code = XPT_REL_SIMQ; 4898fd21cc5eSJustin T. Gibbs crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 4899fd21cc5eSJustin T. Gibbs crs.openings 4900fd21cc5eSJustin T. Gibbs = crs.release_timeout 4901fd21cc5eSJustin T. Gibbs = crs.qfrozen_cnt 4902fd21cc5eSJustin T. Gibbs = 0; 4903fd21cc5eSJustin T. Gibbs xpt_action((union ccb *)&crs); 4904fd21cc5eSJustin T. Gibbs } 4905fd21cc5eSJustin T. Gibbs 490630a4094fSAlexander Motin void 490730a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path) 490830a4094fSAlexander Motin { 490930a4094fSAlexander Motin struct ccb_relsim crs; 491030a4094fSAlexander Motin struct cam_ed *device; 491130a4094fSAlexander Motin struct cam_sim *sim; 491230a4094fSAlexander Motin 491330a4094fSAlexander Motin device = path->device; 491430a4094fSAlexander Motin sim = path->bus->sim; 491530a4094fSAlexander Motin device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 491630a4094fSAlexander Motin device->tag_delay_count = 0; 491730a4094fSAlexander Motin xpt_freeze_devq(path, /*count*/1); 491830a4094fSAlexander Motin device->inq_flags &= ~SID_CmdQue; 491930a4094fSAlexander Motin xpt_dev_ccbq_resize(path, sim->max_dev_openings); 4920581b2e78SAlexander Motin xpt_async(AC_GETDEV_CHANGED, path, NULL); 492130a4094fSAlexander Motin xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 492230a4094fSAlexander Motin crs.ccb_h.func_code = XPT_REL_SIMQ; 492330a4094fSAlexander Motin crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 492430a4094fSAlexander Motin crs.openings 492530a4094fSAlexander Motin = crs.release_timeout 492630a4094fSAlexander Motin = crs.qfrozen_cnt 492730a4094fSAlexander Motin = 0; 492830a4094fSAlexander Motin xpt_action((union ccb *)&crs); 492930a4094fSAlexander Motin } 493030a4094fSAlexander Motin 493183c5d981SAlexander Motin static void 493283c5d981SAlexander Motin xpt_boot_delay(void *arg) 49338b8a9b1dSJustin T. Gibbs { 49342b83592fSScott Long 493583c5d981SAlexander Motin xpt_release_boot(); 49368b8a9b1dSJustin T. Gibbs } 49378b8a9b1dSJustin T. Gibbs 49388b8a9b1dSJustin T. Gibbs static void 49398b8a9b1dSJustin T. Gibbs xpt_config(void *arg) 49408b8a9b1dSJustin T. Gibbs { 49413393f8daSKenneth D. Merry /* 49423393f8daSKenneth D. Merry * Now that interrupts are enabled, go find our devices 49433393f8daSKenneth D. Merry */ 4944227d67aaSAlexander Motin if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq")) 4945227d67aaSAlexander Motin printf("xpt_config: failed to create taskqueue thread.\n"); 49468b8a9b1dSJustin T. Gibbs 4947f0f25b9cSAlexander Motin /* Setup debugging path */ 49488b8a9b1dSJustin T. Gibbs if (cam_dflags != CAM_DEBUG_NONE) { 4949227d67aaSAlexander Motin if (xpt_create_path(&cam_dpath, NULL, 49508b8a9b1dSJustin T. Gibbs CAM_DEBUG_BUS, CAM_DEBUG_TARGET, 49518b8a9b1dSJustin T. Gibbs CAM_DEBUG_LUN) != CAM_REQ_CMP) { 49528b8a9b1dSJustin T. Gibbs printf("xpt_config: xpt_create_path() failed for debug" 49538b8a9b1dSJustin T. Gibbs " target %d:%d:%d, debugging disabled\n", 49548b8a9b1dSJustin T. Gibbs CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN); 49558b8a9b1dSJustin T. Gibbs cam_dflags = CAM_DEBUG_NONE; 49568b8a9b1dSJustin T. Gibbs } 49578b8a9b1dSJustin T. Gibbs } else 49588b8a9b1dSJustin T. Gibbs cam_dpath = NULL; 49598b8a9b1dSJustin T. Gibbs 496083c5d981SAlexander Motin periphdriver_init(1); 496183c5d981SAlexander Motin xpt_hold_boot(); 496283c5d981SAlexander Motin callout_init(&xsoftc.boot_callout, 1); 496385c9dd9dSSteven Hartland callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0, 496485c9dd9dSSteven Hartland xpt_boot_delay, NULL, 0); 496583c5d981SAlexander Motin /* Fire up rescan thread. */ 4966227d67aaSAlexander Motin if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0, 4967227d67aaSAlexander Motin "cam", "scanner")) { 49686f15a274SAlexander Motin printf("xpt_config: failed to create rescan thread.\n"); 49691e637ba6SAlexander Motin } 497083c5d981SAlexander Motin } 49718b8a9b1dSJustin T. Gibbs 497283c5d981SAlexander Motin void 497383c5d981SAlexander Motin xpt_hold_boot(void) 497483c5d981SAlexander Motin { 497583c5d981SAlexander Motin xpt_lock_buses(); 497683c5d981SAlexander Motin xsoftc.buses_to_config++; 497783c5d981SAlexander Motin xpt_unlock_buses(); 497883c5d981SAlexander Motin } 497983c5d981SAlexander Motin 498083c5d981SAlexander Motin void 498183c5d981SAlexander Motin xpt_release_boot(void) 498283c5d981SAlexander Motin { 498383c5d981SAlexander Motin xpt_lock_buses(); 498483c5d981SAlexander Motin xsoftc.buses_to_config--; 498583c5d981SAlexander Motin if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) { 498683c5d981SAlexander Motin struct xpt_task *task; 498783c5d981SAlexander Motin 498883c5d981SAlexander Motin xsoftc.buses_config_done = 1; 498983c5d981SAlexander Motin xpt_unlock_buses(); 4990ecdf1113SJustin T. Gibbs /* Call manually because we don't have any busses */ 499183c5d981SAlexander Motin task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT); 499283c5d981SAlexander Motin if (task != NULL) { 499383c5d981SAlexander Motin TASK_INIT(&task->task, 0, xpt_finishconfig_task, task); 499483c5d981SAlexander Motin taskqueue_enqueue(taskqueue_thread, &task->task); 49952863f7b1SJustin T. Gibbs } 499683c5d981SAlexander Motin } else 499783c5d981SAlexander Motin xpt_unlock_buses(); 49982863f7b1SJustin T. Gibbs } 49998b8a9b1dSJustin T. Gibbs 50008b8a9b1dSJustin T. Gibbs /* 50018b8a9b1dSJustin T. Gibbs * If the given device only has one peripheral attached to it, and if that 50028b8a9b1dSJustin T. Gibbs * peripheral is the passthrough driver, announce it. This insures that the 50038b8a9b1dSJustin T. Gibbs * user sees some sort of announcement for every peripheral in their system. 50048b8a9b1dSJustin T. Gibbs */ 50058b8a9b1dSJustin T. Gibbs static int 50068b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg) 50078b8a9b1dSJustin T. Gibbs { 50088b8a9b1dSJustin T. Gibbs struct cam_periph *periph; 50098b8a9b1dSJustin T. Gibbs int i; 50108b8a9b1dSJustin T. Gibbs 50118b8a9b1dSJustin T. Gibbs for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL; 50128b8a9b1dSJustin T. Gibbs periph = SLIST_NEXT(periph, periph_links), i++); 50138b8a9b1dSJustin T. Gibbs 50148b8a9b1dSJustin T. Gibbs periph = SLIST_FIRST(&device->periphs); 50158b8a9b1dSJustin T. Gibbs if ((i == 1) 50168b8a9b1dSJustin T. Gibbs && (strncmp(periph->periph_name, "pass", 4) == 0)) 50178b8a9b1dSJustin T. Gibbs xpt_announce_periph(periph, NULL); 50188b8a9b1dSJustin T. Gibbs 50198b8a9b1dSJustin T. Gibbs return(1); 50208b8a9b1dSJustin T. Gibbs } 50218b8a9b1dSJustin T. Gibbs 50228b8a9b1dSJustin T. Gibbs static void 50232b83592fSScott Long xpt_finishconfig_task(void *context, int pending) 50248b8a9b1dSJustin T. Gibbs { 50258b8a9b1dSJustin T. Gibbs 502683c5d981SAlexander Motin periphdriver_init(2); 50272b83592fSScott Long /* 50282b83592fSScott Long * Check for devices with no "standard" peripheral driver 50292b83592fSScott Long * attached. For any devices like that, announce the 50302b83592fSScott Long * passthrough driver so the user will see something. 50312b83592fSScott Long */ 50323089bb2eSAlexander Motin if (!bootverbose) 50332b83592fSScott Long xpt_for_all_devices(xptpassannouncefunc, NULL); 50342b83592fSScott Long 50352b83592fSScott Long /* Release our hook so that the boot can continue. */ 50362b83592fSScott Long config_intrhook_disestablish(xsoftc.xpt_config_hook); 50370dd50e9bSScott Long free(xsoftc.xpt_config_hook, M_CAMXPT); 50382b83592fSScott Long xsoftc.xpt_config_hook = NULL; 50392b83592fSScott Long 50402b83592fSScott Long free(context, M_CAMXPT); 50412b83592fSScott Long } 50422b83592fSScott Long 504385d92640SScott Long cam_status 504485d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg, 504585d92640SScott Long struct cam_path *path) 504685d92640SScott Long { 504785d92640SScott Long struct ccb_setasync csa; 504885d92640SScott Long cam_status status; 504985d92640SScott Long int xptpath = 0; 505085d92640SScott Long 505185d92640SScott Long if (path == NULL) { 505285d92640SScott Long status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 505385d92640SScott Long CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 5054227d67aaSAlexander Motin if (status != CAM_REQ_CMP) 505585d92640SScott Long return (status); 5056227d67aaSAlexander Motin xpt_path_lock(path); 505785d92640SScott Long xptpath = 1; 505885d92640SScott Long } 505985d92640SScott Long 506083c5d981SAlexander Motin xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL); 506185d92640SScott Long csa.ccb_h.func_code = XPT_SASYNC_CB; 506285d92640SScott Long csa.event_enable = event; 506385d92640SScott Long csa.callback = cbfunc; 506485d92640SScott Long csa.callback_arg = cbarg; 506585d92640SScott Long xpt_action((union ccb *)&csa); 506685d92640SScott Long status = csa.ccb_h.status; 50673501942bSJustin T. Gibbs 506885d92640SScott Long if (xptpath) { 5069227d67aaSAlexander Motin xpt_path_unlock(path); 507085d92640SScott Long xpt_free_path(path); 50713501942bSJustin T. Gibbs } 50727685edecSAlexander Motin 50737685edecSAlexander Motin if ((status == CAM_REQ_CMP) && 50747685edecSAlexander Motin (csa.event_enable & AC_FOUND_DEVICE)) { 50757685edecSAlexander Motin /* 50767685edecSAlexander Motin * Get this peripheral up to date with all 50777685edecSAlexander Motin * the currently existing devices. 50787685edecSAlexander Motin */ 50797685edecSAlexander Motin xpt_for_all_devices(xptsetasyncfunc, &csa); 50807685edecSAlexander Motin } 50817685edecSAlexander Motin if ((status == CAM_REQ_CMP) && 50827685edecSAlexander Motin (csa.event_enable & AC_PATH_REGISTERED)) { 50837685edecSAlexander Motin /* 50847685edecSAlexander Motin * Get this peripheral up to date with all 50857685edecSAlexander Motin * the currently existing busses. 50867685edecSAlexander Motin */ 50877685edecSAlexander Motin xpt_for_all_busses(xptsetasyncbusfunc, &csa); 50887685edecSAlexander Motin } 50893501942bSJustin T. Gibbs 509085d92640SScott Long return (status); 509185d92640SScott Long } 509285d92640SScott Long 50938b8a9b1dSJustin T. Gibbs static void 50948b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb) 50958b8a9b1dSJustin T. Gibbs { 50968b8a9b1dSJustin T. Gibbs CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n")); 50978b8a9b1dSJustin T. Gibbs 50988b8a9b1dSJustin T. Gibbs switch (work_ccb->ccb_h.func_code) { 50998b8a9b1dSJustin T. Gibbs /* Common cases first */ 51008b8a9b1dSJustin T. Gibbs case XPT_PATH_INQ: /* Path routing inquiry */ 51018b8a9b1dSJustin T. Gibbs { 51028b8a9b1dSJustin T. Gibbs struct ccb_pathinq *cpi; 51038b8a9b1dSJustin T. Gibbs 51048b8a9b1dSJustin T. Gibbs cpi = &work_ccb->cpi; 51058b8a9b1dSJustin T. Gibbs cpi->version_num = 1; /* XXX??? */ 51068b8a9b1dSJustin T. Gibbs cpi->hba_inquiry = 0; 51078b8a9b1dSJustin T. Gibbs cpi->target_sprt = 0; 51088b8a9b1dSJustin T. Gibbs cpi->hba_misc = 0; 51098b8a9b1dSJustin T. Gibbs cpi->hba_eng_cnt = 0; 51108b8a9b1dSJustin T. Gibbs cpi->max_target = 0; 51118b8a9b1dSJustin T. Gibbs cpi->max_lun = 0; 51128b8a9b1dSJustin T. Gibbs cpi->initiator_id = 0; 51138b8a9b1dSJustin T. Gibbs strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 51148b8a9b1dSJustin T. Gibbs strncpy(cpi->hba_vid, "", HBA_IDLEN); 51158b8a9b1dSJustin T. Gibbs strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 51168b8a9b1dSJustin T. Gibbs cpi->unit_number = sim->unit_number; 51178b8a9b1dSJustin T. Gibbs cpi->bus_id = sim->bus_id; 51189deea857SKenneth D. Merry cpi->base_transfer_speed = 0; 51193393f8daSKenneth D. Merry cpi->protocol = PROTO_UNSPECIFIED; 51203393f8daSKenneth D. Merry cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 51213393f8daSKenneth D. Merry cpi->transport = XPORT_UNSPECIFIED; 51223393f8daSKenneth D. Merry cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 51238b8a9b1dSJustin T. Gibbs cpi->ccb_h.status = CAM_REQ_CMP; 51248b8a9b1dSJustin T. Gibbs xpt_done(work_ccb); 51258b8a9b1dSJustin T. Gibbs break; 51268b8a9b1dSJustin T. Gibbs } 51278b8a9b1dSJustin T. Gibbs default: 51288b8a9b1dSJustin T. Gibbs work_ccb->ccb_h.status = CAM_REQ_INVALID; 51298b8a9b1dSJustin T. Gibbs xpt_done(work_ccb); 51308b8a9b1dSJustin T. Gibbs break; 51318b8a9b1dSJustin T. Gibbs } 51328b8a9b1dSJustin T. Gibbs } 51338b8a9b1dSJustin T. Gibbs 51348b8a9b1dSJustin T. Gibbs /* 5135434bbf6eSJustin T. Gibbs * The xpt as a "controller" has no interrupt sources, so polling 5136434bbf6eSJustin T. Gibbs * is a no-op. 5137434bbf6eSJustin T. Gibbs */ 5138434bbf6eSJustin T. Gibbs static void 5139434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim) 5140434bbf6eSJustin T. Gibbs { 5141434bbf6eSJustin T. Gibbs } 5142434bbf6eSJustin T. Gibbs 51432b83592fSScott Long void 51442b83592fSScott Long xpt_lock_buses(void) 51452b83592fSScott Long { 51462b83592fSScott Long mtx_lock(&xsoftc.xpt_topo_lock); 51472b83592fSScott Long } 51482b83592fSScott Long 51492b83592fSScott Long void 51502b83592fSScott Long xpt_unlock_buses(void) 51512b83592fSScott Long { 51522b83592fSScott Long mtx_unlock(&xsoftc.xpt_topo_lock); 51532b83592fSScott Long } 51542b83592fSScott Long 5155227d67aaSAlexander Motin struct mtx * 5156227d67aaSAlexander Motin xpt_path_mtx(struct cam_path *path) 51578b8a9b1dSJustin T. Gibbs { 5158227d67aaSAlexander Motin 5159227d67aaSAlexander Motin return (&path->device->device_mtx); 5160227d67aaSAlexander Motin } 5161227d67aaSAlexander Motin 5162227d67aaSAlexander Motin static void 5163227d67aaSAlexander Motin xpt_done_process(struct ccb_hdr *ccb_h) 5164227d67aaSAlexander Motin { 51652b83592fSScott Long struct cam_sim *sim; 5166227d67aaSAlexander Motin struct cam_devq *devq; 5167227d67aaSAlexander Motin struct mtx *mtx = NULL; 51688b8a9b1dSJustin T. Gibbs 51698b8a9b1dSJustin T. Gibbs if (ccb_h->flags & CAM_HIGH_POWER) { 51708b8a9b1dSJustin T. Gibbs struct highpowerlist *hphead; 5171ea541bfdSAlexander Motin struct cam_ed *device; 51728b8a9b1dSJustin T. Gibbs 5173daa5487fSAlexander Motin mtx_lock(&xsoftc.xpt_highpower_lock); 51742b83592fSScott Long hphead = &xsoftc.highpowerq; 51758b8a9b1dSJustin T. Gibbs 5176ea541bfdSAlexander Motin device = STAILQ_FIRST(hphead); 51778b8a9b1dSJustin T. Gibbs 51788b8a9b1dSJustin T. Gibbs /* 51798b8a9b1dSJustin T. Gibbs * Increment the count since this command is done. 51808b8a9b1dSJustin T. Gibbs */ 51812b83592fSScott Long xsoftc.num_highpower++; 51828b8a9b1dSJustin T. Gibbs 51838b8a9b1dSJustin T. Gibbs /* 51848b8a9b1dSJustin T. Gibbs * Any high powered commands queued up? 51858b8a9b1dSJustin T. Gibbs */ 5186ea541bfdSAlexander Motin if (device != NULL) { 51878b8a9b1dSJustin T. Gibbs 5188ea541bfdSAlexander Motin STAILQ_REMOVE_HEAD(hphead, highpowerq_entry); 5189daa5487fSAlexander Motin mtx_unlock(&xsoftc.xpt_highpower_lock); 51908b8a9b1dSJustin T. Gibbs 5191227d67aaSAlexander Motin mtx_lock(&device->sim->devq->send_mtx); 5192ea541bfdSAlexander Motin xpt_release_devq_device(device, 51932cefde5fSJustin T. Gibbs /*count*/1, /*runqueue*/TRUE); 5194227d67aaSAlexander Motin mtx_unlock(&device->sim->devq->send_mtx); 51952b83592fSScott Long } else 5196daa5487fSAlexander Motin mtx_unlock(&xsoftc.xpt_highpower_lock); 51978b8a9b1dSJustin T. Gibbs } 51982b83592fSScott Long 5199227d67aaSAlexander Motin sim = ccb_h->path->bus->sim; 5200227d67aaSAlexander Motin 5201227d67aaSAlexander Motin if (ccb_h->status & CAM_RELEASE_SIMQ) { 5202227d67aaSAlexander Motin xpt_release_simq(sim, /*run_queue*/FALSE); 5203227d67aaSAlexander Motin ccb_h->status &= ~CAM_RELEASE_SIMQ; 5204227d67aaSAlexander Motin } 5205227d67aaSAlexander Motin 5206227d67aaSAlexander Motin if ((ccb_h->flags & CAM_DEV_QFRZDIS) 5207227d67aaSAlexander Motin && (ccb_h->status & CAM_DEV_QFRZN)) { 5208d718926bSAlexander Motin xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE); 5209227d67aaSAlexander Motin ccb_h->status &= ~CAM_DEV_QFRZN; 5210227d67aaSAlexander Motin } 5211227d67aaSAlexander Motin 5212227d67aaSAlexander Motin devq = sim->devq; 52139deea857SKenneth D. Merry if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) { 5214227d67aaSAlexander Motin struct cam_ed *dev = ccb_h->path->device; 52158b8a9b1dSJustin T. Gibbs 5216227d67aaSAlexander Motin mtx_lock(&devq->send_mtx); 5217227d67aaSAlexander Motin devq->send_active--; 5218227d67aaSAlexander Motin devq->send_openings++; 52198b8a9b1dSJustin T. Gibbs cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h); 52208b8a9b1dSJustin T. Gibbs 5221b9c473b2SAlexander Motin if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 52228b8a9b1dSJustin T. Gibbs && (dev->ccbq.dev_active == 0))) { 5223b9c473b2SAlexander Motin dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY; 5224227d67aaSAlexander Motin xpt_release_devq_device(dev, /*count*/1, 5225b9c473b2SAlexander Motin /*run_queue*/FALSE); 5226b9c473b2SAlexander Motin } 5227b9c473b2SAlexander Motin 5228b9c473b2SAlexander Motin if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0 5229b9c473b2SAlexander Motin && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) { 5230b9c473b2SAlexander Motin dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 5231227d67aaSAlexander Motin xpt_release_devq_device(dev, /*count*/1, 523283c5d981SAlexander Motin /*run_queue*/FALSE); 52338b8a9b1dSJustin T. Gibbs } 52348b8a9b1dSJustin T. Gibbs 5235227d67aaSAlexander Motin if (!device_is_queued(dev)) 5236227d67aaSAlexander Motin (void)xpt_schedule_devq(devq, dev); 5237d718926bSAlexander Motin xpt_run_devq(devq); 5238227d67aaSAlexander Motin mtx_unlock(&devq->send_mtx); 5239227d67aaSAlexander Motin 5240227d67aaSAlexander Motin if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) { 5241227d67aaSAlexander Motin mtx = xpt_path_mtx(ccb_h->path); 5242227d67aaSAlexander Motin mtx_lock(mtx); 5243227d67aaSAlexander Motin 5244fd21cc5eSJustin T. Gibbs if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 5245fd21cc5eSJustin T. Gibbs && (--dev->tag_delay_count == 0)) 5246fd21cc5eSJustin T. Gibbs xpt_start_tags(ccb_h->path); 52473501942bSJustin T. Gibbs } 52488b8a9b1dSJustin T. Gibbs } 52498b8a9b1dSJustin T. Gibbs 5250227d67aaSAlexander Motin if ((ccb_h->flags & CAM_UNLOCKED) == 0) { 5251227d67aaSAlexander Motin if (mtx == NULL) { 5252227d67aaSAlexander Motin mtx = xpt_path_mtx(ccb_h->path); 5253227d67aaSAlexander Motin mtx_lock(mtx); 5254434bbf6eSJustin T. Gibbs } 5255227d67aaSAlexander Motin } else { 5256227d67aaSAlexander Motin if (mtx != NULL) { 5257227d67aaSAlexander Motin mtx_unlock(mtx); 5258227d67aaSAlexander Motin mtx = NULL; 5259227d67aaSAlexander Motin } 52608b8a9b1dSJustin T. Gibbs } 52618b8a9b1dSJustin T. Gibbs 52628b8a9b1dSJustin T. Gibbs /* Call the peripheral driver's callback */ 5263f1486b51SAlexander Motin ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 5264434bbf6eSJustin T. Gibbs (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h); 5265227d67aaSAlexander Motin if (mtx != NULL) 5266227d67aaSAlexander Motin mtx_unlock(mtx); 5267227d67aaSAlexander Motin } 5268227d67aaSAlexander Motin 5269227d67aaSAlexander Motin void 5270227d67aaSAlexander Motin xpt_done_td(void *arg) 5271227d67aaSAlexander Motin { 5272227d67aaSAlexander Motin struct cam_doneq *queue = arg; 5273227d67aaSAlexander Motin struct ccb_hdr *ccb_h; 5274227d67aaSAlexander Motin STAILQ_HEAD(, ccb_hdr) doneq; 5275227d67aaSAlexander Motin 5276227d67aaSAlexander Motin STAILQ_INIT(&doneq); 5277227d67aaSAlexander Motin mtx_lock(&queue->cam_doneq_mtx); 5278227d67aaSAlexander Motin while (1) { 5279227d67aaSAlexander Motin while (STAILQ_EMPTY(&queue->cam_doneq)) { 5280227d67aaSAlexander Motin queue->cam_doneq_sleep = 1; 5281227d67aaSAlexander Motin msleep(&queue->cam_doneq, &queue->cam_doneq_mtx, 5282227d67aaSAlexander Motin PRIBIO, "-", 0); 5283227d67aaSAlexander Motin queue->cam_doneq_sleep = 0; 5284227d67aaSAlexander Motin } 5285227d67aaSAlexander Motin STAILQ_CONCAT(&doneq, &queue->cam_doneq); 5286227d67aaSAlexander Motin mtx_unlock(&queue->cam_doneq_mtx); 5287227d67aaSAlexander Motin 5288227d67aaSAlexander Motin THREAD_NO_SLEEPING(); 5289227d67aaSAlexander Motin while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) { 5290227d67aaSAlexander Motin STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe); 5291227d67aaSAlexander Motin xpt_done_process(ccb_h); 5292227d67aaSAlexander Motin } 5293227d67aaSAlexander Motin THREAD_SLEEPING_OK(); 5294227d67aaSAlexander Motin 5295227d67aaSAlexander Motin mtx_lock(&queue->cam_doneq_mtx); 5296227d67aaSAlexander Motin } 5297227d67aaSAlexander Motin } 5298227d67aaSAlexander Motin 5299227d67aaSAlexander Motin static void 5300227d67aaSAlexander Motin camisr_runqueue(void) 5301227d67aaSAlexander Motin { 5302227d67aaSAlexander Motin struct ccb_hdr *ccb_h; 5303227d67aaSAlexander Motin struct cam_doneq *queue; 5304227d67aaSAlexander Motin int i; 5305227d67aaSAlexander Motin 5306227d67aaSAlexander Motin /* Process global queues. */ 5307227d67aaSAlexander Motin for (i = 0; i < cam_num_doneqs; i++) { 5308227d67aaSAlexander Motin queue = &cam_doneqs[i]; 5309227d67aaSAlexander Motin mtx_lock(&queue->cam_doneq_mtx); 5310227d67aaSAlexander Motin while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) { 5311227d67aaSAlexander Motin STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe); 5312227d67aaSAlexander Motin mtx_unlock(&queue->cam_doneq_mtx); 5313227d67aaSAlexander Motin xpt_done_process(ccb_h); 5314227d67aaSAlexander Motin mtx_lock(&queue->cam_doneq_mtx); 5315227d67aaSAlexander Motin } 5316227d67aaSAlexander Motin mtx_unlock(&queue->cam_doneq_mtx); 53178b8a9b1dSJustin T. Gibbs } 53188b8a9b1dSJustin T. Gibbs } 5319